Skip to content

Commit 1bf7921

Browse files
committed
[mlir][LLVM] Add support for adding a garbage collector to a LLVM function
This patch simply adds an optional garbage collector attribute to LLVMFuncOp which maps 1:1 to the "gc" property of functions in LLVM. Differential Revision: https://reviews.llvm.org/D119492
1 parent e714b98 commit 1bf7921

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,7 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
12371237
let arguments = (ins DefaultValuedAttr<Linkage, "Linkage::External">:$linkage,
12381238
UnitAttr:$dso_local,
12391239
OptionalAttr<FlatSymbolRefAttr>:$personality,
1240+
OptionalAttr<StrAttr>:$garbageCollector,
12401241
OptionalAttr<ArrayAttr>:$passthrough);
12411242

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

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ LogicalResult Importer::processFunction(llvm::Function *f) {
812812
emitWarning(UnknownLoc::get(context),
813813
"could not deduce personality, skipping it");
814814

815+
if (f->hasGC())
816+
fop.setGarbageCollectorAttr(b.getStringAttr(f->getGC()));
817+
815818
if (f->isDeclaration())
816819
return success();
817820

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
797797
llvmFunc->setPersonalityFn(pfunc);
798798
}
799799

800+
if (auto gc = func.getGarbageCollector())
801+
llvmFunc->setGC(gc->str());
802+
800803
// First, create all blocks so we can jump to them.
801804
llvm::LLVMContext &llvmContext = llvmFunc->getContext();
802805
for (auto &bb : func) {

mlir/test/Target/LLVMIR/import.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ define i32 @invokeLandingpad() personality i8* bitcast (i32 (...)* @__gxx_person
331331
ret i32 0
332332
}
333333

334+
; CHECK-LABEL: @hasGCFunction
335+
; CHECK-SAME: garbageCollector = "statepoint-example"
336+
define void @hasGCFunction() gc "statepoint-example" {
337+
ret void
338+
}
339+
334340
;CHECK-LABEL: @useFreezeOp
335341
define i32 @useFreezeOp(i32 %x) {
336342
;CHECK: %{{[0-9]+}} = llvm.freeze %{{[0-9a-z]+}} : i32

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,12 @@ llvm.func @invoke_phis() -> i32 attributes { personality = @__gxx_personality_v0
13781378

13791379
// -----
13801380

1381+
// CHECK-LABEL: @hasGCFunction
1382+
// CHECK-SAME: gc "statepoint-example"
1383+
llvm.func @hasGCFunction() attributes { garbageCollector = "statepoint-example" } {
1384+
llvm.return
1385+
}
1386+
13811387
// CHECK-LABEL: @callFreezeOp
13821388
llvm.func @callFreezeOp(%x : i32) {
13831389
// CHECK: freeze i32 %{{[0-9]+}}

0 commit comments

Comments
 (0)