Skip to content

Commit 9764938

Browse files
authored
[llvm][mlir] Adding instrument function entry and instrument function exit attributes (#137856)
1 parent e66eb83 commit 9764938

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
19021902
OptionalAttr<StrAttr>:$denormal_fp_math,
19031903
OptionalAttr<StrAttr>:$denormal_fp_math_f32,
19041904
OptionalAttr<StrAttr>:$fp_contract,
1905+
OptionalAttr<StrAttr>:$instrument_function_entry,
1906+
OptionalAttr<StrAttr>:$instrument_function_exit,
19051907
OptionalAttr<UnitAttr>:$no_inline,
19061908
OptionalAttr<UnitAttr>:$always_inline,
19071909
OptionalAttr<UnitAttr>:$no_unwind,

mlir/lib/Target/LLVMIR/ModuleImport.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -2147,6 +2147,8 @@ static constexpr std::array kExplicitAttributes{
21472147
StringLiteral("denormal-fp-math-f32"),
21482148
StringLiteral("fp-contract"),
21492149
StringLiteral("frame-pointer"),
2150+
StringLiteral("instrument-function-entry"),
2151+
StringLiteral("instrument-function-exit"),
21502152
StringLiteral("no-infs-fp-math"),
21512153
StringLiteral("no-nans-fp-math"),
21522154
StringLiteral("no-signed-zeros-fp-math"),
@@ -2302,6 +2304,16 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
23022304
attr.isStringAttribute())
23032305
funcOp.setApproxFuncFpMath(attr.getValueAsBool());
23042306

2307+
if (llvm::Attribute attr = func->getFnAttribute("instrument-function-entry");
2308+
attr.isStringAttribute())
2309+
funcOp.setInstrumentFunctionEntry(
2310+
StringAttr::get(context, attr.getValueAsString()));
2311+
2312+
if (llvm::Attribute attr = func->getFnAttribute("instrument-function-exit");
2313+
attr.isStringAttribute())
2314+
funcOp.setInstrumentFunctionExit(
2315+
StringAttr::get(context, attr.getValueAsString()));
2316+
23052317
if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
23062318
attr.isStringAttribute())
23072319
funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,12 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
15331533
if (auto fpContract = func.getFpContract())
15341534
llvmFunc->addFnAttr("fp-contract", *fpContract);
15351535

1536+
if (auto instrumentFunctionEntry = func.getInstrumentFunctionEntry())
1537+
llvmFunc->addFnAttr("instrument-function-entry", *instrumentFunctionEntry);
1538+
1539+
if (auto instrumentFunctionExit = func.getInstrumentFunctionExit())
1540+
llvmFunc->addFnAttr("instrument-function-exit", *instrumentFunctionExit);
1541+
15361542
// First, create all blocks so we can jump to them.
15371543
llvm::LLVMContext &llvmContext = llvmFunc->getContext();
15381544
for (auto &bb : func) {

mlir/test/Dialect/LLVMIR/func.mlir

+12
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

+12
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

+18
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,24 @@ llvm.func @convergent() attributes { convergent } {
25672567

25682568
// -----
25692569

2570+
// CHECK-LABEL: define void @function_entry_instrument_test()
2571+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
2572+
llvm.func @function_entry_instrument_test() attributes {instrument_function_entry = "__cyg_profile_func_enter"} {
2573+
llvm.return
2574+
}
2575+
// CHECK: attributes #[[ATTRS]] = { "instrument-function-entry"="__cyg_profile_func_enter" }
2576+
2577+
// -----
2578+
2579+
// CHECK-LABEL: define void @function_exit_instrument_test()
2580+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
2581+
llvm.func @function_exit_instrument_test() attributes {instrument_function_exit = "__cyg_profile_func_exit"} {
2582+
llvm.return
2583+
}
2584+
// CHECK: attributes #[[ATTRS]] = { "instrument-function-exit"="__cyg_profile_func_exit" }
2585+
2586+
// -----
2587+
25702588
// CHECK-LABEL: @nounwind
25712589
// CHECK-SAME: #[[ATTRS:[0-9]+]]
25722590
llvm.func @nounwind() attributes { no_unwind } {

0 commit comments

Comments
 (0)