MLIR  20.0.0git
ConvertVectorToLLVM.cpp
Go to the documentation of this file.
1 //===- VectorToLLVM.cpp - Conversion from Vector to the LLVM dialect ------===//
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 
10 
26 #include "mlir/IR/BuiltinTypes.h"
27 #include "mlir/IR/TypeUtilities.h"
30 #include "llvm/ADT/APFloat.h"
31 #include "llvm/Support/Casting.h"
32 #include <optional>
33 
34 using namespace mlir;
35 using namespace mlir::vector;
36 
37 // Helper to reduce vector type by *all* but one rank at back.
38 static VectorType reducedVectorTypeBack(VectorType tp) {
39  assert((tp.getRank() > 1) && "unlowerable vector type");
40  return VectorType::get(tp.getShape().take_back(), tp.getElementType(),
41  tp.getScalableDims().take_back());
42 }
43 
44 // Helper that picks the proper sequence for inserting.
46  const LLVMTypeConverter &typeConverter, Location loc,
47  Value val1, Value val2, Type llvmType, int64_t rank,
48  int64_t pos) {
49  assert(rank > 0 && "0-D vector corner case should have been handled already");
50  if (rank == 1) {
51  auto idxType = rewriter.getIndexType();
52  auto constant = rewriter.create<LLVM::ConstantOp>(
53  loc, typeConverter.convertType(idxType),
54  rewriter.getIntegerAttr(idxType, pos));
55  return rewriter.create<LLVM::InsertElementOp>(loc, llvmType, val1, val2,
56  constant);
57  }
58  return rewriter.create<LLVM::InsertValueOp>(loc, val1, val2, pos);
59 }
60 
61 // Helper that picks the proper sequence for extracting.
63  const LLVMTypeConverter &typeConverter, Location loc,
64  Value val, Type llvmType, int64_t rank, int64_t pos) {
65  if (rank <= 1) {
66  auto idxType = rewriter.getIndexType();
67  auto constant = rewriter.create<LLVM::ConstantOp>(
68  loc, typeConverter.convertType(idxType),
69  rewriter.getIntegerAttr(idxType, pos));
70  return rewriter.create<LLVM::ExtractElementOp>(loc, llvmType, val,
71  constant);
72  }
73  return rewriter.create<LLVM::ExtractValueOp>(loc, val, pos);
74 }
75 
76 // Helper that returns data layout alignment of a memref.
77 LogicalResult getMemRefAlignment(const LLVMTypeConverter &typeConverter,
78  MemRefType memrefType, unsigned &align) {
79  Type elementTy = typeConverter.convertType(memrefType.getElementType());
80  if (!elementTy)
81  return failure();
82 
83  // TODO: this should use the MLIR data layout when it becomes available and
84  // stop depending on translation.
85  llvm::LLVMContext llvmContext;
86  align = LLVM::TypeToLLVMIRTranslator(llvmContext)
87  .getPreferredAlignment(elementTy, typeConverter.getDataLayout());
88  return success();
89 }
90 
91 // Check if the last stride is non-unit and has a valid memory space.
92 static LogicalResult isMemRefTypeSupported(MemRefType memRefType,
93  const LLVMTypeConverter &converter) {
94  if (!isLastMemrefDimUnitStride(memRefType))
95  return failure();
96  if (failed(converter.getMemRefAddressSpace(memRefType)))
97  return failure();
98  return success();
99 }
100 
101 // Add an index vector component to a base pointer.
103  const LLVMTypeConverter &typeConverter,
104  MemRefType memRefType, Value llvmMemref, Value base,
105  Value index, VectorType vectorType) {
106  assert(succeeded(isMemRefTypeSupported(memRefType, typeConverter)) &&
107  "unsupported memref type");
108  assert(vectorType.getRank() == 1 && "expected a 1-d vector type");
109  auto pType = MemRefDescriptor(llvmMemref).getElementPtrType();
110  auto ptrsType =
111  LLVM::getVectorType(pType, vectorType.getDimSize(0),
112  /*isScalable=*/vectorType.getScalableDims()[0]);
113  return rewriter.create<LLVM::GEPOp>(
114  loc, ptrsType, typeConverter.convertType(memRefType.getElementType()),
115  base, index);
116 }
117 
118 /// Convert `foldResult` into a Value. Integer attribute is converted to
119 /// an LLVM constant op.
120 static Value getAsLLVMValue(OpBuilder &builder, Location loc,
121  OpFoldResult foldResult) {
122  if (auto attr = foldResult.dyn_cast<Attribute>()) {
123  auto intAttr = cast<IntegerAttr>(attr);
124  return builder.create<LLVM::ConstantOp>(loc, intAttr).getResult();
125  }
126 
127  return foldResult.get<Value>();
128 }
129 
130 namespace {
131 
132 /// Trivial Vector to LLVM conversions
133 using VectorScaleOpConversion =
135 
136 /// Conversion pattern for a vector.bitcast.
137 class VectorBitCastOpConversion
138  : public ConvertOpToLLVMPattern<vector::BitCastOp> {
139 public:
141 
142  LogicalResult
143  matchAndRewrite(vector::BitCastOp bitCastOp, OpAdaptor adaptor,
144  ConversionPatternRewriter &rewriter) const override {
145  // Only 0-D and 1-D vectors can be lowered to LLVM.
146  VectorType resultTy = bitCastOp.getResultVectorType();
147  if (resultTy.getRank() > 1)
148  return failure();
149  Type newResultTy = typeConverter->convertType(resultTy);
150  rewriter.replaceOpWithNewOp<LLVM::BitcastOp>(bitCastOp, newResultTy,
151  adaptor.getOperands()[0]);
152  return success();
153  }
154 };
155 
156 /// Conversion pattern for a vector.matrix_multiply.
157 /// This is lowered directly to the proper llvm.intr.matrix.multiply.
158 class VectorMatmulOpConversion
159  : public ConvertOpToLLVMPattern<vector::MatmulOp> {
160 public:
162 
163  LogicalResult
164  matchAndRewrite(vector::MatmulOp matmulOp, OpAdaptor adaptor,
165  ConversionPatternRewriter &rewriter) const override {
166  rewriter.replaceOpWithNewOp<LLVM::MatrixMultiplyOp>(
167  matmulOp, typeConverter->convertType(matmulOp.getRes().getType()),
168  adaptor.getLhs(), adaptor.getRhs(), matmulOp.getLhsRows(),
169  matmulOp.getLhsColumns(), matmulOp.getRhsColumns());
170  return success();
171  }
172 };
173 
174 /// Conversion pattern for a vector.flat_transpose.
175 /// This is lowered directly to the proper llvm.intr.matrix.transpose.
176 class VectorFlatTransposeOpConversion
177  : public ConvertOpToLLVMPattern<vector::FlatTransposeOp> {
178 public:
180 
181  LogicalResult
182  matchAndRewrite(vector::FlatTransposeOp transOp, OpAdaptor adaptor,
183  ConversionPatternRewriter &rewriter) const override {
184  rewriter.replaceOpWithNewOp<LLVM::MatrixTransposeOp>(
185  transOp, typeConverter->convertType(transOp.getRes().getType()),
186  adaptor.getMatrix(), transOp.getRows(), transOp.getColumns());
187  return success();
188  }
189 };
190 
191 /// Overloaded utility that replaces a vector.load, vector.store,
192 /// vector.maskedload and vector.maskedstore with their respective LLVM
193 /// couterparts.
194 static void replaceLoadOrStoreOp(vector::LoadOp loadOp,
195  vector::LoadOpAdaptor adaptor,
196  VectorType vectorTy, Value ptr, unsigned align,
197  ConversionPatternRewriter &rewriter) {
198  rewriter.replaceOpWithNewOp<LLVM::LoadOp>(loadOp, vectorTy, ptr, align,
199  /*volatile_=*/false,
200  loadOp.getNontemporal());
201 }
202 
203 static void replaceLoadOrStoreOp(vector::MaskedLoadOp loadOp,
204  vector::MaskedLoadOpAdaptor adaptor,
205  VectorType vectorTy, Value ptr, unsigned align,
206  ConversionPatternRewriter &rewriter) {
207  rewriter.replaceOpWithNewOp<LLVM::MaskedLoadOp>(
208  loadOp, vectorTy, ptr, adaptor.getMask(), adaptor.getPassThru(), align);
209 }
210 
211 static void replaceLoadOrStoreOp(vector::StoreOp storeOp,
212  vector::StoreOpAdaptor adaptor,
213  VectorType vectorTy, Value ptr, unsigned align,
214  ConversionPatternRewriter &rewriter) {
215  rewriter.replaceOpWithNewOp<LLVM::StoreOp>(storeOp, adaptor.getValueToStore(),
216  ptr, align, /*volatile_=*/false,
217  storeOp.getNontemporal());
218 }
219 
220 static void replaceLoadOrStoreOp(vector::MaskedStoreOp storeOp,
221  vector::MaskedStoreOpAdaptor adaptor,
222  VectorType vectorTy, Value ptr, unsigned align,
223  ConversionPatternRewriter &rewriter) {
224  rewriter.replaceOpWithNewOp<LLVM::MaskedStoreOp>(
225  storeOp, adaptor.getValueToStore(), ptr, adaptor.getMask(), align);
226 }
227 
228 /// Conversion pattern for a vector.load, vector.store, vector.maskedload, and
229 /// vector.maskedstore.
230 template <class LoadOrStoreOp>
231 class VectorLoadStoreConversion : public ConvertOpToLLVMPattern<LoadOrStoreOp> {
232 public:
234 
235  LogicalResult
236  matchAndRewrite(LoadOrStoreOp loadOrStoreOp,
237  typename LoadOrStoreOp::Adaptor adaptor,
238  ConversionPatternRewriter &rewriter) const override {
239  // Only 1-D vectors can be lowered to LLVM.
240  VectorType vectorTy = loadOrStoreOp.getVectorType();
241  if (vectorTy.getRank() > 1)
242  return failure();
243 
244  auto loc = loadOrStoreOp->getLoc();
245  MemRefType memRefTy = loadOrStoreOp.getMemRefType();
246 
247  // Resolve alignment.
248  unsigned align;
249  if (failed(getMemRefAlignment(*this->getTypeConverter(), memRefTy, align)))
250  return failure();
251 
252  // Resolve address.
253  auto vtype = cast<VectorType>(
254  this->typeConverter->convertType(loadOrStoreOp.getVectorType()));
255  Value dataPtr = this->getStridedElementPtr(loc, memRefTy, adaptor.getBase(),
256  adaptor.getIndices(), rewriter);
257  replaceLoadOrStoreOp(loadOrStoreOp, adaptor, vtype, dataPtr, align,
258  rewriter);
259  return success();
260  }
261 };
262 
263 /// Conversion pattern for a vector.gather.
264 class VectorGatherOpConversion
265  : public ConvertOpToLLVMPattern<vector::GatherOp> {
266 public:
268 
269  LogicalResult
270  matchAndRewrite(vector::GatherOp gather, OpAdaptor adaptor,
271  ConversionPatternRewriter &rewriter) const override {
272  MemRefType memRefType = dyn_cast<MemRefType>(gather.getBaseType());
273  assert(memRefType && "The base should be bufferized");
274 
275  if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
276  return failure();
277 
278  auto loc = gather->getLoc();
279 
280  // Resolve alignment.
281  unsigned align;
282  if (failed(getMemRefAlignment(*getTypeConverter(), memRefType, align)))
283  return failure();
284 
285  Value ptr = getStridedElementPtr(loc, memRefType, adaptor.getBase(),
286  adaptor.getIndices(), rewriter);
287  Value base = adaptor.getBase();
288 
289  auto llvmNDVectorTy = adaptor.getIndexVec().getType();
290  // Handle the simple case of 1-D vector.
291  if (!isa<LLVM::LLVMArrayType>(llvmNDVectorTy)) {
292  auto vType = gather.getVectorType();
293  // Resolve address.
294  Value ptrs =
295  getIndexedPtrs(rewriter, loc, *this->getTypeConverter(), memRefType,
296  base, ptr, adaptor.getIndexVec(), vType);
297  // Replace with the gather intrinsic.
298  rewriter.replaceOpWithNewOp<LLVM::masked_gather>(
299  gather, typeConverter->convertType(vType), ptrs, adaptor.getMask(),
300  adaptor.getPassThru(), rewriter.getI32IntegerAttr(align));
301  return success();
302  }
303 
304  const LLVMTypeConverter &typeConverter = *this->getTypeConverter();
305  auto callback = [align, memRefType, base, ptr, loc, &rewriter,
306  &typeConverter](Type llvm1DVectorTy,
307  ValueRange vectorOperands) {
308  // Resolve address.
309  Value ptrs = getIndexedPtrs(
310  rewriter, loc, typeConverter, memRefType, base, ptr,
311  /*index=*/vectorOperands[0], cast<VectorType>(llvm1DVectorTy));
312  // Create the gather intrinsic.
313  return rewriter.create<LLVM::masked_gather>(
314  loc, llvm1DVectorTy, ptrs, /*mask=*/vectorOperands[1],
315  /*passThru=*/vectorOperands[2], rewriter.getI32IntegerAttr(align));
316  };
317  SmallVector<Value> vectorOperands = {
318  adaptor.getIndexVec(), adaptor.getMask(), adaptor.getPassThru()};
320  gather, vectorOperands, *getTypeConverter(), callback, rewriter);
321  }
322 };
323 
324 /// Conversion pattern for a vector.scatter.
325 class VectorScatterOpConversion
326  : public ConvertOpToLLVMPattern<vector::ScatterOp> {
327 public:
329 
330  LogicalResult
331  matchAndRewrite(vector::ScatterOp scatter, OpAdaptor adaptor,
332  ConversionPatternRewriter &rewriter) const override {
333  auto loc = scatter->getLoc();
334  MemRefType memRefType = scatter.getMemRefType();
335 
336  if (failed(isMemRefTypeSupported(memRefType, *this->getTypeConverter())))
337  return failure();
338 
339  // Resolve alignment.
340  unsigned align;
341  if (failed(getMemRefAlignment(*getTypeConverter(), memRefType, align)))
342  return failure();
343 
344  // Resolve address.
345  VectorType vType = scatter.getVectorType();
346  Value ptr = getStridedElementPtr(loc, memRefType, adaptor.getBase(),
347  adaptor.getIndices(), rewriter);
348  Value ptrs =
349  getIndexedPtrs(rewriter, loc, *this->getTypeConverter(), memRefType,
350  adaptor.getBase(), ptr, adaptor.getIndexVec(), vType);
351 
352  // Replace with the scatter intrinsic.
353  rewriter.replaceOpWithNewOp<LLVM::masked_scatter>(
354  scatter, adaptor.getValueToStore(), ptrs, adaptor.getMask(),
355  rewriter.getI32IntegerAttr(align));
356  return success();
357  }
358 };
359 
360 /// Conversion pattern for a vector.expandload.
361 class VectorExpandLoadOpConversion
362  : public ConvertOpToLLVMPattern<vector::ExpandLoadOp> {
363 public:
365 
366  LogicalResult
367  matchAndRewrite(vector::ExpandLoadOp expand, OpAdaptor adaptor,
368  ConversionPatternRewriter &rewriter) const override {
369  auto loc = expand->getLoc();
370  MemRefType memRefType = expand.getMemRefType();
371 
372  // Resolve address.
373  auto vtype = typeConverter->convertType(expand.getVectorType());
374  Value ptr = getStridedElementPtr(loc, memRefType, adaptor.getBase(),
375  adaptor.getIndices(), rewriter);
376 
377  rewriter.replaceOpWithNewOp<LLVM::masked_expandload>(
378  expand, vtype, ptr, adaptor.getMask(), adaptor.getPassThru());
379  return success();
380  }
381 };
382 
383 /// Conversion pattern for a vector.compressstore.
384 class VectorCompressStoreOpConversion
385  : public ConvertOpToLLVMPattern<vector::CompressStoreOp> {
386 public:
388 
389  LogicalResult
390  matchAndRewrite(vector::CompressStoreOp compress, OpAdaptor adaptor,
391  ConversionPatternRewriter &rewriter) const override {
392  auto loc = compress->getLoc();
393  MemRefType memRefType = compress.getMemRefType();
394 
395  // Resolve address.
396  Value ptr = getStridedElementPtr(loc, memRefType, adaptor.getBase(),
397  adaptor.getIndices(), rewriter);
398 
399  rewriter.replaceOpWithNewOp<LLVM::masked_compressstore>(
400  compress, adaptor.getValueToStore(), ptr, adaptor.getMask());
401  return success();
402  }
403 };
404 
405 /// Reduction neutral classes for overloading.
406 class ReductionNeutralZero {};
407 class ReductionNeutralIntOne {};
408 class ReductionNeutralFPOne {};
409 class ReductionNeutralAllOnes {};
410 class ReductionNeutralSIntMin {};
411 class ReductionNeutralUIntMin {};
412 class ReductionNeutralSIntMax {};
413 class ReductionNeutralUIntMax {};
414 class ReductionNeutralFPMin {};
415 class ReductionNeutralFPMax {};
416 
417 /// Create the reduction neutral zero value.
418 static Value createReductionNeutralValue(ReductionNeutralZero neutral,
419  ConversionPatternRewriter &rewriter,
420  Location loc, Type llvmType) {
421  return rewriter.create<LLVM::ConstantOp>(loc, llvmType,
422  rewriter.getZeroAttr(llvmType));
423 }
424 
425 /// Create the reduction neutral integer one value.
426 static Value createReductionNeutralValue(ReductionNeutralIntOne neutral,
427  ConversionPatternRewriter &rewriter,
428  Location loc, Type llvmType) {
429  return rewriter.create<LLVM::ConstantOp>(
430  loc, llvmType, rewriter.getIntegerAttr(llvmType, 1));
431 }
432 
433 /// Create the reduction neutral fp one value.
434 static Value createReductionNeutralValue(ReductionNeutralFPOne neutral,
435  ConversionPatternRewriter &rewriter,
436  Location loc, Type llvmType) {
437  return rewriter.create<LLVM::ConstantOp>(
438  loc, llvmType, rewriter.getFloatAttr(llvmType, 1.0));
439 }
440 
441 /// Create the reduction neutral all-ones value.
442 static Value createReductionNeutralValue(ReductionNeutralAllOnes neutral,
443  ConversionPatternRewriter &rewriter,
444  Location loc, Type llvmType) {
445  return rewriter.create<LLVM::ConstantOp>(
446  loc, llvmType,
447  rewriter.getIntegerAttr(
448  llvmType, llvm::APInt::getAllOnes(llvmType.getIntOrFloatBitWidth())));
449 }
450 
451 /// Create the reduction neutral signed int minimum value.
452 static Value createReductionNeutralValue(ReductionNeutralSIntMin neutral,
453  ConversionPatternRewriter &rewriter,
454  Location loc, Type llvmType) {
455  return rewriter.create<LLVM::ConstantOp>(
456  loc, llvmType,
457  rewriter.getIntegerAttr(llvmType, llvm::APInt::getSignedMinValue(
458  llvmType.getIntOrFloatBitWidth())));
459 }
460 
461 /// Create the reduction neutral unsigned int minimum value.
462 static Value createReductionNeutralValue(ReductionNeutralUIntMin neutral,
463  ConversionPatternRewriter &rewriter,
464  Location loc, Type llvmType) {
465  return rewriter.create<LLVM::ConstantOp>(
466  loc, llvmType,
467  rewriter.getIntegerAttr(llvmType, llvm::APInt::getMinValue(
468  llvmType.getIntOrFloatBitWidth())));
469 }
470 
471 /// Create the reduction neutral signed int maximum value.
472 static Value createReductionNeutralValue(ReductionNeutralSIntMax neutral,
473  ConversionPatternRewriter &rewriter,
474  Location loc, Type llvmType) {
475  return rewriter.create<LLVM::ConstantOp>(
476  loc, llvmType,
477  rewriter.getIntegerAttr(llvmType, llvm::APInt::getSignedMaxValue(
478  llvmType.getIntOrFloatBitWidth())));
479 }
480 
481 /// Create the reduction neutral unsigned int maximum value.
482 static Value createReductionNeutralValue(ReductionNeutralUIntMax neutral,
483  ConversionPatternRewriter &rewriter,
484  Location loc, Type llvmType) {
485  return rewriter.create<LLVM::ConstantOp>(
486  loc, llvmType,
487  rewriter.getIntegerAttr(llvmType, llvm::APInt::getMaxValue(
488  llvmType.getIntOrFloatBitWidth())));
489 }
490 
491 /// Create the reduction neutral fp minimum value.
492 static Value createReductionNeutralValue(ReductionNeutralFPMin neutral,
493  ConversionPatternRewriter &rewriter,
494  Location loc, Type llvmType) {
495  auto floatType = cast<FloatType>(llvmType);
496  return rewriter.create<LLVM::ConstantOp>(
497  loc, llvmType,
498  rewriter.getFloatAttr(
499  llvmType, llvm::APFloat::getQNaN(floatType.getFloatSemantics(),
500  /*Negative=*/false)));
501 }
502 
503 /// Create the reduction neutral fp maximum value.
504 static Value createReductionNeutralValue(ReductionNeutralFPMax neutral,
505  ConversionPatternRewriter &rewriter,
506  Location loc, Type llvmType) {
507  auto floatType = cast<FloatType>(llvmType);
508  return rewriter.create<LLVM::ConstantOp>(
509  loc, llvmType,
510  rewriter.getFloatAttr(
511  llvmType, llvm::APFloat::getQNaN(floatType.getFloatSemantics(),
512  /*Negative=*/true)));
513 }
514 
515 /// Returns `accumulator` if it has a valid value. Otherwise, creates and
516 /// returns a new accumulator value using `ReductionNeutral`.
517 template <class ReductionNeutral>
518 static Value getOrCreateAccumulator(ConversionPatternRewriter &rewriter,
519  Location loc, Type llvmType,
520  Value accumulator) {
521  if (accumulator)
522  return accumulator;
523 
524  return createReductionNeutralValue(ReductionNeutral(), rewriter, loc,
525  llvmType);
526 }
527 
528 /// Creates a value with the 1-D vector shape provided in `llvmType`.
529 /// This is used as effective vector length by some intrinsics supporting
530 /// dynamic vector lengths at runtime.
531 static Value createVectorLengthValue(ConversionPatternRewriter &rewriter,
532  Location loc, Type llvmType) {
533  VectorType vType = cast<VectorType>(llvmType);
534  auto vShape = vType.getShape();
535  assert(vShape.size() == 1 && "Unexpected multi-dim vector type");
536 
537  Value baseVecLength = rewriter.create<LLVM::ConstantOp>(
538  loc, rewriter.getI32Type(),
539  rewriter.getIntegerAttr(rewriter.getI32Type(), vShape[0]));
540 
541  if (!vType.getScalableDims()[0])
542  return baseVecLength;
543 
544  // For a scalable vector type, create and return `vScale * baseVecLength`.
545  Value vScale = rewriter.create<vector::VectorScaleOp>(loc);
546  vScale =
547  rewriter.create<arith::IndexCastOp>(loc, rewriter.getI32Type(), vScale);
548  Value scalableVecLength =
549  rewriter.create<arith::MulIOp>(loc, baseVecLength, vScale);
550  return scalableVecLength;
551 }
552 
553 /// Helper method to lower a `vector.reduction` op that performs an arithmetic
554 /// operation like add,mul, etc.. `VectorOp` is the LLVM vector intrinsic to use
555 /// and `ScalarOp` is the scalar operation used to add the accumulation value if
556 /// non-null.
557 template <class LLVMRedIntrinOp, class ScalarOp>
558 static Value createIntegerReductionArithmeticOpLowering(
559  ConversionPatternRewriter &rewriter, Location loc, Type llvmType,
560  Value vectorOperand, Value accumulator) {
561 
562  Value result = rewriter.create<LLVMRedIntrinOp>(loc, llvmType, vectorOperand);
563 
564  if (accumulator)
565  result = rewriter.create<ScalarOp>(loc, accumulator, result);
566  return result;
567 }
568 
569 /// Helper method to lower a `vector.reduction` operation that performs
570 /// a comparison operation like `min`/`max`. `VectorOp` is the LLVM vector
571 /// intrinsic to use and `predicate` is the predicate to use to compare+combine
572 /// the accumulator value if non-null.
573 template <class LLVMRedIntrinOp>
574 static Value createIntegerReductionComparisonOpLowering(
575  ConversionPatternRewriter &rewriter, Location loc, Type llvmType,
576  Value vectorOperand, Value accumulator, LLVM::ICmpPredicate predicate) {
577  Value result = rewriter.create<LLVMRedIntrinOp>(loc, llvmType, vectorOperand);
578  if (accumulator) {
579  Value cmp =
580  rewriter.create<LLVM::ICmpOp>(loc, predicate, accumulator, result);
581  result = rewriter.create<LLVM::SelectOp>(loc, cmp, accumulator, result);
582  }
583  return result;
584 }
585 
586 namespace {
587 template <typename Source>
588 struct VectorToScalarMapper;
589 template <>
590 struct VectorToScalarMapper<LLVM::vector_reduce_fmaximum> {
591  using Type = LLVM::MaximumOp;
592 };
593 template <>
594 struct VectorToScalarMapper<LLVM::vector_reduce_fminimum> {
595  using Type = LLVM::MinimumOp;
596 };
597 template <>
598 struct VectorToScalarMapper<LLVM::vector_reduce_fmax> {
599  using Type = LLVM::MaxNumOp;
600 };
601 template <>
602 struct VectorToScalarMapper<LLVM::vector_reduce_fmin> {
603  using Type = LLVM::MinNumOp;
604 };
605 } // namespace
606 
607 template <class LLVMRedIntrinOp>
608 static Value createFPReductionComparisonOpLowering(
609  ConversionPatternRewriter &rewriter, Location loc, Type llvmType,
610  Value vectorOperand, Value accumulator, LLVM::FastmathFlagsAttr fmf) {
611  Value result =
612  rewriter.create<LLVMRedIntrinOp>(loc, llvmType, vectorOperand, fmf);
613 
614  if (accumulator) {
615  result =
616  rewriter.create<typename VectorToScalarMapper<LLVMRedIntrinOp>::Type>(
617  loc, result, accumulator);
618  }
619 
620  return result;
621 }
622 
623 /// Reduction neutral classes for overloading
624 class MaskNeutralFMaximum {};
625 class MaskNeutralFMinimum {};
626 
627 /// Get the mask neutral floating point maximum value
628 static llvm::APFloat
629 getMaskNeutralValue(MaskNeutralFMaximum,
630  const llvm::fltSemantics &floatSemantics) {
631  return llvm::APFloat::getSmallest(floatSemantics, /*Negative=*/true);
632 }
633 /// Get the mask neutral floating point minimum value
634 static llvm::APFloat
635 getMaskNeutralValue(MaskNeutralFMinimum,
636  const llvm::fltSemantics &floatSemantics) {
637  return llvm::APFloat::getLargest(floatSemantics, /*Negative=*/false);
638 }
639 
640 /// Create the mask neutral floating point MLIR vector constant
641 template <typename MaskNeutral>
642 static Value createMaskNeutralValue(ConversionPatternRewriter &rewriter,
643  Location loc, Type llvmType,
644  Type vectorType) {
645  const auto &floatSemantics = cast<FloatType>(llvmType).getFloatSemantics();
646  auto value = getMaskNeutralValue(MaskNeutral{}, floatSemantics);
647  auto denseValue = DenseElementsAttr::get(cast<ShapedType>(vectorType), value);
648  return rewriter.create<LLVM::ConstantOp>(loc, vectorType, denseValue);
649 }
650 
651 /// Lowers masked `fmaximum` and `fminimum` reductions using the non-masked
652 /// intrinsics. It is a workaround to overcome the lack of masked intrinsics for
653 /// `fmaximum`/`fminimum`.
654 /// More information: https://github.com/llvm/llvm-project/issues/64940
655 template <class LLVMRedIntrinOp, class MaskNeutral>
656 static Value
657 lowerMaskedReductionWithRegular(ConversionPatternRewriter &rewriter,
658  Location loc, Type llvmType,
659  Value vectorOperand, Value accumulator,
660  Value mask, LLVM::FastmathFlagsAttr fmf) {
661  const Value vectorMaskNeutral = createMaskNeutralValue<MaskNeutral>(
662  rewriter, loc, llvmType, vectorOperand.getType());
663  const Value selectedVectorByMask = rewriter.create<LLVM::SelectOp>(
664  loc, mask, vectorOperand, vectorMaskNeutral);
665  return createFPReductionComparisonOpLowering<LLVMRedIntrinOp>(
666  rewriter, loc, llvmType, selectedVectorByMask, accumulator, fmf);
667 }
668 
669 template <class LLVMRedIntrinOp, class ReductionNeutral>
670 static Value
671 lowerReductionWithStartValue(ConversionPatternRewriter &rewriter, Location loc,
672  Type llvmType, Value vectorOperand,
673  Value accumulator, LLVM::FastmathFlagsAttr fmf) {
674  accumulator = getOrCreateAccumulator<ReductionNeutral>(rewriter, loc,
675  llvmType, accumulator);
676  return rewriter.create<LLVMRedIntrinOp>(loc, llvmType,
677  /*startValue=*/accumulator,
678  vectorOperand, fmf);
679 }
680 
681 /// Overloaded methods to lower a *predicated* reduction to an llvm instrinsic
682 /// that requires a start value. This start value format spans across fp
683 /// reductions without mask and all the masked reduction intrinsics.
684 template <class LLVMVPRedIntrinOp, class ReductionNeutral>
685 static Value
686 lowerPredicatedReductionWithStartValue(ConversionPatternRewriter &rewriter,
687  Location loc, Type llvmType,
688  Value vectorOperand, Value accumulator) {
689  accumulator = getOrCreateAccumulator<ReductionNeutral>(rewriter, loc,
690  llvmType, accumulator);
691  return rewriter.create<LLVMVPRedIntrinOp>(loc, llvmType,
692  /*startValue=*/accumulator,
693  vectorOperand);
694 }
695 
696 template <class LLVMVPRedIntrinOp, class ReductionNeutral>
697 static Value lowerPredicatedReductionWithStartValue(
698  ConversionPatternRewriter &rewriter, Location loc, Type llvmType,
699  Value vectorOperand, Value accumulator, Value mask) {
700  accumulator = getOrCreateAccumulator<ReductionNeutral>(rewriter, loc,
701  llvmType, accumulator);
702  Value vectorLength =
703  createVectorLengthValue(rewriter, loc, vectorOperand.getType());
704  return rewriter.create<LLVMVPRedIntrinOp>(loc, llvmType,
705  /*startValue=*/accumulator,
706  vectorOperand, mask, vectorLength);
707 }
708 
709 template <class LLVMIntVPRedIntrinOp, class IntReductionNeutral,
710  class LLVMFPVPRedIntrinOp, class FPReductionNeutral>
711 static Value lowerPredicatedReductionWithStartValue(
712  ConversionPatternRewriter &rewriter, Location loc, Type llvmType,
713  Value vectorOperand, Value accumulator, Value mask) {
714  if (llvmType.isIntOrIndex())
715  return lowerPredicatedReductionWithStartValue<LLVMIntVPRedIntrinOp,
716  IntReductionNeutral>(
717  rewriter, loc, llvmType, vectorOperand, accumulator, mask);
718 
719  // FP dispatch.
720  return lowerPredicatedReductionWithStartValue<LLVMFPVPRedIntrinOp,
721  FPReductionNeutral>(
722  rewriter, loc, llvmType, vectorOperand, accumulator, mask);
723 }
724 
725 /// Conversion pattern for all vector reductions.
726 class VectorReductionOpConversion
727  : public ConvertOpToLLVMPattern<vector::ReductionOp> {
728 public:
729  explicit VectorReductionOpConversion(const LLVMTypeConverter &typeConv,
730  bool reassociateFPRed)
731  : ConvertOpToLLVMPattern<vector::ReductionOp>(typeConv),
732  reassociateFPReductions(reassociateFPRed) {}
733 
734  LogicalResult
735  matchAndRewrite(vector::ReductionOp reductionOp, OpAdaptor adaptor,
736  ConversionPatternRewriter &rewriter) const override {
737  auto kind = reductionOp.getKind();
738  Type eltType = reductionOp.getDest().getType();
739  Type llvmType = typeConverter->convertType(eltType);
740  Value operand = adaptor.getVector();
741  Value acc = adaptor.getAcc();
742  Location loc = reductionOp.getLoc();
743 
744  if (eltType.isIntOrIndex()) {
745  // Integer reductions: add/mul/min/max/and/or/xor.
746  Value result;
747  switch (kind) {
748  case vector::CombiningKind::ADD:
749  result =
750  createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_add,
751  LLVM::AddOp>(
752  rewriter, loc, llvmType, operand, acc);
753  break;
754  case vector::CombiningKind::MUL:
755  result =
756  createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_mul,
757  LLVM::MulOp>(
758  rewriter, loc, llvmType, operand, acc);
759  break;
761  result = createIntegerReductionComparisonOpLowering<
762  LLVM::vector_reduce_umin>(rewriter, loc, llvmType, operand, acc,
763  LLVM::ICmpPredicate::ule);
764  break;
765  case vector::CombiningKind::MINSI:
766  result = createIntegerReductionComparisonOpLowering<
767  LLVM::vector_reduce_smin>(rewriter, loc, llvmType, operand, acc,
768  LLVM::ICmpPredicate::sle);
769  break;
770  case vector::CombiningKind::MAXUI:
771  result = createIntegerReductionComparisonOpLowering<
772  LLVM::vector_reduce_umax>(rewriter, loc, llvmType, operand, acc,
773  LLVM::ICmpPredicate::uge);
774  break;
775  case vector::CombiningKind::MAXSI:
776  result = createIntegerReductionComparisonOpLowering<
777  LLVM::vector_reduce_smax>(rewriter, loc, llvmType, operand, acc,
778  LLVM::ICmpPredicate::sge);
779  break;
780  case vector::CombiningKind::AND:
781  result =
782  createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_and,
783  LLVM::AndOp>(
784  rewriter, loc, llvmType, operand, acc);
785  break;
786  case vector::CombiningKind::OR:
787  result =
788  createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_or,
789  LLVM::OrOp>(
790  rewriter, loc, llvmType, operand, acc);
791  break;
792  case vector::CombiningKind::XOR:
793  result =
794  createIntegerReductionArithmeticOpLowering<LLVM::vector_reduce_xor,
795  LLVM::XOrOp>(
796  rewriter, loc, llvmType, operand, acc);
797  break;
798  default:
799  return failure();
800  }
801  rewriter.replaceOp(reductionOp, result);
802 
803  return success();
804  }
805 
806  if (!isa<FloatType>(eltType))
807  return failure();
808 
809  arith::FastMathFlagsAttr fMFAttr = reductionOp.getFastMathFlagsAttr();
810  LLVM::FastmathFlagsAttr fmf = LLVM::FastmathFlagsAttr::get(
811  reductionOp.getContext(),
812  convertArithFastMathFlagsToLLVM(fMFAttr.getValue()));
814  reductionOp.getContext(),
815  fmf.getValue() | (reassociateFPReductions ? LLVM::FastmathFlags::reassoc
816  : LLVM::FastmathFlags::none));
817 
818  // Floating-point reductions: add/mul/min/max
819  Value result;
820  if (kind == vector::CombiningKind::ADD) {
821  result = lowerReductionWithStartValue<LLVM::vector_reduce_fadd,
822  ReductionNeutralZero>(
823  rewriter, loc, llvmType, operand, acc, fmf);
824  } else if (kind == vector::CombiningKind::MUL) {
825  result = lowerReductionWithStartValue<LLVM::vector_reduce_fmul,
826  ReductionNeutralFPOne>(
827  rewriter, loc, llvmType, operand, acc, fmf);
828  } else if (kind == vector::CombiningKind::MINIMUMF) {
829  result =
830  createFPReductionComparisonOpLowering<LLVM::vector_reduce_fminimum>(
831  rewriter, loc, llvmType, operand, acc, fmf);
832  } else if (kind == vector::CombiningKind::MAXIMUMF) {
833  result =
834  createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmaximum>(
835  rewriter, loc, llvmType, operand, acc, fmf);
836  } else if (kind == vector::CombiningKind::MINNUMF) {
837  result = createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmin>(
838  rewriter, loc, llvmType, operand, acc, fmf);
839  } else if (kind == vector::CombiningKind::MAXNUMF) {
840  result = createFPReductionComparisonOpLowering<LLVM::vector_reduce_fmax>(
841  rewriter, loc, llvmType, operand, acc, fmf);
842  } else
843  return failure();
844 
845  rewriter.replaceOp(reductionOp, result);
846  return success();
847  }
848 
849 private:
850  const bool reassociateFPReductions;
851 };
852 
853 /// Base class to convert a `vector.mask` operation while matching traits
854 /// of the maskable operation nested inside. A `VectorMaskOpConversionBase`
855 /// instance matches against a `vector.mask` operation. The `matchAndRewrite`
856 /// method performs a second match against the maskable operation `MaskedOp`.
857 /// Finally, it invokes the virtual method `matchAndRewriteMaskableOp` to be
858 /// implemented by the concrete conversion classes. This method can match
859 /// against specific traits of the `vector.mask` and the maskable operation. It
860 /// must replace the `vector.mask` operation.
861 template <class MaskedOp>
862 class VectorMaskOpConversionBase
863  : public ConvertOpToLLVMPattern<vector::MaskOp> {
864 public:
866 
867  LogicalResult
868  matchAndRewrite(vector::MaskOp maskOp, OpAdaptor adaptor,
869  ConversionPatternRewriter &rewriter) const final {
870  // Match against the maskable operation kind.
871  auto maskedOp = llvm::dyn_cast_or_null<MaskedOp>(maskOp.getMaskableOp());
872  if (!maskedOp)
873  return failure();
874  return matchAndRewriteMaskableOp(maskOp, maskedOp, rewriter);
875  }
876 
877 protected:
878  virtual LogicalResult
879  matchAndRewriteMaskableOp(vector::MaskOp maskOp,
880  vector::MaskableOpInterface maskableOp,
881  ConversionPatternRewriter &rewriter) const = 0;
882 };
883 
884 class MaskedReductionOpConversion
885  : public VectorMaskOpConversionBase<vector::ReductionOp> {
886 
887 public:
888  using VectorMaskOpConversionBase<
889  vector::ReductionOp>::VectorMaskOpConversionBase;
890 
891  LogicalResult matchAndRewriteMaskableOp(
892  vector::MaskOp maskOp, MaskableOpInterface maskableOp,
893  ConversionPatternRewriter &rewriter) const override {
894  auto reductionOp = cast<ReductionOp>(maskableOp.getOperation());
895  auto kind = reductionOp.getKind();
896  Type eltType = reductionOp.getDest().getType();
897  Type llvmType = typeConverter->convertType(eltType);
898  Value operand = reductionOp.getVector();
899  Value acc = reductionOp.getAcc();
900  Location loc = reductionOp.getLoc();
901 
902  arith::FastMathFlagsAttr fMFAttr = reductionOp.getFastMathFlagsAttr();
903  LLVM::FastmathFlagsAttr fmf = LLVM::FastmathFlagsAttr::get(
904  reductionOp.getContext(),
905  convertArithFastMathFlagsToLLVM(fMFAttr.getValue()));
906 
907  Value result;
908  switch (kind) {
909  case vector::CombiningKind::ADD:
910  result = lowerPredicatedReductionWithStartValue<
911  LLVM::VPReduceAddOp, ReductionNeutralZero, LLVM::VPReduceFAddOp,
912  ReductionNeutralZero>(rewriter, loc, llvmType, operand, acc,
913  maskOp.getMask());
914  break;
915  case vector::CombiningKind::MUL:
916  result = lowerPredicatedReductionWithStartValue<
917  LLVM::VPReduceMulOp, ReductionNeutralIntOne, LLVM::VPReduceFMulOp,
918  ReductionNeutralFPOne>(rewriter, loc, llvmType, operand, acc,
919  maskOp.getMask());
920  break;
922  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceUMinOp,
923  ReductionNeutralUIntMax>(
924  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
925  break;
926  case vector::CombiningKind::MINSI:
927  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceSMinOp,
928  ReductionNeutralSIntMax>(
929  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
930  break;
931  case vector::CombiningKind::MAXUI:
932  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceUMaxOp,
933  ReductionNeutralUIntMin>(
934  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
935  break;
936  case vector::CombiningKind::MAXSI:
937  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceSMaxOp,
938  ReductionNeutralSIntMin>(
939  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
940  break;
941  case vector::CombiningKind::AND:
942  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceAndOp,
943  ReductionNeutralAllOnes>(
944  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
945  break;
946  case vector::CombiningKind::OR:
947  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceOrOp,
948  ReductionNeutralZero>(
949  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
950  break;
951  case vector::CombiningKind::XOR:
952  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceXorOp,
953  ReductionNeutralZero>(
954  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
955  break;
956  case vector::CombiningKind::MINNUMF:
957  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceFMinOp,
958  ReductionNeutralFPMax>(
959  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
960  break;
961  case vector::CombiningKind::MAXNUMF:
962  result = lowerPredicatedReductionWithStartValue<LLVM::VPReduceFMaxOp,
963  ReductionNeutralFPMin>(
964  rewriter, loc, llvmType, operand, acc, maskOp.getMask());
965  break;
966  case CombiningKind::MAXIMUMF:
967  result = lowerMaskedReductionWithRegular<LLVM::vector_reduce_fmaximum,
968  MaskNeutralFMaximum>(
969  rewriter, loc, llvmType, operand, acc, maskOp.getMask(), fmf);
970  break;
971  case CombiningKind::MINIMUMF:
972  result = lowerMaskedReductionWithRegular<LLVM::vector_reduce_fminimum,
973  MaskNeutralFMinimum>(
974  rewriter, loc, llvmType, operand, acc, maskOp.getMask(), fmf);
975  break;
976  }
977 
978  // Replace `vector.mask` operation altogether.
979  rewriter.replaceOp(maskOp, result);
980  return success();
981  }
982 };
983 
984 class VectorShuffleOpConversion
985  : public ConvertOpToLLVMPattern<vector::ShuffleOp> {
986 public:
988 
989  LogicalResult
990  matchAndRewrite(vector::ShuffleOp shuffleOp, OpAdaptor adaptor,
991  ConversionPatternRewriter &rewriter) const override {
992  auto loc = shuffleOp->getLoc();
993  auto v1Type = shuffleOp.getV1VectorType();
994  auto v2Type = shuffleOp.getV2VectorType();
995  auto vectorType = shuffleOp.getResultVectorType();
996  Type llvmType = typeConverter->convertType(vectorType);
997  auto maskArrayAttr = shuffleOp.getMask();
998 
999  // Bail if result type cannot be lowered.
1000  if (!llvmType)
1001  return failure();
1002 
1003  // Get rank and dimension sizes.
1004  int64_t rank = vectorType.getRank();
1005 #ifndef NDEBUG
1006  bool wellFormed0DCase =
1007  v1Type.getRank() == 0 && v2Type.getRank() == 0 && rank == 1;
1008  bool wellFormedNDCase =
1009  v1Type.getRank() == rank && v2Type.getRank() == rank;
1010  assert((wellFormed0DCase || wellFormedNDCase) && "op is not well-formed");
1011 #endif
1012 
1013  // For rank 0 and 1, where both operands have *exactly* the same vector
1014  // type, there is direct shuffle support in LLVM. Use it!
1015  if (rank <= 1 && v1Type == v2Type) {
1016  Value llvmShuffleOp = rewriter.create<LLVM::ShuffleVectorOp>(
1017  loc, adaptor.getV1(), adaptor.getV2(),
1018  LLVM::convertArrayToIndices<int32_t>(maskArrayAttr));
1019  rewriter.replaceOp(shuffleOp, llvmShuffleOp);
1020  return success();
1021  }
1022 
1023  // For all other cases, insert the individual values individually.
1024  int64_t v1Dim = v1Type.getDimSize(0);
1025  Type eltType;
1026  if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(llvmType))
1027  eltType = arrayType.getElementType();
1028  else
1029  eltType = cast<VectorType>(llvmType).getElementType();
1030  Value insert = rewriter.create<LLVM::UndefOp>(loc, llvmType);
1031  int64_t insPos = 0;
1032  for (const auto &en : llvm::enumerate(maskArrayAttr)) {
1033  int64_t extPos = cast<IntegerAttr>(en.value()).getInt();
1034  Value value = adaptor.getV1();
1035  if (extPos >= v1Dim) {
1036  extPos -= v1Dim;
1037  value = adaptor.getV2();
1038  }
1039  Value extract = extractOne(rewriter, *getTypeConverter(), loc, value,
1040  eltType, rank, extPos);
1041  insert = insertOne(rewriter, *getTypeConverter(), loc, insert, extract,
1042  llvmType, rank, insPos++);
1043  }
1044  rewriter.replaceOp(shuffleOp, insert);
1045  return success();
1046  }
1047 };
1048 
1049 class VectorExtractElementOpConversion
1050  : public ConvertOpToLLVMPattern<vector::ExtractElementOp> {
1051 public:
1052  using ConvertOpToLLVMPattern<
1053  vector::ExtractElementOp>::ConvertOpToLLVMPattern;
1054 
1055  LogicalResult
1056  matchAndRewrite(vector::ExtractElementOp extractEltOp, OpAdaptor adaptor,
1057  ConversionPatternRewriter &rewriter) const override {
1058  auto vectorType = extractEltOp.getSourceVectorType();
1059  auto llvmType = typeConverter->convertType(vectorType.getElementType());
1060 
1061  // Bail if result type cannot be lowered.
1062  if (!llvmType)
1063  return failure();
1064 
1065  if (vectorType.getRank() == 0) {
1066  Location loc = extractEltOp.getLoc();
1067  auto idxType = rewriter.getIndexType();
1068  auto zero = rewriter.create<LLVM::ConstantOp>(
1069  loc, typeConverter->convertType(idxType),
1070  rewriter.getIntegerAttr(idxType, 0));
1071  rewriter.replaceOpWithNewOp<LLVM::ExtractElementOp>(
1072  extractEltOp, llvmType, adaptor.getVector(), zero);
1073  return success();
1074  }
1075 
1076  rewriter.replaceOpWithNewOp<LLVM::ExtractElementOp>(
1077  extractEltOp, llvmType, adaptor.getVector(), adaptor.getPosition());
1078  return success();
1079  }
1080 };
1081 
1082 class VectorExtractOpConversion
1083  : public ConvertOpToLLVMPattern<vector::ExtractOp> {
1084 public:
1086 
1087  LogicalResult
1088  matchAndRewrite(vector::ExtractOp extractOp, OpAdaptor adaptor,
1089  ConversionPatternRewriter &rewriter) const override {
1090  auto loc = extractOp->getLoc();
1091  auto resultType = extractOp.getResult().getType();
1092  auto llvmResultType = typeConverter->convertType(resultType);
1093  // Bail if result type cannot be lowered.
1094  if (!llvmResultType)
1095  return failure();
1096 
1098  adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);
1099 
1100  // Extract entire vector. Should be handled by folder, but just to be safe.
1101  ArrayRef<OpFoldResult> position(positionVec);
1102  if (position.empty()) {
1103  rewriter.replaceOp(extractOp, adaptor.getVector());
1104  return success();
1105  }
1106 
1107  // One-shot extraction of vector from array (only requires extractvalue).
1108  if (isa<VectorType>(resultType)) {
1109  if (extractOp.hasDynamicPosition())
1110  return failure();
1111 
1112  Value extracted = rewriter.create<LLVM::ExtractValueOp>(
1113  loc, adaptor.getVector(), getAsIntegers(position));
1114  rewriter.replaceOp(extractOp, extracted);
1115  return success();
1116  }
1117 
1118  // Potential extraction of 1-D vector from array.
1119  Value extracted = adaptor.getVector();
1120  if (position.size() > 1) {
1121  if (extractOp.hasDynamicPosition())
1122  return failure();
1123 
1124  SmallVector<int64_t> nMinusOnePosition =
1125  getAsIntegers(position.drop_back());
1126  extracted = rewriter.create<LLVM::ExtractValueOp>(loc, extracted,
1127  nMinusOnePosition);
1128  }
1129 
1130  Value lastPosition = getAsLLVMValue(rewriter, loc, position.back());
1131  // Remaining extraction of element from 1-D LLVM vector.
1132  rewriter.replaceOpWithNewOp<LLVM::ExtractElementOp>(extractOp, extracted,
1133  lastPosition);
1134  return success();
1135  }
1136 };
1137 
1138 /// Conversion pattern that turns a vector.fma on a 1-D vector
1139 /// into an llvm.intr.fmuladd. This is a trivial 1-1 conversion.
1140 /// This does not match vectors of n >= 2 rank.
1141 ///
1142 /// Example:
1143 /// ```
1144 /// vector.fma %a, %a, %a : vector<8xf32>
1145 /// ```
1146 /// is converted to:
1147 /// ```
1148 /// llvm.intr.fmuladd %va, %va, %va:
1149 /// (!llvm."<8 x f32>">, !llvm<"<8 x f32>">, !llvm<"<8 x f32>">)
1150 /// -> !llvm."<8 x f32>">
1151 /// ```
1152 class VectorFMAOp1DConversion : public ConvertOpToLLVMPattern<vector::FMAOp> {
1153 public:
1155 
1156  LogicalResult
1157  matchAndRewrite(vector::FMAOp fmaOp, OpAdaptor adaptor,
1158  ConversionPatternRewriter &rewriter) const override {
1159  VectorType vType = fmaOp.getVectorType();
1160  if (vType.getRank() > 1)
1161  return failure();
1162 
1163  rewriter.replaceOpWithNewOp<LLVM::FMulAddOp>(
1164  fmaOp, adaptor.getLhs(), adaptor.getRhs(), adaptor.getAcc());
1165  return success();
1166  }
1167 };
1168 
1169 class VectorInsertElementOpConversion
1170  : public ConvertOpToLLVMPattern<vector::InsertElementOp> {
1171 public:
1173 
1174  LogicalResult
1175  matchAndRewrite(vector::InsertElementOp insertEltOp, OpAdaptor adaptor,
1176  ConversionPatternRewriter &rewriter) const override {
1177  auto vectorType = insertEltOp.getDestVectorType();
1178  auto llvmType = typeConverter->convertType(vectorType);
1179 
1180  // Bail if result type cannot be lowered.
1181  if (!llvmType)
1182  return failure();
1183 
1184  if (vectorType.getRank() == 0) {
1185  Location loc = insertEltOp.getLoc();
1186  auto idxType = rewriter.getIndexType();
1187  auto zero = rewriter.create<LLVM::ConstantOp>(
1188  loc, typeConverter->convertType(idxType),
1189  rewriter.getIntegerAttr(idxType, 0));
1190  rewriter.replaceOpWithNewOp<LLVM::InsertElementOp>(
1191  insertEltOp, llvmType, adaptor.getDest(), adaptor.getSource(), zero);
1192  return success();
1193  }
1194 
1195  rewriter.replaceOpWithNewOp<LLVM::InsertElementOp>(
1196  insertEltOp, llvmType, adaptor.getDest(), adaptor.getSource(),
1197  adaptor.getPosition());
1198  return success();
1199  }
1200 };
1201 
1202 class VectorInsertOpConversion
1203  : public ConvertOpToLLVMPattern<vector::InsertOp> {
1204 public:
1206 
1207  LogicalResult
1208  matchAndRewrite(vector::InsertOp insertOp, OpAdaptor adaptor,
1209  ConversionPatternRewriter &rewriter) const override {
1210  auto loc = insertOp->getLoc();
1211  auto sourceType = insertOp.getSourceType();
1212  auto destVectorType = insertOp.getDestVectorType();
1213  auto llvmResultType = typeConverter->convertType(destVectorType);
1214  // Bail if result type cannot be lowered.
1215  if (!llvmResultType)
1216  return failure();
1217 
1219  adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);
1220 
1221  // Overwrite entire vector with value. Should be handled by folder, but
1222  // just to be safe.
1223  ArrayRef<OpFoldResult> position(positionVec);
1224  if (position.empty()) {
1225  rewriter.replaceOp(insertOp, adaptor.getSource());
1226  return success();
1227  }
1228 
1229  // One-shot insertion of a vector into an array (only requires insertvalue).
1230  if (isa<VectorType>(sourceType)) {
1231  if (insertOp.hasDynamicPosition())
1232  return failure();
1233 
1234  Value inserted = rewriter.create<LLVM::InsertValueOp>(
1235  loc, adaptor.getDest(), adaptor.getSource(), getAsIntegers(position));
1236  rewriter.replaceOp(insertOp, inserted);
1237  return success();
1238  }
1239 
1240  // Potential extraction of 1-D vector from array.
1241  Value extracted = adaptor.getDest();
1242  auto oneDVectorType = destVectorType;
1243  if (position.size() > 1) {
1244  if (insertOp.hasDynamicPosition())
1245  return failure();
1246 
1247  oneDVectorType = reducedVectorTypeBack(destVectorType);
1248  extracted = rewriter.create<LLVM::ExtractValueOp>(
1249  loc, extracted, getAsIntegers(position.drop_back()));
1250  }
1251 
1252  // Insertion of an element into a 1-D LLVM vector.
1253  Value inserted = rewriter.create<LLVM::InsertElementOp>(
1254  loc, typeConverter->convertType(oneDVectorType), extracted,
1255  adaptor.getSource(), getAsLLVMValue(rewriter, loc, position.back()));
1256 
1257  // Potential insertion of resulting 1-D vector into array.
1258  if (position.size() > 1) {
1259  if (insertOp.hasDynamicPosition())
1260  return failure();
1261 
1262  inserted = rewriter.create<LLVM::InsertValueOp>(
1263  loc, adaptor.getDest(), inserted,
1264  getAsIntegers(position.drop_back()));
1265  }
1266 
1267  rewriter.replaceOp(insertOp, inserted);
1268  return success();
1269  }
1270 };
1271 
1272 /// Lower vector.scalable.insert ops to LLVM vector.insert
1273 struct VectorScalableInsertOpLowering
1274  : public ConvertOpToLLVMPattern<vector::ScalableInsertOp> {
1275  using ConvertOpToLLVMPattern<
1276  vector::ScalableInsertOp>::ConvertOpToLLVMPattern;
1277 
1278  LogicalResult
1279  matchAndRewrite(vector::ScalableInsertOp insOp, OpAdaptor adaptor,
1280  ConversionPatternRewriter &rewriter) const override {
1281  rewriter.replaceOpWithNewOp<LLVM::vector_insert>(
1282  insOp, adaptor.getDest(), adaptor.getSource(), adaptor.getPos());
1283  return success();
1284  }
1285 };
1286 
1287 /// Lower vector.scalable.extract ops to LLVM vector.extract
1288 struct VectorScalableExtractOpLowering
1289  : public ConvertOpToLLVMPattern<vector::ScalableExtractOp> {
1290  using ConvertOpToLLVMPattern<
1291  vector::ScalableExtractOp>::ConvertOpToLLVMPattern;
1292 
1293  LogicalResult
1294  matchAndRewrite(vector::ScalableExtractOp extOp, OpAdaptor adaptor,
1295  ConversionPatternRewriter &rewriter) const override {
1296  rewriter.replaceOpWithNewOp<LLVM::vector_extract>(
1297  extOp, typeConverter->convertType(extOp.getResultVectorType()),
1298  adaptor.getSource(), adaptor.getPos());
1299  return success();
1300  }
1301 };
1302 
1303 /// Rank reducing rewrite for n-D FMA into (n-1)-D FMA where n > 1.
1304 ///
1305 /// Example:
1306 /// ```
1307 /// %d = vector.fma %a, %b, %c : vector<2x4xf32>
1308 /// ```
1309 /// is rewritten into:
1310 /// ```
1311 /// %r = splat %f0: vector<2x4xf32>
1312 /// %va = vector.extractvalue %a[0] : vector<2x4xf32>
1313 /// %vb = vector.extractvalue %b[0] : vector<2x4xf32>
1314 /// %vc = vector.extractvalue %c[0] : vector<2x4xf32>
1315 /// %vd = vector.fma %va, %vb, %vc : vector<4xf32>
1316 /// %r2 = vector.insertvalue %vd, %r[0] : vector<4xf32> into vector<2x4xf32>
1317 /// %va2 = vector.extractvalue %a2[1] : vector<2x4xf32>
1318 /// %vb2 = vector.extractvalue %b2[1] : vector<2x4xf32>
1319 /// %vc2 = vector.extractvalue %c2[1] : vector<2x4xf32>
1320 /// %vd2 = vector.fma %va2, %vb2, %vc2 : vector<4xf32>
1321 /// %r3 = vector.insertvalue %vd2, %r2[1] : vector<4xf32> into vector<2x4xf32>
1322 /// // %r3 holds the final value.
1323 /// ```
1324 class VectorFMAOpNDRewritePattern : public OpRewritePattern<FMAOp> {
1325 public:
1327 
1328  void initialize() {
1329  // This pattern recursively unpacks one dimension at a time. The recursion
1330  // bounded as the rank is strictly decreasing.
1331  setHasBoundedRewriteRecursion();
1332  }
1333 
1334  LogicalResult matchAndRewrite(FMAOp op,
1335  PatternRewriter &rewriter) const override {
1336  auto vType = op.getVectorType();
1337  if (vType.getRank() < 2)
1338  return failure();
1339 
1340  auto loc = op.getLoc();
1341  auto elemType = vType.getElementType();
1342  Value zero = rewriter.create<arith::ConstantOp>(
1343  loc, elemType, rewriter.getZeroAttr(elemType));
1344  Value desc = rewriter.create<vector::SplatOp>(loc, vType, zero);
1345  for (int64_t i = 0, e = vType.getShape().front(); i != e; ++i) {
1346  Value extrLHS = rewriter.create<ExtractOp>(loc, op.getLhs(), i);
1347  Value extrRHS = rewriter.create<ExtractOp>(loc, op.getRhs(), i);
1348  Value extrACC = rewriter.create<ExtractOp>(loc, op.getAcc(), i);
1349  Value fma = rewriter.create<FMAOp>(loc, extrLHS, extrRHS, extrACC);
1350  desc = rewriter.create<InsertOp>(loc, fma, desc, i);
1351  }
1352  rewriter.replaceOp(op, desc);
1353  return success();
1354  }
1355 };
1356 
1357 /// Returns the strides if the memory underlying `memRefType` has a contiguous
1358 /// static layout.
1359 static std::optional<SmallVector<int64_t, 4>>
1360 computeContiguousStrides(MemRefType memRefType) {
1361  int64_t offset;
1362  SmallVector<int64_t, 4> strides;
1363  if (failed(getStridesAndOffset(memRefType, strides, offset)))
1364  return std::nullopt;
1365  if (!strides.empty() && strides.back() != 1)
1366  return std::nullopt;
1367  // If no layout or identity layout, this is contiguous by definition.
1368  if (memRefType.getLayout().isIdentity())
1369  return strides;
1370 
1371  // Otherwise, we must determine contiguity form shapes. This can only ever
1372  // work in static cases because MemRefType is underspecified to represent
1373  // contiguous dynamic shapes in other ways than with just empty/identity
1374  // layout.
1375  auto sizes = memRefType.getShape();
1376  for (int index = 0, e = strides.size() - 1; index < e; ++index) {
1377  if (ShapedType::isDynamic(sizes[index + 1]) ||
1378  ShapedType::isDynamic(strides[index]) ||
1379  ShapedType::isDynamic(strides[index + 1]))
1380  return std::nullopt;
1381  if (strides[index] != strides[index + 1] * sizes[index + 1])
1382  return std::nullopt;
1383  }
1384  return strides;
1385 }
1386 
1387 class VectorTypeCastOpConversion
1388  : public ConvertOpToLLVMPattern<vector::TypeCastOp> {
1389 public:
1391 
1392  LogicalResult
1393  matchAndRewrite(vector::TypeCastOp castOp, OpAdaptor adaptor,
1394  ConversionPatternRewriter &rewriter) const override {
1395  auto loc = castOp->getLoc();
1396  MemRefType sourceMemRefType =
1397  cast<MemRefType>(castOp.getOperand().getType());
1398  MemRefType targetMemRefType = castOp.getType();
1399 
1400  // Only static shape casts supported atm.
1401  if (!sourceMemRefType.hasStaticShape() ||
1402  !targetMemRefType.hasStaticShape())
1403  return failure();
1404 
1405  auto llvmSourceDescriptorTy =
1406  dyn_cast<LLVM::LLVMStructType>(adaptor.getOperands()[0].getType());
1407  if (!llvmSourceDescriptorTy)
1408  return failure();
1409  MemRefDescriptor sourceMemRef(adaptor.getOperands()[0]);
1410 
1411  auto llvmTargetDescriptorTy = dyn_cast_or_null<LLVM::LLVMStructType>(
1412  typeConverter->convertType(targetMemRefType));
1413  if (!llvmTargetDescriptorTy)
1414  return failure();
1415 
1416  // Only contiguous source buffers supported atm.
1417  auto sourceStrides = computeContiguousStrides(sourceMemRefType);
1418  if (!sourceStrides)
1419  return failure();
1420  auto targetStrides = computeContiguousStrides(targetMemRefType);
1421  if (!targetStrides)
1422  return failure();
1423  // Only support static strides for now, regardless of contiguity.
1424  if (llvm::any_of(*targetStrides, ShapedType::isDynamic))
1425  return failure();
1426 
1427  auto int64Ty = IntegerType::get(rewriter.getContext(), 64);
1428 
1429  // Create descriptor.
1430  auto desc = MemRefDescriptor::undef(rewriter, loc, llvmTargetDescriptorTy);
1431  // Set allocated ptr.
1432  Value allocated = sourceMemRef.allocatedPtr(rewriter, loc);
1433  desc.setAllocatedPtr(rewriter, loc, allocated);
1434 
1435  // Set aligned ptr.
1436  Value ptr = sourceMemRef.alignedPtr(rewriter, loc);
1437  desc.setAlignedPtr(rewriter, loc, ptr);
1438  // Fill offset 0.
1439  auto attr = rewriter.getIntegerAttr(rewriter.getIndexType(), 0);
1440  auto zero = rewriter.create<LLVM::ConstantOp>(loc, int64Ty, attr);
1441  desc.setOffset(rewriter, loc, zero);
1442 
1443  // Fill size and stride descriptors in memref.
1444  for (const auto &indexedSize :
1445  llvm::enumerate(targetMemRefType.getShape())) {
1446  int64_t index = indexedSize.index();
1447  auto sizeAttr =
1448  rewriter.getIntegerAttr(rewriter.getIndexType(), indexedSize.value());
1449  auto size = rewriter.create<LLVM::ConstantOp>(loc, int64Ty, sizeAttr);
1450  desc.setSize(rewriter, loc, index, size);
1451  auto strideAttr = rewriter.getIntegerAttr(rewriter.getIndexType(),
1452  (*targetStrides)[index]);
1453  auto stride = rewriter.create<LLVM::ConstantOp>(loc, int64Ty, strideAttr);
1454  desc.setStride(rewriter, loc, index, stride);
1455  }
1456 
1457  rewriter.replaceOp(castOp, {desc});
1458  return success();
1459  }
1460 };
1461 
1462 /// Conversion pattern for a `vector.create_mask` (1-D scalable vectors only).
1463 /// Non-scalable versions of this operation are handled in Vector Transforms.
1464 class VectorCreateMaskOpRewritePattern
1465  : public OpRewritePattern<vector::CreateMaskOp> {
1466 public:
1467  explicit VectorCreateMaskOpRewritePattern(MLIRContext *context,
1468  bool enableIndexOpt)
1469  : OpRewritePattern<vector::CreateMaskOp>(context),
1470  force32BitVectorIndices(enableIndexOpt) {}
1471 
1472  LogicalResult matchAndRewrite(vector::CreateMaskOp op,
1473  PatternRewriter &rewriter) const override {
1474  auto dstType = op.getType();
1475  if (dstType.getRank() != 1 || !cast<VectorType>(dstType).isScalable())
1476  return failure();
1477  IntegerType idxType =
1478  force32BitVectorIndices ? rewriter.getI32Type() : rewriter.getI64Type();
1479  auto loc = op->getLoc();
1480  Value indices = rewriter.create<LLVM::StepVectorOp>(
1481  loc, LLVM::getVectorType(idxType, dstType.getShape()[0],
1482  /*isScalable=*/true));
1483  auto bound = getValueOrCreateCastToIndexLike(rewriter, loc, idxType,
1484  op.getOperand(0));
1485  Value bounds = rewriter.create<SplatOp>(loc, indices.getType(), bound);
1486  Value comp = rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::slt,
1487  indices, bounds);
1488  rewriter.replaceOp(op, comp);
1489  return success();
1490  }
1491 
1492 private:
1493  const bool force32BitVectorIndices;
1494 };
1495 
1496 class VectorPrintOpConversion : public ConvertOpToLLVMPattern<vector::PrintOp> {
1497 public:
1499 
1500  // Lowering implementation that relies on a small runtime support library,
1501  // which only needs to provide a few printing methods (single value for all
1502  // data types, opening/closing bracket, comma, newline). The lowering splits
1503  // the vector into elementary printing operations. The advantage of this
1504  // approach is that the library can remain unaware of all low-level
1505  // implementation details of vectors while still supporting output of any
1506  // shaped and dimensioned vector.
1507  //
1508  // Note: This lowering only handles scalars, n-D vectors are broken into
1509  // printing scalars in loops in VectorToSCF.
1510  //
1511  // TODO: rely solely on libc in future? something else?
1512  //
1513  LogicalResult
1514  matchAndRewrite(vector::PrintOp printOp, OpAdaptor adaptor,
1515  ConversionPatternRewriter &rewriter) const override {
1516  auto parent = printOp->getParentOfType<ModuleOp>();
1517  if (!parent)
1518  return failure();
1519 
1520  auto loc = printOp->getLoc();
1521 
1522  if (auto value = adaptor.getSource()) {
1523  Type printType = printOp.getPrintType();
1524  if (isa<VectorType>(printType)) {
1525  // Vectors should be broken into elementary print ops in VectorToSCF.
1526  return failure();
1527  }
1528  if (failed(emitScalarPrint(rewriter, parent, loc, printType, value)))
1529  return failure();
1530  }
1531 
1532  auto punct = printOp.getPunctuation();
1533  if (auto stringLiteral = printOp.getStringLiteral()) {
1534  LLVM::createPrintStrCall(rewriter, loc, parent, "vector_print_str",
1535  *stringLiteral, *getTypeConverter(),
1536  /*addNewline=*/false);
1537  } else if (punct != PrintPunctuation::NoPunctuation) {
1538  emitCall(rewriter, printOp->getLoc(), [&] {
1539  switch (punct) {
1540  case PrintPunctuation::Close:
1541  return LLVM::lookupOrCreatePrintCloseFn(parent);
1542  case PrintPunctuation::Open:
1543  return LLVM::lookupOrCreatePrintOpenFn(parent);
1544  case PrintPunctuation::Comma:
1545  return LLVM::lookupOrCreatePrintCommaFn(parent);
1546  case PrintPunctuation::NewLine:
1547  return LLVM::lookupOrCreatePrintNewlineFn(parent);
1548  default:
1549  llvm_unreachable("unexpected punctuation");
1550  }
1551  }());
1552  }
1553 
1554  rewriter.eraseOp(printOp);
1555  return success();
1556  }
1557 
1558 private:
1559  enum class PrintConversion {
1560  // clang-format off
1561  None,
1562  ZeroExt64,
1563  SignExt64,
1564  Bitcast16
1565  // clang-format on
1566  };
1567 
1568  LogicalResult emitScalarPrint(ConversionPatternRewriter &rewriter,
1569  ModuleOp parent, Location loc, Type printType,
1570  Value value) const {
1571  if (typeConverter->convertType(printType) == nullptr)
1572  return failure();
1573 
1574  // Make sure element type has runtime support.
1575  PrintConversion conversion = PrintConversion::None;
1576  Operation *printer;
1577  if (printType.isF32()) {
1578  printer = LLVM::lookupOrCreatePrintF32Fn(parent);
1579  } else if (printType.isF64()) {
1580  printer = LLVM::lookupOrCreatePrintF64Fn(parent);
1581  } else if (printType.isF16()) {
1582  conversion = PrintConversion::Bitcast16; // bits!
1583  printer = LLVM::lookupOrCreatePrintF16Fn(parent);
1584  } else if (printType.isBF16()) {
1585  conversion = PrintConversion::Bitcast16; // bits!
1586  printer = LLVM::lookupOrCreatePrintBF16Fn(parent);
1587  } else if (printType.isIndex()) {
1588  printer = LLVM::lookupOrCreatePrintU64Fn(parent);
1589  } else if (auto intTy = dyn_cast<IntegerType>(printType)) {
1590  // Integers need a zero or sign extension on the operand
1591  // (depending on the source type) as well as a signed or
1592  // unsigned print method. Up to 64-bit is supported.
1593  unsigned width = intTy.getWidth();
1594  if (intTy.isUnsigned()) {
1595  if (width <= 64) {
1596  if (width < 64)
1597  conversion = PrintConversion::ZeroExt64;
1598  printer = LLVM::lookupOrCreatePrintU64Fn(parent);
1599  } else {
1600  return failure();
1601  }
1602  } else {
1603  assert(intTy.isSignless() || intTy.isSigned());
1604  if (width <= 64) {
1605  // Note that we *always* zero extend booleans (1-bit integers),
1606  // so that true/false is printed as 1/0 rather than -1/0.
1607  if (width == 1)
1608  conversion = PrintConversion::ZeroExt64;
1609  else if (width < 64)
1610  conversion = PrintConversion::SignExt64;
1611  printer = LLVM::lookupOrCreatePrintI64Fn(parent);
1612  } else {
1613  return failure();
1614  }
1615  }
1616  } else {
1617  return failure();
1618  }
1619 
1620  switch (conversion) {
1621  case PrintConversion::ZeroExt64:
1622  value = rewriter.create<arith::ExtUIOp>(
1623  loc, IntegerType::get(rewriter.getContext(), 64), value);
1624  break;
1625  case PrintConversion::SignExt64:
1626  value = rewriter.create<arith::ExtSIOp>(
1627  loc, IntegerType::get(rewriter.getContext(), 64), value);
1628  break;
1629  case PrintConversion::Bitcast16:
1630  value = rewriter.create<LLVM::BitcastOp>(
1631  loc, IntegerType::get(rewriter.getContext(), 16), value);
1632  break;
1633  case PrintConversion::None:
1634  break;
1635  }
1636  emitCall(rewriter, loc, printer, value);
1637  return success();
1638  }
1639 
1640  // Helper to emit a call.
1641  static void emitCall(ConversionPatternRewriter &rewriter, Location loc,
1642  Operation *ref, ValueRange params = ValueRange()) {
1643  rewriter.create<LLVM::CallOp>(loc, TypeRange(), SymbolRefAttr::get(ref),
1644  params);
1645  }
1646 };
1647 
1648 /// The Splat operation is lowered to an insertelement + a shufflevector
1649 /// operation. Splat to only 0-d and 1-d vector result types are lowered.
1650 struct VectorSplatOpLowering : public ConvertOpToLLVMPattern<vector::SplatOp> {
1652 
1653  LogicalResult
1654  matchAndRewrite(vector::SplatOp splatOp, OpAdaptor adaptor,
1655  ConversionPatternRewriter &rewriter) const override {
1656  VectorType resultType = cast<VectorType>(splatOp.getType());
1657  if (resultType.getRank() > 1)
1658  return failure();
1659 
1660  // First insert it into an undef vector so we can shuffle it.
1661  auto vectorType = typeConverter->convertType(splatOp.getType());
1662  Value undef = rewriter.create<LLVM::UndefOp>(splatOp.getLoc(), vectorType);
1663  auto zero = rewriter.create<LLVM::ConstantOp>(
1664  splatOp.getLoc(),
1665  typeConverter->convertType(rewriter.getIntegerType(32)),
1666  rewriter.getZeroAttr(rewriter.getIntegerType(32)));
1667 
1668  // For 0-d vector, we simply do `insertelement`.
1669  if (resultType.getRank() == 0) {
1670  rewriter.replaceOpWithNewOp<LLVM::InsertElementOp>(
1671  splatOp, vectorType, undef, adaptor.getInput(), zero);
1672  return success();
1673  }
1674 
1675  // For 1-d vector, we additionally do a `vectorshuffle`.
1676  auto v = rewriter.create<LLVM::InsertElementOp>(
1677  splatOp.getLoc(), vectorType, undef, adaptor.getInput(), zero);
1678 
1679  int64_t width = cast<VectorType>(splatOp.getType()).getDimSize(0);
1680  SmallVector<int32_t> zeroValues(width, 0);
1681 
1682  // Shuffle the value across the desired number of elements.
1683  rewriter.replaceOpWithNewOp<LLVM::ShuffleVectorOp>(splatOp, v, undef,
1684  zeroValues);
1685  return success();
1686  }
1687 };
1688 
1689 /// The Splat operation is lowered to an insertelement + a shufflevector
1690 /// operation. Splat to only 2+-d vector result types are lowered by the
1691 /// SplatNdOpLowering, the 1-d case is handled by SplatOpLowering.
1692 struct VectorSplatNdOpLowering : public ConvertOpToLLVMPattern<SplatOp> {
1694 
1695  LogicalResult
1696  matchAndRewrite(SplatOp splatOp, OpAdaptor adaptor,
1697  ConversionPatternRewriter &rewriter) const override {
1698  VectorType resultType = splatOp.getType();
1699  if (resultType.getRank() <= 1)
1700  return failure();
1701 
1702  // First insert it into an undef vector so we can shuffle it.
1703  auto loc = splatOp.getLoc();
1704  auto vectorTypeInfo =
1705  LLVM::detail::extractNDVectorTypeInfo(resultType, *getTypeConverter());
1706  auto llvmNDVectorTy = vectorTypeInfo.llvmNDVectorTy;
1707  auto llvm1DVectorTy = vectorTypeInfo.llvm1DVectorTy;
1708  if (!llvmNDVectorTy || !llvm1DVectorTy)
1709  return failure();
1710 
1711  // Construct returned value.
1712  Value desc = rewriter.create<LLVM::UndefOp>(loc, llvmNDVectorTy);
1713 
1714  // Construct a 1-D vector with the splatted value that we insert in all the
1715  // places within the returned descriptor.
1716  Value vdesc = rewriter.create<LLVM::UndefOp>(loc, llvm1DVectorTy);
1717  auto zero = rewriter.create<LLVM::ConstantOp>(
1718  loc, typeConverter->convertType(rewriter.getIntegerType(32)),
1719  rewriter.getZeroAttr(rewriter.getIntegerType(32)));
1720  Value v = rewriter.create<LLVM::InsertElementOp>(loc, llvm1DVectorTy, vdesc,
1721  adaptor.getInput(), zero);
1722 
1723  // Shuffle the value across the desired number of elements.
1724  int64_t width = resultType.getDimSize(resultType.getRank() - 1);
1725  SmallVector<int32_t> zeroValues(width, 0);
1726  v = rewriter.create<LLVM::ShuffleVectorOp>(loc, v, v, zeroValues);
1727 
1728  // Iterate of linear index, convert to coords space and insert splatted 1-D
1729  // vector in each position.
1730  nDVectorIterate(vectorTypeInfo, rewriter, [&](ArrayRef<int64_t> position) {
1731  desc = rewriter.create<LLVM::InsertValueOp>(loc, desc, v, position);
1732  });
1733  rewriter.replaceOp(splatOp, desc);
1734  return success();
1735  }
1736 };
1737 
1738 /// Conversion pattern for a `vector.interleave`.
1739 /// This supports fixed-sized vectors and scalable vectors.
1740 struct VectorInterleaveOpLowering
1741  : public ConvertOpToLLVMPattern<vector::InterleaveOp> {
1743 
1744  LogicalResult
1745  matchAndRewrite(vector::InterleaveOp interleaveOp, OpAdaptor adaptor,
1746  ConversionPatternRewriter &rewriter) const override {
1747  VectorType resultType = interleaveOp.getResultVectorType();
1748  // n-D interleaves should have been lowered already.
1749  if (resultType.getRank() != 1)
1750  return rewriter.notifyMatchFailure(interleaveOp,
1751  "InterleaveOp not rank 1");
1752  // If the result is rank 1, then this directly maps to LLVM.
1753  if (resultType.isScalable()) {
1754  rewriter.replaceOpWithNewOp<LLVM::vector_interleave2>(
1755  interleaveOp, typeConverter->convertType(resultType),
1756  adaptor.getLhs(), adaptor.getRhs());
1757  return success();
1758  }
1759  // Lower fixed-size interleaves to a shufflevector. While the
1760  // vector.interleave2 intrinsic supports fixed and scalable vectors, the
1761  // langref still recommends fixed-vectors use shufflevector, see:
1762  // https://llvm.org/docs/LangRef.html#id876.
1763  int64_t resultVectorSize = resultType.getNumElements();
1764  SmallVector<int32_t> interleaveShuffleMask;
1765  interleaveShuffleMask.reserve(resultVectorSize);
1766  for (int i = 0, end = resultVectorSize / 2; i < end; ++i) {
1767  interleaveShuffleMask.push_back(i);
1768  interleaveShuffleMask.push_back((resultVectorSize / 2) + i);
1769  }
1770  rewriter.replaceOpWithNewOp<LLVM::ShuffleVectorOp>(
1771  interleaveOp, adaptor.getLhs(), adaptor.getRhs(),
1772  interleaveShuffleMask);
1773  return success();
1774  }
1775 };
1776 
1777 /// Conversion pattern for a `vector.deinterleave`.
1778 /// This supports fixed-sized vectors and scalable vectors.
1779 struct VectorDeinterleaveOpLowering
1780  : public ConvertOpToLLVMPattern<vector::DeinterleaveOp> {
1782 
1783  LogicalResult
1784  matchAndRewrite(vector::DeinterleaveOp deinterleaveOp, OpAdaptor adaptor,
1785  ConversionPatternRewriter &rewriter) const override {
1786  VectorType resultType = deinterleaveOp.getResultVectorType();
1787  VectorType sourceType = deinterleaveOp.getSourceVectorType();
1788  auto loc = deinterleaveOp.getLoc();
1789 
1790  // Note: n-D deinterleave operations should be lowered to the 1-D before
1791  // converting to LLVM.
1792  if (resultType.getRank() != 1)
1793  return rewriter.notifyMatchFailure(deinterleaveOp,
1794  "DeinterleaveOp not rank 1");
1795 
1796  if (resultType.isScalable()) {
1797  auto llvmTypeConverter = this->getTypeConverter();
1798  auto deinterleaveResults = deinterleaveOp.getResultTypes();
1799  auto packedOpResults =
1800  llvmTypeConverter->packOperationResults(deinterleaveResults);
1801  auto intrinsic = rewriter.create<LLVM::vector_deinterleave2>(
1802  loc, packedOpResults, adaptor.getSource());
1803 
1804  auto evenResult = rewriter.create<LLVM::ExtractValueOp>(
1805  loc, intrinsic->getResult(0), 0);
1806  auto oddResult = rewriter.create<LLVM::ExtractValueOp>(
1807  loc, intrinsic->getResult(0), 1);
1808 
1809  rewriter.replaceOp(deinterleaveOp, ValueRange{evenResult, oddResult});
1810  return success();
1811  }
1812  // Lower fixed-size deinterleave to two shufflevectors. While the
1813  // vector.deinterleave2 intrinsic supports fixed and scalable vectors, the
1814  // langref still recommends fixed-vectors use shufflevector, see:
1815  // https://llvm.org/docs/LangRef.html#id889.
1816  int64_t resultVectorSize = resultType.getNumElements();
1817  SmallVector<int32_t> evenShuffleMask;
1818  SmallVector<int32_t> oddShuffleMask;
1819 
1820  evenShuffleMask.reserve(resultVectorSize);
1821  oddShuffleMask.reserve(resultVectorSize);
1822 
1823  for (int i = 0; i < sourceType.getNumElements(); ++i) {
1824  if (i % 2 == 0)
1825  evenShuffleMask.push_back(i);
1826  else
1827  oddShuffleMask.push_back(i);
1828  }
1829 
1830  auto poison = rewriter.create<LLVM::PoisonOp>(loc, sourceType);
1831  auto evenShuffle = rewriter.create<LLVM::ShuffleVectorOp>(
1832  loc, adaptor.getSource(), poison, evenShuffleMask);
1833  auto oddShuffle = rewriter.create<LLVM::ShuffleVectorOp>(
1834  loc, adaptor.getSource(), poison, oddShuffleMask);
1835 
1836  rewriter.replaceOp(deinterleaveOp, ValueRange{evenShuffle, oddShuffle});
1837  return success();
1838  }
1839 };
1840 
1841 /// Conversion pattern for a `vector.from_elements`.
1842 struct VectorFromElementsLowering
1843  : public ConvertOpToLLVMPattern<vector::FromElementsOp> {
1845 
1846  LogicalResult
1847  matchAndRewrite(vector::FromElementsOp fromElementsOp, OpAdaptor adaptor,
1848  ConversionPatternRewriter &rewriter) const override {
1849  Location loc = fromElementsOp.getLoc();
1850  VectorType vectorType = fromElementsOp.getType();
1851  // TODO: Multi-dimensional vectors lower to !llvm.array<... x vector<>>.
1852  // Such ops should be handled in the same way as vector.insert.
1853  if (vectorType.getRank() > 1)
1854  return rewriter.notifyMatchFailure(fromElementsOp,
1855  "rank > 1 vectors are not supported");
1856  Type llvmType = typeConverter->convertType(vectorType);
1857  Value result = rewriter.create<LLVM::UndefOp>(loc, llvmType);
1858  for (auto [idx, val] : llvm::enumerate(adaptor.getElements()))
1859  result = rewriter.create<vector::InsertOp>(loc, val, result, idx);
1860  rewriter.replaceOp(fromElementsOp, result);
1861  return success();
1862  }
1863 };
1864 
1865 /// Conversion pattern for vector.step.
1866 struct VectorStepOpLowering : public ConvertOpToLLVMPattern<vector::StepOp> {
1868 
1869  LogicalResult
1870  matchAndRewrite(vector::StepOp stepOp, OpAdaptor adaptor,
1871  ConversionPatternRewriter &rewriter) const override {
1872  Type llvmType = typeConverter->convertType(stepOp.getType());
1873  rewriter.replaceOpWithNewOp<LLVM::StepVectorOp>(stepOp, llvmType);
1874  return success();
1875  }
1876 };
1877 
1878 } // namespace
1879 
1880 /// Populate the given list with patterns that convert from Vector to LLVM.
1882  LLVMTypeConverter &converter, RewritePatternSet &patterns,
1883  bool reassociateFPReductions, bool force32BitVectorIndices) {
1884  MLIRContext *ctx = converter.getDialect()->getContext();
1885  patterns.add<VectorFMAOpNDRewritePattern>(ctx);
1887  patterns.add<VectorReductionOpConversion>(converter, reassociateFPReductions);
1888  patterns.add<VectorCreateMaskOpRewritePattern>(ctx, force32BitVectorIndices);
1889  patterns.add<VectorBitCastOpConversion, VectorShuffleOpConversion,
1890  VectorExtractElementOpConversion, VectorExtractOpConversion,
1891  VectorFMAOp1DConversion, VectorInsertElementOpConversion,
1892  VectorInsertOpConversion, VectorPrintOpConversion,
1893  VectorTypeCastOpConversion, VectorScaleOpConversion,
1894  VectorLoadStoreConversion<vector::LoadOp>,
1895  VectorLoadStoreConversion<vector::MaskedLoadOp>,
1896  VectorLoadStoreConversion<vector::StoreOp>,
1897  VectorLoadStoreConversion<vector::MaskedStoreOp>,
1898  VectorGatherOpConversion, VectorScatterOpConversion,
1899  VectorExpandLoadOpConversion, VectorCompressStoreOpConversion,
1900  VectorSplatOpLowering, VectorSplatNdOpLowering,
1901  VectorScalableInsertOpLowering, VectorScalableExtractOpLowering,
1902  MaskedReductionOpConversion, VectorInterleaveOpLowering,
1903  VectorDeinterleaveOpLowering, VectorFromElementsLowering,
1904  VectorStepOpLowering>(converter);
1905  // Transfer ops with rank > 1 are handled by VectorToSCF.
1906  populateVectorTransferLoweringPatterns(patterns, /*maxTransferRank=*/1);
1907 }
1908 
1910  LLVMTypeConverter &converter, RewritePatternSet &patterns) {
1911  patterns.add<VectorMatmulOpConversion>(converter);
1912  patterns.add<VectorFlatTransposeOpConversion>(converter);
1913 }
static Value getIndexedPtrs(ConversionPatternRewriter &rewriter, Location loc, const LLVMTypeConverter &typeConverter, MemRefType memRefType, Value llvmMemref, Value base, Value index, VectorType vectorType)
LogicalResult getMemRefAlignment(const LLVMTypeConverter &typeConverter, MemRefType memrefType, unsigned &align)
static Value extractOne(ConversionPatternRewriter &rewriter, const LLVMTypeConverter &typeConverter, Location loc, Value val, Type llvmType, int64_t rank, int64_t pos)
static Value insertOne(ConversionPatternRewriter &rewriter, const LLVMTypeConverter &typeConverter, Location loc, Value val1, Value val2, Type llvmType, int64_t rank, int64_t pos)
static Value getAsLLVMValue(OpBuilder &builder, Location loc, OpFoldResult foldResult)
Convert foldResult into a Value.
static LogicalResult isMemRefTypeSupported(MemRefType memRefType, const LLVMTypeConverter &converter)
static VectorType reducedVectorTypeBack(VectorType tp)
@ None
#define MINUI(lhs, rhs)
static void printOp(llvm::raw_ostream &os, Operation *op, OpPrintingFlags &flags)
Definition: Unit.cpp:19
Attributes are known-constant values of operations.
Definition: Attributes.h:25
IntegerAttr getI32IntegerAttr(int32_t value)
Definition: Builders.cpp:220
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition: Builders.cpp:242
FloatAttr getFloatAttr(Type type, double value)
Definition: Builders.cpp:265
IntegerType getI64Type()
Definition: Builders.cpp:89
IntegerType getI32Type()
Definition: Builders.cpp:87
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:91
TypedAttr getZeroAttr(Type type)
Definition: Builders.cpp:335
MLIRContext * getContext() const
Definition: Builders.h:55
IndexType getIndexType()
Definition: Builders.cpp:75
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
PatternRewriter hook for replacing an operation.
void eraseOp(Operation *op) override
PatternRewriter hook for erasing a dead operation.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:143
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition: Pattern.h:147
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:34
LLVM::LLVMDialect * getDialect() const
Returns the LLVM dialect.
Definition: TypeConverter.h:92
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
FailureOr< unsigned > getMemRefAddressSpace(BaseMemRefType type) const
Return the LLVM address space corresponding to the memory space of the memref type type or failure if...
const llvm::DataLayout & getDataLayout() const
Returns the data layout to use during and after conversion.
Utility class to translate MLIR LLVM dialect types to LLVM IR.
Definition: TypeToLLVM.h:39
unsigned getPreferredAlignment(Type type, const llvm::DataLayout &layout)
Returns the preferred alignment for the type given the data layout.
Definition: TypeToLLVM.cpp:193
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Helper class to produce LLVM dialect operations extracting or inserting elements of a MemRef descript...
Definition: MemRefBuilder.h:33
static MemRefDescriptor undef(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
LLVM::LLVMPointerType getElementPtrType()
Returns the (LLVM) pointer type this descriptor contains.
Generic implementation of one-to-one conversion from "SourceOp" to "TargetOp" where the latter belong...
Definition: Pattern.h:198
This class helps build Operations.
Definition: Builders.h:210
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:468
This class represents a single result from folding an operation.
Definition: OpDefinition.h:268
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Value getOperand(unsigned idx)
Definition: Operation.h:345
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:847
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,...
Definition: PatternMatch.h:718
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:536
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isIntOrIndex() const
Return true if this is an integer (of any signedness) or an index type.
Definition: Types.cpp:116
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:126
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
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:129
LogicalResult handleMultidimensionalVectors(Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter, std::function< Value(Type, ValueRange)> createOperand, ConversionPatternRewriter &rewriter)
void printType(Type type, AsmPrinter &printer)
Prints an LLVM Dialect type.
void nDVectorIterate(const NDVectorTypeInfo &info, OpBuilder &builder, function_ref< void(ArrayRef< int64_t >)> fun)
NDVectorTypeInfo extractNDVectorTypeInfo(VectorType vectorType, const LLVMTypeConverter &converter)
Type getVectorType(Type elementType, unsigned numElements, bool isScalable=false)
Creates an LLVM dialect-compatible vector type with the given element type and length.
Definition: LLVMTypes.cpp:926
LLVM::LLVMFuncOp lookupOrCreatePrintI64Fn(Operation *moduleOp)
Helper functions to lookup or create the declaration for commonly used external C function calls.
LLVM::LLVMFuncOp lookupOrCreatePrintBF16Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF16Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintF32Fn(Operation *moduleOp)
void createPrintStrCall(OpBuilder &builder, Location loc, ModuleOp moduleOp, StringRef symbolName, StringRef string, const LLVMTypeConverter &typeConverter, bool addNewline=true, std::optional< StringRef > runtimeFunctionName={})
Generate IR that prints the given string to stdout.
LLVM::LLVMFuncOp lookupOrCreatePrintF64Fn(Operation *moduleOp)
LLVM::LLVMFuncOp lookupOrCreatePrintU64Fn(Operation *moduleOp)
LLVM::FastmathFlags convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF)
Maps arithmetic fastmath enum values to LLVM enum values.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:285
void populateVectorTransferLoweringPatterns(RewritePatternSet &patterns, std::optional< unsigned > maxTransferRank=std::nullopt, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
SmallVector< int64_t > getAsIntegers(ArrayRef< Value > values)
Returns the integer numbers in values.
Definition: VectorOps.cpp:315
void populateVectorInsertExtractStridedSliceTransforms(RewritePatternSet &patterns, PatternBenefit benefit=1)
Populate patterns with the following patterns.
Include the generated interface declarations.
bool isLastMemrefDimUnitStride(MemRefType type)
Return "true" if the last dimension of the given type has a static unit stride.
LogicalResult getStridesAndOffset(MemRefType t, SmallVectorImpl< int64_t > &strides, int64_t &offset)
Returns the strides of the MemRef if the layout map is in strided form.
void populateVectorToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns, bool reassociateFPReductions=false, bool force32BitVectorIndices=false)
Collect a set of patterns to convert from the Vector dialect to LLVM.
void populateVectorToLLVMMatrixConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to convert from Vector contractions to LLVM Matrix Intrinsics.
Value getValueOrCreateCastToIndexLike(OpBuilder &b, Location loc, Type targetType, Value value)
Create a cast from an index-like value (index or integer) to another index-like value.
Definition: Utils.cpp:120
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
SmallVector< OpFoldResult > getMixedValues(ArrayRef< int64_t > staticValues, ValueRange dynamicValues, Builder &b)
Return a vector of OpFoldResults with the same size a staticValues, but all elements for which Shaped...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:358