[MLIR] Missed Optimization: XeGPU layout propagation misses redundant round-trip conversions with multiple users
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
@llvm/issue-subscribers-mlir
Author: CC_Fans (dev-816)
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
hi, can I take a look at this?
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.