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_IR_IRBUILDER_H |
17 | #define GLOW_IR_IRBUILDER_H |
18 | |
19 | #include "glow/Base/Type.h" |
20 | #include "glow/IR/IR.h" |
21 | #include "glow/IR/Instrs.h" |
22 | |
23 | #include "llvm/ADT/ArrayRef.h" |
24 | #include "llvm/ADT/StringRef.h" |
25 | |
26 | namespace glow { |
27 | |
28 | /// The IRBuilder constructs the IR in the function. |
29 | class IRBuilder { |
30 | using MutabilityKind = WeightVar::MutabilityKind; |
31 | |
32 | /// The function that we are building. |
33 | IRFunction *F_; |
34 | |
35 | /// \returns a unique legal name that's based on the string \p name. Legal |
36 | /// names are legal C identifiers in the form: "[a-zA-Z_][a-zA-Z0-9_]*". |
37 | llvm::StringRef uniqueName(llvm::StringRef name) { |
38 | return F_->uniqueName(name); |
39 | } |
40 | |
41 | public: |
42 | explicit IRBuilder(IRFunction *F) : F_(F) {} |
43 | |
44 | ~IRBuilder(); |
45 | |
46 | /// \returns the function of the current builder. |
47 | IRFunction &getIRFunction() { |
48 | assert(F_); |
49 | return *F_; |
50 | } |
51 | |
52 | /// @name High-level, operation-level IRBuilder. |
53 | ///@{ |
54 | |
55 | MaxPoolWithArgmaxInst *createMaxPoolWithArgmaxOp( |
56 | llvm::StringRef name, Value *input, llvm::ArrayRef<unsigned_t> kernels, |
57 | llvm::ArrayRef<unsigned_t> strides, llvm::ArrayRef<unsigned_t> pads, |
58 | unsigned_t layout, ElemKind argMaxIndicesTy); |
59 | |
60 | ArgMaxInst *createArgMaxOp(llvm::StringRef name, Value *input, |
61 | unsigned_t axis, bool keepDims, |
62 | ElemKind outIndicesTy); |
63 | |
64 | AvgPoolInst *createAvgPoolOp(Value *input, llvm::ArrayRef<unsigned_t> kernels, |
65 | llvm::ArrayRef<unsigned_t> strides, |
66 | llvm::ArrayRef<unsigned_t> pads, |
67 | unsigned_t layout, bool countIncludePads); |
68 | |
69 | CrossEntropyLossInst *createCrossEntropyLossOp(llvm::StringRef name, Value *P, |
70 | Value *labels); |
71 | |
72 | TensorViewInst *createTensorView(ElemKind elemKind, |
73 | llvm::ArrayRef<dim_t> dims, Value *src, |
74 | llvm::StringRef name, |
75 | llvm::ArrayRef<dim_t> offsets = {}); |
76 | |
77 | LocalResponseNormalizationInst *createLocalResponseNormalizationOp( |
78 | llvm::StringRef name, Value *input, size_t halfWindowSize = 2, |
79 | float alpha = 1e-4, float beta = 0.75, float k = 2.0); |
80 | |
81 | TopKInst *createTopKOp(llvm::StringRef name, Value *input, size_t k, |
82 | ElemKind outIndicesTy); |
83 | |
84 | Value *createReturnOp(Value *input); |
85 | |
86 | ///@} |
87 | |
88 | /// @name Low-level, instruction-level IRBuilder. |
89 | ///@{ |
90 | |
91 | WeightVar *createWeightVar(TypeRef T, llvm::StringRef name = "" , |
92 | MutabilityKind m = MutabilityKind::Mutable); |
93 | |
94 | WeightVar *createWeightVar(ElemKind elemTy, llvm::ArrayRef<dim_t> dims, |
95 | llvm::StringRef name = "" , |
96 | MutabilityKind m = MutabilityKind::Mutable); |
97 | |
98 | WeightVar *createWeightVar(ElemKind elemTy, llvm::ArrayRef<dim_t> dims, |
99 | float scale, int32_t offset, |
100 | llvm::StringRef name = "" , |
101 | MutabilityKind m = MutabilityKind::Mutable); |
102 | |
103 | AllocActivationInst *createAllocActivationInst(llvm::StringRef name, |
104 | ElemKind elemTy, |
105 | llvm::ArrayRef<dim_t> dims); |
106 | |
107 | // Import the auto-generated instruction creation methods: |
108 | #include "glow/AutoGenIRBuilder.h" |
109 | |
110 | ///@} |
111 | |
112 | /// Inserts the deallocation instructions for all 'alloc' instructions that |
113 | /// need to be terminated. |
114 | void deallocateActiveInstrs(); |
115 | }; |
116 | |
117 | } // namespace glow |
118 | |
119 | #endif // GLOW_IR_IRBUILDER_H |
120 | |