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

[MLIR][Linalg] `extract_slice_sinking` introduces `poison` for `reductions over partial slices` and cause wrong results

Open quiet-maple-47 opened this issue 1 month ago • 2 comments

Target repository: llvm/llvm-project
Kind: silent wrong code (wrong value)
Component: MLIR (upstream passes, run with mlir-opt)

Version

mlir-opt built from llvm-project b3df0665 (2026-06-08) with assertions enabled; mlir-opt --version prints LLVM (http://llvm.org/): LLVM version 23.0.0git Optimized build with assertions.. Host: Linux x86_64. Also reproduced with Homebrew LLVM 23.1.4 on macOS arm64 and with mlir-opt trunk on Compiler Explorer: https://godbolt.org/z/1aTc7c47c

Issue

The linalg.extract_slice_sinking pass will introduce ub.poison results when do reduction over tensor partial slices.

Reproducer

run.mlir

func.func @prod(%a: tensor<4xi32>, %init: tensor<i32>) -> tensor<i32> {
  %s = tensor.extract_slice %a[1] [2] [1] // <~ slice
      : tensor<4xi32> to tensor<2xi32>

  %r = linalg.generic { // <~ multiply with reduction 
      indexing_maps = [
        affine_map<(i) -> (i)>,
        affine_map<(i) -> ()>
      ],
      iterator_types = ["reduction"]
    }
    ins(%s : tensor<2xi32>)
    outs(%init : tensor<i32>) {
  ^bb0(%x: i32, %acc: i32):
    %y = arith.muli %x, %acc : i32
    linalg.yield %y : i32
  } -> tensor<i32>

  return %r : tensor<i32>
} // The function computes the product of a[1] and a[2], initialized to 1.

func.func @main() {
  %a = arith.constant dense<[5, 3, 4, 7]> : tensor<4xi32> // <~ For a = [5, 3, 4, 7] , the expected result is 3 * 4 = 12.
  %init = arith.constant dense<1> : tensor<i32>

  %r = func.call @prod(%a, %init)
      : (tensor<4xi32>, tensor<i32>) -> tensor<i32>

  %v = tensor.extract %r[] : tensor<i32>
  vector.print %v : i32
  return
}

sink.mlir

module attributes {transform.with_named_sequence} {
  transform.named_sequence @__transform_main(
      %root: !transform.any_op {transform.readonly}) {
    %f = transform.structured.match ops{["func.func"]} in %root
        : (!transform.any_op) -> !transform.any_op

    transform.apply_patterns to %f {
      transform.apply_patterns.linalg.extract_slice_sinking // Apply the extract_slice_sinking rewrite patterns
    } : !transform.any_op

    transform.yield
  }
}

Run the transformation:

mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir

We found:

run.mlir (you can found in the godbolt link):

%0 = ub.poison : i32  // <~ ub.poison

%extracted_slice = tensor.extract_slice %arg0[1] [2] [1]
    : tensor<4xi32> to tensor<2xi32>

%padded = tensor.pad %extracted_slice low[1] high[1] {
^bb0(%arg2: index):
  tensor.yield %0 : i32
} : tensor<2xi32> to tensor<4xi32> // <~ padding with ub.poison

Result

Expected:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Reduction: 1 * 3 * 4 = 12

Actual:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Poison padding: [poison, 3, 4, poison] Reduction: 1 * poison * 3 * 4 * poison = poison

On my mac:

Compilation and execution commands (macOS)
lower() {
  mlir-opt --one-shot-bufferize=bufferize-function-boundaries \
    --convert-linalg-to-loops --convert-scf-to-cf \
    --expand-strided-metadata --lower-affine \
    --finalize-memref-to-llvm --convert-vector-to-llvm \
    --convert-arith-to-llvm --convert-func-to-llvm \
    --convert-cf-to-llvm --convert-ub-to-llvm \
    --reconcile-unrealized-casts "$@"
}

run() {
  mlir-runner -e main -entry-point-result=void \
    -shared-libs="$(brew --prefix llvm)/lib/libmlir_c_runner_utils.dylib"
}

# Without transformation
lower run.mlir | run

# With extract_slice_sinking
mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir | lower | run
Configuration Runtime output
Without extract_slice_sinking 12 (expected)
With extract_slice_sinking 0 (incorrect)

The same behavior was observed with both mlir-runner -O0 and -O3.

Claim

This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

Assisted-by: Claude Opus 5.5

quiet-maple-47 avatar Oct 28 '26 20:10 quiet-maple-47

@llvm/issue-subscribers-mlir

Author: quiet-maple-47

Target repository: llvm/llvm-project
Kind: silent wrong code (wrong value)
Component: MLIR (upstream passes, run with mlir-opt)

Version

mlir-opt built from llvm-project b3df0665 (2026-06-08) with assertions enabled; mlir-opt --version prints LLVM (http://llvm.org/): LLVM version 23.0.0git Optimized build with assertions.. Host: Linux x86_64. Also reproduced with Homebrew LLVM 23.1.4 on macOS arm64 and with mlir-opt trunk on Compiler Explorer: https://godbolt.org/z/1aTc7c47c

Issue

The linalg.extract_slice_sinking pass will introduce ub.poison results when do reduction over tensor partial slices.

Reproducer

run.mlir

func.func @<!-- -->prod(%a: tensor&lt;4xi32&gt;, %init: tensor&lt;i32&gt;) -&gt; tensor&lt;i32&gt; {
  %s = tensor.extract_slice %a[1] [2] [1] // &lt;~ slice
      : tensor&lt;4xi32&gt; to tensor&lt;2xi32&gt;

  %r = linalg.generic { // &lt;~ multiply with reduction 
      indexing_maps = [
        affine_map&lt;(i) -&gt; (i)&gt;,
        affine_map&lt;(i) -&gt; ()&gt;
      ],
      iterator_types = ["reduction"]
    }
    ins(%s : tensor&lt;2xi32&gt;)
    outs(%init : tensor&lt;i32&gt;) {
  ^bb0(%x: i32, %acc: i32):
    %y = arith.muli %x, %acc : i32
    linalg.yield %y : i32
  } -&gt; tensor&lt;i32&gt;

  return %r : tensor&lt;i32&gt;
} // The function computes the product of a[1] and a[2], initialized to 1.

func.func @<!-- -->main() {
  %a = arith.constant dense&lt;[5, 3, 4, 7]&gt; : tensor&lt;4xi32&gt; // &lt;~ For a = [5, 3, 4, 7] , the expected result is 3 * 4 = 12.
  %init = arith.constant dense&lt;1&gt; : tensor&lt;i32&gt;

  %r = func.call @<!-- -->prod(%a, %init)
      : (tensor&lt;4xi32&gt;, tensor&lt;i32&gt;) -&gt; tensor&lt;i32&gt;

  %v = tensor.extract %r[] : tensor&lt;i32&gt;
  vector.print %v : i32
  return
}

sink.mlir

module attributes {transform.with_named_sequence} {
  transform.named_sequence @<!-- -->__transform_main(
      %root: !transform.any_op {transform.readonly}) {
    %f = transform.structured.match ops{["func.func"]} in %root
        : (!transform.any_op) -&gt; !transform.any_op

    transform.apply_patterns to %f {
      transform.apply_patterns.linalg.extract_slice_sinking // Apply the extract_slice_sinking rewrite patterns
    } : !transform.any_op

    transform.yield
  }
}

Run the transformation:

mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir

We found:

run.mlir (you can found in the godbolt link):

%0 = ub.poison : i32  // &lt;~ ub.poison

%extracted_slice = tensor.extract_slice %arg0[1] [2] [1]
    : tensor&lt;4xi32&gt; to tensor&lt;2xi32&gt;

%padded = tensor.pad %extracted_slice low[1] high[1] {
^bb0(%arg2: index):
  tensor.yield %0 : i32
} : tensor&lt;2xi32&gt; to tensor&lt;4xi32&gt; // &lt;~ padding with ub.poison

Result

Expected:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Reduction: 1 * 3 * 4 = 12

Actual:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Poison padding: [poison, 3, 4, poison] Reduction: 1 * poison * 3 * 4 * poison = poison

On my mac:

<details> <summary>Compilation and execution commands (macOS)</summary>

lower() {
  mlir-opt --one-shot-bufferize=bufferize-function-boundaries \
    --convert-linalg-to-loops --convert-scf-to-cf \
    --expand-strided-metadata --lower-affine \
    --finalize-memref-to-llvm --convert-vector-to-llvm \
    --convert-arith-to-llvm --convert-func-to-llvm \
    --convert-cf-to-llvm --convert-ub-to-llvm \
    --reconcile-unrealized-casts "$@"
}

run() {
  mlir-runner -e main -entry-point-result=void \
    -shared-libs="$(brew --prefix llvm)/lib/libmlir_c_runner_utils.dylib"
}

# Without transformation
lower run.mlir | run

# With extract_slice_sinking
mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir | lower | run

</details>

Configuration Runtime output
Without extract_slice_sinking 12 (expected)
With extract_slice_sinking 0 (incorrect)

<p><span>The same behavior was observed with both </span><code dir="ltr"><span>mlir-runner -O0</span></code><span> and </span><code dir="ltr"><span>-O3</span></code><span>.</span></p>

@llvm/issue-subscribers-mlir-linalg

Author: quiet-maple-47

Target repository: llvm/llvm-project
Kind: silent wrong code (wrong value)
Component: MLIR (upstream passes, run with mlir-opt)

Version

mlir-opt built from llvm-project b3df0665 (2026-06-08) with assertions enabled; mlir-opt --version prints LLVM (http://llvm.org/): LLVM version 23.0.0git Optimized build with assertions.. Host: Linux x86_64. Also reproduced with Homebrew LLVM 23.1.4 on macOS arm64 and with mlir-opt trunk on Compiler Explorer: https://godbolt.org/z/1aTc7c47c

Issue

The linalg.extract_slice_sinking pass will introduce ub.poison results when do reduction over tensor partial slices.

Reproducer

run.mlir

func.func @<!-- -->prod(%a: tensor&lt;4xi32&gt;, %init: tensor&lt;i32&gt;) -&gt; tensor&lt;i32&gt; {
  %s = tensor.extract_slice %a[1] [2] [1] // &lt;~ slice
      : tensor&lt;4xi32&gt; to tensor&lt;2xi32&gt;

  %r = linalg.generic { // &lt;~ multiply with reduction 
      indexing_maps = [
        affine_map&lt;(i) -&gt; (i)&gt;,
        affine_map&lt;(i) -&gt; ()&gt;
      ],
      iterator_types = ["reduction"]
    }
    ins(%s : tensor&lt;2xi32&gt;)
    outs(%init : tensor&lt;i32&gt;) {
  ^bb0(%x: i32, %acc: i32):
    %y = arith.muli %x, %acc : i32
    linalg.yield %y : i32
  } -&gt; tensor&lt;i32&gt;

  return %r : tensor&lt;i32&gt;
} // The function computes the product of a[1] and a[2], initialized to 1.

func.func @<!-- -->main() {
  %a = arith.constant dense&lt;[5, 3, 4, 7]&gt; : tensor&lt;4xi32&gt; // &lt;~ For a = [5, 3, 4, 7] , the expected result is 3 * 4 = 12.
  %init = arith.constant dense&lt;1&gt; : tensor&lt;i32&gt;

  %r = func.call @<!-- -->prod(%a, %init)
      : (tensor&lt;4xi32&gt;, tensor&lt;i32&gt;) -&gt; tensor&lt;i32&gt;

  %v = tensor.extract %r[] : tensor&lt;i32&gt;
  vector.print %v : i32
  return
}

sink.mlir

module attributes {transform.with_named_sequence} {
  transform.named_sequence @<!-- -->__transform_main(
      %root: !transform.any_op {transform.readonly}) {
    %f = transform.structured.match ops{["func.func"]} in %root
        : (!transform.any_op) -&gt; !transform.any_op

    transform.apply_patterns to %f {
      transform.apply_patterns.linalg.extract_slice_sinking // Apply the extract_slice_sinking rewrite patterns
    } : !transform.any_op

    transform.yield
  }
}

Run the transformation:

mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir

We found:

run.mlir (you can found in the godbolt link):

%0 = ub.poison : i32  // &lt;~ ub.poison

%extracted_slice = tensor.extract_slice %arg0[1] [2] [1]
    : tensor&lt;4xi32&gt; to tensor&lt;2xi32&gt;

%padded = tensor.pad %extracted_slice low[1] high[1] {
^bb0(%arg2: index):
  tensor.yield %0 : i32
} : tensor&lt;2xi32&gt; to tensor&lt;4xi32&gt; // &lt;~ padding with ub.poison

Result

Expected:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Reduction: 1 * 3 * 4 = 12

Actual:

Original tensor: [5, 3, 4, 7] Extract slice: [3, 4] Poison padding: [poison, 3, 4, poison] Reduction: 1 * poison * 3 * 4 * poison = poison

On my mac:

<details> <summary>Compilation and execution commands (macOS)</summary>

lower() {
  mlir-opt --one-shot-bufferize=bufferize-function-boundaries \
    --convert-linalg-to-loops --convert-scf-to-cf \
    --expand-strided-metadata --lower-affine \
    --finalize-memref-to-llvm --convert-vector-to-llvm \
    --convert-arith-to-llvm --convert-func-to-llvm \
    --convert-cf-to-llvm --convert-ub-to-llvm \
    --reconcile-unrealized-casts "$@"
}

run() {
  mlir-runner -e main -entry-point-result=void \
    -shared-libs="$(brew --prefix llvm)/lib/libmlir_c_runner_utils.dylib"
}

# Without transformation
lower run.mlir | run

# With extract_slice_sinking
mlir-opt \
  --transform-preload-library=transform-library-paths=sink.mlir \
  --transform-interpreter run.mlir | lower | run

</details>

Configuration Runtime output
Without extract_slice_sinking 12 (expected)
With extract_slice_sinking 0 (incorrect)

<p><span>The same behavior was observed with both </span><code dir="ltr"><span>mlir-runner -O0</span></code><span> and </span><code dir="ltr"><span>-O3</span></code><span>.</span></p>

Claim

This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

Assisted-by: Claude Opus 5.5