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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
// ir.h
#ifndef SLANG_IR_H_INCLUDED
#define SLANG_IR_H_INCLUDED
// This file defines the intermediate representation (IR) used for Slang
// shader code. This is a typed static single assignment (SSA) IR,
// similar in spirit to LLVM (but much simpler).
//
#include "../core/basic.h"
namespace Slang {
// TODO(tfoley): We should ditch this enumeration
// and just use the IR opcodes that represent these
// types directly. The one major complication there
// is that the order of the enum values currently
// matters, since it determines promotion rank.
// We either need to keep that restriction, or
// look up promotion rank by some other means.
//
enum class BaseType
{
// Note(tfoley): These are ordered in terms of promotion rank, so be vareful when messing with this
Void = 0,
Bool,
Int,
UInt,
UInt64,
Half,
Float,
Double,
};
class Layout;
struct IRFunc;
struct IRInst;
struct IRModule;
struct IRParentInst;
struct IRType;
typedef unsigned int IROpFlags;
enum : IROpFlags
{
kIROpFlags_None = 0,
// This op is a parent op
kIROpFlag_Parent = 1 << 0,
};
enum IROp : int16_t
{
#define INST(ID, MNEMONIC, ARG_COUNT, FLAGS) \
kIROp_##ID,
#include "ir-inst-defs.h"
kIROpCount,
// We use the negative range of opcode values
// to encode "pseudo" instructions that should
// not appear in valid IR.
kIRPseduoOp_FirstPseudo = -1000,
#define INST(ID, MNEMONIC, ARG_COUNT, FLAGS) /* empty */
#define PSEUDO_INST(ID) kIRPseudoOp_##ID,
#include "ir-inst-defs.h"
kIROp_Invalid = -1,
};
#if 0
enum IRPseudoOp
{
kIRPseudoOp_Pos = -1000,
kIRPseudoOp_PreInc,
kIRPseudoOp_PreDec,
kIRPseudoOp_PostInc,
kIRPseudoOp_PostDec,
kIRPseudoOp_Sequence,
kIRPseudoOp_AddAssign,
kIRPseudoOp_SubAssign,
kIRPseudoOp_MulAssign,
kIRPseudoOp_DivAssign,
kIRPseudoOp_ModAssign,
kIRPseudoOp_AndAssign,
kIRPseudoOp_OrAssign,
kIRPseudoOp_XorAssign ,
kIRPseudoOp_LshAssign,
kIRPseudoOp_RshAssign,
kIRPseudoOp_Assign,
kIRPseudoOp_BitNot,
kIRPseudoOp_And,
kIRPseudoOp_Or,
kIROp_Invalid = -1,
};
#endif
IROp findIROp(char const* name);
// A logical operation/opcode in the IR
struct IROpInfo
{
// What is the name/mnemonic for this operation
char const* name;
// How many required arguments are there
// (not including the mandatory type argument)
unsigned int fixedArgCount;
// Flags to control how we emit additional info
IROpFlags flags;
};
// A use of another value/inst within an IR operation
struct IRUse
{
// The value that is doing the using.
IRInst* user;
// The value that is being used
IRInst* usedValue;
// The next use of the same value
IRUse* nextUse;
// A "link" back to where this use is referenced,
// so that we can simplify updates.
IRUse** prevLink;
void init(IRInst* user, IRInst* usedValue);
};
enum IRDecorationOp : uint16_t
{
kIRDecorationOp_HighLevelDecl,
kIRDecorationOp_Layout,
};
// A "decoration" that gets applied to an instruction.
// These usually don't affect semantics, but are useful
// for preserving high-level source information.
struct IRDecoration
{
// Next decoration attached to the same instruction
IRDecoration* next;
IRDecorationOp op;
};
typedef uint32_t IRInstID;
// In the IR, almost *everything* is an instruction,
// in order to make the representation as uniform as possible.
struct IRInst
{
// The operation that this value represents
IROp op;
// A unique ID to represent the op when printing
// (or zero to indicate that the value of this
// op isn't special).
IRInstID id;
// The total number of arguments of this instruction
// (including the type)
uint32_t argCount;
// The parent of this instruction.
// This will often be a basic block, but we
// allow instructions to nest in more general ways.
IRParentInst* parent;
// The next and previous instructions in the same parent block
IRInst* nextInst;
IRInst* prevInst;
// The first use of this value (start of a linked list)
IRUse* firstUse;
// The linked list of decorations attached to this instruction
IRDecoration* firstDecoration;
IRDecoration* findDecorationImpl(IRDecorationOp op);
template<typename T>
T* findDecoration()
{
return (T*) findDecorationImpl(IRDecorationOp(T::kDecorationOp));
}
// The type of this value
IRUse type;
IRType* getType() { return (IRType*) type.usedValue; }
UInt getArgCount()
{
return argCount;
}
IRUse* getArgs();
IRInst* getArg(UInt index)
{
return getArgs()[index].usedValue;
}
};
// This type alias exists because I waffled on the name for a bit.
// All existing uses of `IRValue` should move to `IRInst`
typedef IRInst IRValue;
class Decl;
// Associates an IR-level decoration with a source declaration
// in the high-level AST, that can be used to extract
// additional information that informs code emission.
struct IRHighLevelDeclDecoration : IRDecoration
{
enum { kDecorationOp = kIRDecorationOp_HighLevelDecl };
Decl* decl;
};
// Associates an IR-level decoration with a source layout
struct IRLayoutDecoration : IRDecoration
{
enum { kDecorationOp = kIRDecorationOp_Layout };
Layout* layout;
};
typedef long long IRIntegerValue;
typedef double IRFloatingPointValue;
struct IRConstant : IRInst
{
union
{
IRIntegerValue intVal;
IRFloatingPointValue floatVal;
// HACK: allows us to hash the value easily
void* ptrData[2];
} u;
};
// Representation of a type at the IR level.
// Such a type may not correspond to the high-level-language notion
// of a type as used by the front end.
//
// Note that types are instructions in the IR, so that operations
// may take type operands as easily as values.
struct IRType : IRInst
{
};
struct IRVectorType : IRType
{
IRUse elementType;
IRUse elementCount;
IRType* getElementType() { return (IRType*) elementType.usedValue; }
IRInst* getElementCount() { return elementCount.usedValue; }
};
struct IRFuncType : IRType
{
IRUse resultType;
// parameter tyeps are varargs...
IRType* getResultType() { return (IRType*) resultType.usedValue; }
UInt getParamCount()
{
return getArgCount() - 2;
}
IRType* getParamType(UInt index)
{
return (IRType*) getArg(2 + index);
}
};
struct IRPtrType : IRType
{
IRUse valueType;
IRType* getValueType() { return (IRType*) valueType.usedValue; }
};
struct IRTextureType : IRType
{
IRUse flavor;
IRUse elementType;
IRIntegerValue getFlavor() { return ((IRConstant*) flavor.usedValue)->u.intVal; }
IRType* getElementType() { return (IRType*) elementType.usedValue; }
};
struct IRUniformBufferType : IRType
{
IRUse elementType;
IRType* getElementType() { return (IRType*) elementType.usedValue; }
};
struct IRConstantBufferType : IRUniformBufferType {};
struct IRTextureBufferType : IRUniformBufferType {};
struct IRCall : IRInst
{
IRUse func;
};
struct IRLoad : IRInst
{
IRUse ptr;
};
struct IRStructField;
struct IRFieldExtract : IRInst
{
IRUse base;
IRUse field;
IRInst* getBase() { return base.usedValue; }
IRStructField* getField() { return (IRStructField*) field.usedValue; }
};
struct IRFieldAddress : IRInst
{
IRUse base;
IRUse field;
IRInst* getBase() { return base.usedValue; }
IRStructField* getField() { return (IRStructField*) field.usedValue; }
};
// A instruction that ends a basic block (usually because of control flow)
struct IRTerminatorInst : IRInst
{};
struct IRReturn : IRTerminatorInst
{};
struct IRReturnVal : IRReturn
{
IRUse val;
IRInst* getVal() { return val.usedValue; }
};
struct IRReturnVoid : IRReturn
{};
// A parent instruction contains a sequence of other instructions
//
struct IRParentInst : IRInst
{
// The first and last instruction in the container (or NULL in
// the case that the container is empty).
//
IRInst* firstChild;
IRInst* lastChild;
};
struct IRStructField : IRInst
{
IRType* getFieldType() { return (IRType*) type.usedValue; }
IRStructField* getNextField() { return (IRStructField*) nextInst; }
};
struct IRStructDecl : IRParentInst
{
IRStructField* getFirstField() { return (IRStructField*) firstChild; }
IRStructField* getLastField() { return (IRStructField*) lastChild; }
};
// A basic block is a parent instruction that adds the constraint
// that all the children need to be "ordinary" instructions (so
// no function declarations, or nested blocks). We also expect
// that the previous/next instruction are always a basic block.
//
struct IRBlock : IRParentInst
{
// Note that in a valid program, every block must end with
// a "terminator" instruction, so these should be non-NULL,
// and `last` should actually be an `IRTerminatorInst`.
IRBlock* getPrevBlock() { return (IRBlock*) prevInst; }
IRBlock* getNextBlock() { return (IRBlock*) nextInst; }
};
// A function parameter is represented by an instruction
// in the entry block of a function.
struct IRParam : IRInst
{
IRParam* getNextParam();
};
struct IRVar : IRInst
{};
// A function is a parent to zero or more blocks of instructions.
//
// A function is itself a value, so that it can be a direct operand of
// an instruction (e.g., a call).
struct IRFunc : IRParentInst
{
IRFuncType* getType() { return (IRFuncType*) type.usedValue; }
IRType* getResultType() { return getType()->getResultType(); }
UInt getParamCount() { return getType()->getParamCount(); }
IRType* getParamType(UInt index) { return getType()->getParamType(index); }
IRBlock* getFirstBlock() { return (IRBlock*) firstChild; }
IRBlock* getLastBlock() { return (IRBlock*) lastChild; }
IRParam* getFirstParam();
};
// A module is a parent to functions, global variables, types, etc.
struct IRModule : IRParentInst
{
// The designated entry-point function, if any
IRFunc* entryPoint;
// A special counter used to assign logical ids to instructions in this module.
IRInstID idCounter;
};
// Description of an instruction to be used for global value numbering
struct IRInstKey
{
IRInst* inst;
int GetHashCode();
};
bool operator==(IRInstKey const& left, IRInstKey const& right);
struct IRConstantKey
{
IRConstant* inst;
int GetHashCode();
};
bool operator==(IRConstantKey const& left, IRConstantKey const& right);
struct SharedIRBuilder
{
// The module that will own all of the IR
IRModule* module;
Dictionary<IRInstKey, IRInst*> globalValueNumberingMap;
Dictionary<IRConstantKey, IRConstant*> constantMap;
};
struct IRBuilder
{
// Shared state for all IR builders working on the same module
SharedIRBuilder* shared;
IRModule* getModule() { return shared->module; }
// The parent instruction to add children to.
IRParentInst* parentInst;
void addInst(IRParentInst* parent, IRInst* inst);
void addInst(IRInst* inst);
IRType* getBaseType(BaseType flavor);
IRType* getBoolType();
IRType* getVectorType(IRType* elementType, IRValue* elementCount);
IRType* getTypeType();
IRType* getVoidType();
IRType* getBlockType();
IRType* getIntrinsicType(
IROp op,
UInt argCount,
IRValue* const* args);
IRStructDecl* createStructType();
IRStructField* createStructField(IRType* fieldType);
IRType* getFuncType(
UInt paramCount,
IRType* const* paramTypes,
IRType* resultType);
IRType* getPtrType(
IRType* valueType);
IRValue* getBoolValue(bool value);
IRValue* getIntValue(IRType* type, IRIntegerValue value);
IRValue* getFloatValue(IRType* type, IRFloatingPointValue value);
IRInst* emitCallInst(
IRType* type,
IRValue* func,
UInt argCount,
IRValue* const* args);
IRInst* emitIntrinsicInst(
IRType* type,
IROp op,
UInt argCount,
IRValue* const* args);
IRInst* emitConstructorInst(
IRType* type,
UInt argCount,
IRValue* const* args);
IRModule* createModule();
IRFunc* createFunc();
IRBlock* createBlock();
IRBlock* emitBlock();
IRParam* emitParam(
IRType* type);
IRVar* emitVar(
IRType* type);
IRInst* emitLoad(
IRValue* ptr);
IRInst* emitFieldExtract(
IRType* type,
IRValue* base,
IRStructField* field);
IRInst* emitFieldAddress(
IRType* type,
IRValue* basePtr,
IRStructField* field);
IRInst* emitReturn(
IRValue* val);
IRInst* emitReturn();
IRDecoration* addDecorationImpl(
IRInst* inst,
UInt decorationSize,
IRDecorationOp op);
template<typename T>
T* addDecoration(IRInst* inst, IRDecorationOp op)
{
return (T*) addDecorationImpl(inst, sizeof(T), op);
}
template<typename T>
T* addDecoration(IRInst* inst)
{
return (T*) addDecorationImpl(inst, sizeof(T), IRDecorationOp(T::kDecorationOp));
}
IRHighLevelDeclDecoration* addHighLevelDeclDecoration(IRInst* inst, Decl* decl);
IRLayoutDecoration* addLayoutDecoration(IRInst* inst, Layout* layout);
};
void dumpIR(IRModule* module);
}
#endif
|