Skip to content

Commit

Permalink
[DebugInfo][RemoveDIs] Make getDbgValueRange inlineable (#79331)
Browse files Browse the repository at this point in the history
`getDbgValueRange` is the replacement of a common LLVM idiom of:
  1) Am I currently looking at a `DbgVariableIntrinsic` instruction?
  2) Let's do something special with it!

We instead iterate over the range of DPValues attached to an instruction
and do special things with those. Unfortunately in the common case of
"there is no debug-info", this generates a spurious function call that's
paid by non-debug builds.

To get around this, make `getDbgValueRange` inlineable so that the "`if
(DbgMarker)`" test can be inlined and guard the more expensive call. The
false path should be optimisable-awayable to skipping the loop. However,
due to header inclusion order we can't just make
`Instruction::getDbgValueRange` inline because `DPMarker` hasn't been
declared yet. So, define an inlinable function in the llvm:: namespace
and pre-declare it -- the eventual code should be inlineable almost 100%
of the time.
  • Loading branch information
jmorse committed Jan 25, 2024
1 parent 3490f03 commit 47a2e73
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
11 changes: 11 additions & 0 deletions llvm/include/llvm/IR/DebugProgramInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ inline raw_ostream &operator<<(raw_ostream &OS, const DPValue &Value) {
return OS;
}

/// Inline helper to return a range of DPValues attached to a marker. It needs
/// to be inlined as it's frequently called, but also come after the declaration
/// of DPMarker. Thus: it's pre-declared by users like Instruction, then an
/// inlineable body defined here.
inline iterator_range<simple_ilist<DPValue>::iterator>
getDbgValueRange(DPMarker *DbgMarker) {
if (!DbgMarker)
return DPMarker::getEmptyDPValueRange();
return DbgMarker->getDbgValueRange();
}

} // namespace llvm

#endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
6 changes: 5 additions & 1 deletion llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ template <> struct ilist_alloc_traits<Instruction> {
static inline void deleteNode(Instruction *V);
};

iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange(DPMarker *);

class Instruction : public User,
public ilist_node_with_parent<Instruction, BasicBlock,
ilist_iterator_bits<true>> {
Expand Down Expand Up @@ -76,7 +78,9 @@ class Instruction : public User,
bool InsertAtHead = false);

/// Return a range over the DPValues attached to this instruction.
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange() const;
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange() const {
return llvm::getDbgValueRange(DbgMarker);
}

/// Return an iterator to the position of the "Next" DPValue after this
/// instruction, or std::nullopt. This is the position to pass to
Expand Down
12 changes: 0 additions & 12 deletions llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,6 @@ Instruction::cloneDebugInfoFrom(const Instruction *From,
return DbgMarker->cloneDebugInfoFrom(From->DbgMarker, FromHere, InsertAtHead);
}

iterator_range<DPValue::self_iterator>
Instruction::getDbgValueRange() const {
BasicBlock *Parent = const_cast<BasicBlock *>(getParent());
assert(Parent && "Instruction must be inserted to have DPValues");
(void)Parent;

if (!DbgMarker)
return DPMarker::getEmptyDPValueRange();

return DbgMarker->getDbgValueRange();
}

std::optional<DPValue::self_iterator> Instruction::getDbgReinsertionPosition() {
// Is there a marker on the next instruction?
DPMarker *NextMarker = getParent()->getNextMarker(this);
Expand Down

0 comments on commit 47a2e73

Please sign in to comment.