Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref-counted ModuleSlotTracker #614

Merged
merged 2 commits into from
May 4, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bin/*
# build directories for cmake
build/
build_*/
build-*/

# LLVM project
llvm-project/*
Expand Down
42 changes: 32 additions & 10 deletions lib/PhasarLLVM/Utils/LLVMShorthands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <charconv>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <optional>
#include <system_error>

Expand Down Expand Up @@ -504,32 +505,44 @@ llvm::StringRef getVarAnnotationIntrinsicName(const llvm::CallInst *CallInst) {
return Data->getAsCString();
}

struct PhasarModuleSlotTrackerWrapper {
PhasarModuleSlotTrackerWrapper(const llvm::Module *M) : MST(M) {}

llvm::ModuleSlotTracker MST;
size_t RefCount = 0;
};

static llvm::SmallDenseMap<const llvm::Module *,
std::unique_ptr<llvm::ModuleSlotTracker>, 2>
std::unique_ptr<PhasarModuleSlotTrackerWrapper>, 2>
MToST{};

static std::mutex MSTMx;

llvm::ModuleSlotTracker &
ModulesToSlotTracker::getSlotTrackerForModule(const llvm::Module *M) {
std::lock_guard Lck(MSTMx);

auto &Ret = MToST[M];
if (M == nullptr && Ret == nullptr) {
Ret = std::make_unique<llvm::ModuleSlotTracker>(M);
Ret = std::make_unique<PhasarModuleSlotTrackerWrapper>(M);
Ret->RefCount++;
}
assert(Ret != nullptr && "no ModuleSlotTracker instance for module cached");
return *Ret;
return Ret->MST;
}

void ModulesToSlotTracker::setMSTForModule(const llvm::Module *M) {
std::lock_guard Lck(MSTMx);

auto [It, Inserted] = MToST.try_emplace(M, nullptr);
if (!Inserted) {
llvm::report_fatal_error(
"Cannot register the same module twice in the ModulesToSlotTracker! "
"Probably you have managed the same LLVM Module with multiple "
"ProjectIRDB instances at the same time. Don't do that!");
if (Inserted) {
It->second = std::make_unique<PhasarModuleSlotTrackerWrapper>(M);
}
It->second = std::make_unique<llvm::ModuleSlotTracker>(M);
It->second->RefCount++;
}

void ModulesToSlotTracker::updateMSTForModule(const llvm::Module *Module) {
std::lock_guard Lck(MSTMx);
auto It = MToST.find(Module);
if (It == MToST.end()) {
llvm::report_fatal_error(
Expand All @@ -541,7 +554,16 @@ void ModulesToSlotTracker::updateMSTForModule(const llvm::Module *Module) {
}

void ModulesToSlotTracker::deleteMSTForModule(const llvm::Module *M) {
MToST.erase(M);
std::lock_guard Lck(MSTMx);

auto It = MToST.find(M);
if (It == MToST.end()) {
return;
}

if (--It->second->RefCount == 0) {
MToST.erase(It);
}
}

} // namespace psr
19 changes: 19 additions & 0 deletions unittests/Utils/LLVMShorthandsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "phasar/PhasarLLVM/DB/LLVMProjectIRDB.h"
#include "phasar/Utils/Utilities.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"

Expand Down Expand Up @@ -43,6 +44,24 @@ TEST(LLVMGetterTest, HandlesLLVMTermInstruction) {
ASSERT_EQ(getNthTermInstruction(F, 5), nullptr);
}

TEST(SlotTrackerTest, HandleTwoReferences) {
LLVMProjectIRDB IRDB(unittest::PathToLLTestFiles +
"control_flow/global_stmt_cpp.ll");

const auto *F = IRDB.getFunctionDefinition("main");

ASSERT_NE(F, nullptr);
const auto *Inst = getNthInstruction(F, 6);
llvm::StringRef InstStr = "%0 = load i32, i32* @i, align 4 | ID: 6";
{
LLVMProjectIRDB IRDB2(IRDB.getModule());

EXPECT_EQ(llvmIRToStableString(Inst), InstStr);
}

EXPECT_EQ(llvmIRToStableString(Inst), InstStr);
}

int main(int Argc, char **Argv) {
::testing::InitGoogleTest(&Argc, Argv);
return RUN_ALL_TESTS();
Expand Down