From 47fee8cd36906b4b203e9a0bf2fe95e17b7150f1 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Mon, 29 Apr 2024 20:59:03 +0200 Subject: [PATCH] Add --template-codegen-depth to stop very deep template instantiations. --- dmd/dtemplate.d | 7 +++++++ dmd/globals.d | 1 + dmd/globals.h | 1 + driver/cl_options.cpp | 5 +++++ 4 files changed, 14 insertions(+) diff --git a/dmd/dtemplate.d b/dmd/dtemplate.d index 1f277bf41fe..68c29a34978 100644 --- a/dmd/dtemplate.d +++ b/dmd/dtemplate.d @@ -4136,6 +4136,13 @@ extern (C++) class TemplateInstance : ScopeDsymbol version (IN_LLVM) { assert(global.params.linkonceTemplates != LinkonceTemplates.aggressive); + + static uint callDepth; + callDepth++; + scope(exit) callDepth--; + + if (global.params.templateCodegenDepth && (callDepth > global.params.templateCodegenDepth)) + return false; } //printf("needsCodegen() %s\n", toChars()); diff --git a/dmd/globals.d b/dmd/globals.d index 88c52e012c4..82341fc8f89 100644 --- a/dmd/globals.d +++ b/dmd/globals.d @@ -326,6 +326,7 @@ version (IN_LLVM) bool outputSourceLocations; // if true, output line tables. LinkonceTemplates linkonceTemplates; // -linkonce-templates + uint templateCodegenDepth; // -template-codegen-depth // Windows-specific: bool dllexport; // dllexport ~all defined symbols? diff --git a/dmd/globals.h b/dmd/globals.h index ee89ee6afdf..c577793c7f8 100644 --- a/dmd/globals.h +++ b/dmd/globals.h @@ -305,6 +305,7 @@ struct Param bool outputSourceLocations; // if true, output line tables. LinkonceTemplates linkonceTemplates; // -linkonce-templates + uint32_t templateCodegenDepth; // -template-codegen-depth // Windows-specific: bool dllexport; // dllexport ~all defined symbols? diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index 4cec62a46ba..4414a86171f 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -526,6 +526,11 @@ static cl::opt linkonceTemplates( "linkonce-templates-aggressive", "Experimental, more aggressive variant"))); +static cl::opt + templateCodegenDepth("template-codegen-depth", + cl::desc("Don't codegen templates beyond this instantiation depth (0 = off)."), + cl::location(global.params.templateCodegenDepth), cl::init(0)); + cl::opt disableLinkerStripDead( "disable-linker-strip-dead", cl::ZeroOrMore, cl::desc("Do not try to remove unused symbols during linking"),