diff options
| author | Tim Foley <tfoley@nvidia.com> | 2017-08-17 14:17:29 -0700 |
|---|---|---|
| committer | Tim Foley <tfoley@nvidia.com> | 2017-08-17 14:17:29 -0700 |
| commit | 95348fdb623509eb22c04d4c7c19af8228c5a533 (patch) | |
| tree | 213fec5c06df3401fddd99d90c95f2ed7ccd2c9e /source/slang/lower-to-ir.cpp | |
| parent | ad19484792dcc5a1fb90720614830c66c4b9712d (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/lower-to-ir.cpp')
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index ed8ed3318..5d0a40072 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -394,7 +394,7 @@ struct ExprLoweringVisitor : ExprVisitor<ExprLoweringVisitor, LoweredValInfo> LoweredValInfo extractField( LoweredTypeInfo fieldType, LoweredValInfo base, - UInt fieldIndex) + LoweredValInfo field) { switch (base.flavor) { @@ -405,7 +405,7 @@ struct ExprLoweringVisitor : ExprVisitor<ExprLoweringVisitor, LoweredValInfo> getBuilder()->emitFieldExtract( getSimpleType(fieldType), irBase, - fieldIndex)); + (IRStructField*) getSimpleVal(field))); } break; @@ -424,22 +424,8 @@ struct ExprLoweringVisitor : ExprVisitor<ExprLoweringVisitor, LoweredValInfo> { // Okay, easy enough: we have a reference to a field of a struct type... - // HACK: for now just scan the decl to find the right index. - // TODO: we need to deal with the fact that the struct might get - // tuple-ified. - // - UInt index = 0; - for (auto fieldDecl : getMembersOfType<StructField>(fieldDeclRef.GetParent().As<AggTypeDecl>())) - { - if (fieldDecl == fieldDeclRef.getDecl()) - { - break; - } - - index++; - } - - return extractField(loweredType, loweredBase, index); + auto loweredField = ensureDecl(context, fieldDeclRef); + return extractField(loweredType, loweredBase, loweredField); } SLANG_UNIMPLEMENTED_X("codegen for subscript expression"); @@ -559,20 +545,27 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> // User-defined aggregate type: need to translate into // a corresponding IR aggregate type. - List<LoweredTypeInfo> fieldTypes; - List<IRType*> irFieldTypes; + auto builder = getBuilder(); + IRStructDecl* irStruct = builder->createStructType(); for (auto fieldDecl : decl->GetFields()) { + // TODO: need to track relationship to original fields... + // TODO: need to be prepared to deal with tuple-ness of fields here auto fieldType = lowerType(context, fieldDecl->getType()); - fieldTypes.Add(fieldType); - switch (fieldType.flavor) { case LoweredTypeInfo::Flavor::Simple: - irFieldTypes.Add(fieldType.type); + { + auto irField = builder->createStructField(getSimpleType(fieldType)); + builder->addInst(irStruct, irField); + + context->shared->declValues.Add( + DeclRef<StructField>(fieldDecl, nullptr), + LoweredValInfo::simple(irField)); + } break; default: @@ -580,13 +573,10 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> } } - // TODO: need to track relationship to original fields... - IRType* irStructType = getBuilder()->getStructType( - irFieldTypes.Count(), - &irFieldTypes[0]); + builder->addInst(irStruct); - return LoweredValInfo::simple(irStructType); + return LoweredValInfo::simple(irStruct); } LoweredValInfo visitFunctionDeclBase(FunctionDeclBase* decl) |
