Skip to content

[Clang] Fix nomerge attribute not working with __builtin_trap(), __debugbreak(), __builtin_verbose_trap() #101549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3883,6 +3883,8 @@ llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) {
TrapCall->addFnAttr(A);
}

if (InNoMergeAttributedStmt)
TrapCall->addFnAttr(llvm::Attribute::NoMerge);
return TrapCall;
}

Expand Down
8 changes: 7 additions & 1 deletion clang/test/CodeGen/attr-nomerge.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -emit-llvm %s -triple x86_64-unknown-linux-gnu -o - | FileCheck %s
// RUN: %clang_cc1 -emit-llvm %s -fms-extensions -o - | FileCheck %s

class A {
public:
Expand Down Expand Up @@ -42,6 +42,9 @@ void foo(int i, A *ap, B *bp) {

A *newA = new B();
delete newA;
[[clang::nomerge]] __builtin_trap();
[[clang::nomerge]] __debugbreak();
[[clang::nomerge]] __builtin_verbose_trap("check null", "Argument must not be null.");
}

int g(int i);
Expand Down Expand Up @@ -97,6 +100,9 @@ void something_else_again() {
// CHECK: load ptr, ptr
// CHECK: %[[AG:.*]] = load ptr, ptr
// CHECK-NEXT: call void %[[AG]](ptr {{.*}}) #[[ATTR1]]
// CHECK: call void @llvm.trap() #[[ATTR0]]
// CHECK: call void @llvm.debugtrap() #[[ATTR0]]
// CHECK: call void @llvm.trap() #[[ATTR0]]
// CHECK: call void @_ZN1AD1Ev(ptr {{.*}}) #[[ATTR1]]

// CHECK-DAG: attributes #[[ATTR0]] = {{{.*}}nomerge{{.*}}}
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7448,6 +7448,8 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
break;
default: llvm_unreachable("unknown trap intrinsic");
}
DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
I.hasFnAttr(Attribute::NoMerge));
return;
}
TargetLowering::ArgListTy Args;
Expand All @@ -7464,7 +7466,7 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
DAG.getExternalSymbol(TrapFuncName.data(),
TLI.getPointerTy(DAG.getDataLayout())),
std::move(Args));

CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
DAG.setRoot(Result.second);
return;
Expand Down
92 changes: 92 additions & 0 deletions llvm/test/CodeGen/X86/nomerge.ll
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,96 @@ if.end:

declare dso_local void @bar()

define void @nomerge_trap(i32 %i) {
; CHECK-LABEL: nomerge_trap:
; CHECK: # %bb.0: # %entry
; CHECK: # %bb.1: # %entry
; CHECK: # %bb.2: # %if.then
; CHECK-NEXT: ud2
; CHECK-NEXT: LBB1_3: # %if.then2
; CHECK-NEXT: ud2
; CHECK-NEXT: .LBB1_4: # %if.end3
; CHECK-NEXT: ud2
entry:
switch i32 %i, label %if.end3 [
i32 5, label %if.then
i32 7, label %if.then2
]

if.then:
tail call void @llvm.trap() #0
unreachable

if.then2:
tail call void @llvm.trap() #0
unreachable

if.end3:
tail call void @llvm.trap() #0
unreachable
}

declare dso_local void @llvm.trap()

define void @nomerge_debugtrap(i32 %i) {
; CHECK-LABEL: nomerge_debugtrap:
; CHECK: # %bb.0: # %entry
; CHECK: # %bb.1: # %entry
; CHECK: # %bb.2: # %if.then
; CHECK-NEXT: int3
; CHECK-NEXT: LBB2_3: # %if.then2
; CHECK-NEXT: int3
; CHECK-NEXT: .LBB2_4: # %if.end3
; CHECK-NEXT: int3
entry:
switch i32 %i, label %if.end3 [
i32 5, label %if.then
i32 7, label %if.then2
]

if.then:
tail call void @llvm.debugtrap() #0
unreachable

if.then2:
tail call void @llvm.debugtrap() #0
unreachable

if.end3:
tail call void @llvm.debugtrap() #0
unreachable
}

define void @nomerge_named_debugtrap(i32 %i) {
; CHECK-LABEL: nomerge_named_debugtrap:
; CHECK: # %bb.0: # %entry
; CHECK: # %bb.1: # %entry
; CHECK: # %bb.2: # %if.then
; CHECK-NEXT: callq trap_func@PLT
; CHECK-NEXT: LBB3_3: # %if.then2
; CHECK-NEXT: callq trap_func@PLT
; CHECK-NEXT: .LBB3_4: # %if.end3
; CHECK-NEXT: callq trap_func@PLT
entry:
switch i32 %i, label %if.end3 [
i32 5, label %if.then
i32 7, label %if.then2
]

if.then:
tail call void @llvm.debugtrap() #1
unreachable

if.then2:
tail call void @llvm.debugtrap() #1
unreachable

if.end3:
tail call void @llvm.debugtrap() #1
unreachable
}

declare dso_local void @llvm.debugtrap()

attributes #0 = { nomerge }
attributes #1 = { nomerge "trap-func-name"="trap_func" }
Loading