MLIR 24.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 !llvm::is_contained(memRefType.getShape(), 0);
22}
23
24static Value materializeAsUnrealizedCast(OpBuilder &builder, Type resultType,
25 ValueRange inputs, Location loc) {
26 if (inputs.size() != 1)
27 return Value();
28
29 return UnrealizedConversionCastOp::create(builder, loc, resultType, inputs)
30 .getResult(0);
31}
32
33} // namespace
34
36 (void)ctx;
37
38 addConversion([](Type type) -> std::optional<Type> {
40 return {};
41 return type;
42 });
43
44 addConversion([&](MemRefType memRefType) -> std::optional<Type> {
45 if (!isMemRefTypeLegalForEmitC(memRefType))
46 return {};
47
48 Type convertedElementType = convertType(memRefType.getElementType());
49 if (!convertedElementType)
50 return {};
51
52 if (memRefType.getRank() == 0)
53 return emitc::PointerType::get(convertedElementType);
54 return emitc::ArrayType::get(memRefType.getShape(), convertedElementType);
55 });
56
57 addSourceMaterialization(materializeAsUnrealizedCast);
58 addTargetMaterialization(materializeAsUnrealizedCast);
59}
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:210
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.