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