MLIR 23.0.0git
TypeConverter.cpp
Go to the documentation of this file.
1//===- TypeConverter.cpp - Convert builtin to EmitC dialect types ---------===//
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
13#include "llvm/ADT/STLExtras.h"
14
15using namespace mlir;
16
17namespace {
18
19static bool isMemRefTypeLegalForEmitC(MemRefType memRefType) {
20 return memRefType.hasStaticShape() && memRefType.getLayout().isIdentity() &&
21 memRefType.getRank() != 0 &&
22 !llvm::is_contained(memRefType.getShape(), 0);
23}
24
25static Value materializeAsUnrealizedCast(OpBuilder &builder, Type resultType,
26 ValueRange inputs, Location loc) {
27 if (inputs.size() != 1)
28 return Value();
29
30 return UnrealizedConversionCastOp::create(builder, loc, resultType, inputs)
31 .getResult(0);
32}
33
34} // namespace
35
37 (void)ctx;
38
39 addConversion([](Type type) -> std::optional<Type> {
41 return {};
42 return type;
43 });
44
45 addConversion([&](MemRefType memRefType) -> std::optional<Type> {
46 if (!isMemRefTypeLegalForEmitC(memRefType))
47 return {};
48
49 Type convertedElementType = convertType(memRefType.getElementType());
50 if (!convertedElementType)
51 return {};
52
53 return emitc::ArrayType::get(memRefType.getShape(), convertedElementType);
54 });
55
56 addSourceMaterialization(materializeAsUnrealizedCast);
57 addTargetMaterialization(materializeAsUnrealizedCast);
58}
static bool isMemRefTypeLegalForEmitC(MemRefType memRefType)
EmitCTypeConverter(MLIRContext *ctx)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class helps build Operations.
Definition Builders.h:209
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:389
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:62
Include the generated interface declarations.