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
|
// 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"
#include "source-loc.h"
namespace Slang {
class Decl;
class GenericDecl;
class FuncType;
class Layout;
class Type;
class Session;
struct IRFunc;
struct IRGlobalValueWithCode;
struct IRInst;
struct IRModule;
struct IRUser;
struct IRValue;
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,
};
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;
};
// Look up the info for an op
IROpInfo getIROpInfo(IROp op);
// A use of another value/inst within an IR operation
struct IRUse
{
IRValue* get() { return usedValue; }
IRUser* getUser() { return user; }
void init(IRUser* user, IRValue* usedValue);
void set(IRValue* usedValue);
void clear();
// The value that is being used
IRValue* usedValue = nullptr;
// The value that is doing the using.
IRUser* user = nullptr;
// The next use of the same value
IRUse* nextUse = nullptr;
// A "link" back to where this use is referenced,
// so that we can simplify updates.
IRUse** prevLink = nullptr;
void debugValidate();
};
enum IRDecorationOp : uint16_t
{
kIRDecorationOp_HighLevelDecl,
kIRDecorationOp_Layout,
kIRDecorationOp_LoopControl,
kIRDecorationOp_Target,
kIRDecorationOp_TargetIntrinsic,
};
// 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;
};
// Use AST-level types directly to represent the
// types of IR instructions/values
typedef Type IRType;
struct IRBlock;
// Base class for values in the IR
struct IRValue
{
// The operation that this value represents
IROp op;
// The type of the result value of this instruction,
// or `null` to indicate that the instruction has
// no value.
RefPtr<Type> type;
Type* getType() { return type; }
// Source location information for this value, if any
SourceLoc sourceLoc;
// The linked list of decorations attached to this value
IRDecoration* firstDecoration = nullptr;
// Look up a decoration in the list of decorations
IRDecoration* findDecorationImpl(IRDecorationOp op);
template<typename T>
T* findDecoration()
{
return (T*) findDecorationImpl(IRDecorationOp(T::kDecorationOp));
}
// The first use of this value (start of a linked list)
IRUse* firstUse = nullptr;
// Replace all uses of this value with `other`, so
// that this value will now have no uses.
void replaceUsesWith(IRValue* other);
// Free a value (which needs to have been removed
// from its parent, had its uses eliminated, etc.)
void deallocate();
};
// Values that are contained in a doubly-linked
// list inside of some parent.
//
// TODO: consider merging this into `IRValue` so
// that *all* values have a parent.
struct IRChildValue : IRValue
{
// The parent of this value.
IRValue* parent;
// The next and previous values in the same
// list on teh same parent.
IRChildValue* next;
IRChildValue* prev;
};
// Helper for storing linked lists of child values.
struct IRValueListBase
{
IRChildValue* first = 0;
IRChildValue* last = 0;
protected:
void addImpl(IRValue* parent, IRChildValue* val);
};
template<typename T>
struct IRValueList : IRValueListBase
{
T* getFirst() { return (T*)first; }
T* getLast() { return (T*)last; }
void add(IRValue* parent, T* val)
{
addImpl(parent, val);
}
struct Iterator
{
T* val;
Iterator() : val(0) {}
Iterator(T* val) : val(val) {}
void operator++()
{
if (val)
{
val = (T*)val->next;
}
}
T* operator*()
{
return val;
}
bool operator!=(Iterator const& i)
{
return val != i.val;
}
};
Iterator begin() { return Iterator(getFirst()); }
Iterator end() { return Iterator(nullptr); }
};
// Values that can use other values. These always
// have their operands "tail allocated" after
// the fields of this type, so derived types must
// either:
//
// - Add no new fields, or
// - Add only fields that represent the `IRUse` operands
// - Add a fixed number of `IRUse` operand fields and
// then any additional data after them.
//
struct IRUser : IRChildValue
{
// The total number of arguments of this instruction.
//
// TODO: We shouldn't need to allocate this on
// all instructions. Instead we should have
// instructions that need "vararg" support to
// allocate this field ahead of the `this`
// pointer.
uint32_t argCount;
UInt getArgCount()
{
return argCount;
}
IRUse* getArgs();
IRValue* getArg(UInt index)
{
return getArgs()[index].get();
}
void setArg(UInt index, IRValue* value)
{
getArgs()[index].set(value);
}
};
// Instructions are values that are children of a basic block,
// and can actually be executed.
struct IRInst : IRUser
{
IRBlock* getParentBlock() { return (IRBlock*)parent; }
IRInst* getPrevInst() { return (IRInst*)prev; }
IRInst* getNextInst() { return (IRInst*)next; }
// Insert this instruction into the same basic block
// as `other`, right before it.
void insertBefore(IRInst* other);
// Remove this instruction from its parent block,
// but don't delete it, or replace uses.
void removeFromParent();
// Remove this instruction from its parent block,
// and then destroy it (it had better have no uses!)
void removeAndDeallocate();
// Clear out the arguments of this instruction,
// so that we don't appear on the list of uses
// for those values.
void removeArguments();
};
typedef int64_t IRIntegerValue;
typedef double IRFloatingPointValue;
struct IRConstant : IRValue
{
union
{
IRIntegerValue intVal;
IRFloatingPointValue floatVal;
// HACK: allows us to hash the value easily
void* ptrData[2];
} u;
};
// A instruction that ends a basic block (usually because of control flow)
struct IRTerminatorInst : IRInst
{};
bool isTerminatorInst(IROp op);
bool isTerminatorInst(IRInst* inst);
// A function parameter is owned by a basic block, and represents
// either an incoming function parameter (in the entry block), or
// a value that flows from one SSA block to another (in a non-entry
// block).
//
// In each case, the basic idea is that a block is a "label with
// arguments."
struct IRParam : IRValue
{
IRParam* nextParam;
IRParam* prevParam;
IRParam* getNextParam() { return nextParam; }
IRParam* getPrevParam() { return prevParam; }
};
// 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 : IRValue
{
// Linked list of the instructions contained in this block
//
// Note that in a valid program, every block must end with
// a "terminator" instruction, so these should be non-NULL,
// and `lastInst` should actually be an `IRTerminatorInst`.
IRInst* firstInst;
IRInst* lastInst;
IRInst* getFirstInst() { return firstInst; }
IRInst* getLastInst() { return lastInst; }
// Links for the list of basic blocks in the parent function
IRBlock* prevBlock;
IRBlock* nextBlock;
IRBlock* getPrevBlock() { return prevBlock; }
IRBlock* getNextBlock() { return nextBlock; }
// Linked list of parameters of this block
IRParam* firstParam;
IRParam* lastParam;
IRParam* getFirstParam() { return firstParam; }
IRParam* getLastParam() { return lastParam; }
void addParam(IRParam* param);
// The parent function that contains this block
IRGlobalValueWithCode* parentFunc;
IRGlobalValueWithCode* getParent() { return parentFunc; }
void insertAfter(IRBlock* other);
void insertAfter(IRBlock* other, IRGlobalValueWithCode* func);
struct PredecessorList
{
PredecessorList(IRUse* begin) : b(begin) {}
IRUse* b;
UInt getCount();
struct Iterator
{
Iterator(IRUse* use) : use(use) {}
IRBlock* operator*();
void operator++();
bool operator!=(Iterator const& that)
{
return use != that.use;
}
IRUse* use;
};
Iterator begin() { return Iterator(b); }
Iterator end() { return Iterator(nullptr); }
};
struct SuccessorList
{
SuccessorList(IRUse* begin, IRUse* end, UInt stride = 1) : begin_(begin), end_(end), stride(stride) {}
IRUse* begin_;
IRUse* end_;
UInt stride;
UInt getCount();
struct Iterator
{
Iterator(IRUse* use, UInt stride) : use(use), stride(stride) {}
IRBlock* operator*();
void operator++();
bool operator!=(Iterator const& that)
{
return use != that.use;
}
IRUse* use;
UInt stride;
};
Iterator begin() { return Iterator(begin_, stride); }
Iterator end() { return Iterator(end_, stride); }
};
PredecessorList getPredecessors();
SuccessorList getSuccessors();
};
// For right now, we will represent the type of
// an IR function using the type of the AST
// function from which it was created.
//
// TODO: need to do this better.
typedef FuncType IRFuncType;
struct IRGlobalValue : IRValue
{
IRModule* parentModule;
// The mangled name, for a symbol that should have linkage,
// or which might have multiple declarations.
String mangledName;
IRGlobalValue* nextGlobalValue;
IRGlobalValue* prevGlobalValue;
IRGlobalValue* getNextValue() { return nextGlobalValue; }
IRGlobalValue* getPrevValue() { return prevGlobalValue; }
void insertBefore(IRGlobalValue* other);
void insertBefore(IRGlobalValue* other, IRModule* module);
void insertAtStart(IRModule* module);
void insertAfter(IRGlobalValue* other);
void insertAfter(IRGlobalValue* other, IRModule* module);
void insertAtEnd(IRModule* module);
void removeFromParent();
void moveToEnd();
};
/// @brief A global value that potentially holds executable code.
///
struct IRGlobalValueWithCode : IRGlobalValue
{
// The list of basic blocks in this function
IRBlock* firstBlock = nullptr;
IRBlock* lastBlock = nullptr;
IRBlock* getFirstBlock() { return firstBlock; }
IRBlock* getLastBlock() { return lastBlock; }
// Add a block to the end of this function.
void addBlock(IRBlock* block);
};
// 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 : IRGlobalValueWithCode
{
// The type of the IR-level function
IRFuncType* getType() { return (IRFuncType*) type.Ptr(); }
// If this function is generic, then we store a reference
// to the AST-level generic that defines its parameters
// and their constraints.
List<RefPtr<GenericDecl>> genericDecls;
int specializedGenericLevel = -1;
GenericDecl* getGenericDecl()
{
if (specializedGenericLevel != -1)
return genericDecls[specializedGenericLevel].Ptr();
return nullptr;
}
// Convenience accessors for working with the
// function's type.
Type* getResultType();
UInt getParamCount();
Type* getParamType(UInt index);
// Convenience accessor for the IR parameters,
// which are actually the parameters of the first
// block.
IRParam* getFirstParam();
};
// A module is a parent to functions, global variables, types, etc.
struct IRModule : RefObject
{
// The compilation session in use.
Session* session;
// A list of all the functions and other
// global values declared in this module.
IRGlobalValue* firstGlobalValue = nullptr;
IRGlobalValue* lastGlobalValue = nullptr;
IRGlobalValue* getFirstGlobalValue() { return firstGlobalValue; }
IRGlobalValue* getlastGlobalValue() { return lastGlobalValue; }
};
void printSlangIRAssembly(StringBuilder& builder, IRModule* module);
String getSlangIRAssembly(IRModule* module);
void dumpIR(IRModule* module);
void dumpIR(IRGlobalValue* globalVal);
String dumpIRFunc(IRFunc* func);
}
#endif
|