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 | |
17 | #include "glow/Graph/Hook.h" |
18 | |
19 | #include "glow/Graph/Graph.h" |
20 | #include "glow/Graph/Node.h" |
21 | |
22 | #include "llvm/ADT/DenseMap.h" |
23 | #include "llvm/Support/FormatVariadic.h" |
24 | |
25 | using namespace glow; |
26 | |
27 | HookedFunction glow::hookNode(Function *F, Node *node, bool hookInputs) { |
28 | NodeMap currToNew; |
29 | auto *newF = F->getParent()->createFunction("hook" ); |
30 | Node *hooked = recursiveClone(newF, node, currToNew); |
31 | |
32 | std::list<SaveNode *> output_saves; |
33 | std::list<Placeholder *> output_placeholders; |
34 | |
35 | std::list<SaveNode *> input_saves; |
36 | std::list<Placeholder *> input_placeholders; |
37 | |
38 | for (unsigned i = 0; i < hooked->getNumResults(); ++i) { |
39 | auto *save = |
40 | newF->createSave(hooked->getOutputName(i), hooked->getNthResult(i)); |
41 | output_saves.emplace_back(save); |
42 | output_placeholders.emplace_back(save->getPlaceholder()); |
43 | } |
44 | |
45 | if (hookInputs) { |
46 | for (unsigned i = 0; i < hooked->getNumInputs(); ++i) { |
47 | auto *save = |
48 | newF->createSave(hooked->getInputName(i), hooked->getNthInput(i)); |
49 | input_saves.emplace_back(save); |
50 | input_placeholders.emplace_back(save->getPlaceholder()); |
51 | } |
52 | } |
53 | return HookedFunction{newF, std::move(output_saves), |
54 | std::move(output_placeholders), std::move(input_saves), |
55 | std::move(input_placeholders)}; |
56 | } |
57 | |
58 | HookedFunction glow::hookNode(Function *F, llvm::StringRef nodeName, |
59 | bool hookInputs) { |
60 | auto *node = F->getNodeByName(nodeName); |
61 | return hookNode(F, node, hookInputs); |
62 | } |
63 | |