MLIR 23.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
19#include "mlir/IR/Attributes.h"
20#include "mlir/Pass/Pass.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
24
25namespace mlir {
26#define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
27#include "mlir/Conversion/Passes.h.inc"
28} // namespace mlir
29
30using namespace mlir;
31
32namespace {
33
34emitc::IncludeOp addStandardHeader(OpBuilder &builder, ModuleOp module,
35 StringRef headerName) {
36 StringAttr includeAttr = builder.getStringAttr(headerName);
37 return emitc::IncludeOp::create(
38 builder, module.getLoc(), includeAttr,
39 /*is_standard_include=*/builder.getUnitAttr());
40}
41
42struct ConvertMemRefToEmitCPass
43 : public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
44 using Base::Base;
45 void runOnOperation() override {
46 EmitCTypeConverter converter(&getContext());
47 ConvertMemRefToEmitCOptions options;
48 options.lowerToCpp = this->lowerToCpp;
49
50 RewritePatternSet patterns(&getContext());
51 populateMemRefToEmitCConversionPatterns(patterns, converter);
52
53 ConversionTarget target(getContext());
54 target.addIllegalDialect<memref::MemRefDialect>();
55 target.addLegalDialect<emitc::EmitCDialect>();
56
57 if (failed(applyPartialConversion(getOperation(), target,
58 std::move(patterns))))
59 return signalPassFailure();
60
61 mlir::ModuleOp module = getOperation();
62 llvm::SmallSet<StringRef, 4> existingHeaders;
63 mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
64 module.walk([&](mlir::emitc::IncludeOp includeOp) {
65 if (includeOp.getIsStandardInclude())
66 existingHeaders.insert(includeOp.getInclude());
67 });
68
69 module.walk([&](mlir::emitc::CallOpaqueOp callOp) {
70 StringRef expectedHeader;
71 if (callOp.getCallee() == alignedAllocFunctionName ||
72 callOp.getCallee() == freeFunctionName ||
73 callOp.getCallee() == mallocFunctionName)
74 expectedHeader = options.lowerToCpp ? cppStandardLibraryHeader
76 else if (callOp.getCallee() == memcpyFunctionName)
77 expectedHeader =
79 else
81 if (!existingHeaders.contains(expectedHeader)) {
82 addStandardHeader(builder, module, expectedHeader);
83 existingHeaders.insert(expectedHeader);
84 }
86 });
87 }
88};
89} // 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 * freeFunctionName
constexpr const char * alignedAllocFunctionName
static llvm::ManagedStatic< PassManagerOptions > options
UnitAttr getUnitAttr()
Definition Builders.cpp:102
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:267
This class helps build Operations.
Definition Builders.h:209
static WalkResult advance()
Definition WalkResult.h:47
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:717
Include the generated interface declarations.
void populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns, const TypeConverter &converter)