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 | |
14 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
15 | |
16 | #include "mlir/Transforms/DialectConversion.h" |
17 | |
18 | using namespace mlir; |
19 | |
20 | void mlir::tosa::populateTosaTypeConversion(TypeConverter &converter) { |
21 | converter.addConversion(callback: [&](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(callback: [&](TensorType type) -> std::optional<Type> { |
29 | auto converted = converter.convertType(t: type.getElementType()); |
30 | if (!converted) |
31 | return {}; |
32 | return type.clone(converted); |
33 | }); |
34 | converter.addSourceMaterialization(callback: [&](OpBuilder &builder, Type resultType, |
35 | ValueRange inputs, |
36 | Location loc) -> Value { |
37 | if (inputs.size() != 1) |
38 | return Value(); |
39 | |
40 | return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs) |
41 | .getResult(0); |
42 | }); |
43 | converter.addTargetMaterialization(callback: [&](OpBuilder &builder, Type resultType, |
44 | ValueRange inputs, |
45 | Location loc) -> Value { |
46 | if (inputs.size() != 1) |
47 | return Value(); |
48 | |
49 | return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs) |
50 | .getResult(0); |
51 | }); |
52 | } |
53 | |