MLIR 23.0.0git
Transforms.h
Go to the documentation of this file.
1//===- Transforms.h - XeGPU Dialect transformations -------------*- 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_TRANSFORMS_TRANSFORMS_H
10#define MLIR_DIALECT_XEGPU_TRANSFORMS_TRANSFORMS_H
11
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/Operation.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/Support/LogicalResult.h"
17
18#include <functional>
19#include <optional>
20#include <utility>
21
22namespace mlir {
24
25namespace xegpu {
26
27/// Options to control the XeGPU unrolling. Its main purpose is to
28/// provide a way to customize the native shape of the operation.
30 /// Callback function that indicates whether vector unrolling should be
31 /// attempted on the operation.
32 using FilterConstraintFnType = std::function<LogicalResult(Operation *op)>;
35 filterConstraint = std::move(constraint);
36 return *this;
37 }
38
39 /// Function that computes the target shape for unrolling. It returns an
40 /// optional vector of integers representing the shape. If it returns
41 /// `std::nullopt`, unrolling is aborted for the given operation.
43 std::function<std::optional<SmallVector<int64_t>>(Operation *op)>;
46 nativeShape = std::move(fn);
47 return *this;
48 }
49
50 /// Function that converts a ShapedType (TensorDescType or VectorType)
51 /// into the unrolled type based on the tileShape. It returns a vector of
52 /// types representing the unrolled types for simplicity.
53 using UnrolledTypeFnType = std::function<SmallVector<Type>(
54 ShapedType type, ArrayRef<int64_t> tileShape)>;
57 getUnrolledTypes = std::move(fn);
58 return *this;
59 }
60};
61
62/// Appends patterns for optimizing block load operations into `patterns`.
64/// Appends patterns for array length optimization into `patterns`.
66/// Appends patterns for XeGPU SIMT distribution into `patterns`.
68/// Appends patterns for moving function body into gpu.warp_execute_on_lane0 op.
70/// Define the type conversions needed for XeGPU workgroup to subgroup
71/// distribution. This includes a context-aware 1:N conversion for VectorType
72/// (using the distribute layout attribute on the Value) and a 1:N conversion
73/// for TensorDescType.
75 Operation *topLevelOp);
76/// Appends patterns for XeGPU workgroup to subgroup distribution into
77/// `patterns`.
79/// Define only the type conversions needed for XeGPU subgroup to lane
80/// distribution.
82 TypeConverter &typeConverter, Operation *topLevelOp);
83/// Defines type conversions and legality for XeGPU subgroup to lane
84/// distribution and appends the required conversion patterns into `patterns`.
85/// Appends patterns for XeGPU subgroup to lane distribution into
86/// `patterns`.
88 TypeConverter &typeConverter, RewritePatternSet &patterns,
89 ConversionTarget &target, Operation *topLevelOp);
90
91/// Collect a set of patterns to unroll xegpu operations to a smaller shapes.
92/// Users can control whether an operation to be unrolled or not, as well as
93/// its target shape via `options` structure. (via setting filterConstraint
94/// and nativeShape respectively, both of them are function refs taking `op` as
95/// input).
96/// An `op` is unrolled to the `targetShape` as follows, for each of its
97/// operands:
98/// 1. the unrolled type `unrolledType` and number of unrolled instances
99/// `numUnrolledInstances` are computed from the `targetShape`.
100/// 2. pack each operand. ExtractStridedSlice are created to break-up the
101/// vector operands. And BuiltinUnrealizedCastOp are created to break-up
102/// the TensorDesc operands.
103/// 3. the original op is cloned `numUnrolledInstances` times, once for each
104/// result.
105/// 4. unpack the results. InsertStridedSlice are inserted for VectorType
106/// result, and BuiltinUnrealizedCastOp are inserted for TensorDescType result
107/// to re-assemble the slices into the original shape.
109 const UnrollOptions &options);
110
111} // namespace xegpu
112} // namespace mlir
113
114#endif // MLIR_DIALECT_XEGPU_TRANSFORMS_TRANSFORMS_H
static llvm::ManagedStatic< PassManagerOptions > options
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
void populateXeGPUArrayLengthOptimizationPatterns(RewritePatternSet &patterns)
Appends patterns for array length optimization into patterns.
void populateXeGPUSgToLaneDistributeTypeConversionAndLegality(TypeConverter &typeConverter, RewritePatternSet &patterns, ConversionTarget &target, Operation *topLevelOp)
Defines type conversions and legality for XeGPU subgroup to lane distribution and appends the require...
void populateXeGPUUnrollPatterns(RewritePatternSet &patterns, const UnrollOptions &options)
Collect a set of patterns to unroll xegpu operations to a smaller shapes.
void populateXeGPUWgToSgDistributeTypeConversions(TypeConverter &converter, Operation *topLevelOp)
Define the type conversions needed for XeGPU workgroup to subgroup distribution.
void populateXeGPUMoveFuncBodyToWarpOpPatterns(RewritePatternSet &patterns)
Appends patterns for moving function body into gpu.warp_execute_on_lane0 op.
void populateXeGPUWgToSgDistributePatterns(RewritePatternSet &patterns)
Appends patterns for XeGPU workgroup to subgroup distribution into patterns.
void populateXeGPUSgToLaneDistributeTypeConversions(TypeConverter &typeConverter, Operation *topLevelOp)
Define only the type conversions needed for XeGPU subgroup to lane distribution.
void populateXeGPUPeepHoleOptimizerPatterns(RewritePatternSet &patterns)
Appends patterns for optimizing block load operations into patterns.
void populateXeGPUSubgroupDistributePatterns(RewritePatternSet &patterns)
Appends patterns for XeGPU SIMT distribution into patterns.
Include the generated interface declarations.
Options to control the XeGPU unrolling.
Definition Transforms.h:29
std::function< std::optional< SmallVector< int64_t > >(Operation *op)> NativeShapeFnType
Function that computes the target shape for unrolling.
Definition Transforms.h:42
std::function< SmallVector< Type >( ShapedType type, ArrayRef< int64_t > tileShape)> UnrolledTypeFnType
Function that converts a ShapedType (TensorDescType or VectorType) into the unrolled type based on th...
Definition Transforms.h:53
NativeShapeFnType nativeShape
Definition Transforms.h:44
std::function< LogicalResult(Operation *op)> FilterConstraintFnType
Callback function that indicates whether vector unrolling should be attempted on the operation.
Definition Transforms.h:32
UnrollOptions & setNativeShapeFn(NativeShapeFnType fn)
Definition Transforms.h:45
UnrolledTypeFnType getUnrolledTypes
Definition Transforms.h:55
FilterConstraintFnType filterConstraint
Definition Transforms.h:33
UnrollOptions & setUnrolledTypesFn(UnrolledTypeFnType fn)
Definition Transforms.h:56
UnrollOptions & setFilterConstraint(FilterConstraintFnType constraint)
Definition Transforms.h:34