MLIR  20.0.0git
TosaTypeConverters.cpp
Go to the documentation of this file.
1 
2 //===- TosaTypeConverters.cpp ---------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Type converters for lowering TOSA to linalg/arith.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
17 
18 using namespace mlir;
19 
21  converter.addConversion([&](Type type) -> std::optional<Type> {
22  if (type.isUnsignedInteger()) {
23  return IntegerType::get(type.getContext(), type.getIntOrFloatBitWidth(),
24  IntegerType::SignednessSemantics::Signless);
25  }
26  return type;
27  });
28  converter.addConversion([&](TensorType type) -> std::optional<Type> {
29  auto converted = converter.convertType(type.getElementType());
30  if (!converted)
31  return {};
32  return type.clone(converted);
33  });
34  converter.addSourceMaterialization([&](OpBuilder &builder, Type resultType,
35  ValueRange inputs,
36  Location loc) -> std::optional<Value> {
37  if (inputs.size() != 1)
38  return std::nullopt;
39 
40  return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
41  .getResult(0);
42  });
43  converter.addTargetMaterialization([&](OpBuilder &builder, Type resultType,
44  ValueRange inputs,
45  Location loc) -> std::optional<Value> {
46  if (inputs.size() != 1)
47  return std::nullopt;
48 
49  return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
50  .getResult(0);
51  });
52 }
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
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:402
Tensor types represent multi-dimensional arrays, and have two variants: RankedTensorType and Unranked...
Definition: BuiltinTypes.h:102
Type conversion class.
void addConversion(FnT &&callback)
Register a conversion function.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
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
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition: Types.cpp:99
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
void populateTosaTypeConversion(TypeConverter &converter)
Include the generated interface declarations.