@@ -344,6 +344,12 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
344344 uint64_t SliceSizeInBits, Instruction *OldInst,
345345 Instruction *Inst, Value *Dest, Value *Value,
346346 const DataLayout &DL) {
347+ // If we want allocas to be migrated using this helper then we need to ensure
348+ // that the BaseFragments map code still works. A simple solution would be
349+ // to choose to always clone alloca dbg_assigns (rather than sometimes
350+ // "stealing" them).
351+ assert (!isa<AllocaInst>(Inst) && " Unexpected alloca" );
352+
347353 auto DVRAssignMarkerRange = at::getDVRAssignmentMarkers (OldInst);
348354 // Nothing to do if OldInst has no linked dbg.assign intrinsics.
349355 if (DVRAssignMarkerRange.empty ())
@@ -429,11 +435,22 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
429435 Inst->setMetadata (LLVMContext::MD_DIAssignID, NewID);
430436 }
431437
432- ::Value *NewValue = Value ? Value : DbgAssign->getValue ();
433- DbgVariableRecord *NewAssign = cast<DbgVariableRecord>(cast<DbgRecord *>(
434- DIB.insertDbgAssign (Inst, NewValue, DbgAssign->getVariable (), Expr,
435- Dest, DIExpression::get (Expr->getContext (), {}),
436- DbgAssign->getDebugLoc ())));
438+ DbgVariableRecord *NewAssign;
439+ if (IsSplit) {
440+ ::Value *NewValue = Value ? Value : DbgAssign->getValue ();
441+ NewAssign = cast<DbgVariableRecord>(cast<DbgRecord *>(
442+ DIB.insertDbgAssign (Inst, NewValue, DbgAssign->getVariable (), Expr,
443+ Dest, DIExpression::get (Expr->getContext (), {}),
444+ DbgAssign->getDebugLoc ())));
445+ } else {
446+ // The store is not split, simply steal the existing dbg_assign.
447+ NewAssign = DbgAssign;
448+ NewAssign->setAssignId (NewID); // FIXME: Can we avoid generating new IDs?
449+ NewAssign->setAddress (Dest);
450+ if (Value)
451+ NewAssign->replaceVariableLocationOp (0u , Value);
452+ assert (Expr == NewAssign->getExpression ());
453+ }
437454
438455 // If we've updated the value but the original dbg.assign has an arglist
439456 // then kill it now - we can't use the requested new value.
@@ -464,9 +481,10 @@ static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
464481 // noted as slightly offset (in code) from the store. In practice this
465482 // should have little effect on the debugging experience due to the fact
466483 // that all the split stores should get the same line number.
467- NewAssign->moveBefore (DbgAssign->getIterator ());
468-
469- NewAssign->setDebugLoc (DbgAssign->getDebugLoc ());
484+ if (NewAssign != DbgAssign) {
485+ NewAssign->moveBefore (DbgAssign->getIterator ());
486+ NewAssign->setDebugLoc (DbgAssign->getDebugLoc ());
487+ }
470488 LLVM_DEBUG (dbgs () << " Created new assign: " << *NewAssign << " \n " );
471489 };
472490
0 commit comments