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
37
38LogicalResult propagateLayouts(OpBuilder &builder, Operation *target,
39 LayoutKind layoutKind, bool printOnly = false);
40
42
43/// [to-be-deprecated] Set the DistributeLayoutAttr for each OpOperand and
44/// OpResult of of the given operation. If the operation contains regions, it is
45/// also applied recursively to the contained operations operation.
46/// TODO: To be replaced by recoverTemporaryLayouts()
48
49/// Attach layout attributes to all vector-type operands of operations within
50/// the given operation's nested region. Reports an error if any vector operand
51/// lacks a layout attribute.
53
54/// Removes the LayoutAttr for a given OpOperand or OpResult if it exists.
55template <typename T,
56 typename = std::enable_if_t<std::is_same_v<T, OpOperand> ||
57 std::is_same_v<T, OpResult>>>
58void removeLayoutAttr(const T &operandOrResult);
59
60/// Removes the DistributeLayoutAttr for each OpOperand and OpResult of the
61/// given operation if they exist. If the operation contains regions, it is also
62/// applied recursively to the contained operations
64
65/// Updates the NamedAttribute sequence by dropping sg-layout and
66/// sg-data information from any DistributeLayoutAttr found.
69
70/// Updates the NamedAttribute sequence by dropping inst-data information from
71/// any DistributeLayoutAttr found.
73
74/// Infers the source layout attribute for a broadcast operation given the
75/// result layout attribute, result shape, and source shape.
76DistributeLayoutAttr inferBroadcastSourceLayout(DistributeLayoutAttr resLayout,
77 ArrayRef<int64_t> resShape,
78 ArrayRef<int64_t> srcShape);
79
80/// Infers the source layout attribute for a reduction operation given the
81/// result layout attribute and reduced dims.
82DistributeLayoutAttr
83inferMultiReductionSourceLayout(DistributeLayoutAttr resLayout,
84 SmallVector<int64_t> reduceDims);
85
86/// Infers the source layout attribute for a bitcast operation given the
87/// result layout attribute, result element type bitwidth, and source element
88/// type bitwidth.
89DistributeLayoutAttr inferBitCastSourceLayout(DistributeLayoutAttr resLayout,
90 int resElemTyBitWidth,
91 int srcElemTyBitWidth);
92
93/// Infers the source layout attribute for a shape cast operation given the
94/// result layout attribute, result shape, and source shape.
95DistributeLayoutAttr inferShapeCastSourceLayout(DistributeLayoutAttr resLayout,
96 ArrayRef<int64_t> resShape,
97 ArrayRef<int64_t> srcShape);
98
99/// Infers the source layout attribute for an insert strided slice operation
100/// given the result layout attribute, result shape, and source shape. Removes
101/// leading dimensions from the result layout to match the source shape size.
102DistributeLayoutAttr
103inferInsertStridedSliceSourceLayout(DistributeLayoutAttr resLayout,
104 ArrayRef<int64_t> resShape,
105 ArrayRef<int64_t> srcShape);
106
107/// Sets up layout for reduction operations by creating a SliceAttr for the
108/// result.
109///
110/// This function first attempts to construct a source layout that, when
111/// sliced along reduction dimensions, produces a result layout compatible
112/// with the consumer's preferred layout. This minimizes data redistribution
113/// overhead. The SliceAttr for the result is then created based on the
114/// derived source layout and the specified reduction dimensions.
116 VectorType srcVectorTy,
117 DistributeLayoutAttr consumerLayout,
118 SmallVector<int64_t> reductionDims,
119 const uArch::uArch *uArch);
120
121/// Setup the result layout attribute for a bitcast operation based on element
122/// type bitwidths. This ensures the source layout can always be derived from
123/// the result layout.
124///
125/// When casting from a narrower to a wider element type (srcElemTyBitWidth <
126/// resElemTyBitWidth), the result layout's innermost dimension data sizes
127/// (inst_data, lane_data) are scaled up by the bitwidth ratio. This maintains
128/// the invariant that the source layout can be recovered by adjusting the
129/// result layout based on bitwidth ratio of input vs output.
130DistributeLayoutAttr setupBitCastResultLayout(
131 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
132 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
133
134/// Sets up the result layout for an insert strided slice operation.
135/// Creates a result layout based on the specified layout kind (InstData or
136/// Lane).
138 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
139 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
140
141/// Sets up the anchor layout for a load gather operation.
142DistributeLayoutAttr
143setupLoadGatherAnchorLayout(LayoutKind layoutKind, VectorType vectorTy,
144 int chunkSize, DistributeLayoutAttr consumerLayout,
145 const uArch::uArch *uArch);
146
147/// Sets up the anchor layout for load matrix operation.
148DistributeLayoutAttr
149setupLoadMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy,
150 DistributeLayoutAttr consumerLayout,
151 const uArch::uArch *uArch);
152
153/// Sets up the anchor layout for a store scatter operation.
154DistributeLayoutAttr setupStoreScatterAnchorLayout(LayoutKind layoutKind,
155 VectorType vectorTy,
156 int chunkSize,
157 const uArch::uArch *uArch);
158
159/// Sets up the anchor layout for a store matrix operation.
160DistributeLayoutAttr setupStoreMatrixAnchorLayout(LayoutKind layoutKind,
161 VectorType vectorTy,
162 const uArch::uArch *uArch);
163
164/// Sets up the anchor layouts for a dpas operands (A, B, and C/D).
165/// The numSg and consumerLayout (optional) are only used by sg layout creation.
166std::optional<std::tuple<DistributeLayoutAttr, DistributeLayoutAttr,
167 DistributeLayoutAttr>>
168setupDpasLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy,
169 VectorType cdTy, DistributeLayoutAttr consumerLayout,
170 const uArch::uArch *uArch, int numSg);
171
172} // namespace xegpu
173
174} // namespace mlir
175
176#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:257
This is a value defined by a result of an operation.
Definition Value.h:457
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
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,...
LogicalResult propagateLayouts(OpBuilder &builder, Operation *target, LayoutKind layoutKind, bool printOnly=false)
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).
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.
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.
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.