Skip to content

Commit c9465e4

Browse files
authored
[DebugInfo][RemoveDIs] Assert if we mix PHIs and debug-info (llvm#84054)
A potentially erroneous code construction with the work we've done to remove debug intrinsics, is inserting PHIs into blocks when the position hasn't been "sourced correctly". Specifically, if you have: %foo = PHI #dbg_value %bar = add i32... And plan on inserting a new PHI, you have to use the iterator form of `getFirstNonPHI` or getFirstInsertionPt (or begin()) to acquire an iterator that tells the debug-info maintenance code "this is supposed to be at the start of the block, put it in front of #dbg_value". We can detect call-sites that aren't doing this at runtime, and should do with this assertion. It might invalidate code that's doing something very unexpected, like walking backwards to find a PHI, then going forwards, then inserting: however that's just an inefficient way of calling `getFirstNonPHI`.
1 parent f1aa783 commit c9465e4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

llvm/lib/IR/Instruction.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ void Instruction::insertBefore(BasicBlock &BB,
149149
if (!InsertAtHead) {
150150
DPMarker *SrcMarker = BB.getMarker(InsertPos);
151151
if (SrcMarker && !SrcMarker->empty()) {
152+
// If this assertion fires, the calling code is about to insert a PHI
153+
// after debug-records, which would form a sequence like:
154+
// %0 = PHI
155+
// #dbg_value
156+
// %1 = PHI
157+
// Which is de-normalised and undesired -- hence the assertion. To avoid
158+
// this, you must insert at that position using an iterator, and it must
159+
// be aquired by calling getFirstNonPHIIt / begin or similar methods on
160+
// the block. This will signal to this behind-the-scenes debug-info
161+
// maintenence code that you intend the PHI to be ahead of everything,
162+
// including any debug-info.
163+
assert(!isa<PHINode>(this) && "Inserting PHI after debug-records!");
152164
adoptDbgValues(&BB, InsertPos, false);
153165
}
154166
}

0 commit comments

Comments
 (0)