MLIR 24.0.0git
Transforms.h
Go to the documentation of this file.
1//=- Transforms.h - X86 Dialect Transformation Entrypoints --------*- 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_X86_TRANSFORMS_H
10#define MLIR_DIALECT_X86_TRANSFORMS_H
11
12#include "mlir/IR/Value.h"
13
14namespace mlir {
19
20namespace x86 {
21/// Helper class to factor out the creation and extraction of masks from nibs.
22struct MaskHelper {
23 /// b0 captures the lowest bit, b7 captures the highest bit.
24 /// Meant to be used with instructions such as mm256BlendPs.
25 template <uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4,
26 uint8_t b5, uint8_t b6, uint8_t b7>
27 static uint8_t blend() {
28 static_assert(b0 <= 1 && b1 <= 1 && b2 <= 1 && b3 <= 1, "overflow");
29 static_assert(b4 <= 1 && b5 <= 1 && b6 <= 1 && b7 <= 1, "overflow");
30 return static_cast<uint8_t>((b7 << 7) | (b6 << 6) | (b5 << 5) | (b4 << 4) |
31 (b3 << 3) | (b2 << 2) | (b1 << 1) | b0);
32 }
33 /// b0 captures the lowest bit, b7 captures the highest bit.
34 /// Meant to be used with instructions such as mm256BlendPs.
35 static void extractBlend(uint8_t mask, uint8_t &b0, uint8_t &b1, uint8_t &b2,
36 uint8_t &b3, uint8_t &b4, uint8_t &b5, uint8_t &b6,
37 uint8_t &b7) {
38 b7 = mask & (1 << 7);
39 b6 = mask & (1 << 6);
40 b5 = mask & (1 << 5);
41 b4 = mask & (1 << 4);
42 b3 = mask & (1 << 3);
43 b2 = mask & (1 << 2);
44 b1 = mask & (1 << 1);
45 b0 = mask & 1;
46 }
47 /// b01 captures the lower 2 bits, b67 captures the higher 2 bits.
48 /// Meant to be used with instructions such as mm256ShufflePs.
49 template <unsigned b67, unsigned b45, unsigned b23, unsigned b01>
50 static uint8_t shuffle() {
51 static_assert(b01 <= 0x03, "overflow");
52 static_assert(b23 <= 0x03, "overflow");
53 static_assert(b45 <= 0x03, "overflow");
54 static_assert(b67 <= 0x03, "overflow");
55 return static_cast<uint8_t>((b67 << 6) | (b45 << 4) | (b23 << 2) | b01);
56 }
57 /// b01 captures the lower 2 bits, b67 captures the higher 2 bits.
58 static void extractShuffle(uint8_t mask, uint8_t &b01, uint8_t &b23,
59 uint8_t &b45, uint8_t &b67) {
60 b67 = (mask & (0x03 << 6)) >> 6;
61 b45 = (mask & (0x03 << 4)) >> 4;
62 b23 = (mask & (0x03 << 2)) >> 2;
63 b01 = mask & 0x03;
64 }
65 /// b03 captures the lower 4 bits, b47 captures the higher 4 bits.
66 /// Meant to be used with instructions such as mm256Permute2f128Ps.
67 template <unsigned b47, unsigned b03>
68 static uint8_t permute() {
69 static_assert(b03 <= 0x0f, "overflow");
70 static_assert(b47 <= 0x0f, "overflow");
71 return static_cast<uint8_t>((b47 << 4) + b03);
72 }
73 /// b03 captures the lower 4 bits, b47 captures the higher 4 bits.
74 static void extractPermute(uint8_t mask, uint8_t &b03, uint8_t &b47) {
75 b47 = (mask & (0x0f << 4)) >> 4;
76 b03 = mask & 0x0f;
77 }
78};
79
80//===----------------------------------------------------------------------===//
81
82// A set of patterns for specialized lowering of vector contraction
83// operation to vector fused multiply and add (FMA) operation.
85
86// A set of patterns for lowering 32-bit packed vector contraction operations
87// to their corresponding packed-type dot-product operations, ultimately
88// targeting the relevant x86 LLVM intrinsics (e.g., BF16 and Int8).
90 RewritePatternSet &patterns);
91
92// A set of patterns for lowering 32-bit packed BF16 vector contraction
93// operations to vector fused multiply-add (FMA) operations, following
94// the emulation-based approach using BF16 packed operations.
96
97// Performs forward scheduling of vector producer ops to minimize their live
98// range by placing them at their earliest legal use site.
100
101// Shuffles FMAs with x86 operations as operands such that FMAs are
102// grouped with respect to odd/even packed index.
104
105// A set of patterns for lowering 32-bit packed vector contraction operations
106// to their corresponding packed-type tiled dot-product operations, using
107// AMX ultimately targeting the relevant x86 LLVM intrinsics (e.g., BF16 and
108// Int8).
110
111// Rewrites an additive vector contraction by zeroing the accumulator input
112// and materializing the accumulation as a separate add after the contraction.
114 RewritePatternSet &patterns);
115
116// Adds patterns to fold RHS unpacking into VNNI vector contractions inside
117// scf.for loops for subsequent lowering (e.g. multi-level-unroll).
119
120// A set of patterns that tile and unroll contractions in order to prepare them
121// for the AMX nanokernel lowerings.
123 RewritePatternSet &patterns);
124
125// Approximates vectorized f32 `math.rsqrt` with the AVX `vrsqrtps`
126// instruction followed by one Newton-Raphson step. Requires the inner vector
127// dimension to be a multiple of 8.
129
130//===----------------------------------------------------------------------===//
131/// Helpers extracted from:
132/// - clang/lib/Headers/avxintrin.h
133/// - clang/test/CodeGen/X86/avx-builtins.c
134/// - clang/test/CodeGen/X86/avx2-builtins.c
135/// - clang/test/CodeGen/X86/avx-shuffle-builtins.c
136/// as well as the Intel Intrinsics Guide
137/// (https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html)
138/// make it easier to just implement known good lowerings.
139/// All intrinsics correspond 1-1 to the Intel definition.
140//===----------------------------------------------------------------------===//
141
142namespace avx2 {
143namespace inline_asm {
144//===----------------------------------------------------------------------===//
145/// Methods in the inline_asm namespace emit calls to LLVM::InlineAsmOp.
146//===----------------------------------------------------------------------===//
147/// If bit i of `mask` is zero, take f32@i from v1 else take it from v2.
149 uint8_t mask);
150} // namespace inline_asm
151
152namespace intrin {
153//===----------------------------------------------------------------------===//
154/// Methods in the intrin namespace emulate clang's impl. of X86 intrinsics.
155//===----------------------------------------------------------------------===//
156/// Lower to vector.shuffle v1, v2, [0, 8, 1, 9, 4, 12, 5, 13].
158
159/// Lower to vector.shuffle v1, v2, [0, 8, 1, 9, 4, 12, 5, 13].
161
162/// a a b b a a b b
163/// Take an 8 bit mask, 2 bit for each position of a[0, 3) **and** b[0, 4):
164/// 0:127 | 128:255
165/// b01 b23 C8 D8 | b01+4 b23+4 C8+4 D8+4
166Value mm256ShufflePs(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask);
167
168// imm[0:1] out of imm[0:3] is:
169// 0 1 2 3
170// a[0:127] or a[128:255] or b[0:127] or b[128:255] |
171// a[0:127] or a[128:255] or b[0:127] or b[128:255]
172// 0 1 2 3
173// imm[0:1] out of imm[4:7].
175 uint8_t mask);
176
177/// If bit i of `mask` is zero, take f32@i from v1 else take it from v2.
178Value mm256BlendPs(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask);
179} // namespace intrin
180
181//===----------------------------------------------------------------------===//
182/// Generic lowerings may either use intrin or inline_asm depending on needs.
183//===----------------------------------------------------------------------===//
184/// 4x8xf32-specific AVX2 transpose lowering.
186
187/// 8x8xf32-specific AVX2 transpose lowering.
189
190/// Structure to control the behavior of specialized AVX2 transpose lowering.
192 bool lower4x8xf32_ = false;
194 lower4x8xf32_ = lower;
195 return *this;
196 }
197 bool lower8x8xf32_ = false;
199 lower8x8xf32_ = lower;
200 return *this;
201 }
202};
203
204/// Options for controlling specialized AVX2 lowerings.
206 /// Configure specialized vector lowerings.
212};
213
214/// Insert specialized transpose lowering patterns.
216 RewritePatternSet &patterns, LoweringOptions options = LoweringOptions(),
217 int benefit = 10);
218} // namespace avx2
219} // namespace x86
220
221/// Collect a set of patterns to lower X86 ops to ops that map to LLVM
222/// intrinsics.
223void populateX86LegalizeForLLVMExportPatterns(LLVMTypeConverter &converter,
224 RewritePatternSet &patterns);
225
226/// Configure the target to support lowering X86 ops to ops that map to
227/// LLVM intrinsics.
228void configureX86LegalizeForExportTarget(LLVMConversionTarget &target);
229
230/// Register LLVM conversion interface for X86 dialect.
231void registerConvertX86ToLLVMInterface(DialectRegistry &registry);
232} // namespace mlir
233
234#endif // MLIR_DIALECT_X86_TRANSFORMS_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static llvm::ManagedStatic< PassManagerOptions > options
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
Definition Builders.h:632
Derived class that automatically populates legalization information for different LLVM ops.
Conversion from types to the LLVM IR dialect.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Value mm256BlendPsAsm(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask)
Methods in the inline_asm namespace emit calls to LLVM::InlineAsmOp.
Value mm256UnpackLoPs(ImplicitLocOpBuilder &b, Value v1, Value v2)
Methods in the intrin namespace emulate clang's impl. of X86 intrinsics.
Value mm256Permute2f128Ps(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask)
Value mm256BlendPs(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask)
If bit i of mask is zero, take f32@i from v1 else take it from v2.
Value mm256ShufflePs(ImplicitLocOpBuilder &b, Value v1, Value v2, uint8_t mask)
a a b b a a b b Take an 8 bit mask, 2 bit for each position of a[0, 3) and b[0, 4): 0:127 | 128:255 b...
Value mm256UnpackHiPs(ImplicitLocOpBuilder &b, Value v1, Value v2)
Lower to vector.shuffle v1, v2, [0, 8, 1, 9, 4, 12, 5, 13].
Helpers extracted from:
Definition Transforms.h:142
void transpose4x8xf32(ImplicitLocOpBuilder &ib, MutableArrayRef< Value > vs)
Generic lowerings may either use intrin or inline_asm depending on needs.
void transpose8x8xf32(ImplicitLocOpBuilder &ib, MutableArrayRef< Value > vs)
8x8xf32-specific AVX2 transpose lowering.
void populateSpecializedTransposeLoweringPatterns(RewritePatternSet &patterns, LoweringOptions options=LoweringOptions(), int benefit=10)
Insert specialized transpose lowering patterns.
void populateMathRsqrtApproximationPatterns(RewritePatternSet &patterns)
void populateVectorContractMultiLevelUnrollForAMXPatterns(RewritePatternSet &patterns)
void populateVectorContractToPackedTypeDotProductPatterns(RewritePatternSet &patterns)
void populateSinkVectorProducerOpsPatterns(RewritePatternSet &patterns)
void populateVectorContractToAMXDotProductPatterns(RewritePatternSet &patterns)
void populateVectorContractBF16ToFMAPatterns(RewritePatternSet &patterns)
void populatePrepareVectorContractLoopsPatterns(RewritePatternSet &patterns)
void populateShuffleVectorFMAOpsPatterns(RewritePatternSet &patterns)
void populateVectorContractToFMAPatterns(RewritePatternSet &patterns)
void populateMoveAccumulatorForContractLoopPatterns(RewritePatternSet &patterns)
Include the generated interface declarations.
void configureX86LegalizeForExportTarget(LLVMConversionTarget &target)
Configure the target to support lowering X86 ops to ops that map to LLVM intrinsics.
void populateX86LegalizeForLLVMExportPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to lower X86 ops to ops that map to LLVM intrinsics.
void registerConvertX86ToLLVMInterface(DialectRegistry &registry)
Register LLVM conversion interface for X86 dialect.
Helper class to factor out the creation and extraction of masks from nibs.
Definition Transforms.h:22
static void extractPermute(uint8_t mask, uint8_t &b03, uint8_t &b47)
b03 captures the lower 4 bits, b47 captures the higher 4 bits.
Definition Transforms.h:74
static uint8_t blend()
b0 captures the lowest bit, b7 captures the highest bit.
Definition Transforms.h:27
static uint8_t shuffle()
b01 captures the lower 2 bits, b67 captures the higher 2 bits.
Definition Transforms.h:50
static uint8_t permute()
b03 captures the lower 4 bits, b47 captures the higher 4 bits.
Definition Transforms.h:68
static void extractBlend(uint8_t mask, uint8_t &b0, uint8_t &b1, uint8_t &b2, uint8_t &b3, uint8_t &b4, uint8_t &b5, uint8_t &b6, uint8_t &b7)
b0 captures the lowest bit, b7 captures the highest bit.
Definition Transforms.h:35
static void extractShuffle(uint8_t mask, uint8_t &b01, uint8_t &b23, uint8_t &b45, uint8_t &b67)
b01 captures the lower 2 bits, b67 captures the higher 2 bits.
Definition Transforms.h:58
Options for controlling specialized AVX2 lowerings.
Definition Transforms.h:205
TransposeLoweringOptions transposeOptions
Configure specialized vector lowerings.
Definition Transforms.h:207
LoweringOptions & setTransposeOptions(TransposeLoweringOptions options)
Definition Transforms.h:208
Structure to control the behavior of specialized AVX2 transpose lowering.
Definition Transforms.h:191
TransposeLoweringOptions & lower8x8xf32(bool lower=true)
Definition Transforms.h:198
TransposeLoweringOptions & lower4x8xf32(bool lower=true)
Definition Transforms.h:193