@@ -303,21 +303,28 @@ bool MergeCubeBlockPass::canMergeBlocks(
303303 }
304304
305305 // Step 2: Check if they have same source and sink with no other nodes
306- if (! checkSameSourceAndSink (blockId1, blockId2, graph)) {
306+ if (checkSameSourceAndSink (blockId1, blockId2, graph)) {
307307 LDBG (" Blocks " << blockId1 << " and " << blockId2
308- << " cannot merge: different source or sink\n " );
309- return false ;
308+ << " can merge: same source or sink\n " );
309+ return true ;
310310 }
311311
312312 // Step 3: Check if merging would create a cycle
313- if (checkNoCycle (blockId1, blockId2, graph, memGraph, bm)) {
313+ if (! checkNoCycle (blockId1, blockId2, graph, memGraph, bm)) {
314314 LDBG (" Blocks " << blockId1 << " and " << blockId2
315- << " can merge: no cycle detected \n " );
316- return true ;
315+ << " cannot merge: would create cycle \n " );
316+ return false ;
317317 }
318318
319+ // Step 4: cube block in the same depth
320+ if (hasSameDepth (blockId1, blockId2, graph)) {
321+ LDBG (" Blocks " << blockId1 << " and " << blockId2
322+ << " can merge: cube blocks have same depth\n " );
323+ return true ;
324+ }
325+
319326 LDBG (" Blocks " << blockId1 << " and " << blockId2
320- << " cannot merge: would create cycle \n " );
327+ << " cannot merge: unsupport scenario \n " );
321328 return false ;
322329}
323330
@@ -368,6 +375,20 @@ bool MergeCubeBlockPass::checkSameSourceAndSink(int blockId1, int blockId2,
368375 return true ;
369376}
370377
378+ bool MergeCubeBlockPass::hasSameDepth (int blockId1, int blockId2,
379+ BlockDependencyGraph &graph) {
380+
381+ // Get block nodes
382+ BlockNode *node1 = graph.getBlockNode (blockId1);
383+ BlockNode *node2 = graph.getBlockNode (blockId2);
384+
385+ if (!node1 || !node2) {
386+ return false ;
387+ }
388+
389+ return node1->depth == node2->depth ;
390+ }
391+
371392llvm::SmallVector<int > MergeCubeBlockPass::filterBlocksByType (
372393 int blockId, llvm::SmallVector<int > blocks, BlockDependencyGraph &graph) {
373394
0 commit comments