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
243
244
245
246
247
248
249
|
#include "slang-ir-insert-debug-value-store.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"
namespace Slang
{
struct DebugValueStoreContext
{
Dictionary<IRType*, bool> m_mapTypeToDebugability;
bool isTypeKind(IRInst* inst)
{
if (!inst)
return true;
switch (inst->getOp())
{
case kIROp_TypeKind:
case kIROp_TypeType: return true;
default: return false;
}
}
bool isDebuggableType(IRType* type)
{
if (bool* result = m_mapTypeToDebugability.tryGetValue(type))
return *result;
bool debuggable = false;
switch (type->getOp())
{
case kIROp_VoidType: break;
case kIROp_StructType:
{
auto structType = static_cast<IRStructType*>(type);
bool structDebuggable = true;
for (auto field : structType->getFields())
{
if (!isDebuggableType(field->getFieldType()))
{
structDebuggable = false;
break;
}
}
debuggable = structDebuggable;
break;
}
case kIROp_ArrayType:
case kIROp_UnsizedArrayType:
{
auto arrayType = static_cast<IRArrayTypeBase*>(type);
debuggable = isDebuggableType(arrayType->getElementType());
break;
}
case kIROp_VectorType:
case kIROp_MatrixType:
case kIROp_PtrType: debuggable = true; break;
case kIROp_Param:
// Assume generic parameters are debuggable.
debuggable = true;
break;
case kIROp_Specialize:
{
auto specType = as<IRSpecialize>(type);
auto specTypeDebuggable =
isDebuggableType((IRType*)getResolvedInstForDecorations(specType));
if (!specTypeDebuggable)
break;
for (UInt i = 0; i < specType->getArgCount(); i++)
{
auto arg = specType->getArg(i);
if (isTypeKind(arg->getDataType()) &&
!isDebuggableType((IRType*)specType->getArg(i)))
{
specTypeDebuggable = false;
break;
}
}
debuggable = false; // specTypeDebuggable;
break;
}
default:
if (as<IRBasicType>(type))
debuggable = true;
break;
}
m_mapTypeToDebugability[type] = debuggable;
return debuggable;
}
void insertDebugValueStore(IRFunc* func)
{
IRBuilder builder(func);
Dictionary<IRInst*, IRInst*> mapVarToDebugVar;
auto firstBlock = func->getFirstBlock();
if (!firstBlock)
return;
auto funcDebugLoc = func->findDecoration<IRDebugLocationDecoration>();
if (!funcDebugLoc)
return;
List<IRInst*> params;
for (auto param : firstBlock->getParams())
{
params.add(param);
}
Index paramIndex = 0;
for (auto param : params)
{
builder.setInsertBefore(firstBlock->getFirstOrdinaryInst());
auto paramType = param->getDataType();
bool isRefParam = false;
if (auto outType = as<IROutTypeBase>(paramType))
{
isRefParam = true;
paramType = outType->getValueType();
}
if (!isDebuggableType(paramType))
continue;
auto debugVar = builder.emitDebugVar(
paramType,
funcDebugLoc->getSource(),
funcDebugLoc->getLine(),
funcDebugLoc->getCol(),
builder.getIntValue(builder.getUIntType(), paramIndex));
copyNameHintAndDebugDecorations(debugVar, param);
mapVarToDebugVar[param] = debugVar;
// Store the initial value of the parameter into the debug var.
IRInst* paramVal = nullptr;
if (!isRefParam)
paramVal = param;
else if (as<IRInOutType>(param->getDataType()))
paramVal = builder.emitLoad(param);
if (paramVal)
{
builder.emitDebugValue(debugVar, paramVal);
}
paramIndex++;
}
for (auto block : func->getBlocks())
{
IRInst* nextInst = nullptr;
for (auto inst = block->getFirstInst(); inst; inst = nextInst)
{
nextInst = inst->getNextInst();
if (auto varInst = as<IRVar>(inst))
{
if (auto debugLoc = varInst->findDecoration<IRDebugLocationDecoration>())
{
auto varType = tryGetPointedToType(&builder, varInst->getDataType());
builder.setInsertBefore(varInst);
if (!isDebuggableType(varType))
continue;
auto debugVar = builder.emitDebugVar(
varType,
debugLoc->getSource(),
debugLoc->getLine(),
debugLoc->getCol());
copyNameHintAndDebugDecorations(debugVar, varInst);
mapVarToDebugVar[varInst] = debugVar;
}
}
}
}
// Collect all stores and insert debug value insts to update debug vars.
// Helper func to insert debugValue updates.
auto setDebugValue = [&](IRInst* debugVar, IRInst* newValue, ArrayView<IRInst*> accessChain)
{
auto ptr = builder.emitElementAddress(debugVar, accessChain);
builder.emitDebugValue(ptr, newValue);
};
for (auto block : func->getBlocks())
{
IRInst* nextInst = nullptr;
for (auto inst = block->getFirstInst(); inst; inst = nextInst)
{
nextInst = inst->getNextInst();
if (auto storeInst = as<IRStore>(inst))
{
List<IRInst*> accessChain;
auto varInst = getRootAddr(storeInst->getPtr(), accessChain);
IRInst* debugVar = nullptr;
if (mapVarToDebugVar.tryGetValue(varInst, debugVar))
{
builder.setInsertAfter(storeInst);
setDebugValue(debugVar, storeInst->getVal(), accessChain.getArrayView());
}
}
else if (auto swizzledStore = as<IRSwizzledStore>(inst))
{
List<IRInst*> accessChain;
auto varInst = getRootAddr(swizzledStore->getDest(), accessChain);
IRInst* debugVar = nullptr;
if (mapVarToDebugVar.tryGetValue(varInst, debugVar))
{
builder.setInsertAfter(swizzledStore);
auto loadVal = builder.emitLoad(swizzledStore->getDest());
setDebugValue(debugVar, loadVal, accessChain.getArrayView());
}
}
else if (auto callInst = as<IRCall>(inst))
{
auto funcValue = getResolvedInstForDecorations(callInst->getCallee());
if (!funcValue)
continue;
for (UInt i = 0; i < callInst->getArgCount(); i++)
{
auto arg = callInst->getArg(i);
if (!as<IRPtrTypeBase>(arg->getDataType()))
continue;
List<IRInst*> accessChain;
auto varInst = getRootAddr(arg, accessChain);
IRInst* debugVar = nullptr;
if (mapVarToDebugVar.tryGetValue(varInst, debugVar))
{
builder.setInsertAfter(callInst);
auto loadVal = builder.emitLoad(arg);
setDebugValue(debugVar, loadVal, accessChain.getArrayView());
}
}
}
}
}
}
};
void insertDebugValueStore(IRModule* module)
{
DebugValueStoreContext context;
for (auto globalInst : module->getGlobalInsts())
{
if (auto genericInst = as<IRGeneric>(globalInst))
{
if (auto func = as<IRFunc>(findGenericReturnVal(genericInst)))
{
context.insertDebugValueStore(func);
}
}
else if (auto func = as<IRFunc>(globalInst))
{
context.insertDebugValueStore(func);
}
}
}
} // namespace Slang
|