summaryrefslogtreecommitdiffstats
path: root/source/slang/ir.h
blob: ecc77dbc4138fdbc46cc963c36b1d22c9b53b688 (plain)
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
// 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 {

class   Decl;
class   FuncType;
class   Layout;
class   Type;
class   Session;

struct  IRFunc;
struct  IRInst;
struct  IRModule;
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
{
    // The value that is being used
    IRValue* usedValue;

    // The value that is doing the using.
    IRInst* user;

    // 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, IRValue* usedValue);
};

enum IRDecorationOp : uint16_t
{
    kIRDecorationOp_HighLevelDecl,
    kIRDecorationOp_Layout,
    kIRDecorationOp_LoopControl,
};

// 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; }

    // 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();
};

// Instructions are values that can be executed,
// and which take other values as operands
struct IRInst : IRValue
{
    // 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;

    // The basic block that contains this instruction.
    IRBlock*    parentBlock;

    // The next and previous instructions in the same parent block
    IRInst*     nextInst;
    IRInst*     prevInst;

    UInt getArgCount()
    {
        return argCount;
    }

    IRUse*      getArgs();

    IRValue* getArg(UInt index)
    {
        return getArgs()[index].usedValue;
    }

    // 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();
};

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
    IRFunc* parentFunc;

    IRFunc* getParent() { return parentFunc; }

};

// 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();
};

// 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 : IRGlobalValue
{
    // The type of the IR-level function
    IRFuncType* getType() { return (IRFuncType*) type.Ptr(); }

    // Any generic parameters this function has
    List<RefPtr<Decl>> genericParams;

    // Convenience accessors for working with the 
    // function's type.
    Type* getResultType();
    UInt getParamCount();
    Type* getParamType(UInt index);

    // 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);

    // 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);
}


#endif