llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR] `remove-dead-values` produce invalid IR for dead block argument with mixed-liveness predecessors

Open MonSec-2077 opened this issue 3 months ago • 4 comments

remove-dead-values can produce invalid IR when a dead successor block argument has multiple predecessors whose incoming values have different liveness.

Input Program

input.mlir

module {
  func.func @multi_pred_mixed(%arg0: i1, %arg1: i32) -> i32 {
    %c0_i32 = arith.constant 0 : i32
    cf.cond_br %arg0, ^bb1, ^bb2
  ^bb1:  // pred: ^bb0
    cf.br ^bb3(%c0_i32 : i32)
  ^bb2:  // pred: ^bb0
    cf.br ^bb3(%arg1 : i32)
  ^bb3(%0: i32):  // 2 preds: ^bb1, ^bb2
    return %arg1 : i32
  }
}

Current Result

input.mlir:10:5: error: branch has 1 operands for successor #0, but target block has 0
    cf.br ^join(%x : i32)
    ^
input.mlir:10:5: note: see current operation: "cf.br"(%arg1)[^bb3] : (i32) -> ()

%arg is unused, so the pass removes the block argument. But it only removes the corresponding operand from one predecessor, leaving the other branch still passing an operand to a block with no arguments.

Expected behavior: produce valid IR

Version: ef88536bbf62469eb2396d719d237b3191cf4843

MonSec-2077 avatar Aug 18 '26 12:08 MonSec-2077

@llvm/issue-subscribers-mlir

Author: MonSec-2077

`remove-dead-values` can produce invalid IR when a dead successor block argument has multiple predecessors whose incoming values have different liveness.

Input Program

input.mlir

module {
  func.func @<!-- -->multi_pred_mixed(%arg0: i1, %arg1: i32) -&gt; i32 {
    %c0_i32 = arith.constant 0 : i32
    cf.cond_br %arg0, ^bb1, ^bb2
  ^bb1:  // pred: ^bb0
    cf.br ^bb3(%c0_i32 : i32)
  ^bb2:  // pred: ^bb0
    cf.br ^bb3(%arg1 : i32)
  ^bb3(%0: i32):  // 2 preds: ^bb1, ^bb2
    return %arg1 : i32
  }
}

Current Result

input.mlir:10:5: error: branch has 1 operands for successor #<!-- -->0, but target block has 0
    cf.br ^join(%x : i32)
    ^
input.mlir:10:5: note: see current operation: "cf.br"(%arg1)[^bb3] : (i32) -&gt; ()

%arg is unused, so the pass removes the block argument. But it only removes the corresponding operand from one predecessor, leaving the other branch still passing an operand to a block with no arguments.

Expected behavior: produce valid IR

Version: ef88536bbf62469eb2396d719d237b3191cf4843

hi, can I take a look at this if it's fine?

Naaz30 avatar Aug 18 '26 14:08 Naaz30

hi, can I take a look at this if it's fine?

Just prepare pull request and mention it on this page.

EugeneZelenko avatar Aug 18 '26 14:08 EugeneZelenko

You may, but look at open PRs touching remove-dead-value first.

joker-eph avatar Aug 18 '26 14:08 joker-eph