@@ -300,21 +300,28 @@ bool MergeCubeBlockPass::canMergeBlocks(
300300 }
301301
302302 // Step 2: Check if they have same source and sink with no other nodes
303- if (! checkSameSourceAndSink (blockId1, blockId2, graph)) {
303+ if (checkSameSourceAndSink (blockId1, blockId2, graph)) {
304304 LDBG (" Blocks " << blockId1 << " and " << blockId2
305- << " cannot merge: different source or sink\n " );
306- return false ;
305+ << " can merge: same source or sink\n " );
306+ return true ;
307307 }
308308
309309 // Step 3: Check if merging would create a cycle
310- if (checkNoCycle (blockId1, blockId2, graph, memGraph, bm)) {
310+ if (! checkNoCycle (blockId1, blockId2, graph, memGraph, bm)) {
311311 LDBG (" Blocks " << blockId1 << " and " << blockId2
312- << " can merge: no cycle detected \n " );
313- return true ;
312+ << " cannot merge: would create cycle \n " );
313+ return false ;
314314 }
315315
316+ // Step 4: cube block in the same depth
317+ if (hasSameDepth (blockId1, blockId2, graph)) {
318+ LDBG (" Blocks " << blockId1 << " and " << blockId2
319+ << " can merge: cube blocks have same depth\n " );
320+ return true ;
321+ }
322+
316323 LDBG (" Blocks " << blockId1 << " and " << blockId2
317- << " cannot merge: would create cycle \n " );
324+ << " cannot merge: unsupport scenario \n " );
318325 return false ;
319326}
320327
@@ -365,6 +372,20 @@ bool MergeCubeBlockPass::checkSameSourceAndSink(int blockId1, int blockId2,
365372 return true ;
366373}
367374
375+ bool MergeCubeBlockPass::hasSameDepth (int blockId1, int blockId2,
376+ BlockDependencyGraph &graph) {
377+
378+ // Get block nodes
379+ BlockNode *node1 = graph.getBlockNode (blockId1);
380+ BlockNode *node2 = graph.getBlockNode (blockId2);
381+
382+ if (!node1 || !node2) {
383+ return false ;
384+ }
385+
386+ return node1->depth == node2->depth ;
387+ }
388+
368389llvm::SmallVector<int > MergeCubeBlockPass::filterBlocksByType (
369390 int blockId, llvm::SmallVector<int > blocks, BlockDependencyGraph &graph) {
370391
0 commit comments