@@ -191,9 +191,11 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
191191 void emitArtificialFunction (IRBuilder &Builder, llvm::Function *Fn,
192192 SILType SILTy);
193193
194- void handleFragmentDIExpr (const SILDIExprOperand &CurDIExprOp,
194+ // / Return false if we fail to create the right DW_OP_LLVM_fragment operand.
195+ bool handleFragmentDIExpr (const SILDIExprOperand &CurDIExprOp,
195196 SmallVectorImpl<uint64_t > &Operands);
196- void buildDebugInfoExpression (const SILDebugVariable &VarInfo,
197+ // / Return false if we fail to create the desired !DIExpression.
198+ bool buildDebugInfoExpression (const SILDebugVariable &VarInfo,
197199 SmallVectorImpl<uint64_t > &Operands);
198200
199201 void emitVariableDeclaration (IRBuilder &Builder,
@@ -2336,9 +2338,8 @@ void IRGenDebugInfoImpl::emitArtificialFunction(IRBuilder &Builder,
23362338 setCurrentLoc (Builder, Scope, ALoc);
23372339}
23382340
2339- void IRGenDebugInfoImpl::handleFragmentDIExpr (
2340- const SILDIExprOperand &CurDIExprOp,
2341- SmallVectorImpl<uint64_t > &Operands) {
2341+ bool IRGenDebugInfoImpl::handleFragmentDIExpr (
2342+ const SILDIExprOperand &CurDIExprOp, SmallVectorImpl<uint64_t > &Operands) {
23422343 assert (CurDIExprOp.getOperator () == SILDIExprOperator::Fragment);
23432344 // Expecting a VarDecl that points to a field in an struct
23442345 auto DIExprArgs = CurDIExprOp.args ();
@@ -2348,14 +2349,22 @@ void IRGenDebugInfoImpl::handleFragmentDIExpr(
23482349 " DIExprOperator::Fragment" );
23492350 // Translate the based type
23502351 DeclContext *ParentDecl = VD->getDeclContext ();
2352+ assert (ParentDecl && " VarDecl has no parent context?" );
23512353 SILType ParentSILType =
23522354 IGM.getLoweredType (ParentDecl->getDeclaredTypeInContext ());
23532355 // Retrieve the offset & size of the field
23542356 llvm::Constant *Offset =
23552357 emitPhysicalStructMemberFixedOffset (IGM, ParentSILType, VD);
2356- llvm::Type *FieldTy =
2357- getPhysicalStructFieldTypeInfo (IGM, ParentSILType, VD)->getStorageType ();
2358- assert (Offset && FieldTy && " Non-fixed type?" );
2358+ auto *FieldTypeInfo = getPhysicalStructFieldTypeInfo (IGM, ParentSILType, VD);
2359+ // FIXME: This will only happen if IRGen hasn't processed ParentSILType
2360+ // (into its own representation) but we probably should ask IRGen to process
2361+ // it right now.
2362+ if (!FieldTypeInfo)
2363+ return false ;
2364+ llvm::Type *FieldTy = FieldTypeInfo->getStorageType ();
2365+ // Doesn't support non-fixed type right now
2366+ if (!Offset || !FieldTy)
2367+ return false ;
23592368
23602369 uint64_t SizeOfByte = CI.getTargetInfo ().getCharWidth ();
23612370 uint64_t SizeInBits = IGM.DataLayout .getTypeSizeInBits (FieldTy);
@@ -2366,22 +2375,26 @@ void IRGenDebugInfoImpl::handleFragmentDIExpr(
23662375 Operands.push_back (llvm::dwarf::DW_OP_LLVM_fragment);
23672376 Operands.push_back (OffsetInBits);
23682377 Operands.push_back (SizeInBits);
2378+
2379+ return true ;
23692380}
23702381
2371- void IRGenDebugInfoImpl::buildDebugInfoExpression (
2382+ bool IRGenDebugInfoImpl::buildDebugInfoExpression (
23722383 const SILDebugVariable &VarInfo, SmallVectorImpl<uint64_t > &Operands) {
23732384 assert (VarInfo.DIExpr && " SIL debug info expression not found" );
23742385
23752386 const auto &DIExpr = VarInfo.DIExpr ;
23762387 for (const SILDIExprOperand &ExprOperand : DIExpr.operands ()) {
23772388 switch (ExprOperand.getOperator ()) {
23782389 case SILDIExprOperator::Fragment:
2379- handleFragmentDIExpr (ExprOperand, Operands);
2390+ if (!handleFragmentDIExpr (ExprOperand, Operands))
2391+ return false ;
23802392 break ;
23812393 default :
2382- llvm_unreachable (" Unrecoginized operator" );
2394+ llvm_unreachable (" Unrecognized operator" );
23832395 }
23842396 }
2397+ return true ;
23852398}
23862399
23872400void IRGenDebugInfoImpl::emitVariableDeclaration (
@@ -2455,7 +2468,8 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
24552468 [&VarInfo, this ](llvm::DIExpression *DIExpr) -> llvm::DIExpression * {
24562469 if (VarInfo.DIExpr ) {
24572470 llvm::SmallVector<uint64_t , 2 > Operands;
2458- buildDebugInfoExpression (VarInfo, Operands);
2471+ if (!buildDebugInfoExpression (VarInfo, Operands))
2472+ return nullptr ;
24592473 if (Operands.size ())
24602474 return llvm::DIExpression::append (DIExpr, Operands);
24612475 }
@@ -2493,22 +2507,24 @@ void IRGenDebugInfoImpl::emitVariableDeclaration(
24932507 Operands.push_back (SizeInBits);
24942508 }
24952509 llvm::DIExpression *DIExpr = DBuilder.createExpression (Operands);
2496- emitDbgIntrinsic (Builder, Piece, Var,
2497- // DW_OP_LLVM_fragment must be the last part of an DIExpr
2498- // so we can't append more if IsPiece is true.
2499- IsPiece ? DIExpr : appendDIExpression (DIExpr), DInstLine,
2500- DInstLoc.column , Scope, DS,
2501- Indirection == CoroDirectValue ||
2502- Indirection == CoroIndirectValue);
2510+ // DW_OP_LLVM_fragment must be the last part of an DIExpr
2511+ // so we can't append more if IsPiece is true.
2512+ if (!IsPiece)
2513+ DIExpr = appendDIExpression (DIExpr);
2514+ if (DIExpr)
2515+ emitDbgIntrinsic (
2516+ Builder, Piece, Var, DIExpr, DInstLine, DInstLoc.column , Scope, DS,
2517+ Indirection == CoroDirectValue || Indirection == CoroIndirectValue);
25032518 }
25042519
25052520 // Emit locationless intrinsic for variables that were optimized away.
2506- if (Storage.empty ())
2507- emitDbgIntrinsic (Builder, llvm::ConstantInt::get (IGM.Int64Ty , 0 ), Var,
2508- appendDIExpression (DBuilder.createExpression ()), DInstLine,
2509- DInstLoc.column , Scope, DS,
2510- Indirection == CoroDirectValue ||
2511- Indirection == CoroIndirectValue);
2521+ if (Storage.empty ()) {
2522+ if (auto *DIExpr = appendDIExpression (DBuilder.createExpression ()))
2523+ emitDbgIntrinsic (Builder, llvm::ConstantInt::get (IGM.Int64Ty , 0 ), Var,
2524+ DIExpr, DInstLine, DInstLoc.column , Scope, DS,
2525+ Indirection == CoroDirectValue ||
2526+ Indirection == CoroIndirectValue);
2527+ }
25122528}
25132529
25142530void IRGenDebugInfoImpl::emitDbgIntrinsic (
@@ -2554,7 +2570,19 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
25542570 DBuilder.insertDeclare (Storage, Var, Expr, DL, &EntryBlock);
25552571 } else {
25562572 // Insert a dbg.value at the current insertion point.
2557- DBuilder.insertDbgValueIntrinsic (Storage, Var, Expr, DL, BB);
2573+ if (isa<llvm::Argument>(Storage) && !Var->getArg () &&
2574+ BB->getFirstNonPHIOrDbg ())
2575+ // SelectionDAGISel only generates debug info for a dbg.value
2576+ // that is associated with a llvm::Argument if either its !DIVariable
2577+ // is marked as argument or there is no non-debug intrinsic instruction
2578+ // before it. So In the case of associating a llvm::Argument with a
2579+ // non-argument debug variable -- usually via a !DIExpression -- we
2580+ // need to make sure that dbg.value is before any non-phi / no-dbg
2581+ // instruction.
2582+ DBuilder.insertDbgValueIntrinsic (Storage, Var, Expr, DL,
2583+ BB->getFirstNonPHIOrDbg ());
2584+ else
2585+ DBuilder.insertDbgValueIntrinsic (Storage, Var, Expr, DL, BB);
25582586 }
25592587}
25602588
0 commit comments