[llvm] [DebugInfo][RemoveDIs] Add a DPValue implementation for instcombine sinking (PR #77930)
Stephen Tozer via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 23 10:13:06 PST 2024
================
@@ -4266,19 +4285,140 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
// Perform salvaging without the clones, then sink the clones.
if (!DIIClones.empty()) {
- // RemoveDIs: pass in empty vector of DPValues until we get to instrumenting
- // this pass.
- SmallVector<DPValue *, 1> DummyDPValues;
- salvageDebugInfoForDbgValues(*I, DbgUsersToSalvage, DummyDPValues);
+ salvageDebugInfoForDbgValues(*I, DbgUsersToSalvage, {});
// The clones are in reverse order of original appearance, reverse again to
// maintain the original order.
for (auto &DIIClone : llvm::reverse(DIIClones)) {
DIIClone->insertBefore(&*InsertPos);
LLVM_DEBUG(dbgs() << "SINK: " << *DIIClone << '\n');
}
}
+}
- return true;
+void InstCombinerImpl::tryToSinkInstructionDPValues(
+ Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
+ BasicBlock *DestBlock, SmallVectorImpl<DPValue *> &DPValues) {
+ // Implementation of tryToSinkInstructionDbgValues, but for the DPValue of
+ // variable assignments rather than dbg.values.
+
+ // Fetch all DPValues not already in the destination.
+ SmallVector<DPValue *, 2> DPValuesToSalvage;
+ for (auto &DPV : DPValues)
+ if (DPV->getParent() != DestBlock)
+ DPValuesToSalvage.push_back(DPV);
+
+ // Fetch a second collection, of DPValues in the source block that we're going
+ // to sink.
+ SmallVector<DPValue *> DPValuesToSink;
+ for (DPValue *DPV : DPValuesToSalvage)
+ if (DPV->getParent() == SrcBlock)
+ DPValuesToSink.push_back(DPV);
+
+ // Sort DPValues according to their position in the block. This is a partial
+ // order: DPValues attached to different instructions will be ordered by the
+ // instruction order, but DPValues attached to the same instruction won't
+ // have an order.
+ auto Order = [](DPValue *A, DPValue *B) -> bool {
+ return B->getInstruction()->comesBefore(A->getInstruction());
+ };
+ llvm::stable_sort(DPValuesToSink, Order);
+
+ // If there are two assignments to the same variable attached to the same
+ // instruction, the ordering between the two assignments is important. Scan
+ // for this (rare) case and establish which is the last assignment.
+ using InstVarPair = std::pair<const Instruction *, DebugVariable>;
+ SmallDenseMap<InstVarPair, DPValue *> FilterOutMap;
+ if (DPValuesToSink.size() > 1) {
+ SmallDenseMap<InstVarPair, unsigned> CountMap;
+ // Count how many assignments to each variable there is per instruction.
+ for (DPValue *DPV : DPValuesToSink) {
+ DebugVariable DbgUserVariable =
+ DebugVariable(DPV->getVariable(), DPV->getExpression(),
+ DPV->getDebugLoc()->getInlinedAt());
+ CountMap[std::make_pair(DPV->getInstruction(), DbgUserVariable)] += 1;
+ }
+
+ // If there are any instructions with two assignments, add them to the
+ // FilterOutMap to record that they need extra filtering.
+ SmallPtrSet<const Instruction *, 4> DupSet;
+ for (auto It : CountMap) {
+ if (It.second > 1) {
+ FilterOutMap[It.first] = nullptr;
+ DupSet.insert(It.first.first);
+ }
+ }
+
+ // For all instruction/variable pairs needing extra filtering, find the
+ // latest assignment.
+ for (const Instruction *Inst : DupSet) {
+ for (DPValue &DPV : llvm::reverse(Inst->getDbgValueRange())) {
+ DebugVariable DbgUserVariable =
+ DebugVariable(DPV.getVariable(), DPV.getExpression(),
+ DPV.getDebugLoc()->getInlinedAt());
+ auto FilterIt =
+ FilterOutMap.find(std::make_pair(Inst, DbgUserVariable));
+ if (FilterIt == FilterOutMap.end())
+ continue;
+ if (FilterIt->second != nullptr)
+ continue;
+ FilterIt->second = &DPV;
+ }
+ }
+ }
+
+ // Perform cloning of the DPValues that we plan on sinking, filter out any
+ // duplicate assignments identified above.
+ SmallVector<DPValue *, 2> DPVClones;
+ SmallSet<DebugVariable, 4> SunkVariables;
+ for (DPValue *DPV : DPValuesToSink) {
+ if (DPV->Type == DPValue::LocationType::Declare)
+ continue;
+
+ DebugVariable DbgUserVariable =
+ DebugVariable(DPV->getVariable(), DPV->getExpression(),
+ DPV->getDebugLoc()->getInlinedAt());
+
+ // For any variable where there were multiple assignments in the same place,
+ // ignore all but the last assignment.
+ if (!FilterOutMap.empty()) {
+ InstVarPair IVP = std::make_pair(DPV->getInstruction(), DbgUserVariable);
+ auto It = FilterOutMap.find(IVP);
+
+ // Filter out.
+ if (It != FilterOutMap.end() && It->second != DPV)
+ continue;
+ }
+
+ if (!SunkVariables.insert(DbgUserVariable).second)
+ continue;
+
+ // FIXME: dbg.assign equivalence. dbg.assigns here enter the SunkVariables
+ // map, but don't actually get sunk.
----------------
SLTozer wrote:
This can be implemented now, `if (DPV->isDbgAssign()) continue;`
https://github.com/llvm/llvm-project/pull/77930
More information about the llvm-commits
mailing list