MLIR  22.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/Operation.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Support/LogicalResult.h"
15 
16 #include <functional>
17 #include <optional>
18 #include <utility>
19 
20 namespace mlir {
21 class RewritePatternSet;
22 
23 namespace xegpu {
24 
25 /// Options to control the XeGPU unrolling. Its main purpose is to
26 /// provide a way to customize the native shape of the operation.
27 struct UnrollOptions {
28  /// Callback function that indicates whether vector unrolling should be
29  /// attempted on the operation.
30  using FilterConstraintFnType = std::function<LogicalResult(Operation *op)>;
33  filterConstraint = std::move(constraint);
34  return *this;
35  }
36 
37  /// Function that computes the target shape for unrolling. It returns an
38  /// optional vector of integers representing the shape. If it returns
39  /// `std::nullopt`, unrolling is aborted for the given operation.
41  std::function<std::optional<SmallVector<int64_t>>(Operation *op)>;
44  nativeShape = std::move(fn);
45  return *this;
46  }
47 
48  /// Function that converts a ShapedType (TensorDescType or VectorType)
49  /// into the unrolled type based on the tileShape. It returns a vector of
50  /// types representing the unrolled types for simplicity. When
51  /// `returnSingleType` is true, it returns a vector containing only one single
52  /// unrolled type.
53  using UnrolledTypeFnType = std::function<SmallVector<Type>(
54  ShapedType type, ArrayRef<int64_t> tileShape, bool returnSingleType)>;
57  getUnrolledTypes = std::move(fn);
58  return *this;
59  }
60 };
61 
62 /// Appends patterns for folding aliasing ops into XeGPU ops into `patterns`.
64 
65 /// Appends patterns for XeGPU SIMT distribution into `patterns`.
67 /// Appends patterns for moving function body into gpu.warp_execute_on_lane0 op.
69 /// Appends patterns for XeGPU workgroup to subgroup distribution into
70 /// `patterns`.
72 
73 /// Collect a set of patterns to unroll xegpu operations to a smaller shapes.
74 /// Users can control whether an operation to be unrolled or not, as well as
75 /// its target shape via `options` structure. (via setting filterConstraint
76 /// and nativeShape respectively, both of them are function refs taking `op` as
77 /// input).
78 /// An `op` is unrolled to the `targetShape` as follows, for each of its
79 /// operands:
80 /// 1. the unrolled type `unrolledType` and number of unrolled instances
81 /// `numUnrolledInstances` are computed from the `targetShape`.
82 /// 2. pack each operand. ExtractStridedSlice are created to break-up the
83 /// vector operands. And BuiltinUnrealizedCastop are created to break-up
84 /// the TensorDesc operands.
85 /// 3. the original op is cloned `numUnrolledInstances` times, once for each
86 /// result.
87 /// 4. unpack the results. InsertStridedSlice are inserted for VectorType
88 /// result, and BuiltinUnrealizedCastOp are inserted for TensorDescType result
89 /// to re-assemble the slices into the original shape.
91  const UnrollOptions &options);
92 
93 } // namespace xegpu
94 } // namespace mlir
95 
96 #endif // MLIR_DIALECT_XEGPU_TRANSFORMS_TRANSFORMS_H
static llvm::ManagedStatic< PassManagerOptions > options
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
void populateXeGPUUnrollPatterns(RewritePatternSet &patterns, const UnrollOptions &options)
Collect a set of patterns to unroll xegpu operations to a smaller shapes.
void populateXeGPUMoveFuncBodyToWarpOpPatterns(RewritePatternSet &patterns)
Appends patterns for moving function body into gpu.warp_execute_on_lane0 op.
void populateXeGPUFoldAliasOpsPatterns(RewritePatternSet &patterns)
Appends patterns for folding aliasing ops into XeGPU ops into patterns.
void populateXeGPUWgToSgDistributePatterns(RewritePatternSet &patterns)
Appends patterns for XeGPU workgroup to subgroup distribution into patterns.
void populateXeGPUSubgroupDistributePatterns(RewritePatternSet &patterns)
Appends patterns for XeGPU SIMT distribution into patterns.
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
Options to control the XeGPU unrolling.
Definition: Transforms.h:27
UnrollOptions & setFilterConstraint(FilterConstraintFnType constraint)
Definition: Transforms.h:32
std::function< std::optional< SmallVector< int64_t > >(Operation *op)> NativeShapeFnType
Function that computes the target shape for unrolling.
Definition: Transforms.h:41
UnrollOptions & setUnrolledTypesFn(UnrolledTypeFnType fn)
Definition: Transforms.h:56
NativeShapeFnType nativeShape
Definition: Transforms.h:42
std::function< LogicalResult(Operation *op)> FilterConstraintFnType
Callback function that indicates whether vector unrolling should be attempted on the operation.
Definition: Transforms.h:30
UnrolledTypeFnType getUnrolledTypes
Definition: Transforms.h:55
FilterConstraintFnType filterConstraint
Definition: Transforms.h:31
UnrollOptions & setNativeShapeFn(NativeShapeFnType fn)
Definition: Transforms.h:43
std::function< SmallVector< Type >(ShapedType type, ArrayRef< int64_t > tileShape, bool returnSingleType)> UnrolledTypeFnType
Function that converts a ShapedType (TensorDescType or VectorType) into the unrolled type based on th...
Definition: Transforms.h:54