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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 01:43:45 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}))
----------------
michaelselehov wrote:

Right — `SGPRTotal`/`SGPRGranule`/`SGPRTrapReserve` are always `MCConstantExpr` baked from the subtarget in `createOccupancy`, never symbolic. Moved them ahead of `NumSGPRs`/`NumVGPRs` so all the known-constant operands resolve together under the one assert and the two symbolic operands come last. It reorders the printed `occupancy(...)` form, so the symbolic-expansion tests are updated to match (no numeric change).

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


More information about the llvm-commits mailing list