Skip to content

Commit 4a57538

Browse files
SLTozerjsji
authored andcommitted
[RemoveDIs][DebugInfo] Reject intrinsic->record upgrades for old-format modules
Fixes issue noted at: llvm/llvm-project#86274 When loading bitcode lazily, we may request debug intrinsics be upgraded to debug records during the module parsing phase; later on we perform this upgrade when materializing the module functions. If we change the module's debug info format between parsing and materializing however, then the requested upgrade is no longer correct and leads to an assertion. This patch fixes the issue by adding an extra check in the autoupgrader to see if the upgrade is no longer suitable, and either exit-out or fall back to the correct intrinsic->intrinsic upgrade if one is required. Adding this check in the autoupgrader is technically not optimal, since we could run the check a single time in a higher up call, but there's no way (that I can see) to do so without leaking through abstractions and making the bitcode/linking/materializing steps more complicated and brittle, and the check isn't particularly expensive.
1 parent a5c4266 commit 4a57538

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

llvm/include/llvm/IR/AutoUpgrade.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace llvm {
3636
/// for upgrading, and returns true if it requires upgrading. It may return
3737
/// null in NewFn if the all calls to the original intrinsic function
3838
/// should be transformed to non-function-call instructions.
39-
bool UpgradeIntrinsicFunction(Function *F, Function *&NewFn);
39+
bool UpgradeIntrinsicFunction(Function *F, Function *&NewFn,
40+
bool CanUpgradeDebugIntrinsicsToRecords = true);
4041

4142
/// This is the complement to the above, replacing a specific call to an
4243
/// intrinsic function with a call to the specified new function.

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ static Intrinsic::ID shouldUpgradeNVPTXBF16Intrinsic(StringRef Name) {
983983
return Intrinsic::not_intrinsic;
984984
}
985985

986-
static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
986+
static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,
987+
bool CanUpgradeDebugIntrinsicsToRecords) {
987988
assert(F && "Illegal to upgrade a non-existent Function.");
988989

989990
StringRef Name = F->getName();
@@ -1057,7 +1058,8 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
10571058
case 'd':
10581059
if (Name.consume_front("dbg.")) {
10591060
// Mark debug intrinsics for upgrade to new debug format.
1060-
if (F->getParent()->IsNewDbgInfoFormat) {
1061+
if (CanUpgradeDebugIntrinsicsToRecords &&
1062+
F->getParent()->IsNewDbgInfoFormat) {
10611063
if (Name == "addr" || Name == "value" || Name == "assign" ||
10621064
Name == "declare" || Name == "label") {
10631065
// There's no function to replace these with.
@@ -1407,9 +1409,11 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
14071409
return false;
14081410
}
14091411

1410-
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
1412+
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn,
1413+
bool CanUpgradeDebugIntrinsicsToRecords) {
14111414
NewFn = nullptr;
1412-
bool Upgraded = upgradeIntrinsicFunction1(F, NewFn);
1415+
bool Upgraded =
1416+
upgradeIntrinsicFunction1(F, NewFn, CanUpgradeDebugIntrinsicsToRecords);
14131417
assert(F != NewFn && "Intrinsic function upgraded to the same function");
14141418

14151419
// Upgrade intrinsic attributes. This does not change the function.
@@ -2406,6 +2410,7 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
24062410
Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
24072411

24082412
if (!NewFn) {
2413+
bool ShouldRemove = true;
24092414
// Get the Function's name.
24102415
StringRef Name = F->getName();
24112416

@@ -4256,16 +4261,28 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
42564261
Rep = upgradeARMIntrinsicCall(Name, CI, F, Builder);
42574262
} else if (IsAMDGCN) {
42584263
Rep = upgradeAMDGCNIntrinsicCall(Name, CI, F, Builder);
4259-
} else if (IsDbg && CI->getModule()->IsNewDbgInfoFormat) {
4260-
upgradeDbgIntrinsicToDbgRecord(Name, CI);
4264+
} else if (IsDbg) {
4265+
// We might have decided we don't want the new format after all since
4266+
// requesting the upgrade; skip the conversion if that is the case, and
4267+
// check here to see if the intrinsic needs to be upgraded normally.
4268+
if (!CI->getModule()->IsNewDbgInfoFormat) {
4269+
ShouldRemove = false;
4270+
bool NeedsUpgrade = upgradeIntrinsicFunction1(CI->getCalledFunction(), NewFn, false);
4271+
if (!NeedsUpgrade)
4272+
return;
4273+
} else {
4274+
upgradeDbgIntrinsicToDbgRecord(Name, CI);
4275+
}
42614276
} else {
42624277
llvm_unreachable("Unknown function for CallBase upgrade.");
42634278
}
42644279

42654280
if (Rep)
42664281
CI->replaceAllUsesWith(Rep);
4267-
CI->eraseFromParent();
4268-
return;
4282+
if (ShouldRemove) {
4283+
CI->eraseFromParent();
4284+
return;
4285+
}
42694286
}
42704287

42714288
const auto &DefaultCase = [&]() -> void {

0 commit comments

Comments
 (0)