@@ -153,6 +153,53 @@ DISubprogram *llvm::ProcessSubprogramAttachment(const Function &F,
153
153
return SPClonedWithinModule;
154
154
}
155
155
156
+ bool llvm::BuildDebugInfoMDMap (MDMapT &MD, CloneFunctionChangeType Changes,
157
+ DebugInfoFinder &DIFinder,
158
+ DISubprogram *SPClonedWithinModule) {
159
+ bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
160
+ if (Changes < CloneFunctionChangeType::DifferentModule &&
161
+ DIFinder.subprogram_count () > 0 ) {
162
+ // Turn on module-level changes, since we need to clone (some of) the
163
+ // debug info metadata.
164
+ //
165
+ // FIXME: Metadata effectively owned by a function should be made
166
+ // local, and only that local metadata should be cloned.
167
+ ModuleLevelChanges = true ;
168
+
169
+ auto mapToSelfIfNew = [&MD](MDNode *N) {
170
+ // Avoid clobbering an existing mapping.
171
+ (void )MD.try_emplace (N, N);
172
+ };
173
+
174
+ // Avoid cloning types, compile units, and (other) subprograms.
175
+ SmallPtrSet<const DISubprogram *, 16 > MappedToSelfSPs;
176
+ for (DISubprogram *ISP : DIFinder.subprograms ()) {
177
+ if (ISP != SPClonedWithinModule) {
178
+ mapToSelfIfNew (ISP);
179
+ MappedToSelfSPs.insert (ISP);
180
+ }
181
+ }
182
+
183
+ // If a subprogram isn't going to be cloned skip its lexical blocks as well.
184
+ for (DIScope *S : DIFinder.scopes ()) {
185
+ auto *LScope = dyn_cast<DILocalScope>(S);
186
+ if (LScope && MappedToSelfSPs.count (LScope->getSubprogram ()))
187
+ mapToSelfIfNew (S);
188
+ }
189
+
190
+ for (DICompileUnit *CU : DIFinder.compile_units ())
191
+ mapToSelfIfNew (CU);
192
+
193
+ for (DIType *Type : DIFinder.types ())
194
+ mapToSelfIfNew (Type);
195
+ } else {
196
+ assert (!SPClonedWithinModule &&
197
+ " Subprogram should be in DIFinder->subprogram_count()..." );
198
+ }
199
+
200
+ return ModuleLevelChanges;
201
+ }
202
+
156
203
// Clone OldFunc into NewFunc, transforming the old arguments into references to
157
204
// VMap values.
158
205
void llvm::CloneFunctionInto (Function *NewFunc, const Function *OldFunc,
@@ -212,45 +259,8 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
212
259
DISubprogram *SPClonedWithinModule =
213
260
ProcessSubprogramAttachment (*OldFunc, Changes, DIFinder);
214
261
215
- if (Changes < CloneFunctionChangeType::DifferentModule &&
216
- DIFinder.subprogram_count () > 0 ) {
217
- // Turn on module-level changes, since we need to clone (some of) the
218
- // debug info metadata.
219
- //
220
- // FIXME: Metadata effectively owned by a function should be made
221
- // local, and only that local metadata should be cloned.
222
- ModuleLevelChanges = true ;
223
-
224
- auto mapToSelfIfNew = [&VMap](MDNode *N) {
225
- // Avoid clobbering an existing mapping.
226
- (void )VMap.MD ().try_emplace (N, N);
227
- };
228
-
229
- // Avoid cloning types, compile units, and (other) subprograms.
230
- SmallPtrSet<const DISubprogram *, 16 > MappedToSelfSPs;
231
- for (DISubprogram *ISP : DIFinder.subprograms ()) {
232
- if (ISP != SPClonedWithinModule) {
233
- mapToSelfIfNew (ISP);
234
- MappedToSelfSPs.insert (ISP);
235
- }
236
- }
237
-
238
- // If a subprogram isn't going to be cloned skip its lexical blocks as well.
239
- for (DIScope *S : DIFinder.scopes ()) {
240
- auto *LScope = dyn_cast<DILocalScope>(S);
241
- if (LScope && MappedToSelfSPs.count (LScope->getSubprogram ()))
242
- mapToSelfIfNew (S);
243
- }
244
-
245
- for (DICompileUnit *CU : DIFinder.compile_units ())
246
- mapToSelfIfNew (CU);
247
-
248
- for (DIType *Type : DIFinder.types ())
249
- mapToSelfIfNew (Type);
250
- } else {
251
- assert (!SPClonedWithinModule &&
252
- " Subprogram should be in DIFinder->subprogram_count()..." );
253
- }
262
+ ModuleLevelChanges =
263
+ BuildDebugInfoMDMap (VMap.MD (), Changes, DIFinder, SPClonedWithinModule);
254
264
255
265
const auto RemapFlag = ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges;
256
266
// Duplicate the metadata that is attached to the cloned function.
0 commit comments