[llvm] [AMDGPU] Don't unify divergent exit nodes with musttail calls (PR #126395)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 8 18:42:31 PST 2025


https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/126395

Fixes SWDEV-512254.

>From 2a5fbdf91dbcaabd6f038cad30be66e6c51138bf Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sat, 8 Feb 2025 21:42:02 -0500
Subject: [PATCH] [AMDGPU] Don't unify divergent exit nodes with musttail calls

Fixes SWDEV-512254.
---
 .../AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp  |  5 +++-
 ...nify-divergent-exit-nodes-with-musttail.ll | 25 +++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/AMDGPU/do-not-unify-divergent-exit-nodes-with-musttail.ll

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
index fda2a38c2464e00..d087fbc86545c99 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUUnifyDivergentExitNodes.cpp
@@ -215,7 +215,10 @@ bool AMDGPUUnifyDivergentExitNodesImpl::run(Function &F, DominatorTree *DT,
       PDT.roots(), [&](auto BB) { return !isUniformlyReached(UA, *BB); });
 
   for (BasicBlock *BB : PDT.roots()) {
-    if (isa<ReturnInst>(BB->getTerminator())) {
+    if (auto *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
+      auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode());
+      if (CI && CI->isMustTailCall())
+        continue;
       if (HasDivergentExitBlock)
         ReturningBlocks.push_back(BB);
     } else if (isa<UnreachableInst>(BB->getTerminator())) {
diff --git a/llvm/test/CodeGen/AMDGPU/do-not-unify-divergent-exit-nodes-with-musttail.ll b/llvm/test/CodeGen/AMDGPU/do-not-unify-divergent-exit-nodes-with-musttail.ll
new file mode 100644
index 000000000000000..2f367d64351089c
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/do-not-unify-divergent-exit-nodes-with-musttail.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=amdgpu-unify-divergent-exit-nodes -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a %s -o - | FileCheck %s
+
+define void @spill_sgpr_with_tail_call() {
+; CHECK-LABEL: define void @spill_sgpr_with_tail_call(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[L1:%.*]] = load i1, ptr null, align 1
+; CHECK-NEXT:    br i1 [[L1]], label %[[SW_C:.*]], label %[[SW_D:.*]]
+; CHECK:       [[SW_D]]:
+; CHECK-NEXT:    musttail call void null()
+; CHECK-NEXT:    ret void
+; CHECK:       [[SW_C]]:
+; CHECK-NEXT:    ret void
+;
+  %L1 = load i1, ptr null, align 1
+  br i1 %L1, label %SW_C, label %SW_D
+
+SW_D:
+  musttail call void null()
+  ret void
+
+SW_C:
+  ret void
+}
+



More information about the llvm-commits mailing list