jlegare created this revision. Herald added a project: All. jlegare requested review of this revision. Herald added subscribers: cfe-commits, MaskRay. Herald added a project: clang.
Two options have been added: -finstrument-functions-exclude-function-list and -finstrument-functions-exclude-file-list. The first can be used to exclude functions by name, and the second can be used to exclude a function by the file name where its definition occurs. Both options take a comma-separated list of values as their argument. By-name exclusion is done on the demangled name. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130161 Files: clang/docs/ClangCommandLineReference.rst clang/include/clang/Basic/CodeGenOptions.h clang/include/clang/Driver/Options.td clang/lib/CodeGen/CodeGenFunction.cpp clang/lib/CodeGen/CodeGenFunction.h clang/lib/Driver/ToolChains/Clang.cpp
Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -5628,6 +5628,8 @@ Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, options::OPT_finstrument_functions_after_inlining, options::OPT_finstrument_function_entry_bare); + Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions_exclude_function_list); + Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions_exclude_file_list); // NVPTX/AMDGCN doesn't support PGO or coverage. There's no runtime support // for sampling, overhead of call arc collection is way too high and there's Index: clang/lib/CodeGen/CodeGenFunction.h =================================================================== --- clang/lib/CodeGen/CodeGenFunction.h +++ clang/lib/CodeGen/CodeGenFunction.h @@ -2338,7 +2338,7 @@ /// ShouldInstrumentFunction - Return true if the current function should be /// instrumented with __cyg_profile_func_* calls - bool ShouldInstrumentFunction(); + bool ShouldInstrumentFunction(llvm::Function *F); /// ShouldSkipSanitizerInstrumentation - Return true if the current function /// should not be instrumented with sanitizers. Index: clang/lib/CodeGen/CodeGenFunction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenFunction.cpp +++ clang/lib/CodeGen/CodeGenFunction.cpp @@ -375,7 +375,7 @@ // Emit function epilog (to return). llvm::DebugLoc Loc = EmitReturnBlock(); - if (ShouldInstrumentFunction()) { + if (ShouldInstrumentFunction(CurFn)) { if (CGM.getCodeGenOpts().InstrumentFunctions) CurFn->addFnAttr("instrument-function-exit", "__cyg_profile_func_exit"); if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) @@ -521,13 +521,40 @@ /// ShouldInstrumentFunction - Return true if the current function should be /// instrumented with __cyg_profile_func_* calls -bool CodeGenFunction::ShouldInstrumentFunction() { +bool CodeGenFunction::ShouldInstrumentFunction(llvm::Function *F) { if (!CGM.getCodeGenOpts().InstrumentFunctions && !CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining && !CGM.getCodeGenOpts().InstrumentFunctionEntryBare) return false; if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) return false; + + // Downcast to a function declaration and check to see if the current function + // is inlined. If it is inlined but not externally-visible, then don't + // generate tracing for it: at link-time, the function will be gone and the + // first argument to the __cyg_profile_* function will be unresolvable. + const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(CurFuncDecl); + if (FDecl && + FDecl->isInlined() && + !FDecl->isInlineDefinitionExternallyVisible()) + return false; + + typedef std::vector<std::string>::const_iterator ExcludedFunctionIterator; + + // The list of excluded function name substrings specified on the command-line + // is searched to see if any of them is a substring of our (current) function + // name. + const std::vector<std::string> &ExcludedFunctions = + CGM.getCodeGenOpts().InstrumentFunctionsExcludedFunctions; + + std::string FName(F->getName()); + for (ExcludedFunctionIterator ExcludedFunction = ExcludedFunctions.begin(), + Sentinel = ExcludedFunctions.end(); + ExcludedFunction != Sentinel; ++ExcludedFunction) { + if (FName.find(*ExcludedFunction) != std::string::npos) + return false; + } + return true; } @@ -1018,7 +1045,7 @@ CurFuncIsThunk); } - if (ShouldInstrumentFunction()) { + if (ShouldInstrumentFunction(CurFn)) { if (CGM.getCodeGenOpts().InstrumentFunctions) CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter"); if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2010,6 +2010,13 @@ def finstrument_function_entry_bare : Flag<["-"], "finstrument-function-entry-bare">, Group<f_Group>, Flags<[CC1Option]>, HelpText<"Instrument function entry only, after inlining, without arguments to the instrumentation call">, MarshallingInfoFlag<CodeGenOpts<"InstrumentFunctionEntryBare">>; +def finstrument_functions_exclude_function_list : CommaJoined<["-"], + "finstrument-functions-exclude-function-list=">, Group<f_Group>, Flags<[CC1Option]>, + HelpText<"Exclude the listed functions from function instrumentation.">, + MarshallingInfoStringVector<CodeGenOpts<"InstrumentFunctionsExcludedFunctions">>; +def finstrument_functions_exclude_file_list : CommaJoined<["-"], + "finstrument-functions-exclude-file-list=">, Group<f_Group>, Flags<[CC1Option]>, + HelpText<"Exclude the functions defined in the given files from function instrumentation.">; def fcf_protection_EQ : Joined<["-"], "fcf-protection=">, Flags<[CoreOption, CC1Option]>, Group<f_Group>, HelpText<"Instrument control-flow architecture protection">, Values<"return,branch,full,none">; def fcf_protection : Flag<["-"], "fcf-protection">, Group<f_Group>, Flags<[CoreOption, CC1Option]>, Index: clang/include/clang/Basic/CodeGenOptions.h =================================================================== --- clang/include/clang/Basic/CodeGenOptions.h +++ clang/include/clang/Basic/CodeGenOptions.h @@ -426,6 +426,12 @@ /// values in order to be included in misexpect diagnostics. Optional<uint64_t> DiagnosticsMisExpectTolerance = 0; + /// List of functions excluded from function instrumentation. + std::vector<std::string> InstrumentFunctionsExcludedFunctions; + + /// List of paths containing excluded function definitions. + std::vector<std::string> InstrumentFunctionsExcludedPaths; + public: // Define accessors/mutators for code generation options of enumeration type. #define CODEGENOPT(Name, Bits, Default) Index: clang/docs/ClangCommandLineReference.rst =================================================================== --- clang/docs/ClangCommandLineReference.rst +++ clang/docs/ClangCommandLineReference.rst @@ -1917,6 +1917,14 @@ Like -finstrument-functions, but insert the calls after inlining +.. option:: -finstrument-functions-exclude-file-list=<path1,path2,...> + +Exclude from instrumentation any function pulled in from the specified path(s) + +.. option:: -finstrument-functions-exclude-function-list=<fun1,fun2,...> + +Exclude from instrumentation the specified function(s) + .. option:: -fintegrated-as, -fno-integrated-as, -integrated-as Enable the integrated assembler
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits