MLIR 22.0.0git
MemRefToEmitCPass.cpp
Go to the documentation of this file.
1//===- MemRefToEmitC.cpp - MemRef to EmitC conversion ---------------------===//
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// This file implements a pass to convert memref ops into emitc ops.
10//
11//===----------------------------------------------------------------------===//
12
14
18#include "mlir/IR/Attributes.h"
19#include "mlir/Pass/Pass.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/StringRef.h"
23
24namespace mlir {
25#define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
26#include "mlir/Conversion/Passes.h.inc"
27} // namespace mlir
28
29using namespace mlir;
30
31namespace {
32
33emitc::IncludeOp addStandardHeader(OpBuilder &builder, ModuleOp module,
34 StringRef headerName) {
35 StringAttr includeAttr = builder.getStringAttr(headerName);
36 return emitc::IncludeOp::create(
37 builder, module.getLoc(), includeAttr,
38 /*is_standard_include=*/builder.getUnitAttr());
39}
40
41struct ConvertMemRefToEmitCPass
42 : public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
43 using Base::Base;
44 void runOnOperation() override {
45 TypeConverter converter;
46 ConvertMemRefToEmitCOptions options;
47 options.lowerToCpp = this->lowerToCpp;
48 // Fallback for other types.
49 converter.addConversion([](Type type) -> std::optional<Type> {
51 return {};
52 return type;
53 });
54
56
57 RewritePatternSet patterns(&getContext());
59
60 ConversionTarget target(getContext());
61 target.addIllegalDialect<memref::MemRefDialect>();
62 target.addLegalDialect<emitc::EmitCDialect>();
63
64 if (failed(applyPartialConversion(getOperation(), target,
65 std::move(patterns))))
66 return signalPassFailure();
67
68 mlir::ModuleOp module = getOperation();
69 llvm::SmallSet<StringRef, 4> existingHeaders;
70 mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
71 module.walk([&](mlir::emitc::IncludeOp includeOp) {
72 if (includeOp.getIsStandardInclude())
73 existingHeaders.insert(includeOp.getInclude());
74 });
75
76 module.walk([&](mlir::emitc::CallOpaqueOp callOp) {
77 StringRef expectedHeader;
78 if (callOp.getCallee() == alignedAllocFunctionName ||
79 callOp.getCallee() == mallocFunctionName)
80 expectedHeader = options.lowerToCpp ? cppStandardLibraryHeader
82 else if (callOp.getCallee() == memcpyFunctionName)
83 expectedHeader =
85 else
87 if (!existingHeaders.contains(expectedHeader)) {
88 addStandardHeader(builder, module, expectedHeader);
89 existingHeaders.insert(expectedHeader);
90 }
92 });
93 }
94};
95} // namespace
b getContext())
constexpr const char * mallocFunctionName
constexpr const char * cppStandardLibraryHeader
constexpr const char * cStringLibraryHeader
constexpr const char * cStandardLibraryHeader
constexpr const char * memcpyFunctionName
constexpr const char * cppStringLibraryHeader
constexpr const char * alignedAllocFunctionName
static llvm::ManagedStatic< PassManagerOptions > options
UnitAttr getUnitAttr()
Definition Builders.cpp:98
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:262
This class helps build Operations.
Definition Builders.h:207
static WalkResult advance()
Definition WalkResult.h:47
::mlir::Pass::Option< bool > lowerToCpp
bool isSupportedEmitCType(mlir::Type type)
Determines whether type is valid in EmitC.
Definition EmitC.cpp:61
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:561
Include the generated interface declarations.
void populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter)
const FrozenRewritePatternSet & patterns
void populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns, const TypeConverter &converter)