Skip to content

Conversation

djtodoro
Copy link
Collaborator

@djtodoro djtodoro commented Oct 3, 2025

Properly handle clang::musttail attribute on MIPS backend.

It fixes: #161193

Properly handle clang::musttail attribute on MIPS backend.
@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2025

@llvm/pr-subscribers-backend-mips

Author: Djordje Todorovic (djtodoro)

Changes

Properly handle clang::musttail attribute on MIPS backend.

It fixes: #161193


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

2 Files Affected:

  • (modified) llvm/lib/Target/Mips/MipsISelLowering.cpp (+4-3)
  • (added) llvm/test/CodeGen/Mips/musttail.ll (+38)
diff --git a/llvm/lib/Target/Mips/MipsISelLowering.cpp b/llvm/lib/Target/Mips/MipsISelLowering.cpp
index 881ba8e2f9eff..d79db127f01a2 100644
--- a/llvm/lib/Target/Mips/MipsISelLowering.cpp
+++ b/llvm/lib/Target/Mips/MipsISelLowering.cpp
@@ -3407,7 +3407,8 @@ MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   // Check if it's really possible to do a tail call. Restrict it to functions
   // that are part of this compilation unit.
   bool InternalLinkage = false;
-  if (IsTailCall) {
+  bool IsMustTail = CLI.CB && CLI.CB->isMustTailCall();
+  if (IsTailCall && !IsMustTail) {
     IsTailCall = isEligibleForTailCallOptimization(
         CCInfo, StackSize, *MF.getInfo<MipsFunctionInfo>());
     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
@@ -3416,9 +3417,9 @@ MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
                      G->getGlobal()->hasPrivateLinkage() ||
                      G->getGlobal()->hasHiddenVisibility() ||
                      G->getGlobal()->hasProtectedVisibility());
-     }
+    }
   }
-  if (!IsTailCall && CLI.CB && CLI.CB->isMustTailCall())
+  if (!IsTailCall && IsMustTail)
     report_fatal_error("failed to perform tail call elimination on a call "
                        "site marked musttail");
 
diff --git a/llvm/test/CodeGen/Mips/musttail.ll b/llvm/test/CodeGen/Mips/musttail.ll
new file mode 100644
index 0000000000000..d5f457f6eb665
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/musttail.ll
@@ -0,0 +1,38 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=mips-unknown-linux-gnu < %s | FileCheck %s --check-prefix=MIPS32
+; RUN: llc -mtriple=mips64-unknown-linux-gnu < %s | FileCheck %s --check-prefix=MIPS64
+
+; Test musttail support for MIPS
+
+declare void @external_func()
+
+; Test basic musttail with external function
+define void @test_musttail_external() {
+; MIPS32-LABEL: test_musttail_external:
+; MIPS32:       # %bb.0:
+; MIPS32-NEXT:    j external_func
+; MIPS32-NEXT:    nop
+;
+; MIPS64-LABEL: test_musttail_external:
+; MIPS64:       # %bb.0:
+; MIPS64-NEXT:    j external_func
+; MIPS64-NEXT:    nop
+  musttail call void @external_func()
+  ret void
+}
+
+declare i32 @callee_args(i32 %a, i32 %b, i32 %c)
+
+define i32 @test_musttail_args(i32 %x, i32 %y, i32 %z) {
+; MIPS32-LABEL: test_musttail_args:
+; MIPS32:       # %bb.0:
+; MIPS32-NEXT:    j callee_args
+; MIPS32-NEXT:    nop
+;
+; MIPS64-LABEL: test_musttail_args:
+; MIPS64:       # %bb.0:
+; MIPS64-NEXT:    j callee_args
+; MIPS64-NEXT:    nop
+  %ret = musttail call i32 @callee_args(i32 %x, i32 %y, i32 %z)
+  ret i32 %ret
+}

Copy link
Contributor

@kraj kraj left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

Choose a reason for hiding this comment

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

Uh don't we still need to check it's eligible?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, that thing confuses me in the case of musttail. On ARM, we do check it. Lets do it for MIPS as well.

Copy link
Collaborator Author

@djtodoro djtodoro Oct 7, 2025

Choose a reason for hiding this comment

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

@jrtc27 yep, it does make sense to still check it. I incorporated in the new commit (basically - do not check for the optional mips flag for enabling tail call optimizations in case of musttial, and I also enabled tail call opts for dso_local functions (1dcb9110612ab) ). Thanks for the review!

Copy link
Collaborator

@jrtc27 jrtc27 left a comment

Choose a reason for hiding this comment

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

.

@djtodoro
Copy link
Collaborator Author

djtodoro commented Oct 7, 2025

I checked the issues listed above, all cases from those still pass with the new patch.

Copy link

github-actions bot commented Oct 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@kraj
Copy link
Contributor

kraj commented Oct 8, 2025

Please squash changes into initial commit

@djtodoro
Copy link
Collaborator Author

djtodoro commented Oct 8, 2025

Please squash changes into initial commit

Per "Contributing to LLVM" Policy, we do not do force-push (and squash), we rather use incremental changes to address comments during review process. And, once the pull request is approved, we select the “Squash and merge” button.

Copy link
Contributor

@kraj kraj left a comment

Choose a reason for hiding this comment

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

my checks pass with this series as well.

@kraj
Copy link
Contributor

kraj commented Oct 8, 2025

Please squash changes into initial commit

Per "Contributing to LLVM" Policy, we do not do force-push (and squash), we rather use incremental changes to address comments during review process. And, once the pull request is approved, we select the “Squash and merge” button.

right, squashing is what I meant.

@djtodoro
Copy link
Collaborator Author

I will wait for @jrtc27 to have another look. :)

@brad0
Copy link
Contributor

brad0 commented Oct 10, 2025

@jrtc27 @yingopq Ping.

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.

[[clang::musttail]] triggers backend crash in llc on MIPS

5 participants