Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/phasar/Utils/LLVMShorthands.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/IR/Value.h"

#include "phasar/Utils/Utilities.h"
Expand Down Expand Up @@ -216,6 +217,23 @@ bool isVarAnnotationIntrinsic(const llvm::Function *F);
*
*/
llvm::StringRef getVarAnnotationIntrinsicName(const llvm::CallInst *CallInst);

class ModulesToSlotTracker {
friend class ProjectIRDB;
friend class LLVMBasedICFG;
friend class LLVMZeroValue;

private:
static inline llvm::SmallDenseMap<const llvm::Module *,
std::unique_ptr<llvm::ModuleSlotTracker>, 2>
MToST;

static void updateMSTForModule(const llvm::Module *);
static void deleteMSTForModule(const llvm::Module *);
Comment thread
vulder marked this conversation as resolved.

public:
static llvm::ModuleSlotTracker &getSlotTrackerForModule(const llvm::Module *);
};
} // namespace psr

#endif
6 changes: 6 additions & 0 deletions lib/DB/ProjectIRDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ProjectIRDB::ProjectIRDB(IRDBOptions Options) : Options(Options) {
MPM.addPass(ValueAnnotationPass());
// just to be sure that none of the passes messed up the module!
MPM.addPass(llvm::VerifierPass());
ModulesToSlotTracker::updateMSTForModule(LLVMZeroValueMod.get());
}

ProjectIRDB::ProjectIRDB(const std::vector<std::string> &IRFiles,
Expand Down Expand Up @@ -105,6 +106,9 @@ ProjectIRDB::ProjectIRDB(const std::vector<llvm::Module *> &Modules,
}

ProjectIRDB::~ProjectIRDB() {
for (auto &[File, Module] : Modules) {
ModulesToSlotTracker::deleteMSTForModule(Module.get());
}
// release resources if IRDB does not own
if (!(Options & IRDBOptions::OWNS)) {
for (auto &Context : Contexts) {
Expand Down Expand Up @@ -134,6 +138,7 @@ void ProjectIRDB::preprocessModule(llvm::Module *M) {
RetOrResInstructions.insert(RRInsts.begin(), RRInsts.end());
STOP_TIMER("LLVM Passes", PAMM_SEVERITY_LEVEL::Full);
buildIDModuleMapping(M);
ModulesToSlotTracker::updateMSTForModule(M);
}

void ProjectIRDB::linkForWPA() {
Expand Down Expand Up @@ -199,6 +204,7 @@ void ProjectIRDB::linkForWPA() {
// to link at all. But we have to update the WPAMOD pointer!
WPAModule = Modules.begin()->second.get();
}
ModulesToSlotTracker::updateMSTForModule(WPAModule);
}

void ProjectIRDB::preprocessAllModules() {
Expand Down
8 changes: 6 additions & 2 deletions lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,12 @@ LLVMBasedICFG::getReturnSitesOfCallAt(const llvm::Instruction *N) const {
if (const auto *Invoke = llvm::dyn_cast<llvm::InvokeInst>(N)) {
const llvm::Instruction *NormalSucc = &Invoke->getNormalDest()->front();
auto *UnwindSucc = &Invoke->getUnwindDest()->front();
if (!IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(NormalSucc)) {
if (!IgnoreDbgInstructions &&
llvm::isa<llvm::DbgInfoIntrinsic>(NormalSucc)) {
NormalSucc = NormalSucc->getNextNonDebugInstruction();
}
if (!IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(UnwindSucc)) {
if (!IgnoreDbgInstructions &&
llvm::isa<llvm::DbgInfoIntrinsic>(UnwindSucc)) {
UnwindSucc = UnwindSucc->getNextNonDebugInstruction();
}
if (NormalSucc != nullptr) {
Expand Down Expand Up @@ -947,6 +949,8 @@ LLVMBasedICFG::buildCRuntimeGlobalCtorsDtorsModel(llvm::Module &M) {
IRB.CreateRetVoid();
}

ModulesToSlotTracker::updateMSTForModule(&M);

return GlobModel;
}

Expand Down
29 changes: 19 additions & 10 deletions lib/Utils/LLVMShorthands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,10 @@ bool matchesSignature(const llvm::FunctionType *FType1,
return false;
}

static llvm::ModuleSlotTracker &getModuleSlotTrackerFor(const llvm::Value *V) {
static llvm::SmallDenseMap<const llvm::Module *,
std::unique_ptr<llvm::ModuleSlotTracker>, 2>
ModuleToSlotTracker;
llvm::ModuleSlotTracker &getModuleSlotTrackerFor(const llvm::Value *V) {
const auto *M = getModuleFromVal(V);

auto &ret = ModuleToSlotTracker[M];
if (!ret) {
ret = std::make_unique<llvm::ModuleSlotTracker>(M);
}

return *ret;
return ModulesToSlotTracker::getSlotTrackerForModule(M);
}

std::string llvmIRToString(const llvm::Value *V) {
Expand Down Expand Up @@ -437,4 +429,21 @@ llvm::StringRef getVarAnnotationIntrinsicName(const llvm::CallInst *CallInst) {
return data->getAsCString();
}

llvm::ModuleSlotTracker &
ModulesToSlotTracker::getSlotTrackerForModule(const llvm::Module *M) {
auto &ret = MToST[M];
if (M == nullptr && ret == nullptr) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an assertion to prevent returning nullptr if M != nullptr && ret == nullptr

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that might ease debugging in case it happens. However, if we return nullptr we will get a crash anyway.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, see below

ret = std::make_unique<llvm::ModuleSlotTracker>(M);
}
assert(ret != nullptr && "no ModuleSlotTracker instance for module cached");
return *ret;
}

void ModulesToSlotTracker::updateMSTForModule(const llvm::Module *M) {
MToST[M] = std::make_unique<llvm::ModuleSlotTracker>(M);
}
void ModulesToSlotTracker::deleteMSTForModule(const llvm::Module *M) {
MToST.erase(M);
}

} // namespace psr