Skip to content

Commit c53c427

Browse files
author
git apple-llvm automerger
committed
Merge commit 'a08a83151591' from llvm.org/main into next
2 parents 8f8c455 + a08a831 commit c53c427

22 files changed

+133
-66
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class LegalizationArtifactCombiner {
100100
const LLT DstTy = MRI.getType(DstReg);
101101
if (isInstLegal({TargetOpcode::G_CONSTANT, {DstTy}})) {
102102
auto &CstVal = SrcMI->getOperand(1);
103-
auto *MergedLocation = DILocation::getMergedLocation(
104-
MI.getDebugLoc().get(), SrcMI->getDebugLoc().get());
103+
auto MergedLocation =
104+
DebugLoc::getMergedLocation(MI.getDebugLoc(), SrcMI->getDebugLoc());
105105
// Set the debug location to the merged location of the SrcMI and the MI
106106
// if the aext fold is successful.
107107
Builder.setDebugLoc(MergedLocation);

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,25 +2444,21 @@ class DILocation : public MDNode {
24442444
inline std::optional<const DILocation *>
24452445
cloneByMultiplyingDuplicationFactor(unsigned DF) const;
24462446

2447-
/// When two instructions are combined into a single instruction we also
2448-
/// need to combine the original locations into a single location.
2449-
/// When the locations are the same we can use either location.
2450-
/// When they differ, we need a third location which is distinct from either.
2451-
/// If they share a common scope, use this scope and compare the line/column
2452-
/// pair of the locations with the common scope:
2453-
/// * if both match, keep the line and column;
2454-
/// * if only the line number matches, keep the line and set the column as 0;
2455-
/// * otherwise set line and column as 0.
2456-
/// If they do not share a common scope the location is ambiguous and can't be
2457-
/// represented in a line entry. In this case, set line and column as 0 and
2458-
/// use the scope of any location.
2459-
///
2460-
/// \p LocA \p LocB: The locations to be merged.
2447+
/// Attempts to merge \p LocA and \p LocB into a single location; see
2448+
/// DebugLoc::getMergedLocation for more details.
2449+
/// NB: When merging the locations of instructions, prefer to use
2450+
/// DebugLoc::getMergedLocation(), as an instruction's DebugLoc may contain
2451+
/// additional metadata that will not be preserved when merging the unwrapped
2452+
/// DILocations.
24612453
LLVM_ABI static DILocation *getMergedLocation(DILocation *LocA,
24622454
DILocation *LocB);
24632455

24642456
/// Try to combine the vector of locations passed as input in a single one.
24652457
/// This function applies getMergedLocation() repeatedly left-to-right.
2458+
/// NB: When merging the locations of instructions, prefer to use
2459+
/// DebugLoc::getMergedLocations(), as an instruction's DebugLoc may contain
2460+
/// additional metadata that will not be preserved when merging the unwrapped
2461+
/// DILocations.
24662462
///
24672463
/// \p Locs: The locations to be merged.
24682464
LLVM_ABI static DILocation *getMergedLocations(ArrayRef<DILocation *> Locs);

llvm/include/llvm/IR/DebugLoc.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ namespace llvm {
142142
static inline DebugLoc getDropped() { return DebugLoc(); }
143143
#endif // LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING
144144

145+
/// When two instructions are combined into a single instruction we also
146+
/// need to combine the original locations into a single location.
147+
/// When the locations are the same we can use either location.
148+
/// When they differ, we need a third location which is distinct from
149+
/// either. If they share a common scope, use this scope and compare the
150+
/// line/column pair of the locations with the common scope:
151+
/// * if both match, keep the line and column;
152+
/// * if only the line number matches, keep the line and set the column as
153+
/// 0;
154+
/// * otherwise set line and column as 0.
155+
/// If they do not share a common scope the location is ambiguous and can't
156+
/// be represented in a line entry. In this case, set line and column as 0
157+
/// and use the scope of any location.
158+
///
159+
/// \p LocA \p LocB: The locations to be merged.
160+
LLVM_ABI static DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB);
161+
162+
/// Try to combine the vector of locations passed as input in a single one.
163+
/// This function applies getMergedLocation() repeatedly left-to-right.
164+
///
165+
/// \p Locs: The locations to be merged.
166+
LLVM_ABI static DebugLoc getMergedLocations(ArrayRef<DebugLoc> Locs);
167+
168+
/// If this DebugLoc is non-empty, returns this DebugLoc; otherwise, selects
169+
/// \p Other.
170+
/// In coverage-tracking builds, this also accounts for whether this or
171+
/// \p Other have an annotative DebugLocKind applied, such that if both are
172+
/// empty but exactly one has an annotation, we prefer that annotated
173+
/// location.
174+
DebugLoc orElse(DebugLoc Other) const {
175+
if (*this)
176+
return *this;
177+
#if LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING
178+
if (Other)
179+
return Other;
180+
if (getKind() != DebugLocKind::Normal)
181+
return *this;
182+
if (Other.getKind() != DebugLocKind::Normal)
183+
return Other;
184+
return *this;
185+
#else
186+
return Other;
187+
#endif // LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING
188+
}
189+
145190
/// Get the underlying \a DILocation.
146191
///
147192
/// \pre !*this or \c isa<DILocation>(getAsMDNode()).

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,19 @@ class FMFSource {
113113
/// Common base class shared among various IRBuilders.
114114
class IRBuilderBase {
115115
/// Pairs of (metadata kind, MDNode *) that should be added to all newly
116-
/// created instructions, like !dbg metadata.
116+
/// created instructions, excluding !dbg metadata, which is stored in the
117+
/// StoredDL field.
117118
SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
119+
/// The DebugLoc that will be applied to instructions inserted by this
120+
/// builder.
121+
DebugLoc StoredDL;
118122

119123
/// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
120124
/// null. If \p MD is null, remove the entry with \p Kind.
121125
void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
126+
assert(Kind != LLVMContext::MD_dbg &&
127+
"MD_dbg metadata must be stored in StoredDL");
128+
122129
if (!MD) {
123130
erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
124131
return KV.first == Kind;
@@ -238,7 +245,9 @@ class IRBuilderBase {
238245

239246
/// Set location information used by debugging information.
240247
void SetCurrentDebugLocation(DebugLoc L) {
241-
AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
248+
// For !dbg metadata attachments, we use DebugLoc instead of the raw MDNode
249+
// to include optional introspection data for use in Debugify.
250+
StoredDL = std::move(L);
242251
}
243252

244253
/// Set nosanitize metadata.
@@ -252,8 +261,12 @@ class IRBuilderBase {
252261
/// not on \p Src will be dropped from MetadataToCopy.
253262
void CollectMetadataToCopy(Instruction *Src,
254263
ArrayRef<unsigned> MetadataKinds) {
255-
for (unsigned K : MetadataKinds)
256-
AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
264+
for (unsigned K : MetadataKinds) {
265+
if (K == LLVMContext::MD_dbg)
266+
SetCurrentDebugLocation(Src->getDebugLoc());
267+
else
268+
AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
269+
}
257270
}
258271

259272
/// Get location information used by debugging information.
@@ -267,6 +280,7 @@ class IRBuilderBase {
267280
void AddMetadataToInst(Instruction *I) const {
268281
for (const auto &KV : MetadataToCopy)
269282
I->setMetadata(KV.first, KV.second);
283+
SetInstDebugLocation(I);
270284
}
271285

272286
/// Get the return type of the current function that we're emitting

llvm/include/llvm/IR/Instruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ class Instruction : public User,
698698
/// applications, thus the N-way merging should be in code path.
699699
/// The DebugLoc attached to this instruction will be overwritten by the
700700
/// merged DebugLoc.
701-
LLVM_ABI void applyMergedLocation(DILocation *LocA, DILocation *LocB);
701+
LLVM_ABI void applyMergedLocation(DebugLoc LocA, DebugLoc LocB);
702702

703703
/// Updates the debug location given that the instruction has been hoisted
704704
/// from a block to a predecessor of that block.

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ void BranchFolder::mergeCommonTails(unsigned commonTailIndex) {
862862
"Reached BB end within common tail");
863863
}
864864
assert(MI.isIdenticalTo(*Pos) && "Expected matching MIIs!");
865-
DL = DILocation::getMergedLocation(DL, Pos->getDebugLoc());
865+
DL = DebugLoc::getMergedLocation(DL, Pos->getDebugLoc());
866866
NextCommonInsts[i] = ++Pos;
867867
}
868868
MI.setDebugLoc(DL);

llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ CSEMIRBuilder::getDominatingInstrForID(FoldingSetNodeID &ID,
5353
} else if (!dominates(MI, CurrPos)) {
5454
// Update the spliced machineinstr's debug location by merging it with the
5555
// debug location of the instruction at the insertion point.
56-
auto *Loc = DILocation::getMergedLocation(getDebugLoc().get(),
57-
MI->getDebugLoc().get());
56+
auto Loc = DebugLoc::getMergedLocation(getDebugLoc(), MI->getDebugLoc());
5857
MI->setDebugLoc(Loc);
5958
CurMBB->splice(CurrPos, CurMBB, MI);
6059
}
@@ -170,7 +169,7 @@ CSEMIRBuilder::generateCopiesIfRequired(ArrayRef<DstOp> DstOps,
170169
if (Observer)
171170
Observer->changingInstr(*MIB);
172171
MIB->setDebugLoc(
173-
DILocation::getMergedLocation(MIB->getDebugLoc(), getDebugLoc()));
172+
DebugLoc::getMergedLocation(MIB->getDebugLoc(), getDebugLoc()));
174173
if (Observer)
175174
Observer->changedInstr(*MIB);
176175
}

llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ bool LoadStoreOpt::doSingleStoreMerge(SmallVectorImpl<GStore *> &Stores) {
370370
// For each store, compute pairwise merged debug locs.
371371
DebugLoc MergedLoc = Stores.front()->getDebugLoc();
372372
for (auto *Store : drop_begin(Stores))
373-
MergedLoc = DILocation::getMergedLocation(MergedLoc, Store->getDebugLoc());
373+
MergedLoc = DebugLoc::getMergedLocation(MergedLoc, Store->getDebugLoc());
374374

375375
Builder.setInstr(*Stores.back());
376376
Builder.setDebugLoc(MergedLoc);

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ MachineBasicBlock::findBranchDebugLoc() {
15741574
DL = TI->getDebugLoc();
15751575
for (++TI ; TI != end() ; ++TI)
15761576
if (TI->isBranch())
1577-
DL = DILocation::getMergedLocation(DL, TI->getDebugLoc());
1577+
DL = DebugLoc::getMergedLocation(DL, TI->getDebugLoc());
15781578
}
15791579
return DL;
15801580
}

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,8 +1611,8 @@ static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo,
16111611
// location to prevent debug-info driven tools from potentially reporting
16121612
// wrong location information.
16131613
if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end())
1614-
MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(),
1615-
InsertPos->getDebugLoc()));
1614+
MI.setDebugLoc(DebugLoc::getMergedLocation(MI.getDebugLoc(),
1615+
InsertPos->getDebugLoc()));
16161616
else
16171617
MI.setDebugLoc(DebugLoc());
16181618

0 commit comments

Comments
 (0)