Skip to content

Commit 04672e2

Browse files
authored
[DirectX] ForwardHandle needs to check if globals were stored on allocas (#151751)
fixes #140819 SROA pass is making it so that some globals get loaded into stack allocations. This means we find an alloca where we use to expect a load and now need to walk an alloca -> store -> maybe load chain before we find the global. Doing so fixes All but two instances of #137715 And fixes every instance of `Load of "8.sroa.0" is not a global resource handle we are currently seeing in the DML shaders.
1 parent 01472d8 commit 04672e2

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

llvm/lib/Target/DirectX/DXILForwardHandleAccesses.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,50 @@ static bool forwardHandleAccesses(Function &F, DominatorTree &DT) {
8787

8888
for (LoadInst *LI : LoadsToProcess) {
8989
Value *V = LI->getPointerOperand();
90-
auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
90+
auto *GV = dyn_cast<GlobalVariable>(V);
9191

9292
// If we didn't find the global, we may need to walk through a level of
9393
// indirection. This generally happens at -O0.
94-
if (!GV)
94+
if (!GV) {
9595
if (auto *NestedLI = dyn_cast<LoadInst>(V)) {
9696
BasicBlock::iterator BBI(NestedLI);
9797
Value *Loaded = FindAvailableLoadedValue(
9898
NestedLI, NestedLI->getParent(), BBI, 0, nullptr, nullptr);
9999
GV = dyn_cast_or_null<GlobalVariable>(Loaded);
100+
} else if (auto *NestedAlloca = dyn_cast<AllocaInst>(V)) {
101+
for (auto &Use : NestedAlloca->uses()) {
102+
auto *Store = dyn_cast<StoreInst>(Use.getUser());
103+
if (!Store)
104+
continue;
105+
106+
Value *StoredVal = Store->getValueOperand();
107+
if (!StoredVal)
108+
continue;
109+
110+
// Try direct global match
111+
GV = dyn_cast<GlobalVariable>(StoredVal);
112+
if (GV)
113+
break;
114+
115+
// If it's a load, check its source
116+
if (auto *Load = dyn_cast<LoadInst>(StoredVal)) {
117+
GV = dyn_cast<GlobalVariable>(Load->getPointerOperand());
118+
if (GV)
119+
break;
120+
121+
// If loading from an unmodified stack copy of the global, reuse the
122+
// global's value. Note: we are just repeating what we are doing for
123+
// the load case for the alloca store pattern.
124+
BasicBlock::iterator BBI(Load);
125+
Value *Loaded = FindAvailableLoadedValue(Load, Load->getParent(),
126+
BBI, 0, nullptr, nullptr);
127+
GV = dyn_cast<GlobalVariable>(Loaded);
128+
if (GV)
129+
break;
130+
}
131+
}
100132
}
133+
}
101134

102135
auto It = HandleMap.find(GV);
103136
if (It == HandleMap.end()) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -dxil-forward-handle-accesses %s | FileCheck %s
3+
4+
%"class.hlsl::RWStructuredBuffer" = type { target("dx.RawBuffer", i32, 1, 0) }
5+
@global = internal unnamed_addr global %"class.hlsl::RWStructuredBuffer" poison, align 4
6+
@name = private unnamed_addr constant [5 x i8] c"dest\00", align 1
7+
8+
9+
; NOTE: intent of this test is to confirm load target("dx.RawBuffer", i32, 1, 0)
10+
; is replaced with call @llvm.dx.resource.getpointer
11+
define void @CSMain() local_unnamed_addr {
12+
; CHECK-LABEL: define void @CSMain() local_unnamed_addr {
13+
; CHECK-NEXT: [[ENTRY:.*:]]
14+
; CHECK-NEXT: [[AGG_TMP_I1_SROA_0:%.*]] = alloca target("dx.RawBuffer", i32, 1, 0), align 8
15+
; CHECK-NEXT: [[TMP0:%.*]] = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 3, i32 1, i32 0, i1 false, ptr nonnull @name)
16+
; CHECK-NEXT: store target("dx.RawBuffer", i32, 1, 0) [[TMP0]], ptr @global, align 4
17+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr @global, align 4
18+
; CHECK-NEXT: store i32 [[TMP2]], ptr [[AGG_TMP_I1_SROA_0]], align 8
19+
; CHECK-NEXT: [[TMP3:%.*]] = tail call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) [[TMP0]], i32 0)
20+
; CHECK-NEXT: store i32 0, ptr [[TMP3]], align 4
21+
; CHECK-NEXT: ret void
22+
;
23+
entry:
24+
%alloca = alloca target("dx.RawBuffer", i32, 1, 0), align 8
25+
%handle = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 3, i32 1, i32 0, i1 false, ptr nonnull @name)
26+
store target("dx.RawBuffer", i32, 1, 0) %handle , ptr @global, align 4
27+
%val = load i32, ptr @global, align 4
28+
store i32 %val , ptr %alloca, align 8
29+
%indirect = load target("dx.RawBuffer", i32, 1, 0), ptr %alloca, align 8
30+
%buff = tail call noundef nonnull align 4 dereferenceable(4) ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0) %indirect, i32 0)
31+
store i32 0, ptr %buff, align 4
32+
ret void
33+
}

0 commit comments

Comments
 (0)