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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
from typing import Optional, List
# Base class for an instruction. To implement a basic instruction that doesn't
# impact the control-flow, create a new class inheriting from this.
class Instruction:
# Contains the name of the output register, if any.
_result: Optional[str]
# Contains the instruction opcode.
_opcode: str
# Contains all the instruction operands, except result and opcode.
_operands: List[str]
def __init__(self, line: str):
self.line = line
tokens = line.split()
if len(tokens) > 1 and tokens[1] == "=":
self._result = tokens[0]
self._opcode = tokens[2]
self._operands = tokens[3:] if len(tokens) > 2 else []
else:
self._result = None
self._opcode = tokens[0]
self._operands = tokens[1:] if len(tokens) > 1 else []
def __str__(self):
if self._result is None:
return f" {self._opcode} {self._operands}"
return f"{self._result:3} = {self._opcode} {self._operands}"
# Returns the instruction opcode.
def opcode(self) -> str:
return self._opcode
# Returns the instruction operands.
def operands(self) -> List[str]:
return self._operands
# Returns the instruction output register. Calling this function is
# only allowed if has_output_register() is true.
def output_register(self) -> str:
assert self._result is not None
return self._result
# Returns true if this function has an output register. False otherwise.
def has_output_register(self) -> bool:
return self._result is not None
# This function is used to initialize state related to this instruction
# before module execution begins. For example, global Input variables
# can use this to store the lane ID into the register.
def static_execution(self, lane):
pass
# This function is called everytime this instruction is executed by a
# tangle. This function should not be directly overriden, instead see
# _impl and _advance_ip.
def runtime_execution(self, module, lane):
self._impl(module, lane)
self._advance_ip(module, lane)
# This function needs to be overriden if your instruction can be executed.
# It implements the logic of the instruction.
# 'Static' instructions like OpConstant should not override this since
# they are not supposed to be executed at runtime.
def _impl(self, module, lane):
raise RuntimeError(f"Unimplemented instruction {self}")
# By default, IP is incremented to point to the next instruction.
# If the instruction modifies IP (like OpBranch), this must be overridden.
def _advance_ip(self, module, lane):
lane.set_ip(lane.ip() + 1)
# Those are parsed, but never executed.
class OpEntryPoint(Instruction):
pass
class OpFunction(Instruction):
pass
class OpFunctionEnd(Instruction):
pass
class OpLabel(Instruction):
pass
class OpVariable(Instruction):
pass
class OpName(Instruction):
def name(self) -> str:
return self._operands[1][1:-1]
def decoratedRegister(self) -> str:
return self._operands[0]
# The only decoration we use if the BuiltIn one to initialize the values.
class OpDecorate(Instruction):
def static_execution(self, lane):
if self._operands[1] == "LinkageAttributes":
return
assert (
self._operands[1] == "BuiltIn"
and self._operands[2] == "SubgroupLocalInvocationId"
)
lane.set_register(self._operands[0], lane.tid())
# Constants
class OpConstant(Instruction):
def static_execution(self, lane):
lane.set_register(self._result, int(self._operands[1]))
class OpConstantTrue(OpConstant):
def static_execution(self, lane):
lane.set_register(self._result, True)
class OpConstantFalse(OpConstant):
def static_execution(self, lane):
lane.set_register(self._result, False)
class OpConstantComposite(OpConstant):
def static_execution(self, lane):
result = []
for op in self._operands[1:]:
result.append(lane.get_register(op))
lane.set_register(self._result, result)
# Control flow instructions
class OpFunctionCall(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
entry = module.get_function_entry(self._operands[1])
lane.do_call(entry, self._result)
class OpReturn(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
lane.do_return(None)
class OpReturnValue(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
lane.do_return(lane.get_register(self._operands[0]))
class OpBranch(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
lane.set_ip(module.get_bb_entry(self._operands[0]))
pass
class OpBranchConditional(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
condition = lane.get_register(self._operands[0])
if condition:
lane.set_ip(module.get_bb_entry(self._operands[1]))
else:
lane.set_ip(module.get_bb_entry(self._operands[2]))
class OpSwitch(Instruction):
def _impl(self, module, lane):
pass
def _advance_ip(self, module, lane):
value = lane.get_register(self._operands[0])
default_label = self._operands[1]
i = 2
while i < len(self._operands):
imm = int(self._operands[i])
label = self._operands[i + 1]
if value == imm:
lane.set_ip(module.get_bb_entry(label))
return
i += 2
lane.set_ip(module.get_bb_entry(default_label))
class OpUnreachable(Instruction):
def _impl(self, module, lane):
raise RuntimeError("This instruction should never be executed.")
# Convergence instructions
class MergeInstruction(Instruction):
def merge_location(self):
return self._operands[0]
def continue_location(self):
return None if len(self._operands) < 3 else self._operands[1]
def _impl(self, module, lane):
lane.handle_convergence_header(self)
class OpLoopMerge(MergeInstruction):
pass
class OpSelectionMerge(MergeInstruction):
pass
# Other instructions
class OpBitcast(Instruction):
def _impl(self, module, lane):
# TODO: find out the type from the defining instruction.
# This can only work for DXC.
if self._operands[0] == "%int":
lane.set_register(self._result, int(lane.get_register(self._operands[1])))
else:
raise RuntimeError("Unsupported OpBitcast operand")
class OpAccessChain(Instruction):
def _impl(self, module, lane):
# Python dynamic types allows me to simplify. As long as the SPIR-V
# is legal, this should be fine.
# Note: SPIR-V structs are stored as tuples
value = lane.get_register(self._operands[1])
for operand in self._operands[2:]:
value = value[lane.get_register(operand)]
lane.set_register(self._result, value)
class OpCompositeConstruct(Instruction):
def _impl(self, module, lane):
output = []
for op in self._operands[1:]:
output.append(lane.get_register(op))
lane.set_register(self._result, output)
class OpCompositeExtract(Instruction):
def _impl(self, module, lane):
value = lane.get_register(self._operands[1])
output = value
for op in self._operands[2:]:
output = output[int(op)]
lane.set_register(self._result, output)
class OpStore(Instruction):
def _impl(self, module, lane):
lane.set_register(self._operands[0], lane.get_register(self._operands[1]))
class OpLoad(Instruction):
def _impl(self, module, lane):
lane.set_register(self._result, lane.get_register(self._operands[1]))
class OpIAdd(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS + RHS)
class OpISub(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS - RHS)
class OpIMul(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS * RHS)
class OpLogicalNot(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
lane.set_register(self._result, not LHS)
class _LessThan(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS < RHS)
class _GreaterThan(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS > RHS)
class OpSLessThan(_LessThan):
pass
class OpULessThan(_LessThan):
pass
class OpSGreaterThan(_GreaterThan):
pass
class OpUGreaterThan(_GreaterThan):
pass
class OpIEqual(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS == RHS)
class OpINotEqual(Instruction):
def _impl(self, module, lane):
LHS = lane.get_register(self._operands[1])
RHS = lane.get_register(self._operands[2])
lane.set_register(self._result, LHS != RHS)
class OpPhi(Instruction):
def _impl(self, module, lane):
previousBBName = lane.get_previous_bb_name()
i = 1
while i < len(self._operands):
label = self._operands[i + 1]
if label == previousBBName:
lane.set_register(self._result, lane.get_register(self._operands[i]))
return
i += 2
raise RuntimeError("previousBB not in the OpPhi _operands")
class OpSelect(Instruction):
def _impl(self, module, lane):
condition = lane.get_register(self._operands[1])
value = lane.get_register(self._operands[2 if condition else 3])
lane.set_register(self._result, value)
# Wave intrinsics
class OpGroupNonUniformBroadcastFirst(Instruction):
def _impl(self, module, lane):
assert lane.get_register(self._operands[1]) == 3
if lane.is_first_active_lane():
lane.broadcast_register(self._result, lane.get_register(self._operands[2]))
class OpGroupNonUniformElect(Instruction):
def _impl(self, module, lane):
lane.set_register(self._result, lane.is_first_active_lane())
|