summaryrefslogtreecommitdiffstats
path: root/source/slang/emit.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2017-11-20 13:45:10 -0800
committerGitHub <noreply@github.com>2017-11-20 13:45:10 -0800
commit37315c96ea48045fae60f0e1cb1a3293f3ddd962 (patch)
treea96f139a7a2dae57f67956711be14bf773a50ec2 /source/slang/emit.cpp
parent3dff5a53a21ab2404918e772962004767024c0f3 (diff)
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.
Diffstat (limited to 'source/slang/emit.cpp')
-rw-r--r--source/slang/emit.cpp52
1 files changed, 41 insertions, 11 deletions
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<UniformParameterGroupType>())
{
@@ -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");
}