Skip to content

Commit dad1cfc

Browse files
authored
PIX: Don't seek beyond terminator instructions (value-to-declare pass) (#3855) (#3856)
Background: this pass is trying to find all dbg.value and replace them with dbg.declare. In the code being changed, the pass is trying to seek a valid location at which to insert the dbg.declare. It has to come after the value to which it applies (which isn't true of the dbg.value). So there's this little loop trying to move forward to find the right instruction before which to insert new stuff. I was expecting getNextNode to return null when there is no next node. When called on a terminator, it actually returns a non-null but malformed instruction pointer. So we have to explicitly check for terminators in this loop. This really short basic block tripped up the pass: ; <label>:274 ; preds = %.lr.ph55 %RawBufferLoad = call %dx.types.ResRet.i32 @dx.op.rawBufferLoad.i32(i32 139, %dx.types.Handle %lightBuffer_texture_structbuf, i32 %lightIndex.0, i32 28, i8 1, i32 4), !dbg !384 %275 = extractvalue %dx.types.ResRet.i32 %RawBufferLoad, 0, !dbg !384 switch i32 %275, label %288 [ i32 0, label %276 i32 1, label %280 i32 2, label %284 ], !dbg !397 I think the pass could be smarter about seeking the right insertion point for the dbg.declare. It's currently assuming that the dbg.value always succeeds the value to which it refers, but as in this case, that's not always true. But that's a project for another day. (cherry picked from commit 650de80)
1 parent e48d979 commit dad1cfc

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

lib/DxilPIXPasses/DxilDbgValueToDbgDeclare.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -704,40 +704,44 @@ void DxilDbgValueToDbgDeclare::handleDbgValue(
704704
auto* instruction = llvm::dyn_cast<llvm::Instruction>(V);
705705
if (instruction != nullptr) {
706706
instruction = instruction->getNextNode();
707-
if (instruction != nullptr) {
707+
if (instruction != nullptr && !llvm::isa<TerminatorInst>(instruction)) {
708708
// Drivers may crash if phi nodes aren't always at the top of a block,
709709
// so we must skip over them before inserting instructions.
710710
do {
711711
instruction = instruction->getNextNode();
712-
} while (instruction != nullptr && llvm::isa<llvm::PHINode>(instruction));
712+
} while (instruction != nullptr &&
713+
llvm::isa<llvm::PHINode>(instruction) &&
714+
!llvm::isa<TerminatorInst>(instruction));
713715

714-
B.SetInsertPoint(instruction);
715-
716-
B.SetCurrentDebugLocation(llvm::DebugLoc());
717-
auto *Zero = B.getInt32(0);
718-
719-
// Now traverse a list of pairs {Scalar Value, InitialOffset + Offset}.
720-
// InitialOffset is the offset from DbgValue's expression (i.e., the
721-
// offset from the Variable's start), and Offset is the Scalar Value's
722-
// packed offset from DbgValue's value.
723-
for (const ValueAndOffset &VO : SplitValue(V, InitialOffset, B)) {
724-
725-
OffsetInBits AlignedOffset;
726-
if (!Offsets.GetAlignedOffsetFromPackedOffset(VO.m_PackedOffset,
727-
&AlignedOffset)) {
728-
continue;
729-
}
730-
731-
auto *AllocaInst = Register->GetRegisterForAlignedOffset(AlignedOffset);
732-
if (AllocaInst == nullptr) {
733-
assert(!"Failed to find alloca for var[offset]");
734-
continue;
735-
}
736-
737-
if (AllocaInst->getAllocatedType()->getArrayElementType() ==
738-
VO.m_V->getType()) {
739-
auto *GEP = B.CreateGEP(AllocaInst, {Zero, Zero});
740-
B.CreateStore(VO.m_V, GEP);
716+
if(instruction != nullptr && !llvm::isa<TerminatorInst>(instruction)) {
717+
B.SetInsertPoint(instruction);
718+
719+
B.SetCurrentDebugLocation(llvm::DebugLoc());
720+
auto *Zero = B.getInt32(0);
721+
722+
// Now traverse a list of pairs {Scalar Value, InitialOffset + Offset}.
723+
// InitialOffset is the offset from DbgValue's expression (i.e., the
724+
// offset from the Variable's start), and Offset is the Scalar Value's
725+
// packed offset from DbgValue's value.
726+
for (const ValueAndOffset &VO : SplitValue(V, InitialOffset, B)) {
727+
728+
OffsetInBits AlignedOffset;
729+
if (!Offsets.GetAlignedOffsetFromPackedOffset(VO.m_PackedOffset,
730+
&AlignedOffset)) {
731+
continue;
732+
}
733+
734+
auto *AllocaInst = Register->GetRegisterForAlignedOffset(AlignedOffset);
735+
if (AllocaInst == nullptr) {
736+
assert(!"Failed to find alloca for var[offset]");
737+
continue;
738+
}
739+
740+
if (AllocaInst->getAllocatedType()->getArrayElementType() ==
741+
VO.m_V->getType()) {
742+
auto *GEP = B.CreateGEP(AllocaInst, {Zero, Zero});
743+
B.CreateStore(VO.m_V, GEP);
744+
}
741745
}
742746
}
743747
}

0 commit comments

Comments
 (0)