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

[mlir][hoisting] Currently linalg hoisting can not optimize memref.assume_alignment

Open xiangzh1 opened this issue 1 year ago • 7 comments

Last month (2025-05-18)Kleiman update the AssumeAlignmentOp, let it has AnyMemRef result。 this make the following changes:

%2 = hal.interface.binding.subspan layout ... : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>>
 memref.assume_alignment %2, 64 : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>>
use %2

change to

%2 = hal.interface.binding.subspan layout ... : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>>
%assume_align = memref.assume_alignment %2, 64 : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>>
use %assume_align

Problem: This will affect the linalg hoisting optimization,due to the memref.assume_alignment inherited the interface ViewLikeOpInterface which is excluded by linalg hoisting.

for example , in follow mlir, the "%1 = vector.transfer_read %assume_align_0[%c0, %c0] ..." and "vector.transfer_write %3, %assume_align_0[%c0, %c0]" read from and write to a same location. We can hoist them out of loop:

%m0 = hal.interface.binding.subspan layout ...: memref<4096x4096xf16>
 %m1 = hal.interface.binding.subspan layout ...: memref<4096x4096xf16>
 %assume_align_0 = memref.assume_alignment %m0, 64 : memref<4096x4096xf16>
 %assume_align_1 = memref.assume_alignment %m1, 64 : memref<4096x4096xf16>
 scf.for %arg0 = %c256 to %c4096 step %c256 {
   %1 = vector.transfer_read %assume_align_0[%c0, %c0], %cst_0 {in_bounds = [true, true]} : memref<4096x4096xf16>, vector<16x16xf16>
   %2 = vector.transfer_read %m1[%arg0, %arg0], %cst_0 {in_bounds = [true, true]} : memref<4096x4096xf16>, vector<16x16xf16>
   %3 = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d2, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %2, %2, %1 : vector<16x16xf16>, vector<16x16xf16> into vector<16x16xf16>
   vector.transfer_write %3, %assume_align_0[%c0, %c0] {in_bounds = [true, true]} : vector<16x16xf16>, memref<4096x4096xf16>
 }

but due to the transfer_read/write from/to an assume_alignment operation. The linalg hoisting stop do optimization for it. (I am not much understand why the linalg hoisting do this, I am a beginner in mlir) But the assume_alignment just mark memref's alignment, The linalg hoisting should check its memref operand not it self. so we expect the upper mlir can be optimized to:

   %m0 = hal.interface.binding.subspan layout ...: memref<4096x4096xf16>
    %m1 = hal.interface.binding.subspan layout ...: memref<4096x4096xf16>
    %assume_align_0 = memref.assume_alignment %m0, 64 : memref<4096x4096xf16>
    %assume_align_1 = memref.assume_alignment %m1, 64 : memref<4096x4096xf16>
    %0 = vector.transfer_read %assume_align[%c0, %c0], %cst {in_bounds = [true, true]} : memref<4096x4096xf16>, vector<16x16xf16> // out of loop
    %1 = scf.for %arg0 = %c256 to %c4096 step %c256 iter_args(%arg1 = %0) -> (vector<16x16xf16>) {
      %2 = vector.transfer_read %assume_align_1[%arg0, %arg0], %cst {in_bounds = [true, true]} : memref<4096x4096xf16>, vector<16x16xf16>
      %3 = vector.contract {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %2, %2, %arg1 : vector<16x16xf16>, vector<16x16xf16> into vector<16x16xf16>
      scf.yield %3 : vector<16x16xf16>
    }
    vector.transfer_write %1, %assume_align[%c0, %c0] {in_bounds = [true, true]} : vector<16x16xf16>, memref<4096x4096xf16> // out of loop

detailed example pls refer to example (I don't not how to write hal.interface.binding for mlir-opt so, in the example I use memref.alloc() instead of them.)

xiangzh1 avatar Jun 19 '25 02:06 xiangzh1

@llvm/issue-subscribers-mlir

Author: XiangZhang (xiangzh1)

Last month (2025-05-18)Kleiman update the AssumeAlignmentOp, let it has AnyMemRef result。 this make the following changes:
%2 = hal.interface.binding.subspan layout ... : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
 memref.assume_alignment %2, 64 : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
use %2

change to

%2 = hal.interface.binding.subspan layout ... : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
%assume_align = memref.assume_alignment %2, 64 : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
use %assume_align

Problem: This will affect the linalg hoisting optimization,due to the memref.assume_alignment inherited the interface ViewLikeOpInterface which is excluded by linalg hoisting.

for example , in follow mlir, the "%1 = vector.transfer_read %assume_align_0[%c0, %c0] ..." and "vector.transfer_write %3, %assume_align_0[%c0, %c0]" read from and write to a same location. We can hoist them out of loop:

%m0 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
 %m1 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
 %assume_align_0 = memref.assume_alignment %m0, 64 : memref&lt;4096x4096xf16&gt;
 %assume_align_1 = memref.assume_alignment %m1, 64 : memref&lt;4096x4096xf16&gt;
 scf.for %arg0 = %c256 to %c4096 step %c256 {
   %1 = vector.transfer_read %assume_align_0[%c0, %c0], %cst_0 {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
   %2 = vector.transfer_read %m1[%arg0, %arg0], %cst_0 {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
   %3 = vector.contract {indexing_maps = [affine_map&lt;(d0, d1, d2) -&gt; (d0, d2)&gt;, affine_map&lt;(d0, d1, d2) -&gt; (d2, d1)&gt;, affine_map&lt;(d0, d1, d2) -&gt; (d0, d1)&gt;], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind&lt;add&gt;} %2, %2, %1 : vector&lt;16x16xf16&gt;, vector&lt;16x16xf16&gt; into vector&lt;16x16xf16&gt;
   vector.transfer_write %3, %assume_align_0[%c0, %c0] {in_bounds = [true, true]} : vector&lt;16x16xf16&gt;, memref&lt;4096x4096xf16&gt;
 }

but due to the transfer_read/write from/to an assume_alignment operation. The linalg hoisting stop do optimization for it. (I am not much understand why the linalg hoisting do this, I am a beginner in mlir) But the assume_alignment just mark memref's alignment, The linalg hoisting should check its memref operand not it self. so we expect the upper mlir can be optimized to:

   %m0 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
    %m1 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
    %assume_align_0 = memref.assume_alignment %m0, 64 : memref&lt;4096x4096xf16&gt;
    %assume_align_1 = memref.assume_alignment %m1, 64 : memref&lt;4096x4096xf16&gt;
    %0 = vector.transfer_read %assume_align[%c0, %c0], %cst {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt; // out of loop
    %1 = scf.for %arg0 = %c256 to %c4096 step %c256 iter_args(%arg1 = %0) -&gt; (vector&lt;16x16xf16&gt;) {
      %2 = vector.transfer_read %assume_align_1[%arg0, %arg0], %cst {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
      %3 = vector.contract {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind&lt;add&gt;} %2, %2, %arg1 : vector&lt;16x16xf16&gt;, vector&lt;16x16xf16&gt; into vector&lt;16x16xf16&gt;
      scf.yield %3 : vector&lt;16x16xf16&gt;
    }
    vector.transfer_write %1, %assume_align[%c0, %c0] {in_bounds = [true, true]} : vector&lt;16x16xf16&gt;, memref&lt;4096x4096xf16&gt; // out of loop

detailed example pls refer to example (I don't not how to write hal.interface.binding for mlir-opt so, in the example I use memref.alloc() instead of them.)

llvmbot avatar Jun 19 '25 02:06 llvmbot

@xiangzh1 can I be assigned this issue?

Ameya674 avatar Jun 19 '25 04:06 Ameya674

@xiangzh1 can I be assigned this issue?

hi @Ameya674 I already have a patch for it,let me add you as reviewer.

xiangzh1 avatar Jun 19 '25 12:06 xiangzh1

@xiangzh1 can I be assigned this issue?

hi @Ameya674 I already have a patch for it,let me add you as reviewer.

It is strange that I can't search your name for reviewers , the PR is here : https://github.com/llvm/llvm-project/pull/144843 pls add you as reviewer if possible.

xiangzh1 avatar Jun 19 '25 12:06 xiangzh1

@llvm/issue-subscribers-mlir-linalg

Author: XiangZhang (xiangzh1)

Last month (2025-05-18)Kleiman update the AssumeAlignmentOp, let it has AnyMemRef result。 this make the following changes:
%2 = hal.interface.binding.subspan layout ... : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
 memref.assume_alignment %2, 64 : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
use %2

change to

%2 = hal.interface.binding.subspan layout ... : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
%assume_align = memref.assume_alignment %2, 64 : memref&lt;4096x4096xf16, #hal.descriptor_type&lt;storage_buffer&gt;&gt;
use %assume_align

Problem: This will affect the linalg hoisting optimization,due to the memref.assume_alignment inherited the interface ViewLikeOpInterface which is excluded by linalg hoisting.

for example , in follow mlir, the "%1 = vector.transfer_read %assume_align_0[%c0, %c0] ..." and "vector.transfer_write %3, %assume_align_0[%c0, %c0]" read from and write to a same location. We can hoist them out of loop:

%m0 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
 %m1 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
 %assume_align_0 = memref.assume_alignment %m0, 64 : memref&lt;4096x4096xf16&gt;
 %assume_align_1 = memref.assume_alignment %m1, 64 : memref&lt;4096x4096xf16&gt;
 scf.for %arg0 = %c256 to %c4096 step %c256 {
   %1 = vector.transfer_read %assume_align_0[%c0, %c0], %cst_0 {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
   %2 = vector.transfer_read %m1[%arg0, %arg0], %cst_0 {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
   %3 = vector.contract {indexing_maps = [affine_map&lt;(d0, d1, d2) -&gt; (d0, d2)&gt;, affine_map&lt;(d0, d1, d2) -&gt; (d2, d1)&gt;, affine_map&lt;(d0, d1, d2) -&gt; (d0, d1)&gt;], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind&lt;add&gt;} %2, %2, %1 : vector&lt;16x16xf16&gt;, vector&lt;16x16xf16&gt; into vector&lt;16x16xf16&gt;
   vector.transfer_write %3, %assume_align_0[%c0, %c0] {in_bounds = [true, true]} : vector&lt;16x16xf16&gt;, memref&lt;4096x4096xf16&gt;
 }

but due to the transfer_read/write from/to an assume_alignment operation. The linalg hoisting stop do optimization for it. (I am not much understand why the linalg hoisting do this, I am a beginner in mlir) But the assume_alignment just mark memref's alignment, The linalg hoisting should check its memref operand not it self. so we expect the upper mlir can be optimized to:

   %m0 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
    %m1 = hal.interface.binding.subspan layout ...: memref&lt;4096x4096xf16&gt;
    %assume_align_0 = memref.assume_alignment %m0, 64 : memref&lt;4096x4096xf16&gt;
    %assume_align_1 = memref.assume_alignment %m1, 64 : memref&lt;4096x4096xf16&gt;
    %0 = vector.transfer_read %assume_align[%c0, %c0], %cst {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt; // out of loop
    %1 = scf.for %arg0 = %c256 to %c4096 step %c256 iter_args(%arg1 = %0) -&gt; (vector&lt;16x16xf16&gt;) {
      %2 = vector.transfer_read %assume_align_1[%arg0, %arg0], %cst {in_bounds = [true, true]} : memref&lt;4096x4096xf16&gt;, vector&lt;16x16xf16&gt;
      %3 = vector.contract {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind&lt;add&gt;} %2, %2, %arg1 : vector&lt;16x16xf16&gt;, vector&lt;16x16xf16&gt; into vector&lt;16x16xf16&gt;
      scf.yield %3 : vector&lt;16x16xf16&gt;
    }
    vector.transfer_write %1, %assume_align[%c0, %c0] {in_bounds = [true, true]} : vector&lt;16x16xf16&gt;, memref&lt;4096x4096xf16&gt; // out of loop

detailed example pls refer to example (I don't not how to write hal.interface.binding for mlir-opt so, in the example I use memref.alloc() instead of them.)

llvmbot avatar Jun 19 '25 13:06 llvmbot

Thank you for posting this!

%2 = hal.interface.binding.subspan layout ... : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>> memref.assume_alignment %2, 64 : memref<4096x4096xf16, #hal.descriptor_type<storage_buffer>> use %2

Please provide a reproducer using dialects from upstream MLIR. hal is not an upstream dialect.

Last month (2025-05-18)Kleiman update the AssumeAlignmentOp, let it has AnyMemRef result。 this make the following changes:

Please provide a link to the PR that introduced that change and a SHA of LLVM that can be used to reproduce this.


IIUC, this issue is already being discussed in https://github.com/llvm/llvm-project/pull/144809. Please, can you confirm? If the answer is "yes" then we should make sure that the discussion happens in one place.

banach-space avatar Jun 22 '25 13:06 banach-space

Yes, they are same problem.

xiangzh1 avatar Jun 23 '25 00:06 xiangzh1