diff --git a/clang/docs/ClangCommandLineReference.rst b/clang/docs/ClangCommandLineReference.rst --- a/clang/docs/ClangCommandLineReference.rst +++ b/clang/docs/ClangCommandLineReference.rst @@ -945,6 +945,10 @@ Inline functions which are (explicitly or implicitly) marked inline +.. option:: -finline-max-stacksize= + +Suppress inlining of functions with a stacksize larger than bytes. + .. option:: -fno-legacy-pass-manager, -fexperimental-new-pass-manager .. option:: -fno-sanitize-ignorelist, -fno-sanitize-blacklist diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -399,6 +399,9 @@ /// The kind of inlining to perform. ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining) +/// The maximum stack size a function can have to be considered for inlining. +VALUE_CODEGENOPT(InlineMaxStackSize, 32, UINT_MAX) + // Vector functions library to use. ENUM_CODEGENOPT(VecLib, VectorLibrary, 3, NoLibrary) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1985,6 +1985,11 @@ def finline_hint_functions: Flag<["-"], "finline-hint-functions">, Group, Flags<[CC1Option]>, HelpText<"Inline functions which are (explicitly or implicitly) marked inline">; def finline : Flag<["-"], "finline">, Group; +def finline_max_stacksize_EQ + : Joined<["-"], "finline-max-stacksize=">, + Group, Flags<[CoreOption, CC1Option]>, + HelpText<"Suppress inlining of functions whose stack size exceeds the given value">, + MarshallingInfoInt, "UINT_MAX">; defm jmc : BoolFOption<"jmc", CodeGenOpts<"JMCInstrument">, DefaultFalse, PosFlag, diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2372,6 +2372,9 @@ if (getLangOpts().OpenMP && FD->hasAttr()) getOpenMPRuntime().emitDeclareSimdFunction(FD, F); + if (CodeGenOpts.InlineMaxStackSize != UINT_MAX) + F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize)); + if (const auto *CB = FD->getAttr()) { // Annotate the callback behavior as metadata: // - The callback callee (as argument number). diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6592,6 +6592,8 @@ InlineArg->render(Args, CmdArgs); } + Args.AddLastArg(CmdArgs, options::OPT_finline_max_stacksize_EQ); + // FIXME: Find a better way to determine whether the language has modules // support by default, or just assume that all languages do. bool HaveModules = diff --git a/clang/test/CodeGen/inline-stacksize.c b/clang/test/CodeGen/inline-stacksize.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/inline-stacksize.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -O2 -emit-llvm %s -o - | FileCheck %s --check-prefixes NOOPT +// RUN: %clang_cc1 -O2 -finline-max-stacksize=64 -emit-llvm %s -o - | FileCheck %s --check-prefix OPT + +void foo() {} + +// NOOPT-NOT: inline-max-stacksize +// OPT: define {{.*}}@foo{{.*}}#[[ATTR:[0-9]+]] +// OPT: attributes #[[ATTR]] = {{.*}}"inline-max-stacksize"="64"