[MLIR][Affine] affine-loop-fusion miscompiles when an external func.call uses a memref.cast alias
affine-loop-fusion moves an external call that uses a cast alias from between the producer and consumer loops to before the producer, and then fuses the loops. This changes the program semantics.
Public reproducer: https://www.compiler-explorer.com/z/K3MErMWKc
Tested locally at llvm/llvm-project commit 450cf0aafeab9fed6666b112b018d3913a36527c (LLVM 24.0.0git).
Reproducer
module {
func.func private @escape(memref<?xf64>)
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>
}
%view = memref.cast %comm : memref<32xf64> to memref<?xf64>
func.call @escape(%view) : (memref<?xf64>) -> ()
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 dc8.mlir \
--pass-pipeline='builtin.module(func.func(affine-loop-fusion{mode=producer maximal}))'
Result
The pass emits:
module {
func.func private @escape(memref<?xf64>)
func.func @control(%arg0: memref<32xf64>, %arg1: memref<32xf64>, %arg2: memref<32xf64>) {
%cast = memref.cast %arg1 : memref<32xf64> to memref<?xf64>
call @escape(%cast) : (memref<?xf64>) -> ()
affine.for %arg3 = 0 to 16 {
%0 = affine.load %arg0[%arg3] : memref<32xf64>
%1 = arith.addf %0, %0 : f64
affine.store %1, %arg1[%arg3] : memref<32xf64>
%2 = affine.load %arg1[%arg3] : memref<32xf64>
%3 = arith.addf %2, %2 : f64
affine.store %3, %arg2[%arg3] : memref<32xf64>
}
return
}
}
In the source, the producer fills %comm, then @escape modifies it, and only then does the consumer read it. In the transformed program, @escape runs before the producer, whose stores overwrite the call's results before the consumer reads them.
For a legal synchronous implementation that writes comm[k] = 8192 + k for k = 0..15, the source produces out[0] = 16384, while the transformed program produces out[0] = 4.
Both the source and transformed IR verify. The concrete execution uses initialized, distinct buffers, in-bounds accesses, and a synchronous non-retaining callee.
@llvm/issue-subscribers-mlir
Author: Yonggang Tao (YGTao)
Public reproducer: https://www.compiler-explorer.com/z/K3MErMWKc
Tested locally at llvm/llvm-project commit 450cf0aafeab9fed6666b112b018d3913a36527c (LLVM 24.0.0git).
Reproducer
module {
func.func private @<!-- -->escape(memref<?xf64>)
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>
}
%view = memref.cast %comm : memref<32xf64> to memref<?xf64>
func.call @<!-- -->escape(%view) : (memref<?xf64>) -> ()
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 dc8.mlir \
--pass-pipeline='builtin.module(func.func(affine-loop-fusion{mode=producer maximal}))'
Result
The pass emits:
module {
func.func private @<!-- -->escape(memref<?xf64>)
func.func @<!-- -->control(%arg0: memref<32xf64>, %arg1: memref<32xf64>, %arg2: memref<32xf64>) {
%cast = memref.cast %arg1 : memref<32xf64> to memref<?xf64>
call @<!-- -->escape(%cast) : (memref<?xf64>) -> ()
affine.for %arg3 = 0 to 16 {
%0 = affine.load %arg0[%arg3] : memref<32xf64>
%1 = arith.addf %0, %0 : f64
affine.store %1, %arg1[%arg3] : memref<32xf64>
%2 = affine.load %arg1[%arg3] : memref<32xf64>
%3 = arith.addf %2, %2 : f64
affine.store %3, %arg2[%arg3] : memref<32xf64>
}
return
}
}
In the source, the producer fills %comm, then @<!-- -->escape modifies it, and only then does the consumer read it. In the transformed program, @<!-- -->escape runs before the producer, whose stores overwrite the call's results before the consumer reads them.
For a legal synchronous implementation that writes comm[k] = 8192 + k for k = 0..15, the source produces out[0] = 16384, while the transformed program produces out[0] = 4.
Both the source and transformed IR verify. The concrete execution uses initialized, distinct buffers, in-bounds accesses, and a synchronous non-retaining callee.
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 @escape(memref<?xf64>)
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>
}
%view = memref.cast %comm : memref<32xf64> to memref<?xf64>
func.call @escape(%view) : (memref<?xf64>) -> ()
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
%three = arith.constant 3.0 : f64
%five = arith.constant 5.0 : f64
%one = arith.constant 1.0 : f64
%thousand = arith.constant 1000.0 : f64
%seven = arith.constant 7.0 : f64
%ink = arith.mulf %three, %kf : f64
%in_value = arith.addf %ink, %one : f64
%commk = arith.mulf %five, %kf : f64
%comm_value = arith.addf %commk, %thousand : f64
%outk = arith.mulf %seven, %kf : f64
%out_sum = arith.addf %outk, %thousand : f64
%out_value = arith.negf %out_sum : 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>
void escape(double *allocated, double *aligned, int64_t offset, int64_t size,
int64_t stride) {
(void)allocated;
(void)size;
for (int64_t k = 0; k < 16; ++k)
aligned[offset + k * stride] = 8192.0 + (double)k;
}
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: 16384
with affine-loop-fusion: 4
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.
Working on this.