1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "mlir-hlo/Dialect/mhlo/transforms/type_conversion.h"
17
18 #include "mlir/Dialect/Tensor/IR/Tensor.h"
19 #include "mlir/IR/Builders.h"
20 #include "mlir/IR/BuiltinOps.h"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/TypeUtilities.h"
23 #include "mlir/IR/Types.h"
24 #include "mlir/IR/Value.h"
25
26 namespace mlir {
27
28 class Value;
29
30 namespace mhlo {
31
32 namespace {
33
convertInteger(IntegerType intType)34 Type convertInteger(IntegerType intType) {
35 return IntegerType::get(intType.getContext(),
36 intType.getIntOrFloatBitWidth());
37 }
38
convertShapedType(ShapedType shapedType)39 Type convertShapedType(ShapedType shapedType) {
40 if (auto intType = shapedType.getElementType().dyn_cast<IntegerType>())
41 return shapedType.clone(convertInteger(intType));
42 return shapedType;
43 }
44
materializeCastFromIllegal(OpBuilder & builder,Type type,ValueRange inputs,Location loc)45 llvm::Optional<Value> materializeCastFromIllegal(OpBuilder& builder, Type type,
46 ValueRange inputs,
47 Location loc) {
48 Type fromType = getElementTypeOrSelf(inputs[0].getType());
49 Type toType = getElementTypeOrSelf(type);
50 if ((!fromType.isSignedInteger() && !fromType.isUnsignedInteger()) ||
51 !toType.isSignlessInteger())
52 return llvm::None;
53 // Use unrealized conversion casts to do signful->signless conversions.
54 return builder.create<UnrealizedConversionCastOp>(loc, type, inputs[0])
55 ->getResult(0);
56 }
57
materializeCastToIllegal(OpBuilder & builder,Type type,ValueRange inputs,Location loc)58 llvm::Optional<Value> materializeCastToIllegal(OpBuilder& builder, Type type,
59 ValueRange inputs,
60 Location loc) {
61 Type fromType = getElementTypeOrSelf(inputs[0].getType());
62 Type toType = getElementTypeOrSelf(type);
63 if (!fromType.isSignlessInteger() ||
64 (!toType.isSignedInteger() && !toType.isUnsignedInteger()))
65 return llvm::None;
66 // Use unrealized conversion casts to do signless->signful conversions.
67 return builder.create<UnrealizedConversionCastOp>(loc, type, inputs[0])
68 ->getResult(0);
69 }
70
scalarToTensor(OpBuilder & builder,Type,ValueRange inputs,Location loc)71 llvm::Optional<Value> scalarToTensor(OpBuilder& builder, Type /*type*/,
72 ValueRange inputs, Location loc) {
73 assert(inputs.size() == 1);
74 if (inputs.front().getType().isa<ShapedType>()) {
75 return llvm::None;
76 }
77 return builder
78 .create<tensor::FromElementsOp>(
79 loc, RankedTensorType::get({}, inputs.front().getType()),
80 inputs.front())
81 .getResult();
82 }
83
84 } // namespace
85
RemoveSignTypeConverter()86 RemoveSignTypeConverter::RemoveSignTypeConverter() {
87 addConversion([](Type type) { return type; });
88
89 addConversion(convertInteger);
90 addConversion(convertShapedType);
91
92 addArgumentMaterialization(materializeCastFromIllegal);
93 addSourceMaterialization(materializeCastToIllegal);
94 addTargetMaterialization(materializeCastFromIllegal);
95 }
96
LinalgTypeConverter()97 LinalgTypeConverter::LinalgTypeConverter() : RemoveSignTypeConverter() {
98 addArgumentMaterialization(scalarToTensor);
99 }
100
101 } // namespace mhlo
102 } // namespace mlir
103