Skip to content

Commit 23cc611

Browse files
anchurajGeorgeARM
authored andcommitted
[flang][flang-driver] Support flag -finstrument-functions (llvm#137996)
1 parent 0b6c7b0 commit 23cc611

File tree

9 files changed

+53
-7
lines changed

9 files changed

+53
-7
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,10 +2824,12 @@ def finput_charset_EQ : Joined<["-"], "finput-charset=">,
28242824
Visibility<[ClangOption, FlangOption, FC1Option]>, Group<f_Group>,
28252825
HelpText<"Specify the default character set for source files">;
28262826
def fexec_charset_EQ : Joined<["-"], "fexec-charset=">, Group<f_Group>;
2827-
def finstrument_functions : Flag<["-"], "finstrument-functions">, Group<f_Group>,
2828-
Visibility<[ClangOption, CC1Option]>,
2829-
HelpText<"Generate calls to instrument function entry and exit">,
2830-
MarshallingInfoFlag<CodeGenOpts<"InstrumentFunctions">>;
2827+
def finstrument_functions
2828+
: Flag<["-"], "finstrument-functions">,
2829+
Group<f_Group>,
2830+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
2831+
HelpText<"Generate calls to instrument function entry and exit">,
2832+
MarshallingInfoFlag<CodeGenOpts<"InstrumentFunctions">>;
28312833
def finstrument_functions_after_inlining : Flag<["-"], "finstrument-functions-after-inlining">, Group<f_Group>,
28322834
Visibility<[ClangOption, CC1Option]>,
28332835
HelpText<"Like -finstrument-functions, but insert the calls after inlining">,

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
128128
options::OPT_std_EQ, options::OPT_W_Joined,
129129
options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ,
130130
options::OPT_funderscoring, options::OPT_fno_underscoring,
131-
options::OPT_funsigned, options::OPT_fno_unsigned});
131+
options::OPT_funsigned, options::OPT_fno_unsigned,
132+
options::OPT_finstrument_functions});
132133

133134
llvm::codegenoptions::DebugInfoKind DebugInfoKind;
134135
if (Args.hasArg(options::OPT_gN_Group)) {

flang/include/flang/Frontend/CodeGenOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
2424
CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
2525
///< pass manager.
2626

27+
CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is
28+
///< enabled on the compile step.
2729
CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
2830
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
2931
CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
393393
clEnumValN(mlir::LLVM::framePointerKind::FramePointerKind::All, "All", ""),
394394
clEnumValN(mlir::LLVM::framePointerKind::FramePointerKind::Reserved, "Reserved", "")
395395
)}]>,
396+
Option<"instrumentFunctionEntry", "instrument-function-entry",
397+
"std::string", /*default=*/"",
398+
"Sets the name of the profiling function called during function "
399+
"entry">,
400+
Option<"instrumentFunctionExit", "instrument-function-exit",
401+
"std::string", /*default=*/"",
402+
"Sets the name of the profiling function called during function "
403+
"exit">,
396404
Option<"noInfsFPMath", "no-infs-fp-math", "bool", /*default=*/"false",
397405
"Set the no-infs-fp-math attribute on functions in the module.">,
398406
Option<"noNaNsFPMath", "no-nans-fp-math", "bool", /*default=*/"false",

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
102102
UnsafeFPMath = mathOpts.getAssociativeMath() &&
103103
mathOpts.getReciprocalMath() && NoSignedZerosFPMath &&
104104
ApproxFuncFPMath && mathOpts.getFPContractEnabled();
105+
if (opts.InstrumentFunctions) {
106+
InstrumentFunctionEntry = "__cyg_profile_func_enter";
107+
InstrumentFunctionExit = "__cyg_profile_func_exit";
108+
}
105109
}
106110

107111
llvm::OptimizationLevel OptLevel; ///< optimisation level
108112
bool StackArrays = false; ///< convert memory allocations to alloca.
109113
bool Underscoring = true; ///< add underscores to function names.
110114
bool LoopVersioning = false; ///< Run the version loop pass.
111-
bool AliasAnalysis = false; ///< Add TBAA tags to generated LLVMIR
115+
bool AliasAnalysis = false; ///< Add TBAA tags to generated LLVMIR.
112116
llvm::codegenoptions::DebugInfoKind DebugInfo =
113117
llvm::codegenoptions::NoDebugInfo; ///< Debug info generation.
114118
llvm::FramePointerKind FramePointerKind =
@@ -124,6 +128,12 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
124128
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
125129
bool NSWOnLoopVarInc = true; ///< Add nsw flag to loop variable increments.
126130
bool EnableOpenMP = false; ///< Enable OpenMP lowering.
131+
std::string InstrumentFunctionEntry =
132+
""; ///< Name of the instrument-function that is called on each
133+
///< function-entry
134+
std::string InstrumentFunctionExit =
135+
""; ///< Name of the instrument-function that is called on each
136+
///< function-exit
127137
};
128138

129139
struct OffloadModuleOpts {

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
310310
args.filtered(clang::driver::options::OPT_fembed_offload_object_EQ))
311311
opts.OffloadObjects.push_back(a->getValue());
312312

313+
if (args.hasArg(clang::driver::options::OPT_finstrument_functions))
314+
opts.InstrumentFunctions = 1;
315+
313316
// -flto=full/thin option.
314317
if (const llvm::opt::Arg *a =
315318
args.getLastArg(clang::driver::options::OPT_flto_EQ)) {

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
349349
framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::None;
350350

351351
pm.addPass(fir::createFunctionAttr(
352-
{framePointerKind, config.NoInfsFPMath, config.NoNaNsFPMath,
352+
{framePointerKind, config.InstrumentFunctionEntry,
353+
config.InstrumentFunctionExit, config.NoInfsFPMath, config.NoNaNsFPMath,
353354
config.ApproxFuncFPMath, config.NoSignedZerosFPMath, config.UnsafeFPMath,
354355
""}));
355356

flang/lib/Optimizer/Transforms/FunctionAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace {
2828
class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> {
2929
public:
3030
FunctionAttrPass(const fir::FunctionAttrOptions &options) {
31+
instrumentFunctionEntry = options.instrumentFunctionEntry;
32+
instrumentFunctionExit = options.instrumentFunctionExit;
3133
framePointerKind = options.framePointerKind;
3234
noInfsFPMath = options.noInfsFPMath;
3335
noNaNsFPMath = options.noNaNsFPMath;
@@ -72,6 +74,14 @@ void FunctionAttrPass::runOnOperation() {
7274

7375
auto llvmFuncOpName =
7476
mlir::OperationName(mlir::LLVM::LLVMFuncOp::getOperationName(), context);
77+
if (!instrumentFunctionEntry.empty())
78+
func->setAttr(mlir::LLVM::LLVMFuncOp::getInstrumentFunctionEntryAttrName(
79+
llvmFuncOpName),
80+
mlir::StringAttr::get(context, instrumentFunctionEntry));
81+
if (!instrumentFunctionExit.empty())
82+
func->setAttr(mlir::LLVM::LLVMFuncOp::getInstrumentFunctionExitAttrName(
83+
llvmFuncOpName),
84+
mlir::StringAttr::get(context, instrumentFunctionExit));
7585
if (noInfsFPMath)
7686
func->setAttr(
7787
mlir::LLVM::LLVMFuncOp::getNoInfsFpMathAttrName(llvmFuncOpName),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! RUN: %flang -O1 -finstrument-functions -emit-llvm -S -o - %s 2>&1| FileCheck %s
2+
3+
subroutine func
4+
end subroutine func
5+
6+
! CHECK: define void @func_()
7+
! CHECK: {{.*}}call void @__cyg_profile_func_enter(ptr {{.*}}@func_, ptr {{.*}})
8+
! CHECK: {{.*}}call void @__cyg_profile_func_exit(ptr {{.*}}@func_, ptr {{.*}})
9+
! CHECK-NEXT: ret {{.*}}

0 commit comments

Comments
 (0)