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

[MLIR] Missed Optimization: XeGPU layout propagation misses redundant round-trip conversions with multiple users

Open dev-816 opened this issue 2 months ago • 3 comments

The resolver only retargets a producer conversion when its result has one use. When %middle has multiple users, satisfying the A-layout consumer can introduce an unnecessary B-to-A conversion. math.sqrt could directly use %input, which already holds the same value in layout A, while preserving the conversion needed by the B-layout consumer.

Code Example

input.mlir

#A = #xegpu.layout<sg_layout = [1, 4], sg_data = [32, 16]>
#B = #xegpu.layout<sg_layout = [2, 2], sg_data = [16, 32]>

gpu.module @test {
  func.func @round_trip(%arg: vector<32x64xf32>)
      -> (vector<32x64xf32>, vector<32x64xf32>) {
    %input = xegpu.convert_layout %arg <{target_layout = #A}>
        : vector<32x64xf32>
    %middle = xegpu.convert_layout %input
        <{input_layout = #A, target_layout = #B}>
        : vector<32x64xf32>
    %b = math.exp %middle : vector<32x64xf32>
    %keep = xegpu.convert_layout %b <{target_layout = #B}>
        : vector<32x64xf32>
    %a = math.sqrt %middle : vector<32x64xf32>
    %result = xegpu.convert_layout %a <{target_layout = #A}>
        : vector<32x64xf32>
    return %keep, %result : vector<32x64xf32>, vector<32x64xf32>
  }
}

Command

mlir-opt input.mlir --pass-pipeline='builtin.module(gpu.module(xegpu-propagate-layout{layout-kind=subgroup}))'

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

dev-816 avatar Oct 02 '26 04:10 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

The resolver only retargets a producer conversion when its result has one use. When `%middle` has multiple users, satisfying the A-layout consumer can introduce an unnecessary B-to-A conversion. `math.sqrt` could directly use `%input`, which already holds the same value in layout A, while preserving the conversion needed by the B-layout consumer.

Code Example

input.mlir

#A = #xegpu.layout&lt;sg_layout = [1, 4], sg_data = [32, 16]&gt;
#B = #xegpu.layout&lt;sg_layout = [2, 2], sg_data = [16, 32]&gt;

gpu.module @<!-- -->test {
  func.func @<!-- -->round_trip(%arg: vector&lt;32x64xf32&gt;)
      -&gt; (vector&lt;32x64xf32&gt;, vector&lt;32x64xf32&gt;) {
    %input = xegpu.convert_layout %arg &lt;{target_layout = #A}&gt;
        : vector&lt;32x64xf32&gt;
    %middle = xegpu.convert_layout %input
        &lt;{input_layout = #A, target_layout = #B}&gt;
        : vector&lt;32x64xf32&gt;
    %b = math.exp %middle : vector&lt;32x64xf32&gt;
    %keep = xegpu.convert_layout %b &lt;{target_layout = #B}&gt;
        : vector&lt;32x64xf32&gt;
    %a = math.sqrt %middle : vector&lt;32x64xf32&gt;
    %result = xegpu.convert_layout %a &lt;{target_layout = #A}&gt;
        : vector&lt;32x64xf32&gt;
    return %keep, %result : vector&lt;32x64xf32&gt;, vector&lt;32x64xf32&gt;
  }
}

Command

mlir-opt input.mlir --pass-pipeline='builtin.module(gpu.module(xegpu-propagate-layout{layout-kind=subgroup}))'

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

hi, can I take a look at this?

Naaz30 avatar Oct 02 '26 21:10 Naaz30

There is no point creating some artificial intermediate IR and try to optimize them. You are welcome to use xegpu dialect to construct meaningful high-perf kernel, identify performance issues, and propose PRs.

Jianhui-Li avatar Oct 18 '26 05:10 Jianhui-Li