Skip to content

Commit

Permalink
Misc fixes to make Souper work better with SPEC 2017 (google#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
manasij7479 committed Sep 14, 2021
1 parent ab9a314 commit 9342448
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build_deps.sh
Expand Up @@ -75,7 +75,7 @@ mkdir -p $llvm_srcdir

mkdir -p $llvm_builddir

cmake_flags="-DCMAKE_INSTALL_PREFIX=$llvm_installdir -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_FORCE_ENABLE_STATS=ON -DCMAKE_BUILD_TYPE=$llvm_build_type -DLLVM_ENABLE_Z3_SOLVER=OFF -DLLVM_ENABLE_PROJECTS=\'llvm;clang;compiler-rt\'"
cmake_flags="-DCMAKE_INSTALL_PREFIX=$llvm_installdir -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_FORCE_ENABLE_STATS=ON -DCMAKE_BUILD_TYPE=$llvm_build_type -DLLVM_ENABLE_Z3_SOLVER=OFF -DLLVM_ENABLE_PROJECTS=\'llvm;clang;openmp;compiler-rt\'"

if [ -n "`which ninja`" ] ; then
(cd $llvm_builddir && cmake ${llvm_srcdir}/llvm -G Ninja $cmake_flags -DCMAKE_CXX_FLAGS="-DDISABLE_WRONG_OPTIMIZATIONS_DEFAULT_VALUE=true -DDISABLE_PEEPHOLES_DEFAULT_VALUE=false" "$@")
Expand Down
2 changes: 1 addition & 1 deletion include/souper/Inst/Inst.h
Expand Up @@ -315,7 +315,7 @@ Inst *getInstCopy(Inst *I, InstContext &IC,
std::map<Inst *, Inst *> &InstCache,
std::map<Block *, Block *> &BlockCache,
std::map<Inst *, llvm::APInt> *ConstMap,
bool CloneVars);
bool CloneVars, bool CloneBlocks = true);

Inst *instJoin(Inst *I, Inst *Reserved, Inst *NewInst,
std::map<Inst *, Inst *> &InstCache, InstContext &IC);
Expand Down
4 changes: 2 additions & 2 deletions lib/Infer/EnumerativeSynthesis.cpp
Expand Up @@ -725,7 +725,7 @@ std::error_code synthesizeWithKLEE(SynthesisContext &SC, std::vector<Inst *> &RH
if (!ResultConstMap.empty()) {
std::map<Inst *, Inst *> InstCache;
std::map<Block *, Block *> BlockCache;
RHS = getInstCopy(I, SC.IC, InstCache, BlockCache, &ResultConstMap, false);
RHS = getInstCopy(I, SC.IC, InstCache, BlockCache, &ResultConstMap, false, false);
} else {
continue;
}
Expand Down Expand Up @@ -761,7 +761,7 @@ std::error_code synthesizeWithKLEE(SynthesisContext &SC, std::vector<Inst *> &RH
}
std::map<Inst *, Inst *> InstCache;
std::map<Block *, Block *> BlockCache;
auto newRHS = getInstCopy(I, SC.IC, InstCache, BlockCache, &ZeroConstMap, false);
auto newRHS = getInstCopy(I, SC.IC, InstCache, BlockCache, &ZeroConstMap, false, false);
if (isTransformationValid(SC.LHS, newRHS, SC.PCs, SC.BPCs, SC.IC))
RHS = newRHS;
}
Expand Down
12 changes: 8 additions & 4 deletions lib/Inst/Inst.cpp
Expand Up @@ -1172,7 +1172,7 @@ Inst *souper::getInstCopy(Inst *I, InstContext &IC,
std::map<Inst *, Inst *> &InstCache,
std::map<Block *, Block *> &BlockCache,
std::map<Inst *, llvm::APInt> *ConstMap,
bool CloneVars) {
bool CloneVars, bool CloneBlocks) {

if (InstCache.count(I))
return InstCache.at(I);
Expand Down Expand Up @@ -1209,9 +1209,13 @@ Inst *souper::getInstCopy(Inst *I, InstContext &IC,
}
} else if (I->K == Inst::Phi) {
if (!BlockCache.count(I->B)) {
auto BlockCopy = IC.createBlock(I->B->Preds);
BlockCache[I->B] = BlockCopy;
Copy = IC.getPhi(BlockCopy, Ops, I->DemandedBits);
if (CloneBlocks) {
auto BlockCopy = IC.createBlock(I->B->Preds);
BlockCache[I->B] = BlockCopy;
Copy = IC.getPhi(BlockCopy, Ops, I->DemandedBits);
} else {
Copy = IC.getPhi(I->B, Ops, I->DemandedBits);
}
} else {
Copy = IC.getPhi(BlockCache.at(I->B), Ops, I->DemandedBits);
}
Expand Down
21 changes: 20 additions & 1 deletion lib/Pass/Pass.cpp
Expand Up @@ -18,8 +18,10 @@
#include "llvm/Analysis/DemandedBits.h"
#include "llvm/Analysis/LazyValueInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/CodeGen/UnreachableBlockElim.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
Expand All @@ -33,6 +35,7 @@
#include "llvm/IR/Verifier.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Scalar/ADCE.h"
#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Expand Down Expand Up @@ -233,6 +236,17 @@ struct SouperPass : public ModulePass {
if (!TLI)
report_fatal_error("getTLI() failed");

// Run UnreachableBlockElim and ADCE locally
// TODO: In the long run, switch this tool to the new pass manager.
FunctionPassManager FM;
FunctionAnalysisManager FAM;
FAM.registerPass([&] { return PassInstrumentationAnalysis(); });
FAM.registerPass([&] { return DominatorTreeAnalysis(); });
FAM.registerPass([&] { return PostDominatorTreeAnalysis(); });
FM.addPass(UnreachableBlockElimPass());
FM.addPass(ADCEPass());
FM.run(*F, FAM);

FunctionCandidateSet CS = ExtractCandidatesFromPass(F, LI, DB, LVI, SE, TLI, IC, EBC);

if (DebugLevel > 3)
Expand Down Expand Up @@ -277,7 +291,12 @@ struct SouperPass : public ModulePass {
EC == std::errc::value_too_large) {
continue;
} else {
report_fatal_error("Unable to query solver: " + EC.message() + "\n");
llvm::errs() << "[FIXME: Crash commented out]\nUnable to query solver: " + EC.message() + "\n";
continue;
// TODO: This is a temporary workaround to suppress a protocol error which is encountered
// once in SPEC 2017. This workaround does not have a negative effect other than maybe
// missing one potential transformation.
//report_fatal_error("Unable to query solver: " + EC.message() + "\n");
}
}
if (RHSs.empty())
Expand Down

0 comments on commit 9342448

Please sign in to comment.