MLIR  20.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  converter.addArgumentMaterialization(materializeAsUnrealizedCast);
37 }
38 
39 /// Get an unsigned integer or size data type corresponding to \p ty.
40 std::optional<Type> mlir::emitc::getUnsignedTypeFor(Type ty) {
41  if (ty.isInteger())
43  IntegerType::SignednessSemantics::Unsigned);
44  if (isa<PtrDiffTType, SignedSizeTType>(ty))
45  return SizeTType::get(ty.getContext());
46  if (isa<SizeTType>(ty))
47  return ty;
48  return {};
49 }
50 
51 /// Get a signed integer or size data type corresponding to \p ty that supports
52 /// arithmetic on negative values.
53 std::optional<Type> mlir::emitc::getSignedTypeFor(Type ty) {
54  if (ty.isInteger())
57  if (isa<SizeTType, SignedSizeTType>(ty))
58  return PtrDiffTType::get(ty.getContext());
59  if (isa<PtrDiffTType>(ty))
60  return ty;
61  return {};
62 }
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 addArgumentMaterialization(FnT &&callback)
All of the following materializations require function objects that are convertible to the following ...
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
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:66
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:133
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...