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/GraphOptimizer/FunctionPassManager.h"
18#include "glow/Optimizer/GraphOptimizer/FunctionPasses.h"
19
20#include <glog/logging.h>
21
22namespace glow {
23
24/// The purpose of ThePassManager alias is to make the code of this pass manager
25/// look as similar to other pass managers as possible. Often the changes in one
26/// pass manager need to be replicated to other pass managaers. This alias makes
27/// it easier to copy changes among pass managers as the code looks almost
28/// identical.
29using ThePassManager = FunctionPassManager;
30
31/// Options for IRFunctionPassManager.
32static PassManagerOptions functionPassManagerOptions("graph");
33
34template <>
35PassManagerOptions &ThePassManager::options_ = functionPassManagerOptions;
36
37template <>
38void ThePassManager::dumpIR(IRContainer *C, llvm::raw_ostream &os,
39 const std::string &outputFileName) const {
40 static_cast<IRContainerTy *>(C)->dumpDAG(outputFileName);
41}
42
43std::unique_ptr<ThePassManager::IRPassTy>
44createFunctionPass(ThePassManager::PassIDTy passID) {
45 switch (passID) {
46#define FUN_PASS(PASS_NAME) \
47 case (ThePassManager::PassIDTy::PASS_NAME): \
48 return glow::make_unique<PASS_NAME>();
49#include "glow/Optimizer/GraphOptimizer/FunctionPasses.def"
50 }
51 llvm_unreachable("Unexpected pass.");
52}
53
54template <>
55std::unique_ptr<ThePassManager::IRPassTy>
56ThePassManager::createFunctionPass(PassIDTy passID) const {
57 return glow::createFunctionPass(passID);
58}
59
60template <>
61bool ThePassManager::runPassHook(const PassConfigBase &passConfig, PassBase &P,
62 IRContainer *C,
63 const CompilationContext &cctx) {
64 const IRPassConfigTy *thePassConfig =
65 static_cast<const IRPassConfigTy *>(&passConfig);
66 assert(
67 !(thePassConfig->getPassID() == PassIDTy::DCE &&
68 thePassConfig->getDCERequiredMode() == DCERequiredMode::BeforePass) &&
69 "Cannot specify DCE requires DCE before it.");
70 // Run DCE before this pass if it requires it.
71 if (thePassConfig->getDCERequiredMode() == DCERequiredMode::BeforePass) {
72 runPass(getDCEPassConfig(), static_cast<IRContainerTy *>(C), cctx);
73 }
74 return static_cast<IRPassTy *>(&P)->run(static_cast<IRContainerTy *>(C),
75 cctx);
76}
77
78bool runDCEPass(ThePassManager::IRContainerTy *F,
79 const CompilationContext &cctx) {
80 auto pipeline = glow::make_unique<FunctionPassPipeline>();
81 pipeline->pushBack(getDCEPassConfig());
82 return FunctionPassManager("DCE_FPM", std::move(pipeline)).run(F, cctx);
83}
84
85void test() { FunctionPassManager FPM("name", "pipeline.def"); }
86
87} // namespace glow
88