@@ -108,6 +108,9 @@ cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat(
108
108
" load-bitcode-into-experimental-debuginfo-iterators" , cl::Hidden,
109
109
cl::desc (" Load bitcode directly into the new debug info format (regardless "
110
110
" of input format)" ));
111
+ extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
112
+ extern bool WriteNewDbgInfoFormatToBitcode;
113
+ extern cl::opt<bool > WriteNewDbgInfoFormat;
111
114
112
115
namespace {
113
116
@@ -682,6 +685,11 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
682
685
// / (e.g.) blockaddress forward references.
683
686
bool WillMaterializeAllForwardRefs = false ;
684
687
688
+ // / Tracks whether we have seen debug intrinsics or records in this bitcode;
689
+ // / seeing both in a single module is currently a fatal error.
690
+ bool SeenDebugIntrinsic = false ;
691
+ bool SeenDebugRecord = false ;
692
+
685
693
bool StripDebugInfo = false ;
686
694
TBAAVerifier TBAAVerifyHelper;
687
695
@@ -3774,7 +3782,11 @@ Error BitcodeReader::globalCleanup() {
3774
3782
for (Function &F : *TheModule) {
3775
3783
MDLoader->upgradeDebugIntrinsics (F);
3776
3784
Function *NewFn;
3777
- if (UpgradeIntrinsicFunction (&F, NewFn))
3785
+ // If PreserveInputDbgFormat=true, then we don't know whether we want
3786
+ // intrinsics or records, and we won't perform any conversions in either
3787
+ // case, so don't upgrade intrinsics to records.
3788
+ if (UpgradeIntrinsicFunction (
3789
+ &F, NewFn, PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE))
3778
3790
UpgradedIntrinsics[&F] = NewFn;
3779
3791
// Look for functions that rely on old function attribute behavior.
3780
3792
UpgradeFunctionAttributes (F);
@@ -4301,10 +4313,13 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
4301
4313
bool ShouldLazyLoadMetadata,
4302
4314
ParserCallbacks Callbacks) {
4303
4315
// Load directly into RemoveDIs format if LoadBitcodeIntoNewDbgInfoFormat
4304
- // has been set to true (default action: load into the old debug format).
4305
- TheModule->IsNewDbgInfoFormat =
4306
- UseNewDbgInfoFormat &&
4307
- LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE;
4316
+ // has been set to true and we aren't attempting to preserve the existing
4317
+ // format in the bitcode (default action: load into the old debug format).
4318
+ if (PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE) {
4319
+ TheModule->IsNewDbgInfoFormat =
4320
+ UseNewDbgInfoFormat &&
4321
+ LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE;
4322
+ }
4308
4323
4309
4324
this ->ValueTypeCallback = std::move (Callbacks.ValueType );
4310
4325
if (ResumeBit) {
@@ -6453,6 +6468,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
6453
6468
case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
6454
6469
// DbgVariableRecords are placed after the Instructions that they are
6455
6470
// attached to.
6471
+ SeenDebugRecord = true ;
6456
6472
Instruction *Inst = getLastInstruction ();
6457
6473
if (!Inst)
6458
6474
return error (" Invalid dbg record: missing instruction" );
@@ -6613,6 +6629,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
6613
6629
TCK = CallInst::TCK_NoTail;
6614
6630
cast<CallInst>(I)->setTailCallKind (TCK);
6615
6631
cast<CallInst>(I)->setAttributes (PAL);
6632
+ if (isa<DbgInfoIntrinsic>(I))
6633
+ SeenDebugIntrinsic = true ;
6616
6634
if (Error Err = propagateAttributeTypes (cast<CallBase>(I), ArgTyIDs)) {
6617
6635
I->deleteValue ();
6618
6636
return Err;
@@ -6801,20 +6819,48 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
6801
6819
if (Error JumpFailed = Stream.JumpToBit (DFII->second ))
6802
6820
return JumpFailed;
6803
6821
6804
- // Set the debug info mode to "new", possibly creating a mismatch between
6805
- // module and function debug modes. This is okay because we'll convert
6806
- // everything back to the old mode after parsing if needed.
6807
- // FIXME: Remove this once all tools support RemoveDIs.
6822
+ // Regardless of the debug info format we want to end up in, we need
6823
+ // IsNewDbgInfoFormat=true to construct any debug records seen in the bitcode.
6808
6824
F->IsNewDbgInfoFormat = true ;
6809
6825
6810
6826
if (Error Err = parseFunctionBody (F))
6811
6827
return Err;
6812
6828
F->setIsMaterializable (false );
6813
6829
6814
- // Convert new debug info records into intrinsics.
6815
- // FIXME: Remove this once all tools support RemoveDIs.
6816
- if (!F->getParent ()->IsNewDbgInfoFormat )
6817
- F->convertFromNewDbgValues ();
6830
+ // All parsed Functions should load into the debug info format dictated by the
6831
+ // Module, unless we're attempting to preserve the input debug info format.
6832
+ if (SeenDebugIntrinsic && SeenDebugRecord)
6833
+ return error (" Mixed debug intrinsics and debug records in bitcode module!" );
6834
+ if (PreserveInputDbgFormat == cl::boolOrDefault::BOU_TRUE) {
6835
+ bool SeenAnyDebugInfo = SeenDebugIntrinsic || SeenDebugRecord;
6836
+ bool NewDbgInfoFormatDesired =
6837
+ SeenAnyDebugInfo ? SeenDebugRecord : F->getParent ()->IsNewDbgInfoFormat ;
6838
+ if (SeenAnyDebugInfo) {
6839
+ UseNewDbgInfoFormat = SeenDebugRecord;
6840
+ WriteNewDbgInfoFormatToBitcode = SeenDebugRecord;
6841
+ WriteNewDbgInfoFormat = SeenDebugRecord;
6842
+ }
6843
+ // If the module's debug info format doesn't match the observed input
6844
+ // format, then set its format now; we don't need to call the conversion
6845
+ // function because there must be no existing intrinsics to convert.
6846
+ // Otherwise, just set the format on this function now.
6847
+ if (NewDbgInfoFormatDesired != F->getParent ()->IsNewDbgInfoFormat )
6848
+ F->getParent ()->setNewDbgInfoFormatFlag (NewDbgInfoFormatDesired);
6849
+ else
6850
+ F->setNewDbgInfoFormatFlag (NewDbgInfoFormatDesired);
6851
+ } else {
6852
+ // If we aren't preserving formats, we use the Module flag to get our
6853
+ // desired format instead of reading flags, in case we are lazy-loading and
6854
+ // the format of the module has been changed since it was set by the flags.
6855
+ // We only need to convert debug info here if we have debug records but
6856
+ // desire the intrinsic format; everything else is a no-op or handled by the
6857
+ // autoupgrader.
6858
+ bool ModuleIsNewDbgInfoFormat = F->getParent ()->IsNewDbgInfoFormat ;
6859
+ if (ModuleIsNewDbgInfoFormat || !SeenDebugRecord)
6860
+ F->setNewDbgInfoFormatFlag (ModuleIsNewDbgInfoFormat);
6861
+ else
6862
+ F->setIsNewDbgInfoFormat (ModuleIsNewDbgInfoFormat);
6863
+ }
6818
6864
6819
6865
if (StripDebugInfo)
6820
6866
stripDebugInfo (*F);
0 commit comments