Skip to content

Commit

Permalink
Refactor handling of DI type names
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbs96 committed Apr 20, 2024
1 parent 767a90e commit fe605e9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ class DIBasedTypeHierarchy

[[nodiscard]] const auto &getAllVTables() const noexcept { return VTables; }

[[nodiscard]] std::string getTypeName(ClassType Type) const override {
return Type->getName().str();
}
[[nodiscard]] std::string getTypeName(ClassType Type) const override;

[[nodiscard]] bool hasVFTable(ClassType Type) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ struct DIBasedTypeHierarchyData {
// DITypes and llvm::Function * are serialized by serializing their names and
// using the DebugInfoFinder to deserialize them

llvm::StringMap<std::string> NameToType;
llvm::StringMap<size_t> TypeToVertex;
std::vector<std::string> VertexTypes;
std::vector<std::pair<uint32_t, uint32_t>> TransitiveDerivedIndex;
std::vector<std::string> Hierarchy;
std::vector<std::vector<std::string>> VTables;
Expand Down
86 changes: 32 additions & 54 deletions lib/PhasarLLVM/TypeHierarchy/DIBasedTypeHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ static void buildTypeHierarchy(
}
}

static llvm::StringRef getCompositeTypeName(const llvm::DICompositeType *Ty) {
auto Name = Ty->getName();
return Name.empty() ? Ty->getIdentifier() : Name;
}

DIBasedTypeHierarchy::DIBasedTypeHierarchy(const LLVMProjectIRDB &IRDB) {
// -- Find all types
{
Expand Down Expand Up @@ -193,7 +198,9 @@ DIBasedTypeHierarchy::DIBasedTypeHierarchy(const LLVMProjectIRDB &IRDB) {
}
TypeToVertex.try_emplace(Composite, VertexTypes.size());
VertexTypes.push_back(Composite);
NameToType.try_emplace(Composite->getName(), Composite);
NameToType.try_emplace(getCompositeTypeName(Composite), Composite);

assert(!getCompositeTypeName(Composite).empty());
}
}

Expand All @@ -218,7 +225,7 @@ stringToDICompositeType(const LLVMProjectIRDB *IRDB,
const auto *Module = IRDB->getModule();
DIF.processModule(*Module);

for (const auto &Type : DIF.types()) {
for (const auto *Type : DIF.types()) {
if (const auto *DICT = llvm::dyn_cast<llvm::DICompositeType>(Type)) {
if (DICT->getName() == DITypeName) {
return DICT;
Expand All @@ -239,7 +246,7 @@ static const llvm::DIType *stringToDIType(const LLVMProjectIRDB *IRDB,
const auto *Module = IRDB->getModule();
DIF.processModule(*Module);

for (const auto &Type : DIF.types()) {
for (const auto *Type : DIF.types()) {
if (Type) {
if (Type->getName() == DITypeName) {
if (const auto *DIT = llvm::dyn_cast<llvm::DIType>(Type)) {
Expand All @@ -253,44 +260,32 @@ static const llvm::DIType *stringToDIType(const LLVMProjectIRDB *IRDB,
}

DIBasedTypeHierarchy::DIBasedTypeHierarchy(
const LLVMProjectIRDB *IRDB,
const DIBasedTypeHierarchyData &SerializedData) {

int Counter = 0;
VertexTypes.reserve(SerializedData.VertexTypes.size());

for (const auto &Curr : SerializedData.VertexTypes) {
VertexTypes.push_back(stringToDICompositeType(IRDB, Curr));
Counter++;
}

for (const auto &Curr : SerializedData.NameToType) {
NameToType.try_emplace(Curr.getKey(),
stringToDIType(IRDB, Curr.getValue()));
}
const LLVMProjectIRDB *IRDB, const DIBasedTypeHierarchyData &SerializedData)
: TransitiveDerivedIndex(SerializedData.TransitiveDerivedIndex) {

VertexTypes.resize(SerializedData.TypeToVertex.size());
TypeToVertex.reserve(SerializedData.TypeToVertex.size());
for (const auto &Curr : SerializedData.TypeToVertex) {
TypeToVertex.try_emplace(stringToDIType(IRDB, Curr.getKey()),
Curr.getValue());
}

for (const auto &Curr : SerializedData.TransitiveDerivedIndex) {
TransitiveDerivedIndex.emplace_back(
std::pair<uint32_t, uint32_t>(Curr.first, Curr.second));
const auto *Ty = stringToDICompositeType(IRDB, Curr.getKey());
TypeToVertex.try_emplace(Ty, Curr.getValue());
NameToType.try_emplace(Curr.getKey(), Ty);
VertexTypes.at(Curr.getValue()) = Ty;
}

Hierarchy.reserve(SerializedData.Hierarchy.size());
for (const auto &Curr : SerializedData.Hierarchy) {
Hierarchy.push_back(stringToDIType(IRDB, Curr));
}

for (const auto &Curr : SerializedData.VTables) {
LLVMVFTable CurrVTable = LLVMVFTable();
std::vector<const llvm::Function *> CurrVTable;

CurrVTable.reserve(Curr.size());
for (const auto &FuncName : Curr) {
CurrVTable.VFT.push_back(IRDB->getFunction(FuncName));
CurrVTable.push_back(IRDB->getFunction(FuncName));
}

VTables.push_back(std::move(CurrVTable));
VTables.emplace_back(std::move(CurrVTable));
}
}

Expand Down Expand Up @@ -323,6 +318,13 @@ auto DIBasedTypeHierarchy::subTypesOf(ClassType Ty) const noexcept
llvm::report_fatal_error("Not implemented");
}

std::string DIBasedTypeHierarchy::getTypeName(ClassType Type) const {
if (const auto *CT = llvm::dyn_cast<llvm::DICompositeType>(Type)) {
return getCompositeTypeName(CT).str();
}
return Type->getName().str();
}

[[nodiscard]] bool DIBasedTypeHierarchy::hasVFTable(ClassType Type) const {
const auto *StructTy = llvm::dyn_cast<llvm::DICompositeType>(Type);
return StructTy && StructTy->getVTableHolder();
Expand Down Expand Up @@ -365,34 +367,10 @@ DIBasedTypeHierarchy::getAsJson() const {

DIBasedTypeHierarchyData DIBasedTypeHierarchy::getTypeHierarchyData() const {
DIBasedTypeHierarchyData Data;
for (const auto &Curr : NameToType) {
Data.NameToType.try_emplace(Curr.getKey(),
Curr.getValue()->getName().str());
}

for (const auto &Curr : TypeToVertex) {
Data.TypeToVertex.try_emplace(Curr.getFirst()->getName(), Curr.getSecond());
}

Data.VertexTypes.reserve(VertexTypes.size());
for (const auto &Curr : VertexTypes) {
if (!Curr) {
Data.VertexTypes.emplace_back("");
llvm::errs() << "VertexType is null\n";
}

if (!Curr->getName().empty()) {
Data.VertexTypes.push_back(Curr->getName().str());
continue;
}

if (!Curr->getIdentifier().empty()) {
Data.VertexTypes.push_back(Curr->getIdentifier().str());
continue;
}

Data.VertexTypes.emplace_back("");
llvm::errs() << "VertexType has no valid name or identifier\n";
Data.TypeToVertex.try_emplace(getTypeName(Curr.getFirst()),
Curr.getSecond());
}

Data.TransitiveDerivedIndex = TransitiveDerivedIndex;
Expand Down
51 changes: 11 additions & 40 deletions lib/PhasarLLVM/TypeHierarchy/DIBasedTypeHierarchyData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,15 @@ namespace psr {
static DIBasedTypeHierarchyData getDataFromJson(const nlohmann::json &Json) {
DIBasedTypeHierarchyData Data;

for (const auto &[Key, Value] :
Json["NameToType"].get<nlohmann::json::object_t>()) {
Data.NameToType.try_emplace(Key, Value.get<std::string>());
}

for (const auto &[Key, Value] :
Json["TypeToVertex"].get<nlohmann::json::object_t>()) {
for (const auto &[Key, Value] : Json["TypeToVertex"].items()) {
Data.TypeToVertex.try_emplace(Key, Value.get<size_t>());
}

int Counter = 0;
for (const auto &Value : Json["VertexTypes"]) {
Data.VertexTypes.push_back(Value.get<std::string>());
Counter++;
}

for (const auto &CurrPair : Json["TransitiveDerivedIndex"]) {
Data.TransitiveDerivedIndex.emplace_back(CurrPair[0].get<uint32_t>(),
CurrPair[1].get<uint32_t>());
}
Data.TransitiveDerivedIndex =
Json["TransitiveDerivedIndex"]
.get<std::vector<std::pair<uint32_t, uint32_t>>>();

for (const auto &Value : Json["Hierarchy"]) {
Data.Hierarchy.push_back(Value.get<std::string>());
}
Data.Hierarchy = Json["Hierarchy"].get<std::vector<std::string>>();

for (const auto &CurrVTable : Json["VTables"]) {
auto &DataPos = Data.VTables.emplace_back();
Expand All @@ -62,32 +47,18 @@ static DIBasedTypeHierarchyData getDataFromJson(const nlohmann::json &Json) {
void DIBasedTypeHierarchyData::printAsJson(llvm::raw_ostream &OS) {
nlohmann::json Json;

for (const auto &Curr : NameToType) {
Json["NameToType"][Curr.getKey()] = Curr.getValue();
}

auto &JTypeToVertex = Json["TypeToVertex"];
for (const auto &Curr : TypeToVertex) {
Json["TypeToVertex"][Curr.getKey()] = Curr.getValue();
}

int Counter = 0;
for (const auto &Curr : VertexTypes) {
Json["VertexTypes"].push_back(Curr);
Counter++;
JTypeToVertex[Curr.getKey()] = Curr.getValue();
}

int Number = 0;
for (const auto &Curr : TransitiveDerivedIndex) {
Json["TransitiveDerivedIndex"][Number].push_back(Curr.first);
Json["TransitiveDerivedIndex"][Number++].push_back(Curr.second);
}
Json["TransitiveDerivedIndex"] = TransitiveDerivedIndex;

for (const auto &Curr : Hierarchy) {
Json["Hierarchy"].push_back(Curr);
}
Json["Hierarchy"] = Hierarchy;

auto &JVTables = Json["VTables"];
for (const auto &CurrVTable : VTables) {
auto &DataPos = Json["VTables"].emplace_back();
auto &DataPos = JVTables.emplace_back();

for (const auto &CurrVFunc : CurrVTable) {
DataPos.push_back(CurrVFunc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ void compareResults(const psr::DIBasedTypeHierarchy &Orig,
EXPECT_EQ(Orig.getAllTypes().size(), Deser.getAllTypes().size());
EXPECT_EQ(Orig.getAllVTables().size(), Deser.getAllVTables().size());

for (const auto &OrigCurrentType : Orig.getAllTypes()) {
EXPECT_EQ(OrigCurrentType->getName(),
Deser.getType(OrigCurrentType->getName().str())->getName());
for (const auto *OrigCurrentType : Orig.getAllTypes()) {

const auto *DeserTy = Deser.getType(Orig.getTypeName(OrigCurrentType));
EXPECT_EQ(OrigCurrentType, DeserTy)
<< "Mismatched types: Orig " << Orig.getTypeName(OrigCurrentType)
<< " vs Deser: " << Deser.getTypeName(DeserTy);

for (const auto &CurrVFunc :
Orig.getVFTable(OrigCurrentType)->getAllFunctions()) {
Expand Down

0 comments on commit fe605e9

Please sign in to comment.