summaryrefslogtreecommitdiffstats
path: root/source/slang
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-10-11 23:14:41 -0700
committerGitHub <noreply@github.com>2022-10-11 23:14:41 -0700
commit5128de89a9a8da09587f20e8fb5bc324ea14e0df (patch)
tree292d6488ee966eedcff9736f22511038f9bdc84b /source/slang
parent8c43a19d2fd8f9d1bd9d5854a4b1e64d5231bc41 (diff)
Small IR cleanups. (#2441)
Diffstat (limited to 'source/slang')
-rw-r--r--source/slang/slang-emit.cpp3
-rw-r--r--source/slang/slang-ir-eliminate-phis.cpp5
-rw-r--r--source/slang/slang-ir-simplify-cfg.cpp18
3 files changed, 25 insertions, 1 deletions
diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp
index a916d0d63..6d85e1ce0 100644
--- a/source/slang/slang-emit.cpp
+++ b/source/slang/slang-emit.cpp
@@ -830,6 +830,9 @@ Result linkAndOptimizeIR(
}
}
+ // Run a final round of DCE to clean up unused things after phi-elimination.
+ eliminateDeadCode(irModule);
+
// We include one final step to (optionally) dump the IR and validate
// it after all of the optimization passes are complete. This should
// reflect the IR that code is generated from as closely as possible.
diff --git a/source/slang/slang-ir-eliminate-phis.cpp b/source/slang/slang-ir-eliminate-phis.cpp
index c4f0b8b9d..07d0e7374 100644
--- a/source/slang/slang-ir-eliminate-phis.cpp
+++ b/source/slang/slang-ir-eliminate-phis.cpp
@@ -807,7 +807,10 @@ struct PhiEliminationContext
// so that any logic that might have moved another parameter
// into a temporary will influence our result.
//
- m_builder.emitStore(dstParam.temp, *srcArg.currentValPtr);
+ if ((*srcArg.currentValPtr)->getOp() != kIROp_undefined)
+ {
+ m_builder.emitStore(dstParam.temp, *srcArg.currentValPtr);
+ }
//
// Once the store is emitted, the assignment has been performed,
diff --git a/source/slang/slang-ir-simplify-cfg.cpp b/source/slang/slang-ir-simplify-cfg.cpp
index db0274d32..a1bc38b64 100644
--- a/source/slang/slang-ir-simplify-cfg.cpp
+++ b/source/slang/slang-ir-simplify-cfg.cpp
@@ -12,6 +12,9 @@ bool processFunc(IRGlobalValueWithCode* func)
if (!firstBlock)
return false;
+ SharedIRBuilder sharedBuilder(func->getModule());
+ IRBuilder builder(&sharedBuilder);
+
bool changed = false;
List<IRBlock*> workList;
@@ -25,12 +28,27 @@ bool processFunc(IRGlobalValueWithCode* func)
{
if (auto loop = as<IRLoop>(block->getTerminator()))
{
+ // If continue block is unreachable, remove it.
auto continueBlock = loop->getContinueBlock();
if (continueBlock && !continueBlock->hasMoreThanOneUse())
{
loop->continueBlock.set(loop->getTargetBlock());
continueBlock->removeAndDeallocate();
}
+ // If there isn't any actual back jumps into loop target, remove the header
+ // and turn it into a normal branch.
+ auto targetBlock = loop->getTargetBlock();
+ if (targetBlock->getPredecessors().getCount() == 1 && *targetBlock->getPredecessors().begin() == block)
+ {
+ builder.setInsertBefore(loop);
+ List<IRInst*> args;
+ for (UInt i = 0; i < loop->getArgCount(); i++)
+ {
+ args.add(loop->getArg(i));
+ }
+ builder.emitBranch(targetBlock, args.getCount(), args.getBuffer());
+ loop->removeAndDeallocate();
+ }
}
// If `block` does not end with an unconditional branch, bail.
if (block->getTerminator()->getOp() != kIROp_unconditionalBranch)