Skip to content
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

Skip tranformConstExprCastCall for naked function #76496

Merged
merged 1 commit into from
Jan 1, 2024

Conversation

hstk30-hw
Copy link
Contributor

Fix this issue #72843 .

For naked function, assembly might be using an argument, or otherwise rely on the frame layout, so don't transformConstExprCastCall

@llvmbot
Copy link
Collaborator

llvmbot commented Dec 28, 2023

@llvm/pr-subscribers-llvm-transforms

Author: None (hstk30-hw)

Changes

Fix this issue #72843 .

For naked function, assembly might be using an argument, or otherwise rely on the frame layout, so don't transformConstExprCastCall


Full diff: https://github.com/llvm/llvm-project/pull/76496.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+6)
  • (added) llvm/test/Transforms/InstCombine/cast-call-naked.ll (+16)
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..a0b0d48b44d4eb
--- /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 }

@hstk30-hw hstk30-hw force-pushed the naked-func branch 2 times, most recently from d07d219 to d4a394d Compare January 1, 2024 08:27
@hstk30-hw
Copy link
Contributor Author

ping

@@ -0,0 +1,16 @@
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define dso_local void @naked_func() #0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dso_local.

Also directly use naked here instead of an attribute group.

define i32 @main() {
; CHECK: call void @naked_func(i32 noundef 1)
entry:
call void @naked_func(i32 noundef 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove noundef.

@@ -0,0 +1,16 @@
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use update_test_checks.py.

Preferably also find where all other tests for this transform are and add it to that file, instead of creating a new one.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rerun update_test_checks.py instead of manually editing the file.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hstk30-hw
Copy link
Contributor Author

hstk30-hw commented Jan 1, 2024

Thx advice for test cases :)

@hstk30-hw hstk30-hw merged commit 4b2f118 into llvm:main Jan 1, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants