1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_CONVERTER_TYPEATOTYPEBFUNCTIONCONVERTER_H
17#define GLOW_CONVERTER_TYPEATOTYPEBFUNCTIONCONVERTER_H
18
19#include "FunctionConverter.h"
20
21#include "glow/Base/Traits.h" // For KindSet.
22#include "glow/Base/Type.h"
23#include "glow/Optimizer/GraphOptimizer/CompilationContext.h"
24
25namespace glow {
26
27class Function;
28class Module;
29
30/// This helper class converts values of typeA into values of typeB.
31/// The nodes producing these values are morphed into the new type
32/// and proper conversions from/to typeA and typeB are inserted.
33class TypeAToTypeBFunctionConverter : public FunctionConverter {
34protected:
35 /// Module of the function to be converted.
36 Module &mod_;
37 /// Destination type of the conversions.
38 ElemKind dstKind_;
39 /// Source type of the conversions. I.e., the values of this
40 /// element type are going to be converted.
41 ElemKind srcKind_;
42 /// Precision configuration used during conversion.
43 const PrecisionConfiguration &precConfig_;
44
45 /// If the element type of \p out is srcKind_ returns a similarly shaped type
46 /// using dstKind_. Otherwise returns nullptr.
47 /// \see FunctionConverter::getTargetTypeForOutput to know how this
48 /// is used.
49 TypeRef getTargetTypeForOutput(const NodeValue &out) const override;
50
51 /// If the element type of the \p idx-th input of \p use is srcKind_
52 /// returns a similarly shaped type using dstKind_. Otherwise returns nullptr.
53 /// \see FunctionConverter::getTargetTypeForInput to know how this
54 /// is used.
55 TypeRef getTargetTypeForInput(const Node &use, unsigned idx) const override;
56
57 /// Create a node in \p function that converts \p val to \p destTy, given
58 /// context \p node. \p val and \p destTy must have the same shape.
59 Node *createConversion(Function &function, const Node &node, NodeValue &val,
60 TypeRef destTy, bool isInput) override;
61
62 /// Check if \p node can be converted.
63 bool canConvert(const Node &node) const override;
64
65 void convertTensor(Tensor &tensor, TypeRef destTy) override;
66
67 template <class T> static bool isBiasInput(NodeValue input, const Node *N);
68
69public:
70 /// Create a type converter from \p fromKind to \p toKind for \p F given
71 /// \p precConfig.
72 TypeAToTypeBFunctionConverter(Function &F, ElemKind fromKind, ElemKind toKind,
73 const PrecisionConfiguration &precConfig);
74
75 /// Convert and clip all Storage nodes used by the function.
76 void convertAndClipStorage();
77};
78} // namespace glow
79#endif
80