1 | //===- LegalizeForLLVMExport.cpp - Prepare X86Vector for LLVM translation -===// |
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 | #include "mlir/Dialect/X86Vector/Transforms.h" |
10 | |
11 | #include "mlir/Conversion/LLVMCommon/ConversionTarget.h" |
12 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
13 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
14 | #include "mlir/Dialect/X86Vector/X86VectorDialect.h" |
15 | #include "mlir/IR/BuiltinOps.h" |
16 | #include "mlir/IR/PatternMatch.h" |
17 | |
18 | using namespace mlir; |
19 | using namespace mlir::x86vector; |
20 | |
21 | namespace { |
22 | |
23 | /// Generic one-to-one conversion of simply mappable operations into calls |
24 | /// to their respective LLVM intrinsics. |
25 | struct X86IntrinsicOpConversion |
26 | : public OpInterfaceConversionPattern<x86vector::X86IntrinsicOp> { |
27 | using OpInterfaceConversionPattern< |
28 | x86vector::X86IntrinsicOp>::OpInterfaceConversionPattern; |
29 | |
30 | X86IntrinsicOpConversion(const LLVMTypeConverter &typeConverter, |
31 | PatternBenefit benefit = 1) |
32 | : OpInterfaceConversionPattern(typeConverter, &typeConverter.getContext(), |
33 | benefit), |
34 | typeConverter(typeConverter) {} |
35 | |
36 | LogicalResult |
37 | matchAndRewrite(x86vector::X86IntrinsicOp op, ArrayRef<Value> operands, |
38 | ConversionPatternRewriter &rewriter) const override { |
39 | return LLVM::detail::intrinsicRewrite( |
40 | op: op, intrinsic: rewriter.getStringAttr(bytes: op.getIntrinsicName()), |
41 | operands: op.getIntrinsicOperands(operands, typeConverter, rewriter), |
42 | typeConverter, rewriter); |
43 | } |
44 | |
45 | private: |
46 | const LLVMTypeConverter &typeConverter; |
47 | }; |
48 | |
49 | } // namespace |
50 | |
51 | /// Populate the given list with patterns that convert from X86Vector to LLVM. |
52 | void mlir::populateX86VectorLegalizeForLLVMExportPatterns( |
53 | const LLVMTypeConverter &converter, RewritePatternSet &patterns) { |
54 | patterns.add<X86IntrinsicOpConversion>(arg: converter); |
55 | } |
56 | |
57 | void mlir::configureX86VectorLegalizeForExportTarget( |
58 | LLVMConversionTarget &target) { |
59 | target.addIllegalDialect<X86VectorDialect>(); |
60 | } |
61 | |