Skip to content

Commit 5d849d3

Browse files
authored
[mlir][xegpu] Fix seg-fault caused by setting a null attribute (#146002)
1 parent 829f2f2 commit 5d849d3

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

mlir/lib/Dialect/XeGPU/Transforms/XeGPUWgToSgDistribute.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,12 @@ struct WgToSgElementwiseOp : public ConversionPattern {
376376
// Copy all attributes, but update "layout_result_0" to drop
377377
// sgLayout/sgData
378378
for (auto attr : op->getAttrs()) {
379-
if (auto layout = dyn_cast<xegpu::LayoutAttr>(attr.getValue()))
380-
state.addAttribute(attr.getName(), layout.dropSgLayoutAndData());
381-
else
379+
if (auto layout = dyn_cast<xegpu::LayoutAttr>(attr.getValue())) {
380+
if (auto newLayout = layout.dropSgLayoutAndData())
381+
state.addAttribute(attr.getName(), newLayout);
382+
} else {
382383
state.addAttribute(attr.getName(), attr.getValue());
384+
}
383385
}
384386
Operation *newOp = rewriter.create(state);
385387
newResults.push_back(newOp->getResult(0));
@@ -629,8 +631,10 @@ void XeGPUWgToSgDistributePass::runOnOperation() {
629631
std::string name = xegpu::getLayoutName(result);
630632
if (auto layout = op->getAttrOfType<xegpu::LayoutAttr>(name)) {
631633
op->removeAttr(name);
632-
if (!isa<scf::IfOp, scf::ForOp, scf::WhileOp, scf::ConditionOp>(op))
633-
op->setAttr(name, layout.dropSgLayoutAndData());
634+
if (!isa<scf::IfOp, scf::ForOp, scf::WhileOp, scf::ConditionOp>(op)) {
635+
if (auto newLayout = layout.dropSgLayoutAndData())
636+
op->setAttr(name, newLayout);
637+
}
634638
}
635639
}
636640
});

mlir/test/Dialect/XeGPU/xegpu-wg-to-sg-elemwise.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
// RUN: mlir-opt --xegpu-wg-to-sg-distribute -split-input-file %s | FileCheck %s
22

33
gpu.module @test_elementwise_ops {
4+
5+
// CHECK-LABEL: unary_ops_sg_layout_only
6+
gpu.func @unary_ops_sg_layout_only(%a: memref<24x32xf32>) {
7+
%tdesc_a = xegpu.create_nd_tdesc %a[0, 0] : memref<24x32xf32>
8+
-> !xegpu.tensor_desc<24x32xf32, #xegpu.layout<sg_layout = [2, 4], sg_data = [12, 8]>>
9+
%load_a = xegpu.load_nd %tdesc_a
10+
: !xegpu.tensor_desc<24x32xf32, #xegpu.layout<sg_layout = [2, 4], sg_data = [12, 8]>>
11+
-> vector<24x32xf32>
12+
// CHECK: math.exp {{.*}} : vector<12x8xf32>
13+
%exp = math.exp %load_a
14+
{layout_result_0 = #xegpu.layout<sg_layout = [2, 4], sg_data = [12, 8]>}
15+
: vector<24x32xf32>
16+
// CHECK: arith.negf {{.*}} : vector<12x8xf32>
17+
%negf = arith.negf %load_a
18+
{layout_result_0 = #xegpu.layout<sg_layout = [2, 4], sg_data = [12, 8]>}
19+
: vector<24x32xf32>
20+
gpu.return
21+
}
22+
423
// CHECK-LABEL: unary_ops
524
gpu.func @unary_ops(%a: memref<24x32xf32>) {
625
%tdesc_a = xegpu.create_nd_tdesc %a[0, 0] : memref<24x32xf32>

0 commit comments

Comments
 (0)