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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// slang-ir-type-set.cpp
#include "slang-ir-type-set.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"
namespace Slang
{
IRTypeSet::IRTypeSet(Session* session)
{
m_sharedBuilder.module = nullptr;
m_sharedBuilder.session = session;
m_builder.sharedBuilder = &m_sharedBuilder;
m_module = m_builder.createModule();
m_sharedBuilder.module = m_module;
m_builder.setInsertInto(m_module->getModuleInst());
}
IRTypeSet::~IRTypeSet()
{
_clearTypes();
}
void IRTypeSet::clear()
{
_clearTypes();
m_cloneMap.Clear();
m_module = m_builder.createModule();
m_sharedBuilder.module = m_module;
m_builder.setInsertInto(m_module->getModuleInst());
}
void IRTypeSet::_clearTypes()
{
List<IRType*> types;
getTypes(types);
for (auto type : types)
{
// We need to destroy references to instructions in other modules
if (type->getModule() == m_module)
{
// We want to remove arguments because an argument *could* be an instruction in another module,
// and we don't want to those modules insts to have uses, in this module which is being destroyed
type->removeArguments();
}
}
}
IRInst* IRTypeSet::cloneInst(IRInst* inst)
{
if (inst == nullptr)
{
return nullptr;
}
// See if it's already cloned
if (IRInst*const* newInstPtr = m_cloneMap.TryGetValue(inst))
{
return *newInstPtr;
}
IRModule* module = inst->getModule();
// All inst's must belong to a module
SLANG_ASSERT(module);
// If it's in this module then we don't need to clone
if (module == m_module)
{
return inst;
}
if (isNominalOp(inst->getOp()))
{
// We can clone without any definition, and add the linkage
// TODO(JS)
// This is arguably problematic - I'm adding an instruction from another module to the map, to be it's self.
// I did have code which created a copy of the nominal instruction and name hint, but because nominality means
// 'same address' other code would generate a different name for that instruction (say as compared to being a member in
// the original instruction)
//
// Because I use findOrAddInst which doesn't hoist instructions, the hoisting doesn't rely on parenting, that would
// break.
// If nominal, we just use the original inst
m_cloneMap.Add(inst, inst);
return inst;
}
// It would be nice if I could use ir-clone.cpp to do this -> but it doesn't clone
// operands. We wouldn't want to clone decorations, and it can't clone IRConstant(!) so
// it's no use
IRInst* clone = nullptr;
switch (inst->getOp())
{
case kIROp_IntLit:
{
auto intLit = static_cast<IRConstant*>(inst);
IRType* clonedType = cloneType(intLit->getDataType());
clone = m_builder.getIntValue(clonedType, intLit->value.intVal);
break;
}
case kIROp_StringLit:
{
auto stringLit = static_cast<IRStringLit*>(inst);
clone = m_builder.getStringValue(stringLit->getStringSlice());
break;
}
case kIROp_VectorType:
{
auto vecType = static_cast<IRVectorType*>(inst);
const Index elementCount = Index(getIntVal(vecType->getElementCount()));
if (elementCount <= 1)
{
clone = cloneType(vecType->getElementType());
}
break;
}
case kIROp_MatrixType:
{
auto matType = static_cast<IRMatrixType*>(inst);
const Index columnCount = Index(getIntVal(matType->getColumnCount()));
const Index rowCount = Index(getIntVal(matType->getRowCount()));
if (columnCount <= 1 && rowCount <= 1)
{
clone = cloneType(matType->getElementType());
}
break;
}
default: break;
}
if (!clone)
{
if (IRBasicType::isaImpl(inst->getOp()))
{
clone = m_builder.getType(inst->getOp());
}
else
{
IRType* irType = dynamicCast<IRType>(inst);
if (irType)
{
auto clonedType = cloneType(inst->getFullType());
Index operandCount = Index(inst->getOperandCount());
List<IRInst*> cloneOperands;
cloneOperands.setCount(operandCount);
for (Index i = 0; i < operandCount; ++i)
{
cloneOperands[i] = cloneInst(inst->getOperand(i));
}
//clone = m_irBuilder.findOrEmitHoistableInst(cloneType, inst->op, operandCount, cloneOperands.getBuffer());
UInt operandCounts[1] = { UInt(operandCount) };
IRInst*const* listOperands[1] = { cloneOperands.getBuffer() };
clone = m_builder.findOrAddInst(clonedType, inst->getOp(), 1, operandCounts, listOperands);
}
else
{
// This cloning style only works on insts that are not unique
auto clonedType = cloneType(inst->getFullType());
Index operandCount = Index(inst->getOperandCount());
clone = m_builder.emitIntrinsicInst(clonedType, inst->getOp(), operandCount, nullptr);
for (Index i = 0; i < operandCount; ++i)
{
auto cloneOperand = cloneInst(inst->getOperand(i));
clone->getOperands()[i].init(clone, cloneOperand);
}
}
}
}
m_cloneMap.Add(inst, clone);
return clone;
}
IRType* IRTypeSet::add(IRType* irType)
{
if (irType->getModule() == m_module)
{
return irType;
}
// We need to clone the type
return cloneType(irType);
}
void IRTypeSet::getTypes(List<IRType*>& outTypes) const
{
outTypes.clear();
for (auto inst : m_module->getModuleInst()->getChildren())
{
if (IRType* type = as<IRType>(inst))
{
outTypes.add(type);
}
}
}
void IRTypeSet::getTypes(Kind kind, List<IRType*>& outTypes) const
{
outTypes.clear();
for (auto inst : m_module->getModuleInst()->getChildren())
{
IRType* type = nullptr;
switch (kind)
{
case Kind::Scalar:
{
type = as<IRBasicType>(inst);
break;
}
case Kind::Vector:
{
type = as<IRVectorType>(inst);
break;
}
case Kind::Matrix:
{
type = as<IRMatrixType>(inst);
break;
}
default: break;
}
if (type)
{
outTypes.add(type);
}
}
}
IRType* IRTypeSet::addVectorType(IRType* inElementType, int colsCount)
{
IRType* elementType = cloneType(inElementType);
if (colsCount == 1)
{
return elementType;
}
return m_builder.getVectorType(elementType, m_builder.getIntValue(m_builder.getIntType(), colsCount));
}
void IRTypeSet::addVectorForMatrixTypes()
{
// Make a copy so we can alter m_types dictionary
List<IRType*> types;
getTypes(Kind::Matrix, types);
for (IRType* type : types)
{
SLANG_ASSERT(as<IRMatrixType>(type));
IRMatrixType* matType = static_cast<IRMatrixType*>(type);
m_builder.getVectorType(matType->getElementType(), matType->getColumnCount());
}
}
static bool _hasNominalOperand(IRInst* inst)
{
const Index operandCount = Index(inst->getOperandCount());
auto operands = inst->getOperands();
for (Index i = 0; i < operandCount; ++i)
{
IRInst* operand = operands[i].get();
if (isNominalOp(operand->getOp()))
{
return true;
}
}
return false;
}
void IRTypeSet::_addAllBuiltinTypesRec(IRInst* inst)
{
for (IRInst* child = inst->getFirstDecorationOrChild(); child; child = child->getNextInst())
{
IRType* type = nullptr;
if (auto vectorType = as<IRVectorType>(child))
{
type = vectorType;
}
else if (auto matrixType = as<IRMatrixType>(child))
{
type = matrixType;
}
if (type && !_hasNominalOperand(type))
{
add(type);
}
else
{
_addAllBuiltinTypesRec(child);
}
}
}
void IRTypeSet::addAllBuiltinTypes(IRModule* module)
{
_addAllBuiltinTypesRec(module->getModuleInst());
}
}
|