From 37315c96ea48045fae60f0e1cb1a3293f3ddd962 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 20 Nov 2017 13:45:10 -0800 Subject: IR: support global variable with initializers (#294) The big change here is that the ability to contain basic blocks with instructions in them has been hoisted from `IRFunc` into a new base type `IRGlobalValueWithCode` shared with `IRGlobalVar`. The basic blocks of a global variable define initialization logic for it; they can be looked at like a function that returns the initial value. Places in the IR that used to assume functions contain all the code need to be updated, but so far I only handled the cloning step. The emit logic currently handles an initializer for a global variable by outputting its logic as a separate function, and then having the variable call that function to initialize itself. This should be cleaned up over time so that we generate an ordinary expression whenever possible. I also made the emit logic correctly label any global variable without a layout (that is, any that don't represent a shader parameter) as `static` so that the downstream HLSL compiler sees them as variables rather than parameters. --- source/slang/emit.cpp | 52 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) (limited to 'source/slang/emit.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 383442236..4cad7b28d 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -6461,7 +6461,23 @@ emitDeclImpl(decl, nullptr); { auto allocatedType = varDecl->getType(); auto varType = allocatedType->getValueType(); -// auto addressSpace = allocatedType->getAddressSpace(); + + String initFuncName; + if (varDecl->firstBlock) + { + // A global variable with code means it has an initializer + // associated with it. Eventually we'd like to emit that + // initializer directly as an expression here, but for + // now we'll emit it as a separate function. + + initFuncName = getIRName(varDecl); + initFuncName.append("_init"); + emitIRType(ctx, varType, initFuncName); + Emit("()\n{\n"); + emitIRStmtsForBlocks(ctx, varDecl->firstBlock, nullptr); + Emit("}\n"); + } + if (auto paramBlockType = varType->As()) { @@ -6475,20 +6491,27 @@ emitDeclImpl(decl, nullptr); // Need to emit appropriate modifiers here. auto layout = getVarLayout(ctx, varDecl); - - emitIRVarModifiers(ctx, layout); -#if 0 - switch (addressSpace) + if (!layout) { - default: - break; + // A global variable without a layout is just an + // ordinary global variable, and may need special + // modifiers to indicate it as such. + switch (getTarget(ctx)) + { + case CodeGenTarget::HLSL: + // HLSL requires the `static` modifier on any + // global variables; otherwise they are assumed + // to be uniforms. + Emit("static "); + break; - case kIRAddressSpace_GroupShared: - emit("groupshared "); - break; + default: + break; + } } -#endif + + emitIRVarModifiers(ctx, layout); emitIRType(ctx, varType, getIRName(varDecl)); @@ -6496,6 +6519,13 @@ emitDeclImpl(decl, nullptr); emitIRLayoutSemantics(ctx, varDecl); + if (varDecl->firstBlock) + { + Emit(" = "); + emit(initFuncName); + Emit("()"); + } + emit(";\n"); } -- cgit v1.2.3