Index: clang/docs/ClangCommandLineReference.rst =================================================================== --- clang/docs/ClangCommandLineReference.rst +++ 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 Index: clang/include/clang/Basic/CodeGenOptions.def =================================================================== --- clang/include/clang/Basic/CodeGenOptions.def +++ 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) Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ 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, Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ 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). Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ 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 = Index: clang/test/CodeGen/inline-stacksize.c =================================================================== --- /dev/null +++ clang/test/CodeGen/inline-stacksize.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -O2 -finline-max-stacksize=64 -emit-llvm %s -o - | FileCheck %s + +void foo() {} + +// CHECK: define {{.*}}@foo{{.*}}#[[ATTR:[0-9]+]] +// CHECK: attributes #[[ATTR]] = {{.*}}"inline-max-stacksize"="64"