MLIR  20.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/Pass/Pass.h"
20 
21 namespace mlir {
22 #define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
23 #include "mlir/Conversion/Passes.h.inc"
24 } // namespace mlir
25 
26 using namespace mlir;
27 
28 namespace {
29 struct ConvertMemRefToEmitCPass
30  : public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
31  void runOnOperation() override {
32  TypeConverter converter;
33 
34  // Fallback for other types.
35  converter.addConversion([](Type type) -> std::optional<Type> {
37  return type;
38  return {};
39  });
40 
42 
43  auto materializeAsUnrealizedCast = [](OpBuilder &builder, Type resultType,
44  ValueRange inputs,
45  Location loc) -> Value {
46  if (inputs.size() != 1)
47  return Value();
48 
49  return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
50  .getResult(0);
51  };
52 
53  converter.addSourceMaterialization(materializeAsUnrealizedCast);
54  converter.addTargetMaterialization(materializeAsUnrealizedCast);
55 
56  RewritePatternSet patterns(&getContext());
57  populateMemRefToEmitCConversionPatterns(patterns, converter);
58 
59  ConversionTarget target(getContext());
60  target.addIllegalDialect<memref::MemRefDialect>();
61  target.addLegalDialect<emitc::EmitCDialect>();
62 
63  if (failed(applyPartialConversion(getOperation(), target,
64  std::move(patterns))))
65  return signalPassFailure();
66  }
67 };
68 } // namespace
static MLIRContext * getContext(OpFoldResult val)
This class describes a specific conversion target.
void addLegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as legal.
void addIllegalDialect(StringRef name, Names... names)
Register the operations of the given dialects as illegal, i.e.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class helps build Operations.
Definition: Builders.h:215
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:497
Type conversion class.
void addConversion(FnT &&callback)
Register a conversion function.
void addSourceMaterialization(FnT &&callback)
This method registers a materialization that will be called when converting a legal replacement value...
void addTargetMaterialization(FnT &&callback)
This method registers a materialization that will be called when converting an illegal (source) value...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
bool isSupportedEmitCType(mlir::Type type)
Determines whether type is valid in EmitC.
Definition: EmitC.cpp:61
Include the generated interface declarations.
void populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter)
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.