MLIR 24.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
18#include "llvm/ADT/STLFunctionalExtras.h"
19
20namespace mlir {
21class VectorType;
22class OpOperand;
23class OpResult;
24class OpBuilder;
25class ValueRange;
26class TypeConverter;
27class OpFoldResult;
28
29namespace xegpu {
30class DistributeLayoutAttr;
31class LayoutAttr;
32class TensorDescType;
33} // namespace xegpu
34
35namespace xegpu {
36LogicalResult propagateLayouts(OpBuilder &builder, Operation *target,
37 LayoutKind layoutKind, unsigned indexBitWidth,
38 bool printOnly = false);
39
40LogicalResult resolveLayoutConflicts(Operation *target);
41
42/// Replace layout conversions of cheap producer chains by recomputation.
43void rematerializeConversionSources(OpBuilder &builder, Operation *target);
44
45/// Sink `xegpu.convert_layout` ops past the elementwise operations they feed,
46/// so that an elementwise op runs in the coarser layout.
47void sinkElementwiseConversions(OpBuilder &builder, Operation *target);
48
49/// Callable returning the propagated layout for a given Value, used by the
50/// layout-propagation helpers below.
51using GetLayoutFnTy = llvm::function_ref<DistributeLayoutAttr(Value)>;
52
53/// The region helpers below choose layouts and pass each one to a callback;
54/// the caller decides when it reaches the IR. Layout recovery writes it at
55/// once; layout propagation keeps it and writes it after its walk.
56///
57/// Called with the layout chosen for a use, which ends up as the operand's
58/// temporary layout (layout_operand_N). A null layout means none was chosen.
60 llvm::function_ref<void(OpOperand &, DistributeLayoutAttr)>;
61/// Called with the layout chosen for a tensor_desc region argument (e.g. an
62/// scf.for iter_arg), which ends up in the argument's type.
64 llvm::function_ref<void(BlockArgument, DistributeLayoutAttr)>;
65
66/// Propagate layouts from a region branch op's region entry block arguments
67/// back to its init operands. The block argument's layout is obtained via
68/// `getLayoutOfValue`; the matching layout is then recorded, via
69/// `setOperandLayout`, on each init operand that flows into that block
70/// argument (e.g. scf.for's iter_args inits), and, via
71/// `setTDescArgLayout`, for tensor descriptor block arguments.
72LogicalResult
73propagateRegionArgsToInits(RegionBranchOpInterface regionOp,
74 GetLayoutFnTy getLayoutOfValue,
75 SetOperandLayoutFnTy setOperandLayout,
76 SetTDescArgLayoutFnTy setTDescArgLayout);
77
78/// Propagate layouts to the operands of a region branch terminator: each
79/// operand gets, via `getLayoutOfValue`, the layout of the region argument it
80/// feeds, or else of the region result it feeds, recorded via
81/// `setOperandLayout`.
83 RegionBranchTerminatorOpInterface terminator,
84 GetLayoutFnTy getLayoutOfValue, SetOperandLayoutFnTy setOperandLayout);
85
86/// Callbacks for walkBackward.
88public:
89 virtual ~BackwardLayoutVisitor() = default;
90 /// A terminator forwarding values between regions or to their parent op.
91 virtual void
92 visitTerminator(RegionBranchTerminatorOpInterface terminator) = 0;
93 /// A region branch op (e.g. scf.for), visited after all of its regions.
94 virtual void visitRegionOp(RegionBranchOpInterface regionOp) = 0;
95 /// Any other op.
96 virtual void visitRegularOp(Operation *op) = 0;
97 /// Called after visitRegionOp. Return true when a region argument of
98 /// `regionOp` now has a requested layout that differs from the layout the
99 /// terminator feeding it gave its operand.
100 virtual bool shouldRevisitRegions(RegionBranchOpInterface regionOp) {
101 return false;
102 }
103};
104
105/// Walk function bodies under `root` in CFG post-order, visiting ops within
106/// each block back to front and nested regions before their parent op.
107///
108/// Region successors are visited before predecessors where possible. To
109/// break cycles, prefer a region that exits to the parent op; this visits
110/// scf.while's before region before its after region. The only consumers
111/// visited after the producer of their value are those across a loop back
112/// edge (scf.for's yield -> iter_args) and, for scf.while, the uses in its
113/// after region of what scf.condition passes on. A visitor can ask for the
114/// regions of a region op to be walked again (shouldRevisitRegions) once such
115/// a consumer has been seen; this happens at most once more than the number of
116/// region arguments of the op, per visit of the op.
117void walkBackward(Operation *root, BackwardLayoutVisitor &visitor);
118
119/// Attach layout attributes to all vector-type operands of operations within
120/// the given operation's nested region. Reports an error if any vector operand
121/// lacks a layout attribute.
123
124/// Removes the LayoutAttr for a given OpOperand or OpResult if it exists.
125template <typename T,
126 typename = std::enable_if_t<std::is_same_v<T, OpOperand> ||
127 std::is_same_v<T, OpResult>>>
128void removeLayoutAttr(const T &operandOrResult);
129
130/// Removes the DistributeLayoutAttr for each OpOperand and OpResult of the
131/// given operation if they exist. If the operation contains regions, it is also
132/// applied recursively to the contained operations
134
135/// Removes the temporary layout attributes for each OpOperand and OpResult of
136/// the given operation. Recursive for contained operations if the given
137/// operation contains regions.
139
140/// Updates the NamedAttribute sequence by dropping sg-layout and
141/// sg-data information from any DistributeLayoutAttr found.
144
145/// Updates the NamedAttribute sequence by dropping inst-data information from
146/// any DistributeLayoutAttr found.
148
149/// Drops inst-data information from DistributeLayoutAttrs stored as inherent
150/// attributes on the operation.
152
153//===----------------------------------------------------------------------===//
154// Backward layout inference (result layout -> source layout)
155//===----------------------------------------------------------------------===//
156//
157// The infer*SourceLayout helpers below derive the layout of an operation's
158// source operand from the layout of its result. They are the layout rules
159// that the per-op propagation functions of the backward layout propagation
160// apply; it flows layouts from anchor ops (dpas, store_nd, ...) back to their
161// producers.
162
163/// Infers the source layout attribute for a broadcast operation given the
164/// result layout attribute, result shape, and source shape.
165DistributeLayoutAttr inferBroadcastSourceLayout(DistributeLayoutAttr resLayout,
166 ArrayRef<int64_t> resShape,
167 ArrayRef<int64_t> srcShape);
168
169/// Infers the source layout attribute for a reduction operation given the
170/// result layout attribute and reduced dims.
171DistributeLayoutAttr
172inferMultiReductionSourceLayout(DistributeLayoutAttr resLayout,
173 SmallVector<int64_t> reduceDims);
174
175/// Infers the source layout attribute for a reduction operation given the
176/// result layout attribute and reduced dims.
177DistributeLayoutAttr inferReductionSourceLayout(DistributeLayoutAttr resLayout);
178
179/// Infers the source layout attribute for a transpose operation given the
180/// result layout attribute and permutation.
181DistributeLayoutAttr inferTransposeSourceLayout(DistributeLayoutAttr resLayout,
182 ArrayRef<int64_t> permutation);
183
184/// Infers the source layout attribute for a bitcast operation given the
185/// result layout attribute, result element type bitwidth, and source element
186/// type bitwidth.
187DistributeLayoutAttr inferBitCastSourceLayout(DistributeLayoutAttr resLayout,
188 int resElemTyBitWidth,
189 int srcElemTyBitWidth);
190
191/// Infers the source layout attribute for an interleave operation given the
192/// result layout attribute. Interleave doubles the innermost dimension size.
193DistributeLayoutAttr
194inferInterleaveSourceLayout(DistributeLayoutAttr resLayout);
195
196/// Infers the source layout attribute for a deinterleave operation given the
197/// result layout attribute. Deinterleave halves the innermost dimension size.
198DistributeLayoutAttr
199inferDeinterleaveSourceLayout(DistributeLayoutAttr resLayout);
200
201/// Infers the source layout attribute for a shape cast operation given the
202/// result layout attribute, result shape, and source shape.
203DistributeLayoutAttr inferShapeCastSourceLayout(DistributeLayoutAttr resLayout,
204 ArrayRef<int64_t> resShape,
205 ArrayRef<int64_t> srcShape);
206
207/// Infers the source layout attribute for an insert strided slice operation
208/// given the result layout attribute, result shape, and source shape. Removes
209/// leading dimensions from the result layout to match the source shape size.
210DistributeLayoutAttr
211inferInsertStridedSliceSourceLayout(DistributeLayoutAttr resLayout,
212 ArrayRef<int64_t> resShape,
213 ArrayRef<int64_t> srcShape);
214
215/// Infers the source layout attribute for an insert operation.
216/// using same logic as inferInsertStridedSliceSourceLayout
217DistributeLayoutAttr inferInsertSourceLayout(DistributeLayoutAttr resLayout,
218 ArrayRef<int64_t> resShape,
219 ArrayRef<int64_t> srcShape);
220
221/// Infers the source layout attribute for an extract operation. Adds
222/// leading dimensions to the source layout to match the source shape size.
223DistributeLayoutAttr inferExtractSourceLayout(DistributeLayoutAttr resLayout,
224 ArrayRef<int64_t> resShape,
225 ArrayRef<int64_t> srcShape);
226
227/// Infers the source layout attribute for an operand using result layout
228/// attribute
229DistributeLayoutAttr
231 DistributeLayoutAttr resLayout);
232
233//===----------------------------------------------------------------------===//
234// Forward layout inference (source layout -> result layout)
235//===----------------------------------------------------------------------===//
236//
237// The infer*ResultLayout helpers below are the forward counterparts of the
238// infer*SourceLayout helpers above: given the layout of an operation's source
239// operand they derive the layout of its result. They are used by the local
240// forward-fill step in XeGPUPropagateLayout that assigns layouts to values not
241// reached by the backward propagation analysis (e.g. loop-carried values whose
242// only consumer is the next iteration).
243
244/// Infers the result layout attribute for a transpose operation given the
245/// source layout attribute and permutation. Inverse of
246/// inferTransposeSourceLayout.
247DistributeLayoutAttr inferTransposeResultLayout(DistributeLayoutAttr srcLayout,
248 ArrayRef<int64_t> permutation);
249
250/// Infers the result layout attribute for a shape cast operation given the
251/// source layout attribute, source shape, and result shape. Inverse of
252/// inferShapeCastSourceLayout. Returns nullptr for shape-cast patterns whose
253/// forward direction is ambiguous (e.g. unit-dim expansion).
254DistributeLayoutAttr inferShapeCastResultLayout(DistributeLayoutAttr srcLayout,
255 ArrayRef<int64_t> srcShape,
256 ArrayRef<int64_t> resShape);
257
258/// Infers the result layout attribute for a non-anchor operation from the
259/// layouts of its source operands (the forward counterpart of
260/// inferSourceLayoutFromResultForNonAnchorOp). `operandLayouts` is indexed by
261/// operand number; entries may be null for operands without a known layout.
262/// Returns nullptr when no forward rule applies (the result is then left
263/// un-laid-out).
265 Operation *op, ArrayRef<DistributeLayoutAttr> operandLayouts);
266
267/// Note on the `consumerLayout` argument used by the consumer-driven setup* /
268/// complete* helpers below:
269///
270/// Layout propagation visits a producer after its consumers, in one backward
271/// scan. The `consumerLayout` passed to these helpers is the layout requested
272/// by the nearest consumer, or null if none requests one. They do not pick
273/// among or merge several consumers' layouts; an inconsistent consumer is
274/// reconciled afterwards by `ResolveLayoutConflicts`, which inserts a
275/// `convert_layout` on that edge.
276
277/// Sets up layout for Multi-Reduction operations by creating a SliceAttr for
278/// the result.
279///
280/// This function first attempts to construct a source layout that, when
281/// sliced along reduction dimensions, produces a result layout compatible
282/// with the consumer's preferred layout. This minimizes data redistribution
283/// overhead. The SliceAttr for the result is then created based on the
284/// derived source layout and the specified reduction dimensions.
286 VectorType srcVectorTy,
287 DistributeLayoutAttr consumerLayout,
288 SmallVector<int64_t> reductionDims,
289 int numSg, const uArch::uArch *uArch);
290
291/// Sets up layout for Reduction operations by creating a SliceAttr for the
292/// result.
293SliceAttr setupReductionResultLayout(LayoutKind layoutKind,
294 VectorType srcVectorTy,
295 const uArch::uArch *uArch);
296
297/// Setup the result layout attribute for a bitcast operation based on element
298/// type bitwidths. This ensures the source layout can always be derived from
299/// the result layout.
300///
301/// When casting from a narrower to a wider element type (srcElemTyBitWidth <
302/// resElemTyBitWidth), the result layout's innermost dimension data sizes
303/// (inst_data, lane_data) are scaled up by the bitwidth ratio. This maintains
304/// the invariant that the source layout can be recovered by adjusting the
305/// result layout based on bitwidth ratio of input vs output.
306DistributeLayoutAttr setupBitCastResultLayout(
307 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
308 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
309
310/// Sets up the result layout for an interleave operation to ensure the source
311/// layout can be safely derived. Interleave doubles the innermost dimension,
312/// so the result layout must ensure that laneData is at least 2 (or a multiple
313/// of 2), and instData must be divisible by innermostDimLaneLayout * 2.
314DistributeLayoutAttr setupInterleaveResultLayout(
315 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
316 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
317
318/// Sets up the result layout for a shape cast that splits one source dim into
319/// several consecutive result dims, so that the source layout can be safely
320/// derived by collapsing each split group.
321///
322/// Within a group, the leading dims the lanes do not split and the first dim
323/// they do split may stay partial. Every dim after that is stretched to full:
324/// lane_data becomes dim_size / lane_layout, and inst_data the dim size.
325///
326/// Example:
327/// shape_cast: vector<16x1024xf32> -> vector<16x32x32xf32>
328/// Consumer layout: inst_data = [1, 2, 8], lane_layout = [1, 2, 8],
329/// lane_data = [1, 1, 1]
330/// Adjusted: inst_data = [1, 2, 32], lane_layout = [1, 2, 8],
331/// lane_data = [1, 1, 4]
332/// The adjusted layout collapses to inst_data = [1, 64],
333/// lane_layout = [1, 16], lane_data = [1, 4] on the source, whereas the
334/// unadjusted one would have collapsed to a strided inst_data = [1, 16].
335///
336/// Stretching does not guarantee the result layout is collapsible: collapsing
337/// also requires each lane to own one contiguous run of the source dim, which
338/// the consumer's lane_layout can rule out. This function checks that
339/// restriction on the stretched layout and returns nullptr when it does not
340/// hold, rejecting the consumer layout.
341///
342/// Example of a rejected layout:
343/// shape_cast: vector<16x128xf32> -> vector<16x2x4x16xf32>
344/// Consumer layout: inst_data = [1, 2, 2, 4], lane_layout = [1, 2, 2, 4],
345/// lane_data = [1, 1, 1, 1]
346/// Adjusted: inst_data = [1, 2, 4, 16], lane_layout = [1, 2, 2, 4],
347/// lane_data = [1, 1, 2, 4]
348/// Each lane owns two runs of 4 elements, 16 apart, instead of the 8
349/// contiguous source elements the collapsed lane_data = [1, 8] claims.
350///
351/// At subgroup level, if a group's sg tile is not contiguous in the collapsed
352/// source dim, the group's subgroups are moved to its outermost dims; the
353/// consumer then receives its layout through a convert_layout.
354///
355/// Example:
356/// shape_cast: vector<32x1024xbf16> -> vector<32x32x32xbf16>
357/// Consumer layout: sg_layout = [2, 1, 2], sg_data = [16, 32, 16]
358/// Adjusted: sg_layout = [2, 2, 1], sg_data = [16, 16, 32]
359/// The consumer's sg tile 32x16 is not contiguous in the collapsed dim 1024,
360/// so it has no source layout. The adjusted sg tile 16x32 is, and collapses
361/// to sg_layout = [2, 2], sg_data = [16, 512].
362DistributeLayoutAttr
363setupShapeCastResultLayout(LayoutKind layoutKind, VectorType srcVectorTy,
364 VectorType resVectorTy,
365 DistributeLayoutAttr consumerLayout);
366
367/// Sets up the result layout for an insert strided slice operation.
368/// Creates a result layout based on the specified layout kind (InstData or
369/// Lane).
371 LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy,
372 DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch);
373
374/// Sets up the anchor layout for a load gather operation.
375/// `contigChunkSize` is the per-lane contiguous run the offsets allow, from
376/// the op's `contiguity` attribute (1 when it is absent).
377/// Use a default scatter IO layout when `consumerLayout` is absent.
378/// `numSg` is needed only for that default in Subgroup mode.
379DistributeLayoutAttr setupLoadGatherAnchorLayout(
380 LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize,
381 DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch);
382
383/// Sets up the anchor layout for load matrix operation.
384/// Use a default scatter IO layout when `consumerLayout` is absent.
385/// `numSg` is needed only for that default in Subgroup mode.
386DistributeLayoutAttr setupLoadMatrixAnchorLayout(
387 LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize,
388 DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch);
389
390/// Sets up the anchor layout for a store scatter operation.
391/// `contigChunkSize` is the per-lane contiguous run the offsets allow, from
392/// the op's `contiguity` attribute (1 when it is absent).
393/// `numSg` is only used for Subgroup-kind layouts.
394DistributeLayoutAttr setupStoreScatterAnchorLayout(LayoutKind layoutKind,
395 VectorType vectorTy,
396 int contigChunkSize,
397 int numSg,
398 const uArch::uArch *uArch);
399
400/// Sets up the anchor layout for a store matrix operation.
401/// `numSg` is only used for Subgroup-kind layouts.
402DistributeLayoutAttr setupStoreMatrixAnchorLayout(LayoutKind layoutKind,
403 VectorType vectorTy,
404 int contigChunkSize,
405 int numSg,
406 const uArch::uArch *uArch);
407
408/// If the consumer layout has only inst_data (no lane_layout/lane_data),
409/// completes it by running the corresponding scatter-style Lane-kind setup
410/// rule with inst_data as the destination shape. The resulting lane info is
411/// merged with the consumer's inst_data so downstream setup* paths see a
412/// fully-populated layout.
413/// Returns the layout unchanged when it is null, has no inst_data, or already
414/// carries lane info; returns nullopt when the derived lane factorization does
415/// not divide the user's inst_data (an invalid inst_data).
416std::optional<DistributeLayoutAttr> completeScatterLoadLaneLayoutFromInstData(
417 DistributeLayoutAttr userSpecifiedLayout,
418 DistributeLayoutAttr consumerLayout, Type elemTy,
419 const xegpu::uArch::LoadGatherInstruction *uArchInstruction,
420 const int subgroupSize);
421
422/// Like completeScatterLoadLaneLayoutFromInstData, but for scatter stores
423/// (store_scatter / store_matrix). A store is a data sink: lane info is derived
424/// purely from inst_data using the uArch's StoreScatter per-lane store width,
425/// with no consumer layout to reuse.
426std::optional<DistributeLayoutAttr> completeScatterStoreLaneLayoutFromInstData(
427 DistributeLayoutAttr specifiedLayout, Type elemTy,
428 const xegpu::uArch::StoreScatterInstruction *uArchInstruction,
429 const int subgroupSize);
430
431/// Completes a user-provided 2D-block store_nd / prefetch_nd anchor that has
432/// only inst_data. These ops are data sinks, so lane info is derived purely
433/// from inst_data using the shared BlockIOInstructionInterface; one helper
434/// serves both store_nd and prefetch_nd.
435std::optional<DistributeLayoutAttr> completeBlockStoreLaneLayoutFromInstData(
436 DistributeLayoutAttr specifiedLayout, Type elemTy,
437 const xegpu::uArch::BlockIOInstructionInterface *uArchInstruction,
438 const int subgroupSize);
439
440/// Like completeBlockStoreLaneLayoutFromInstData, but for load_nd. The consumer
441/// layout supplies the transform / transpose / packing properties; the lane
442/// factorization is recomputed from inst_data (load-side lane counts differ
443/// from the consumer's).
444std::optional<DistributeLayoutAttr> completeBlockLoadLaneLayoutFromInstData(
445 DistributeLayoutAttr specifiedLayout, DistributeLayoutAttr consumerLayout,
446 Type elemTy,
447 const xegpu::uArch::BlockIOInstructionInterface *uArchInstruction,
448 const int subgroupSize);
449
450/// Sets up the anchor layout for a store_nd operation. StoreNd does not
451/// consider a consumer layout (it is a data sink), and picks its layout from
452/// uArch block parameters. `numSg` is only used for Subgroup-kind layouts.
453DistributeLayoutAttr setupStoreNdAnchorLayout(LayoutKind layoutKind,
454 VectorType vectorTy, int numSg,
455 const uArch::uArch *uArch);
456
457/// Sets up the anchor layout for a prefetch_nd operation. PrefetchNd has no
458/// value result and thus no consumer; it picks its layout from uArch block
459/// parameters. `numSg` is only used for Subgroup-kind layouts.
460DistributeLayoutAttr setupPrefetchNdAnchorLayout(LayoutKind layoutKind,
461 TensorDescType tdescTy,
462 int numSg,
463 const uArch::uArch *uArch);
464
465/// Sets up the anchor layout for a load_nd operation. LoadNd takes a
466/// (downstream) consumer layout and validates it against uArch constraints;
467/// when valid, the consumer's `inst_data` / `sg_layout` are honored.
468/// Otherwise defaults derived from uArch block parameters are used.
469/// Use the block load's default when `consumerLayout` is absent.
470/// `numSg` is needed only for that default in Subgroup mode.
471DistributeLayoutAttr
472setupLoadNdAnchorLayout(LayoutKind layoutKind, VectorType vectorTy,
473 DistributeLayoutAttr consumerLayout, int numSg,
474 const uArch::uArch *uArch);
475
476/// Sets up the anchor layouts for a dpas operands (A, B, and C/D).
477/// The numSg and consumerLayout (optional) are only used by sg layout creation.
478std::optional<std::tuple<DistributeLayoutAttr, DistributeLayoutAttr,
479 DistributeLayoutAttr>>
480setupDpasLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy,
481 VectorType cdTy, DistributeLayoutAttr consumerLayout, int numSg,
482 const uArch::uArch *uArch);
483
484/// Sets up the anchor layouts for dpas_mx operands (A, B, C/D, A_scale, and
485/// B_scale). The numSg and consumerLayout (optional) are only used by sg layout
486/// creation. A_scale and B_scale are optional.
487std::optional<
488 std::tuple<DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr,
489 DistributeLayoutAttr, DistributeLayoutAttr>>
490setupDpasMxLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy,
491 VectorType cdTy, VectorType aScaleTy, VectorType bScaleTy,
492 DistributeLayoutAttr consumerLayout, int numSg,
493 const uArch::uArch *uArch);
494
495/// Completes user-provided DPAS A/B/C-D anchors that carry only inst_data by
496/// filling in lane_layout / lane_data derived from the operand shapes (mirrors
497/// the InstData branch of setupDpasLayout). Returns nullopt if the uArch lacks
498/// the matmul instruction.
499std::optional<std::tuple<DistributeLayoutAttr, DistributeLayoutAttr,
500 DistributeLayoutAttr>>
501completeDpasLaneLayoutFromInstData(DistributeLayoutAttr aLayout,
502 DistributeLayoutAttr bLayout,
503 DistributeLayoutAttr cdLayout,
504 VectorType aTy, VectorType bTy,
505 VectorType cdTy, const uArch::uArch *uArch);
506
507/// Like completeDpasLaneLayoutFromInstData, but for dpas_mx: additionally
508/// re-derives the A_scale / B_scale layouts from the completed A / B layouts.
509std::optional<
510 std::tuple<DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr,
511 DistributeLayoutAttr, DistributeLayoutAttr>>
512completeDpasMxLaneLayoutFromInstData(DistributeLayoutAttr aLayout,
513 DistributeLayoutAttr bLayout,
514 DistributeLayoutAttr cdLayout,
515 VectorType aTy, VectorType bTy,
516 VectorType cdTy, VectorType aScaleTy,
517 VectorType bScaleTy,
518 const uArch::uArch *uArch);
519
520/// Gets the expected layout for a given consumer operand. This will check if
521/// the owning operation of the consumer operand is one of the special layout
522/// users and determine the expected layout accordingly.
523DistributeLayoutAttr getConsumerLayoutAt(OpOperand &operand);
524
525/// Returns true if the vector produced by `op` is cheap to recompute in any
526/// layout, i.e. `rematerialize` would succeed: a bounded chain of pure vector
527/// generators (`vector.step`, splat `arith.constant`, `vector.create_mask`,
528/// scalar `vector.broadcast`) and layout-preserving elementwise ops.
530
531/// Clone a bounded chain of pure vector generators and layout-preserving
532/// elementwise ops without layout attributes. Each producer is cloned once,
533/// immediately after its original; scalar operands remain shared. Return null
534/// if the chain is ineligible, without modifying the IR.
535Value rematerialize(OpBuilder &builder, Value value);
536} // namespace xegpu
537} // namespace mlir
538
539#endif // MLIR_DIALECT_XEGPU_UTILS_XEGPUUTILS_H_
This class represents an argument of a Block.
Definition Value.h:308
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
This class represents an operand of an operation.
Definition Value.h:256
This is a value defined by a result of an operation.
Definition Value.h:456
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Callbacks for walkBackward.
virtual bool shouldRevisitRegions(RegionBranchOpInterface regionOp)
Called after visitRegionOp.
virtual void visitTerminator(RegionBranchTerminatorOpInterface terminator)=0
A terminator forwarding values between regions or to their parent op.
virtual void visitRegionOp(RegionBranchOpInterface regionOp)=0
A region branch op (e.g. scf.for), visited after all of its regions.
virtual void visitRegularOp(Operation *op)=0
Any other op.
virtual ~BackwardLayoutVisitor()=default
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,...
void sinkElementwiseConversions(OpBuilder &builder, Operation *target)
Sink xegpu.convert_layout ops past the elementwise operations they feed, so that an elementwise op ru...
DistributeLayoutAttr setupLoadNdAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a load_nd operation.
DistributeLayoutAttr inferResultLayoutFromSourceForNonAnchorOp(Operation *op, ArrayRef< DistributeLayoutAttr > operandLayouts)
Infers the result layout attribute for a non-anchor operation from the layouts of its source operands...
DistributeLayoutAttr setupInterleaveResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy, DistributeLayoutAttr consumerLayout, const uArch::uArch *uArch)
Sets up the result layout for an interleave operation to ensure the source layout can be safely deriv...
DistributeLayoutAttr inferTransposeSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > permutation)
Infers the source layout attribute for a transpose operation given the result layout attribute and pe...
DistributeLayoutAttr inferInsertSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > resShape, ArrayRef< int64_t > srcShape)
Infers the source layout attribute for an insert operation.
llvm::function_ref< void(BlockArgument, DistributeLayoutAttr)> SetTDescArgLayoutFnTy
Called with the layout chosen for a tensor_desc region argument (e.g.
std::optional< std::tuple< DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr > > completeDpasMxLaneLayoutFromInstData(DistributeLayoutAttr aLayout, DistributeLayoutAttr bLayout, DistributeLayoutAttr cdLayout, VectorType aTy, VectorType bTy, VectorType cdTy, VectorType aScaleTy, VectorType bScaleTy, const uArch::uArch *uArch)
Like completeDpasLaneLayoutFromInstData, but for dpas_mx: additionally re-derives the A_scale / B_sca...
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...
DistributeLayoutAttr setupStoreMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a store matrix operation.
void removeTemporaryLayoutAttrs(Operation *op)
Removes the temporary layout attributes for each OpOperand and OpResult of the given operation.
std::optional< std::tuple< DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr > > completeDpasLaneLayoutFromInstData(DistributeLayoutAttr aLayout, DistributeLayoutAttr bLayout, DistributeLayoutAttr cdLayout, VectorType aTy, VectorType bTy, VectorType cdTy, const uArch::uArch *uArch)
Completes user-provided DPAS A/B/C-D anchors that carry only inst_data by filling in lane_layout / la...
LogicalResult propagateRegionArgsToInits(RegionBranchOpInterface regionOp, GetLayoutFnTy getLayoutOfValue, SetOperandLayoutFnTy setOperandLayout, SetTDescArgLayoutFnTy setTDescArgLayout)
Propagate layouts from a region branch op's region entry block arguments back to its init operands.
LayoutKind
Specifies the level of a layout hierarchy for comparison or propagation.
Definition XeGPU.h:31
SmallVector< NamedAttribute > dropInstDataOnAttrs(ArrayRef< NamedAttribute > attrs)
Updates the NamedAttribute sequence by dropping inst-data information from any DistributeLayoutAttr f...
DistributeLayoutAttr inferSourceLayoutFromResultForNonAnchorOp(OpOperand &operand, DistributeLayoutAttr resLayout)
Infers the source layout attribute for an operand using result layout attribute.
DistributeLayoutAttr inferInterleaveSourceLayout(DistributeLayoutAttr resLayout)
Infers the source layout attribute for an interleave operation given the result layout attribute.
DistributeLayoutAttr setupLoadGatherAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize, DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a load gather 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,...
std::optional< std::tuple< DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr > > setupDpasMxLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy, VectorType cdTy, VectorType aScaleTy, VectorType bScaleTy, DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch)
Sets up the anchor layouts for dpas_mx operands (A, B, C/D, A_scale, and B_scale).
void walkBackward(Operation *root, BackwardLayoutVisitor &visitor)
Walk function bodies under root in CFG post-order, visiting ops within each block back to front and n...
SliceAttr setupMultiReductionResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, DistributeLayoutAttr consumerLayout, SmallVector< int64_t > reductionDims, int numSg, const uArch::uArch *uArch)
Note on the consumerLayout argument used by the consumer-driven setup* / complete* helpers below:
llvm::function_ref< void(OpOperand &, DistributeLayoutAttr)> SetOperandLayoutFnTy
The region helpers below choose layouts and pass each one to a callback; the caller decides when it r...
llvm::function_ref< DistributeLayoutAttr(Value)> GetLayoutFnTy
Callable returning the propagated layout for a given Value, used by the layout-propagation helpers be...
std::optional< DistributeLayoutAttr > completeScatterLoadLaneLayoutFromInstData(DistributeLayoutAttr userSpecifiedLayout, DistributeLayoutAttr consumerLayout, Type elemTy, const xegpu::uArch::LoadGatherInstruction *uArchInstruction, const int subgroupSize)
If the consumer layout has only inst_data (no lane_layout/lane_data), completes it by running the cor...
DistributeLayoutAttr setupLoadMatrixAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize, DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for load matrix operation.
DistributeLayoutAttr setupStoreScatterAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int contigChunkSize, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a store scatter operation.
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 propagateRegionResultsToYieldOperands(RegionBranchTerminatorOpInterface terminator, GetLayoutFnTy getLayoutOfValue, SetOperandLayoutFnTy setOperandLayout)
Propagate layouts to the operands of a region branch terminator: each operand gets,...
void removeLayoutAttr(const T &operandOrResult)
Removes the LayoutAttr for a given OpOperand or OpResult if it exists.
void dropInstDataOnInherentAttrs(Operation *op)
Drops inst-data information from DistributeLayoutAttrs stored as inherent attributes on the operation...
SmallVector< NamedAttribute > dropSgLayoutAndDataOnAttrs(ArrayRef< NamedAttribute > attrs)
Updates the NamedAttribute sequence by dropping sg-layout and sg-data information from any Distribute...
DistributeLayoutAttr setupPrefetchNdAnchorLayout(LayoutKind layoutKind, TensorDescType tdescTy, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a prefetch_nd operation.
DistributeLayoutAttr inferShapeCastResultLayout(DistributeLayoutAttr srcLayout, ArrayRef< int64_t > srcShape, ArrayRef< int64_t > resShape)
Infers the result layout attribute for a shape cast operation given the source layout attribute,...
DistributeLayoutAttr inferExtractSourceLayout(DistributeLayoutAttr resLayout, ArrayRef< int64_t > resShape, ArrayRef< int64_t > srcShape)
Infers the source layout attribute for an extract operation.
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.
DistributeLayoutAttr setupShapeCastResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, VectorType resVectorTy, DistributeLayoutAttr consumerLayout)
Sets up the result layout for a shape cast that splits one source dim into several consecutive result...
DistributeLayoutAttr inferReductionSourceLayout(DistributeLayoutAttr resLayout)
Infers the source layout attribute for a reduction operation given the result layout attribute and re...
std::optional< DistributeLayoutAttr > completeScatterStoreLaneLayoutFromInstData(DistributeLayoutAttr specifiedLayout, Type elemTy, const xegpu::uArch::StoreScatterInstruction *uArchInstruction, const int subgroupSize)
Like completeScatterLoadLaneLayoutFromInstData, but for scatter stores (store_scatter / store_matrix)...
std::optional< DistributeLayoutAttr > completeBlockStoreLaneLayoutFromInstData(DistributeLayoutAttr specifiedLayout, Type elemTy, const xegpu::uArch::BlockIOInstructionInterface *uArchInstruction, const int subgroupSize)
Completes a user-provided 2D-block store_nd / prefetch_nd anchor that has only inst_data.
DistributeLayoutAttr inferDeinterleaveSourceLayout(DistributeLayoutAttr resLayout)
Infers the source layout attribute for a deinterleave operation given the result layout attribute.
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...
bool isTriviallyRematerializable(Operation *op)
Returns true if the vector produced by op is cheap to recompute in any layout, i.e.
LogicalResult propagateLayouts(OpBuilder &builder, Operation *target, LayoutKind layoutKind, unsigned indexBitWidth, bool printOnly=false)
Value rematerialize(OpBuilder &builder, Value value)
Clone a bounded chain of pure vector generators and layout-preserving elementwise ops without layout ...
DistributeLayoutAttr setupStoreNdAnchorLayout(LayoutKind layoutKind, VectorType vectorTy, int numSg, const uArch::uArch *uArch)
Sets up the anchor layout for a store_nd operation.
DistributeLayoutAttr inferTransposeResultLayout(DistributeLayoutAttr srcLayout, ArrayRef< int64_t > permutation)
Infers the result layout attribute for a transpose operation given the source layout attribute and pe...
std::optional< DistributeLayoutAttr > completeBlockLoadLaneLayoutFromInstData(DistributeLayoutAttr specifiedLayout, DistributeLayoutAttr consumerLayout, Type elemTy, const xegpu::uArch::BlockIOInstructionInterface *uArchInstruction, const int subgroupSize)
Like completeBlockStoreLaneLayoutFromInstData, but for load_nd.
std::optional< std::tuple< DistributeLayoutAttr, DistributeLayoutAttr, DistributeLayoutAttr > > setupDpasLayout(LayoutKind layoutKind, VectorType aTy, VectorType bTy, VectorType cdTy, DistributeLayoutAttr consumerLayout, int numSg, const uArch::uArch *uArch)
Sets up the anchor layouts for a dpas operands (A, B, and C/D).
void rematerializeConversionSources(OpBuilder &builder, Operation *target)
Replace layout conversions of cheap producer chains by recomputation.
SliceAttr setupReductionResultLayout(LayoutKind layoutKind, VectorType srcVectorTy, const uArch::uArch *uArch)
Sets up layout for Reduction operations by creating a SliceAttr for the result.
Include the generated interface declarations.