llvm.org GIT mirror llvm / 55d5ea3
Split the set of identified struct types into opaque and non-opaque ones. The non-opaque part can be structurally uniqued. To keep this to just a hash lookup, we don't try to unique cyclic types. Also change the type mapping algorithm to be optimistic about a type not being recursive and only create a new type when proven to be wrong. This is not as strong as trying to speculate that we can keep the source type, but is simpler (no speculation to revert) and more powerfull than what we had before (we don't copy non-recursive types at least). I initially wrote this to try to replace the name based type merging. It is not strong enough to replace it, but is is a useful addition. With this patch the number of named struct types is a clang lto bootstrap goes from 49674 to 15986. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223278 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
3 changed file(s) with 257 addition(s) and 121 deletion(s). Raw diff Collapse all Expand all
99 #ifndef LLVM_LINKER_LINKER_H
1010 #define LLVM_LINKER_LINKER_H
1111
12 #include "llvm/ADT/SmallPtrSet.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
1315
1416 #include
1517
1719 class DiagnosticInfo;
1820 class Module;
1921 class StructType;
22 class Type;
2023
2124 /// This class provides the core functionality of linking in LLVM. It keeps a
2225 /// pointer to the merged module so far. It doesn't take ownership of the
2528 class Linker {
2629 public:
2730 typedef std::function DiagnosticHandlerFunction;
31
32 struct StructTypeKeyInfo {
33 struct KeyTy {
34 ArrayRef ETypes;
35 bool IsPacked;
36 KeyTy(ArrayRef E, bool P);
37 KeyTy(const StructType *ST);
38 bool operator==(const KeyTy &that) const;
39 bool operator!=(const KeyTy &that) const;
40 };
41 static StructType *getEmptyKey();
42 static StructType *getTombstoneKey();
43 static unsigned getHashValue(const KeyTy &Key);
44 static unsigned getHashValue(const StructType *ST);
45 static bool isEqual(const KeyTy &LHS, const StructType *RHS);
46 static bool isEqual(const StructType *LHS, const StructType *RHS);
47 };
48
49 typedef DenseMap
50 NonOpaqueStructTypeSet;
51 typedef DenseSet OpaqueStructTypeSet;
52
53 struct IdentifiedStructTypeSet {
54 // The set of opaque types is the composite module.
55 OpaqueStructTypeSet OpaqueStructTypes;
56
57 // The set of identified but non opaque structures in the composite module.
58 NonOpaqueStructTypeSet NonOpaqueStructTypes;
59
60 void addNonOpaque(StructType *Ty);
61 void addOpaque(StructType *Ty);
62 StructType *findNonOpaque(ArrayRef ETypes, bool IsPacked);
63 bool hasType(StructType *Ty);
64 };
2865
2966 Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
3067 Linker(Module *M);
4582 private:
4683 void init(Module *M, DiagnosticHandlerFunction DiagnosticHandler);
4784 Module *Composite;
48 SmallPtrSet IdentifiedStructTypes;
85
86 IdentifiedStructTypeSet IdentifiedStructTypes;
87
4988 DiagnosticHandlerFunction DiagnosticHandler;
5089 };
5190
1212
1313 #include "llvm/Linker/Linker.h"
1414 #include "llvm-c/Linker.h"
15 #include "llvm/ADT/Hashing.h"
1516 #include "llvm/ADT/Optional.h"
1617 #include "llvm/ADT/SetVector.h"
1718 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Statistic.h"
1820 #include "llvm/IR/Constants.h"
1921 #include "llvm/IR/DiagnosticInfo.h"
2022 #include "llvm/IR/DiagnosticPrinter.h"
3537 //===----------------------------------------------------------------------===//
3638
3739 namespace {
38 typedef SmallPtrSet TypeSet;
39
4040 class TypeMapTy : public ValueMapTypeRemapper {
4141 /// This is a mapping from a source type to a destination type to use.
4242 DenseMap MappedTypes;
5757 SmallPtrSet DstResolvedOpaqueTypes;
5858
5959 public:
60 TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
61
62 TypeSet &DstStructTypesSet;
60 TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
61 : DstStructTypesSet(DstStructTypesSet) {}
62
63 Linker::IdentifiedStructTypeSet &DstStructTypesSet;
6364 /// Indicate that the specified type in the destination module is conceptually
6465 /// equivalent to the specified type in the source module.
6566 void addTypeMapping(Type *DstTy, Type *SrcTy);
7172 /// Return the mapped type to use for the specified input type from the
7273 /// source module.
7374 Type *get(Type *SrcTy);
75 Type *get(Type *SrcTy, SmallPtrSet &Visited);
76
77 void finishType(StructType *DTy, StructType *STy, ArrayRef ETypes);
7478
7579 FunctionType *get(FunctionType *T) {
7680 return cast(get((Type *)T));
224228 DstResolvedOpaqueTypes.clear();
225229 }
226230
227 Type *TypeMapTy::get(Type *Ty) {
228 #ifndef NDEBUG
229 for (auto &Pair : MappedTypes) {
230 assert(!(Pair.first != Ty && Pair.second == Ty) &&
231 "mapping to a source type");
232 }
233 #endif
234
235 // If we already have an entry for this type, return it.
236 Type **Entry = &MappedTypes[Ty];
237 if (*Entry)
238 return *Entry;
239
240 // If this is not a named struct type, then just map all of the elements and
241 // then rebuild the type from inside out.
242 if (!isa(Ty) || cast(Ty)->isLiteral()) {
243 // If there are no element types to map, then the type is itself. This is
244 // true for the anonymous {} struct, things like 'float', integers, etc.
245 if (Ty->getNumContainedTypes() == 0)
246 return *Entry = Ty;
247
248 // Remap all of the elements, keeping track of whether any of them change.
249 bool AnyChange = false;
250 SmallVector ElementTypes;
251 ElementTypes.resize(Ty->getNumContainedTypes());
252 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
253 ElementTypes[I] = get(Ty->getContainedType(I));
254 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
255 }
256
257 // If we found our type while recursively processing stuff, just use it.
258 Entry = &MappedTypes[Ty];
259 if (*Entry)
260 return *Entry;
261
262 // If all of the element types mapped directly over, then the type is usable
263 // as-is.
264 if (!AnyChange)
265 return *Entry = Ty;
266
267 // Otherwise, rebuild a modified type.
268 switch (Ty->getTypeID()) {
269 default:
270 llvm_unreachable("unknown derived type to remap");
271 case Type::ArrayTyID:
272 return *Entry = ArrayType::get(ElementTypes[0],
273 cast(Ty)->getNumElements());
274 case Type::VectorTyID:
275 return *Entry = VectorType::get(ElementTypes[0],
276 cast(Ty)->getNumElements());
277 case Type::PointerTyID:
278 return *Entry = PointerType::get(
279 ElementTypes[0], cast(Ty)->getAddressSpace());
280 case Type::FunctionTyID:
281 return *Entry = FunctionType::get(ElementTypes[0],
282 makeArrayRef(ElementTypes).slice(1),
283 cast(Ty)->isVarArg());
284 case Type::StructTyID:
285 // Note that this is only reached for anonymous structs.
286 return *Entry = StructType::get(Ty->getContext(), ElementTypes,
287 cast(Ty)->isPacked());
288 }
289 }
290
291 // Otherwise, this is an unmapped named struct. If the struct can be directly
292 // mapped over, just use it as-is. This happens in a case when the linked-in
293 // module has something like:
294 // %T = type {%T*, i32}
295 // @GV = global %T* null
296 // where T does not exist at all in the destination module.
297 //
298 // The other case we watch for is when the type is not in the destination
299 // module, but that it has to be rebuilt because it refers to something that
300 // is already mapped. For example, if the destination module has:
301 // %A = type { i32 }
302 // and the source module has something like
303 // %A' = type { i32 }
304 // %B = type { %A'* }
305 // @GV = global %B* null
306 // then we want to create a new type: "%B = type { %A*}" and have it take the
307 // pristine "%B" name from the source module.
308 //
309 // To determine which case this is, we have to recursively walk the type graph
310 // speculating that we'll be able to reuse it unmodified. Only if this is
311 // safe would we map the entire thing over. Because this is an optimization,
312 // and is not required for the prettiness of the linked module, we just skip
313 // it and always rebuild a type here.
314 StructType *STy = cast(Ty);
315
316 // If the type is opaque, we can just use it directly.
317 if (STy->isOpaque()) {
318 // A named structure type from src module is used. Add it to the Set of
319 // identified structs in the destination module.
320 DstStructTypesSet.insert(STy);
321 return *Entry = STy;
322 }
323
324 // Otherwise we create a new type.
325 StructType *DTy = StructType::create(STy->getContext());
326 // A new identified structure type was created. Add it to the set of
327 // identified structs in the destination module.
328 DstStructTypesSet.insert(DTy);
329 *Entry = DTy;
330
331 SmallVector ElementTypes;
332 ElementTypes.resize(STy->getNumElements());
333 for (unsigned I = 0, E = ElementTypes.size(); I != E; ++I)
334 ElementTypes[I] = get(STy->getElementType(I));
335 DTy->setBody(ElementTypes, STy->isPacked());
231 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
232 ArrayRef ETypes) {
233 DTy->setBody(ETypes, STy->isPacked());
336234
337235 // Steal STy's name.
338236 if (STy->hasName()) {
341239 DTy->setName(TmpName);
342240 }
343241
344 return DTy;
242 DstStructTypesSet.addNonOpaque(DTy);
243 }
244
245 Type *TypeMapTy::get(Type *Ty) {
246 SmallPtrSet Visited;
247 return get(Ty, Visited);
248 }
249
250 Type *TypeMapTy::get(Type *Ty, SmallPtrSet &Visited) {
251 // If we already have an entry for this type, return it.
252 Type **Entry = &MappedTypes[Ty];
253 if (*Entry)
254 return *Entry;
255
256 // These are types that LLVM itself will unique.
257 bool IsUniqued = !isa(Ty) || cast(Ty)->isLiteral();
258
259 #ifndef NDEBUG
260 if (!IsUniqued) {
261 for (auto &Pair : MappedTypes) {
262 assert(!(Pair.first != Ty && Pair.second == Ty) &&
263 "mapping to a source type");
264 }
265 }
266 #endif
267
268 if (!IsUniqued && !Visited.insert(cast(Ty)).second) {
269 StructType *DTy = StructType::create(Ty->getContext());
270 return *Entry = DTy;
271 }
272
273 // If this is not a recursive type, then just map all of the elements and
274 // then rebuild the type from inside out.
275 SmallVector ElementTypes;
276
277 // If there are no element types to map, then the type is itself. This is
278 // true for the anonymous {} struct, things like 'float', integers, etc.
279 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
280 return *Entry = Ty;
281
282 // Remap all of the elements, keeping track of whether any of them change.
283 bool AnyChange = false;
284 ElementTypes.resize(Ty->getNumContainedTypes());
285 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
286 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
287 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
288 }
289
290 // If we found our type while recursively processing stuff, just use it.
291 Entry = &MappedTypes[Ty];
292 if (*Entry) {
293 if (auto *DTy = dyn_cast(*Entry)) {
294 if (DTy->isOpaque()) {
295 auto *STy = cast(Ty);
296 finishType(DTy, STy, ElementTypes);
297 }
298 }
299 return *Entry;
300 }
301
302 // If all of the element types mapped directly over and the type is not
303 // a nomed struct, then the type is usable as-is.
304 if (!AnyChange && IsUniqued)
305 return *Entry = Ty;
306
307 // Otherwise, rebuild a modified type.
308 switch (Ty->getTypeID()) {
309 default:
310 llvm_unreachable("unknown derived type to remap");
311 case Type::ArrayTyID:
312 return *Entry = ArrayType::get(ElementTypes[0],
313 cast(Ty)->getNumElements());
314 case Type::VectorTyID:
315 return *Entry = VectorType::get(ElementTypes[0],
316 cast(Ty)->getNumElements());
317 case Type::PointerTyID:
318 return *Entry = PointerType::get(ElementTypes[0],
319 cast(Ty)->getAddressSpace());
320 case Type::FunctionTyID:
321 return *Entry = FunctionType::get(ElementTypes[0],
322 makeArrayRef(ElementTypes).slice(1),
323 cast(Ty)->isVarArg());
324 case Type::StructTyID: {
325 auto *STy = cast(Ty);
326 bool IsPacked = STy->isPacked();
327 if (IsUniqued)
328 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
329
330 // If the type is opaque, we can just use it directly.
331 if (STy->isOpaque()) {
332 DstStructTypesSet.addOpaque(STy);
333 return *Entry = Ty;
334 }
335
336 if (StructType *OldT =
337 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
338 STy->setName("");
339 return *Entry = OldT;
340 }
341
342 if (!AnyChange) {
343 DstStructTypesSet.addNonOpaque(STy);
344 return *Entry = Ty;
345 }
346
347 StructType *DTy = StructType::create(Ty->getContext());
348 finishType(DTy, STy, ElementTypes);
349 return *Entry = DTy;
350 }
351 }
345352 }
346353
347354 //===----------------------------------------------------------------------===//
411418 Linker::DiagnosticHandlerFunction DiagnosticHandler;
412419
413420 public:
414 ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM,
421 ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
415422 Linker::DiagnosticHandlerFunction DiagnosticHandler)
416423 : DstM(dstM), SrcM(srcM), TypeMap(Set),
417424 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
824831 // we prefer to take the '%C' version. So we are then left with both
825832 // '%C.1' and '%C' being used for the same types. This leads to some
826833 // variables using one type and some using the other.
827 if (TypeMap.DstStructTypesSet.count(DST))
834 if (TypeMap.DstStructTypesSet.hasType(DST))
828835 TypeMap.addTypeMapping(DST, ST);
829836 }
830837
15881595 return false;
15891596 }
15901597
1598 Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef E, bool P)
1599 : ETypes(E), IsPacked(P) {}
1600
1601 Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1602 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1603
1604 bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1605 if (IsPacked != That.IsPacked)
1606 return false;
1607 if (ETypes != That.ETypes)
1608 return false;
1609 return true;
1610 }
1611
1612 bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1613 return !this->operator==(That);
1614 }
1615
1616 StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
1617 return DenseMapInfo::getEmptyKey();
1618 }
1619
1620 StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
1621 return DenseMapInfo::getTombstoneKey();
1622 }
1623
1624 unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1625 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1626 Key.IsPacked);
1627 }
1628
1629 unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1630 return getHashValue(KeyTy(ST));
1631 }
1632
1633 bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1634 const StructType *RHS) {
1635 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1636 return false;
1637 return LHS == KeyTy(RHS);
1638 }
1639
1640 bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
1641 const StructType *RHS) {
1642 if (RHS == getEmptyKey())
1643 return LHS == getEmptyKey();
1644
1645 if (RHS == getTombstoneKey())
1646 return LHS == getTombstoneKey();
1647
1648 return KeyTy(LHS) == KeyTy(RHS);
1649 }
1650
1651 void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1652 assert(!Ty->isOpaque());
1653 bool &Entry = NonOpaqueStructTypes[Ty];
1654 Entry = true;
1655 }
1656
1657 void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1658 assert(Ty->isOpaque());
1659 OpaqueStructTypes.insert(Ty);
1660 }
1661
1662 StructType *
1663 Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef ETypes,
1664 bool IsPacked) {
1665 Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1666 auto I = NonOpaqueStructTypes.find_as(Key);
1667 if (I == NonOpaqueStructTypes.end())
1668 return nullptr;
1669 return I->first;
1670 }
1671
1672 bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1673 if (Ty->isOpaque())
1674 return OpaqueStructTypes.count(Ty);
1675 auto I = NonOpaqueStructTypes.find(Ty);
1676 if (I == NonOpaqueStructTypes.end())
1677 return false;
1678 return I->first == Ty;
1679 }
1680
15911681 void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
15921682 this->Composite = M;
15931683 this->DiagnosticHandler = DiagnosticHandler;
15941684
15951685 TypeFinder StructTypes;
15961686 StructTypes.run(*M, true);
1597 IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
1687 for (StructType *Ty : StructTypes) {
1688 if (Ty->isOpaque())
1689 IdentifiedStructTypes.addOpaque(Ty);
1690 else
1691 IdentifiedStructTypes.addNonOpaque(Ty);
1692 }
15981693 }
15991694
16001695 Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
88 ; CHECK: %C.0 = type { %B }
99 ; CHECK-NEXT: %B = type { %A }
1010 ; CHECK-NEXT: %A = type { i8 }
11 ; CHECK-NEXT: %C = type { %B }
11
12 ; CHECK: @g1 = external global %C.0
13 ; CHECK: getelementptr %C.0* null, i64 0, i32 0, i32 0
1214
1315 %A = type { i8 }
1416 %B = type { %A }