Skip to content

Commit 2987760

Browse files
dlei6gigcbot
authored andcommitted
Mark more phis as uniform with undefs
Mark more phis as uniform with undefs
1 parent f37e8e9 commit 2987760

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

IGC/Compiler/CISACodeGen/WIAnalysis.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,49 @@ bool WIAnalysisRunner::isWaveBroadcastIndex(const llvm::Instruction* inst)
930930
return false;
931931
}
932932

933+
// Checks all incoming values of PHI for a unique value, including PHI nodes that
934+
// loop back on itself. We ignore undef values as not counting towards a unique incoming value.
935+
static bool hasUniqueIncoming(llvm::PHINode* PN, Value*& UniqueVal, std::set<PHINode*>& phiLoop)
936+
{
937+
UniqueVal = nullptr;
938+
phiLoop.insert(PN);
939+
940+
for (Value* V : PN->incoming_values())
941+
{
942+
if (isa<UndefValue>(V))
943+
continue;
944+
945+
if (auto* phi = dyn_cast<PHINode>(V))
946+
{
947+
// Ignore previously seen PHIs
948+
if (phiLoop.count(phi) > 0)
949+
continue;
950+
951+
Value* temp;
952+
if (hasUniqueIncoming(phi, temp, phiLoop))
953+
{
954+
V = temp;
955+
}
956+
}
957+
958+
if (!UniqueVal)
959+
UniqueVal = V;
960+
else if (UniqueVal != V)
961+
{
962+
UniqueVal = nullptr;
963+
return false;
964+
}
965+
}
966+
return true;
967+
}
968+
969+
bool WIAnalysisRunner::hasUniqueNonUndef(llvm::PHINode* PN)
970+
{
971+
Value* temp;
972+
std::set<PHINode*> phiLoop;
973+
return hasUniqueIncoming(PN, temp, phiLoop);
974+
}
975+
933976
void WIAnalysisRunner::update_cf_dep(const IGCLLVM::TerminatorInst* inst)
934977
{
935978
IGC_ASSERT(hasDependency(inst));
@@ -1098,14 +1141,23 @@ void WIAnalysisRunner::updatePHIDepAtJoin(BasicBlock* blk, BranchInfo* brInfo)
10981141
continue;
10991142
}
11001143
Value* trickySrc = nullptr;
1144+
bool isUnique = hasUniqueNonUndef(phi);
11011145
for (unsigned predIdx = 0; predIdx < phi->getNumOperands(); ++predIdx)
11021146
{
11031147
Value* srcVal = phi->getOperand(predIdx);
11041148
Instruction* defi = dyn_cast<Instruction>(srcVal);
11051149
if (defi && brInfo->influence_region.count(defi->getParent()))
11061150
{
1107-
updateDepMap(phi, brDep);
1108-
break;
1151+
// If we have a phi join that looks like this:
1152+
// %v = phi i32 [ %x, %if ], [ undef, %else ]
1153+
//
1154+
// Then if %x is uniform, %v isn't really non-uniform because
1155+
// there will be no phi-mov associated with the undef.
1156+
if (!isUnique)
1157+
{
1158+
updateDepMap(phi, brDep);
1159+
break;
1160+
}
11091161
}
11101162
else
11111163
{
@@ -1120,7 +1172,7 @@ void WIAnalysisRunner::updatePHIDepAtJoin(BasicBlock* blk, BranchInfo* brInfo)
11201172
{
11211173
trickySrc = srcVal;
11221174
}
1123-
else if (trickySrc != srcVal)
1175+
else if (trickySrc != srcVal && !isUnique)
11241176
{
11251177
updateDepMap(phi, brDep);
11261178
break;

IGC/Compiler/CISACodeGen/WIAnalysis.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ namespace IGC
245245
bool& IsLyUniform,
246246
bool& IsLzUniform);
247247

248+
/// @brief return true if the phi has at most one incoming value that is
249+
/// not an undef value.
250+
static bool hasUniqueNonUndef(llvm::PHINode* PN);
251+
248252
private:
249253

250254
/// The WIAnalysis follows pointer arithmetic

0 commit comments

Comments
 (0)