MLIR 24.0.0git
VectorEmulateNarrowType.cpp
Go to the documentation of this file.
1//===- VectorEmulateNarrowType.cpp - Narrow type emulation ----------------===//
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// This file implements target-independent rewrites and utilities to emulate
10// narrow types that are not supported by the target hardware, e.g. i4
11// ("emulated type"), using wider types, e.g. i8 ("container type").
12//
13/// Currently, only power-of-two integer types are supported. These are
14/// converted to wider integers that are either 8 bits wide or wider.
15///
16/// TODO: Support for non-powers-of-two.
17//===----------------------------------------------------------------------===//
18
32#include "mlir/IR/Value.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/Support/DebugLog.h"
36#include "llvm/Support/MathExtras.h"
37#include "llvm/Support/raw_ostream.h"
38#include <cstdint>
39#include <optional>
40
42
43using namespace mlir;
44
45#define DEBUG_TYPE "vector-narrow-type-emulation"
46
49
50//===----------------------------------------------------------------------===//
51// Utils
52//===----------------------------------------------------------------------===//
53
54/// Returns a compressed mask for the emulated vector. For example, when
55/// emulating an eight-element `i8` vector with `i32` (i.e. when the source
56/// elements span two dest elements), this method compresses `vector<8xi1>`
57/// into `vector<2xi1>`.
58///
59/// The compressed/output mask value is set iff any mask in the corresponding
60/// `numSrcElemsPerDest` range of uncompressed/input masks is set. E.g., if
61/// `numSrcElemsPerDest` equals to 2, and `numFrontPadElems` equals to 1, the
62/// following mask:
63///
64/// %mask = [1, 1, 0, 0, 0, 0]
65///
66/// will first be padded in the front with `numFrontPadElems` zeros, and zeros
67/// will be added in the back to make the number of elements a multiple of
68/// `numSrcElemsPerDest` (for easier computation). The resulting mask will be:
69///
70/// %mask = [0, 1, 1, 0, 0, 0, 0, 0]
71///
72/// then it will return the following new compressed mask:
73///
74/// %mask = [1, 1, 0, 0]
75///
76/// NOTE: `numFrontPadElems` is assumed to be strictly smaller than
77/// `numSrcElemsPerDest`.
78static FailureOr<Operation *> getCompressedMaskOp(OpBuilder &rewriter,
79 Location loc, Value mask,
80 int numSrcElems,
81 int numSrcElemsPerDest,
82 int numFrontPadElems = 0) {
83
84 assert(numFrontPadElems < numSrcElemsPerDest &&
85 "numFrontPadElems must be less than numSrcElemsPerDest");
86
87 auto numDestElems =
88 (numFrontPadElems + numSrcElems + numSrcElemsPerDest - 1) /
89 numSrcElemsPerDest;
90
91 Operation *maskOp = mask.getDefiningOp();
92 // Chain of `vector.extract` ops that lead to the op that created the mask.
93 // TODO: add support to `vector.broadcast`.
95
96 // Trace the mask back to its creation op, looking through `vector.extract`
97 // ops. Any other defining op is unsupported.
98 while (maskOp &&
99 !isa<arith::ConstantOp, vector::CreateMaskOp, vector::ConstantMaskOp>(
100 maskOp)) {
101 auto extractOp = dyn_cast<vector::ExtractOp>(maskOp);
102 if (!extractOp)
103 return failure();
104 maskOp = extractOp.getSource().getDefiningOp();
105 extractOps.push_back(extractOp);
106 }
107
108 // `maskOp` is null when the mask is a block argument, which has no defining
109 // op.
110 if (!isa_and_present<arith::ConstantOp, vector::CreateMaskOp,
111 vector::ConstantMaskOp>(maskOp))
112 return failure();
113
114 // Computing the "compressed" mask. All the emulation logic (i.e. computing
115 // new mask index) only happens on the last dimension of the vectors.
116 SmallVector<int64_t> maskShape(
117 cast<VectorType>(maskOp->getResultTypes()[0]).getShape());
118 maskShape.back() = numDestElems;
119 auto newMaskType = VectorType::get(maskShape, rewriter.getI1Type());
120 std::optional<Operation *> newMask =
122 .Case(
123 [&](vector::CreateMaskOp createMaskOp)
124 -> std::optional<Operation *> {
125 OperandRange maskOperands = createMaskOp.getOperands();
126 // The `vector.create_mask` op creates a mask arrangement
127 // without any zeros at the front. Also, because
128 // `numFrontPadElems` is strictly smaller than
129 // `numSrcElemsPerDest`, the compressed mask generated by
130 // padding the original mask by `numFrontPadElems` will not
131 // have any zeros at the front as well.
132 AffineExpr s0;
133 bindSymbols(rewriter.getContext(), s0);
134 s0 = (s0 + numFrontPadElems).ceilDiv(numSrcElemsPerDest);
135 OpFoldResult origIndex = getAsOpFoldResult(maskOperands.back());
137 rewriter, loc, s0, origIndex);
138 SmallVector<Value> newMaskOperands(maskOperands.drop_back());
139 newMaskOperands.push_back(
140 getValueOrCreateConstantIndexOp(rewriter, loc, maskIndex));
141 return vector::CreateMaskOp::create(rewriter, loc, newMaskType,
142 newMaskOperands);
143 })
144 .Case([&](vector::ConstantMaskOp constantMaskOp)
145 -> std::optional<Operation *> {
146 // Take the shape of mask, compress its trailing dimension:
147 SmallVector<int64_t> maskDimSizes(constantMaskOp.getMaskDimSizes());
148 int64_t &maskIndex = maskDimSizes.back();
149 maskIndex = llvm::divideCeil(numFrontPadElems + maskIndex,
150 numSrcElemsPerDest);
151 return vector::ConstantMaskOp::create(rewriter, loc, newMaskType,
152 maskDimSizes);
153 })
154 .Case([&](arith::ConstantOp constantOp)
155 -> std::optional<Operation *> {
156 // TODO: Support multiple dimensions.
157 if (maskShape.size() != 1)
158 return std::nullopt;
159 // Rearrange the original mask values to cover the whole potential
160 // loading region. For example, in the case of using byte-size for
161 // emulation, given the following mask:
162 //
163 // %mask = [0, 1, 0, 1, 0, 0]
164 //
165 // With front offset of 1, the mask will be padded 0s in the front
166 // and back so that:
167 // 1. It is aligned with the effective loading bits
168 // 2. Its length is multiple of `numSrcElemPerDest` (and the total
169 // coverage size is mulitiple of bytes). The new mask will be like
170 // this before compressing:
171 //
172 // %new_mask = [0, 0, 1, 0, 1, 0, 0, 0]
173 auto originalMask =
174 cast<DenseIntElementsAttr>(constantOp.getValue());
175 SmallVector<bool> paddedMaskValues(numFrontPadElems, false);
176 paddedMaskValues.append(originalMask.template value_begin<bool>(),
177 originalMask.template value_end<bool>());
178 paddedMaskValues.resize(numDestElems * numSrcElemsPerDest, false);
179
180 // Compressing by combining every `numSrcElemsPerDest` elements:
181 SmallVector<bool> compressedMaskValues;
182 for (size_t i = 0; i < paddedMaskValues.size();
183 i += numSrcElemsPerDest) {
184 bool combinedValue = false;
185 for (int j = 0; j < numSrcElemsPerDest; ++j) {
186 combinedValue |= paddedMaskValues[i + j];
187 }
188 compressedMaskValues.push_back(combinedValue);
189 }
190 return arith::ConstantOp::create(
191 rewriter, loc,
192 DenseElementsAttr::get(newMaskType, compressedMaskValues));
193 });
194
195 if (!newMask)
196 return failure();
197
198 while (!extractOps.empty()) {
199 newMask =
200 vector::ExtractOp::create(rewriter, loc, (*newMask)->getResults()[0],
201 extractOps.back().getMixedPosition());
202 extractOps.pop_back();
203 }
204
205 return *newMask;
206}
207
208/// Extracts 1-D subvector from a 1-D vector.
209///
210/// Given the input rank-1 source vector, extracts `numElemsToExtract` elements
211/// from `src`, starting at `offset`. The result is also a rank-1 vector:
212///
213/// vector<numElemsToExtract x !elemType>
214///
215/// (`!elType` is the element type of the source vector). As `offset` is a known
216/// _static_ value, this helper hook emits `vector.extract_strided_slice`.
217///
218/// EXAMPLE:
219/// %res = vector.extract_strided_slice %src
220/// { offsets = [offset], sizes = [numElemsToExtract], strides = [1] }
222 Value src, int64_t offset,
223 int64_t numElemsToExtract) {
224 auto vectorType = cast<VectorType>(src.getType());
225 assert(vectorType.getRank() == 1 && "expected source to be rank-1-D vector ");
226 assert(offset + numElemsToExtract <= vectorType.getNumElements() &&
227 "subvector out of bounds");
228
229 // When extracting all available elements, just use the source vector as the
230 // result.
231 if (vectorType.getNumElements() == numElemsToExtract)
232 return src;
233
234 auto offsets = rewriter.getI64ArrayAttr({offset});
235 auto sizes = rewriter.getI64ArrayAttr({numElemsToExtract});
236 auto strides = rewriter.getI64ArrayAttr({1});
237
238 auto resultVectorType =
239 VectorType::get({numElemsToExtract}, vectorType.getElementType());
240 return vector::ExtractStridedSliceOp::create(rewriter, loc, resultVectorType,
241 src, offsets, sizes, strides)
242 ->getResult(0);
243}
244
245/// Inserts 1-D subvector into a 1-D vector.
246///
247/// Inserts the input rank-1 source vector into the destination vector starting
248/// at `offset`. As `offset` is a known _static_ value, this helper hook emits
249/// `vector.insert_strided_slice`.
250///
251/// EXAMPLE:
252/// %res = vector.insert_strided_slice %src, %dest
253/// {offsets = [%offset], strides [1]}
255 Value src, Value dest, int64_t offset) {
256 [[maybe_unused]] auto srcVecTy = cast<VectorType>(src.getType());
257 [[maybe_unused]] auto destVecTy = cast<VectorType>(dest.getType());
258 assert(srcVecTy.getRank() == 1 && destVecTy.getRank() == 1 &&
259 "expected source and dest to be rank-1 vector types");
260
261 // If overwritting the destination vector, just return the source.
262 if (srcVecTy.getNumElements() == destVecTy.getNumElements() && offset == 0)
263 return src;
264
265 auto offsets = rewriter.getI64ArrayAttr({offset});
266 auto strides = rewriter.getI64ArrayAttr({1});
267 return vector::InsertStridedSliceOp::create(rewriter, loc, destVecTy, src,
268 dest, offsets, strides);
269}
270
271/// Extracts 1-D subvector from a 1-D vector.
272///
273/// Given the input rank-1 source vector, extracts `numElemsToExtact` elements
274/// from `src`, starting at `offset`. The result is also a rank-1 vector:
275///
276/// vector<numElemsToExtact x !elType>
277///
278/// (`!elType` is the element type of the source vector). As `offset` is assumed
279/// to be a _dynamic_ SSA value, this helper method generates a sequence of
280/// `vector.extract` + `vector.insert` pairs.
281///
282/// EXAMPLE:
283/// %v1 = vector.extract %src[%offset] : i2 from vector<8xi2>
284/// %r1 = vector.insert %v1, %dest[0] : i2 into vector<3xi2>
285/// %c1 = arith.constant 1 : index
286/// %idx2 = arith.addi %offset, %c1 : index
287/// %v2 = vector.extract %src[%idx2] : i2 from vector<8xi2>
288/// %r2 = vector.insert %v2, %r1 [1] : i2 into vector<3xi2>
289/// (...)
291 Value src, Value dest,
292 OpFoldResult offset,
293 int64_t numElemsToExtract) {
294 auto srcVecTy = cast<VectorType>(src.getType());
295 assert(srcVecTy.getRank() == 1 && "expected source to be rank-1-D vector ");
296 // NOTE: We are unable to take the offset into account in the following
297 // assert, hence its still possible that the subvector is out-of-bounds even
298 // if the condition is true.
299 assert(numElemsToExtract <= srcVecTy.getNumElements() &&
300 "subvector out of bounds");
301
302 // When extracting all available elements, just use the source vector as the
303 // result.
304 if (srcVecTy.getNumElements() == numElemsToExtract)
305 return src;
306
307 for (int i = 0; i < numElemsToExtract; ++i) {
308 Value extractLoc =
309 (i == 0) ? dyn_cast<Value>(offset)
310 : arith::AddIOp::create(
311 rewriter, loc, rewriter.getIndexType(),
312 dyn_cast<Value>(offset),
313 arith::ConstantIndexOp::create(rewriter, loc, i));
314 auto extractOp = vector::ExtractOp::create(rewriter, loc, src, extractLoc);
315 dest = vector::InsertOp::create(rewriter, loc, extractOp, dest, i);
316 }
317 return dest;
318}
319
320/// Inserts 1-D subvector into a 1-D vector.
321///
322/// Inserts the input rank-1 source vector into the destination vector starting
323/// at `offset`. As `offset` is assumed to be a _dynamic_ SSA value, this hook
324/// uses a sequence of `vector.extract` + `vector.insert` pairs.
325///
326/// EXAMPLE:
327/// %v1 = vector.extract %src[0] : i2 from vector<8xi2>
328/// %r1 = vector.insert %v1, %dest[%offset] : i2 into vector<3xi2>
329/// %c1 = arith.constant 1 : index
330/// %idx2 = arith.addi %offset, %c1 : index
331/// %v2 = vector.extract %src[1] : i2 from vector<8xi2>
332/// %r2 = vector.insert %v2, %r1 [%idx2] : i2 into vector<3xi2>
333/// (...)
335 Value src, Value dest,
336 OpFoldResult offset,
337 int64_t numElemsToInsert) {
338 auto srcVecTy = cast<VectorType>(src.getType());
339 auto destVecTy = cast<VectorType>(dest.getType());
340 assert(srcVecTy.getRank() == 1 && destVecTy.getRank() == 1 &&
341 "expected source and dest to be rank-1 vector types");
342 (void)srcVecTy;
343 (void)destVecTy;
344 assert(numElemsToInsert > 0 &&
345 "the number of elements to insert must be greater than 0");
346 // NOTE: We are unable to take the offset into account in the following
347 // assert, hence its still possible that the subvector is out-of-bounds even
348 // if the condition is true.
349 assert(numElemsToInsert <= destVecTy.getNumElements() &&
350 "subvector out of bounds");
351
352 Value destOffsetVal = getValueOrCreateConstantIndexOp(rewriter, loc, offset);
353 for (int64_t i = 0; i < numElemsToInsert; ++i) {
354 auto insertLoc =
355 i == 0 ? destOffsetVal
356 : arith::AddIOp::create(
357 rewriter, loc, rewriter.getIndexType(), destOffsetVal,
358 arith::ConstantIndexOp::create(rewriter, loc, i));
359 auto extractOp = vector::ExtractOp::create(rewriter, loc, src, i);
360 dest = vector::InsertOp::create(rewriter, loc, extractOp, dest, insertLoc);
361 }
362 return dest;
363}
364
365/// Emulate a vector load for `emulatedElemTy` using `containerElemTy`
366///
367/// Specifically, use `containerElemTy` for loading a vector of
368/// `emulatedElemTy`. The load location is given by `base` and
369/// `linearizedIndices`, and the load size is given by
370/// `numEmulatedElementsToLoad`.
372 Value base,
373 OpFoldResult linearizedIndices,
374 int64_t numContainerElemsToLoad,
375 Type emulatedElemTy,
376 Type containerElemTy) {
377 auto emulatedPerContainerElem = containerElemTy.getIntOrFloatBitWidth() /
378 emulatedElemTy.getIntOrFloatBitWidth();
379 auto newLoad = vector::LoadOp::create(
380 rewriter, loc, VectorType::get(numContainerElemsToLoad, containerElemTy),
381 base, getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices));
382 return vector::BitCastOp::create(
383 rewriter, loc,
384 VectorType::get(numContainerElemsToLoad * emulatedPerContainerElem,
385 emulatedElemTy),
386 newLoad);
387}
388
389/// Downcast two values to `downcastType`, then select values
390/// based on `mask`, and casts the result to `upcastType`.
392 VectorType downcastType,
393 VectorType upcastType, Value mask,
394 Value trueValue, Value falseValue) {
395 assert(
396 downcastType.getNumElements() * downcastType.getElementTypeBitWidth() ==
397 upcastType.getNumElements() * upcastType.getElementTypeBitWidth() &&
398 "expected input and output number of bits to match");
399 if (trueValue.getType() != downcastType) {
400 trueValue =
401 vector::BitCastOp::create(builder, loc, downcastType, trueValue);
402 }
403 if (falseValue.getType() != downcastType) {
404 falseValue =
405 vector::BitCastOp::create(builder, loc, downcastType, falseValue);
406 }
407 Value selectedType =
408 arith::SelectOp::create(builder, loc, mask, trueValue, falseValue);
409 // Upcast the selected value to the new type.
410 return vector::BitCastOp::create(builder, loc, upcastType, selectedType);
411}
412
413/// Emits `memref.generic_atomic_rmw` op to store a subbyte-sized value to a
414/// byte in `linearizedMemref`, with a mask. The `valueToStore` is a vector of
415/// subbyte-sized elements, with size of 8 bits, and the mask is used to select
416/// which elements to store.
417///
418/// Inputs:
419/// linearizedMemref = |2|2|2|2| : <4xi2> (<1xi8>)
420/// storeIdx = 2
421/// valueToStore = |3|3|3|3| : vector<4xi2>
422/// mask = |0|0|1|1| : vector<4xi1>
423///
424/// Result:
425/// linearizedMemref = |2|2|3|3| : <4xi2> (<1xi8>)
426static void atomicRMW(OpBuilder &builder, Location loc,
427 MemRefValue linearizedMemref, Value storeIdx,
428 VectorValue valueToStore, Value mask) {
429 assert(valueToStore.getType().getRank() == 1 && "expected 1-D vector");
430
431 // Create an atomic load-modify-write region using
432 // `memref.generic_atomic_rmw`.
433 auto atomicOp = memref::GenericAtomicRMWOp::create(
434 builder, loc, linearizedMemref, ValueRange{storeIdx});
435 Value origValue = atomicOp.getCurrentValue();
436
437 OpBuilder::InsertionGuard guard(builder);
438 builder.setInsertionPointToStart(atomicOp.getBody());
439
440 // Load the original value from memory, and cast it to the original element
441 // type.
442 auto oneElemVecType = VectorType::get({1}, origValue.getType());
443 Value origVecValue = vector::FromElementsOp::create(
444 builder, loc, oneElemVecType, ValueRange{origValue});
445
446 // Construct the final masked value and yield it.
447 Value maskedValue =
448 downcastSelectAndUpcast(builder, loc, valueToStore.getType(),
449 oneElemVecType, mask, valueToStore, origVecValue);
450 auto scalarMaskedValue =
451 vector::ExtractOp::create(builder, loc, maskedValue, 0);
452 memref::AtomicYieldOp::create(builder, loc, scalarMaskedValue);
453}
454
455/// Generate a non-atomic read-modify-write sequence for storing to the emulated
456/// type. It has similar logic to `atomicRMWStore`, but without atomicity.
457static void nonAtomicRMW(OpBuilder &builder, Location loc,
458 MemRefValue linearizedMemref, Value linearizedIndex,
459 VectorValue valueToStore, Value mask) {
460 assert(valueToStore.getType().getRank() == 1 && "expected 1-D vector");
461
462 auto oneElemVecType =
463 VectorType::get({1}, linearizedMemref.getType().getElementType());
464 Value origVecValue =
465 vector::LoadOp::create(builder, loc, oneElemVecType, linearizedMemref,
466 ValueRange{linearizedIndex});
467 origVecValue = vector::BitCastOp::create(builder, loc, valueToStore.getType(),
468 origVecValue);
469
470 Value maskedValue =
471 downcastSelectAndUpcast(builder, loc, valueToStore.getType(),
472 oneElemVecType, mask, valueToStore, origVecValue);
473 vector::StoreOp::create(builder, loc, maskedValue, linearizedMemref,
474 linearizedIndex);
475}
476
477/// Extract `sliceNumElements` from source `vector` at `extractOffset`,
478/// and insert it into an empty vector at `insertOffset`.
479/// Inputs:
480/// vec_in = |0|1|2|3| : vector<4xi2>
481/// extractOffset = 1
482/// sliceNumElements = 2
483/// insertOffset = 2
484/// Output:
485/// vec_out = |0|0|1|2| : vector<4xi2>
486static Value extractSliceIntoByte(ConversionPatternRewriter &rewriter,
488 int64_t extractOffset,
489 int64_t sliceNumElements,
490 int64_t insertOffset) {
491 assert(vector.getType().getRank() == 1 && "expected 1-D vector");
492 auto vectorElementType = vector.getType().getElementType();
493 // TODO: update and use `alignedConversionPrecondition` in the place of
494 // these asserts.
495 assert(
496 sliceNumElements * vectorElementType.getIntOrFloatBitWidth() <= 8 &&
497 "sliceNumElements * vector element size must be less than or equal to 8");
498 assert(8 % vectorElementType.getIntOrFloatBitWidth() == 0 &&
499 "vector element must be a valid sub-byte type");
500 auto emulatedPerContainerElem = 8 / vectorElementType.getIntOrFloatBitWidth();
501 auto emptyByteVector = arith::ConstantOp::create(
502 rewriter, loc,
503 VectorType::get({emulatedPerContainerElem}, vectorElementType),
504 rewriter.getZeroAttr(
505 VectorType::get({emulatedPerContainerElem}, vectorElementType)));
506 auto extracted = staticallyExtractSubvector(rewriter, loc, vector,
507 extractOffset, sliceNumElements);
508 return staticallyInsertSubvector(rewriter, loc, extracted, emptyByteVector,
509 insertOffset);
510}
511
512namespace {
513
514//===----------------------------------------------------------------------===//
515// ConvertVectorStore
516//===----------------------------------------------------------------------===//
517
518// Emulate `vector.store` using a multi-byte container type.
519//
520// When `assumeAligned` is true, store offsets are assumed to be aligned to
521// container element boundaries, so a store whose source vector fills whole
522// container elements (isDivisibleInSize) is emitted as a simple bitcast +
523// store without checking the offset. Stores that are not divisible in size
524// are rejected. This is useful for downstream users that have already
525// ensured alignment.
526//
527// The container type is obtained through Op adaptor and would normally be
528// generated via `NarrowTypeEmulationConverter`.
529//
530// EXAMPLE 1
531// (aligned store of i4, emulated using i8 as the container type)
532//
533// vector.store %src, %dest[%idx_1, %idx_2] : memref<4x8xi4>, vector<8xi4>
534//
535// is rewritten as:
536//
537// %src_bitcast = vector.bitcast %src : vector<8xi4> to vector<4xi8>
538// vector.store %src_bitcast, %dest_bitcast[%idx]
539// : memref<16xi8>, vector<4xi8>
540//
541// EXAMPLE 2
542// (unaligned store of i2, emulated using i8 as the container type)
543//
544// vector.store %src, %dest[%c2, %c0] :memref<3x3xi2>, vector<3xi2>
545//
546// The i2 store is emulated through 2 x RMW sequences. The destination i2 memref
547// is modelled using 3 bytes:
548//
549// Byte 0 Byte 1 Byte 2
550// +----------+----------+----------+
551// | oooooooo | ooooNNNN | NNoooooo |
552// +----------+----------+----------+
553//
554// N - (N)ew entries (i.e. to be overwritten by vector.store)
555// o - (o)ld entries (to be preserved)
556//
557// For the generated output in the non-atomic case, see:
558// * @vector_store_i2_const_index_two_partial_stores`
559// in:
560// * "vector-emulate-narrow-type-unaligned-non-atomic.mlir".
561//
562// NOTE: By default, all RMW sequences are atomic. Set `disableAtomicRMW` to
563// `false` to generate non-atomic RMW sequences.
564struct ConvertVectorStore final : OpConversionPattern<vector::StoreOp> {
565 using Base::Base;
566
567 ConvertVectorStore(MLIRContext *context, bool disableAtomicRMW,
568 bool assumeAligned)
569 : OpConversionPattern<vector::StoreOp>(context),
570 disableAtomicRMW(disableAtomicRMW), assumeAligned(assumeAligned) {}
571
572 LogicalResult
573 matchAndRewrite(vector::StoreOp op, OpAdaptor adaptor,
574 ConversionPatternRewriter &rewriter) const override {
575
576 if (op.getValueToStore().getType().getRank() != 1)
577 return rewriter.notifyMatchFailure(op,
578 "only 1-D vectors are supported ATM");
579
580 auto loc = op.getLoc();
581
582 auto valueToStore = cast<VectorValue>(op.getValueToStore());
583 auto containerElemTy =
584 cast<MemRefType>(adaptor.getBase().getType()).getElementType();
585 Type emulatedElemTy = op.getValueToStore().getType().getElementType();
586 int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
587 int containerBits = containerElemTy.getIntOrFloatBitWidth();
588
589 // Check per-element alignment.
590 if (containerBits % emulatedBits != 0) {
591 return rewriter.notifyMatchFailure(
592 op, "impossible to pack emulated elements into container elements "
593 "(bit-wise misalignment)");
594 }
595 int emulatedPerContainerElem = containerBits / emulatedBits;
596
597 // Adjust the number of elements to store when emulating narrow types.
598 // Here only the 1-D vector store is considered, and the N-D memref types
599 // should be linearized.
600 // For example, to emulate i4 to i8, the following op:
601 //
602 // vector.store %arg1, %0[%arg2, %arg3] : memref<4x8xi4>, vector<8xi4>
603 //
604 // can be replaced with
605 //
606 // %bitcast = vector.bitcast %arg1 : vector<8xi4> to vector<4xi8>
607 // vector.store %bitcast, %alloc[%linear_index] : memref<16xi8>,
608 // vector<4xi8>
609
610 auto origElements = valueToStore.getType().getNumElements();
611 // Note, per-element-alignment was already verified above.
612 bool isDivisibleInSize = origElements % emulatedPerContainerElem == 0;
613
614 // In assume-aligned mode, isDivisibleInSize alone is sufficient — the
615 // caller guarantees that store offsets are aligned to container element
616 // boundaries.
617 if (assumeAligned) {
618 if (!isDivisibleInSize)
619 return rewriter.notifyMatchFailure(
620 op, "the source vector does not fill whole container elements "
621 "(not divisible in size)");
622
623 auto stridedMetadata =
624 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
625 OpFoldResult linearizedIndices;
626 std::tie(std::ignore, linearizedIndices) =
628 rewriter, loc, emulatedBits, containerBits,
629 stridedMetadata.getConstifiedMixedOffset(),
630 stridedMetadata.getConstifiedMixedSizes(),
631 stridedMetadata.getConstifiedMixedStrides(),
632 getAsOpFoldResult(adaptor.getIndices()));
633 auto memrefBase = cast<MemRefValue>(adaptor.getBase());
634 int numElements = origElements / emulatedPerContainerElem;
635 auto bitCast = vector::BitCastOp::create(
636 rewriter, loc, VectorType::get(numElements, containerElemTy),
637 op.getValueToStore());
638 rewriter.replaceOpWithNewOp<vector::StoreOp>(
639 op, bitCast.getResult(), memrefBase,
640 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices));
641 return success();
642 }
643
644 auto stridedMetadata =
645 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
646
647 OpFoldResult linearizedIndices;
648 memref::LinearizedMemRefInfo linearizedInfo;
649 std::tie(linearizedInfo, linearizedIndices) =
651 rewriter, loc, emulatedBits, containerBits,
652 stridedMetadata.getConstifiedMixedOffset(),
653 stridedMetadata.getConstifiedMixedSizes(),
654 stridedMetadata.getConstifiedMixedStrides(),
655 getAsOpFoldResult(adaptor.getIndices()));
656
657 // Use the exact intraDataOffset when it can be folded. Dynamic values are
658 // rejected in this path because a dynamic offset is not necessarily aligned
659 // to a container element boundary. Callers that can guarantee alignment
660 // should use assumeAligned.
661 std::optional<int64_t> foldedNumFrontPadElems =
662 getConstantIntValue(linearizedInfo.intraDataOffset);
663
664 if (!foldedNumFrontPadElems) {
665 return rewriter.notifyMatchFailure(
666 op, "subbyte store emulation: dynamic front padding size is "
667 "not yet implemented");
668 }
669
670 auto memrefBase = cast<MemRefValue>(adaptor.getBase());
671
672 // RMWs are not needed when:
673 // * no _partial_ stores are required.
674 // A partial store is defined as a store in which only a part of the
675 // container element is overwritten, e.g.
676 //
677 // Dest before (8 bits)
678 // +----------+
679 // | 11000000 |
680 // +----------+
681 //
682 // Dest after storing 0xF at offset 4 (in bits)
683 // +----------+
684 // | 11001111 |
685 // +----------+
686 //
687 // At a higher level, this translats to:
688 // 1. The source vector size (in bits) is a multiple of byte size.
689 // 2. The address of the store is aligned to the container type width
690 // boundary.
691 //
692 // EXAMPLE 1:
693 // Requires partial store:
694 // vector.store %arg0, %0[%c3] : memref<13xi2>, vector<4xi2>
695 //
696 // EXAMPLE 2:
697 // Does not require a partial store:
698 // vector.store %arg0, %0[%c4] : memref<13xi2>, vector<4xi2>
699 //
700 // TODO: Take linearizedInfo.linearizedOffset into account. This is
701 // currently not needed/used/exercised as all our tests set offset to 0.
702 bool emulationRequiresPartialStores = *foldedNumFrontPadElems != 0;
703
704 if (!emulationRequiresPartialStores) {
705 // Basic case: storing full bytes.
706 auto numElements = origElements / emulatedPerContainerElem;
707 auto bitCast = vector::BitCastOp::create(
708 rewriter, loc, VectorType::get(numElements, containerElemTy),
709 op.getValueToStore());
710 rewriter.replaceOpWithNewOp<vector::StoreOp>(
711 op, bitCast.getResult(), memrefBase,
712 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices));
713 return success();
714 }
715
716 // Next, handle the case when sub-byte read-modify-write
717 // sequences are needed to emulate a vector store.
718 // Here is an example:
719 //
720 // Vector to store: vector<7xi2>
721 // Value to store: 11 11 11 11 11 11 11 (all ones)
722 //
723 // Destination: memref<12xi2>
724 // Store offset: 2 (i.e. 4 bits into the 1st emulated byte).
725 //
726 // Input MLIR: vector.store %val, %dest[%c2] : memref<12xi2>, vector<7xi2>
727 //
728 // Destination memref before:
729 //
730 // Byte 0 Byte 1 Byte 2
731 // +----------+----------+----------+
732 // | 00000000 | 00000000 | 00000000 |
733 // +----------+----------+----------+
734 //
735 // Destination memref after:
736 //
737 // Byte 0 Byte 1 Byte 2
738 // +----------+----------+----------+
739 // | 00001111 | 11111111 | 11000000 |
740 // +----------+----------+----------+
741 //
742 // Note, stores to Byte 1 are "full-width" and hence don't require RMW (no
743 // need for atomicity). Stores to Bytes 0 and Byte 2 are "partial", hence
744 // requiring RMW access (atomicity is required).
745
746 // The index into the target memref we are storing to.
747 Value currentDestIndex =
748 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices);
749 // The index into the source vector we are currently processing.
750 auto currentSourceIndex = 0;
751
752 // Build a mask used for rmw.
753 auto subWidthStoreMaskType =
754 VectorType::get({emulatedPerContainerElem}, rewriter.getI1Type());
755
756 auto storeFunc = disableAtomicRMW ? nonAtomicRMW : atomicRMW;
757
758 // 1. Partial width store for the leading byte.
759 // When the store address is not aligned to emulated width boundary, deal
760 // with the unaligned part so that the rest elements are aligned to width
761 // boundary.
762 auto frontSubWidthStoreElem =
763 (emulatedPerContainerElem - *foldedNumFrontPadElems) %
764 emulatedPerContainerElem;
765 if (frontSubWidthStoreElem > 0) {
766 SmallVector<bool> frontMaskValues(emulatedPerContainerElem, false);
767 if (*foldedNumFrontPadElems + origElements < emulatedPerContainerElem) {
768 std::fill_n(frontMaskValues.begin() + *foldedNumFrontPadElems,
769 origElements, true);
770 frontSubWidthStoreElem = origElements;
771 } else {
772 std::fill_n(frontMaskValues.end() - frontSubWidthStoreElem,
773 *foldedNumFrontPadElems, true);
774 }
775 auto frontMask = arith::ConstantOp::create(
776 rewriter, loc,
777 DenseElementsAttr::get(subWidthStoreMaskType, frontMaskValues));
778
779 currentSourceIndex = emulatedPerContainerElem - (*foldedNumFrontPadElems);
780 auto value =
781 extractSliceIntoByte(rewriter, loc, valueToStore, 0,
782 frontSubWidthStoreElem, *foldedNumFrontPadElems);
783
784 storeFunc(rewriter, loc, memrefBase, currentDestIndex,
785 cast<VectorValue>(value), frontMask.getResult());
786 }
787
788 if (currentSourceIndex >= origElements) {
789 rewriter.eraseOp(op);
790 return success();
791 }
792
793 // Increment the destination index by 1 to align to the emulated width
794 // boundary.
795 auto constantOne = arith::ConstantIndexOp::create(rewriter, loc, 1);
796 currentDestIndex = arith::AddIOp::create(
797 rewriter, loc, rewriter.getIndexType(), currentDestIndex, constantOne);
798
799 // 2. Full width store for the inner output bytes.
800 // After the previous step, the store address is aligned to the emulated
801 // width boundary.
802 int64_t fullWidthStoreSize =
803 (origElements - currentSourceIndex) / emulatedPerContainerElem;
804 int64_t numNonFullWidthElements =
805 fullWidthStoreSize * emulatedPerContainerElem;
806 if (fullWidthStoreSize > 0) {
807 auto fullWidthStorePart = staticallyExtractSubvector(
808 rewriter, loc, valueToStore, currentSourceIndex,
809 numNonFullWidthElements);
810
811 auto originType = cast<VectorType>(fullWidthStorePart.getType());
812 auto memrefElemType = getElementTypeOrSelf(memrefBase.getType());
813 auto storeType = VectorType::get(
814 {originType.getNumElements() / emulatedPerContainerElem},
815 memrefElemType);
816 auto bitCast = vector::BitCastOp::create(rewriter, loc, storeType,
817 fullWidthStorePart);
818 vector::StoreOp::create(rewriter, loc, bitCast.getResult(), memrefBase,
819 currentDestIndex);
820
821 currentSourceIndex += numNonFullWidthElements;
822 currentDestIndex = arith::AddIOp::create(
823 rewriter, loc, rewriter.getIndexType(), currentDestIndex,
824 arith::ConstantIndexOp::create(rewriter, loc, fullWidthStoreSize));
825 }
826
827 // 3. Partial width store for the trailing output byte.
828 // It is needed when the residual length is smaller than the emulated width,
829 // which is not covered in step 2 above.
830 auto remainingElements = origElements - currentSourceIndex;
831 if (remainingElements != 0) {
832 auto subWidthStorePart =
833 extractSliceIntoByte(rewriter, loc, cast<VectorValue>(valueToStore),
834 currentSourceIndex, remainingElements, 0);
835
836 // Generate back mask.
837 auto maskValues = SmallVector<bool>(emulatedPerContainerElem, false);
838 std::fill_n(maskValues.begin(), remainingElements, 1);
839 auto backMask = arith::ConstantOp::create(
840 rewriter, loc,
841 DenseElementsAttr::get(subWidthStoreMaskType, maskValues));
842
843 storeFunc(rewriter, loc, memrefBase, currentDestIndex,
844 cast<VectorValue>(subWidthStorePart), backMask.getResult());
845 }
846
847 rewriter.eraseOp(op);
848 return success();
849 }
850
851private:
852 const bool disableAtomicRMW;
853 const bool assumeAligned;
854};
855
856//===----------------------------------------------------------------------===//
857// ConvertVectorMaskedStore
858//===----------------------------------------------------------------------===//
859
860/// Converts `vector.maskedstore` operations on narrow element types to work
861/// with wider, byte-aligned container types by adjusting the mask and using
862/// bitcasting.
863///
864/// Example: Storing `vector<6xi4>` is emulated by bitcasting to `vector<3xi8>`
865/// (each `i8` container element holds two `i4` values) and storing with an
866/// adjusted mask .
867struct ConvertVectorMaskedStore final
868 : OpConversionPattern<vector::MaskedStoreOp> {
869 using Base::Base;
870
871 LogicalResult
872 matchAndRewrite(vector::MaskedStoreOp op, OpAdaptor adaptor,
873 ConversionPatternRewriter &rewriter) const override {
874
875 // Prerequisite: memref in the vector.maskedstore op is flattened into 1-D.
876 if (op.getValueToStore().getType().getRank() != 1)
877 return rewriter.notifyMatchFailure(
878 op, "Memref in vector.maskedstore op must be flattened beforehand.");
879
880 auto loc = op.getLoc();
881 auto containerElemTy =
882 cast<MemRefType>(adaptor.getBase().getType()).getElementType();
883 Type emulatedElemTy = op.getValueToStore().getType().getElementType();
884 int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
885 int containerBits = containerElemTy.getIntOrFloatBitWidth();
886
887 // Check per-element alignment.
888 if (containerBits % emulatedBits != 0) {
889 return rewriter.notifyMatchFailure(
890 op, "impossible to pack emulated elements into container elements "
891 "(bit-wise misalignment)");
892 }
893
894 int emulatedPerContainerElem = containerBits / emulatedBits;
895 int origElements = op.getValueToStore().getType().getNumElements();
896 if (origElements % emulatedPerContainerElem != 0)
897 return failure();
898
899 auto stridedMetadata =
900 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
901 OpFoldResult linearizedIndicesOfr;
902 memref::LinearizedMemRefInfo linearizedInfo;
903 std::tie(linearizedInfo, linearizedIndicesOfr) =
905 rewriter, loc, emulatedBits, containerBits,
906 stridedMetadata.getConstifiedMixedOffset(),
907 stridedMetadata.getConstifiedMixedSizes(),
908 stridedMetadata.getConstifiedMixedStrides(),
909 getAsOpFoldResult(adaptor.getIndices()));
910 Value linearizedIndices =
911 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndicesOfr);
912
913 // Load the whole data and use arith.select to handle the corner cases.
914 //
915 // As an example, for this masked store of i4 values:
916 //
917 // vector.maskedstore %0[%c0, %c0], %mask, %val_to_store
918 //
919 // and given these input values:
920 //
921 // %mask = [0, 1, 1, 1, 1, 0, 0, 0] (8 * i1)
922 // %0[%c0, %c0] =
923 // [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8] (8 * i4)
924 // %val_to_store =
925 // [0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x0] (8 * i4)
926 //
927 // we'll have the following i4 output:
928 //
929 // expected output: [0x1, 0xA, 0xB, 0xC, 0xD, 0x6, 0x7, 0x8]
930 //
931 // Emulating the above using i8 will give:
932 //
933 // %compressed_mask = [1, 1, 1, 0] (4 * i1)
934 // %maskedload = [0x12, 0x34, 0x56, 0x00] (4 * i8)
935 // %bitcast = [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x0, 0x0] (8 * i4)
936 // %select_using_shifted_mask =
937 // [0x1, 0xA, 0xB, 0xC, 0xD, 0x6, 0x0, 0x0] (8 * i4)
938 // %packed_data = [0x1A, 0xBC, 0xD6, 0x00] (4 * i8)
939 //
940 // Using the compressed mask to store %packed_data results in expected
941 // output.
942 //
943 // FIXME: Make an example based on the comment above work (see #115460 for
944 // reproducer).
945 FailureOr<Operation *> newMask = getCompressedMaskOp(
946 rewriter, loc, op.getMask(), origElements, emulatedPerContainerElem);
947 if (failed(newMask))
948 return failure();
949
950 auto numElements = (origElements + emulatedPerContainerElem - 1) /
951 emulatedPerContainerElem;
952 auto newType = VectorType::get(numElements, containerElemTy);
953 auto passThru = arith::ConstantOp::create(rewriter, loc, newType,
954 rewriter.getZeroAttr(newType));
955
956 auto newLoad = vector::MaskedLoadOp::create(
957 rewriter, loc, newType, adaptor.getBase(), linearizedIndices,
958 newMask.value()->getResult(0), passThru);
959
960 auto newBitCastType =
961 VectorType::get(numElements * emulatedPerContainerElem, emulatedElemTy);
962 Value valueToStore =
963 vector::BitCastOp::create(rewriter, loc, newBitCastType, newLoad);
964 valueToStore = arith::SelectOp::create(rewriter, loc, op.getMask(),
965 op.getValueToStore(), valueToStore);
966 valueToStore =
967 vector::BitCastOp::create(rewriter, loc, newType, valueToStore);
968
969 rewriter.replaceOpWithNewOp<vector::MaskedStoreOp>(
970 op, adaptor.getBase(), linearizedIndices, newMask.value()->getResult(0),
971 valueToStore);
972 return success();
973 }
974};
975
976//===----------------------------------------------------------------------===//
977// ConvertVectorLoad
978//===----------------------------------------------------------------------===//
979
980/// Converts `vector.load` on narrow element types to work with
981/// wider, byte-aligned container types by adjusting load sizes and using
982/// bitcasting.
983///
984/// Example: `vector.load` of `vector<4xi4>` from `memref<3x4xi4>` is emulated
985/// by loading `vector<2xi8>` from the linearized `memref<6xi8>` (each `i8`
986/// container holds two `i4` values) and bitcasting back.
987///
988/// There are cases where the number of elements to load is not byte-aligned. In
989/// those cases, loads are converted to byte-aligned, byte-sized loads and the
990/// target vector is extracted from the loaded vector.
991struct ConvertVectorLoad final : OpConversionPattern<vector::LoadOp> {
992 using Base::Base;
993
994 LogicalResult
995 matchAndRewrite(vector::LoadOp op, OpAdaptor adaptor,
996 ConversionPatternRewriter &rewriter) const override {
997 // Prerequisite: memref in the vector.load op is flattened into 1-D.
998 if (op.getVectorType().getRank() != 1)
999 return rewriter.notifyMatchFailure(
1000 op, "Memref in emulated vector ops must be flattened beforehand.");
1001
1002 auto loc = op.getLoc();
1003 auto containerElemTy =
1004 cast<MemRefType>(adaptor.getBase().getType()).getElementType();
1005 Type emulatedElemTy = op.getType().getElementType();
1006 int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
1007 int containerBits = containerElemTy.getIntOrFloatBitWidth();
1008
1009 // Check per-element alignment.
1010 if (containerBits % emulatedBits != 0) {
1011 return rewriter.notifyMatchFailure(
1012 op, "impossible to pack emulated elements into container elements "
1013 "(bit-wise misalignment)");
1014 }
1015 int emulatedPerContainerElem = containerBits / emulatedBits;
1016
1017 // Adjust the number of elements to load when emulating narrow types,
1018 // and then cast back to the original type with vector.bitcast op.
1019 // For example, to emulate i4 to i8, the following op:
1020 //
1021 // %1 = vector.load %0[%c0, %c0] : memref<3x4xi4>, vector<4xi4>
1022 //
1023 // can be replaced with
1024 //
1025 // %1 = vector.load %0[%linear_index] : memref<6xi8>, vector<2xi8>
1026 // %2 = vector.bitcast %1 : vector<2xi8> to vector<4xi4>
1027 //
1028 // There are cases where the number of elements to load is not byte-aligned,
1029 // for example:
1030 //
1031 // %1 = vector.load %0[%c1, %c0] : memref<3x3xi2>, vector<3xi2>
1032 //
1033 // we will have to load extra bytes and extract the exact slice in between.
1034 //
1035 // %1 = vector.load %0[%c2] : memref<3xi8>, vector<2xi8>
1036 // %2 = vector.bitcast %1 : vector<2xi8> to vector<8xi2>
1037 // %3 = vector.extract_strided_slice %1 {offsets = [2], sizes = [3], strides
1038 // = [1]}
1039 // : vector<8xi2> to vector<3xi2>
1040 //
1041 // TODO: Currently the extract_strided_slice's attributes must be known at
1042 // compile time as they must be constants.
1043
1044 auto origElements = op.getVectorType().getNumElements();
1045 // Note, per-element-alignment was already verified above.
1046 bool isDivisibleInSize = origElements % emulatedPerContainerElem == 0;
1047
1048 auto stridedMetadata =
1049 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
1050
1051 OpFoldResult linearizedIndices;
1052 memref::LinearizedMemRefInfo linearizedInfo;
1053 std::tie(linearizedInfo, linearizedIndices) =
1055 rewriter, loc, emulatedBits, containerBits,
1056 stridedMetadata.getConstifiedMixedOffset(),
1057 stridedMetadata.getConstifiedMixedSizes(),
1058 stridedMetadata.getConstifiedMixedStrides(),
1059 getAsOpFoldResult(adaptor.getIndices()));
1060
1061 std::optional<int64_t> foldedIntraVectorOffset =
1062 isDivisibleInSize ? 0
1063 : getConstantIntValue(linearizedInfo.intraDataOffset);
1064
1065 // Always load enough elements which can cover the original elements.
1066 int64_t maxintraDataOffset =
1067 foldedIntraVectorOffset.value_or(emulatedPerContainerElem - 1);
1068 auto numElements = llvm::divideCeil(maxintraDataOffset + origElements,
1069 emulatedPerContainerElem);
1070 Value result =
1071 emulatedVectorLoad(rewriter, loc, adaptor.getBase(), linearizedIndices,
1072 numElements, emulatedElemTy, containerElemTy);
1073
1074 if (!foldedIntraVectorOffset) {
1075 auto resultVector = arith::ConstantOp::create(
1076 rewriter, loc, op.getType(), rewriter.getZeroAttr(op.getType()));
1078 rewriter, loc, dyn_cast<TypedValue<VectorType>>(result), resultVector,
1079 linearizedInfo.intraDataOffset, origElements);
1080 } else if (!isDivisibleInSize) {
1082 rewriter, loc, result, *foldedIntraVectorOffset, origElements);
1083 }
1084 rewriter.replaceOp(op, result);
1085 return success();
1086 }
1087};
1088
1089//===----------------------------------------------------------------------===//
1090// ConvertVectorMaskedLoad
1091//===----------------------------------------------------------------------===//
1092
1093/// Converts `vector.maskedload` operations on narrow element types to work with
1094/// wider, byte-aligned container types by adjusting the mask and using
1095/// bitcasting.
1096///
1097/// Example: Loading `vector<6xi4>` is emulated by loading `vector<3xi8>` and
1098/// bitcasting, since each `i8` container element holds two `i4` values.
1099struct ConvertVectorMaskedLoad final
1100 : OpConversionPattern<vector::MaskedLoadOp> {
1101 using Base::Base;
1102
1103 LogicalResult
1104 matchAndRewrite(vector::MaskedLoadOp op, OpAdaptor adaptor,
1105 ConversionPatternRewriter &rewriter) const override {
1106 if (op.getVectorType().getRank() != 1)
1107 return rewriter.notifyMatchFailure(
1108 op, "Memref in emulated vector ops must be flattened beforehand.");
1109
1110 auto loc = op.getLoc();
1111
1112 auto containerElemTy =
1113 cast<MemRefType>(adaptor.getBase().getType()).getElementType();
1114 Type emulatedElemTy = op.getType().getElementType();
1115 int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
1116 int containerBits = containerElemTy.getIntOrFloatBitWidth();
1117
1118 // Check per-element alignment.
1119 if (containerBits % emulatedBits != 0) {
1120 return rewriter.notifyMatchFailure(
1121 op, "impossible to pack emulated elements into container elements "
1122 "(bit-wise misalignment)");
1123 }
1124 int emulatedPerContainerElem = containerBits / emulatedBits;
1125
1126 // Adjust the number of elements to load when emulating narrow types,
1127 // and then cast back to the original type with vector.bitcast op.
1128 // For example, to emulate i4 to i8, the following op:
1129 //
1130 // %mask = vector.constant_mask [3] : vector<6xi1>
1131 // %1 = vector.maskedload %0[%c0, %c0], %mask, %pass_thru :
1132 // memref<3x6xi4>, vector<6xi1>, vector<6xi4> into vector<6xi4>
1133 //
1134 // can be replaced with
1135 //
1136 // %new_mask = vector.constant_mask [2] : vector<3xi1>
1137 // %new_pass_thru = vector.bitcast %pass_thru :
1138 // vector<6xi4> to vector<3xi8>
1139 // %1 = vector.maskedload %0[%linear_index], %new_mask, %new_pass_thru :
1140 // memref<9xi8>, vector<3xi1>, vector<3xi8> into vector<3xi8>
1141 // %2 = vector.bitcast %1 : vector<3xi8> to vector<6xi4>
1142 //
1143 // Since we are effectively loading 16 bits (2xi8) from the memref with the
1144 // new mask, while originally we only wanted to effectively load 12 bits
1145 // (3xi4) from the memref, we need to set the second half of the last i8
1146 // that was effectively loaded (i.e. the second i8) to %pass_thru.
1147 //
1148 // %3 = arith.select %mask, %2, %pass_thru : vector<6xi1>, vector<6xi4>
1149 //
1150 // Given these input values:
1151 // %mask = [1, 1, 1, 0, 0, 0]
1152 // %0[%c0, %c0] contains [0x1, 0x2, 0x3, 0x4, 0x5, 0x6]
1153 // %pass_thru = [0x7, 0x8, 0x9, 0xA, 0xB, 0xC]
1154 //
1155 // we'll have:
1156 //
1157 // expected output: [0x1, 0x2, 0x3, 0xA, 0xB, 0xC]
1158 //
1159 // %new_mask = [1, 1, 0]
1160 // %new_pass_thru = [0x78, 0x9A, 0xBC]
1161 // %1 = [0x12, 0x34, 0xBC]
1162 // %2 = [0x1, 0x2, 0x3, 0x4, 0xB, 0xC]
1163 // %3 = [0x1, 0x2, 0x3, 0xA, 0xB, 0xC]
1164 //
1165 // TODO: Currently, only the even number of elements loading is supported.
1166 // To deal with the odd number of elements, one has to extract the
1167 // subvector at the proper offset after bit-casting.
1168 auto origType = op.getVectorType();
1169 auto origElements = origType.getNumElements();
1170 // Note, per-element-alignment was already verified above.
1171 bool isDivisibleInSize = origElements % emulatedPerContainerElem == 0;
1172
1173 auto stridedMetadata =
1174 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
1175 OpFoldResult linearizedIndices;
1176 memref::LinearizedMemRefInfo linearizedInfo;
1177 std::tie(linearizedInfo, linearizedIndices) =
1179 rewriter, loc, emulatedBits, containerBits,
1180 stridedMetadata.getConstifiedMixedOffset(),
1181 stridedMetadata.getConstifiedMixedSizes(),
1182 stridedMetadata.getConstifiedMixedStrides(),
1183 getAsOpFoldResult(adaptor.getIndices()));
1184
1185 std::optional<int64_t> foldedIntraVectorOffset =
1186 isDivisibleInSize ? 0
1187 : getConstantIntValue(linearizedInfo.intraDataOffset);
1188
1189 int64_t maxIntraDataOffset =
1190 foldedIntraVectorOffset.value_or(emulatedPerContainerElem - 1);
1191 FailureOr<Operation *> newMask =
1192 getCompressedMaskOp(rewriter, loc, op.getMask(), origElements,
1193 emulatedPerContainerElem, maxIntraDataOffset);
1194 if (failed(newMask))
1195 return failure();
1196
1197 Value passthru = op.getPassThru();
1198
1199 auto numElements = llvm::divideCeil(maxIntraDataOffset + origElements,
1200 emulatedPerContainerElem);
1201 auto loadType = VectorType::get(numElements, containerElemTy);
1202 auto newBitcastType =
1203 VectorType::get(numElements * emulatedPerContainerElem, emulatedElemTy);
1204
1205 auto emptyVector = arith::ConstantOp::create(
1206 rewriter, loc, newBitcastType, rewriter.getZeroAttr(newBitcastType));
1207 if (!foldedIntraVectorOffset) {
1208 passthru = dynamicallyInsertSubVector(
1209 rewriter, loc, passthru, emptyVector, linearizedInfo.intraDataOffset,
1210 origElements);
1211 } else if (!isDivisibleInSize) {
1212 passthru = staticallyInsertSubvector(rewriter, loc, passthru, emptyVector,
1213 *foldedIntraVectorOffset);
1214 }
1215 auto newPassThru =
1216 vector::BitCastOp::create(rewriter, loc, loadType, passthru);
1217
1218 // Generating the new masked load.
1219 auto newLoad = vector::MaskedLoadOp::create(
1220 rewriter, loc, loadType, adaptor.getBase(),
1221 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices),
1222 newMask.value()->getResult(0), newPassThru);
1223
1224 // Setting the part that originally was not effectively loaded from memory
1225 // to pass through.
1226 auto bitCast =
1227 vector::BitCastOp::create(rewriter, loc, newBitcastType, newLoad);
1228
1229 Value mask = op.getMask();
1230 auto newSelectMaskType = VectorType::get(
1231 numElements * emulatedPerContainerElem, rewriter.getI1Type());
1232 // TODO: try to fold if op's mask is constant
1233 auto emptyMask =
1234 arith::ConstantOp::create(rewriter, loc, newSelectMaskType,
1235 rewriter.getZeroAttr(newSelectMaskType));
1236 if (!foldedIntraVectorOffset) {
1237 mask = dynamicallyInsertSubVector(rewriter, loc, mask, emptyMask,
1238 linearizedInfo.intraDataOffset,
1239 origElements);
1240 } else if (!isDivisibleInSize) {
1241 mask = staticallyInsertSubvector(rewriter, loc, op.getMask(), emptyMask,
1242 *foldedIntraVectorOffset);
1243 }
1244
1245 Value result =
1246 arith::SelectOp::create(rewriter, loc, mask, bitCast, passthru);
1247 if (!foldedIntraVectorOffset) {
1249 rewriter, loc, result, op.getPassThru(),
1250 linearizedInfo.intraDataOffset, origElements);
1251 } else if (!isDivisibleInSize) {
1253 rewriter, loc, result, *foldedIntraVectorOffset, origElements);
1254 }
1255 rewriter.replaceOp(op, result);
1256
1257 return success();
1258 }
1259};
1260
1261/// Check whether `subByteVecTy` fits wthin a vector of `multiByteScalarTy`
1262///
1263/// "Fitting" means that `subByteVecTy` (a vector of sub-byte elements, e.g.
1264/// vector<4xi4>), can fit within N scalar elements of type `multiByteScalarTy`
1265/// (a multi-byte scalar, e.g. i16), where N is some integer.
1266///
1267/// Put differently, this method checks whether this would be valid:
1268///
1269/// vector.bitcast subByteVecTy into vector<N x multiByteScalarTy>
1270///
1271/// EXAMPLES:
1272/// * vector<4xi4> -> i16 - yes (N = 1)
1273/// * vector<4xi4> -> i8 - yes (N = 2)
1274/// * vector<3xi4> -> i8 - no (N would have to be 1.5)
1275/// * vector<3xi2> -> i16 - no (N would have to be 0.5)
1276static bool fitsInMultiByteContainerTy(VectorType subByteVecTy,
1277 Type multiByteScalarTy) {
1278 assert((isa<IntegerType, FloatType>(multiByteScalarTy)) && "Not scalar!");
1279
1280 int subByteBits = subByteVecTy.getElementType().getIntOrFloatBitWidth();
1281 int multiByteBits = multiByteScalarTy.getIntOrFloatBitWidth();
1282
1283 assert(subByteBits < 8 && "Not a sub-byte scalar type!");
1284 assert(multiByteBits % 8 == 0 && "Not a multi-byte scalar type!");
1285 assert(multiByteBits % subByteBits == 0 && "Unalagined element types!");
1286
1287 int elemsPerMultiByte = multiByteBits / subByteBits;
1288
1289 return subByteVecTy.getShape().back() % elemsPerMultiByte == 0;
1290}
1291
1292//===----------------------------------------------------------------------===//
1293// ConvertVectorTransferRead
1294//===----------------------------------------------------------------------===//
1295
1296// TODO: Document-me
1297struct ConvertVectorTransferRead final
1298 : OpConversionPattern<vector::TransferReadOp> {
1299 using Base::Base;
1300
1301 LogicalResult
1302 matchAndRewrite(vector::TransferReadOp op, OpAdaptor adaptor,
1303 ConversionPatternRewriter &rewriter) const override {
1304 if (op.getMask() || op.isMasked())
1305 return rewriter.notifyMatchFailure(op, "masked reads are not supported");
1306
1307 // Prerequisites: memref in the vector.transfer_read op is flattened into
1308 // 1-D.
1309 if (op.getVectorType().getRank() != 1)
1310 return rewriter.notifyMatchFailure(
1311 op, "Memref in emulated vector ops must be flattened beforehand.");
1312
1313 auto loc = op.getLoc();
1314 auto containerElemTy =
1315 cast<MemRefType>(adaptor.getBase().getType()).getElementType();
1316 Type emulatedElemTy = op.getType().getElementType();
1317 int emulatedBits = emulatedElemTy.getIntOrFloatBitWidth();
1318 int containerBits = containerElemTy.getIntOrFloatBitWidth();
1319
1320 // Check per-element alignment.
1321 if (containerBits % emulatedBits != 0) {
1322 return rewriter.notifyMatchFailure(
1323 op, "impossible to pack emulated elements into container elements "
1324 "(bit-wise misalignment)");
1325 }
1326 int emulatedPerContainerElem = containerBits / emulatedBits;
1327
1328 auto origElements = op.getVectorType().getNumElements();
1329
1330 // Note, per-element-alignment was already verified above.
1331 bool isDivisibleInSize =
1332 fitsInMultiByteContainerTy(op.getVectorType(), containerElemTy);
1333
1334 // Pad the padding value with 0s on the left. These bits are discarded and
1335 // thus their values don't matter.
1336 Value padding = adaptor.getPadding();
1337 if (!padding.getType().isInteger()) {
1338 padding = arith::BitcastOp::create(
1339 rewriter, loc,
1340 IntegerType::get(rewriter.getContext(),
1341 padding.getType().getIntOrFloatBitWidth()),
1342 padding);
1343 }
1344 auto newPadding =
1345 arith::ExtUIOp::create(rewriter, loc, containerElemTy, padding);
1346
1347 auto stridedMetadata =
1348 memref::ExtractStridedMetadataOp::create(rewriter, loc, op.getBase());
1349
1350 OpFoldResult linearizedIndices;
1351 memref::LinearizedMemRefInfo linearizedInfo;
1352 std::tie(linearizedInfo, linearizedIndices) =
1354 rewriter, loc, emulatedBits, containerBits,
1355 stridedMetadata.getConstifiedMixedOffset(),
1356 stridedMetadata.getConstifiedMixedSizes(),
1357 stridedMetadata.getConstifiedMixedStrides(),
1358 getAsOpFoldResult(adaptor.getIndices()));
1359
1360 std::optional<int64_t> foldedIntraVectorOffset =
1361 isDivisibleInSize ? 0
1362 : getConstantIntValue(linearizedInfo.intraDataOffset);
1363
1364 int64_t maxIntraDataOffset =
1365 foldedIntraVectorOffset.value_or(emulatedPerContainerElem - 1);
1366 auto numElements = llvm::divideCeil(maxIntraDataOffset + origElements,
1367 emulatedPerContainerElem);
1368
1369 auto newRead = vector::TransferReadOp::create(
1370 rewriter, loc, VectorType::get(numElements, containerElemTy),
1371 adaptor.getBase(),
1372 getValueOrCreateConstantIndexOp(rewriter, loc, linearizedIndices),
1373 newPadding);
1374
1375 auto bitCast = vector::BitCastOp::create(
1376 rewriter, loc,
1377 VectorType::get(numElements * emulatedPerContainerElem, emulatedElemTy),
1378 newRead);
1379
1380 Value result = bitCast->getResult(0);
1381 if (!foldedIntraVectorOffset) {
1382 auto zeros = arith::ConstantOp::create(
1383 rewriter, loc, op.getType(), rewriter.getZeroAttr(op.getType()));
1384 result = dynamicallyExtractSubVector(rewriter, loc, bitCast, zeros,
1385 linearizedInfo.intraDataOffset,
1386 origElements);
1387 } else if (!isDivisibleInSize) {
1389 rewriter, loc, result, *foldedIntraVectorOffset, origElements);
1390 }
1391 rewriter.replaceOp(op, result);
1392
1393 return success();
1394 }
1395};
1396} // end anonymous namespace
1397
1398//===----------------------------------------------------------------------===//
1399// RewriteBitCastOfTruncI
1400//===----------------------------------------------------------------------===//
1401
1402namespace {
1403
1404/// Helper struct to keep track of the provenance of a contiguous set of bits
1405/// in a source vector.
1406struct SourceElementRange {
1407 /// The index of the source vector element that contributes bits to *this.
1408 int64_t sourceElementIdx;
1409 /// The range of bits in the source vector element that contribute to *this.
1410 int64_t sourceBitBegin;
1411 int64_t sourceBitEnd;
1412};
1413
1414struct SourceElementRangeList : public SmallVector<SourceElementRange> {
1415 /// Given the index of a SourceElementRange in the SourceElementRangeList,
1416 /// compute the amount of bits that need to be shifted to the left to get the
1417 /// bits in their final location. This shift amount is simply the sum of the
1418 /// bits *before* `shuffleIdx` (i.e. the bits of `shuffleIdx = 0` are always
1419 /// the LSBs, the bits of `shuffleIdx = ` come next, etc).
1420 int64_t computeLeftShiftAmount(int64_t shuffleIdx) const {
1421 int64_t res = 0;
1422 for (int64_t i = 0; i < shuffleIdx; ++i)
1423 res += (*this)[i].sourceBitEnd - (*this)[i].sourceBitBegin;
1424 return res;
1425 }
1426};
1427
1428/// Helper struct to enumerate the source elements and bit ranges that are
1429/// involved in a bitcast operation.
1430/// This allows rewriting a vector.bitcast into shuffles and bitwise ops for
1431/// any 1-D vector shape and any source/target bitwidths.
1432/// This creates and holds a mapping of the form:
1433/// [dstVectorElementJ] ==
1434/// [ {srcVectorElementX, bitRange}, {srcVectorElementY, bitRange}, ... ]
1435/// E.g. `vector.bitcast ... : vector<1xi24> to vector<3xi8>` is decomposed as:
1436/// [0] = {0, [0-8)}
1437/// [1] = {0, [8-16)}
1438/// [2] = {0, [16-24)}
1439/// and `vector.bitcast ... : vector<2xi15> to vector<3xi10>` is decomposed as:
1440/// [0] = {0, [0, 10)}, {1, [0, 5)}
1441/// [1] = {1, [5, 10)}, {2, [0, 10)}
1442struct BitCastBitsEnumerator {
1443 BitCastBitsEnumerator(VectorType sourceVectorType,
1444 VectorType targetVectorType);
1445
1446 int64_t getMaxNumberOfEntries() {
1447 int64_t numVectors = 0;
1448 for (const auto &l : sourceElementRanges)
1449 numVectors = std::max(numVectors, (int64_t)l.size());
1450 return numVectors;
1451 }
1452
1453 VectorType sourceVectorType;
1454 VectorType targetVectorType;
1455 SmallVector<SourceElementRangeList> sourceElementRanges;
1456};
1457
1458/// Rewrite vector.bitcast to a sequence of shuffles and bitwise ops that take
1459/// advantage of high-level information to avoid leaving LLVM to scramble with
1460/// peephole optimizations.
1461/// BitCastBitsEnumerator encodes for each element of the target vector the
1462/// provenance of the bits in the source vector. We can "transpose" this
1463/// information to build a sequence of shuffles and bitwise ops that will
1464/// produce the desired result.
1465//
1466/// Consider the following motivating example:
1467/// ```
1468/// %1 = vector.bitcast %0 : vector<32xi5> to vector<20xi8>
1469/// ```
1470//
1471/// BitCastBitsEnumerator contains the following information:
1472/// ```
1473/// { 0: b@[0..5) lshl: 0}{ 1: b@[0..3) lshl: 5}
1474/// { 1: b@[3..5) lshl: 0}{ 2: b@[0..5) lshl: 2}{ 3: b@[0..1) lshl: 7}
1475/// { 3: b@[1..5) lshl: 0}{ 4: b@[0..4) lshl: 4}
1476/// { 4: b@[4..5) lshl: 0}{ 5: b@[0..5) lshl: 1}{ 6: b@[0..2) lshl: 6}
1477/// { 6: b@[2..5) lshl: 0}{ 7: b@[0..5) lshl: 3}
1478/// { 8: b@[0..5) lshl: 0}{ 9: b@[0..3) lshl: 5}
1479/// { 9: b@[3..5) lshl: 0}{10: b@[0..5) lshl: 2}{11: b@[0..1) lshl: 7}
1480/// {11: b@[1..5) lshl: 0}{12: b@[0..4) lshl: 4}
1481/// {12: b@[4..5) lshl: 0}{13: b@[0..5) lshl: 1}{14: b@[0..2) lshl: 6}
1482/// {14: b@[2..5) lshl: 0}{15: b@[0..5) lshl: 3}
1483/// {16: b@[0..5) lshl: 0}{17: b@[0..3) lshl: 5}
1484/// {17: b@[3..5) lshl: 0}{18: b@[0..5) lshl: 2}{19: b@[0..1) lshl: 7}
1485/// {19: b@[1..5) lshl: 0}{20: b@[0..4) lshl: 4}
1486/// {20: b@[4..5) lshl: 0}{21: b@[0..5) lshl: 1}{22: b@[0..2) lshl: 6}
1487/// {22: b@[2..5) lshl: 0}{23: b@[0..5) lshl: 3}
1488/// {24: b@[0..5) lshl: 0}{25: b@[0..3) lshl: 5}
1489/// {25: b@[3..5) lshl: 0}{26: b@[0..5) lshl: 2}{27: b@[0..1) lshl: 7}
1490/// {27: b@[1..5) lshl: 0}{28: b@[0..4) lshl: 4}
1491/// {28: b@[4..5) lshl: 0}{29: b@[0..5) lshl: 1}{30: b@[0..2) lshl: 6}
1492/// {30: b@[2..5) lshl: 0}{31: b@[0..5) lshl: 3}
1493/// ```
1494///
1495/// In the above, each row represents one target vector element and each
1496/// column represents one bit contribution from a source vector element.
1497/// The algorithm creates vector.shuffle operations (in this case there are 3
1498/// shuffles (i.e. the max number of columns in BitCastBitsEnumerator). The
1499/// algorithm populates the bits as follows:
1500/// ```
1501/// src bits 0 ...
1502/// 1st shuffle |xxxxx |xx |...
1503/// 2nd shuffle | xxx| xxxxx |...
1504/// 3rd shuffle | | x|...
1505/// ```
1506//
1507/// The algorithm proceeds as follows:
1508/// 1. for each vector.shuffle, collect the source vectors that participate in
1509/// this shuffle. One source vector per target element of the resulting
1510/// vector.shuffle. If there is no source element contributing bits for the
1511/// current vector.shuffle, take 0 (i.e. row 0 in the above example has only
1512/// 2 columns).
1513/// 2. represent the bitrange in the source vector as a mask. If there is no
1514/// source element contributing bits for the current vector.shuffle, take 0.
1515/// 3. shift right by the proper amount to align the source bitrange at
1516/// position 0. This is exactly the low end of the bitrange. For instance,
1517/// the first element of row 2 is `{ 1: b@[3..5) lshl: 0}` and one needs to
1518/// shift right by 3 to get the bits contributed by the source element #1
1519/// into position 0.
1520/// 4. shift left by the proper amount to to align to the desired position in
1521/// the result element vector. For instance, the contribution of the second
1522/// source element for the first row needs to be shifted by `5` to form the
1523/// first i8 result element.
1524///
1525/// Eventually, we end up building the sequence
1526/// `(shuffle -> and -> shiftright -> shiftleft -> or)` to iteratively update
1527/// the result vector (i.e. the `shiftright -> shiftleft -> or` part) with the
1528/// bits extracted from the source vector (i.e. the `shuffle -> and` part).
1529struct BitCastRewriter {
1530 /// Helper metadata struct to hold the static quantities for the rewrite.
1531 struct Metadata {
1532 SmallVector<int64_t> shuffles;
1533 SmallVector<Attribute> masks, shiftRightAmounts, shiftLeftAmounts;
1534 };
1535
1536 BitCastRewriter(VectorType sourceVectorType, VectorType targetVectorType);
1537
1538 /// Verify that general preconditions for the rewrite are met.
1539 LogicalResult commonPrecondition(PatternRewriter &rewriter,
1540 VectorType preconditionType, Operation *op);
1541
1542 /// Precompute the metadata for the rewrite.
1543 SmallVector<BitCastRewriter::Metadata>
1544 precomputeMetadata(IntegerType shuffledElementType);
1545
1546 /// Rewrite one step of the sequence:
1547 /// `(shuffle -> and -> shiftright -> shiftleft -> or)`.
1548 Value genericRewriteStep(PatternRewriter &rewriter, Location loc,
1549 Value initialValue, Value runningResult,
1550 const BitCastRewriter::Metadata &metadata);
1551
1552private:
1553 /// Underlying enumerator that encodes the provenance of the bits in the each
1554 /// element of the result vector.
1555 BitCastBitsEnumerator enumerator;
1556};
1557
1558} // namespace
1559
1560[[maybe_unused]] static raw_ostream &
1562 for (const auto &l : vec) {
1563 for (auto it : llvm::enumerate(l)) {
1564 os << "{ " << it.value().sourceElementIdx << ": b@["
1565 << it.value().sourceBitBegin << ".." << it.value().sourceBitEnd
1566 << ") lshl: " << l.computeLeftShiftAmount(it.index()) << " } ";
1567 }
1568 os << "\n";
1569 }
1570 return os;
1571}
1572
1573BitCastBitsEnumerator::BitCastBitsEnumerator(VectorType sourceVectorType,
1574 VectorType targetVectorType)
1575 : sourceVectorType(sourceVectorType), targetVectorType(targetVectorType) {
1576
1577 assert(sourceVectorType.getRank() == 1 && !sourceVectorType.isScalable() &&
1578 "requires -D non-scalable vector type");
1579 assert(targetVectorType.getRank() == 1 && !targetVectorType.isScalable() &&
1580 "requires -D non-scalable vector type");
1581 int64_t sourceBitWidth = sourceVectorType.getElementTypeBitWidth();
1582 int64_t mostMinorSourceDim = sourceVectorType.getShape().back();
1583 LDBG() << "sourceVectorType: " << sourceVectorType;
1584
1585 int64_t targetBitWidth = targetVectorType.getElementTypeBitWidth();
1586 int64_t mostMinorTargetDim = targetVectorType.getShape().back();
1587 LDBG() << "targetVectorType: " << targetVectorType;
1588
1589 int64_t bitwidth = targetBitWidth * mostMinorTargetDim;
1590 (void)mostMinorSourceDim;
1591 assert(bitwidth == sourceBitWidth * mostMinorSourceDim &&
1592 "source and target bitwidths must match");
1593
1594 // Prepopulate one source element range per target element.
1595 sourceElementRanges = SmallVector<SourceElementRangeList>(mostMinorTargetDim);
1596 for (int64_t resultBit = 0; resultBit < bitwidth;) {
1597 int64_t resultElement = resultBit / targetBitWidth;
1598 int64_t resultBitInElement = resultBit % targetBitWidth;
1599 int64_t sourceElementIdx = resultBit / sourceBitWidth;
1600 int64_t sourceBitInElement = resultBit % sourceBitWidth;
1601 int64_t step = std::min(sourceBitWidth - sourceBitInElement,
1602 targetBitWidth - resultBitInElement);
1603 sourceElementRanges[resultElement].push_back(
1604 {sourceElementIdx, sourceBitInElement, sourceBitInElement + step});
1605 resultBit += step;
1606 }
1607}
1608
1609BitCastRewriter::BitCastRewriter(VectorType sourceVectorType,
1610 VectorType targetVectorType)
1611 : enumerator(BitCastBitsEnumerator(sourceVectorType, targetVectorType)) {
1612 LDBG() << "\n" << enumerator.sourceElementRanges;
1613}
1614
1615/// Verify that the precondition type meets the common preconditions for any
1616/// conversion.
1617static LogicalResult commonConversionPrecondition(PatternRewriter &rewriter,
1618 VectorType preconditionType,
1619 Operation *op) {
1620 if (!preconditionType || preconditionType.isScalable())
1621 return rewriter.notifyMatchFailure(op, "scalable vector");
1622
1623 // TODO: consider relaxing this restriction in the future if we find ways
1624 // to really work with subbyte elements across the MLIR/LLVM boundary.
1625 unsigned bitwidth = preconditionType.getElementTypeBitWidth();
1626 if (bitwidth % 8 != 0)
1627 return rewriter.notifyMatchFailure(op, "bitwidth is not k * 8");
1628
1629 return success();
1630}
1631
1632LogicalResult BitCastRewriter::commonPrecondition(PatternRewriter &rewriter,
1633 VectorType preconditionType,
1634 Operation *op) {
1635 if (!enumerator.sourceVectorType || !enumerator.targetVectorType)
1636 return rewriter.notifyMatchFailure(op, "types are not vector");
1637
1638 if (!preconditionType || preconditionType.getRank() != 1)
1639 return rewriter.notifyMatchFailure(op, "unsupported >1-D vector");
1640
1641 return commonConversionPrecondition(rewriter, preconditionType, op);
1642}
1643
1644/// Verify that `subByteVecTy` (vector) and `containerTy` (scalar) are aligned.
1645///
1646/// Alignment means that `subByteVecTy` can be packed into a vector of
1647/// `containerTy` elements. More specifically:
1648/// 1. The bit-width of `containerTy` is a multiple of the
1649/// bit-width of `subByteVecTy` elements. For example, for `i4` and `i16`
1650/// this multiple is 4.
1651/// 2. The multiple from 1. above divides evenly the number of the (trailing)
1652/// elements in `subByteVecTy`.
1653///
1654/// EXAMPLE 1:
1655/// `subByteVecTy = vector<2xi4>`, and
1656/// `containerTy = i16`
1657///
1658/// 2 divides evenly 4 ( = 16 / 4), hence both conditions are _met_.
1659///
1660/// EXAMPLE 2:
1661/// `subByteVecTy = vector<3xi4>`, and
1662/// `containerTy = i16`
1663///
1664/// 3 _does not_ divide evenly 4 (= 16/4), hence the conditions are _not met_.
1665///
1666/// EXAMPLE 3:
1667/// `subByteVecTy = vector<3xi3>`, and
1668/// `containerTy = i16`
1669///
1670/// 16 _is not_ a multiple of 3, hence the conditions are _not met_.
1671///
1672/// NOTE: This method assumes that common conversion preconditions are met. In
1673/// particular, `containerTy` is assumed to be a
1674/// multi-byte scalar type (e.g., i8, i16, i32).
1675static LogicalResult alignedConversionPrecondition(PatternRewriter &rewriter,
1676 VectorType subByteVecTy,
1677 Type containerTy,
1678 Operation *op) {
1679 assert(containerTy.isIntOrFloat() &&
1680 "container element type is not a scalar");
1681
1682 // TODO: This is validating the inputs rather than checking the conditions
1683 // documented above. Replace with an assert.
1684 if (!subByteVecTy)
1685 return rewriter.notifyMatchFailure(op, "not a vector!");
1686
1687 unsigned subByteBits = subByteVecTy.getElementTypeBitWidth();
1688 unsigned containerBits = containerTy.getIntOrFloatBitWidth();
1689
1690 // Enforced by the common pre-conditions.
1691 assert(containerBits % 8 == 0 && "Not a multi-byte scalar type!");
1692
1693 // TODO: Add support other widths (when/if needed)
1694 if (subByteBits != 2 && subByteBits != 4)
1695 return rewriter.notifyMatchFailure(
1696 op, "only 2-bit and 4-bit sub-byte type is supported at this moment");
1697
1698 // Condition 1 ("per-element" alignment)
1699 if (containerBits % subByteBits != 0)
1700 return rewriter.notifyMatchFailure(op, "unalagined element types");
1701
1702 // Condition 2 ("full" alignment)
1703 if (!fitsInMultiByteContainerTy(subByteVecTy, containerTy))
1704 return rewriter.notifyMatchFailure(
1705 op, "not possible to fit this sub-byte vector type into a vector of "
1706 "the given multi-byte type");
1707
1708 return success();
1709}
1710
1711SmallVector<BitCastRewriter::Metadata>
1712BitCastRewriter::precomputeMetadata(IntegerType shuffledElementType) {
1713 SmallVector<BitCastRewriter::Metadata> result;
1714 for (int64_t shuffleIdx = 0, e = enumerator.getMaxNumberOfEntries();
1715 shuffleIdx < e; ++shuffleIdx) {
1716 SmallVector<int64_t> shuffles;
1717 SmallVector<Attribute> masks, shiftRightAmounts, shiftLeftAmounts;
1718
1719 // Create the attribute quantities for the shuffle / mask / shift ops.
1720 for (auto &srcEltRangeList : enumerator.sourceElementRanges) {
1721 int64_t sourceElement = (shuffleIdx < (int64_t)srcEltRangeList.size())
1722 ? srcEltRangeList[shuffleIdx].sourceElementIdx
1723 : 0;
1724 shuffles.push_back(sourceElement);
1725
1726 int64_t bitLo = (shuffleIdx < (int64_t)srcEltRangeList.size())
1727 ? srcEltRangeList[shuffleIdx].sourceBitBegin
1728 : 0;
1729 int64_t bitHi = (shuffleIdx < (int64_t)srcEltRangeList.size())
1730 ? srcEltRangeList[shuffleIdx].sourceBitEnd
1731 : 0;
1732 IntegerAttr mask = IntegerAttr::get(
1733 shuffledElementType,
1734 llvm::APInt::getBitsSet(shuffledElementType.getIntOrFloatBitWidth(),
1735 bitLo, bitHi));
1736 masks.push_back(mask);
1737
1738 int64_t shiftRight = bitLo;
1739 shiftRightAmounts.push_back(
1740 IntegerAttr::get(shuffledElementType, shiftRight));
1741
1742 int64_t shiftLeft = srcEltRangeList.computeLeftShiftAmount(shuffleIdx);
1743 shiftLeftAmounts.push_back(
1744 IntegerAttr::get(shuffledElementType, shiftLeft));
1745 }
1746
1747 result.push_back({shuffles, masks, shiftRightAmounts, shiftLeftAmounts});
1748 }
1749 return result;
1750}
1751
1752Value BitCastRewriter::genericRewriteStep(
1753 PatternRewriter &rewriter, Location loc, Value initialValue,
1754 Value runningResult, const BitCastRewriter::Metadata &metadata) {
1755 // Create vector.shuffle from the metadata.
1756 auto shuffleOp = vector::ShuffleOp::create(rewriter, loc, initialValue,
1757 initialValue, metadata.shuffles);
1758
1759 // Intersect with the mask.
1760 VectorType shuffledVectorType = shuffleOp.getResultVectorType();
1761 auto constOp = arith::ConstantOp::create(
1762 rewriter, loc,
1763 DenseElementsAttr::get(shuffledVectorType, metadata.masks));
1764 Value andValue = arith::AndIOp::create(rewriter, loc, shuffleOp, constOp);
1765
1766 // Align right on 0.
1767 auto shiftRightConstantOp = arith::ConstantOp::create(
1768 rewriter, loc,
1769 DenseElementsAttr::get(shuffledVectorType, metadata.shiftRightAmounts));
1770 Value shiftedRight =
1771 arith::ShRUIOp::create(rewriter, loc, andValue, shiftRightConstantOp);
1772
1773 // Shift bits left into their final position.
1774 auto shiftLeftConstantOp = arith::ConstantOp::create(
1775 rewriter, loc,
1776 DenseElementsAttr::get(shuffledVectorType, metadata.shiftLeftAmounts));
1777 Value shiftedLeft =
1778 arith::ShLIOp::create(rewriter, loc, shiftedRight, shiftLeftConstantOp);
1779
1780 runningResult =
1781 runningResult
1782 ? arith::OrIOp::create(rewriter, loc, runningResult, shiftedLeft)
1783 : shiftedLeft;
1784
1785 return runningResult;
1786}
1787
1788/// Bitcasts the aligned `subByteVec` vector to a vector of i8.
1789/// Where aligned means it satisfies the alignedConversionPreconditions.
1790///
1791/// Example:
1792/// vector<16x16xi2> -> vector<16x4xi8>
1793/// vector<16x16xi4> -> vector<16x8xi8>
1795 Value subByteVec) {
1796 auto srcVecType = cast<VectorType>(subByteVec.getType());
1797 int64_t srcBitwidth = srcVecType.getElementType().getIntOrFloatBitWidth();
1798 assert(8 % srcBitwidth == 0 &&
1799 "Unsupported sub-byte type (not a divisor of i8)");
1800 int64_t numSrcElemsPerByte = 8 / srcBitwidth;
1801 SmallVector<int64_t> vecShape(srcVecType.getShape());
1802 // Adjust last dimension of the vector, so the total size remains the same.
1803 vecShape.back() = vecShape.back() / numSrcElemsPerByte;
1804 auto i8VecType = VectorType::get(vecShape, rewriter.getI8Type());
1805 return vector::BitCastOp::create(rewriter, loc, i8VecType, subByteVec);
1806}
1807
1808/// Extracts a signed N-bit sequence from each element of a vector of bytes,
1809/// starting at the specified bit index.
1810/// The `bitIdx` starts at 0 from the LSB and moves to the left.
1811///
1812/// Example for a single element:
1813/// Extract numBits=2 starting at bitIdx=2
1814/// src = [0 | 1 | 0 | 1 | 1 | 1 | 1 | 0]
1815/// indices = [7 | 6 | 5 | 4 | 3 | 2 | 1 | 0]
1816/// target = [. . . . ^ ^ . .]
1817///
1818/// The target sequence is [11](decimal=-1) as signed 2-bit integer.
1819/// So the result should be [11 11 11 11](decimal=-1) as signed 8-bit integer.
1820///
1821/// src = [01 01 11 10]
1822/// shl = arith.shl(src, 4) -> [11 10 00 00]
1823/// result = arith.shrsi(shl, 6) -> [11 11 11 11]
1825 Location loc, Value src,
1826 int bitIdx, int numBits) {
1827 auto srcType = cast<VectorType>(src.getType());
1828 Value shl = src;
1829 int8_t bitsToShiftLeft = 8 - numBits - bitIdx;
1830 assert(bitIdx >= 0 && bitsToShiftLeft >= 0 && numBits > 0 && numBits <= 8 &&
1831 "Invalid bitIdx range");
1832 if (bitsToShiftLeft != 0) {
1833 Value shiftLeftValues = arith::ConstantOp::create(
1834 rewriter, loc, DenseElementsAttr::get(srcType, bitsToShiftLeft));
1835 shl = arith::ShLIOp::create(rewriter, loc, src, shiftLeftValues);
1836 }
1837
1838 int8_t bitsToShiftRight = 8 - numBits;
1839 Value shiftRightValues = arith::ConstantOp::create(
1840 rewriter, loc, DenseElementsAttr::get(srcType, bitsToShiftRight));
1841 Value shr = arith::ShRSIOp::create(rewriter, loc, shl, shiftRightValues);
1842 return shr;
1843}
1844
1845/// Extracts an unsigned N-bit sequence from each element of a vector of bytes,
1846/// starting at the specified bit index.
1847/// The `bitIdx` starts at 0 from the LSB and moves to the left.
1848///
1849/// Example for a single element:
1850/// Extract numBits=2 starting at bitIdx=2
1851/// src = [0 | 1 | 0 | 1 | 1 | 0 | 1 | 0]
1852/// indices = [7 | 6 | 5 | 4 | 3 | 2 | 1 | 0]
1853/// target = [. . . . ^ ^ . .]
1854///
1855/// The target sequence is [10](decimal=2) as unsigned 2-bit integer.
1856/// So the result should be [00 00 00 10](decimal=2) as unsigned 8-bit integer.
1857///
1858/// src = [01 01 10 10]
1859/// mask = [00 00 00 11]
1860/// shr = arith.shrui(src, 2) = [00 01 01 10]
1861/// result = arith.andi(shr, mask) = [00 00 00 10]
1862/// NOTE: Similarly to extractNBitsPerByteAndSignExtendToI8, this could be
1863/// achieved by using arith::ShLIOp + arith::ShRUIOp instead of the masking.
1864/// However, by using arith::ShRUIOp + arith::AndIOp, we are eliminating shift
1865/// left when the index is 0.
1867 Location loc, Value src,
1868 int bitIdx, int numBits) {
1869 assert(bitIdx >= 0 && bitIdx <= 8 - numBits && numBits > 0 && numBits <= 8 &&
1870 "Invalid bitIdx range");
1871 auto srcType = cast<VectorType>(src.getType());
1872 int8_t bitsToShiftRight = bitIdx;
1873 Value shr = src;
1874 if (bitsToShiftRight != 0) {
1875 Value shiftRightValues = arith::ConstantOp::create(
1876 rewriter, loc, DenseElementsAttr::get(srcType, bitsToShiftRight));
1877 shr = arith::ShRUIOp::create(rewriter, loc, src, shiftRightValues);
1878 }
1879 if (bitIdx + numBits == 8) {
1880 return shr;
1881 }
1882 uint8_t lowBitsMask = (1 << numBits) - 1;
1883 Value lowBitsMaskValues = arith::ConstantOp::create(
1884 rewriter, loc, DenseElementsAttr::get(srcType, lowBitsMask));
1885 return arith::AndIOp::create(rewriter, loc, shr, lowBitsMaskValues);
1886}
1887
1889 std::function<Value(PatternRewriter &, Location, Value, int, int)>;
1890
1891/// Rewrite the i4 -> i8 extension into a sequence of shuffles and
1892/// bitwise ops to avoid leaving LLVM to scramble with peephole optimizations.
1894 Value srcValue, const ExtractNBitsFn &extFn) {
1895 [[maybe_unused]] auto srcVecType = cast<VectorType>(srcValue.getType());
1896 assert(srcVecType.getElementType().isSignlessInteger(4) &&
1897 "Expected i4 type");
1898
1899 // 1. Generate a bitcast vector<Xxi4> -> vector<X/2xi8>.
1900 Value i8Vector = bitcastSubByteVectorToI8(rewriter, loc, srcValue);
1901
1902 // 2. Extend i4 elements to i8 elements. Low i4 elemens of each
1903 // byte are place in one vector and the high i4 elements in another vector.
1904 Value low = extFn(rewriter, loc, i8Vector, 0, 4);
1905 Value high = extFn(rewriter, loc, i8Vector, 4, 4);
1906
1907 // 3. Interleave low and high i8 elements.
1908 return vector::InterleaveOp::create(rewriter, loc, low, high);
1909}
1910
1911/// Rewrite the i2 -> i8 extension into a sequence of shuffles and
1912/// bitwise ops to avoid leaving LLVM to scramble with peephole optimizations.
1914 Value srcValue, const ExtractNBitsFn &extFn) {
1915 [[maybe_unused]] VectorType srcVecType = cast<VectorType>(srcValue.getType());
1916 assert(srcVecType.getElementType().isSignlessInteger(2) &&
1917 "Expected i2 type");
1918
1919 // 1. Generate a bitcast vector<Xxi2> -> vector<X/2xi8>.
1920 Value i8Vector = bitcastSubByteVectorToI8(rewriter, loc, srcValue);
1921
1922 // 2. Extract each i2 element
1923 // Positon 0 (bits 0-1)
1924 Value vec0 = extFn(rewriter, loc, i8Vector, 0, 2);
1925 // Position 1 (bits 2-3)
1926 Value vec1 = extFn(rewriter, loc, i8Vector, 2, 2);
1927 // Position 2 (bits 4-5)
1928 Value vec2 = extFn(rewriter, loc, i8Vector, 4, 2);
1929 // Position 3 (bits 6-7)
1930 Value vec3 = extFn(rewriter, loc, i8Vector, 6, 2);
1931
1932 // 3. Interleave all 4 elements by first interleaving
1933 // even elements and then odd
1934 // vec0 = [0,0,0,0],...
1935 // vec1 = [1,1,1,1],...
1936 // vec2 = [2,2,2,2],...
1937 // vec3 = [3,3,3,3],...
1938 // 02 = [0,2,0,2,0,2,0,2],...
1939 // 13 = [1,3,1,3,1,3,1,3],...
1940 // 0213 = [0,1,2,3,...],...
1941 Value interleave02 = vector::InterleaveOp::create(rewriter, loc, vec0, vec2);
1942 Value interleave13 = vector::InterleaveOp::create(rewriter, loc, vec1, vec3);
1943 return vector::InterleaveOp::create(rewriter, loc, interleave02,
1944 interleave13);
1945}
1946
1947/// Rewrite the i8 -> i4 truncation into a deinterleave and series of bitwise
1948/// ops to avoid leaving LLVM to scramble with peephole optimizations.
1950 Value srcValue) {
1951 VectorType srcVecType = cast<VectorType>(srcValue.getType());
1952 assert(srcVecType.getElementType().isSignlessInteger(8) &&
1953 "Expected i8 type");
1954
1955 // 1. De-interleave low and high i8 elements.
1956 auto deinterleaveOp = vector::DeinterleaveOp::create(rewriter, loc, srcValue);
1957
1958 // 2. Zero out the upper side of each low i8 element.
1959 constexpr int8_t i8LowBitMask = 0x0F;
1960 VectorType deinterI8VecType = deinterleaveOp.getResultVectorType();
1961 Value zeroOutMask = arith::ConstantOp::create(
1962 rewriter, loc, DenseElementsAttr::get(deinterI8VecType, i8LowBitMask));
1963 Value zeroOutLow = arith::AndIOp::create(
1964 rewriter, loc, deinterleaveOp.getResultEven(), zeroOutMask);
1965
1966 // 3. Move high i4 values to upper side of the byte.
1967 constexpr int8_t bitsToShift = 4;
1968 auto shiftValues = arith::ConstantOp::create(
1969 rewriter, loc, DenseElementsAttr::get(deinterI8VecType, bitsToShift));
1970 Value shlHigh = arith::ShLIOp::create(
1971 rewriter, loc, deinterleaveOp.getResultOdd(), shiftValues);
1972
1973 // 4. Merge high and low i4 values.
1974 auto mergedHiLowOp = arith::OrIOp::create(rewriter, loc, zeroOutLow, shlHigh);
1975
1976 // 5. Generate a bitcast vector<Xxi8> -> vector<2Xxi4>.
1977 auto i4VecType = srcVecType.cloneWith(std::nullopt, rewriter.getI4Type());
1978 return vector::BitCastOp::create(rewriter, loc, i4VecType, mergedHiLowOp);
1979}
1980
1981/// Rewrite the i8 -> i2 truncation into two levels of deinterleave and a
1982/// series of bitwise ops to avoid leaving LLVM to scramble with peephole
1983/// optimizations.
1985 Value srcValue) {
1986 VectorType srcVecType = cast<VectorType>(srcValue.getType());
1987 assert(srcVecType.getElementType().isSignlessInteger(8) &&
1988 "Expected i8 type");
1989
1990 // 1. De-interleave twice to group the i8 elements by their position within
1991 // the destination byte.
1992 // in = [0,1,2,3,4,5,6,7]
1993 // evenOdd = [0,2,4,6], [1,3,5,7]
1994 // evenEvenOdd = [0,4], [2,6]
1995 // oddEvenOdd = [1,5], [3,7]
1996 auto evenOdd = vector::DeinterleaveOp::create(rewriter, loc, srcValue);
1997 auto evenEvenOdd =
1998 vector::DeinterleaveOp::create(rewriter, loc, evenOdd.getResultEven());
1999 auto oddEvenOdd =
2000 vector::DeinterleaveOp::create(rewriter, loc, evenOdd.getResultOdd());
2001
2002 // 2. Zero out the upper side of each i8 element, then move the low i2 to
2003 // its position in the byte. Position 3 needs no mask, the upper bits are
2004 // shifted out.
2005 VectorType deinterI8VecType = evenEvenOdd.getResultVectorType();
2006 constexpr int8_t i8LowBitMask = 0x03;
2007 Value zeroOutMask = arith::ConstantOp::create(
2008 rewriter, loc, DenseElementsAttr::get(deinterI8VecType, i8LowBitMask));
2009 auto shiftLeft = [&](Value value, int8_t bitsToShift) {
2010 Value shiftValues = arith::ConstantOp::create(
2011 rewriter, loc, DenseElementsAttr::get(deinterI8VecType, bitsToShift));
2012 return arith::ShLIOp::create(rewriter, loc, value, shiftValues);
2013 };
2014 Value elem0 = arith::AndIOp::create(rewriter, loc,
2015 evenEvenOdd.getResultEven(), zeroOutMask);
2016 Value elem1 =
2017 shiftLeft(arith::AndIOp::create(rewriter, loc, oddEvenOdd.getResultEven(),
2018 zeroOutMask),
2019 2);
2020 Value elem2 =
2021 shiftLeft(arith::AndIOp::create(rewriter, loc, evenEvenOdd.getResultOdd(),
2022 zeroOutMask),
2023 4);
2024 Value elem3 = shiftLeft(oddEvenOdd.getResultOdd(), 6);
2025
2026 // 3. Merge the four i2 values.
2027 Value merged = arith::OrIOp::create(rewriter, loc, elem0, elem1);
2028 merged = arith::OrIOp::create(rewriter, loc, merged, elem2);
2029 merged = arith::OrIOp::create(rewriter, loc, merged, elem3);
2030
2031 // 4. Generate a bitcast vector<Xxi8> -> vector<4Xxi2>.
2032 auto i2VecType = srcVecType.cloneWith(std::nullopt, rewriter.getI2Type());
2033 return vector::BitCastOp::create(rewriter, loc, i2VecType, merged);
2034}
2035
2036namespace {
2037/// Rewrite bitcast(trunci) to a sequence of shuffles and bitwise ops that take
2038/// advantage of high-level information to avoid leaving LLVM to scramble with
2039/// peephole optimizations.
2040struct RewriteBitCastOfTruncI : OpRewritePattern<vector::BitCastOp> {
2041 using Base::Base;
2042
2043 LogicalResult matchAndRewrite(vector::BitCastOp bitCastOp,
2044 PatternRewriter &rewriter) const override {
2045 // The source must be a trunc op.
2046 auto truncOp =
2047 bitCastOp.getSource().template getDefiningOp<arith::TruncIOp>();
2048 if (!truncOp)
2049 return rewriter.notifyMatchFailure(bitCastOp, "not a trunci source");
2050
2051 // Set up the BitCastRewriter and verify the precondition.
2052 VectorType sourceVectorType = bitCastOp.getSourceVectorType();
2053 VectorType targetVectorType = bitCastOp.getResultVectorType();
2054 BitCastRewriter bcr(sourceVectorType, targetVectorType);
2055 if (failed(bcr.commonPrecondition(rewriter, targetVectorType, bitCastOp)))
2056 return failure();
2057
2058 // Perform the rewrite.
2059 Value truncValue = truncOp.getIn();
2060 auto shuffledElementType =
2061 cast<IntegerType>(getElementTypeOrSelf(truncValue.getType()));
2062 Value runningResult;
2063 for (const BitCastRewriter ::Metadata &metadata :
2064 bcr.precomputeMetadata(shuffledElementType)) {
2065 runningResult = bcr.genericRewriteStep(
2066 rewriter, bitCastOp->getLoc(), truncValue, runningResult, metadata);
2067 }
2068
2069 // Finalize the rewrite.
2070 bool narrowing = targetVectorType.getElementTypeBitWidth() <=
2071 shuffledElementType.getIntOrFloatBitWidth();
2072 if (narrowing) {
2073 if (runningResult.getType() == bitCastOp.getResultVectorType()) {
2074 rewriter.replaceOp(bitCastOp, runningResult);
2075 } else {
2076 rewriter.replaceOpWithNewOp<arith::TruncIOp>(
2077 bitCastOp, bitCastOp.getResultVectorType(), runningResult);
2078 }
2079 } else {
2080 if (runningResult.getType() == bitCastOp.getResultVectorType()) {
2081 rewriter.replaceOp(bitCastOp, runningResult);
2082 } else {
2083 rewriter.replaceOpWithNewOp<arith::ExtUIOp>(
2084 bitCastOp, bitCastOp.getResultVectorType(), runningResult);
2085 }
2086 }
2087
2088 return success();
2089 }
2090};
2091} // namespace
2092
2093//===----------------------------------------------------------------------===//
2094// RewriteExtOfBitCast
2095//===----------------------------------------------------------------------===//
2096
2097namespace {
2098/// Rewrite ext{s,u}i(bitcast) to a sequence of shuffles and bitwise ops that
2099/// take advantage of high-level information to avoid leaving LLVM to scramble
2100/// with peephole optimizations.
2101template <typename ExtOpType>
2102struct RewriteExtOfBitCast : OpRewritePattern<ExtOpType> {
2103 using OpRewritePattern<ExtOpType>::OpRewritePattern;
2104
2105 RewriteExtOfBitCast(MLIRContext *context, PatternBenefit benefit)
2106 : OpRewritePattern<ExtOpType>(context, benefit) {}
2107
2108 LogicalResult matchAndRewrite(ExtOpType extOp,
2109 PatternRewriter &rewriter) const override {
2110 // The source must be a bitcast op.
2111 auto bitCastOp = extOp.getIn().template getDefiningOp<vector::BitCastOp>();
2112 if (!bitCastOp)
2113 return rewriter.notifyMatchFailure(extOp, "not a bitcast source");
2114
2115 // Set up the BitCastRewriter and verify the precondition.
2116 VectorType sourceVectorType = bitCastOp.getSourceVectorType();
2117 VectorType targetVectorType = bitCastOp.getResultVectorType();
2118 BitCastRewriter bcr(sourceVectorType, targetVectorType);
2119 if (failed(bcr.commonPrecondition(
2120 rewriter, cast<VectorType>(extOp.getOut().getType()), bitCastOp)))
2121 return failure();
2122
2123 // Perform the rewrite.
2124 Value runningResult;
2125 Value sourceValue = bitCastOp.getSource();
2126 auto shuffledElementType =
2127 cast<IntegerType>(getElementTypeOrSelf(sourceValue.getType()));
2128 for (const BitCastRewriter::Metadata &metadata :
2129 bcr.precomputeMetadata(shuffledElementType)) {
2130 runningResult = bcr.genericRewriteStep(
2131 rewriter, bitCastOp->getLoc(), sourceValue, runningResult, metadata);
2132 }
2133
2134 // Finalize the rewrite.
2135 bool narrowing =
2136 cast<VectorType>(extOp.getOut().getType()).getElementTypeBitWidth() <=
2137 shuffledElementType.getIntOrFloatBitWidth();
2138 if (narrowing) {
2139 rewriter.replaceOpWithNewOp<arith::TruncIOp>(
2140 extOp, cast<VectorType>(extOp.getOut().getType()), runningResult);
2141 } else {
2142 rewriter.replaceOpWithNewOp<ExtOpType>(
2143 extOp, cast<VectorType>(extOp.getOut().getType()), runningResult);
2144 }
2145
2146 return success();
2147 }
2148};
2149
2150/// Rewrite the i4 -> i8 part of any conversion into a sequence of shuffles and
2151/// bitwise ops that take advantage of high-level information to avoid leaving
2152/// LLVM to scramble with peephole optimizations. Templated to choose between
2153/// signed and unsigned conversions.
2154///
2155/// EXAMPLE 1 (signed):
2156/// arith.extsi %in : vector<8xi4> to vector<8xi32>
2157/// is rewriten as:
2158/// %0 = vector.bitcast %in : vector<8xi4> to vector<4xi8>
2159/// %1 = arith.shli %0, 4 : vector<4xi8>
2160/// %2 = arith.shrsi %1, 4 : vector<4xi8>
2161/// %3 = arith.shrsi %0, 4 : vector<4xi8>
2162/// %4 = vector.interleave %2, %3 : vector<4xi8> -> vector<8xi8>
2163/// %5 = arith.extsi %4 : vector<8xi8> to vector<8xi32>
2164///
2165/// EXAMPLE 2 (fp):
2166/// arith.sitofp %in : vector<8xi4> to vector<8xf32>
2167/// is rewriten as:
2168/// %0 = vector.bitcast %in : vector<8xi4> to vector<4xi8>
2169/// %1 = arith.shli %0, 4 : vector<4xi8>
2170/// %2 = arith.shrsi %1, 4 : vector<4xi8>
2171/// %3 = arith.shrsi %0, 4 : vector<4xi8>
2172/// %4 = vector.interleave %2, %3 : vector<4xi8> -> vector<8xi8>
2173/// %5 = arith.sitofp %4 : vector<8xi8> to vector<8xf32>
2174///
2175/// EXAMPLE 3 (unsigned):
2176/// arith.extui %in : vector<8xi4> to vector<8xi32>
2177/// is rewritten as:
2178/// %0 = vector.bitcast %in : vector<8xi4> to vector<4xi8>
2179/// %1 = arith.andi %0, 15 : vector<4xi8>
2180/// %2 = arith.shrui %0, 4 : vector<4xi8>
2181/// %3 = vector.interleave %1, %2 : vector<4xi8> -> vector<8xi8>
2182/// %4 = arith.extui %3 : vector<8xi8> to vector<8xi32>
2183///
2184template <typename ConversionOpType, bool isSigned>
2185struct RewriteAlignedSubByteIntExt : OpRewritePattern<ConversionOpType> {
2186 using OpRewritePattern<ConversionOpType>::OpRewritePattern;
2187
2188 LogicalResult matchAndRewrite(ConversionOpType conversionOp,
2189 PatternRewriter &rewriter) const override {
2190 // Verify the preconditions.
2191 Value srcValue = conversionOp.getIn();
2192 VectorType srcVecType = dyn_cast<VectorType>(srcValue.getType());
2193 VectorType dstVecType = dyn_cast<VectorType>(conversionOp.getType());
2194
2195 if (failed(
2196 commonConversionPrecondition(rewriter, dstVecType, conversionOp)))
2197 return failure();
2198
2199 // Check general alignment preconditions.
2201 rewriter, srcVecType,
2202 /*containerTy=*/rewriter.getI8Type(), conversionOp)))
2203 return failure();
2204
2205 // Perform the rewrite.
2206 Location loc = conversionOp.getLoc();
2207 const auto &extFn = isSigned ? extractNBitsPerByteAndSignExtendToI8
2209 Value subByteExt;
2210 switch (srcVecType.getElementType().getIntOrFloatBitWidth()) {
2211 case 2:
2212 subByteExt = rewriteI2ToI8Ext(rewriter, loc, srcValue, extFn);
2213 break;
2214 case 4:
2215 subByteExt = rewriteI4ToI8Ext(rewriter, loc, srcValue, extFn);
2216 break;
2217 default:
2218 return failure();
2219 }
2220
2221 // Finalize the rewrite. If subByteExt already has the destination type
2222 // (e.g. extsi i4->i8 where the container is i8), replace directly without
2223 // creating a new conversion op that would have identical src and dst types.
2224 if (subByteExt.getType() == conversionOp.getType())
2225 rewriter.replaceOp(conversionOp, subByteExt);
2226 else
2227 rewriter.replaceOpWithNewOp<ConversionOpType>(
2228 conversionOp, conversionOp.getType(), subByteExt);
2229 return success();
2230 }
2231};
2232
2233/// Rewrite the i8 -> i4 or i8 -> i2 part of any truncation into deinterleaves
2234/// and bitwise ops that take advantage of high-level information to avoid
2235/// leaving LLVM to scramble with peephole optimizations.
2236///
2237/// For example:
2238/// arith.trunci %in : vector<8xi32> to vector<8xi4>
2239///
2240/// is rewriten as:
2241///
2242/// %cst = arith.constant dense<15> : vector<4xi8>
2243/// %cst_0 = arith.constant dense<4> : vector<4xi8>
2244/// %0 = arith.trunci %in : vector<8xi32> to vector<8xi8>
2245/// %1, %2 = vector.deinterleave %0 : vector<8xi8> -> vector<4xi8>
2246/// %3 = arith.andi %1, %cst : vector<4xi8>
2247/// %4 = arith.shli %2, %cst_0 : vector<4xi8>
2248/// %5 = arith.ori %3, %4 : vector<4xi8>
2249/// %6 = vector.bitcast %5 : vector<4xi8> to vector<8xi4>
2250///
2251/// and, with four elements per byte,
2252/// arith.trunci %in : vector<4xi32> to vector<4xi2>
2253///
2254/// is rewritten as:
2255///
2256/// %cst = arith.constant dense<3> : vector<1xi8>
2257/// %cst_0 = arith.constant dense<2> : vector<1xi8>
2258/// %cst_1 = arith.constant dense<4> : vector<1xi8>
2259/// %cst_2 = arith.constant dense<6> : vector<1xi8>
2260/// %0 = arith.trunci %in : vector<4xi32> to vector<4xi8>
2261/// %1, %2 = vector.deinterleave %0 : vector<4xi8> -> vector<2xi8>
2262/// %3, %4 = vector.deinterleave %1 : vector<2xi8> -> vector<1xi8>
2263/// %5, %6 = vector.deinterleave %2 : vector<2xi8> -> vector<1xi8>
2264/// %7 = arith.andi %3, %cst : vector<1xi8>
2265/// %8 = arith.andi %5, %cst : vector<1xi8>
2266/// %9 = arith.shli %8, %cst_0 : vector<1xi8>
2267/// %10 = arith.andi %4, %cst : vector<1xi8>
2268/// %11 = arith.shli %10, %cst_1 : vector<1xi8>
2269/// %12 = arith.shli %6, %cst_2 : vector<1xi8>
2270/// %13 = arith.ori %7, %9 : vector<1xi8>
2271/// %14 = arith.ori %13, %11 : vector<1xi8>
2272/// %15 = arith.ori %14, %12 : vector<1xi8>
2273/// %16 = vector.bitcast %15 : vector<1xi8> to vector<4xi2>
2274///
2275struct RewriteAlignedSubByteIntTrunc : OpRewritePattern<arith::TruncIOp> {
2276 using Base::Base;
2277
2278 LogicalResult matchAndRewrite(arith::TruncIOp truncOp,
2279 PatternRewriter &rewriter) const override {
2280 // Verify the preconditions.
2281 Value srcValue = truncOp.getIn();
2282 auto srcVecType = dyn_cast<VectorType>(srcValue.getType());
2283 auto dstVecType = dyn_cast<VectorType>(truncOp.getType());
2284 if (!srcVecType || !dstVecType)
2285 return failure();
2286
2287 if (failed(commonConversionPrecondition(rewriter, srcVecType, truncOp)))
2288 return failure();
2289
2290 // Check general alignment preconditions. We invert the src/dst type order
2291 // to reuse the existing precondition logic.
2293 rewriter, dstVecType,
2294 /*containerTy=*/rewriter.getI8Type(), truncOp)))
2295 return failure();
2296
2297 // Create a new iX -> i8 truncation op, unless the source is already i8.
2298 Location loc = truncOp.getLoc();
2299 auto i8VecType = srcVecType.cloneWith(std::nullopt, rewriter.getI8Type());
2300 Value i8TruncVal =
2301 srcVecType == i8VecType
2302 ? srcValue
2303 : arith::TruncIOp::create(rewriter, loc, i8VecType, srcValue);
2304
2305 // Rewrite the i8 -> i4 / i8 -> i2 truncation part. The alignment
2306 // precondition above only admits these two widths.
2307 Value subByteTrunc = dstVecType.getElementTypeBitWidth() == 2
2308 ? rewriteI8ToI2Trunc(rewriter, loc, i8TruncVal)
2309 : rewriteI8ToI4Trunc(rewriter, loc, i8TruncVal);
2310
2311 // Finalize the rewrite.
2312 rewriter.replaceOp(truncOp, subByteTrunc);
2313 return success();
2314 }
2315};
2316
2317/// Rewrite a sub-byte vector transpose into a sequence of instructions that
2318/// perform the transpose on wider (byte) element types.
2319///
2320/// EXAMPLE:
2321/// %0 = vector.transpose %a, [1, 0] : vector<8x16xi4> to vector<16x8xi4>
2322///
2323/// is rewritten as:
2324///
2325/// %0 = arith.extsi %arg0 : vector<8x16xi4> to vector<8x16xi8>
2326/// %1 = vector.transpose %0, [1, 0] : vector<8x16xi8> to vector<16x8xi8>
2327/// %2 = arith.trunci %1 : vector<16x8xi8> to vector<16x8xi4>
2328///
2329struct RewriteVectorTranspose : OpRewritePattern<vector::TransposeOp> {
2330 using Base::Base;
2331
2332 RewriteVectorTranspose(MLIRContext *context, PatternBenefit benefit)
2333 : OpRewritePattern<vector::TransposeOp>(context, benefit) {}
2334
2335 LogicalResult matchAndRewrite(vector::TransposeOp transposeOp,
2336 PatternRewriter &rewriter) const override {
2337 // Precondition: sub-byte integer transpose.
2338 constexpr unsigned minNativeBitwidth = 8;
2339 VectorType srcSubByteVecType = transposeOp.getSourceVectorType();
2340 if (!srcSubByteVecType.getElementType().isSignlessInteger() ||
2341 srcSubByteVecType.getElementTypeBitWidth() >= minNativeBitwidth) {
2342 return rewriter.notifyMatchFailure(transposeOp,
2343 "not a sub-byte transpose");
2344 }
2345
2346 // Perform the rewrite.
2347 Location loc = transposeOp.getLoc();
2348 // Signed/unsigned interpretation shouldn't matter here as we are just
2349 // transposing the elements and truncating them back to the original size.
2350 // TODO: Use unsigned extension (more efficient) when emulation or backend
2351 // support is available.
2352 auto srcNativeVecType = srcSubByteVecType.cloneWith(
2353 std::nullopt, rewriter.getIntegerType(minNativeBitwidth));
2354 Value extOp = arith::ExtSIOp::create(rewriter, loc, srcNativeVecType,
2355 transposeOp.getVector());
2356 Value newTranspose = vector::TransposeOp::create(
2357 rewriter, loc, extOp, transposeOp.getPermutation());
2358 VectorType dstSubByteVecType = transposeOp.getResultVectorType();
2359 rewriter.replaceOpWithNewOp<arith::TruncIOp>(transposeOp, dstSubByteVecType,
2360 newTranspose);
2361 return success();
2362 }
2363};
2364
2365} // namespace
2366
2367//===----------------------------------------------------------------------===//
2368// Public Interface Definition
2369//===----------------------------------------------------------------------===//
2370
2371// The emulated type is inferred from the converted memref type.
2372void vector::populateVectorNarrowTypeEmulationPatterns(
2373 const arith::NarrowTypeEmulationConverter &typeConverter,
2374 RewritePatternSet &patterns, bool disableAtomicRMW, bool assumeAligned) {
2375 // Populate `vector.*` conversion patterns.
2376 // TODO: #119553 support atomicity
2377 patterns.add<ConvertVectorLoad, ConvertVectorMaskedLoad,
2378 ConvertVectorMaskedStore, ConvertVectorTransferRead>(
2379 typeConverter, patterns.getContext());
2380
2381 // Populate `vector.*` store conversion patterns. The caller can choose
2382 // to avoid emitting atomic operations and reduce it to read-modify-write
2383 // sequence for stores if it is known there are no thread contentions.
2384 patterns.insert<ConvertVectorStore>(patterns.getContext(), disableAtomicRMW,
2385 assumeAligned);
2386}
2387
2388void vector::populateVectorNarrowTypeRewritePatterns(
2389 RewritePatternSet &patterns, PatternBenefit benefit) {
2390 // TODO: Document what the emulated type is.
2391 patterns.add<RewriteBitCastOfTruncI, RewriteExtOfBitCast<arith::ExtUIOp>,
2392 RewriteExtOfBitCast<arith::ExtSIOp>>(patterns.getContext(),
2393 benefit);
2394
2395 // Patterns for aligned cases. We set higher priority as they are expected to
2396 // generate better performance for aligned cases.
2397 // The container type is always i8.
2398 patterns.add<RewriteAlignedSubByteIntExt<arith::ExtSIOp, /*isSigned=*/true>,
2399 RewriteAlignedSubByteIntExt<arith::SIToFPOp, /*isSigned=*/true>,
2400 RewriteAlignedSubByteIntTrunc>(patterns.getContext(),
2401 benefit.getBenefit() + 1);
2402 // The container type is always i8.
2403 patterns
2404 .add<RewriteAlignedSubByteIntExt<arith::ExtUIOp, /*isSigned=*/false>,
2405 RewriteAlignedSubByteIntExt<arith::UIToFPOp, /*isSigned=*/false>>(
2406 patterns.getContext(), benefit.getBenefit() + 1);
2407}
2408
2409// The container type is always i8.
2410void vector::populateVectorTransposeNarrowTypeRewritePatterns(
2411 RewritePatternSet &patterns, PatternBenefit benefit) {
2412 patterns.add<RewriteVectorTranspose>(patterns.getContext(), benefit);
2413}
2414
2415void vector::populateMemRefFlattenAndVectorNarrowTypeEmulationPatterns(
2416 arith::NarrowTypeEmulationConverter &typeConverter,
2417 RewritePatternSet &patterns) {
2419 vector::populateVectorNarrowTypeEmulationPatterns(typeConverter, patterns);
2420}
return success()
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:230
static Value extractSliceIntoByte(ConversionPatternRewriter &rewriter, Location loc, VectorValue vector, int64_t extractOffset, int64_t sliceNumElements, int64_t insertOffset)
Extract sliceNumElements from source vector at extractOffset, and insert it into an empty vector at i...
static Value rewriteI8ToI4Trunc(PatternRewriter &rewriter, Location loc, Value srcValue)
Rewrite the i8 -> i4 truncation into a deinterleave and series of bitwise ops to avoid leaving LLVM t...
std::function< Value(PatternRewriter &, Location, Value, int, int)> ExtractNBitsFn
TypedValue< MemRefType > MemRefValue
static VectorValue emulatedVectorLoad(OpBuilder &rewriter, Location loc, Value base, OpFoldResult linearizedIndices, int64_t numContainerElemsToLoad, Type emulatedElemTy, Type containerElemTy)
Emulate a vector load for emulatedElemTy using containerElemTy
TypedValue< VectorType > VectorValue
static FailureOr< Operation * > getCompressedMaskOp(OpBuilder &rewriter, Location loc, Value mask, int numSrcElems, int numSrcElemsPerDest, int numFrontPadElems=0)
Returns a compressed mask for the emulated vector.
static Value downcastSelectAndUpcast(OpBuilder &builder, Location loc, VectorType downcastType, VectorType upcastType, Value mask, Value trueValue, Value falseValue)
Downcast two values to downcastType, then select values based on mask, and casts the result to upcast...
static Value rewriteI4ToI8Ext(PatternRewriter &rewriter, Location loc, Value srcValue, const ExtractNBitsFn &extFn)
Rewrite the i4 -> i8 extension into a sequence of shuffles and bitwise ops to avoid leaving LLVM to s...
static Value dynamicallyInsertSubVector(RewriterBase &rewriter, Location loc, Value src, Value dest, OpFoldResult offset, int64_t numElemsToInsert)
Inserts 1-D subvector into a 1-D vector.
static Value staticallyInsertSubvector(OpBuilder &rewriter, Location loc, Value src, Value dest, int64_t offset)
Inserts 1-D subvector into a 1-D vector.
static void atomicRMW(OpBuilder &builder, Location loc, MemRefValue linearizedMemref, Value storeIdx, VectorValue valueToStore, Value mask)
Emits memref.generic_atomic_rmw op to store a subbyte-sized value to a byte in linearizedMemref,...
static Value staticallyExtractSubvector(OpBuilder &rewriter, Location loc, Value src, int64_t offset, int64_t numElemsToExtract)
Extracts 1-D subvector from a 1-D vector.
static LogicalResult commonConversionPrecondition(PatternRewriter &rewriter, VectorType preconditionType, Operation *op)
Verify that the precondition type meets the common preconditions for any conversion.
static Value dynamicallyExtractSubVector(OpBuilder &rewriter, Location loc, Value src, Value dest, OpFoldResult offset, int64_t numElemsToExtract)
Extracts 1-D subvector from a 1-D vector.
static LogicalResult alignedConversionPrecondition(PatternRewriter &rewriter, VectorType subByteVecTy, Type containerTy, Operation *op)
Verify that subByteVecTy (vector) and containerTy (scalar) are aligned.
static void nonAtomicRMW(OpBuilder &builder, Location loc, MemRefValue linearizedMemref, Value linearizedIndex, VectorValue valueToStore, Value mask)
Generate a non-atomic read-modify-write sequence for storing to the emulated type.
static Value bitcastSubByteVectorToI8(PatternRewriter &rewriter, Location loc, Value subByteVec)
Bitcasts the aligned subByteVec vector to a vector of i8.
static Value extractNBitsPerByteAndExtendToI8(PatternRewriter &rewriter, Location loc, Value src, int bitIdx, int numBits)
Extracts an unsigned N-bit sequence from each element of a vector of bytes, starting at the specified...
static Value rewriteI2ToI8Ext(PatternRewriter &rewriter, Location loc, Value srcValue, const ExtractNBitsFn &extFn)
Rewrite the i2 -> i8 extension into a sequence of shuffles and bitwise ops to avoid leaving LLVM to s...
static Value extractNBitsPerByteAndSignExtendToI8(PatternRewriter &rewriter, Location loc, Value src, int bitIdx, int numBits)
Extracts a signed N-bit sequence from each element of a vector of bytes, starting at the specified bi...
static Value rewriteI8ToI2Trunc(PatternRewriter &rewriter, Location loc, Value srcValue)
Rewrite the i8 -> i2 truncation into two levels of deinterleave and a series of bitwise ops to avoid ...
Base type for affine expression.
Definition AffineExpr.h:68
IntegerType getI2Type()
Definition Builders.cpp:63
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:75
IntegerType getI4Type()
Definition Builders.cpp:65
IntegerType getI1Type()
Definition Builders.cpp:61
MLIRContext * getContext() const
Definition Builders.h:56
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:290
IndexType getIndexType()
Definition Builders.cpp:59
IntegerType getI8Type()
Definition Builders.cpp:67
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:351
This class helps build Operations.
Definition Builders.h:210
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:434
This class represents a single result from folding an operation.
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
result_type_range getResultTypes()
Definition Operation.h:455
unsigned short getBenefit() const
If the corresponding pattern can match, return its benefit. If the.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:118
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
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
Type getType() const
Return the type of this value.
Definition Value.h:105
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:453
OpFoldResult makeComposedFoldedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands, bool composeAffineMin=false)
Constructs an AffineApplyOp that applies map to operands after composing the map with the maps of any...
std::pair< LinearizedMemRefInfo, OpFoldResult > getLinearizedMemRefOffsetAndSize(OpBuilder &builder, Location loc, int srcBits, int dstBits, OpFoldResult offset, ArrayRef< OpFoldResult > sizes, ArrayRef< OpFoldResult > strides, ArrayRef< OpFoldResult > indices={}, LinearizedDivKind sizeDivKind=LinearizedDivKind::Floor)
void populateFlattenMemrefsPatterns(RewritePatternSet &patterns)
Patterns for flattening all supported multi-dimensional memref operations into one-dimensional memref...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Value constantOne(OpBuilder &builder, Location loc, Type tp)
Generates a 1-valued constant of the given type.
Include the generated interface declarations.
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
Definition Value.h:496
void bindSymbols(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to SymbolExpr at positions: [0 .
Definition AffineExpr.h:327
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:147
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition Utils.cpp:114
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.