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_LLVMIRCODEGEN_BUNDLESAVER_H |
17 | #define GLOW_LLVMIRCODEGEN_BUNDLESAVER_H |
18 | |
19 | #include "glow/LLVMIRCodeGen/AllocationsInfo.h" |
20 | #include "glow/LLVMIRCodeGen/LLVMIRGen.h" |
21 | |
22 | #include "glow/IR/IR.h" |
23 | |
24 | namespace glow { |
25 | class LLVMBackend; |
26 | |
27 | class BundleSaver { |
28 | public: |
29 | /// Information about a saved IR function. |
30 | struct SavedIRFunction { |
31 | /// Entry name for the IR function. |
32 | std::string entryName; |
33 | /// Saved IRFunction. |
34 | const IRFunction *savedF{nullptr}; |
35 | /// LLVM IR function created for this IR function. |
36 | llvm::Function *llvmF{nullptr}; |
37 | }; |
38 | /// WeightInfo represents a constant weight and a constant it is produced |
39 | /// from. |
40 | using WeightInfo = std::pair<const WeightVar *, const Constant *>; |
41 | /// Comparator for WeightInfo objects, sorting them by their allocated |
42 | /// address. |
43 | class WeightAddrComparator { |
44 | public: |
45 | WeightAddrComparator(BundleSaver &bundleSaver) |
46 | : bundleSaver_(&bundleSaver) {} |
47 | bool operator()(const WeightInfo &LHS, const WeightInfo &RHS) const; |
48 | |
49 | private: |
50 | const BundleSaver *bundleSaver_; |
51 | }; |
52 | /// Ctor. |
53 | explicit BundleSaver(const LLVMBackend &llvmBackend, |
54 | llvm::StringRef outputDir, llvm::StringRef bundleName); |
55 | /// Dtor. |
56 | virtual ~BundleSaver() = default; |
57 | virtual void save(llvm::StringRef mainEntryName, const IRFunction *F); |
58 | /// Produce a bundle. |
59 | virtual void produceBundle(); |
60 | |
61 | /// Set flag for saving weights. |
62 | /// \p val set the flag to this value. |
63 | void setSaveWeights(bool val) { saveWeights_ = val; } |
64 | /// Set flag for saving header. |
65 | /// \p val set the flag to this value. |
66 | void (bool val) { saveHeader_ = val; } |
67 | /// Set flag for saving weights as text. |
68 | /// \p val set the flag to this value. |
69 | void setSaveWeightsAsText(bool val) { saveWeightsAsText_ = val; } |
70 | |
71 | /// Get flag for saving weights. |
72 | bool getSaveWeights() const { return saveWeights_; } |
73 | /// Get flag for saving header. |
74 | bool () const { return saveHeader_; } |
75 | /// Get flag for saving weights as text. |
76 | bool getSaveWeightsAsText() const { return saveWeightsAsText_; } |
77 | |
78 | const AllocationsInfo &getAllocationsInfo() const { return allocationsInfo_; } |
79 | |
80 | protected: |
81 | /// Perform memory allocation for a bundle. |
82 | virtual void performBundleMemoryAllocation(); |
83 | /// Create bundle archive by archiving additional object files to the existing |
84 | /// bundle from \p bundlePath. The object names to be archived are given by |
85 | /// \p bundleObjects and the object raw content is taken from the object array |
86 | /// \p bundleObjectRegistry. |
87 | virtual void createBundleArchive( |
88 | llvm::StringRef bundlePath, |
89 | llvm::ArrayRef<llvm::MemoryBufferRef> bundleObjectRegistry, |
90 | const std::vector<std::string> &bundleObjects); |
91 | /// Save weights for the bundle. |
92 | virtual void saveWeights(llvm::StringRef weightsFileName); |
93 | /// Save header file for the bundle. |
94 | virtual void (llvm::StringRef ); |
95 | /// Emit config for a bundle. |
96 | virtual void emitBundleConfig(); |
97 | /// Emit the symbol table for a bundle. |
98 | virtual void emitSymbolTable(); |
99 | /// Emit the entry function for the saved function \p savedF. |
100 | virtual void emitBundleEntryFunction(SavedIRFunction &savedF); |
101 | /// Set current IRFunction. |
102 | virtual void setIRFunction(llvm::StringRef mainEntryName, |
103 | const IRFunction *F); |
104 | /// Returns a set of placeholders associated with IR functions inside this |
105 | /// bundle. |
106 | virtual std::set<const Placeholder *> findPlaceholders() const; |
107 | /// Returns a set of constant weights associated with IR functions inside this |
108 | /// bundle. |
109 | virtual std::set<WeightInfo, WeightAddrComparator> |
110 | findConstantWeights() const; |
111 | /// \returns the weight that the variable \p v is lowered into in one of the |
112 | /// IR functions inside this bundle, or null if the variable is unknown. |
113 | virtual Value *getWeightForNode(const Storage *V) const; |
114 | /// \returns LLVMIRGen used by the bundle saver. |
115 | virtual LLVMIRGen *getLLVMIRGen(); |
116 | /// Information about allocations. |
117 | AllocationsInfo allocationsInfo_; |
118 | /// The LLVM IR code generator. |
119 | std::unique_ptr<LLVMIRGen> irgen_; |
120 | /// Information about IR functions inside this bundle. |
121 | std::vector<SavedIRFunction> savedIRFunctions_; |
122 | /// Bundle API to use. |
123 | BundleApiType bundleAPI_; |
124 | /// Indicates if this bundle was saved already. |
125 | bool isSaved_{false}; |
126 | /// Flag for saving weights (to binary file). |
127 | bool saveWeights_{true}; |
128 | /// Flag for saving header. |
129 | bool {true}; |
130 | /// Flag for saving weights as text. |
131 | bool saveWeightsAsText_{true}; |
132 | }; |
133 | |
134 | } // namespace glow |
135 | |
136 | #endif // GLOW_LLVMIRCODEGEN_BUNDLESAVER_H |
137 | |