diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2018-02-08 14:46:12 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-08 14:46:12 -0800 |
| commit | c7c97ad4bb62b83efd6e26cdd4f38ebf164ec40e (patch) | |
| tree | db419221788d50dd1e8940b547713e5dcfceb48d /source/slang/ir.cpp | |
| parent | 112caca00ba9bfd9e1051bb94969efa9e74c6c03 (diff) | |
Basic IR support for `static const` globals (#404)
* Basic IR support for `static const` globals
Our strategy for lowering global *variables* can fall back to putting their initialization into a function, but that isn't really appropriate for global constants (it also isn't appropriate for arrays, but we'll need to deal with that seaprately).
This change adds a distinct case for global constants (rather than treating them as variables), and forces the emission logic to always emit them as a single expression.
Doing this makes assumptions about how the IR for these constants gets emitted (and what optimziations might do to it).
In order to make things work, I had to switch the handling of initializer-list expressions to not be lowered via temporaries and mutation (since that isn't a good fit for reverting to a single expression).
I've added a single test case to ensure that this works in the simplest scenario. My next priority will be to see if this unblocks my work in Falcor.
* Fixup: bug fixes
Diffstat (limited to 'source/slang/ir.cpp')
| -rw-r--r-- | source/slang/ir.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/source/slang/ir.cpp b/source/slang/ir.cpp index 824af64ae..40c7e20d5 100644 --- a/source/slang/ir.cpp +++ b/source/slang/ir.cpp @@ -1004,6 +1004,30 @@ namespace Slang return inst; } + IRInst* IRBuilder::emitMakeVector( + IRType* type, + UInt argCount, + IRValue* const* args) + { + return emitIntrinsicInst(type, kIROp_makeVector, argCount, args); + } + + IRInst* IRBuilder::emitMakeArray( + IRType* type, + UInt argCount, + IRValue* const* args) + { + return emitIntrinsicInst(type, kIROp_makeArray, argCount, args); + } + + IRInst* IRBuilder::emitMakeStruct( + IRType* type, + UInt argCount, + IRValue* const* args) + { + return emitIntrinsicInst(type, kIROp_makeStruct, argCount, args); + } + IRModule* IRBuilder::createModule() { auto module = new IRModule(); @@ -1176,6 +1200,18 @@ namespace Slang return globalVar; } + IRGlobalConstant* IRBuilder::createGlobalConstant( + IRType* valueType) + { + IRGlobalConstant* globalConstant = createValue<IRGlobalConstant>( + this, + kIROp_global_constant, + valueType); + maybeSetSourceLoc(this, globalConstant); + addGlobalValue(getModule(), globalConstant); + return globalConstant; + } + IRWitnessTable* IRBuilder::createWitnessTable() { IRWitnessTable* witnessTable = createValue<IRWitnessTable>( @@ -1747,6 +1783,7 @@ namespace Slang { case kIROp_Func: case kIROp_global_var: + case kIROp_global_constant: case kIROp_witness_table: { auto irFunc = (IRFunc*) inst; @@ -2391,6 +2428,22 @@ namespace Slang dump(context, ";\n"); } + void dumpIRGlobalConstant( + IRDumpContext* context, + IRGlobalConstant* val) + { + dump(context, "\n"); + dumpIndent(context); + dump(context, "ir_global_constant "); + dumpID(context, val); + dumpInstTypeClause(context, val->getType()); + + // TODO: deal with the case where a global + // might have embedded initialization logic. + + dump(context, ";\n"); + } + void dumpIRWitnessTableEntry( IRDumpContext* context, IRWitnessTableEntry* entry) @@ -2436,6 +2489,10 @@ namespace Slang dumpIRGlobalVar(context, (IRGlobalVar*)value); break; + case kIROp_global_constant: + dumpIRGlobalConstant(context, (IRGlobalConstant*)value); + break; + case kIROp_witness_table: dumpIRWitnessTable(context, (IRWitnessTable*)value); break; @@ -3501,6 +3558,7 @@ namespace Slang switch (originalValue->op) { case kIROp_global_var: + case kIROp_global_constant: case kIROp_Func: case kIROp_witness_table: return cloneGlobalValue(this, (IRGlobalValue*) originalValue); @@ -3776,6 +3834,29 @@ namespace Slang return clonedVar; } + IRGlobalConstant* cloneGlobalConstantImpl( + IRSpecContext* context, + IRGlobalConstant* originalVal, + IROriginalValuesForClone const& originalValues) + { + auto clonedVal = context->builder->createGlobalConstant(context->maybeCloneType(originalVal->getType())); + registerClonedValue(context, clonedVal, originalValues); + + auto mangledName = originalVal->mangledName; + clonedVal->mangledName = mangledName; + + cloneDecorations(context, clonedVal, originalVal); + + // Clone any code in the body of the constant, since this + // represents the initializer. + cloneGlobalValueWithCodeCommon( + context, + clonedVal, + originalVal); + + return clonedVal; + } + IRWitnessTable* cloneWitnessTableImpl( IRSpecContextBase* context, IRWitnessTable* originalTable, @@ -4051,6 +4132,7 @@ namespace Slang return ((IRWitnessTable*)val)->entries.first != nullptr; case kIROp_global_var: + case kIROp_global_constant: case kIROp_Func: return ((IRGlobalValueWithCode*)val)->firstBlock != nullptr; @@ -4149,6 +4231,9 @@ namespace Slang case kIROp_global_var: return cloneGlobalVarImpl(context, (IRGlobalVar*)originalVal, sym); + case kIROp_global_constant: + return cloneGlobalConstantImpl(context, (IRGlobalConstant*)originalVal, sym); + case kIROp_witness_table: return cloneWitnessTableImpl(context, (IRWitnessTable*)originalVal, sym); |
