Skip to content

Conversation

koachan
Copy link
Contributor

@koachan koachan commented Sep 29, 2025

Do not move meta instructions like FAKE_USE/@llvm.fake.use into delay slots, as they don't correspond to real machine instructions.

This should fix crashes when compiling with, for example, clang -Og.

Do not move instructions with generic opcodes like `FAKE_USE`/`@llvm.fake.use`
into delay slots, as they are not real machine instructions.

This should fix crashes when running `clang -Og`.
@llvmbot
Copy link
Member

llvmbot commented Sep 29, 2025

@llvm/pr-subscribers-backend-sparc

Author: Koakuma (koachan)

Changes

Do not move instructions with generic opcodes like FAKE_USE/@<!-- -->llvm.fake.use into delay slots, as they are not real machine instructions.

This should fix crashes when compiling with, for example, clang -Og.


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

2 Files Affected:

  • (modified) llvm/lib/Target/Sparc/DelaySlotFiller.cpp (+2-2)
  • (modified) llvm/test/CodeGen/SPARC/2011-01-19-DelaySlot.ll (+26)
diff --git a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
index 6c19049a001cf..73d03fc271100 100644
--- a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
+++ b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
@@ -206,8 +206,8 @@ Filler::findDelayInstr(MachineBasicBlock &MBB,
     if (!done)
       --I;
 
-    // skip debug instruction
-    if (I->isDebugInstr())
+    // Skip debug and generic instructions.
+    if (I->isDebugInstr() || (I->getOpcode() <= TargetOpcode::GENERIC_OP_END))
       continue;
 
     if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
diff --git a/llvm/test/CodeGen/SPARC/2011-01-19-DelaySlot.ll b/llvm/test/CodeGen/SPARC/2011-01-19-DelaySlot.ll
index 9ccd4f1c0ac9a..d4b21b248d60d 100644
--- a/llvm/test/CodeGen/SPARC/2011-01-19-DelaySlot.ll
+++ b/llvm/test/CodeGen/SPARC/2011-01-19-DelaySlot.ll
@@ -184,4 +184,30 @@ entry:
   ret i32 %2
 }
 
+define i32 @test_generic_inst(i32 %a) #0 {
+;CHECK-LABEL: test_generic_inst:
+;CHECK: ! fake_use: {{.*}}
+;CHECK: bne {{.*}}
+;CHECK-NEXT: nop
+
+%2 = call i32 @bar(i32 %a)
+  %3 = and i32 %2, 1
+  %4 = icmp eq i32 %3, 0
+  ; This shouldn't get reordered into a delay slot
+  call void (...) @llvm.fake.use(i32 %a)
+  br i1 %4, label %5, label %7
+5:
+  %6 = call i32 @bar(i32 %2)
+  br label %9
+
+7:
+  %8 = add nsw i32 %2, 1
+  br label %9
+
+9:
+  %10 = phi i32 [ %6, %5 ], [ %8, %7 ]
+  ret i32 %10
+}
+
+declare void @llvm.fake.use(...)
 attributes #0 = { nounwind "disable-tail-calls"="true" }

@brad0 brad0 requested a review from arsenm October 3, 2025 02:51
;CHECK: bne {{.*}}
;CHECK-NEXT: nop

%2 = call i32 @bar(i32 %a)
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indent and use named values in tests

// skip debug instruction
if (I->isDebugInstr())
// Skip debug and generic instructions.
if (I->isDebugInstr() || (I->getOpcode() <= TargetOpcode::GENERIC_OP_END))
Copy link
Contributor

Choose a reason for hiding this comment

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

Generic is probably the wrong thing. isMetaInstruction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works, but I have a question:
Are debug instructions considered meta too? If so then I'll remove the redundant debug check too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

@koachan koachan changed the title [SPARC] Prevent generic opcodes from being inserted into delay slots [SPARC] Prevent meta instructions from being inserted into delay slots Oct 3, 2025
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

LGTM but meta check is redundant with debug

@koachan koachan merged commit 2e1fab9 into llvm:main Oct 3, 2025
9 checks passed
MixedMatched pushed a commit to MixedMatched/llvm-project that referenced this pull request Oct 3, 2025
llvm#161111)

Do not move meta instructions like `FAKE_USE`/`@llvm.fake.use` into
delay slots, as they don't correspond to real machine instructions.

This should fix crashes when compiling with, for example, `clang -Og`.
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Oct 6, 2025
llvm#161111)

Do not move meta instructions like `FAKE_USE`/`@llvm.fake.use` into
delay slots, as they don't correspond to real machine instructions.

This should fix crashes when compiling with, for example, `clang -Og`.

(cherry picked from commit 2e1fab9)
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.

3 participants