Skip to content

Commit 404ddc9

Browse files
committed
[Debug Info] Avoid emitting a single fragment that covers the entire variable.
1 parent fcbebc5 commit 404ddc9

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,6 +3402,9 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
34023402
if (!DInstLine || (ArgNo > 0 && VarInfo.Name == IGM.Context.Id_self.str()))
34033403
Artificial = ArtificialValue;
34043404

3405+
if (VarInfo.Name == "index")
3406+
llvm::errs();
3407+
34053408
llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
34063409
if (Artificial || DITy->isArtificial() || DITy == InternalType)
34073410
Flags |= llvm::DINode::FlagArtificial;
@@ -3458,10 +3461,10 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
34583461

34593462
auto appendDIExpression =
34603463
[&VarInfo, this](llvm::DIExpression *DIExpr,
3461-
llvm::DIExpression::FragmentInfo PieceFragment)
3462-
-> llvm::DIExpression * {
3464+
llvm::DIExpression::FragmentInfo PieceFragment,
3465+
bool IsFirstAndOnlyPiece) -> llvm::DIExpression * {
34633466
if (!VarInfo.DIExpr) {
3464-
if (!PieceFragment.SizeInBits)
3467+
if (!PieceFragment.SizeInBits || IsFirstAndOnlyPiece)
34653468
return DIExpr;
34663469

34673470
return llvm::DIExpression::createFragmentExpression(
@@ -3478,7 +3481,7 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
34783481
DIExpr = llvm::DIExpression::append(DIExpr, Operands);
34793482

34803483
// Add the fragment of the SIL variable.
3481-
if (VarFragment.SizeInBits)
3484+
if (VarFragment.SizeInBits && !IsFirstAndOnlyPiece)
34823485
DIExpr = llvm::DIExpression::createFragmentExpression(
34833486
DIExpr, VarFragment.OffsetInBits, VarFragment.SizeInBits)
34843487
.value_or(nullptr);
@@ -3528,8 +3531,14 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
35283531
Fragment.OffsetInBits = OffsetInBits;
35293532
Fragment.SizeInBits = SizeInBits;
35303533
}
3534+
// LLVM complains if a single fragment covers the entire variable. This can
3535+
// happen if, e.g., the optimizer takes the _value out of an Int
3536+
// struct. Detect this case and don't emit a fragment.
3537+
bool IsFirstAndOnlyPiece =
3538+
!IsPiece && Fragment.OffsetInBits == 0 &&
3539+
Fragment.SizeInBits == getSizeInBits(Var->getType());
35313540
llvm::DIExpression *DIExpr = DBuilder.createExpression(Operands);
3532-
DIExpr = appendDIExpression(DIExpr, Fragment);
3541+
DIExpr = appendDIExpression(DIExpr, Fragment, IsFirstAndOnlyPiece);
35333542
if (DIExpr)
35343543
emitDbgIntrinsic(
35353544
Builder, Piece, Var, DIExpr, DInstLine, DInstLoc.Column, Scope, DS,
@@ -3541,7 +3550,7 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
35413550
if (Storage.empty()) {
35423551
llvm::DIExpression::FragmentInfo NoFragment = {0, 0};
35433552
if (auto *DIExpr =
3544-
appendDIExpression(DBuilder.createExpression(), NoFragment))
3553+
appendDIExpression(DBuilder.createExpression(), NoFragment, false))
35453554
emitDbgIntrinsic(Builder, llvm::ConstantInt::get(IGM.Int64Ty, 0), Var,
35463555
DIExpr, DInstLine, DInstLoc.Column, Scope, DS,
35473556
Indirection == CoroDirectValue ||

0 commit comments

Comments
 (0)