From 95348fdb623509eb22c04d4c7c19af8228c5a533 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 17 Aug 2017 14:17:29 -0700 Subject: [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. --- source/slang/ir.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'source/slang/ir.cpp') diff --git a/source/slang/ir.cpp b/source/slang/ir.cpp index d2c3f5ba5..b6af6aabb 100644 --- a/source/slang/ir.cpp +++ b/source/slang/ir.cpp @@ -622,20 +622,23 @@ namespace Slang getTypeType()); } - IRType* IRBuilder::getStructType( - UInt fieldCount, - IRType* const* fieldTypes) + IRStructDecl* IRBuilder::createStructType() { - auto inst = createInstWithTrailingArgs( + return createInst( this, kIROp_StructType, - getTypeType(), - fieldCount, - (IRValue* const*)fieldTypes); - addInst(inst); - return inst; + getTypeType()); } + IRStructField* IRBuilder::createStructField(IRType* fieldType) + { + return createInst( + this, + kIROp_StructField, + fieldType); + } + + IRType* IRBuilder::getFuncType( UInt paramCount, IRType* const* paramTypes, @@ -753,17 +756,16 @@ namespace Slang } IRInst* IRBuilder::emitFieldExtract( - IRType* type, - IRValue* base, - UInt fieldIndex) + IRType* type, + IRValue* base, + IRStructField* field) { auto inst = createInst( this, kIROp_FieldExtract, type, - base); - - inst->fieldIndex = fieldIndex; + base, + field); addInst(inst); return inst; -- cgit v1.2.3