Skip to content

Commit d77ea79

Browse files
committed
Adding instrument function entry and instrument function exit attributes
1 parent a557550 commit d77ea79

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
18261826
OptionalAttr<StrAttr>:$denormal_fp_math,
18271827
OptionalAttr<StrAttr>:$denormal_fp_math_f32,
18281828
OptionalAttr<StrAttr>:$fp_contract,
1829+
OptionalAttr<StrAttr>:$instrument_function_entry,
1830+
OptionalAttr<StrAttr>:$instrument_function_exit,
18291831
OptionalAttr<UnitAttr>:$no_inline,
18301832
OptionalAttr<UnitAttr>:$always_inline,
18311833
OptionalAttr<UnitAttr>:$no_unwind,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,8 @@ static constexpr std::array kExplicitAttributes{
20662066
StringLiteral("denormal-fp-math-f32"),
20672067
StringLiteral("fp-contract"),
20682068
StringLiteral("frame-pointer"),
2069+
StringLiteral("instrument-function-entry"),
2070+
StringLiteral("instrument-function-exit"),
20692071
StringLiteral("no-infs-fp-math"),
20702072
StringLiteral("no-nans-fp-math"),
20712073
StringLiteral("no-signed-zeros-fp-math"),
@@ -2220,6 +2222,16 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
22202222
attr.isStringAttribute())
22212223
funcOp.setApproxFuncFpMath(attr.getValueAsBool());
22222224

2225+
if (llvm::Attribute attr = func->getFnAttribute("instrument-function-entry");
2226+
attr.isStringAttribute())
2227+
funcOp.setInstrumentFunctionEntry(
2228+
StringAttr::get(context, attr.getValueAsString()));
2229+
2230+
if (llvm::Attribute attr = func->getFnAttribute("instrument-function-exit");
2231+
attr.isStringAttribute())
2232+
funcOp.setInstrumentFunctionExit(
2233+
StringAttr::get(context, attr.getValueAsString()));
2234+
22232235
if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
22242236
attr.isStringAttribute())
22252237
funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,12 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
15251525
if (auto fpContract = func.getFpContract())
15261526
llvmFunc->addFnAttr("fp-contract", *fpContract);
15271527

1528+
if (auto instrumentFunctionEntry = func.getInstrumentFunctionEntry())
1529+
llvmFunc->addFnAttr("instrument-function-entry", *instrumentFunctionEntry);
1530+
1531+
if (auto instrumentFunctionExit = func.getInstrumentFunctionExit())
1532+
llvmFunc->addFnAttr("instrument-function-exit", *instrumentFunctionExit);
1533+
15281534
// First, create all blocks so we can jump to them.
15291535
llvm::LLVMContext &llvmContext = llvmFunc->getContext();
15301536
for (auto &bb : func) {

mlir/test/Dialect/LLVMIR/func.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ module {
312312
llvm.return
313313
}
314314

315+
llvm.func @instrument_function_entry_function() attributes {instrument_function_entry = "__cyg_profile_func_enter"} {
316+
// CHECK: @instrument_function_entry_function
317+
// CHECK-SAME: attributes {instrument_function_entry = "__cyg_profile_func_enter"}
318+
llvm.return
319+
}
320+
321+
llvm.func @instrument_function_exit_function() attributes {instrument_function_exit = "__cyg_profile_func_exit"} {
322+
// CHECK: @instrument_function_exit_function
323+
// CHECK-SAME: attributes {instrument_function_exit = "__cyg_profile_func_exit"}
324+
llvm.return
325+
}
326+
315327
llvm.func @nounwind_function() attributes {no_unwind} {
316328
// CHECK: @nounwind_function
317329
// CHECK-SAME: attributes {no_unwind}

mlir/test/Target/LLVMIR/Import/function-attributes.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,18 @@ declare void @func_attr_fp_contract_fast() "fp-contract"="fast"
381381

382382
// -----
383383

384+
; CHECK-LABEL: @func_attr_instrument_function_entry
385+
; CHECK-SAME: attributes {instrument_function_entry = "__cyg_profile_func_enter"}
386+
declare void @func_attr_instrument_function_entry() "instrument-function-entry"="__cyg_profile_func_enter"
387+
388+
// -----
389+
390+
; CHECK-LABEL: @func_attr_instrument_function_exit
391+
; CHECK-SAME: attributes {instrument_function_exit = "__cyg_profile_func_exit"}
392+
declare void @func_attr_instrument_function_exit() "instrument-function-exit"="__cyg_profile_func_exit"
393+
394+
// -----
395+
384396
; CHECK-LABEL: @noinline_attribute
385397
; CHECK-SAME: attributes {no_inline}
386398
declare void @noinline_attribute() noinline

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,24 @@ llvm.func @convergent() attributes { convergent } {
25592559

25602560
// -----
25612561

2562+
// CHECK-LABEL: define void @function_entry_instrument_test()
2563+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
2564+
llvm.func @function_entry_instrument_test() attributes {instrument_function_entry = "__cyg_profile_func_enter"} {
2565+
llvm.return
2566+
}
2567+
// CHECK: attributes #[[ATTRS]] = { "instrument-function-entry"="__cyg_profile_func_enter" }
2568+
2569+
// -----
2570+
2571+
// CHECK-LABEL: define void @function_exit_instrument_test()
2572+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
2573+
llvm.func @function_exit_instrument_test() attributes {instrument_function_exit = "__cyg_profile_func_exit"} {
2574+
llvm.return
2575+
}
2576+
// CHECK: attributes #[[ATTRS]] = { "instrument-function-exit"="__cyg_profile_func_exit" }
2577+
2578+
// -----
2579+
25622580
// CHECK-LABEL: @nounwind
25632581
// CHECK-SAME: #[[ATTRS:[0-9]+]]
25642582
llvm.func @nounwind() attributes { no_unwind } {

0 commit comments

Comments
 (0)