alive-tv: allow specification of LLVM passes
MODIFY
CMakeLists.txt
CMakeLists.txt
@@ -169,6 +169,7 @@ if (BUILD_LLVM_UTILS OR BUILD_TV)
set(LLVM_UTIL_SRCS
"llvm_util/known_fns.cpp"
+ "llvm_util/llvm_optimizer.cpp"
"llvm_util/llvm2alive.cpp"
"llvm_util/utils.cpp"
)

ADD
llvm_util/llvm_optimizer.cpp
+// Copyright (c) 2018-present The Alive2 Authors.
+// Distributed under the MIT license that can be found in the LICENSE file.
+
+#include "llvm_optimizer.h"
+#include "llvm/Passes/PassBuilder.h"
+
+using namespace llvm;
+
+namespace llvm_util {
+
+void optimize_module(llvm::Module *M, llvm::StringRef optArgs) {
+ llvm::LoopAnalysisManager LAM;
+ llvm::FunctionAnalysisManager FAM;
+ llvm::CGSCCAnalysisManager CGAM;
+ llvm::ModuleAnalysisManager MAM;
+ llvm::PassBuilder PB;
+
+ llvm::ModulePassManager MPM;
+
+ PB.registerModuleAnalyses(MAM);
+ PB.registerCGSCCAnalyses(CGAM);
+ PB.registerFunctionAnalyses(FAM);
+ PB.registerLoopAnalyses(LAM);
+ PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
+
+ if (optArgs == "O3") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O3);
+ } else if (optArgs == "O2") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O2);
+ } else if (optArgs == "O1") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O1);
+ } else if (optArgs == "O0") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::O0);
+ } else if (optArgs == "Os") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::Os);
+ } else if (optArgs == "Oz") {
+ MPM = PB.buildPerModuleDefaultPipeline(OptimizationLevel::Oz);
+ } else {
+ if (auto E = PB.parsePassPipeline(MPM, optArgs)) {
+ llvm::errs() << E << "\n";
+ }
+ }
+ MPM.run(*M, MAM);
+}
+
+}

ADD
llvm_util/llvm_optimizer.h
+#pragma once
+
+// Copyright (c) 2018-present The Alive2 Authors.
+// Distributed under the MIT license that can be found in the LICENSE file.
+
+#include <llvm/ADT/StringRef.h>
+
+namespace llvm {
+class Module;
+}
+
+namespace llvm_util {
+void optimize_module(llvm::Module *M, llvm::StringRef optArgs);
+}

MODIFY
tools/alive-tv.cpp
tools/alive-tv.cpp
// Distributed under the MIT license that can be found in the LICENSE file.
#include "llvm_util/llvm2alive.h"
+#include "llvm_util/llvm_optimizer.h"
#include "smt/smt.h"
#include "tools/transform.h"
#include "util/version.h"
@@ -58,6 +59,12 @@ llvm::cl::opt<std::string> opt_tgt_fn(LLVM_ARGS_PREFIX"tgt-fn",
llvm::cl::desc("Name of tgt function (without @)"),
llvm::cl::cat(alive_cmdargs), llvm::cl::init("tgt"));
+llvm::cl::opt<string>
+ optPass(LLVM_ARGS_PREFIX "passes",
+ llvm::cl::value_desc("optimization passes"),
+ llvm::cl::desc("a parameter to specify optimization passes, separated by comma"),
+ llvm::cl::cat(alive_cmdargs),llvm::cl::init("O2"));
+
llvm::ExitOnError ExitOnErr;
@@ -237,26 +244,6 @@ bool compareFunctions(llvm::Function &F1, llvm::Function &F2,
return true;
}
-void optimizeModule(llvm::Module *M) {
- llvm::LoopAnalysisManager LAM;
- llvm::FunctionAnalysisManager FAM;
- llvm::CGSCCAnalysisManager CGAM;
- llvm::ModuleAnalysisManager MAM;
-
- llvm::PassBuilder PB;
- PB.registerModuleAnalyses(MAM);
- PB.registerCGSCCAnalyses(CGAM);
- PB.registerFunctionAnalyses(FAM);
- PB.registerLoopAnalyses(LAM);
- PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
-
- llvm::FunctionPassManager FPM = PB.buildFunctionSimplificationPipeline(
- llvm::OptimizationLevel::O2, llvm::ThinOrFullLTOPhase::None);
- llvm::ModulePassManager MPM;
- MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
- MPM.run(*M, MAM);
-}
-
llvm::Function *findFunction(llvm::Module &M, const string &FName) {
for (auto &F : M) {
if (F.isDeclaration())
@@ -269,6 +256,7 @@ llvm::Function *findFunction(llvm::Module &M, const string &FName) {
}
}
+
int main(int argc, char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
llvm::PrettyStackTraceProgram X(argc, argv);
@@ -330,7 +318,7 @@ convenient way to demonstrate an existing optimizer bug.
goto end;
} else {
M2 = CloneModule(*M1);
- optimizeModule(M2.get());
+ optimize_module(M2.get(), optPass);
}
} else {
M2 = openInputFile(Context, opt_file2);

alive-tv: allow specification of LLVM passes (#795)