Hi all, I am trying to work on [mlir] `mergeIdenticalBlocks` is blowing up on a conditional to identical CFGs · Issue #63230 · llvm/llvm-project · GitHub.
The optimization can be stated as: “remove a block argument if all the predecessors are passing the same value for that argument”
I implemented it, but I get some issues in -buffer-deallocation-simplification
. I can give more details, but at some point the IR looks like the following:
module {
func.func @condBranchDynamicTypeNested(%arg0: i1, %arg1: memref<?xf32>, %arg2: memref<?xf32>, %arg3: index) {
%true = arith.constant true
%false = arith.constant false
%0 = arith.select %arg0, %false, %false : i1
%1 = arith.select %arg0, %false, %false : i1
cf.cond_br %arg0, ^bb1, ^bb2
^bb1: // pred: ^bb0
cf.br ^bb5(%arg1, %true : memref<?xf32>, i1)
^bb2: // pred: ^bb0
%alloc = memref.alloc(%arg3) : memref<?xf32>
// If I remove the following two lines the code works
cf.br ^bb4(%true : i1)
^bb4(%10: i1): // pred: ^bb3
cf.br ^bb5(%alloc, %true : memref<?xf32>, i1)
^bb5(%11: memref<?xf32>, %12: i1): // 2 preds: ^bb1, ^bb4
%base_buffer_8, %offset_9, %sizes_10, %strides_11 = memref.extract_strided_metadata %arg2 : memref<?xf32> -> memref<f32>, index, index, index
%base_buffer_12, %offset_13, %sizes_14, %strides_15 = memref.extract_strided_metadata %11 : memref<?xf32> -> memref<f32>, index, index, index
%13:2 = bufferization.dealloc (%base_buffer_8, %base_buffer_12 : memref<f32>, memref<f32>) if (%1, %12) retain (%11, %arg2 : memref<?xf32>, memref<?xf32>)
cf.br ^bb6(%true : i1)
^bb6(%14: i1): // pred: ^bb5
return
}
}
And when I run: mlir-opt -buffer-deallocation-simplification bug.mlir
I get:
mlir/lib/Dialect/Bufferization/Transforms/BufferViewFlowAnalysis.cpp:287: auto mlir::BufferOriginAnalysis::isSameAllocation(mlir::Value, mlir::Value)::(anonymous class)::operator()(const SmallPtrSet<mlir::Value, 16> &, SmallPtrSet<mlir::Value, 16> &, bool &, bool &) const: Assertion `!terminal.empty() && "expected non-empty terminal set"' failed.
The weird thing (for my inexperienced eyes) is that if I remove those two lines:
cf.br ^bb4(%true : i1)
^bb4(%10: i1): // pred: ^bb3
The pass does not complain anymore. Is there someone that can help me understand what is going on?
I can show a more complete IR, if what I am providing is not enough.
Thanks in advance,
Giuseppe
EDIT: I copied/pasted the wrong IR. This should be the right example (cc @matthias-springer )