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

[MLIR][Affine] affine-loop-fusion miscompiles a producer loop with an external func.call

Open YGTao opened this issue 4 months ago • 4 comments

affine-loop-fusion incorrectly fuses a consumer into a producer loop that passes the produced memref to an external func.call.

The silent miscompilation is the primary issue. The same reduced input also crashes in assertions-enabled/current public trunk builds.

Public reproducer: https://www.compiler-explorer.com/z/a8ba738Pb

Tested locally at llvm/llvm-project commit 450cf0aafeab9fed6666b112b018d3913a36527c (LLVM 24.0.0git).

Reproducer

module {
  func.func private @opaque(memref<32xf64>)
  func.func @control(%in: memref<32xf64>, %comm: memref<32xf64>, %out: memref<32xf64>) {
    affine.for %i = 0 to 16 {
      %a = affine.load %in[%i] : memref<32xf64>
      %b = arith.addf %a, %a : f64
      affine.store %b, %comm[%i] : memref<32xf64>
      func.call @opaque(%comm) : (memref<32xf64>) -> ()
    }
    affine.for %j = 0 to 16 {
      %c = affine.load %comm[%j] : memref<32xf64>
      %d = arith.addf %c, %c : f64
      affine.store %d, %out[%j] : memref<32xf64>
    }
    return
  }
}

Command

mlir-opt dc6.mlir \
  --pass-pipeline='builtin.module(func.func(affine-loop-fusion{mode=producer maximal}))'

Result

With assertions disabled, the consumer is fused into the producer:

affine.for %i = 0 to 16 {
  %a = affine.load %in[%i] : memref<32xf64>
  %b = arith.addf %a, %a : f64
  affine.store %b, %comm[%i] : memref<32xf64>
  func.call @opaque(%comm) : (memref<32xf64>) -> ()
  %c = affine.load %comm[%i] : memref<32xf64>
  %d = arith.addf %c, %c : f64
  affine.store %d, %out[%i] : memref<32xf64>
}

In the source, all calls complete before the consumer reads %comm. After fusion, each consumer iteration runs before the remaining calls.

Using a legal synchronous callee that updates %comm[0] on every call, the source produces out[0] = 8222, while the transformed program produces out[0] = 8192.

Both IR modules verify, and the concrete execution uses initialized buffers, in-bounds accesses, and a synchronous non-retaining callee.

With assertions enabled on the local commit, the same command exits with status 134:

unexpected op
UNREACHABLE executed at mlir/lib/Dialect/Affine/Analysis/Utils.cpp:256!

At filing time, Compiler Explorer's MLIR opt (trunk) terminates on the same input with SIGSEGV (exit code 139).

Related: #205745 reports a crash in the same pass area. PR #205865 prevents the local assertion failure for this reproducer, but the assertions-disabled build still emits the incorrect fusion.

YGTao avatar Jul 23 '26 15:07 YGTao

@llvm/issue-subscribers-mlir

Author: Yonggang Tao (YGTao)

`affine-loop-fusion` incorrectly fuses a consumer into a producer loop that passes the produced memref to an external `func.call`.

The silent miscompilation is the primary issue. The same reduced input also crashes in assertions-enabled/current public trunk builds.

Public reproducer: https://www.compiler-explorer.com/z/a8ba738Pb

Tested locally at llvm/llvm-project commit 450cf0aafeab9fed6666b112b018d3913a36527c (LLVM 24.0.0git).

Reproducer

module {
  func.func private @<!-- -->opaque(memref&lt;32xf64&gt;)
  func.func @<!-- -->control(%in: memref&lt;32xf64&gt;, %comm: memref&lt;32xf64&gt;, %out: memref&lt;32xf64&gt;) {
    affine.for %i = 0 to 16 {
      %a = affine.load %in[%i] : memref&lt;32xf64&gt;
      %b = arith.addf %a, %a : f64
      affine.store %b, %comm[%i] : memref&lt;32xf64&gt;
      func.call @<!-- -->opaque(%comm) : (memref&lt;32xf64&gt;) -&gt; ()
    }
    affine.for %j = 0 to 16 {
      %c = affine.load %comm[%j] : memref&lt;32xf64&gt;
      %d = arith.addf %c, %c : f64
      affine.store %d, %out[%j] : memref&lt;32xf64&gt;
    }
    return
  }
}

Command

mlir-opt dc6.mlir \
  --pass-pipeline='builtin.module(func.func(affine-loop-fusion{mode=producer maximal}))'

Result

With assertions disabled, the consumer is fused into the producer:

affine.for %i = 0 to 16 {
  %a = affine.load %in[%i] : memref&lt;32xf64&gt;
  %b = arith.addf %a, %a : f64
  affine.store %b, %comm[%i] : memref&lt;32xf64&gt;
  func.call @<!-- -->opaque(%comm) : (memref&lt;32xf64&gt;) -&gt; ()
  %c = affine.load %comm[%i] : memref&lt;32xf64&gt;
  %d = arith.addf %c, %c : f64
  affine.store %d, %out[%i] : memref&lt;32xf64&gt;
}

In the source, all calls complete before the consumer reads %comm. After fusion, each consumer iteration runs before the remaining calls.

Using a legal synchronous callee that updates %comm[0] on every call, the source produces out[0] = 8222, while the transformed program produces out[0] = 8192.

Both IR modules verify, and the concrete execution uses initialized buffers, in-bounds accesses, and a synchronous non-retaining callee.

With assertions enabled on the local commit, the same command exits with status 134:

unexpected op
UNREACHABLE executed at mlir/lib/Dialect/Affine/Analysis/Utils.cpp:256!

At filing time, Compiler Explorer's MLIR opt (trunk) terminates on the same input with SIGSEGV (exit code 139).

Related: #205745 reports a crash in the same pass area. PR #205865 prevents the local assertion failure for this reproducer, but the assertions-disabled build still emits the incorrect fusion.

End-to-end reproduction

The same byte-identical INPUT_PROGRAM.mlir is used in both runs. The optimized module is generated from that input by mlir-opt; it is not a separately prepared target.

Tested with llvm/llvm-project commit 450cf0aafeab9fed6666b112b018d3913a36527c (LLVM 24.0.0git) using an assertions-disabled Release build.

INPUT_PROGRAM.mlir:

module {
  func.func private @opaque(memref<32xf64>)

  func.func @control(%in: memref<32xf64>, %comm: memref<32xf64>, %out: memref<32xf64>) {
    affine.for %i = 0 to 16 {
      %a = affine.load %in[%i] : memref<32xf64>
      %b = arith.addf %a, %a : f64
      affine.store %b, %comm[%i] : memref<32xf64>
      func.call @opaque(%comm) : (memref<32xf64>) -> ()
    }
    affine.for %j = 0 to 16 {
      %c = affine.load %comm[%j] : memref<32xf64>
      %d = arith.addf %c, %c : f64
      affine.store %d, %out[%j] : memref<32xf64>
    }
    return
  }

  func.func @main() -> i64 {
    %in = memref.alloc() : memref<32xf64>
    %comm = memref.alloc() : memref<32xf64>
    %out = memref.alloc() : memref<32xf64>
    affine.for %k = 0 to 32 {
      %k64 = arith.index_cast %k : index to i64
      %kf = arith.sitofp %k64 : i64 to f64
      %two = arith.constant 2.0 : f64
      %five = arith.constant 5.0 : f64
      %hundred = arith.constant 100.0 : f64
      %twok = arith.mulf %two, %kf : f64
      %in_value = arith.addf %twok, %five : f64
      %comm_value = arith.addf %kf, %hundred : f64
      %out_value = arith.negf %comm_value : f64
      affine.store %in_value, %in[%k] : memref<32xf64>
      affine.store %comm_value, %comm[%k] : memref<32xf64>
      affine.store %out_value, %out[%k] : memref<32xf64>
    }
    func.call @control(%in, %comm, %out) : (memref<32xf64>, memref<32xf64>, memref<32xf64>) -> ()
    %value = affine.load %out[0] : memref<32xf64>
    %result = arith.fptosi %value : f64 to i64
    return %result : i64
  }
}

CALLEE.c:

#include <stdint.h>

static int64_t call_index;

void opaque(double *allocated, double *aligned, int64_t offset, int64_t size,
            int64_t stride) {
  (void)allocated;
  (void)size;
  aligned[offset] = 4096.0 + (double)call_index;
  ++call_index;
}

Run from the directory containing those two files:

LLVM_BUILD=/path/to/llvm-build
LOWER='builtin.module(lower-affine,convert-scf-to-cf,expand-strided-metadata,finalize-memref-to-llvm,convert-func-to-llvm,convert-arith-to-llvm,convert-cf-to-llvm,reconcile-unrealized-casts)'

cc -std=c11 -O2 -fPIC -shared CALLEE.c -o libcallee.so

"$LLVM_BUILD/bin/mlir-opt" INPUT_PROGRAM.mlir -o baseline.mlir
"$LLVM_BUILD/bin/mlir-opt" INPUT_PROGRAM.mlir \
  --pass-pipeline='builtin.module(func.func(affine-loop-fusion{mode=producer maximal}))' \
  -o optimized.mlir

"$LLVM_BUILD/bin/mlir-opt" baseline.mlir \
  -pass-pipeline="$LOWER" -o baseline.lowered.mlir
"$LLVM_BUILD/bin/mlir-opt" optimized.mlir \
  -pass-pipeline="$LOWER" -o optimized.lowered.mlir

SHARED_LIBS="$PWD/libcallee.so,$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"

"$LLVM_BUILD/bin/mlir-runner" baseline.lowered.mlir -e main \
  --entry-point-result=i64 --shared-libs="$SHARED_LIBS"
"$LLVM_BUILD/bin/mlir-runner" optimized.lowered.mlir -e main \
  --entry-point-result=i64 --shared-libs="$SHARED_LIBS"

Output:

without affine-loop-fusion: 8222
with affine-loop-fusion:    8192

The optimized @control generated in this run was textually identical to the previously reported pass output. Both modules verified, lowered, and executed successfully in two confirming runs.

YGTao avatar Jul 24 '26 11:07 YGTao

I'd like to look into this.

heturing avatar Aug 06 '26 08:08 heturing

Follow-up with a correctness fix for the illegal producer-consumer fusion described here:

https://github.com/llvm/llvm-project/pull/225250

Summary of the approach:

  1. Harden non-affine dependence checking with mayAccessMemRef (same direction as #205865) so assertion builds do not crash on ops like func.call.
  2. Add an early legality predicate hasOpaqueEffectOnPCMemrefs that blocks PC fusion when a shared memref has opaque/non-affine producer effects or non-dereferencing users (e.g. func.call) in the fusion block.

Measured locally on tip with assertions enabled:

  • before: crash at Utils.cpp (unexpected op)
  • after representation harden only: no crash, but still illegally fused
  • after legality gate: two nests preserved for the repro; ordinary no-call fusion still happens

Happy to iterate on review feedback.

agourakis82 avatar Sep 22 '26 01:09 agourakis82