MLIR  15.0.0git
LinalgToLLVM.cpp
Go to the documentation of this file.
1 //===- LinalgToLLVM.cpp - conversion from Linalg to 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 
11 #include "../PassDetail.h"
24 #include "mlir/IR/AffineExpr.h"
25 #include "mlir/IR/AffineMap.h"
26 #include "mlir/IR/Attributes.h"
27 #include "mlir/IR/Builders.h"
28 #include "mlir/IR/BuiltinOps.h"
29 #include "mlir/IR/BuiltinTypes.h"
30 #include "mlir/IR/MLIRContext.h"
31 #include "mlir/IR/Operation.h"
32 #include "mlir/IR/PatternMatch.h"
33 #include "mlir/IR/Types.h"
36 #include "mlir/Transforms/Passes.h"
37 #include "llvm/ADT/SetVector.h"
38 #include "llvm/IR/DerivedTypes.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/ErrorHandling.h"
43 
44 using namespace mlir;
45 using namespace mlir::LLVM;
46 using namespace mlir::linalg;
47 
48 template <typename T>
49 static Type getPtrToElementType(T containerType, LLVMTypeConverter &lowering) {
50  return LLVMPointerType::get(
51  lowering.convertType(containerType.getElementType()));
52 }
53 
54 namespace {
55 // YieldOp produces and LLVM::ReturnOp.
56 class YieldOpConversion : public ConvertOpToLLVMPattern<linalg::YieldOp> {
57 public:
59 
61  matchAndRewrite(linalg::YieldOp op, OpAdaptor adaptor,
62  ConversionPatternRewriter &rewriter) const override {
63  rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, adaptor.getOperands());
64  return success();
65  }
66 };
67 } // namespace
68 
69 /// Populate the given list with patterns that convert from Linalg to LLVM.
71  RewritePatternSet &patterns) {
72  patterns.add<YieldOpConversion>(converter);
73 }
74 
75 namespace {
76 struct ConvertLinalgToLLVMPass
77  : public ConvertLinalgToLLVMBase<ConvertLinalgToLLVMPass> {
78  void runOnOperation() override;
79 };
80 } // namespace
81 
82 void ConvertLinalgToLLVMPass::runOnOperation() {
83  auto module = getOperation();
84 
85  // Convert to the LLVM IR dialect using the converter defined above.
86  RewritePatternSet patterns(&getContext());
87  LLVMTypeConverter converter(&getContext());
88  populateLinalgToLLVMConversionPatterns(converter, patterns);
89  populateMemRefToLLVMConversionPatterns(converter, patterns);
90 
91  LLVMConversionTarget target(getContext());
92  target.addLegalOp<ModuleOp>();
93  if (failed(applyPartialConversion(module, target, std::move(patterns))))
94  signalPassFailure();
95 }
96 
97 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertLinalgToLLVMPass() {
98  return std::make_unique<ConvertLinalgToLLVMPass>();
99 }
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition: Pattern.h:132
void addLegalOp(OperationName op)
Register the given operations as legal.
LogicalResult applyPartialConversion(ArrayRef< Operation *> ops, ConversionTarget &target, const FrozenRewritePatternSet &patterns, DenseSet< Operation *> *unconvertedOps=nullptr)
Below we define several entry points for operation conversion.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results)
Convert the given type.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
Derived class that automatically populates legalization information for different LLVM ops...
void populateMemRefToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to convert memory-related operations from the MemRef dialect to the LLVM di...
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
void populateLinalgToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Populate the given list with patterns that convert from Linalg to LLVM.
static LLVMPointerType get(MLIRContext *context, unsigned addressSpace=0)
Gets or creates an instance of LLVM dialect pointer type pointing to an object of pointee type in the...
Definition: LLVMTypes.cpp:188
std::unique_ptr< OperationPass< ModuleOp > > createConvertLinalgToLLVMPass()
Create a pass to convert Linalg operations to the LLVMIR dialect.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&... args)
Add an instance of each of the pattern types &#39;Ts&#39; to the pattern list with the given arguments...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replaces the result op with a new op that is created without verification.
Definition: PatternMatch.h:451
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:30
This class implements a pattern rewriter for use with ConversionPatterns.
static Type getPtrToElementType(T containerType, LLVMTypeConverter &lowering)