summaryrefslogtreecommitdiffstats
path: root/source/slang/ir.h
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-08-17 14:17:29 -0700
committerTim Foley <tfoley@nvidia.com>2017-08-17 14:17:29 -0700
commit95348fdb623509eb22c04d4c7c19af8228c5a533 (patch)
tree213fec5c06df3401fddd99d90c95f2ed7ccd2c9e /source/slang/ir.h
parentad19484792dcc5a1fb90720614830c66c4b9712d (diff)
[ir] Represent fields more direclty
Previously, a `StructType` was an ordinary instruction that took a variable number of types are operands, representing the types of fields. This ends up being inconvenient for a few reasons: - To add decorations to the fields, you'd end up having to decorate the struct type instead (SPIR-V has this problem) - You need to compute field indices during lowering, when you might prefer to defer that until later - The get/set field operations now need an index, which needs to be an explicit operand, which means a magic numeric literal floating around to represent the index The new approach fixes for the first two of these, and at least makes the last one a bit nicer. A `StructDecl` is now a parent instruction, and its sub-instructions represent the fields of the type - each field is an explicit instruction of type `StructField`. The operation to extract a field takes a direct reference the struct field, so everything is quite explicit.
Diffstat (limited to 'source/slang/ir.h')
-rw-r--r--source/slang/ir.h37
1 files changed, 23 insertions, 14 deletions
diff --git a/source/slang/ir.h b/source/slang/ir.h
index e583997fd..319340024 100644
--- a/source/slang/ir.h
+++ b/source/slang/ir.h
@@ -153,13 +153,6 @@ struct IRVectorType : IRType
IRInst* getElementCount() { return elementCount.usedValue; }
};
-struct IRStructType : IRType
-{
-
- UInt getFieldCount() { return getArgCount() - 1; }
- IRType* getFieldType(UInt index) { return (IRType*) getArg(index + 1); }
-};
-
struct IRFuncType : IRType
{
IRUse resultType;
@@ -176,12 +169,14 @@ struct IRFuncType : IRType
}
};
+struct IRStructField;
struct IRFieldExtract : IRInst
{
IRUse base;
- UInt fieldIndex;
+ 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)
@@ -212,6 +207,20 @@ struct IRParentInst : IRInst
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
@@ -308,9 +317,9 @@ struct IRBuilder
IRType* getTypeType();
IRType* getVoidType();
IRType* getBlockType();
- IRType* getStructType(
- UInt fieldCount,
- IRType* const* fieldTypes);
+
+ IRStructDecl* createStructType();
+ IRStructField* createStructField(IRType* fieldType);
IRType* getFuncType(
UInt paramCount,
@@ -343,9 +352,9 @@ struct IRBuilder
IRType* type);
IRInst* emitFieldExtract(
- IRType* type,
- IRValue* base,
- UInt fieldIndex);
+ IRType* type,
+ IRValue* base,
+ IRStructField* field);
IRInst* emitReturn(
IRValue* val);