From 7159bb59fed778bca6fca8f127f4a523aa45ace2 Mon Sep 17 00:00:00 2001 From: Eugene Leviant Date: Wed, 21 Feb 2018 15:13:48 +0000 Subject: [PATCH] [IRMover] Implement name based structure type mapping Differential revision: https://reviews.llvm.org/D43199 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325686 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Linker/IRMover.h | 28 +++++++++++++++++----------- lib/Linker/IRMover.cpp | 21 ++++++++++++++------- test/Linker/Inputs/struct-mapping.ll | 4 ++++ test/Linker/struct-mapping.ll | 12 ++++++++++++ tools/llvm-link/llvm-link.cpp | 9 ++++++++- 5 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 test/Linker/Inputs/struct-mapping.ll create mode 100644 test/Linker/struct-mapping.ll diff --git a/include/llvm/Linker/IRMover.h b/include/llvm/Linker/IRMover.h index 235ada47cef..7b483cb53f0 100644 --- a/include/llvm/Linker/IRMover.h +++ b/include/llvm/Linker/IRMover.h @@ -49,17 +49,23 @@ public: // The set of opaque types is the composite module. DenseSet OpaqueStructTypes; - // The set of identified but non opaque structures in the composite module. - DenseSet NonOpaqueStructTypes; - - public: - void addNonOpaque(StructType *Ty); - void switchToNonOpaque(StructType *Ty); - void addOpaque(StructType *Ty); - StructType *findNonOpaque(ArrayRef ETypes, bool IsPacked); - bool hasType(StructType *Ty); - }; - + // The set of identified but non opaque structures in the composite module. + DenseSet NonOpaqueStructTypes; + + // Map between structure type name and instance. Used in findNonOpaque + // to correctly map imported global variable type during ThinLTO import + // phase. + DenseMap NonOpaqueStructNameMap; + + public: + void addNonOpaque(StructType *Ty); + void switchToNonOpaque(StructType *Ty); + void addOpaque(StructType *Ty); + StructType *findNonOpaque(ArrayRef ETypes, bool IsPacked, + StringRef Name); + bool hasType(StructType *Ty); + }; + IRMover(Module &M); typedef std::function ValueAdder; diff --git a/lib/Linker/IRMover.cpp b/lib/Linker/IRMover.cpp index 42081442db7..03539f393cb 100644 --- a/lib/Linker/IRMover.cpp +++ b/lib/Linker/IRMover.cpp @@ -318,8 +318,8 @@ Type *TypeMapTy::get(Type *Ty, SmallPtrSet &Visited) { return *Entry = Ty; } - if (StructType *OldT = - DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) { + if (StructType *OldT = DstStructTypesSet.findNonOpaque( + ElementTypes, IsPacked, STy->getName())) { STy->setName(""); return *Entry = OldT; } @@ -906,7 +906,6 @@ bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { Expected IRLinker::linkGlobalValueProto(GlobalValue *SGV, bool ForAlias) { GlobalValue *DGV = getLinkedToGlobal(SGV); - bool ShouldLink = shouldLink(DGV, *SGV); // just missing from map @@ -1410,12 +1409,14 @@ bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS, void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) { assert(!Ty->isOpaque()); + if (Ty->hasName()) + NonOpaqueStructNameMap.insert({getTypeNamePrefix(Ty->getName()), Ty}); + NonOpaqueStructTypes.insert(Ty); } void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) { - assert(!Ty->isOpaque()); - NonOpaqueStructTypes.insert(Ty); + addNonOpaque(Ty); bool Removed = OpaqueStructTypes.erase(Ty); (void)Removed; assert(Removed); @@ -1428,10 +1429,16 @@ void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) { StructType * IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef ETypes, - bool IsPacked) { + bool IsPacked, StringRef Name) { IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked); auto I = NonOpaqueStructTypes.find_as(Key); - return I == NonOpaqueStructTypes.end() ? nullptr : *I; + if (I == NonOpaqueStructTypes.end()) + return nullptr; + auto NI = NonOpaqueStructNameMap.find(getTypeNamePrefix(Name)); + if (NI != NonOpaqueStructNameMap.end() && + IRMover::StructTypeKeyInfo::KeyTy((*NI).second) == Key) + return (*NI).second; + return *I; } bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) { diff --git a/test/Linker/Inputs/struct-mapping.ll b/test/Linker/Inputs/struct-mapping.ll new file mode 100644 index 00000000000..d4fa07313c6 --- /dev/null +++ b/test/Linker/Inputs/struct-mapping.ll @@ -0,0 +1,4 @@ +%struct.Baz = type { i64, i64, %struct.Foo } +%struct.Foo = type { i64, i64 } + +@baz = global %struct.Baz zeroinitializer diff --git a/test/Linker/struct-mapping.ll b/test/Linker/struct-mapping.ll new file mode 100644 index 00000000000..732ef8c50cd --- /dev/null +++ b/test/Linker/struct-mapping.ll @@ -0,0 +1,12 @@ +; RUN: llvm-link --initial-module=%s %p/Inputs/struct-mapping.ll -S -o - | FileCheck %s + +; Here we check that new type mapping algorithm correctly mapped type of internal +; member of struct.Baz to struct.Foo. Without it we'd map that type to struct.Bar, because +; it is recursively isomorphic to struct.Foo and is defined first in source file. +; CHECK: %struct.Baz = type { i64, i64, %struct.Foo } + +%struct.Bar = type { i64, i64 } +%struct.Foo = type { i64, i64 } + +@bar = global %struct.Bar zeroinitializer +@foo = global %struct.Foo zeroinitializer diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp index 1ada60ee5f2..8d29caf47bf 100644 --- a/tools/llvm-link/llvm-link.cpp +++ b/tools/llvm-link/llvm-link.cpp @@ -70,6 +70,11 @@ static cl::opt OutputFilename("o", cl::desc("Override output filename"), cl::init("-"), cl::value_desc("filename")); +static cl::opt + InitialModule("initial-module", + cl::desc("Link to existing destination module"), cl::init(""), + cl::value_desc("filename")); + static cl::opt Internalize("internalize", cl::desc("Internalize linked symbols")); @@ -360,7 +365,9 @@ int main(int argc, char **argv) { if (!DisableDITypeMap) Context.enableDebugTypeODRUniquing(); - auto Composite = make_unique("llvm-link", Context); + auto Composite = InitialModule.empty() + ? make_unique("llvm-link", Context) + : loadFile(argv[0], InitialModule, Context); Linker L(*Composite); unsigned Flags = Linker::Flags::None; -- 2.11.0