[llvm] [Clang][HLSL][GVN] Prevent phi codgen of isTokenLike types (PR #162363)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 09:20:56 PDT 2025


https://github.com/farzonl updated https://github.com/llvm/llvm-project/pull/162363

>From e2ef66f2128863c79a3c52374d0f3d8d53b62198 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Tue, 7 Oct 2025 16:00:36 -0400
Subject: [PATCH 1/3] [HLSL] prevent phi codgen of isTokenLike types

fixes #161754

When the GVN pass is PerformLoadPRE we need to check if it might be
doing this load on a token like type. This is because if we don't
GVN will use the SSAUpdater to insert a phi node to reduce duplicate
resource.getpointer calls.

Because in an earlier commit: https://github.com/llvm/llvm-project/commit/01c0a8409a21344c822deba9467bd9d547f6e5d8
we made the verifier error with `PHI nodes cannot have token type!`

This test case will fail today if we try to perform this load
optimization
https://godbolt.org/z/xM69fY8zM
---
 llvm/lib/Transforms/Scalar/GVN.cpp            |  3 ++
 .../GVN/PRE/no-pre-load-for-token-like.ll     | 34 +++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll

diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index b9b5b5823d780..0fa99eb375a47 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -1659,6 +1659,9 @@ bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
   // that we only have to insert *one* load (which means we're basically moving
   // the load, not inserting a new one).
 
+  if (Load->getType()->isTokenLikeTy())
+    return false;
+
   SmallPtrSet<BasicBlock *, 4> Blockers(llvm::from_range, UnavailableBlocks);
 
   // Let's find the first basic block with more than one predecessor.  Walk
diff --git a/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll b/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll
new file mode 100644
index 0000000000000..7fc56e8b5d8f1
--- /dev/null
+++ b/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=gvn %s | FileCheck %s
+
+; NOTE: A test to confirm GVN doesn't introduce phis for pre loads of token
+; NOTE: like types. This implies the CHECKS should exactly match the IR.
+
+define ptr @main() {
+; CHECK-LABEL: define ptr @main() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 false, label %[[ENTRY_IF_END_I_CRIT_EDGE:.*]], label %[[IF_THEN_I:.*]]
+; CHECK:       [[ENTRY_IF_END_I_CRIT_EDGE]]:
+; CHECK-NEXT:    br label %[[IF_END_I:.*]]
+; CHECK:       [[IF_THEN_I]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = load target("dx.RawBuffer", half, 1, 0), ptr null, align 4
+; CHECK-NEXT:    [[TMP1:%.*]] = tail call ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f16_1_0t(target("dx.RawBuffer", half, 1, 0) [[TMP0]], i32 0)
+; CHECK-NEXT:    br label %[[IF_END_I]]
+; CHECK:       [[IF_END_I]]:
+; CHECK-NEXT:    [[TMP2:%.*]] = load target("dx.RawBuffer", half, 1, 0), ptr null, align 4
+; CHECK-NEXT:    [[TMP3:%.*]] = tail call ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f16_1_0t(target("dx.RawBuffer", half, 1, 0) [[TMP2]], i32 0)
+; CHECK-NEXT:    ret ptr [[TMP3]]
+;
+entry:
+  br i1 false, label %if.end.i, label %if.then.i
+
+if.then.i:                                        ; preds = %entry
+  %0 = load target("dx.RawBuffer", half, 1, 0), ptr null, align 4
+  %1 = tail call ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f16_1_0t(target("dx.RawBuffer", half, 1, 0) %0, i32 0)
+  br label %if.end.i
+
+if.end.i:                                         ; preds = %if.then.i, %entry
+  %2 = load target("dx.RawBuffer", half, 1, 0), ptr null, align 4
+  %3 = tail call ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_f16_1_0t(target("dx.RawBuffer", half, 1, 0) %2, i32 0)
+  ret ptr %3
+}

>From 863a8ebaab5dad41d56dc8369a7e3f5ea9a901d6 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Tue, 7 Oct 2025 23:12:24 -0400
Subject: [PATCH 2/3] move isTokenLikeTy check from  to processLoad to capture
 all the processNonLocalLoad cases

---
 llvm/lib/Transforms/Scalar/GVN.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 0fa99eb375a47..ec79bd0020419 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -1659,9 +1659,6 @@ bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
   // that we only have to insert *one* load (which means we're basically moving
   // the load, not inserting a new one).
 
-  if (Load->getType()->isTokenLikeTy())
-    return false;
-
   SmallPtrSet<BasicBlock *, 4> Blockers(llvm::from_range, UnavailableBlocks);
 
   // Let's find the first basic block with more than one predecessor.  Walk
@@ -2247,6 +2244,9 @@ bool GVNPass::processLoad(LoadInst *L) {
   if (!L->isUnordered())
     return false;
 
+  if (L->getType()->isTokenLikeTy())
+    return false;
+
   if (L->use_empty()) {
     salvageAndRemoveInstruction(L);
     return true;

>From 967f6a873bce3392fdd973f5017120f50f455739 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Fri, 10 Oct 2025 12:20:39 -0400
Subject: [PATCH 3/3] address pr comment

---
 llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll b/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll
index 7fc56e8b5d8f1..1b36aba6bca09 100644
--- a/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll
+++ b/llvm/test/Transforms/GVN/PRE/no-pre-load-for-token-like.ll
@@ -1,8 +1,8 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt -S -passes=gvn %s | FileCheck %s
 
-; NOTE: A test to confirm GVN doesn't introduce phis for pre loads of token
-; NOTE: like types. This implies the CHECKS should exactly match the IR.
+; NOTE: A test to confirm GVN doesn't collapse loads of token-like types into
+; NOTE: phi nodes.
 
 define ptr @main() {
 ; CHECK-LABEL: define ptr @main() {



More information about the llvm-commits mailing list