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-legalize-types.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-legalize-types.cpp')
| -rw-r--r-- | source/slang/ir-legalize-types.cpp | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/source/slang/ir-legalize-types.cpp b/source/slang/ir-legalize-types.cpp index d4f6d2a64..9234af8f5 100644 --- a/source/slang/ir-legalize-types.cpp +++ b/source/slang/ir-legalize-types.cpp @@ -104,7 +104,7 @@ static void registerLegalizedValue( static void maybeRegisterLegalizedGlobal( IRTypeLegalizationContext* context, - IRGlobalVar* irGlobalVar, + IRGlobalValue* irGlobalVar, LegalVal const& legalVal) { // Check the mangled name of the symbol and don't register @@ -122,7 +122,7 @@ static void maybeRegisterLegalizedGlobal( struct IRGlobalNameInfo { - IRGlobalVar* globalVar; + IRGlobalValue* globalVar; UInt counter; }; @@ -1095,6 +1095,48 @@ static void legalizeGlobalVar( } } +static void legalizeGlobalConstant( + IRTypeLegalizationContext* context, + IRGlobalConstant* irGlobalConstant) +{ + // Legalize the type for the variable's value + auto legalValueType = legalizeType( + context, + irGlobalConstant->getType()); + + switch (legalValueType.flavor) + { + case LegalType::Flavor::simple: + // Easy case: the type is usable as-is, and we + // should just do that. + irGlobalConstant->type = legalValueType.getSimple(); + break; + + default: + { + context->insertBeforeGlobal = irGlobalConstant->getNextValue(); + + IRGlobalNameInfo globalNameInfo; + globalNameInfo.globalVar = irGlobalConstant; + globalNameInfo.counter = 0; + + // TODO: need to handle initializer here! + LegalVal newVal = declareVars(context, kIROp_global_constant, legalValueType, nullptr, nullptr, &globalNameInfo); + + // Register the new value as the replacement for the old + registerLegalizedValue(context, irGlobalConstant, newVal); + + // Also register the variable according to its mangled name, if any. + maybeRegisterLegalizedGlobal(context, irGlobalConstant, newVal); + + // Remove the old global from the module. + irGlobalConstant->removeFromParent(); + // TODO: actually clean up the global! + } + break; + } +} + static void legalizeGlobalValue( IRTypeLegalizationContext* context, IRGlobalValue* irValue) @@ -1113,6 +1155,10 @@ static void legalizeGlobalValue( legalizeGlobalVar(context, (IRGlobalVar*)irValue); break; + case kIROp_global_constant: + legalizeGlobalConstant(context, (IRGlobalConstant*)irValue); + break; + default: SLANG_UNEXPECTED("unknown global value type"); break; |
