Skip to content

Commit 3257e4c

Browse files
Radu2kzero9178Dinistro
authored
[MLIR] Add support for frame pointers in MLIR (#72145)
Add support for frame pointers in MLIR. --------- Co-authored-by: Markus Böck <[email protected]> Co-authored-by: Christian Ulmann <[email protected]>
1 parent 900bb31 commit 3257e4c

File tree

8 files changed

+69
-2
lines changed

8 files changed

+69
-2
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def LinkageAttr : LLVM_Attr<"Linkage", "linkage"> {
4848
let assemblyFormat = "`<` $linkage `>`";
4949
}
5050

51+
//===----------------------------------------------------------------------===//
52+
// FramePointerKindAttr
53+
//===----------------------------------------------------------------------===//
54+
55+
def FramePointerKindAttr : LLVM_Attr<"FramePointerKind", "framePointerKind"> {
56+
let parameters = (ins "framePointerKind::FramePointerKind":$framePointerKind);
57+
let assemblyFormat = "`<` $framePointerKind `>`";
58+
}
59+
5160
//===----------------------------------------------------------------------===//
5261
// Loop Attributes
5362
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ def Linkage : DialectAttr<
587587
"::mlir::LLVM::LinkageAttr::get($_builder.getContext(), $0)";
588588
}
589589

590-
591590
//===----------------------------------------------------------------------===//
592591
// Comdat
593592
//===----------------------------------------------------------------------===//
@@ -664,4 +663,23 @@ def ModRefInfoEnum : LLVM_EnumAttr<
664663
let cppNamespace = "::mlir::LLVM";
665664
}
666665

666+
//===----------------------------------------------------------------------===//
667+
// FramePointerKind
668+
//===----------------------------------------------------------------------===//
669+
670+
def FramePointerKindNone
671+
: LLVM_EnumAttrCase<"None", "none", "None", 0>;
672+
def FramePointerKindNonLeaf
673+
: LLVM_EnumAttrCase<"NonLeaf", "non-leaf", "NonLeaf", 1>;
674+
def FramePointerKindAll
675+
: LLVM_EnumAttrCase<"All", "all", "All", 2>;
676+
677+
def FramePointerKindEnum : LLVM_EnumAttr<
678+
"FramePointerKind",
679+
"::llvm::FramePointerKind",
680+
"LLVM FramePointerKind",
681+
[FramePointerKindNone, FramePointerKindNonLeaf, FramePointerKindAll]> {
682+
let cppNamespace = "::mlir::LLVM::framePointerKind";
683+
}
684+
667685
#endif // LLVMIR_ENUMS

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,8 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
13931393
OptionalAttr<StrAttr>:$section,
13941394
OptionalAttr<UnnamedAddr>:$unnamed_addr,
13951395
OptionalAttr<I64Attr>:$alignment,
1396-
OptionalAttr<LLVM_VScaleRangeAttr>:$vscale_range
1396+
OptionalAttr<LLVM_VScaleRangeAttr>:$vscale_range,
1397+
OptionalAttr<FramePointerKindAttr>:$frame_pointer
13971398
);
13981399

13991400
let regions = (region AnyRegion:$body);

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,7 @@ static constexpr std::array ExplicitAttributes{
16261626
StringLiteral("aarch64_pstate_sm_body"),
16271627
StringLiteral("aarch64_pstate_za_new"),
16281628
StringLiteral("vscale_range"),
1629+
StringLiteral("frame-pointer"),
16291630
};
16301631

16311632
static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp) {
@@ -1706,6 +1707,16 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
17061707
context, IntegerAttr::get(intTy, attr.getVScaleRangeMin()),
17071708
IntegerAttr::get(intTy, attr.getVScaleRangeMax().value_or(0))));
17081709
}
1710+
1711+
// Process frame-pointer attribute.
1712+
if (func->hasFnAttribute("frame-pointer")) {
1713+
StringRef stringRefFramePointerKind =
1714+
func->getFnAttribute("frame-pointer").getValueAsString();
1715+
funcOp.setFramePointerAttr(LLVM::FramePointerKindAttr::get(
1716+
funcOp.getContext(), LLVM::framePointerKind::symbolizeFramePointerKind(
1717+
stringRefFramePointerKind)
1718+
.value()));
1719+
}
17091720
}
17101721

17111722
DictionaryAttr

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,12 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
973973
getLLVMContext(), attr->getMinRange().getInt(),
974974
attr->getMaxRange().getInt()));
975975

976+
// Add function attribute frame-pointer, if found.
977+
if (FramePointerKindAttr attr = func.getFramePointerAttr())
978+
llvmFunc->addFnAttr("frame-pointer",
979+
LLVM::framePointerKind::stringifyFramePointerKind(
980+
(attr.getFramePointerKind())));
981+
976982
// First, create all blocks so we can jump to them.
977983
llvm::LLVMContext &llvmContext = llvmFunc->getContext();
978984
for (auto &bb : func) {

mlir/test/Dialect/LLVMIR/func.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ module {
249249
// CHECK-SAME: vscale_range(1, 2)
250250
llvm.return
251251
}
252+
253+
// CHECK-LABEL: @frame_pointer_roundtrip()
254+
// CHECK-SAME: attributes {frame_pointer = #llvm.framePointerKind<"non-leaf">}
255+
llvm.func @frame_pointer_roundtrip() attributes {frame_pointer = #llvm.framePointerKind<"non-leaf">} {
256+
llvm.return
257+
}
252258
}
253259

254260
// -----
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
2+
3+
; CHECK-LABEL: llvm.func @frame_pointer_func
4+
; CHECK-SAME: attributes {frame_pointer = #llvm.framePointerKind<"non-leaf">}
5+
6+
define void @frame_pointer_func() "frame-pointer"="non-leaf" {
7+
ret void
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
// CHECK-LABEL: define void @frame_pointer_func()
4+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
5+
llvm.func @frame_pointer_func() attributes {frame_pointer = #llvm.framePointerKind<"non-leaf">} {
6+
llvm.return
7+
}
8+
// CHECK: attributes #[[ATTRS]] = { "frame-pointer"="non-leaf" }

0 commit comments

Comments
 (0)