@@ -108,6 +108,9 @@ cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat(
108108 " load-bitcode-into-experimental-debuginfo-iterators" , cl::Hidden,
109109 cl::desc (" Load bitcode directly into the new debug info format (regardless "
110110 " of input format)" ));
111+ extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
112+ extern bool WriteNewDbgInfoFormatToBitcode;
113+ extern cl::opt<bool > WriteNewDbgInfoFormat;
111114
112115namespace {
113116
@@ -682,6 +685,11 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
682685 // / (e.g.) blockaddress forward references.
683686 bool WillMaterializeAllForwardRefs = false ;
684687
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+
685693 bool StripDebugInfo = false ;
686694 TBAAVerifier TBAAVerifyHelper;
687695
@@ -3774,7 +3782,11 @@ Error BitcodeReader::globalCleanup() {
37743782 for (Function &F : *TheModule) {
37753783 MDLoader->upgradeDebugIntrinsics (F);
37763784 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))
37783790 UpgradedIntrinsics[&F] = NewFn;
37793791 // Look for functions that rely on old function attribute behavior.
37803792 UpgradeFunctionAttributes (F);
@@ -4301,10 +4313,13 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
43014313 bool ShouldLazyLoadMetadata,
43024314 ParserCallbacks Callbacks) {
43034315 // 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+ }
43084323
43094324 this ->ValueTypeCallback = std::move (Callbacks.ValueType );
43104325 if (ResumeBit) {
@@ -6453,6 +6468,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
64536468 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
64546469 // DbgVariableRecords are placed after the Instructions that they are
64556470 // attached to.
6471+ SeenDebugRecord = true ;
64566472 Instruction *Inst = getLastInstruction ();
64576473 if (!Inst)
64586474 return error (" Invalid dbg record: missing instruction" );
@@ -6613,6 +6629,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
66136629 TCK = CallInst::TCK_NoTail;
66146630 cast<CallInst>(I)->setTailCallKind (TCK);
66156631 cast<CallInst>(I)->setAttributes (PAL);
6632+ if (isa<DbgInfoIntrinsic>(I))
6633+ SeenDebugIntrinsic = true ;
66166634 if (Error Err = propagateAttributeTypes (cast<CallBase>(I), ArgTyIDs)) {
66176635 I->deleteValue ();
66186636 return Err;
@@ -6801,20 +6819,48 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
68016819 if (Error JumpFailed = Stream.JumpToBit (DFII->second ))
68026820 return JumpFailed;
68036821
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.
68086824 F->IsNewDbgInfoFormat = true ;
68096825
68106826 if (Error Err = parseFunctionBody (F))
68116827 return Err;
68126828 F->setIsMaterializable (false );
68136829
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+ }
68186864
68196865 if (StripDebugInfo)
68206866 stripDebugInfo (*F);
0 commit comments