[llvm] [AMDGPU] Make SGPR occupancy the exact inverse of getMaxNumSGPRs (PR #201342)

Janek van Oirschot via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 21:51:50 PDT 2026


================
@@ -166,24 +192,29 @@ bool AMDGPUMCExpr::evaluateAlignTo(MCValue &Res, const MCAssembler *Asm) const {
 
 bool AMDGPUMCExpr::evaluateOccupancy(MCValue &Res,
                                      const MCAssembler *Asm) const {
-  uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, Generation,
-      NumSGPRs, NumVGPRs;
+  uint64_t InitOccupancy, MaxWaves, Granule, TargetTotalNumVGPRs, NumSGPRs,
+      NumVGPRs, SGPRTotal, SGPRGranule, SGPRTrapReserve;
 
-  bool Success = evaluateMCExprs(
-      Args.slice(0, 5), Asm,
-      {MaxWaves, Granule, TargetTotalNumVGPRs, Generation, InitOccupancy});
+  bool Success =
+      evaluateMCExprs(Args.slice(0, 4), Asm,
+                      {MaxWaves, Granule, TargetTotalNumVGPRs, InitOccupancy});
 
-  assert(Success && "Arguments 1 to 5 for Occupancy should be known constants");
+  assert(Success && "Arguments 1 to 4 for Occupancy should be known constants");
+
+  if (!Success || !evaluateMCExprs(Args.slice(4, 2), Asm, {NumSGPRs, NumVGPRs}))
+    return false;
 
-  if (!Success || !evaluateMCExprs(Args.slice(5, 2), Asm, {NumSGPRs, NumVGPRs}))
+  // Trailing operands carry the codegen SGPR budget (total, granule, trap
+  // reserve) so the SGPR-limited occupancy matches getMaxNumSGPRs().
+  if (!evaluateMCExprs(Args.slice(6, 3), Asm,
+                       {SGPRTotal, SGPRGranule, SGPRTrapReserve}))
----------------
JanekvO wrote:

AFAIU, these are supposed to be known constants at this point, right? They won't turn into symbolic MCExpr (or some arithmetic thereof)? If so, can probably move them in front of `NumSGPRs, NumVGPRs` in the arg list and handle resolving as part of `MaxWaves, Granule, TargetTotalNumVGPRs, InitOccupancy` as known constants.

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


More information about the llvm-commits mailing list