From c5c8cfbb360d9a763f549df48636effde839eacd Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:50:13 -0400 Subject: Handle the case where the parent if-else region's after-block is unreachable. (#3241) Also added a test for this. Co-authored-by: Yong He --- source/slang/slang-ir-autodiff-cfg-norm.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'source') diff --git a/source/slang/slang-ir-autodiff-cfg-norm.cpp b/source/slang/slang-ir-autodiff-cfg-norm.cpp index e7c269756..3602e77ae 100644 --- a/source/slang/slang-ir-autodiff-cfg-norm.cpp +++ b/source/slang/slang-ir-autodiff-cfg-norm.cpp @@ -356,6 +356,30 @@ struct CFGNormalizationPass // afterBaseRegion = true; + // One case we do check for is if the after block is 'unreachable' + // i.e. the terminator is an `unreachable` instruction. + // In this case, we can safely assume that the after block does not + // have anything to execute. Further, we need to re-wire the + // previously unreachable block to the parent break block. + // Note that this operation is safe because if the after block was + // originally unreachable, all potential paths to it must have + // broken out of the region. + // + if (auto unreachInst = as(afterBlock->getTerminator())) + { + // Link it to the parentAfterBlock. + builder.setInsertInto(afterBlock); + unreachInst->removeAndDeallocate(); + + builder.emitBranch(parentAfterBlock); + + // We can now safely assume that the after block is empty. + // Set 'afterBaseRegion' to false, which should lead the rest + // of the logic to avoid splitting the after block + // + afterBaseRegion = false; + } + // Do we need to split the after region? if (afterBaseRegion && afterBreakRegion) { -- cgit v1.2.3