[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 11:51:40 PDT 2023


https://github.com/majaha created https://github.com/llvm/llvm-project/pull/67051

Add the command line option --no-trap-after-noreturn, which exposes the pre-existing TargetOption `NoTrapAfterNoreturn`.

This pull request was split off from this one: https://github.com/llvm/llvm-project/pull/65876

>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] 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
+}



More information about the llvm-commits mailing list