[llvm] 86d1e00 - [DirectX] Prevent `dxil-resource-access` from constructing a foldable phi (#211346)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 18 10:57:03 PDT 2026


Author: Finn Plummer
Date: 2026-08-18T10:56:59-07:00
New Revision: 86d1e003b74c8d32167b72d8d38ff980717fb6dd

URL: https://github.com/llvm/llvm-project/commit/86d1e003b74c8d32167b72d8d38ff980717fb6dd
DIFF: https://github.com/llvm/llvm-project/commit/86d1e003b74c8d32167b72d8d38ff980717fb6dd.diff

LOG: [DirectX] Prevent `dxil-resource-access` from constructing a foldable phi (#211346)

A phi node of the index should only be created if the indices actually
differ: https://godbolt.org/z/x6z64qqvK.

This is resolved by updating `getAccessIndices` to only create a new phi
when needed.

This causes validation errors when accessing specific resources that
require all indices are constant.

Resolves, in part, https://github.com/llvm/llvm-project/issues/211121

Assisted by: Claude Opus 4.8

Added: 
    

Modified: 
    llvm/lib/Target/DirectX/DXILResourceAccess.cpp
    llvm/test/CodeGen/DirectX/ResourceAccess/handle-to-index.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
index 4328066fa01b8..37b13dcc3455c 100644
--- a/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
+++ b/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
@@ -851,7 +851,22 @@ getAccessIndices(Instruction *I, SmallSetVector<Instruction *, 16> &DeadInsts,
     Builder.Insert(HandlePhi);
 
     DeadInsts.insert(Phi);
-    return {GetPtrPhi, HandlePhi};
+
+    Value *GetPtrIdx = GetPtrPhi;
+    Value *HandleIdx = HandlePhi;
+
+    if (GetPtrPhi)
+      if (Value *ConstantGetPtr = GetPtrPhi->hasConstantValue()) {
+        GetPtrIdx = ConstantGetPtr;
+        DeadInsts.insert(GetPtrPhi);
+      }
+
+    if (Value *ConstantHandle = HandlePhi->hasConstantValue()) {
+      HandleIdx = ConstantHandle;
+      DeadInsts.insert(HandlePhi);
+    }
+
+    return {GetPtrIdx, HandleIdx};
   }
 
   if (auto *Select = dyn_cast<SelectInst>(I)) {

diff  --git a/llvm/test/CodeGen/DirectX/ResourceAccess/handle-to-index.ll b/llvm/test/CodeGen/DirectX/ResourceAccess/handle-to-index.ll
index 74384436222c8..1392a779d95d6 100644
--- a/llvm/test/CodeGen/DirectX/ResourceAccess/handle-to-index.ll
+++ b/llvm/test/CodeGen/DirectX/ResourceAccess/handle-to-index.ll
@@ -103,8 +103,7 @@ define void @gvn_ptr_store(i32 %x, i1 %cond, i32 %a, i32 %b) {
 ; CHECK-NOT: handlefromimplicitbinding
 ; CHECK:     main:
 ; CHECK-NEXT:  %[[C:.*]] = phi i32 [ %a, %entry ], [ %b, %if.then.i ]
-; CHECK-NEXT:  %[[IDX:.*]] = phi i32 [ 0, %entry ], [ 0, %if.then.i ]
-; CHECK-NEXT:  %[[HANDLE:.*]] = tail call target("dx.TypedBuffer", i32, 1, 0, 1) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0_1t(i32 2, i32 0, i32 1, i32 %[[IDX]], ptr nonnull @OutArr.str)
+; CHECK-NEXT:  %[[HANDLE:.*]] = tail call target("dx.TypedBuffer", i32, 1, 0, 1) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0_1t(i32 2, i32 0, i32 1, i32 0, ptr nonnull @OutArr.str)
 ; CHECK-NEXT:  call void @llvm.dx.resource.store.typedbuffer.tdx.TypedBuffer_i32_1_0_1t.i32(target("dx.TypedBuffer", i32, 1, 0, 1) %[[HANDLE]], i32 %[[C]], i32 %[[X]])
 ; CHECK-NEXT:  ret void
 entry:
@@ -130,8 +129,7 @@ define void @multiple_use_handle(i32 %x, i1 %cond, i32 %a, i32 %b) {
 ; CHECK-NEXT:  %[[HANDLE0:.*]] = tail call target("dx.TypedBuffer", i32, 1, 0, 1) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0_1t(i32 2, i32 0, i32 1, i32 0, ptr nonnull @OutArr.str)
 ; CHECK:     main:
 ; CHECK-NEXT:  %[[C:.*]] = phi i32 [ %[[A]], %entry ], [ %[[B]], %if.then.i ]
-; CHECK-NEXT:  %[[IDX:.*]] = phi i32 [ 0, %entry ], [ 0, %if.then.i ]
-; CHECK-NEXT:  %[[HANDLE1:.*]] = tail call target("dx.TypedBuffer", i32, 1, 0, 1) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0_1t(i32 2, i32 0, i32 1, i32 %[[IDX]], ptr nonnull @OutArr.str)
+; CHECK-NEXT:  %[[HANDLE1:.*]] = tail call target("dx.TypedBuffer", i32, 1, 0, 1) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0_1t(i32 2, i32 0, i32 1, i32 0, ptr nonnull @OutArr.str)
 ; CHECK-NEXT:  %[[LOAD:.*]] = call { i32, i1 } @llvm.dx.resource.load.typedbuffer.i32.tdx.TypedBuffer_i32_1_0_1t(target("dx.TypedBuffer", i32, 1, 0, 1) %[[HANDLE1]], i32 %[[C]])
 ; CHECK-NEXT:  %[[Y:.*]] = extractvalue { i32, i1 } %[[LOAD]], 0
 ; CHECK-NEXT:  %[[ADD:.*]] = add i32 %[[Y]], %[[X]]


        


More information about the llvm-commits mailing list