From 3979660d4fe1fd6c1f1d9b8956e96817e17c3f4e Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 13 Dec 2023 15:15:19 -0800 Subject: Fix GLSL static initialization bug. (#3409) * Fix GLSL static initialization bug. Fixes #3408. * Update comment. * Fold global var initializer as an expression if possible. --------- Co-authored-by: Yong He --- source/slang/slang-ir-explicit-global-init.cpp | 27 +++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'source/slang/slang-ir-explicit-global-init.cpp') diff --git a/source/slang/slang-ir-explicit-global-init.cpp b/source/slang/slang-ir-explicit-global-init.cpp index a65118d5a..94733bf6d 100644 --- a/source/slang/slang-ir-explicit-global-init.cpp +++ b/source/slang/slang-ir-explicit-global-init.cpp @@ -187,11 +187,28 @@ struct MoveGlobalVarInitializationToEntryPointsPass // auto valType = globalVar->getDataType()->getValueType(); - // We compute the initial value for the variable by calling - // the initial-value function with no arguments, and then - // we store that value into the corresponding global. - // - auto initVal = builder.emitCallInst(valType, initFunc, 0, nullptr); + // To simplify the resulting code a bit, if we see the + // initFunc just returns a constant value, then we just + // use that inline. + IRInst* initVal = nullptr; + if (auto initFirstBlock = initFunc->getFirstBlock()) + { + if (auto returnInst = as(initFirstBlock->getTerminator())) + { + if (returnInst->getVal() && returnInst->getVal()->getParent() == m_module->getModuleInst()) + { + initVal = returnInst->getVal(); + } + } + } + if (!initVal) + { + // We compute the initial value for the variable by calling + // the initial-value function with no arguments, and then + // we store that value into the corresponding global. + // + initVal = builder.emitCallInst(valType, initFunc, 0, nullptr); + } builder.emitStore(globalVar, initVal); } } -- cgit v1.2.3