aboutsummaryrefslogtreecommitdiff
path: root/flang
diff options
context:
space:
mode:
Diffstat (limited to 'flang')
-rw-r--r--flang/include/flang/Frontend/CodeGenOptions.def2
-rw-r--r--flang/include/flang/Optimizer/Transforms/Passes.td8
-rw-r--r--flang/include/flang/Tools/CrossToolHelpers.h12
-rw-r--r--flang/lib/Frontend/CompilerInvocation.cpp3
-rw-r--r--flang/lib/Optimizer/Passes/Pipelines.cpp3
-rw-r--r--flang/lib/Optimizer/Transforms/FunctionAttr.cpp10
-rw-r--r--flang/test/Driver/func-attr-instrument-functions.f909
7 files changed, 45 insertions, 2 deletions
diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def
index 57830bf..d9dbd27 100644
--- a/flang/include/flang/Frontend/CodeGenOptions.def
+++ b/flang/include/flang/Frontend/CodeGenOptions.def
@@ -24,6 +24,8 @@ CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified.
CODEGENOPT(DebugPassManager, 1, 0) ///< Prints debug information for the new
///< pass manager.
+CODEGENOPT(InstrumentFunctions, 1, 0) ///< Set when -finstrument_functions is
+ ///< enabled on the compile step.
CODEGENOPT(IsPIE, 1, 0) ///< PIE level is the same as PIC Level.
CODEGENOPT(PICLevel, 2, 0) ///< PIC level of the LLVM module.
CODEGENOPT(PrepareForFullLTO , 1, 0) ///< Set when -flto is enabled on the
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index c59416f..9b6919e 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -393,6 +393,14 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
clEnumValN(mlir::LLVM::framePointerKind::FramePointerKind::All, "All", ""),
clEnumValN(mlir::LLVM::framePointerKind::FramePointerKind::Reserved, "Reserved", "")
)}]>,
+ Option<"instrumentFunctionEntry", "instrument-function-entry",
+ "std::string", /*default=*/"",
+ "Sets the name of the profiling function called during function "
+ "entry">,
+ Option<"instrumentFunctionExit", "instrument-function-exit",
+ "std::string", /*default=*/"",
+ "Sets the name of the profiling function called during function "
+ "exit">,
Option<"noInfsFPMath", "no-infs-fp-math", "bool", /*default=*/"false",
"Set the no-infs-fp-math attribute on functions in the module.">,
Option<"noNaNsFPMath", "no-nans-fp-math", "bool", /*default=*/"false",
diff --git a/flang/include/flang/Tools/CrossToolHelpers.h b/flang/include/flang/Tools/CrossToolHelpers.h
index 1dbc18e..118695b 100644
--- a/flang/include/flang/Tools/CrossToolHelpers.h
+++ b/flang/include/flang/Tools/CrossToolHelpers.h
@@ -102,13 +102,17 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
UnsafeFPMath = mathOpts.getAssociativeMath() &&
mathOpts.getReciprocalMath() && NoSignedZerosFPMath &&
ApproxFuncFPMath && mathOpts.getFPContractEnabled();
+ if (opts.InstrumentFunctions) {
+ InstrumentFunctionEntry = "__cyg_profile_func_enter";
+ InstrumentFunctionExit = "__cyg_profile_func_exit";
+ }
}
llvm::OptimizationLevel OptLevel; ///< optimisation level
bool StackArrays = false; ///< convert memory allocations to alloca.
bool Underscoring = true; ///< add underscores to function names.
bool LoopVersioning = false; ///< Run the version loop pass.
- bool AliasAnalysis = false; ///< Add TBAA tags to generated LLVMIR
+ bool AliasAnalysis = false; ///< Add TBAA tags to generated LLVMIR.
llvm::codegenoptions::DebugInfoKind DebugInfo =
llvm::codegenoptions::NoDebugInfo; ///< Debug info generation.
llvm::FramePointerKind FramePointerKind =
@@ -124,6 +128,12 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
bool NSWOnLoopVarInc = true; ///< Add nsw flag to loop variable increments.
bool EnableOpenMP = false; ///< Enable OpenMP lowering.
+ std::string InstrumentFunctionEntry =
+ ""; ///< Name of the instrument-function that is called on each
+ ///< function-entry
+ std::string InstrumentFunctionExit =
+ ""; ///< Name of the instrument-function that is called on each
+ ///< function-exit
};
struct OffloadModuleOpts {
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 6f87a18..d6ba644 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -310,6 +310,9 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
args.filtered(clang::driver::options::OPT_fembed_offload_object_EQ))
opts.OffloadObjects.push_back(a->getValue());
+ if (args.hasArg(clang::driver::options::OPT_finstrument_functions))
+ opts.InstrumentFunctions = 1;
+
// -flto=full/thin option.
if (const llvm::opt::Arg *a =
args.getLastArg(clang::driver::options::OPT_flto_EQ)) {
diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index 130cbe7..a3ef473 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -349,7 +349,8 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
framePointerKind = mlir::LLVM::framePointerKind::FramePointerKind::None;
pm.addPass(fir::createFunctionAttr(
- {framePointerKind, config.NoInfsFPMath, config.NoNaNsFPMath,
+ {framePointerKind, config.InstrumentFunctionEntry,
+ config.InstrumentFunctionExit, config.NoInfsFPMath, config.NoNaNsFPMath,
config.ApproxFuncFPMath, config.NoSignedZerosFPMath, config.UnsafeFPMath,
""}));
diff --git a/flang/lib/Optimizer/Transforms/FunctionAttr.cpp b/flang/lib/Optimizer/Transforms/FunctionAttr.cpp
index c79843f..43e4c1a7af 100644
--- a/flang/lib/Optimizer/Transforms/FunctionAttr.cpp
+++ b/flang/lib/Optimizer/Transforms/FunctionAttr.cpp
@@ -28,6 +28,8 @@ namespace {
class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> {
public:
FunctionAttrPass(const fir::FunctionAttrOptions &options) {
+ instrumentFunctionEntry = options.instrumentFunctionEntry;
+ instrumentFunctionExit = options.instrumentFunctionExit;
framePointerKind = options.framePointerKind;
noInfsFPMath = options.noInfsFPMath;
noNaNsFPMath = options.noNaNsFPMath;
@@ -72,6 +74,14 @@ void FunctionAttrPass::runOnOperation() {
auto llvmFuncOpName =
mlir::OperationName(mlir::LLVM::LLVMFuncOp::getOperationName(), context);
+ if (!instrumentFunctionEntry.empty())
+ func->setAttr(mlir::LLVM::LLVMFuncOp::getInstrumentFunctionEntryAttrName(
+ llvmFuncOpName),
+ mlir::StringAttr::get(context, instrumentFunctionEntry));
+ if (!instrumentFunctionExit.empty())
+ func->setAttr(mlir::LLVM::LLVMFuncOp::getInstrumentFunctionExitAttrName(
+ llvmFuncOpName),
+ mlir::StringAttr::get(context, instrumentFunctionExit));
if (noInfsFPMath)
func->setAttr(
mlir::LLVM::LLVMFuncOp::getNoInfsFpMathAttrName(llvmFuncOpName),
diff --git a/flang/test/Driver/func-attr-instrument-functions.f90 b/flang/test/Driver/func-attr-instrument-functions.f90
new file mode 100644
index 0000000..0ef8180
--- /dev/null
+++ b/flang/test/Driver/func-attr-instrument-functions.f90
@@ -0,0 +1,9 @@
+! RUN: %flang -O1 -finstrument-functions -emit-llvm -S -o - %s 2>&1| FileCheck %s
+
+subroutine func
+end subroutine func
+
+! CHECK: define void @func_()
+! CHECK: {{.*}}call void @__cyg_profile_func_enter(ptr {{.*}}@func_, ptr {{.*}})
+! CHECK: {{.*}}call void @__cyg_profile_func_exit(ptr {{.*}}@func_, ptr {{.*}})
+! CHECK-NEXT: ret {{.*}}