Skip to content

Commit 1ce2c33

Browse files
bognerdvbuka
authored andcommitted
[SPIRV][HLSL] Fix assert with cbuffers through constexpr (llvm#164555)
The comment here pointed out that RAUW would fall over given a constantexpr, but then proceeded to just do what RAUW does by hand, which falls over in the same way. Instead, convert constantexprs involving cbuffer globals to instructions before processing them. The test update just modifies the existing cbuffer test, since it implied it was trying to test this exact case anyways.
1 parent e789e05 commit 1ce2c33

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/lib/Target/SPIRV/SPIRVCBufferAccess.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/IR/IRBuilder.h"
3636
#include "llvm/IR/IntrinsicsSPIRV.h"
3737
#include "llvm/IR/Module.h"
38+
#include "llvm/IR/ReplaceConstant.h"
3839

3940
#define DEBUG_TYPE "spirv-cbuffer-access"
4041
using namespace llvm;
@@ -57,6 +58,12 @@ static bool replaceCBufferAccesses(Module &M) {
5758
if (!CBufMD)
5859
return false;
5960

61+
SmallVector<Constant *> CBufferGlobals;
62+
for (const hlsl::CBufferMapping &Mapping : *CBufMD)
63+
for (const hlsl::CBufferMember &Member : Mapping.Members)
64+
CBufferGlobals.push_back(Member.GV);
65+
convertUsersOfConstantsToInstructions(CBufferGlobals);
66+
6067
for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
6168
Instruction *HandleDef = findHandleDef(Mapping.Handle);
6269
if (!HandleDef) {
@@ -80,12 +87,7 @@ static bool replaceCBufferAccesses(Module &M) {
8087
Value *GetPointerCall = Builder.CreateIntrinsic(
8188
PtrType, Intrinsic::spv_resource_getpointer, {HandleDef, IndexVal});
8289

83-
// We cannot use replaceAllUsesWith here because some uses may be
84-
// ConstantExprs, which cannot be replaced with non-constants.
85-
SmallVector<User *, 4> Users(MemberGV->users());
86-
for (User *U : Users) {
87-
U->replaceUsesOfWith(MemberGV, GetPointerCall);
88-
}
90+
MemberGV->replaceAllUsesWith(GetPointerCall);
8991
}
9092
}
9193

llvm/test/CodeGen/SPIRV/hlsl-resources/cbuffer.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv1.6-vulkan1.3-library %s -o - | FileCheck %s
2-
; Test that uses of cbuffer members inside ConstantExprs are handled correctly.
2+
; Test that uses of cbuffer members are handled correctly.
33

44
; CHECK-DAG: OpDecorate %[[MyCBuffer:[0-9]+]] DescriptorSet 0
55
; CHECK-DAG: OpDecorate %[[MyCBuffer]] Binding 0
@@ -37,10 +37,8 @@ entry:
3737
; CHECK: %[[tmp_ptr:[0-9]+]] = OpAccessChain {{%[0-9]+}} %[[tmp]] %[[uint_0]] %[[uint_0]]
3838
; CHECK: %[[v_ptr:.+]] = OpAccessChain %[[_ptr_Uniform_v4float]] %[[tmp]] %[[uint_0]] %[[uint_1]]
3939
; CHECK: %[[s_ptr_gep:[0-9]+]] = OpInBoundsAccessChain %[[_ptr_Uniform_float]] %[[tmp_ptr]] %[[uint_0]] %[[uint_1]]
40-
%gep = getelementptr inbounds %MyStruct, ptr addrspace(12) @s, i32 0, i32 0, i32 1
41-
4240
; CHECK: %[[s_val:.+]] = OpLoad %[[float]] %[[s_ptr_gep]]
43-
%load_from_gep = load float, ptr addrspace(12) %gep, align 4
41+
%load_from_gep = load float, ptr addrspace(12) getelementptr inbounds (%MyStruct, ptr addrspace(12) @s, i32 0, i32 0, i32 1), align 4
4442

4543
; CHECK: %[[v_val:.+]] = OpLoad %[[v4float]] %[[v_ptr]]
4644
%load_v = load <4 x float>, ptr addrspace(12) @v, align 16

0 commit comments

Comments
 (0)