MLIR  21.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 
11 #include "mlir/IR/BuiltinTypes.h"
13 #include <optional>
14 
15 using namespace mlir;
16 
17 namespace {
18 
19 Value materializeAsUnrealizedCast(OpBuilder &builder, Type resultType,
20  ValueRange inputs, Location loc) {
21  if (inputs.size() != 1)
22  return Value();
23 
24  return builder.create<UnrealizedConversionCastOp>(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.
39 std::optional<Type> mlir::emitc::getUnsignedTypeFor(Type ty) {
40  if (ty.isInteger())
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.
52 std::optional<Type> mlir::emitc::getSignedTypeFor(Type ty) {
53  if (ty.isInteger())
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:66
This class helps build Operations.
Definition: Builders.h:205
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
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 replacement value back ...
void addTargetMaterialization(FnT &&callback)
This method registers a materialization that will be called when converting a value to a target type ...
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:47
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:114
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
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)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...