MLIR 22.0.0git
TypeConversions.cpp
Go to the documentation of this file.
1//===- TypeConversions.cpp - Convert signless types into C/C++ 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
13#include <optional>
14
15using namespace mlir;
16
17namespace {
18
19Value materializeAsUnrealizedCast(OpBuilder &builder, Type resultType,
20 ValueRange inputs, Location loc) {
21 if (inputs.size() != 1)
22 return Value();
23
24 return UnrealizedConversionCastOp::create(builder, loc, resultType, inputs)
25 .getResult(0);
26}
27
28} // namespace
29
31 converter.addConversion(
32 [](IndexType type) { return emitc::SizeTType::get(type.getContext()); });
33
34 converter.addSourceMaterialization(materializeAsUnrealizedCast);
35 converter.addTargetMaterialization(materializeAsUnrealizedCast);
36}
37
38/// Get an unsigned integer or size data type corresponding to \p ty.
39std::optional<Type> mlir::emitc::getUnsignedTypeFor(Type ty) {
40 if (ty.isInteger())
41 return IntegerType::get(ty.getContext(), ty.getIntOrFloatBitWidth(),
42 IntegerType::SignednessSemantics::Unsigned);
43 if (isa<PtrDiffTType, SignedSizeTType>(ty))
44 return SizeTType::get(ty.getContext());
45 if (isa<SizeTType>(ty))
46 return ty;
47 return {};
48}
49
50/// Get a signed integer or size data type corresponding to \p ty that supports
51/// arithmetic on negative values.
52std::optional<Type> mlir::emitc::getSignedTypeFor(Type ty) {
53 if (ty.isInteger())
54 return IntegerType::get(ty.getContext(), ty.getIntOrFloatBitWidth(),
55 IntegerType::SignednessSemantics::Signed);
56 if (isa<SizeTType, SignedSizeTType>(ty))
57 return PtrDiffTType::get(ty.getContext());
58 if (isa<PtrDiffTType>(ty))
59 return ty;
60 return {};
61}
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition Types.cpp:35
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:56
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:122
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
std::optional< Type > getSignedTypeFor(Type ty)
Get a signed integer or size data type corresponding to ty that supports arithmetic on negative value...
std::optional< Type > getUnsignedTypeFor(Type ty)
Get an unsigned integer or size data type corresponding to ty.
Include the generated interface declarations.
void populateEmitCSizeTTypeConversions(TypeConverter &converter)