[llvm] [AMDGPU] Filter candidates of LiveRegOptimizer for profitable cases (PR #124624)

Jeffrey Byrnes via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 7 12:47:37 PST 2025


================
@@ -291,6 +333,9 @@ bool LiveRegOptimizer::optimizeLiveType(
       }
 
       Instruction *UseInst = cast<Instruction>(V);
+      if (!shouldReplaceBasedOnOp(UseInst))
----------------
jrbyrnes wrote:

If we do this check after we do the PHINode handling above, then I think we'll probably end up with corrupt PHI node coercion in some cases.

At this position, we have already added the PHINodes and their incoming values to the list to be coerced, but we block adding their uses if coercion is not profitable. 

Besides, if the check returns false, this just breaks out of the users loop, meaning we may have added some uses but not other uses, resulting in further corruption.

So, we need to move this check to the top. Also, it is not enough to just check the users of the instruction. User can be a PHI node which really just resolves values across control flow -- what we care about is the user of the PHI node since this may be an instruction which truly maps to some hardware instruction -- we should check if that hardware instruction natively supports 8 bit vectors. Similarly for shuffle/insert/extract vectors, we should look through to their 



```
bool IsCoercionProfitable = false;
SmallVector<Instruction *, 4> UserList;

for (User *V : II->users())
	UserList.push_back(cast<Instruction *>(V);

while (!UserList.empty() && !IsCoercionProfitable) {
  Instruction *CandInstruction = UserList.pop_back_val();
  if (isa<PHINode>(CandInstruction)) {
  	for (User *V : CandInstruction->users())
		UserList.push_back(cast<Instruction *>(V);
	if (CandInstruction->getParent() == II->getParent())
		continue;

	// TODO -- look through shufflevector , insert/extract vectorelemt

	IsCoercionProfitable = shouldReplaceBasedOnOp(CandInstruction);
	// Prefer type coercion if we have any proftiable use
	if (IsCoercionProfitable)
		break;
  }
}

if (!IsCoercionProfitable)
	continue;


```



https://github.com/llvm/llvm-project/pull/124624


More information about the llvm-commits mailing list