MLIR  22.0.0git
LegalizeForLLVMExport.cpp
Go to the documentation of this file.
1 //===- LegalizeForLLVMExport.cpp - Prepare AMX 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 
15 #include "mlir/IR/PatternMatch.h"
16 
17 using namespace mlir;
18 using namespace mlir::amx;
19 
20 namespace {
21 
22 /// Generic one-to-one conversion of simply mappable operations into calls
23 /// to their respective LLVM intrinsics.
24 struct AMXIntrinsicOpConversion
25  : public ConvertOpInterfaceToLLVMPattern<amx::AMXIntrinsicOp> {
27 
28  LogicalResult
29  matchAndRewrite(amx::AMXIntrinsicOp 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 
43  patterns.add<AMXIntrinsicOpConversion>(converter);
44  converter.addConversion([&](amx::TileType type) {
45  return LLVM::LLVMX86AMXType::get(&converter.getContext());
46  });
47 }
48 
50  target.addIllegalDialect<AMXDialect>();
51 }
52 
53 namespace {
54 /// Implement the interface to convert AMX to LLVM.
55 struct AMXToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
57 
58  void populateConvertToLLVMConversionPatterns(
59  ConversionTarget &target, LLVMTypeConverter &typeConverter,
60  RewritePatternSet &patterns) const final {
62  }
63 };
64 } // namespace
65 
67  registry.addExtension(+[](MLIRContext *ctx, amx::AMXDialect *dialect) {
68  dialect->addInterfaces<AMXToLLVMDialectInterface>();
69  });
70 }
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:261
This class implements a pattern rewriter for use with ConversionPatterns.
This class describes a specific conversion target.
void addIllegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as illegal, i.e.
Utility class for operation conversions targeting the LLVM dialect that allows for matching and rewri...
Definition: Pattern.h:257
ConvertOpInterfaceToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition: Pattern.h:259
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.
Definition: TypeConverter.h:35
MLIRContext & getContext() const
Returns the MLIR context.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:63
void addConversion(FnT &&callback)
Register a conversion function.
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:347
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
void populateAMXLegalizeForLLVMExportPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to lower AMX ops to ops that map to LLVM intrinsics.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void configureAMXLegalizeForExportTarget(LLVMConversionTarget &target)
Configure the target to support lowering AMX ops to ops that map to LLVM intrinsics.
void registerConvertAMXToLLVMInterface(DialectRegistry &registry)
Register LLVM conversion interface for AMX dialect.