[llvm] Add command line option --no-trap-after-noreturn (PR #67051)
Matt Harding via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 21 15:52:14 PDT 2023
https://github.com/majaha updated https://github.com/llvm/llvm-project/pull/67051
>From cd9c53d3e3dce6a028ee1c025a8eeb84fffc5089 Mon Sep 17 00:00:00 2001
From: Matt Harding <majaharding at gmail.com>
Date: Thu, 21 Sep 2023 18:29:31 +0100
Subject: [PATCH 1/2] Add command line option --no-trap-after-noreturn
Add the command line option --no-trap-after-noreturn, which
exposes the pre-existing TargetOption NoTrapAfterNoreturn.
---
llvm/lib/CodeGen/LLVMTargetMachine.cpp | 7 +++++++
llvm/test/CodeGen/ARM/trap-unreachable.ll | 23 ++++++++++++++++++++---
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
index 7ec8390033fbabc..87a17b88db12433 100644
--- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp
+++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
@@ -37,6 +37,11 @@ static cl::opt<bool>
EnableTrapUnreachable("trap-unreachable", cl::Hidden,
cl::desc("Enable generating trap for unreachable"));
+static cl::opt<bool> EnableNoTrapAfterNoreturn(
+ "no-trap-after-noreturn", cl::Hidden,
+ cl::desc("Do not emit a trap instruction for 'unreachable' IR instructions "
+ "after noreturn calls, even if --trap-unreachable is set."));
+
void LLVMTargetMachine::initAsmInfo() {
MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
assert(MRI && "Unable to create reg info");
@@ -95,6 +100,8 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
if (EnableTrapUnreachable)
this->Options.TrapUnreachable = true;
+ if (EnableNoTrapAfterNoreturn)
+ this->Options.NoTrapAfterNoreturn = true;
}
TargetTransformInfo
diff --git a/llvm/test/CodeGen/ARM/trap-unreachable.ll b/llvm/test/CodeGen/ARM/trap-unreachable.ll
index 605d5a234291a42..aaa55e43d617cf3 100644
--- a/llvm/test/CodeGen/ARM/trap-unreachable.ll
+++ b/llvm/test/CodeGen/ARM/trap-unreachable.ll
@@ -1,8 +1,25 @@
-; RUN: llc -mtriple=thumbv7 -trap-unreachable < %s | FileCheck %s
-; CHECK: .inst.n 0xdefe
+; RUN: llc -mtriple=thumbv7 -trap-unreachable < %s | FileCheck %s --check-prefixes CHECK,TRAP_UNREACHABLE
+; RUN: llc -mtriple=thumbv7 -trap-unreachable -no-trap-after-noreturn < %s | FileCheck %s --check-prefixes CHECK,NTANR
-define void @test() #0 {
+define void @test_trap_unreachable() #0 {
+; CHECK-LABEL: test_trap_unreachable:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: .inst.n 0xdefe
unreachable
}
attributes #0 = { nounwind }
+
+declare void @no_return() noreturn
+
+define void @test_ntanr() {
+; CHECK-LABEL: test_ntanr:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: push {r7, lr}
+; CHECK-NEXT: bl no_return
+; TRAP_UNREACHABLE-NEXT: .inst.n 0xdefe
+; NTANR-NOT: .inst.n 0xdefe
+;
+ call void @no_return()
+ unreachable
+}
>From 7a46fcc6c450bc2d18898ca581d0ac800845f30d Mon Sep 17 00:00:00 2001
From: Matt Harding <majaharding at gmail.com>
Date: Thu, 21 Sep 2023 23:31:23 +0100
Subject: [PATCH 2/2] Test --no-trap-after-noreturn for misfires
---
llvm/test/CodeGen/ARM/trap-unreachable.ll | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/llvm/test/CodeGen/ARM/trap-unreachable.ll b/llvm/test/CodeGen/ARM/trap-unreachable.ll
index aaa55e43d617cf3..84dbb04c462b450 100644
--- a/llvm/test/CodeGen/ARM/trap-unreachable.ll
+++ b/llvm/test/CodeGen/ARM/trap-unreachable.ll
@@ -11,9 +11,10 @@ define void @test_trap_unreachable() #0 {
attributes #0 = { nounwind }
declare void @no_return() noreturn
+declare void @could_return()
-define void @test_ntanr() {
-; CHECK-LABEL: test_ntanr:
+define void @test_ntanr_noreturn() {
+; CHECK-LABEL: test_ntanr_noreturn:
; CHECK: @ %bb.0:
; CHECK-NEXT: push {r7, lr}
; CHECK-NEXT: bl no_return
@@ -23,3 +24,13 @@ define void @test_ntanr() {
call void @no_return()
unreachable
}
+
+define void @test_ntanr_could_return() {
+; CHECK-LABEL: test_ntanr_could_return:
+; CHECK: @ %bb.0:
+; CHECK-NEXT: push {r7, lr}
+; CHECK-NEXT: bl could_return
+; CHECK-NEXT: .inst.n 0xdefe
+ call void @could_return()
+ unreachable
+}
More information about the llvm-commits
mailing list