Skip to content

Commit dff87fb

Browse files
ViacheslavRbsys_zuul
authored andcommitted
Code-sinking optimization ignores debug instructions.
As result debug information could become inconsistent, but some following analyzes take into account debug instructions and due to the inconsistency they produce incorrect results. The fix resolves the issue, the optimization move debug instructions after defining instructions. Change-Id: Iba1017854e9f2f6325b18a8c28d842b42f3bb2b0
1 parent 6269086 commit dff87fb

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

IGC/Compiler/CISACodeGen/CodeSinking.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ namespace IGC {
432432
}
433433
}
434434
}
435+
if (madeChange) {
436+
ProcessDbgValueInst(blk);
437+
}
435438

436439
return madeChange;
437440
}
@@ -721,6 +724,9 @@ namespace IGC {
721724
}
722725
}
723726
}
727+
if (madeChange) {
728+
ProcessDbgValueInst(*blk);
729+
}
724730
return madeChange;
725731
}
726732

@@ -1104,6 +1110,10 @@ namespace IGC {
11041110

11051111
bool t = LoopSinkInstructions(sinkCandidates, L);
11061112
changed |= t;
1113+
1114+
if (changed) {
1115+
ProcessDbgValueInst(*Preheader);
1116+
}
11071117
}
11081118

11091119
// Invoke LocalSink() to move def to its first use
@@ -1298,5 +1308,40 @@ namespace IGC {
12981308
return changed;
12991309
}
13001310

1311+
// Move referenced DbgValueInst intrinsics calls after defining instructions
1312+
// it is requared for correct work of LiveVariables analysis and other
1313+
void CodeSinking::ProcessDbgValueInst(BasicBlock& blk)
1314+
{
1315+
if (!CTX->m_instrTypes.hasDebugInfo)
1316+
{
1317+
return;
1318+
}
1319+
1320+
BasicBlock::iterator I = blk.end();
1321+
--I;
1322+
bool processedBegin = false;
1323+
do {
1324+
Instruction* inst = cast<Instruction>(I);
1325+
processedBegin = (I == blk.begin());
1326+
if (!processedBegin)
1327+
--I;
1328+
1329+
if (auto* DVI = dyn_cast<DbgValueInst>(inst))
1330+
{
1331+
if (auto* def = dyn_cast<Instruction>(DVI->getValue()))
1332+
{
1333+
if (!DT->dominates(def, inst))
1334+
{
1335+
auto* instClone = inst->clone();
1336+
instClone->insertAfter(def);
1337+
Value* undef = UndefValue::get(def->getType());
1338+
MetadataAsValue* MAV = MetadataAsValue::get(inst->getContext(), ValueAsMetadata::get(undef));
1339+
cast<CallInst>(inst)->setArgOperand(0, MAV);
1340+
}
1341+
}
1342+
}
1343+
} while (!processedBegin);
1344+
}
1345+
13011346
char CodeSinking::ID = 0;
13021347
}

IGC/Compiler/CISACodeGen/CodeSinking.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ namespace IGC {
151151
bool canLoopSink(llvm::Instruction* I, llvm::Loop* L, llvm::BasicBlock* BB);
152152
bool LoopSinkInstructions(
153153
llvm::SmallVector<llvm::Instruction*, 64> sinkCandidates, llvm::Loop* L);
154+
155+
// Move referencing DbgValueInst intrinsics calls after defining instructions
156+
void ProcessDbgValueInst(llvm::BasicBlock& blk);
157+
154158
};
155159

156160
}

0 commit comments

Comments
 (0)