diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /source/slang/slang-ir-reachability.cpp | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'source/slang/slang-ir-reachability.cpp')
| -rw-r--r-- | source/slang/slang-ir-reachability.cpp | 127 |
1 files changed, 64 insertions, 63 deletions
diff --git a/source/slang/slang-ir-reachability.cpp b/source/slang/slang-ir-reachability.cpp index 5a9d732ca..629e751c8 100644 --- a/source/slang/slang-ir-reachability.cpp +++ b/source/slang/slang-ir-reachability.cpp @@ -1,90 +1,91 @@ #include "slang-ir-reachability.h" + #include "slang-ir-insts.h" #include "slang-ir-util.h" namespace Slang { - // Computes whether block1 can reach block2. - // A block is considered not reachable from itself unless there is a backedge in the CFG. - ReachabilityContext::ReachabilityContext(IRGlobalValueWithCode* code) +// Computes whether block1 can reach block2. +// A block is considered not reachable from itself unless there is a backedge in the CFG. +ReachabilityContext::ReachabilityContext(IRGlobalValueWithCode* code) +{ + int id = 0; + for (auto block : code->getBlocks()) { - int id = 0; - for (auto block : code->getBlocks()) - { - mapBlockToId[block] = id++; - allBlocks.add(block); - } + mapBlockToId[block] = id++; + allBlocks.add(block); + } - sourceBlocks.setCount(allBlocks.getCount()); - for (auto &srcBlock : sourceBlocks) - srcBlock.resizeAndClear(allBlocks.getCount()); + sourceBlocks.setCount(allBlocks.getCount()); + for (auto& srcBlock : sourceBlocks) + srcBlock.resizeAndClear(allBlocks.getCount()); - if (allBlocks.getCount() == 0) - return; + if (allBlocks.getCount() == 0) + return; - List<IRBlock*> workList; - List<IRBlock*> pendingWorkList; + List<IRBlock*> workList; + List<IRBlock*> pendingWorkList; - workList.add(allBlocks[0]); - while (workList.getCount()) + workList.add(allBlocks[0]); + while (workList.getCount()) + { + pendingWorkList.clear(); + for (Index i = 0; i < workList.getCount(); i++) { - pendingWorkList.clear(); - for (Index i = 0; i < workList.getCount(); i++) + auto src = workList[i]; + auto srcId = mapBlockToId.getValue(src); + for (auto successor : src->getSuccessors()) { - auto src = workList[i]; - auto srcId = mapBlockToId.getValue(src); - for (auto successor : src->getSuccessors()) + auto successorId = mapBlockToId.getValue(successor); + auto& blockSet = sourceBlocks[successorId]; + bool changed = false; + if (!blockSet.contains(srcId)) + { + blockSet.add(srcId); + changed = true; + } + if (!blockSet.contains(sourceBlocks[srcId])) { - auto successorId = mapBlockToId.getValue(successor); - auto& blockSet = sourceBlocks[successorId]; - bool changed = false; - if (!blockSet.contains(srcId)) - { - blockSet.add(srcId); - changed = true; - } - if (!blockSet.contains(sourceBlocks[srcId])) - { - blockSet.unionWith(sourceBlocks[srcId]); - changed = true; - } - if (changed) - pendingWorkList.add(successor); + blockSet.unionWith(sourceBlocks[srcId]); + changed = true; } + if (changed) + pendingWorkList.add(successor); } - workList.swapWith(pendingWorkList); } + workList.swapWith(pendingWorkList); } - - bool ReachabilityContext::isInstReachable(IRInst* from, IRInst* to) +} + +bool ReachabilityContext::isInstReachable(IRInst* from, IRInst* to) +{ + // If inst1 and inst2 are in the same block, + // we test if inst2 appears after inst1. + if (getBlock(from) == getBlock(to)) { - // If inst1 and inst2 are in the same block, - // we test if inst2 appears after inst1. - if (getBlock(from) == getBlock(to)) + for (auto inst = from->getNextInst(); inst; inst = inst->getNextInst()) { - for (auto inst = from->getNextInst(); inst; inst = inst->getNextInst()) - { - if (inst == to) - return true; - } + if (inst == to) + return true; } - - return isBlockReachable(getBlock(from), getBlock(to)); } - bool ReachabilityContext::isBlockReachable(IRBlock* from, IRBlock* to) - { - if (!from) - return false; + return isBlockReachable(getBlock(from), getBlock(to)); +} + +bool ReachabilityContext::isBlockReachable(IRBlock* from, IRBlock* to) +{ + if (!from) + return false; - if (!to) - return false; + if (!to) + return false; - int* fromId = mapBlockToId.tryGetValue(from); - int* toId = mapBlockToId.tryGetValue(to); - if (!fromId || !toId) - return true; + int* fromId = mapBlockToId.tryGetValue(from); + int* toId = mapBlockToId.tryGetValue(to); + if (!fromId || !toId) + return true; - return sourceBlocks[*toId].contains(*fromId); - } + return sourceBlocks[*toId].contains(*fromId); } +} // namespace Slang |
