diff options
| author | Yong He <yonghe@outlook.com> | 2023-12-13 15:15:19 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-13 15:15:19 -0800 |
| commit | 3979660d4fe1fd6c1f1d9b8956e96817e17c3f4e (patch) | |
| tree | a1777fc23e2983c5dbe630d39e529c798953484c /source/slang/slang-ir-explicit-global-init.cpp | |
| parent | 1406aa2bc9e398e5e5565ba9c6adbb780c29fee1 (diff) | |
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 <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-ir-explicit-global-init.cpp')
| -rw-r--r-- | source/slang/slang-ir-explicit-global-init.cpp | 27 |
1 files changed, 22 insertions, 5 deletions
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<IRReturn>(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); } } |
