1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#include "slang-ir-defer-buffer-load.h"
#include "slang-ir-clone.h"
#include "slang-ir-dominators.h"
#include "slang-ir-insts.h"
#include "slang-ir-redundancy-removal.h"
#include "slang-ir-util.h"
#include "slang-ir.h"
namespace Slang
{
struct DeferBufferLoadContext
{
// Map an original SSA value to a pointer that can be used to load the value.
Dictionary<IRInst*, IRInst*> mapValueToPtr;
// Map an ptr to its loaded value.
Dictionary<IRInst*, IRInst*> mapPtrToValue;
IRFunc* currentFunc = nullptr;
// Ensure that for an original SSA value, we have formed a pointer that can be used to load the
// value.
IRInst* ensurePtr(IRInst* valueInst)
{
IRInst* result = nullptr;
if (mapValueToPtr.tryGetValue(valueInst, result))
return result;
IRBuilder b(valueInst);
b.setInsertBefore(valueInst);
switch (valueInst->getOp())
{
case kIROp_StructuredBufferLoad:
case kIROp_StructuredBufferLoadStatus:
{
result = b.emitRWStructuredBufferGetElementPtr(
valueInst->getOperand(0),
valueInst->getOperand(1));
break;
}
case kIROp_GetElement:
{
auto ptr = ensurePtr(valueInst->getOperand(0));
if (!ptr)
return nullptr;
result = b.emitElementAddress(ptr, valueInst->getOperand(1));
break;
}
case kIROp_FieldExtract:
{
auto ptr = ensurePtr(valueInst->getOperand(0));
if (!ptr)
return nullptr;
result = b.emitFieldAddress(ptr, valueInst->getOperand(1));
break;
}
case kIROp_Load:
result = valueInst->getOperand(0);
break;
}
if (result)
{
mapValueToPtr[valueInst] = result;
}
return result;
}
static bool isImmutableBufferLoad(IRInst* inst)
{
// Note: we cannot defer loads from RWStructuredBuffer because there can be other
// instructions that modify the buffer.
switch (inst->getOp())
{
case kIROp_StructuredBufferLoad:
case kIROp_StructuredBufferLoadStatus:
return true;
case kIROp_Load:
{
auto rootAddr = getRootAddr(inst->getOperand(0));
return isPointerToImmutableLocation(rootAddr);
}
default:
return false;
}
}
// Ensure that for a pointer value, we have created a load instruction to materialize the value.
IRInst* materializePointer(IRBuilder& builder, IRInst* loadInst)
{
auto ptr = ensurePtr(loadInst);
if (!ptr)
return nullptr;
IRInst* result = nullptr;
if (mapPtrToValue.tryGetValue(ptr, result))
return result;
IRAlignedAttr* align = nullptr;
if (auto load = as<IRLoad>(loadInst))
align = load->findAttr<IRAlignedAttr>();
if (!as<IRModuleInst>(ptr->getParent()))
{
setInsertAfterOrdinaryInst(&builder, ptr);
IRType* valueType = tryGetPointedToType(&builder, ptr->getFullType());
result = builder.emitLoad(valueType, ptr, align);
mapPtrToValue[ptr] = result;
}
else
{
setInsertBeforeOrdinaryInst(&builder, loadInst);
IRType* valueType = tryGetPointedToType(&builder, ptr->getFullType());
result = builder.emitLoad(valueType, ptr, align);
// Since we are inserting the load in a local scope, we can't register
// the mapping to the pointer, since the global pointer needs to be
// loaded once per function.
}
return result;
}
static bool isSimpleType(IRInst* type)
{
if (auto modType = as<IRRateQualifiedType>(type))
type = modType->getValueType();
if (as<IRStructType>(type))
return false;
if (as<IRTupleType>(type))
return false;
if (as<IRArrayTypeBase>(type))
return false;
return true;
}
void deferBufferLoadInst(IRBuilder& builder, List<IRInst*>& workList, IRInst* loadInst)
{
// Don't defer the load anymore if the type is simple.
if (isSimpleType(loadInst->getDataType()) || loadInst->findAttr<IRAlignedAttr>())
{
auto materializedVal = materializePointer(builder, loadInst);
loadInst->transferDecorationsTo(materializedVal);
loadInst->replaceUsesWith(materializedVal);
return;
}
// Otherwise, look for all uses and try to defer the load before actual use of the value.
ShortList<IRInst*> pendingWorkList;
bool needMaterialize = false;
traverseUses(
loadInst,
[&](IRUse* use)
{
if (needMaterialize)
return;
auto user = use->getUser();
switch (user->getOp())
{
case kIROp_GetElement:
case kIROp_FieldExtract:
{
auto basePtr = ensurePtr(loadInst);
if (!basePtr)
return;
pendingWorkList.add(user);
}
break;
default:
needMaterialize = true;
return;
}
});
if (needMaterialize)
{
auto val = materializePointer(builder, loadInst);
loadInst->transferDecorationsTo(val);
loadInst->replaceUsesWith(val);
loadInst->removeAndDeallocate();
}
else
{
// Append to worklist in reverse order so we process the uses in natural appearance
// order.
for (Index i = pendingWorkList.getCount() - 1; i >= 0; i--)
workList.add(pendingWorkList[i]);
}
}
void deferBufferLoadInFunc(IRFunc* func)
{
removeRedundancyInFunc(func, false);
currentFunc = func;
List<IRInst*> workList;
for (auto block : func->getBlocks())
{
for (auto inst : block->getChildren())
{
if (isImmutableBufferLoad(inst))
{
workList.add(inst);
}
}
}
IRBuilder builder(func);
for (Index i = 0; i < workList.getCount(); i++)
{
auto inst = workList[i];
deferBufferLoadInst(builder, workList, inst);
}
}
void deferBufferLoad(IRGlobalValueWithCode* inst)
{
if (auto func = as<IRFunc>(inst))
{
deferBufferLoadInFunc(func);
}
else if (auto generic = as<IRGeneric>(inst))
{
auto inner = findGenericReturnVal(generic);
if (auto innerFunc = as<IRFunc>(inner))
deferBufferLoadInFunc(innerFunc);
}
}
};
void deferBufferLoad(IRModule* module)
{
DeferBufferLoadContext context;
for (auto childInst : module->getGlobalInsts())
{
if (auto code = as<IRGlobalValueWithCode>(childInst))
{
context.deferBufferLoad(code);
}
}
}
} // namespace Slang
|