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

[mlir][xegpu] propagate-layout infers a source layout for a splitting vector.shape_cast that changes per-subgroup element ownership

Open snarang181 opened this issue 2 months ago • 2 comments

Summary

xegpu-propagate-layout=layout-kind=subgroup infers the source layout of a one-to-many vector.shape_cast by collapsing the result layout's sg_layout and sg_data independently (collapseDims). When the result layout distributes an inner dimension while each subgroup's data tile spans an outer dimension, the per-subgroup element set is non-contiguous in the flattened source, but the inferred 1-D layout is contiguous. No convert_layout is inserted and everything verifies, so the two sides of a data-movement-free reshape disagree about which elements a subgroup owns.

gpu.module @test {
  gpu.func @shape_cast_expand(%dst: memref<2x4xf32>) kernel {
    %src = arith.constant dense<0.0> : vector<8xf32>
    %result = vector.shape_cast %src : vector<8xf32> to vector<2x4xf32>
    %td = xegpu.create_nd_tdesc %dst : memref<2x4xf32> -> !xegpu.tensor_desc<2x4xf32>
    xegpu.store_nd %result, %td[0, 0]
      <{layout = #xegpu.layout<sg_layout = [1, 2], sg_data = [2, 2], order = [0, 1]>}>
      : vector<2x4xf32>, !xegpu.tensor_desc<2x4xf32>
    gpu.return
  }
}
mlir-opt -xevm-attach-target='chip=cri' -xegpu-propagate-layout='layout-kind=subgroup' repro.mlir
produces on the source:
#xegpu.layout<sg_layout = [2], sg_data = [4], order = [0]>

Expected

The result layout gives subgroup 1 the 2×2 tile based at (0, 2), i.e. elements (0,2),(0,3),(1,2),(1,3) = flattened {2, 3, 6, 7}. A row-major shape_cast moves no data, so the source layout must assign subgroup 1 exactly those elements. sg_layout=[2], sg_data=[2] does (two distribution units, round-robin). The inferred sg_data=[4] assigns it {4, 5, 6, 7} which is two of four elements wrong.

Configuration

LLVM/MLIR revision: 31a1c03fe67cccd917f11e5ab50cc425770c1400 Build: cmake -G Ninja -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON

snarang181 avatar Sep 18 '26 14:09 snarang181

@llvm/issue-subscribers-mlir

Author: Samarth Narang (snarang181)

## Summary

xegpu-propagate-layout=layout-kind=subgroup infers the source layout of a one-to-many vector.shape_cast by collapsing the result layout's sg_layout and sg_data independently (collapseDims). When the result layout distributes an inner dimension while each subgroup's data tile spans an outer dimension, the per-subgroup element set is non-contiguous in the flattened source, but the inferred 1-D layout is contiguous. No convert_layout is inserted and everything verifies, so the two sides of a data-movement-free reshape disagree about which elements a subgroup owns.

gpu.module @<!-- -->test {
  gpu.func @<!-- -->shape_cast_expand(%dst: memref&lt;2x4xf32&gt;) kernel {
    %src = arith.constant dense&lt;0.0&gt; : vector&lt;8xf32&gt;
    %result = vector.shape_cast %src : vector&lt;8xf32&gt; to vector&lt;2x4xf32&gt;
    %td = xegpu.create_nd_tdesc %dst : memref&lt;2x4xf32&gt; -&gt; !xegpu.tensor_desc&lt;2x4xf32&gt;
    xegpu.store_nd %result, %td[0, 0]
      &lt;{layout = #xegpu.layout&lt;sg_layout = [1, 2], sg_data = [2, 2], order = [0, 1]&gt;}&gt;
      : vector&lt;2x4xf32&gt;, !xegpu.tensor_desc&lt;2x4xf32&gt;
    gpu.return
  }
}
mlir-opt -xevm-attach-target='chip=cri' -xegpu-propagate-layout='layout-kind=subgroup' repro.mlir
produces on the source:
#xegpu.layout&lt;sg_layout = [2], sg_data = [4], order = [0]&gt;

Expected

The result layout gives subgroup 1 the 2×2 tile based at (0, 2), i.e. elements (0,2),(0,3),(1,2),(1,3) = flattened {2, 3, 6, 7}. A row-major shape_cast moves no data, so the source layout must assign subgroup 1 exactly those elements. sg_layout=[2], sg_data=[2] does (two distribution units, round-robin). The inferred sg_data=[4] assigns it {4, 5, 6, 7} which is two of four elements wrong.

Configuration

LLVM/MLIR revision: 31a1c03fe67cccd917f11e5ab50cc425770c1400 Build: cmake -G Ninja -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON

hi, can I take a look at this one ?

Naaz30 avatar Sep 19 '26 03:09 Naaz30