MLIR 23.0.0git
XeGPULayoutImpl.h
Go to the documentation of this file.
1//===- XeGPULayoutImpl.h - Layout utility functions ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_DIALECT_XEGPU_UTILS_XeGPULayoutImpl_H_
10#define MLIR_DIALECT_XEGPU_UTILS_XeGPULayoutImpl_H_
11
17
18namespace mlir {
19
20class VectorType;
21class OpOperand;
22class OpResult;
23class OpBuilder;
24class ValueRange;
25class TypeConverter;
26class OpFoldResult;
27
28namespace xegpu {
29class DistributeLayoutAttr;
30class LayoutAttr;
31class TensorDescType;
32} // namespace xegpu
33
34namespace xegpu {
35
36LogicalResult propagateLayouts(OpBuilder &builder, Operation *target,
37 LayoutKind layoutKind, unsigned indexBitWidth,
38 bool printOnly = false);
39
40LogicalResult resolveLayoutConflicts(Operation *target);
41
42/// [to-be-deprecated] Set the DistributeLayoutAttr for each OpOperand and
43/// OpResult of of the given operation. If the operation contains regions, it is
44/// also applied recursively to the contained operations operation.
45/// TODO: To be replaced by recoverTemporaryLayouts()
46void recoverTemporaryLayoutsDeprecated(Operation *op);
47
48/// Attach layout attributes to all vector-type operands of operations within
49/// the given operation's nested region. Reports an error if any vector operand
50/// lacks a layout attribute.
51bool recoverTemporaryLayouts(Operation *rootOp);
52
53/// Removes the LayoutAttr for a given OpOperand or OpResult if it exists.
54template <typename T,
55 typename = std::enable_if_t<std::is_same_v<T, OpOperand> ||
56 std::is_same_v<T, OpResult>>>
57void removeLayoutAttr(const T &operandOrResult);
58
59/// Removes the DistributeLayoutAttr for each OpOperand and OpResult of the
60/// given operation if they exist. If the operation contains regions, it is also
61/// applied recursively to the contained operations
62void removeLayoutAttrs(Operation *op);
63
64/// Updates the NamedAttribute sequence by dropping sg-layout and
65/// sg-data information from any DistributeLayoutAttr found.
66SmallVector<NamedAttribute>
67dropSgLayoutAndDataOnAttrs(ArrayRef<NamedAttribute> attrs);
68
69/// Updates the NamedAttribute sequence by dropping inst-data information from
70/// any DistributeLayoutAttr found.
71SmallVector<NamedAttribute> dropInstDataOnAttrs(ArrayRef<NamedAttribute> attrs);
72
73/// Infers the source layout attribute for a broadcast operation given the
74/// result layout attribute, result shape, and source shape.
75DistributeLayoutAttr inferBroadcastSourceLayout(DistributeLayoutAttr resLayout,
76 ArrayRef<int64_t> resShape,
77 ArrayRef<int64_t> srcShape);
78
79/// Infers the source layout attribute for a reduction operation given the
80/// result layout attribute and reduced dims.
81DistributeLayoutAttr
82inferMultiReductionSourceLayout(DistributeLayoutAttr resLayout,
83 SmallVector<int64_t> reduceDims);
84
85/// Infers the source layout attribute for a transpose operation given the
86/// result layout attribute and permutation.
87DistributeLayoutAttr inferTransposeSourceLayout(DistributeLayoutAttr resLayout,
88 ArrayRef<int64_t> permutation);
89
90/// Infers the source layout attribute for a bitcast operation given the
91/// result layout attribute, result element type bitwidth, and source element
92/// type bitwidth.
93DistributeLayoutAttr inferBitCastSourceLayout(DistributeLayoutAttr resLayout,
94 int resElemTyBitWidth,
95 int srcElemTyBitWidth);
96
97/// Infers the source layout attribute for a shape cast operation given the
98/// result layout attribute, result shape, and source shape.
99DistributeLayoutAttr inferShapeCastSourceLayout(DistributeLayoutAttr resLayout,
100 ArrayRef<int64_t> resShape,
101 ArrayRef<int64_t> srcShape);
102
103/// Infers the source layout attribute for an insert strided slice operation
104/// given the result layout attribute, result shape, and source shape. Removes
105/// leading dimensions from the result layout to match the source shape size.
106DistributeLayoutAttr
107inferInsertStridedSliceSourceLayout(DistributeLayoutAttr resLayout,
108 ArrayRef<int64_t> resShape,
109 ArrayRef<int64_t> srcShape);
110
111/// Sets up layout for reduction operations by creating a SliceAttr for the
112/// result.
113///
114/// This function first attempts to construct a source layout that, when
115/// sliced along reduction dimensions, produces a result layout compatible
116/// with the consumer's preferred layout. This minimizes data redistribution
117/// overhead. The SliceAttr for the result is then created based on the
118/// derived source layout and the specified reduction dimensions.
120 VectorType srcVectorTy,
121 DistributeLayoutAttr consumerLayout,
122 SmallVector<int64_t> reductionDims,
123 const uArch::uArch *uArch);
124
125/// Setup the result layout attribute for a bitcast operation based on element
126/// type bitwidths. This ensures the source layout can always be derived from
127/// the result layout.
128///
129/// When casting from a narrower to a wider element type (srcElemTyBitWidth <
130/// resElemTyBitWidth), the result layout's innermost dimension data sizes
131/// (inst_data, lane_data) are scaled up by the bitwidth ratio. This maintains
132/// the invariant that the source layout can be recovered by adjusting the
133/// result layout based on bitwidth ratio of input vs output.
134DistributeLayoutAttr setupBitCastResultLayout(
135 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
136 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
137
138/// Sets up the result layout for an insert strided slice operation.
139/// Creates a result layout based on the specified layout kind (InstData or
140/// Lane).
142 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
143 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
144
145/// Sets up the anchor layout for a load gather operation.
146DistributeLayoutAttr
147setupLoadGatherAnchorLayout(LayoutKind layoutKind, VectorType vectorTy,
148 int chunkSize, DistributeLayoutAttr consumerLayout,
149 const uArch::uArch *uArch);
150
151/// Sets up the anchor layout for load matrix operation.
152DistributeLayoutAttr
153setupLoadMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy,
154 DistributeLayoutAttr consumerLayout,
155 const uArch::uArch *uArch);
156
157/// Sets up the anchor layout for a store scatter operation.
158DistributeLayoutAttr setupStoreScatterAnchorLayout(LayoutKind layoutKind,
159 VectorType vectorTy,
160 int chunkSize,
161 const uArch::uArch *uArch);
162
163/// Sets up the anchor layout for a store matrix operation.
164DistributeLayoutAttr setupStoreMatrixAnchorLayout(LayoutKind layoutKind,
165 VectorType vectorTy,
166 const uArch::uArch *uArch);
167
168/// Sets up the anchor layouts for a dpas operands (A, B, and C/D).
169/// The numSg and consumerLayout (optional) are only used by sg layout creation.
170std::optional<std::tuple<DistributeLayoutAttr, DistributeLayoutAttr,
171 DistributeLayoutAttr>>
172setupDpasLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy,
173 VectorType cdTy, DistributeLayoutAttr consumerLayout,
174 const uArch::uArch *uArch, int numSg);
175
176/// Gets the expected layout for a given consumer operand. This will check if
177/// the owning operation of the consumer operand is one of the special layout
178/// users and determine the expected layout accordingly.
179xegpu::DistributeLayoutAttr getConsumerLayoutAt(OpOperand &operand);
180
181} // namespace xegpu
182
183} // namespace mlir
184
185#endif // MLIR_DIALECT_XEGPU_UTILS_XEGPUUTILS_H_
This class helps build Operations.
Definition Builders.h:209
This class represents a single result from folding an operation.
This class represents an operand of an operation.
Definition Value.h:254
This is a value defined by a result of an operation.
Definition Value.h:454
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
DistributeLayoutAttr inferShapeCastSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > resShape, ArrayRef< int64_t > srcShape)
Infers the source layout attribute for a shape cast operation given the result layout attribute,...
DistributeLayoutAttr inferTransposeSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > permutation)
Infers the source layout attribute for a transpose operation given the result layout attribute and pe...
SliceAttr setupMultiReductionResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, DistributeLayoutAttr consumerLayout, SmallVector< int64_t > reductionDims, const uArch::uArch *uArch)
Sets up layout for reduction operations by creating a SliceAttr for the result.
DistributeLayoutAttr inferInsertStridedSliceSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > resShape, ArrayRef< int64_t > srcShape)
Infers the source layout attribute for an insert strided slice operation given the result layout attr...
std::optional< std::tuple< DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr > > setupDpasLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy, VectorType cdTy, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch, int numSg)
Sets up the anchor layouts for a dpas operands (A, B, and C/D).
LayoutKind
Specifies the level of a layout hierarchy for comparison or propagation.
Definition XeGPU.h:32
SmallVector< NamedAttribute > dropInstDataOnAttrs(ArrayRef< NamedAttribute > attrs)
Updates the NamedAttribute sequence by dropping inst-data information from any DistributeLayoutAttr f...
DistributeLayoutAttr setupLoadMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch)
Sets up the anchor layout for load matrix operation.
bool recoverTemporaryLayouts(Operation *rootOp)
Attach layout attributes to all vector-type operands of operations within the given operation's neste...
DistributeLayoutAttr inferBroadcastSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > resShape, ArrayRef< int64_t > srcShape)
Infers the source layout attribute for a broadcast operation given the result layout attribute,...
DistributeLayoutAttr setupStoreScatterAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int chunkSize, const uArch::uArch *uArch)
Sets up the anchor layout for a store scatter operation.
void recoverTemporaryLayoutsDeprecated(Operation *op)
[to-be-deprecated] Set the DistributeLayoutAttr for each OpOperand and OpResult of of the given opera...
DistributeLayoutAttr setupBitCastResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch)
Setup the result layout attribute for a bitcast operation based on element type bitwidths.
void removeLayoutAttr(const T &operandOrResult)
Removes the LayoutAttr for a given OpOperand or OpResult if it exists.
SmallVector< NamedAttribute > dropSgLayoutAndDataOnAttrs(ArrayRef< NamedAttribute > attrs)
Updates the NamedAttribute sequence by dropping sg-layout and sg-data information from any Distribute...
LogicalResult resolveLayoutConflicts(Operation *target)
DistributeLayoutAttr inferBitCastSourceLayout(DistributeLayoutAttr resLayout, int resElemTyBitWidth, int srcElemTyBitWidth)
Infers the source layout attribute for a bitcast operation given the result layout attribute,...
DistributeLayoutAttr setupInsertStridedSliceResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch)
Sets up the result layout for an insert strided slice operation.
xegpu::DistributeLayoutAttr getConsumerLayoutAt(OpOperand &operand)
Gets the expected layout for a given consumer operand.
void removeLayoutAttrs(Operation *op)
Removes the DistributeLayoutAttr for each OpOperand and OpResult of the given operation if they exist...
DistributeLayoutAttr inferMultiReductionSourceLayout(DistributeLayoutAttr resLayout, SmallVector< int64_t > reduceDims)
Infers the source layout attribute for a reduction operation given the result layout attribute and re...
DistributeLayoutAttr setupLoadGatherAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int chunkSize, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch)
Sets up the anchor layout for a load gather operation.
LogicalResult propagateLayouts(OpBuilder &builder, Operation *target, LayoutKind layoutKind, unsigned indexBitWidth, bool printOnly=false)
DistributeLayoutAttr setupStoreMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, const uArch::uArch *uArch)
Sets up the anchor layout for a store matrix operation.
Include the generated interface declarations.