From 465cacf770611b10b662fba230fff3a5666af53d Mon Sep 17 00:00:00 2001 From: fishofnanqi <1074959344@qq.com> Date: Tue, 1 Sep 2026 03:44:12 +0000 Subject: [PATCH 1/2] [ssbuffer](feat) extend scenario of merge cube block Signed-off-by: fishofnanqi <1074959344@qq.com> --- .../ComputeBlockOpt/MergeCubeBlockPass.h | 1 + .../ComputeBlockOpt/MergeCubeBlock.cpp | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/third_party/ascend/include/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlockPass.h b/third_party/ascend/include/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlockPass.h index 3094a3632e..b3b689cf48 100644 --- a/third_party/ascend/include/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlockPass.h +++ b/third_party/ascend/include/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlockPass.h @@ -99,6 +99,7 @@ class MergeCubeBlockPass BlockDependencyGraph &graph); bool checkSameSourceAndSink(int blockId1, int blockId2, BlockDependencyGraph &graph); + bool hasSameDepth(int blockId1, int blockId2, BlockDependencyGraph &graph); bool checkNoCycle(int blockId1, int blockId2, BlockDependencyGraph &graph, const MemoryDependenceGraph &memGraph, ComputeBlockIdManager &bm); diff --git a/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp b/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp index fb909953e7..af868cd51a 100644 --- a/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp +++ b/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp @@ -303,21 +303,28 @@ bool MergeCubeBlockPass::canMergeBlocks( } // Step 2: Check if they have same source and sink with no other nodes - if (!checkSameSourceAndSink(blockId1, blockId2, graph)) { + if (checkSameSourceAndSink(blockId1, blockId2, graph)) { LDBG("Blocks " << blockId1 << " and " << blockId2 - << " cannot merge: different source or sink\n"); - return false; + << " can merge: same source or sink\n"); + return true; } // Step 3: Check if merging would create a cycle - if (checkNoCycle(blockId1, blockId2, graph, memGraph, bm)) { + if (!checkNoCycle(blockId1, blockId2, graph, memGraph, bm)) { LDBG("Blocks " << blockId1 << " and " << blockId2 - << " can merge: no cycle detected\n"); - return true; + << "cannot merge: would create cycle\n"); + return false; } + // Step 4: cube block in the same depth + if (hasSameDepth(blockId1, blockId2, graph)) { + LDBG("Blocks " << blockId1 << " and " << blockId2 + << " can merge: cube blocks have same depth\n"); + return true; + } + LDBG("Blocks " << blockId1 << " and " << blockId2 - << " cannot merge: would create cycle\n"); + << " cannot merge: unsupport scenario\n"); return false; } @@ -368,6 +375,20 @@ bool MergeCubeBlockPass::checkSameSourceAndSink(int blockId1, int blockId2, return true; } +bool MergeCubeBlockPass::hasSameDepth(int blockId1, int blockId2, + BlockDependencyGraph &graph) { + + // Get block nodes + BlockNode *node1 = graph.getBlockNode(blockId1); + BlockNode *node2 = graph.getBlockNode(blockId2); + + if (!node1 || !node2) { + return false; + } + + return node1->depth == node2->depth; +} + llvm::SmallVector MergeCubeBlockPass::filterBlocksByType( int blockId, llvm::SmallVector blocks, BlockDependencyGraph &graph) { From 108f4b83fc84a45cd1ba47cc584945ee629044c6 Mon Sep 17 00:00:00 2001 From: fishofnanqi <1074959344@qq.com> Date: Wed, 2 Sep 2026 16:11:10 +0800 Subject: [PATCH 2/2] successors need to have same maxdepth --- .../ComputeBlockOpt/MergeCubeBlock.cpp | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp b/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp index af868cd51a..8cc23dd70c 100644 --- a/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp +++ b/third_party/ascend/lib/DynamicCVPipeline/ComputeBlockOpt/MergeCubeBlock.cpp @@ -386,7 +386,31 @@ bool MergeCubeBlockPass::hasSameDepth(int blockId1, int blockId2, return false; } - return node1->depth == node2->depth; + if (node1->depth != node2->depth) { + return false; + } + + // Check that the max depth among successors of both blocks is the same + auto getMaxSuccDepth = [&](int blockId) { + int maxDepth = -1; + for (int succId : graph.getSuccessors(blockId)) { + BlockNode *succNode = graph.getBlockNode(succId); + if (succNode && succNode->depth > maxDepth) { + maxDepth = succNode->depth; + } + } + return maxDepth; + }; + + int maxSuccDepth1 = getMaxSuccDepth(blockId1); + int maxSuccDepth2 = getMaxSuccDepth(blockId2); + + // If either block has no successors, still mergeable + if (maxSuccDepth1 == -1 || maxSuccDepth2 == -1) { + return true; + } + + return maxSuccDepth1 == maxSuccDepth2; } llvm::SmallVector MergeCubeBlockPass::filterBlocksByType(