@@ -62,13 +62,24 @@ static cl::opt<bool> AllowIncompleteIR(
62
62
" Allow incomplete IR on a best effort basis (references to unknown "
63
63
" metadata will be dropped)" ));
64
64
65
+ extern llvm::cl::opt<bool > UseNewDbgInfoFormat;
66
+
65
67
static std::string getTypeString (Type *T) {
66
68
std::string Result;
67
69
raw_string_ostream Tmp (Result);
68
70
Tmp << *T;
69
71
return Tmp.str ();
70
72
}
71
73
74
+ // Currently, we should always process modules in the old debug info format by
75
+ // default regardless of the module's format in IR; convert it to the old format
76
+ // here.
77
+ bool finalizeDebugInfoFormat (Module *M) {
78
+ if (M)
79
+ M->setIsNewDbgInfoFormat (false );
80
+ return false ;
81
+ }
82
+
72
83
// / Run: module ::= toplevelentity*
73
84
bool LLParser::Run (bool UpgradeDebugInfo,
74
85
DataLayoutCallbackTy DataLayoutCallback) {
@@ -86,7 +97,7 @@ bool LLParser::Run(bool UpgradeDebugInfo,
86
97
}
87
98
88
99
return parseTopLevelEntities () || validateEndOfModule (UpgradeDebugInfo) ||
89
- validateEndOfIndex ();
100
+ validateEndOfIndex () || finalizeDebugInfoFormat (M) ;
90
101
}
91
102
92
103
bool LLParser::parseStandaloneConstantValue (Constant *&C,
@@ -6041,6 +6052,17 @@ bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
6041
6052
return false ;
6042
6053
}
6043
6054
6055
+ bool isOldDbgFormatIntrinsic (StringRef Name) {
6056
+ // Exit early for the common (non-debug-intrinsic) case.
6057
+ // We can make this the only check when we begin supporting all "llvm.dbg"
6058
+ // intrinsics in the new debug info format.
6059
+ if (!Name.starts_with (" llvm.dbg." ))
6060
+ return false ;
6061
+ Intrinsic::ID FnID = Function::lookupIntrinsicID (Name);
6062
+ return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value ||
6063
+ FnID == Intrinsic::dbg_assign;
6064
+ }
6065
+
6044
6066
// / FunctionHeader
6045
6067
// / ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
6046
6068
// / OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
@@ -6390,9 +6412,31 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
6390
6412
6391
6413
std::string NameStr;
6392
6414
6393
- // parse the instructions in this block until we get a terminator.
6415
+ // Parse the instructions and debug values in this block until we get a
6416
+ // terminator.
6394
6417
Instruction *Inst;
6418
+ auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord (); };
6419
+ using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype (DeleteDbgRecord)>;
6420
+ SmallVector<DbgRecordPtr> TrailingDbgRecord;
6395
6421
do {
6422
+ // Handle debug records first - there should always be an instruction
6423
+ // following the debug records, i.e. they cannot appear after the block
6424
+ // terminator.
6425
+ while (Lex.getKind () == lltok::hash) {
6426
+ if (SeenOldDbgInfoFormat)
6427
+ return error (Lex.getLoc (), " debug record should not appear in a module "
6428
+ " containing debug info intrinsics" );
6429
+ SeenNewDbgInfoFormat = true ;
6430
+ Lex.Lex ();
6431
+ if (!M->IsNewDbgInfoFormat )
6432
+ M->convertToNewDbgValues ();
6433
+
6434
+ DbgRecord *DR;
6435
+ if (parseDebugRecord (DR, PFS))
6436
+ return true ;
6437
+ TrailingDbgRecord.emplace_back (DR, DeleteDbgRecord);
6438
+ }
6439
+
6396
6440
// This instruction may have three possibilities for a name: a) none
6397
6441
// specified, b) name specified "%foo =", c) number specified: "%4 =".
6398
6442
LocTy NameLoc = Lex.getLoc ();
@@ -6437,11 +6481,121 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
6437
6481
// Set the name on the instruction.
6438
6482
if (PFS.setInstName (NameID, NameStr, NameLoc, Inst))
6439
6483
return true ;
6484
+
6485
+ // Attach any preceding debug values to this instruction.
6486
+ for (DbgRecordPtr &DR : TrailingDbgRecord)
6487
+ BB->insertDPValueBefore (DR.release (), Inst->getIterator ());
6488
+ TrailingDbgRecord.clear ();
6440
6489
} while (!Inst->isTerminator ());
6441
6490
6491
+ assert (TrailingDbgRecord.empty () &&
6492
+ " All debug values should have been attached to an instruction." );
6493
+
6442
6494
return false ;
6443
6495
}
6444
6496
6497
+ // / parseDebugRecord
6498
+ // / ::= #dbg_label '(' MDNode ')'
6499
+ // / ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
6500
+ // / (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
6501
+ bool LLParser::parseDebugRecord (DbgRecord *&DR, PerFunctionState &PFS) {
6502
+ using RecordKind = DbgRecord::Kind;
6503
+ using LocType = DPValue::LocationType;
6504
+ LocTy DPVLoc = Lex.getLoc ();
6505
+ if (Lex.getKind () != lltok::DbgRecordType)
6506
+ return error (DPVLoc, " expected debug record type here" );
6507
+ RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal ())
6508
+ .Case (" declare" , RecordKind::ValueKind)
6509
+ .Case (" value" , RecordKind::ValueKind)
6510
+ .Case (" assign" , RecordKind::ValueKind)
6511
+ .Case (" label" , RecordKind::LabelKind);
6512
+
6513
+ // Parsing labels is trivial; parse here and early exit, otherwise go into the
6514
+ // full DPValue processing stage.
6515
+ if (RecordType == RecordKind::LabelKind) {
6516
+ Lex.Lex ();
6517
+ if (parseToken (lltok::lparen, " Expected '(' here" ))
6518
+ return true ;
6519
+ MDNode *Label;
6520
+ if (parseMDNode (Label))
6521
+ return true ;
6522
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6523
+ return true ;
6524
+ MDNode *DbgLoc;
6525
+ if (parseMDNode (DbgLoc))
6526
+ return true ;
6527
+ if (parseToken (lltok::rparen, " Expected ')' here" ))
6528
+ return true ;
6529
+ DR = DPLabel::createUnresolvedDPLabel (Label, DbgLoc);
6530
+ return false ;
6531
+ }
6532
+
6533
+ LocType ValueType = StringSwitch<LocType>(Lex.getStrVal ())
6534
+ .Case (" declare" , LocType::Declare)
6535
+ .Case (" value" , LocType::Value)
6536
+ .Case (" assign" , LocType::Assign);
6537
+
6538
+ Lex.Lex ();
6539
+ if (parseToken (lltok::lparen, " Expected '(' here" ))
6540
+ return true ;
6541
+
6542
+ // Parse Value field.
6543
+ Metadata *ValLocMD;
6544
+ if (parseMetadata (ValLocMD, &PFS))
6545
+ return true ;
6546
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6547
+ return true ;
6548
+
6549
+ // Parse Variable field.
6550
+ MDNode *Variable;
6551
+ if (parseMDNode (Variable))
6552
+ return true ;
6553
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6554
+ return true ;
6555
+
6556
+ // Parse Expression field.
6557
+ MDNode *Expression;
6558
+ if (parseMDNode (Expression))
6559
+ return true ;
6560
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6561
+ return true ;
6562
+
6563
+ // Parse additional fields for #dbg_assign.
6564
+ MDNode *AssignID = nullptr ;
6565
+ Metadata *AddressLocation = nullptr ;
6566
+ MDNode *AddressExpression = nullptr ;
6567
+ if (ValueType == LocType::Assign) {
6568
+ // Parse DIAssignID.
6569
+ if (parseMDNode (AssignID))
6570
+ return true ;
6571
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6572
+ return true ;
6573
+
6574
+ // Parse address ValueAsMetadata.
6575
+ if (parseMetadata (AddressLocation, &PFS))
6576
+ return true ;
6577
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6578
+ return true ;
6579
+
6580
+ // Parse address DIExpression.
6581
+ if (parseMDNode (AddressExpression))
6582
+ return true ;
6583
+ if (parseToken (lltok::comma, " Expected ',' here" ))
6584
+ return true ;
6585
+ }
6586
+
6587
+ // / Parse DILocation.
6588
+ MDNode *DebugLoc;
6589
+ if (parseMDNode (DebugLoc))
6590
+ return true ;
6591
+
6592
+ if (parseToken (lltok::rparen, " Expected ')' here" ))
6593
+ return true ;
6594
+ DR = DPValue::createUnresolvedDPValue (ValueType, ValLocMD, Variable,
6595
+ Expression, AssignID, AddressLocation,
6596
+ AddressExpression, DebugLoc);
6597
+ return false ;
6598
+ }
6445
6599
// ===----------------------------------------------------------------------===//
6446
6600
// Instruction Parsing.
6447
6601
// ===----------------------------------------------------------------------===//
@@ -7669,6 +7823,16 @@ bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
7669
7823
}
7670
7824
CI->setFastMathFlags (FMF);
7671
7825
}
7826
+
7827
+ if (CalleeID.Kind == ValID::t_GlobalName &&
7828
+ isOldDbgFormatIntrinsic (CalleeID.StrVal )) {
7829
+ if (SeenNewDbgInfoFormat) {
7830
+ CI->deleteValue ();
7831
+ return error (CallLoc, " llvm.dbg intrinsic should not appear in a module "
7832
+ " using non-intrinsic debug info" );
7833
+ }
7834
+ SeenOldDbgInfoFormat = true ;
7835
+ }
7672
7836
CI->setAttributes (PAL);
7673
7837
ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
7674
7838
Inst = CI;
0 commit comments