[llvm] Skip tranformConstExprCastCall for naked function (PR #76496)

via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 31 23:31:08 PST 2023


https://github.com/hstk30-hw updated https://github.com/llvm/llvm-project/pull/76496

>From d07d2190046be34303e2733407dae150f081ca74 Mon Sep 17 00:00:00 2001
From: hstk30-hw <hanwei62 at huawei.com>
Date: Thu, 28 Dec 2023 17:02:12 +0800
Subject: [PATCH] Skip tranformConstExprCastCall for naked function

---
 .../Transforms/InstCombine/InstCombineCalls.cpp  |  6 ++++++
 .../Transforms/InstCombine/cast-call-naked.ll    | 16 ++++++++++++++++
 .../LowerTypeTests/cfi-unwind-direct-call.ll     | 14 +++++++-------
 3 files changed, 29 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/cast-call-naked.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 3b7fe7fa226607..43d4496571be50 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3850,6 +3850,12 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
   if (Callee->hasFnAttribute("thunk"))
     return false;
 
+  // If this is a call to a naked function, the assembly might be
+  // using an argument, or otherwise rely on the frame layout,
+  // the function prototype will mismatch.
+  if (Callee->hasFnAttribute(Attribute::Naked))
+    return false;
+
   // If this is a musttail call, the callee's prototype must match the caller's
   // prototype with the exception of pointee types. The code below doesn't
   // implement that, so we can't do this transform.
diff --git a/llvm/test/Transforms/InstCombine/cast-call-naked.ll b/llvm/test/Transforms/InstCombine/cast-call-naked.ll
new file mode 100644
index 00000000000000..1be7fd6ca18304
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/cast-call-naked.ll
@@ -0,0 +1,16 @@
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define dso_local void @naked_func() #0 {
+entry:
+  tail call void asm sideeffect "mov  r1, r0", ""()
+  unreachable
+}
+
+define i32 @main() {
+; CHECK:  call void @naked_func(i32 noundef 1)
+entry:
+  call void @naked_func(i32 noundef 1)
+  ret i32 0
+}
+
+attributes #0 = { naked }
diff --git a/llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll b/llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll
index c560940835279a..d1f76576d3be60 100644
--- a/llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll
+++ b/llvm/test/Transforms/LowerTypeTests/cfi-unwind-direct-call.ll
@@ -175,7 +175,7 @@ attributes #8 = { noreturn nounwind }
 ; CHECK-SAME: (i32 noundef [[NUM:%.*]]) local_unnamed_addr #[[ATTR0]] personality ptr @__gxx_personality_v0 !type !4 !type !5 !type !6 {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    store ptr @_Z7throw_ei.cfi_jt, ptr @catch_ptr, align 8, !tbaa [[TBAA11:![0-9]+]]
-; CHECK-NEXT:    invoke void @_Z7throw_ei.cfi_jt() #[[ATTR7:[0-9]+]]
+; CHECK-NEXT:    invoke void @_Z7throw_ei.cfi_jt(i32 noundef [[NUM]]) #[[ATTR7:[0-9]+]]
 ; CHECK-NEXT:    to label [[TRY_CONT:%.*]] unwind label [[LPAD:%.*]]
 ; CHECK:       lpad:
 ; CHECK-NEXT:    [[TMP0:%.*]] = landingpad { ptr, i32 }
@@ -199,17 +199,17 @@ attributes #8 = { noreturn nounwind }
 ; CHECK-LABEL: define weak_odr hidden void @__cfi_check_fail
 ; CHECK-SAME: (ptr noundef [[TMP0:%.*]], ptr noundef [[TMP1:%.*]]) #[[ATTR2:[0-9]+]] {
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[DOTNOT:%.*]] = icmp eq ptr [[TMP0]], null, !nosanitize !13
-; CHECK-NEXT:    br i1 [[DOTNOT]], label [[TRAP:%.*]], label [[CONT:%.*]], !nosanitize !13
+; CHECK-NEXT:    [[DOTNOT:%.*]] = icmp eq ptr [[TMP0]], null, !nosanitize
+; CHECK-NEXT:    br i1 [[DOTNOT]], label [[TRAP:%.*]], label [[CONT:%.*]], !nosanitize
 ; CHECK:       trap:
-; CHECK-NEXT:    tail call void @llvm.ubsantrap(i8 2) #[[ATTR8:[0-9]+]], !nosanitize !13
-; CHECK-NEXT:    unreachable, !nosanitize !13
+; CHECK-NEXT:    tail call void @llvm.ubsantrap(i8 2) #[[ATTR8:[0-9]+]], !nosanitize
+; CHECK-NEXT:    unreachable, !nosanitize
 ; CHECK:       cont:
-; CHECK-NEXT:    [[TMP2:%.*]] = load i8, ptr [[TMP0]], align 4, !nosanitize !13
+; CHECK-NEXT:    [[TMP2:%.*]] = load i8, ptr [[TMP0]], align 4, !nosanitize
 ; CHECK-NEXT:    [[SWITCH:%.*]] = icmp ult i8 [[TMP2]], 5
 ; CHECK-NEXT:    br i1 [[SWITCH]], label [[TRAP]], label [[CONT6:%.*]]
 ; CHECK:       cont6:
-; CHECK-NEXT:    ret void, !nosanitize !13
+; CHECK-NEXT:    ret void, !nosanitize
 ;
 ;
 ; CHECK-LABEL: define weak void @__cfi_check



More information about the llvm-commits mailing list