@@ -49,21 +49,6 @@ class TypeMapTy : public ValueMapTypeRemapper {
49
49
// / This is a mapping from a source type to a destination type to use.
50
50
DenseMap<Type *, Type *> MappedTypes;
51
51
52
- // / When checking to see if two subgraphs are isomorphic, we speculatively
53
- // / add types to MappedTypes, but keep track of them here in case we need to
54
- // / roll back.
55
- SmallVector<Type *, 16 > SpeculativeTypes;
56
-
57
- SmallVector<StructType *, 16 > SpeculativeDstOpaqueTypes;
58
-
59
- // / This is a list of non-opaque structs in the source module that are mapped
60
- // / to an opaque struct in the destination module.
61
- SmallVector<StructType *, 16 > SrcDefinitionsToResolve;
62
-
63
- // / This is the set of opaque types in the destination modules who are
64
- // / getting a body from the source module.
65
- SmallPtrSet<StructType *, 16 > DstResolvedOpaqueTypes;
66
-
67
52
public:
68
53
TypeMapTy (IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
69
54
: DstStructTypesSet(DstStructTypesSet) {}
@@ -73,10 +58,6 @@ class TypeMapTy : public ValueMapTypeRemapper {
73
58
// / equivalent to the specified type in the source module.
74
59
void addTypeMapping (Type *DstTy, Type *SrcTy);
75
60
76
- // / Produce a body for an opaque type in the dest module from a type
77
- // / definition in the source module.
78
- Error linkDefinedTypeBodies ();
79
-
80
61
// / Return the mapped type to use for the specified input type from the
81
62
// / source module.
82
63
Type *get (Type *SrcTy);
@@ -88,45 +69,19 @@ class TypeMapTy : public ValueMapTypeRemapper {
88
69
private:
89
70
Type *remapType (Type *SrcTy) override { return get (SrcTy); }
90
71
91
- bool areTypesIsomorphic (Type *DstTy, Type *SrcTy);
72
+ bool recursivelyAddMappingIfTypesAreIsomorphic (Type *DstTy, Type *SrcTy);
92
73
};
93
74
}
94
75
95
76
void TypeMapTy::addTypeMapping (Type *DstTy, Type *SrcTy) {
96
- assert (SpeculativeTypes.empty ());
97
- assert (SpeculativeDstOpaqueTypes.empty ());
98
-
99
- // Check to see if these types are recursively isomorphic and establish a
100
- // mapping between them if so.
101
- if (!areTypesIsomorphic (DstTy, SrcTy)) {
102
- // Oops, they aren't isomorphic. Just discard this request by rolling out
103
- // any speculative mappings we've established.
104
- for (Type *Ty : SpeculativeTypes)
105
- MappedTypes.erase (Ty);
106
-
107
- SrcDefinitionsToResolve.resize (SrcDefinitionsToResolve.size () -
108
- SpeculativeDstOpaqueTypes.size ());
109
- for (StructType *Ty : SpeculativeDstOpaqueTypes)
110
- DstResolvedOpaqueTypes.erase (Ty);
111
- } else {
112
- // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy
113
- // and all its descendants to lower amount of renaming in LLVM context
114
- // Renaming occurs because we load all source modules to the same context
115
- // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
116
- // As a result we may get several different types in the destination
117
- // module, which are in fact the same.
118
- for (Type *Ty : SpeculativeTypes)
119
- if (auto *STy = dyn_cast<StructType>(Ty))
120
- if (STy->hasName ())
121
- STy->setName (" " );
122
- }
123
- SpeculativeTypes.clear ();
124
- SpeculativeDstOpaqueTypes.clear ();
77
+ recursivelyAddMappingIfTypesAreIsomorphic (DstTy, SrcTy);
125
78
}
126
79
127
80
// / Recursively walk this pair of types, returning true if they are isomorphic,
128
- // / false if they are not.
129
- bool TypeMapTy::areTypesIsomorphic (Type *DstTy, Type *SrcTy) {
81
+ // / false if they are not. Types that were determined to be isomorphic are
82
+ // / added to MappedTypes.
83
+ bool TypeMapTy::recursivelyAddMappingIfTypesAreIsomorphic (Type *DstTy,
84
+ Type *SrcTy) {
130
85
// Two types with differing kinds are clearly not isomorphic.
131
86
if (DstTy->getTypeID () != SrcTy->getTypeID ())
132
87
return false ;
@@ -145,29 +100,10 @@ bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
145
100
146
101
// Okay, we have two types with identical kinds that we haven't seen before.
147
102
148
- // If this is an opaque struct type, special case it .
103
+ // Always consider opaque struct types non-isomorphic .
149
104
if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
150
- // Mapping an opaque type to any struct, just keep the dest struct.
151
- if (SSTy->isOpaque ()) {
152
- Entry = DstTy;
153
- SpeculativeTypes.push_back (SrcTy);
154
- return true ;
155
- }
156
-
157
- // Mapping a non-opaque source type to an opaque dest. If this is the first
158
- // type that we're mapping onto this destination type then we succeed. Keep
159
- // the dest, but fill it in later. If this is the second (different) type
160
- // that we're trying to map onto the same opaque type then we fail.
161
- if (cast<StructType>(DstTy)->isOpaque ()) {
162
- // We can only map one source type onto the opaque destination type.
163
- if (!DstResolvedOpaqueTypes.insert (cast<StructType>(DstTy)).second )
164
- return false ;
165
- SrcDefinitionsToResolve.push_back (SSTy);
166
- SpeculativeTypes.push_back (SrcTy);
167
- SpeculativeDstOpaqueTypes.push_back (cast<StructType>(DstTy));
168
- Entry = DstTy;
169
- return true ;
170
- }
105
+ if (SSTy->isOpaque () || cast<StructType>(DstTy)->isOpaque ())
106
+ return false ;
171
107
}
172
108
173
109
// If the number of subtypes disagree between the two types, then we fail.
@@ -196,38 +132,27 @@ bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
196
132
return false ;
197
133
}
198
134
199
- // Otherwise, we speculate that these two types will line up and recursively
200
- // check the subelements.
201
- Entry = DstTy;
202
- SpeculativeTypes.push_back (SrcTy);
203
-
135
+ // Recursively check the subelements.
204
136
for (unsigned I = 0 , E = SrcTy->getNumContainedTypes (); I != E; ++I)
205
- if (!areTypesIsomorphic (DstTy->getContainedType (I),
206
- SrcTy->getContainedType (I)))
137
+ if (!recursivelyAddMappingIfTypesAreIsomorphic (DstTy->getContainedType (I),
138
+ SrcTy->getContainedType (I)))
207
139
return false ;
208
140
209
141
// If everything seems to have lined up, then everything is great.
210
- return true ;
211
- }
142
+ [[maybe_unused]] auto Res = MappedTypes. insert ({SrcTy, DstTy}) ;
143
+ assert (!Res. second && " Recursive type? " );
212
144
213
- Error TypeMapTy::linkDefinedTypeBodies () {
214
- SmallVector<Type *, 16 > Elements;
215
- for (StructType *SrcSTy : SrcDefinitionsToResolve) {
216
- StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
217
- assert (DstSTy->isOpaque ());
218
-
219
- // Map the body of the source type over to a new body for the dest type.
220
- Elements.resize (SrcSTy->getNumElements ());
221
- for (unsigned I = 0 , E = Elements.size (); I != E; ++I)
222
- Elements[I] = get (SrcSTy->getElementType (I));
223
-
224
- if (auto E = DstSTy->setBodyOrError (Elements, SrcSTy->isPacked ()))
225
- return E;
226
- DstStructTypesSet.switchToNonOpaque (DstSTy);
145
+ if (auto *STy = dyn_cast<StructType>(SrcTy)) {
146
+ // We clear name of SrcTy to lower amount of renaming in LLVM context.
147
+ // Renaming occurs because we load all source modules to the same context
148
+ // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
149
+ // As a result we may get several different types in the destination
150
+ // module, which are in fact the same.
151
+ if (STy->hasName ())
152
+ STy->setName (" " );
227
153
}
228
- SrcDefinitionsToResolve.clear ();
229
- DstResolvedOpaqueTypes.clear ();
230
- return Error::success ();
154
+
155
+ return true ;
231
156
}
232
157
233
158
Type *TypeMapTy::get (Type *Ty) {
@@ -845,10 +770,6 @@ void IRLinker::computeTypeMapping() {
845
770
if (TypeMap.DstStructTypesSet .hasType (DST))
846
771
TypeMap.addTypeMapping (DST, ST);
847
772
}
848
-
849
- // Now that we have discovered all of the type equivalences, get a body for
850
- // any 'opaque' types in the dest module that are now resolved.
851
- setError (TypeMap.linkDefinedTypeBodies ());
852
773
}
853
774
854
775
static void getArrayElements (const Constant *C,
0 commit comments