MLIR 23.0.0git
LegalizeForLLVMExport.cpp
Go to the documentation of this file.
1//===- LegalizeForLLVMExport.cpp - Prepare X86 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
10
16
17using namespace mlir;
18using namespace mlir::x86;
19
20namespace {
21
22/// Generic one-to-one conversion of simply mappable operations into calls
23/// to their respective LLVM intrinsics.
24struct X86IntrinsicOpConversion
25 : public ConvertOpInterfaceToLLVMPattern<x86::X86IntrinsicOp> {
27
28 LogicalResult
29 matchAndRewrite(x86::X86IntrinsicOp op, ArrayRef<Value> operands,
30 ConversionPatternRewriter &rewriter) const override {
31 const LLVMTypeConverter &typeConverter = *getTypeConverter();
33 op, rewriter.getStringAttr(op.getIntrinsicName()),
34 op.getIntrinsicOperands(operands, typeConverter, rewriter),
35 typeConverter, rewriter);
36 }
37};
38
39} // namespace
40
41/// Populate the given list with patterns that convert from X86 to LLVM.
43 LLVMTypeConverter &converter, RewritePatternSet &patterns) {
44 patterns.add<X86IntrinsicOpConversion>(converter);
45 converter.addConversion([&](x86::amx::TileType type) {
46 return LLVM::LLVMX86AMXType::get(&converter.getContext());
47 });
48}
49
51 target.addIllegalDialect<X86Dialect>();
52}
53
54namespace {
55/// Implement the interface to convert X86 to LLVM.
56struct X86ToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
58
59 void populateConvertToLLVMConversionPatterns(
61 RewritePatternSet &patterns) const final {
62 populateX86LegalizeForLLVMExportPatterns(typeConverter, patterns);
63 }
64};
65} // namespace
66
68 registry.addExtension(+[](MLIRContext *ctx, x86::X86Dialect *dialect) {
69 dialect->addInterfaces<X86ToLLVMDialectInterface>();
70 });
71}
Utility class for operation conversions targeting the LLVM dialect that allows for matching and rewri...
Definition Pattern.h:287
ConvertOpInterfaceToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:289
Base class for dialect interfaces providing translation to LLVM IR.
ConvertToLLVMPatternInterface(Dialect *dialect)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Derived class that automatically populates legalization information for different LLVM ops.
Conversion from types to the LLVM IR dialect.
MLIRContext & getContext() const
Returns the MLIR context.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
LogicalResult intrinsicRewrite(Operation *op, StringRef intrinsic, ValueRange operands, const LLVMTypeConverter &typeConverter, RewriterBase &rewriter)
Replaces the given operation "op" with a call to an LLVM intrinsic with the specified name "intrinsic...
Definition Pattern.cpp:352
mlir::x86::AMXTileType TileType
Definition X86Dialect.h:40
Include the generated interface declarations.
void configureX86LegalizeForExportTarget(LLVMConversionTarget &target)
Configure the target to support lowering X86 ops to ops that map to LLVM intrinsics.
void populateX86LegalizeForLLVMExportPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to lower X86 ops to ops that map to LLVM intrinsics.
void registerConvertX86ToLLVMInterface(DialectRegistry &registry)
Register LLVM conversion interface for X86 dialect.