diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-01-16 08:16:53 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-16 08:16:53 -0800 |
| commit | f0633a7d0b1615f6b115d53763bedeeb857b7f3c (patch) | |
| tree | f3871140fdafe54dfcc412df6a828840c890f17f /source | |
| parent | 29ffac963e9c4b02d3cbc9d4b281512ed820e67d (diff) | |
Add proper IR codegen support for local static const variables (#779)
Previously the IR codegen logic was treating function-scope `static const` variables just like `static` variables, which results in them generating less efficient output HLSL/GLSL.
This change special-cases function-local `static const` variables with logic that mirrors how we handle global-scope `static const` variables.
The approach in this change attempts to find a simpler solution to deal with `static const` variables inside of generic functions than what is currently done for `static` variables in generic functions, but I haven't tested whether that works in practice, so I didn't apply the same approach to the plain `static` case. That would make a good follow-on change.
I've included a single test case to demonstrate that with this fix the Slang compiler generates output DXBC that uses an indexable "immediate" constant buffer, whereas without the fix it generates an array in local memory (slow).
Diffstat (limited to 'source')
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 2242619d0..388ca884e 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -4107,10 +4107,56 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> IRGenContext* getContet() { return &subContextStorage; } }; + LoweredValInfo lowerFunctionStaticConstVarDecl( + VarDeclBase* decl) + { + // We need to insert the constant at a level above + // the function being emitted. This will usually + // be the global scope, but it might be an outer + // generic if we are lowering a generic function. + // + NestedContext nestedContext(this); + auto subBuilder = nestedContext.getBuilder(); + auto subContext = nestedContext.getContet(); + + subBuilder->setInsertInto(subBuilder->getFunc()->getParent()); + + IRType* subVarType = lowerType(subContext, decl->getType()); + + IRGlobalConstant* irConstant = subBuilder->createGlobalConstant(subVarType); + addVarDecorations(subContext, irConstant, decl); + addNameHint(context, irConstant, decl); + maybeSetRate(context, irConstant, decl); + subBuilder->addHighLevelDeclDecoration(irConstant, decl); + + LoweredValInfo constantVal = LoweredValInfo::ptr(irConstant); + setValue(context, decl, constantVal); + + if( auto initExpr = decl->initExpr ) + { + NestedContext nestedInitContext(this); + auto initBuilder = nestedInitContext.getBuilder(); + auto initContext = nestedInitContext.getContet(); + + initBuilder->setInsertInto(irConstant); + + IRBlock* entryBlock = initBuilder->emitBlock(); + initBuilder->setInsertInto(entryBlock); + + LoweredValInfo initVal = lowerRValueExpr(initContext, initExpr); + initBuilder->emitReturn(getSimpleVal(initContext, initVal)); + } + + return constantVal; + } LoweredValInfo lowerFunctionStaticVarDecl( VarDeclBase* decl) { + // We know the variable is `static`, but it might also be `const. + if(decl->HasModifier<ConstModifier>()) + return lowerFunctionStaticConstVarDecl(decl); + // A global variable may need to be generic, if one // of the outer declarations is generic. NestedContext nestedContext(this); |
