Skip to content

Commit cfc9fa7

Browse files
author
git apple-llvm automerger
committed
Merge commit '76197ea6f91f' from llvm.org/main into next
2 parents 2c9d89e + 76197ea commit cfc9fa7

File tree

6 files changed

+150
-40
lines changed

6 files changed

+150
-40
lines changed

llvm/lib/CodeGen/SelectionDAG/FastISel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,9 @@ void FastISel::fastEmitBranch(MachineBasicBlock *MSucc,
16731673
const DebugLoc &DbgLoc) {
16741674
const BasicBlock *BB = FuncInfo.MBB->getBasicBlock();
16751675
bool BlockHasMultipleInstrs = &BB->front() != &BB->back();
1676+
// Handle legacy case of debug intrinsics
1677+
if (BlockHasMultipleInstrs && !BB->getModule()->IsNewDbgInfoFormat)
1678+
BlockHasMultipleInstrs = BB->sizeWithoutDebug() > 1;
16761679
if (BlockHasMultipleInstrs && FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
16771680
// For more accurate line information if this is the only non-debug
16781681
// instruction in the block then emit it, otherwise we have the

llvm/lib/IR/AutoUpgrade.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,6 +4514,7 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
45144514
Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
45154515

45164516
if (!NewFn) {
4517+
bool FallthroughToDefaultUpgrade = false;
45174518
// Get the Function's name.
45184519
StringRef Name = F->getName();
45194520

@@ -4541,15 +4542,29 @@ void llvm::UpgradeIntrinsicCall(CallBase *CI, Function *NewFn) {
45414542
} else if (IsAMDGCN) {
45424543
Rep = upgradeAMDGCNIntrinsicCall(Name, CI, F, Builder);
45434544
} else if (IsDbg) {
4544-
upgradeDbgIntrinsicToDbgRecord(Name, CI);
4545+
// We might have decided we don't want the new format after all between
4546+
// first requesting the upgrade and now; skip the conversion if that is
4547+
// the case, and check here to see if the intrinsic needs to be upgraded
4548+
// normally.
4549+
if (!CI->getModule()->IsNewDbgInfoFormat) {
4550+
bool NeedsUpgrade =
4551+
upgradeIntrinsicFunction1(CI->getCalledFunction(), NewFn, false);
4552+
if (!NeedsUpgrade)
4553+
return;
4554+
FallthroughToDefaultUpgrade = true;
4555+
} else {
4556+
upgradeDbgIntrinsicToDbgRecord(Name, CI);
4557+
}
45454558
} else {
45464559
llvm_unreachable("Unknown function for CallBase upgrade.");
45474560
}
45484561

4549-
if (Rep)
4550-
CI->replaceAllUsesWith(Rep);
4551-
CI->eraseFromParent();
4552-
return;
4562+
if (!FallthroughToDefaultUpgrade) {
4563+
if (Rep)
4564+
CI->replaceAllUsesWith(Rep);
4565+
CI->eraseFromParent();
4566+
return;
4567+
}
45534568
}
45544569

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

llvm/lib/IR/DIBuilder.cpp

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,13 +1047,36 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10471047
LinkedInstr->getMetadata(LLVMContext::MD_DIAssignID));
10481048
assert(Link && "Linked instruction must have DIAssign metadata attached");
10491049

1050-
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
1051-
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
1052-
// Insert after LinkedInstr.
1053-
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
1054-
NextIt.setHeadBit(true);
1055-
insertDbgVariableRecord(DVR, NextIt);
1056-
return DVR;
1050+
if (M.IsNewDbgInfoFormat) {
1051+
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
1052+
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
1053+
// Insert after LinkedInstr.
1054+
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
1055+
NextIt.setHeadBit(true);
1056+
insertDbgVariableRecord(DVR, NextIt);
1057+
return DVR;
1058+
}
1059+
1060+
LLVMContext &Ctx = LinkedInstr->getContext();
1061+
Module *M = LinkedInstr->getModule();
1062+
if (!AssignFn)
1063+
AssignFn = Intrinsic::getOrInsertDeclaration(M, Intrinsic::dbg_assign);
1064+
1065+
std::array<Value *, 6> Args = {
1066+
MetadataAsValue::get(Ctx, ValueAsMetadata::get(Val)),
1067+
MetadataAsValue::get(Ctx, SrcVar),
1068+
MetadataAsValue::get(Ctx, ValExpr),
1069+
MetadataAsValue::get(Ctx, Link),
1070+
MetadataAsValue::get(Ctx, ValueAsMetadata::get(Addr)),
1071+
MetadataAsValue::get(Ctx, AddrExpr),
1072+
};
1073+
1074+
IRBuilder<> B(Ctx);
1075+
B.SetCurrentDebugLocation(DL);
1076+
1077+
auto *DVI = cast<DbgAssignIntrinsic>(B.CreateCall(AssignFn, Args));
1078+
DVI->insertAfter(LinkedInstr->getIterator());
1079+
return DVI;
10571080
}
10581081

10591082
/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
@@ -1078,10 +1101,18 @@ DbgInstPtr DIBuilder::insertDbgValueIntrinsic(llvm::Value *Val,
10781101
DIExpression *Expr,
10791102
const DILocation *DL,
10801103
InsertPosition InsertPt) {
1081-
DbgVariableRecord *DVR =
1082-
DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL);
1083-
insertDbgVariableRecord(DVR, InsertPt);
1084-
return DVR;
1104+
if (M.IsNewDbgInfoFormat) {
1105+
DbgVariableRecord *DVR =
1106+
DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL);
1107+
insertDbgVariableRecord(DVR, InsertPt);
1108+
return DVR;
1109+
}
1110+
1111+
if (!ValueFn)
1112+
ValueFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_value);
1113+
auto *DVI = insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertPt);
1114+
cast<CallInst>(DVI)->setTailCall();
1115+
return DVI;
10851116
}
10861117

10871118
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
@@ -1093,10 +1124,25 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10931124
VarInfo->getScope()->getSubprogram() &&
10941125
"Expected matching subprograms");
10951126

1096-
DbgVariableRecord *DVR =
1097-
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1098-
insertDbgVariableRecord(DVR, InsertPt);
1099-
return DVR;
1127+
if (M.IsNewDbgInfoFormat) {
1128+
DbgVariableRecord *DVR =
1129+
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1130+
insertDbgVariableRecord(DVR, InsertPt);
1131+
return DVR;
1132+
}
1133+
1134+
if (!DeclareFn)
1135+
DeclareFn = getDeclareIntrin(M);
1136+
1137+
trackIfUnresolved(VarInfo);
1138+
trackIfUnresolved(Expr);
1139+
Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
1140+
MetadataAsValue::get(VMContext, VarInfo),
1141+
MetadataAsValue::get(VMContext, Expr)};
1142+
1143+
IRBuilder<> B(DL->getContext());
1144+
initIRBuilder(B, DL, InsertPt);
1145+
return B.CreateCall(DeclareFn, Args);
11001146
}
11011147

11021148
void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
@@ -1145,12 +1191,23 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11451191
"Expected matching subprograms");
11461192

11471193
trackIfUnresolved(LabelInfo);
1148-
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1149-
if (InsertPt.isValid()) {
1150-
auto *BB = InsertPt.getBasicBlock();
1151-
BB->insertDbgRecordBefore(DLR, InsertPt);
1194+
if (M.IsNewDbgInfoFormat) {
1195+
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1196+
if (InsertPt.isValid()) {
1197+
auto *BB = InsertPt.getBasicBlock();
1198+
BB->insertDbgRecordBefore(DLR, InsertPt);
1199+
}
1200+
return DLR;
11521201
}
1153-
return DLR;
1202+
1203+
if (!LabelFn)
1204+
LabelFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_label);
1205+
1206+
Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
1207+
1208+
IRBuilder<> B(DL->getContext());
1209+
initIRBuilder(B, DL, InsertPt);
1210+
return B.CreateCall(LabelFn, Args);
11541211
}
11551212

11561213
void DIBuilder::replaceVTableHolder(DICompositeType *&T, DIType *VTableHolder) {

llvm/lib/IR/DebugInfo.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,11 +2123,22 @@ static void emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
21232123
Expr = *R;
21242124
}
21252125
DIExpression *AddrExpr = DIExpression::get(StoreLikeInst.getContext(), {});
2126-
auto *Assign = DbgVariableRecord::createLinkedDVRAssign(
2127-
&StoreLikeInst, Val, VarRec.Var, Expr, Dest, AddrExpr, VarRec.DL);
2126+
if (StoreLikeInst.getParent()->IsNewDbgInfoFormat) {
2127+
auto *Assign = DbgVariableRecord::createLinkedDVRAssign(
2128+
&StoreLikeInst, Val, VarRec.Var, Expr, Dest, AddrExpr, VarRec.DL);
2129+
(void)Assign;
2130+
LLVM_DEBUG(if (Assign) errs() << " > INSERT: " << *Assign << "\n");
2131+
return;
2132+
}
2133+
auto Assign = DIB.insertDbgAssign(&StoreLikeInst, Val, VarRec.Var, Expr, Dest,
2134+
AddrExpr, VarRec.DL);
21282135
(void)Assign;
2129-
LLVM_DEBUG(if (Assign) errs() << " > INSERT: " << *Assign << "\n");
2130-
return;
2136+
LLVM_DEBUG(if (!Assign.isNull()) {
2137+
if (const auto *Record = dyn_cast<DbgRecord *>(Assign))
2138+
errs() << " > INSERT: " << *Record << "\n";
2139+
else
2140+
errs() << " > INSERT: " << *cast<Instruction *>(Assign) << "\n";
2141+
});
21312142
}
21322143

21332144
#undef DEBUG_TYPE // Silence redefinition warning (from ConstantsContext.h).

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
606606

607607
// Use a map to unique and a vector to guarantee deterministic ordering.
608608
llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet;
609+
llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
609610
llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
610611

611612
if (ExitBlock) {
@@ -632,19 +633,29 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
632633
U.set(Poison);
633634
}
634635

635-
// For one of each variable encountered, preserve a debug record (set
636+
// RemoveDIs: do the same as below for DbgVariableRecords.
637+
if (Block->IsNewDbgInfoFormat) {
638+
for (DbgVariableRecord &DVR : llvm::make_early_inc_range(
639+
filterDbgVars(I.getDbgRecordRange()))) {
640+
DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
641+
DVR.getDebugLoc().get());
642+
if (!DeadDebugSet.insert(Key).second)
643+
continue;
644+
// Unlinks the DVR from it's container, for later insertion.
645+
DVR.removeFromParent();
646+
DeadDbgVariableRecords.push_back(&DVR);
647+
}
648+
}
649+
650+
// For one of each variable encountered, preserve a debug intrinsic (set
636651
// to Poison) and transfer it to the loop exit. This terminates any
637652
// variable locations that were set during the loop.
638-
for (DbgVariableRecord &DVR :
639-
llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
640-
DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
641-
DVR.getDebugLoc().get());
642-
if (!DeadDebugSet.insert(Key).second)
643-
continue;
644-
// Unlinks the DVR from it's container, for later insertion.
645-
DVR.removeFromParent();
646-
DeadDbgVariableRecords.push_back(&DVR);
647-
}
653+
auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
654+
if (!DVI)
655+
continue;
656+
if (!DeadDebugSet.insert(DebugVariable(DVI)).second)
657+
continue;
658+
DeadDebugInst.push_back(DVI);
648659
}
649660

650661
// After the loop has been deleted all the values defined and modified
@@ -660,6 +671,9 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
660671
"There should be a non-PHI instruction in exit block, else these "
661672
"instructions will have no parent.");
662673

674+
for (auto *DVI : DeadDebugInst)
675+
DVI->moveBefore(*ExitBlock, InsertDbgValueBefore);
676+
663677
// Due to the "head" bit in BasicBlock::iterator, we're going to insert
664678
// each DbgVariableRecord right at the start of the block, wheras dbg.values
665679
// would be repeatedly inserted before the first instruction. To replicate

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,18 @@ TEST_F(IRBuilderTest, DIBuilder) {
10031003
EXPECT_TRUE(verifyModule(*M));
10041004
};
10051005

1006+
// Test in new-debug mode.
1007+
EXPECT_TRUE(M->IsNewDbgInfoFormat);
10061008
RunTest();
1009+
1010+
// Test in old-debug mode.
1011+
// Reset the test then call convertFromNewDbgValues to flip the flag
1012+
// on the test's Module, Function and BasicBlock.
10071013
TearDown();
1014+
SetUp();
1015+
M->convertFromNewDbgValues();
1016+
EXPECT_FALSE(M->IsNewDbgInfoFormat);
1017+
RunTest();
10081018
}
10091019

10101020
TEST_F(IRBuilderTest, createArtificialSubprogram) {

0 commit comments

Comments
 (0)