[llvm-branch-commits] [llvm] [AMDGPU] Allocate and reserve registers for address space 13 objects (PR #214248)

Carl Ritson via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 9 02:47:02 PST 2026


================
@@ -446,6 +512,96 @@ bool AMDGPUPromoteAllocaImpl::run(Function &F, bool PromoteToLDS) {
   return Changed;
 }
 
+// Give an alloca in the VGPR ("as memory") address space a place there,
+// recorded as !amdgpu.allocated.vgprs, plus lifetime markers the backend
+// understands. AMDGPUPrivateObjectVGPRs turns those into defs and uses of the
+// physical registers, which keeps register allocation off them.
+void AMDGPUPromoteAllocaImpl::allocateVgprs(AllocaAnalysis &AA) {
+  LLVMContext &Ctx = Mod.getContext();
+  const unsigned AllocaSize =
+      DL.getTypeAllocSize(AA.Alloca->getAllocatedType()).getFixedValue();
+
+  // The generic lifetime intrinsics do not survive into the backend, so use
+  // the address-space specific ones. An object with no start is live from its
+  // definition.
+  //
+  // An already-converted marker counts as a start: this runs over IR it has
+  // converted before, and a second start would move the live range back to
+  // the alloca and keep the object live from there.
+  bool HaveLifetimeStart = false;
+  for (Use &U : AA.Alloca->uses()) {
+    auto *II = dyn_cast<IntrinsicInst>(U.getUser());
+    if (!II)
+      continue;
+    switch (II->getIntrinsicID()) {
+    case Intrinsic::lifetime_start:
+      II->setCalledFunction(Intrinsic::getOrInsertDeclaration(
+          &Mod, Intrinsic::amdgcn_vgpr_lifetime_start, AA.Alloca->getType()));
+      HaveLifetimeStart = true;
+      break;
+    case Intrinsic::lifetime_end:
+      II->setCalledFunction(Intrinsic::getOrInsertDeclaration(
+          &Mod, Intrinsic::amdgcn_vgpr_lifetime_end, AA.Alloca->getType()));
+      break;
+    case Intrinsic::amdgcn_vgpr_lifetime_start:
+      HaveLifetimeStart = true;
+      break;
+    default:
+      break;
+    }
+  }
+
+  if (!HaveLifetimeStart) {
+    Instruction *IP = AA.Alloca->getNextNode();
+    while (isa<AllocaInst>(IP))
+      IP = IP->getNextNode();
+    IRBuilder<> B(IP);
+    B.SetCurrentDebugLocation(AA.Alloca->getDebugLoc());
+    B.CreateIntrinsic(B.getVoidTy(), Intrinsic::amdgcn_vgpr_lifetime_start,
+                      AA.Alloca);
+  }
+
+  // An object that already has its place keeps it, or a second allocation
+  // would land on top of it. The markers above are still rewritten, since IR
+  // carrying the metadata need not carry them.
+  unsigned Address = AllocVGPROffset;
+  if (MDNode *MD = AA.Alloca->getMetadata("amdgpu.allocated.vgprs")) {
+    Address = cast<AMDGPU::AllocatedVGPRsMetadata>(MD)->getAddress();
+    AllocVGPROffset =
+        std::max(AllocVGPROffset, Address + alignTo(AllocaSize, 4));
+  } else {
+    // Find space for the alloca. Objects are packed in order at dword
+    // granularity, since a register cannot be shared between two of them.
+    Type *I32 = Type::getInt32Ty(Ctx);
+    AA.Alloca->setMetadata(
+        "amdgpu.allocated.vgprs",
+        MDNode::get(
+            Ctx, {ConstantAsMetadata::get(ConstantInt::get(I32, Address)),
+                  ConstantAsMetadata::get(ConstantInt::get(I32, AllocaSize))}));
+    AllocVGPROffset += alignTo(AllocaSize, 4);
+  }
+
+  // Nothing may move or spill the object, so it has to fit the register
+  // budget this function is entitled to - for a non-entry function, the 32
----------------
perlfu wrote:

32 seems oddly specific.

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


More information about the llvm-branch-commits mailing list