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
|
#include "slang-ir-lower-bit-cast.h"
#include "slang-ir-extract-value-from-type.h"
#include "slang-ir-insts.h"
#include "slang-ir-layout.h"
#include "slang-ir.h"
namespace Slang
{
struct BitCastLoweringContext
{
TargetProgram* targetProgram;
IRModule* module;
OrderedHashSet<IRInst*> workList;
DiagnosticSink* sink;
void addToWorkList(IRInst* inst)
{
for (auto ii = inst->getParent(); ii; ii = ii->getParent())
{
if (as<IRGeneric>(ii))
return;
}
if (workList.contains(inst))
return;
workList.add(inst);
}
void processInst(IRInst* inst)
{
switch (inst->getOp())
{
case kIROp_BitCast:
processBitCast(inst);
break;
default:
break;
}
}
void processModule()
{
addToWorkList(module->getModuleInst());
while (workList.getCount() != 0)
{
IRInst* inst = workList.getLast();
workList.removeLast();
processInst(inst);
for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
{
addToWorkList(child);
}
}
}
// Extract an object of `type` from `offset` in `src`.
IRInst* readObject(IRBuilder& builder, IRInst* src, IRType* type, uint32_t offset)
{
switch (type->getOp())
{
case kIROp_StructType:
{
auto structType = as<IRStructType>(type);
List<IRInst*> fieldValues;
for (auto field : structType->getFields())
{
IRIntegerValue fieldOffset = 0;
SLANG_RELEASE_ASSERT(
getNaturalOffset(targetProgram->getOptionSet(), field, &fieldOffset) ==
SLANG_OK);
auto fieldType = field->getFieldType();
auto fieldValue =
readObject(builder, src, fieldType, (uint32_t)(fieldOffset + offset));
fieldValues.add(fieldValue);
}
return builder.emitMakeStruct(structType, fieldValues);
}
break;
case kIROp_ArrayType:
{
auto arrayType = as<IRArrayType>(type);
auto arrayCount = as<IRIntLit>(arrayType->getElementCount());
SLANG_RELEASE_ASSERT(arrayCount && "bit_cast: array size must be fixed.");
List<IRInst*> elements;
IRSizeAndAlignment elementLayout;
SLANG_RELEASE_ASSERT(
getNaturalSizeAndAlignment(
targetProgram->getOptionSet(),
arrayType->getElementType(),
&elementLayout) == SLANG_OK);
for (IRIntegerValue i = 0; i < arrayCount->value.intVal; i++)
{
elements.add(readObject(
builder,
src,
arrayType->getElementType(),
(uint32_t)(offset + elementLayout.getStride() * i)));
}
return builder.emitMakeArray(
arrayType,
(UInt)arrayCount->value.intVal,
elements.getBuffer());
}
break;
case kIROp_VectorType:
{
auto vectorType = as<IRVectorType>(type);
auto elementCount = as<IRIntLit>(vectorType->getElementCount());
SLANG_RELEASE_ASSERT(elementCount && "bit_cast: vector size must be int literal.");
List<IRInst*> elements;
IRSizeAndAlignment elementLayout;
SLANG_RELEASE_ASSERT(
getNaturalSizeAndAlignment(
targetProgram->getOptionSet(),
vectorType->getElementType(),
&elementLayout) == SLANG_OK);
for (IRIntegerValue i = 0; i < elementCount->value.intVal; i++)
{
elements.add(readObject(
builder,
src,
vectorType->getElementType(),
(uint32_t)(offset + elementLayout.getStride() * i)));
}
return builder.emitMakeVector(
vectorType,
(UInt)elementCount->value.intVal,
elements.getBuffer());
}
break;
case kIROp_MatrixType:
{
// Assuming row-major order
auto matrixType = as<IRMatrixType>(type);
auto elementCount = as<IRIntLit>(matrixType->getRowCount());
SLANG_RELEASE_ASSERT(elementCount && "bit_cast: vector size must be int literal.");
List<IRInst*> elements;
auto elementType = builder.getVectorType(
matrixType->getElementType(),
matrixType->getColumnCount());
IRSizeAndAlignment elementLayout;
SLANG_RELEASE_ASSERT(
getNaturalSizeAndAlignment(
targetProgram->getOptionSet(),
elementType,
&elementLayout) == SLANG_OK);
for (IRIntegerValue i = 0; i < elementCount->value.intVal; i++)
{
elements.add(readObject(
builder,
src,
elementType,
(uint32_t)(offset + elementLayout.getStride() * i)));
}
return builder.emitMakeMatrix(
matrixType,
(UInt)elementCount->value.intVal,
elements.getBuffer());
}
break;
case kIROp_HalfType:
case kIROp_Int16Type:
case kIROp_UInt16Type:
{
auto object = extractValueAtOffset(builder, targetProgram, src, offset, 2);
object = builder.emitCast(builder.getUInt16Type(), object);
return builder.emitBitCast(type, object);
}
break;
case kIROp_IntType:
case kIROp_UIntType:
case kIROp_FloatType:
case kIROp_BoolType:
#if SLANG_PTR_IS_32
case kIROp_IntPtrType:
case kIROp_UIntPtrType:
#endif
{
auto object = extractValueAtOffset(builder, targetProgram, src, offset, 4);
object = builder.emitCast(builder.getUIntType(), object);
return builder.emitBitCast(type, object);
}
break;
case kIROp_DoubleType:
case kIROp_Int64Type:
case kIROp_UInt64Type:
#if SLANG_PTR_IS_64
case kIROp_IntPtrType:
case kIROp_UIntPtrType:
#endif
case kIROp_RawPointerType:
case kIROp_PtrType:
case kIROp_FuncType:
{
auto object = extractValueAtOffset(builder, targetProgram, src, offset, 8);
object = builder.emitCast(builder.getUInt64Type(), object);
return builder.emitBitCast(type, object);
}
break;
case kIROp_UInt8Type:
case kIROp_Int8Type:
{
auto object = extractValueAtOffset(builder, targetProgram, src, offset, 1);
object = builder.emitCast(builder.getUInt8Type(), object);
return builder.emitBitCast(type, object);
}
break;
default:
{
SLANG_UNEXPECTED("Unable to generate bit_cast code for the given type");
}
break;
}
}
void processBitCast(IRInst* inst)
{
auto operand = inst->getOperand(0);
auto fromType = operand->getDataType();
auto toType = inst->getDataType();
IRSizeAndAlignment toTypeSize;
getNaturalSizeAndAlignment(targetProgram->getOptionSet(), toType, &toTypeSize);
IRSizeAndAlignment fromTypeSize;
getNaturalSizeAndAlignment(targetProgram->getOptionSet(), fromType, &fromTypeSize);
if (as<IRBasicType>(fromType) != nullptr && as<IRBasicType>(toType) != nullptr)
{
if (fromTypeSize.size != toTypeSize.size)
sink->diagnose(
inst->sourceLoc,
Diagnostics::notEqualBitCastSize,
fromType,
fromTypeSize.size,
toType,
toTypeSize.size);
// Both fromType and toType are basic types, no processing needed.
return;
}
// Ignore cases we cannot handle yet.
if (as<IRResourceTypeBase>(fromType) || as<IRResourceTypeBase>(toType))
{
return;
}
if (as<IRPointerLikeType>(fromType) || as<IRPointerLikeType>(toType))
{
return;
}
if (as<IRSamplerStateTypeBase>(fromType) || as<IRSamplerStateTypeBase>(toType))
{
return;
}
if (fromTypeSize.size != toTypeSize.size)
sink->diagnose(
inst->sourceLoc,
Diagnostics::notEqualBitCastSize,
fromType,
fromTypeSize.size,
toType,
toTypeSize.size);
// Enumerate all fields in to-type and obtain its value from operand object.
IRBuilder builder(module);
builder.setInsertBefore(inst);
auto finalObject = readObject(builder, operand, toType, 0);
inst->replaceUsesWith(finalObject);
inst->removeAndDeallocate();
}
};
void lowerBitCast(TargetProgram* targetProgram, IRModule* module, DiagnosticSink* sink)
{
BitCastLoweringContext context;
context.module = module;
context.targetProgram = targetProgram;
context.sink = sink;
context.processModule();
}
} // namespace Slang
|