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 
24 namespace mlir {
25 #define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
26 #include "mlir/Conversion/Passes.h.inc"
27 } // namespace mlir
28 
29 using namespace mlir;
30 
31 namespace {
32 
33 emitc::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 
41 struct 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> {
50  if (!emitc::isSupportedEmitCType(type))
51  return {};
52  return type;
53  });
54 
56 
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
static MLIRContext * getContext(OpFoldResult val)
constexpr const char * mallocFunctionName
Definition: MemRefToEmitC.h:12
constexpr const char * cppStandardLibraryHeader
Definition: MemRefToEmitC.h:14
constexpr const char * cStringLibraryHeader
Definition: MemRefToEmitC.h:17
constexpr const char * cStandardLibraryHeader
Definition: MemRefToEmitC.h:15
constexpr const char * memcpyFunctionName
Definition: MemRefToEmitC.h:13
constexpr const char * cppStringLibraryHeader
Definition: MemRefToEmitC.h:16
constexpr const char * alignedAllocFunctionName
Definition: MemRefToEmitC.h:11
static llvm::ManagedStatic< PassManagerOptions > options
UnitAttr getUnitAttr()
Definition: Builders.cpp:97
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:261
This class describes a specific conversion target.
This class helps build Operations.
Definition: Builders.h:207
Type conversion class.
void addConversion(FnT &&callback)
Register a conversion function.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
static WalkResult advance()
Definition: WalkResult.h:47
bool isSupportedEmitCType(mlir::Type type)
Determines whether type is valid in EmitC.
Definition: EmitC.cpp:59
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
void populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter)
const FrozenRewritePatternSet & patterns
void populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns, const TypeConverter &converter)
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.