summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2020-07-23 13:47:12 -0700
committerGitHub <noreply@github.com>2020-07-23 13:47:12 -0700
commitfed4292a581364b611a82a0f6c1c1c95f82dfeb2 (patch)
treef4e50a0d448f2710313a05b5def07a1c0a3b67b3 /source
parente93d3a443934b50fb983f77306a72e9c695bd5b9 (diff)
Run SSA pass to clean up temporary variables during generics lowering. (#1447)
* Run SSA pass to clean up generic temporary variables during lowering. * Fix `undefined` emitting logic. * revert dumpir control flag * Defer fold decision of `undefined` values after special case logic for GLSL and HLSL. * Update expected test result. * Manually update raygen.slang.glsl to minimize change. * fix formatting Co-authored-by: Tim Foley <tfoleyNV@users.noreply.github.com>
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-emit-c-like.cpp9
-rw-r--r--source/slang/slang-ir-lower-generic-var.cpp8
-rw-r--r--source/slang/slang-ir-lower-generics.cpp9
-rw-r--r--source/slang/slang-ir.cpp1
4 files changed, 26 insertions, 1 deletions
diff --git a/source/slang/slang-emit-c-like.cpp b/source/slang/slang-emit-c-like.cpp
index 733811183..a3b11a908 100644
--- a/source/slang/slang-emit-c-like.cpp
+++ b/source/slang/slang-emit-c-like.cpp
@@ -1074,6 +1074,15 @@ bool CLikeSourceEmitter::shouldFoldInstIntoUseSites(IRInst* inst)
if(inst->findDecoration<IRPreciseDecoration>())
return false;
+ // In general, undefined value should be emitted as an uninitialized
+ // variable, so we shouldn't fold it.
+ // However, we cannot emit all undefined values a separate variable
+ // definition for certain types on certain targets (e.g. `out TriangleStream<T>`
+ // for GLSL), so we check this only after all those special cases are
+ // considered.
+ if (inst->op == kIROp_undefined)
+ return false;
+
// Okay, at this point we know our instruction must have a single use.
auto use = inst->firstUse;
SLANG_ASSERT(use);
diff --git a/source/slang/slang-ir-lower-generic-var.cpp b/source/slang/slang-ir-lower-generic-var.cpp
index 8799dea6b..936ed017f 100644
--- a/source/slang/slang-ir-lower-generic-var.cpp
+++ b/source/slang/slang-ir-lower-generic-var.cpp
@@ -79,6 +79,12 @@ namespace Slang
// have already been replaced with the pointer to that variable by now.
// So we don't need to do anything here.
}
+ else if (valPtr->op == kIROp_undefined)
+ {
+ // We don't need to store an undef value.
+ storeInst->removeAndDeallocate();
+ return;
+ }
else
{
// If value does not come from another generic variable, then it must be
@@ -138,7 +144,7 @@ namespace Slang
void processInst(IRInst* inst)
{
- if (inst->op == kIROp_Var || inst->op == kIROp_undefined)
+ if (inst->op == kIROp_Var)
{
processVarInst(inst);
}
diff --git a/source/slang/slang-ir-lower-generics.cpp b/source/slang/slang-ir-lower-generics.cpp
index 61fa8ad17..c60b5386a 100644
--- a/source/slang/slang-ir-lower-generics.cpp
+++ b/source/slang/slang-ir-lower-generics.cpp
@@ -6,6 +6,8 @@
#include "slang-ir-lower-generic-call.h"
#include "slang-ir-lower-generic-var.h"
#include "slang-ir-witness-table-wrapper.h"
+#include "slang-ir-ssa.h"
+#include "slang-ir-dce.h"
namespace Slang
{
@@ -16,7 +18,14 @@ namespace Slang
sharedContext.module = module;
lowerGenericFunctions(&sharedContext);
lowerGenericCalls(&sharedContext);
+ // We might have generated new temporary variables during lowering.
+ // An SSA pass can clean up unncessary load/stores.
+ constructSSA(module);
+ eliminateDeadCode(module);
lowerGenericVar(&sharedContext);
+ // After lowerGenericVar, there could be some unused `undef` values.
+ // We eliminate them in a DCE pass.
+ eliminateDeadCode(module);
generateWitnessTableWrapperFunctions(&sharedContext);
}
} // namespace Slang
diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp
index fc4b71138..f551dcbba 100644
--- a/source/slang/slang-ir.cpp
+++ b/source/slang/slang-ir.cpp
@@ -5151,6 +5151,7 @@ namespace Slang
return false;
case kIROp_Nop:
+ case kIROp_undefined:
case kIROp_Specialize:
case kIROp_lookup_interface_method:
case kIROp_getAddr: