Skip to content

Commit 8a43bc2

Browse files
[MLIR][LLVMIR] Import: fix llvm.call attribute inheritance (#130221)
`inst->getFnAttr(Kind)` fallbacks to check if the parent has an attribute, which breaks roundtriping the LLVM IR. This change actually checks only in the call attribute list (no fallback to parent queries). It's possible to argue that this small optimization isn't harmful, but seems too early if it's breaking roundtrip behavior.
1 parent 106c964 commit 8a43bc2

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,10 +2263,15 @@ LogicalResult ModuleImport::convertInvokeAttributes(llvm::InvokeInst *inst,
22632263
LogicalResult ModuleImport::convertCallAttributes(llvm::CallInst *inst,
22642264
CallOp op) {
22652265
setFastmathFlagsAttr(inst, op.getOperation());
2266+
// Query the attributes directly instead of using `inst->getFnAttr(Kind)`, the
2267+
// latter does additional lookup to the parent and inherits, changing the
2268+
// semantics too early.
2269+
llvm::AttributeList callAttrs = inst->getAttributes();
2270+
22662271
op.setTailCallKind(convertTailCallKindFromLLVM(inst->getTailCallKind()));
2267-
op.setConvergent(inst->isConvergent());
2268-
op.setNoUnwind(inst->doesNotThrow());
2269-
op.setWillReturn(inst->hasFnAttr(llvm::Attribute::WillReturn));
2272+
op.setConvergent(callAttrs.getFnAttr(llvm::Attribute::Convergent).isValid());
2273+
op.setNoUnwind(callAttrs.getFnAttr(llvm::Attribute::NoUnwind).isValid());
2274+
op.setWillReturn(callAttrs.getFnAttr(llvm::Attribute::WillReturn).isValid());
22702275

22712276
llvm::MemoryEffects memEffects = inst->getMemoryEffects();
22722277
ModRefInfo othermem = convertModRefInfoFromLLVM(

mlir/test/Target/LLVMIR/Import/call-argument-attributes.ll

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: mlir-translate -import-llvm %s | FileCheck %s
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
22

33
; CHECK-LABEL: llvm.func @somefunc(i32, !llvm.ptr)
44
declare void @somefunc(i32, ptr)
@@ -20,3 +20,22 @@ define i16 @test_call_arg_attrs_indirect(i16 %0, ptr %1) {
2020
%3 = tail call signext i16 %1(i16 noundef signext %0)
2121
ret i16 %3
2222
}
23+
24+
; // -----
25+
26+
%struct.S = type { i8 }
27+
28+
; CHECK-LABEL: @t
29+
define void @t(i1 %0) #0 {
30+
%3 = alloca %struct.S, align 1
31+
; CHECK-NOT: llvm.call @z(%1) {no_unwind} : (!llvm.ptr) -> ()
32+
; CHECK: llvm.call @z(%1) : (!llvm.ptr) -> ()
33+
call void @z(ptr %3)
34+
ret void
35+
}
36+
37+
define linkonce_odr void @z(ptr %0) #0 {
38+
ret void
39+
}
40+
41+
attributes #0 = { nounwind }

0 commit comments

Comments
 (0)