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/Optimizer/Lower/Lower.h" |
18 | #include "glow/Backend/Backend.h" |
19 | #include "glow/Graph/Graph.h" |
20 | #include "glow/Graph/Node.h" |
21 | #include "glow/Graph/Nodes.h" |
22 | #include "glow/Graph/TensorLayout.h" |
23 | #include "glow/Optimizer/GraphOptimizer/FunctionPasses.h" |
24 | #include "glow/Optimizer/GraphOptimizer/GraphOptimizer.h" |
25 | |
26 | #include "llvm/Support/Casting.h" |
27 | |
28 | #include <numeric> |
29 | |
30 | using namespace glow; |
31 | using llvm::dyn_cast; |
32 | |
33 | void glow::lower(Function *F, CompilationContext &cctx, const Backend *B, |
34 | const KindSet &doNotLowerKinds) { |
35 | LOG_SCOPE(F->getLogContext(), "glow::lower" ) |
36 | F->setState(FunctionState::FuncLoaded); |
37 | |
38 | auto &nodes = F->getNodes(); |
39 | for (auto &N : nodes) { |
40 | if (B && !B->shouldLower(&N)) { |
41 | continue; |
42 | } |
43 | if (doNotLowerKinds.count(N.getKind())) { |
44 | continue; |
45 | } |
46 | lowerNode(F, &N, cctx); |
47 | } |
48 | |
49 | for (auto it = F->getNodes().begin(), e = F->getNodes().end(); it != e;) { |
50 | auto cur = &*(it++); |
51 | if (dyn_cast<SGDNode>(cur)) { |
52 | F->eraseNode(cur); |
53 | } |
54 | } |
55 | |
56 | // Remove nodes that were lowered. |
57 | runDCEPass(F, cctx); |
58 | } |
59 | |