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/IR/Builders.h"
19 #include "mlir/IR/BuiltinOps.h"
20 #include "mlir/IR/BuiltinTypes.h"
21 #include "mlir/IR/TypeUtilities.h"
22 #include "mlir/IR/Types.h"
23 #include "mlir/IR/Value.h"
24
25 namespace mlir {
26
27 struct Value;
28
29 namespace mhlo {
30
31 namespace {
32
convertInteger(IntegerType int_type)33 Type convertInteger(IntegerType int_type) {
34 return IntegerType::get(int_type.getContext(),
35 int_type.getIntOrFloatBitWidth());
36 }
37
convertShapedType(ShapedType shaped_type)38 Type convertShapedType(ShapedType shaped_type) {
39 if (auto int_type = shaped_type.getElementType().dyn_cast<IntegerType>())
40 return shaped_type.clone(convertInteger(int_type));
41 return shaped_type;
42 }
43
materializeCastFromIllegal(OpBuilder & builder,Type type,ValueRange inputs,Location loc)44 llvm::Optional<Value> materializeCastFromIllegal(OpBuilder& builder, Type type,
45 ValueRange inputs,
46 Location loc) {
47 Type from_type = getElementTypeOrSelf(inputs[0].getType());
48 Type to_type = getElementTypeOrSelf(type);
49 if ((!from_type.isSignedInteger() && !from_type.isUnsignedInteger()) ||
50 !to_type.isSignlessInteger())
51 return llvm::None;
52 // Use unrealized conversion casts to do signful->signless conversions.
53 return builder.create<UnrealizedConversionCastOp>(loc, type, inputs[0])
54 ->getResult(0);
55 }
56
materializeCastToIllegal(OpBuilder & builder,Type type,ValueRange inputs,Location loc)57 llvm::Optional<Value> materializeCastToIllegal(OpBuilder& builder, Type type,
58 ValueRange inputs,
59 Location loc) {
60 Type from_type = getElementTypeOrSelf(inputs[0].getType());
61 Type to_type = getElementTypeOrSelf(type);
62 if (!from_type.isSignlessInteger() ||
63 (!to_type.isSignedInteger() && !to_type.isUnsignedInteger()))
64 return llvm::None;
65 // Use unrealized conversion casts to do signless->signful conversions.
66 return builder.create<UnrealizedConversionCastOp>(loc, type, inputs[0])
67 ->getResult(0);
68 }
69
70 } // namespace
71
RemoveSignTypeConverter()72 RemoveSignTypeConverter::RemoveSignTypeConverter() {
73 addConversion([](Type type) { return type; });
74
75 addConversion(convertInteger);
76 addConversion(convertShapedType);
77
78 addArgumentMaterialization(materializeCastFromIllegal);
79 addSourceMaterialization(materializeCastToIllegal);
80 addTargetMaterialization(materializeCastFromIllegal);
81 }
82
83 } // namespace mhlo
84 } // namespace mlir
85