Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions mlir/lib/Dialect/XeGPU/Transforms/XeGPUWgToSgDistribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,48 @@ struct UnrealizedConversionCastOpPattern
}
};

// This pattern distributes arith.constant op into subgroup-level constants
struct WgToSgArithConstantOp : public OpConversionPattern<arith::ConstantOp> {
using OpConversionPattern<arith::ConstantOp>::OpConversionPattern;

LogicalResult
matchAndRewrite(arith::ConstantOp op, OneToNOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
auto vecAttr = dyn_cast<DenseElementsAttr>(op.getValue());
auto vecType = dyn_cast<VectorType>(op.getType());
if (!vecAttr || !vecAttr.isSplat() || !vecType)
return failure();

xegpu::LayoutAttr layout = xegpu::getLayoutAttr(op.getResult());
if (!layout || !layout.getSgLayout())
return failure();

ArrayRef<int64_t> wgShape = vecType.getShape();
SmallVector<int64_t> sgShape;
int count;
std::tie(sgShape, count) = getSgShapeAndCount(wgShape, layout);

// Current limitation: constant of vector with single value.
// TODO: support more complex cases, e.g., vector with multiple values.
Attribute singleVal = vecAttr.getSplatValue<Attribute>();

SmallVector<Value> newConsts;
auto newType = VectorType::get(sgShape, vecType.getElementType());
auto newLayout = layout.dropSgLayoutAndData();
for (int i = 0; i < count; ++i) {
auto sgAttr = DenseElementsAttr::get(newType, singleVal);
auto cstOp =
rewriter.create<arith::ConstantOp>(op.getLoc(), newType, sgAttr);
if (newLayout)
xegpu::setLayoutAttr(cstOp->getResult(0), newLayout);
newConsts.push_back(cstOp);
}

rewriter.replaceOpWithMultiple(op, {newConsts});
return success();
}
};

} // namespace

namespace mlir {
Expand All @@ -657,8 +699,8 @@ void populateXeGPUWgToSgDistributePatterns(RewritePatternSet &patterns) {
patterns.add<WgToSgCreateNdOp, WgToSgLoadNdOp, WgToSgStoreNdOp,
WgToSgUpdateNdOffsetOp, WgToSgDpasOp, WgToSgPrefetchNdOp,
UnrealizedConversionCastOpPattern, WgToSgElementwiseOp,
WgToSgVectorBroadcastOp, WgToSgConvertLayoutOp>(
patterns.getContext());
WgToSgVectorBroadcastOp, WgToSgConvertLayoutOp,
WgToSgArithConstantOp>(patterns.getContext());
}
} // namespace xegpu
} // namespace mlir
Expand Down Expand Up @@ -770,6 +812,14 @@ void XeGPUWgToSgDistributePass::runOnOperation() {
return isLegal(xegpu::getLayoutAttr(op.getResult()));
});

target.addDynamicallyLegalOp<arith::ConstantOp>(
[=](arith::ConstantOp op) -> bool {
auto vecType = dyn_cast<VectorType>(op.getType());
if (!vecType)
return true;
return isLegal(xegpu::getLayoutAttr(op.getResult()));
});

target.addDynamicallyLegalOp<xegpu::ConvertLayoutOp>(
[=](xegpu::ConvertLayoutOp op) -> bool {
return isLegal(op.getInputLayout()) && isLegal(op.getTargetLayout());
Expand Down
7 changes: 7 additions & 0 deletions mlir/test/Dialect/XeGPU/xegpu-wg-to-sg-rr.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,11 @@ gpu.module @test_round_robin_assignment {
target_layout = #xegpu.layout<sg_layout = [2, 2], sg_data = [16, 16], inst_data = [8, 16]>}> : vector<32x64xf32>
gpu.return
}

// CHECK-LABEL: distribute_constant
gpu.func @distribute_constant() {
// CHECK-COUNT-4: arith.constant dense<1.000000e+00> : vector<16x16xf32>
%cst = arith.constant {layout_result_0 = #xegpu.layout<sg_layout = [8, 4], sg_data = [16, 16]>} dense<1.0> : vector<256x128xf32>
gpu.return
}
}
7 changes: 7 additions & 0 deletions mlir/test/Dialect/XeGPU/xegpu-wg-to-sg.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,11 @@ gpu.func @dpas_no_sg_data(%a: memref<24x32xf32>, %b: memref<32x24xf32>) {
} {sg_id_range = #xegpu.range<[3, 19]>}
gpu.return
}

// CHECK-LABEL: distribute_constant
gpu.func @distribute_constant() {
// CHECK: arith.constant dense<1.000000e+00> : vector<32x32xf32>
%cst = arith.constant {layout_result_0 = #xegpu.layout<sg_layout = [8, 4], sg_data = [32, 32]>} dense<1.0> : vector<256x128xf32>
gpu.return
}
}