@@ -113,12 +113,19 @@ class FMFSource {
113113// / Common base class shared among various IRBuilders.
114114class IRBuilderBase {
115115 // / Pairs of (metadata kind, MDNode *) that should be added to all newly
116- // / created instructions, like !dbg metadata.
116+ // / created instructions, excluding !dbg metadata, which is stored in the
117+ // / StoredDL field.
117118 SmallVector<std::pair<unsigned , MDNode *>, 2 > MetadataToCopy;
119+ // / The DebugLoc that will be applied to instructions inserted by this
120+ // / builder.
121+ DebugLoc StoredDL;
118122
119123 // / Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
120124 // / null. If \p MD is null, remove the entry with \p Kind.
121125 void AddOrRemoveMetadataToCopy (unsigned Kind, MDNode *MD) {
126+ assert (Kind != LLVMContext::MD_dbg &&
127+ " MD_dbg metadata must be stored in StoredDL" );
128+
122129 if (!MD) {
123130 erase_if (MetadataToCopy, [Kind](const std::pair<unsigned , MDNode *> &KV) {
124131 return KV.first == Kind;
@@ -238,7 +245,9 @@ class IRBuilderBase {
238245
239246 // / Set location information used by debugging information.
240247 void SetCurrentDebugLocation (DebugLoc L) {
241- AddOrRemoveMetadataToCopy (LLVMContext::MD_dbg, L.getAsMDNode ());
248+ // For !dbg metadata attachments, we use DebugLoc instead of the raw MDNode
249+ // to include optional introspection data for use in Debugify.
250+ StoredDL = std::move (L);
242251 }
243252
244253 // / Set nosanitize metadata.
@@ -252,8 +261,12 @@ class IRBuilderBase {
252261 // / not on \p Src will be dropped from MetadataToCopy.
253262 void CollectMetadataToCopy (Instruction *Src,
254263 ArrayRef<unsigned > MetadataKinds) {
255- for (unsigned K : MetadataKinds)
256- AddOrRemoveMetadataToCopy (K, Src->getMetadata (K));
264+ for (unsigned K : MetadataKinds) {
265+ if (K == LLVMContext::MD_dbg)
266+ SetCurrentDebugLocation (Src->getDebugLoc ());
267+ else
268+ AddOrRemoveMetadataToCopy (K, Src->getMetadata (K));
269+ }
257270 }
258271
259272 // / Get location information used by debugging information.
@@ -267,6 +280,7 @@ class IRBuilderBase {
267280 void AddMetadataToInst (Instruction *I) const {
268281 for (const auto &KV : MetadataToCopy)
269282 I->setMetadata (KV.first , KV.second );
283+ SetInstDebugLocation (I);
270284 }
271285
272286 // / Get the return type of the current function that we're emitting
0 commit comments