Mercurial > hg > CbC > CbC_llvm
comparison tools/bugpoint/OptimizerDriver.cpp @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 3a76565eade5 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// | 1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 // | 8 // |
10 // This file defines an interface that allows bugpoint to run various passes | 9 // This file defines an interface that allows bugpoint to run various passes |
11 // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint | 10 // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint |
14 // If this client dies, we can always fork a new one. :) | 13 // If this client dies, we can always fork a new one. :) |
15 // | 14 // |
16 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
17 | 16 |
18 #include "BugDriver.h" | 17 #include "BugDriver.h" |
18 #include "ToolRunner.h" | |
19 #include "llvm/Bitcode/BitcodeWriter.h" | 19 #include "llvm/Bitcode/BitcodeWriter.h" |
20 #include "llvm/IR/DataLayout.h" | 20 #include "llvm/IR/DataLayout.h" |
21 #include "llvm/IR/Module.h" | 21 #include "llvm/IR/Module.h" |
22 #include "llvm/Support/CommandLine.h" | 22 #include "llvm/Support/CommandLine.h" |
23 #include "llvm/Support/Debug.h" | 23 #include "llvm/Support/Debug.h" |
77 } | 77 } |
78 | 78 |
79 bool BugDriver::writeProgramToFile(const std::string &Filename, | 79 bool BugDriver::writeProgramToFile(const std::string &Filename, |
80 const Module &M) const { | 80 const Module &M) const { |
81 std::error_code EC; | 81 std::error_code EC; |
82 ToolOutputFile Out(Filename, EC, sys::fs::F_None); | 82 ToolOutputFile Out(Filename, EC, sys::fs::OF_None); |
83 if (!EC) | 83 if (!EC) |
84 return writeProgramToFileAux(Out, M); | 84 return writeProgramToFileAux(Out, M); |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
164 return 1; | 164 return 1; |
165 } | 165 } |
166 | 166 |
167 std::string tool = OptCmd; | 167 std::string tool = OptCmd; |
168 if (OptCmd.empty()) { | 168 if (OptCmd.empty()) { |
169 if (ErrorOr<std::string> Path = sys::findProgramByName("opt")) | 169 if (ErrorOr<std::string> Path = |
170 FindProgramByName("opt", getToolName(), &OutputPrefix)) | |
170 tool = *Path; | 171 tool = *Path; |
171 else | 172 else |
172 errs() << Path.getError().message() << "\n"; | 173 errs() << Path.getError().message() << "\n"; |
173 } | 174 } |
174 if (tool.empty()) { | 175 if (tool.empty()) { |
192 errs() << "Cannot find `valgrind' in PATH!\n"; | 193 errs() << "Cannot find `valgrind' in PATH!\n"; |
193 return 1; | 194 return 1; |
194 } | 195 } |
195 | 196 |
196 // setup the child process' arguments | 197 // setup the child process' arguments |
197 SmallVector<const char *, 8> Args; | 198 SmallVector<StringRef, 8> Args; |
198 if (UseValgrind) { | 199 if (UseValgrind) { |
199 Args.push_back("valgrind"); | 200 Args.push_back("valgrind"); |
200 Args.push_back("--error-exitcode=1"); | 201 Args.push_back("--error-exitcode=1"); |
201 Args.push_back("-q"); | 202 Args.push_back("-q"); |
202 Args.push_back(tool.c_str()); | 203 Args.push_back(tool); |
203 } else | 204 } else |
204 Args.push_back(tool.c_str()); | 205 Args.push_back(tool); |
205 | 206 |
206 for (unsigned i = 0, e = OptArgs.size(); i != e; ++i) | 207 for (unsigned i = 0, e = OptArgs.size(); i != e; ++i) |
207 Args.push_back(OptArgs[i].c_str()); | 208 Args.push_back(OptArgs[i]); |
208 Args.push_back("-disable-symbolication"); | 209 Args.push_back("-disable-symbolication"); |
209 Args.push_back("-o"); | 210 Args.push_back("-o"); |
210 Args.push_back(OutputFilename.c_str()); | 211 Args.push_back(OutputFilename); |
211 std::vector<std::string> pass_args; | 212 std::vector<std::string> pass_args; |
212 for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { | 213 for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { |
213 pass_args.push_back(std::string("-load")); | 214 pass_args.push_back(std::string("-load")); |
214 pass_args.push_back(PluginLoader::getPlugin(i)); | 215 pass_args.push_back(PluginLoader::getPlugin(i)); |
215 } | 216 } |
222 I != E; ++I) | 223 I != E; ++I) |
223 Args.push_back(I->c_str()); | 224 Args.push_back(I->c_str()); |
224 Args.push_back(Temp->TmpName.c_str()); | 225 Args.push_back(Temp->TmpName.c_str()); |
225 for (unsigned i = 0; i < NumExtraArgs; ++i) | 226 for (unsigned i = 0; i < NumExtraArgs; ++i) |
226 Args.push_back(*ExtraArgs); | 227 Args.push_back(*ExtraArgs); |
227 Args.push_back(nullptr); | 228 |
228 | 229 LLVM_DEBUG(errs() << "\nAbout to run:\t"; |
229 DEBUG(errs() << "\nAbout to run:\t"; | 230 for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs() |
230 for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs() | 231 << " " << Args[i]; |
231 << " " << Args[i]; | 232 errs() << "\n";); |
232 errs() << "\n";); | |
233 | 233 |
234 Optional<StringRef> Redirects[3] = {None, None, None}; | 234 Optional<StringRef> Redirects[3] = {None, None, None}; |
235 // Redirect stdout and stderr to nowhere if SilencePasses is given. | 235 // Redirect stdout and stderr to nowhere if SilencePasses is given. |
236 if (SilencePasses) { | 236 if (SilencePasses) { |
237 Redirects[1] = ""; | 237 Redirects[1] = ""; |
238 Redirects[2] = ""; | 238 Redirects[2] = ""; |
239 } | 239 } |
240 | 240 |
241 std::string ErrMsg; | 241 std::string ErrMsg; |
242 int result = sys::ExecuteAndWait(Prog, Args.data(), nullptr, Redirects, | 242 int result = sys::ExecuteAndWait(Prog, Args, None, Redirects, Timeout, |
243 Timeout, MemoryLimit, &ErrMsg); | 243 MemoryLimit, &ErrMsg); |
244 | 244 |
245 // If we are supposed to delete the bitcode file or if the passes crashed, | 245 // If we are supposed to delete the bitcode file or if the passes crashed, |
246 // remove it now. This may fail if the file was never created, but that's ok. | 246 // remove it now. This may fail if the file was never created, but that's ok. |
247 if (DeleteOutput || result != 0) | 247 if (DeleteOutput || result != 0) |
248 sys::fs::remove(OutputFilename); | 248 sys::fs::remove(OutputFilename); |