================ @@ -0,0 +1,352 @@ +//===- AMDGPUPrivateObjectVGPRs.cpp - Private object VGPRs ----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +/// \file +/// Mark the physical VGPRs an object in the VGPR ("as memory") address space +/// was allocated to as used, by giving the VGPR_LIFETIME_{START,END} pseudos +/// implicit operands and adding the registers to block live-ins, so register +/// allocation cannot hand them to anything else while the object is live. +/// +/// AMDGPUPromoteAlloca records the allocation on the alloca as +/// !amdgpu.allocated.vgprs; it reaches this pass through the markers' MMO. +// +//===----------------------------------------------------------------------===// + +#include "AMDGPU.h" +#include "AMDGPUMachineInstrs.h" +#include "AMDGPUMemoryUtils.h" +#include "GCNSubtarget.h" +#include "llvm/ADT/PostOrderIterator.h" +#include "llvm/ADT/Sequence.h" +#include "llvm/ADT/SmallBitVector.h" +#include "llvm/CodeGen/LiveIntervals.h" +#include "llvm/CodeGen/LiveVariables.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/SlotIndexes.h" +#include "llvm/IR/DiagnosticInfo.h" +#include "llvm/IR/Instructions.h" +#include "llvm/InitializePasses.h" +#include "llvm/Support/MathExtras.h" + +using namespace llvm; + +#define DEBUG_TYPE "amdgpu-private-object-vgprs" + +using ObjectRegs = SmallVector<MCPhysReg, 50>; + +namespace { + +class AMDGPUPrivateObjectVGPRsImpl { +public: + AMDGPUPrivateObjectVGPRsImpl(const MachineFunction &MF, LiveIntervals *LIS) + : TII(MF.getSubtarget<GCNSubtarget>().getInstrInfo()), LIS(LIS) {} + + bool run(MachineFunction &MF); + +private: + struct AllocaBBInfo { + const AllocaInst *Alloca = nullptr; + bool LiveIn = false; + bool Starts = false; + bool Ends = false; + }; + + ObjectRegs computeObjectRegs(const AllocaInst &Alloca) const; + + const SIInstrInfo *TII; + LiveIntervals *LIS; + + DenseMap<const AllocaInst *, std::pair<ObjectRegs, MachineMemOperand *>> + AllocaObjectRegs; +}; + +} // end anonymous namespace + +// The registers an object occupies: one per dword, starting at the register its +// byte address in the address space names. +ObjectRegs AMDGPUPrivateObjectVGPRsImpl::computeObjectRegs( + const AllocaInst &Alloca) const { + const auto &MD = AMDGPU::AllocatedVGPRsMetadata::get(Alloca); + unsigned Offset = MD.getAddress(); + assert(Offset % 4 == 0 && "Object is not dword aligned"); + + // An object that does not fill its last register still owns the whole of it. + unsigned BaseRegIdx = Offset / 4; + unsigned NumRegs = divideCeil(MD.getSize(), 4); + + ObjectRegs Regs; + Regs.reserve(NumRegs); + for (unsigned I : seq(NumRegs)) + Regs.push_back(AMDGPU::VGPR_32RegClass.getRegister(BaseRegIdx + I)); + + return Regs; +} + +bool AMDGPUPrivateObjectVGPRsImpl::run(MachineFunction &MF) { + // Reverse post-order, for the live-out/live-in propagation below. + DenseMap<MachineBasicBlock *, unsigned> BlockToIndex; + SmallVector<MachineBasicBlock *> IndexToBlock; + ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin()); + for (MachineBasicBlock *MBB : RPOT) { + BlockToIndex[MBB] = IndexToBlock.size(); + IndexToBlock.push_back(MBB); + } + + // Fixed-point iteration for block live-ins. The first pass also scans + // instructions, augmenting the markers and recording per-block state. + SmallVector<SmallVector<AllocaBBInfo>> BBInfos(IndexToBlock.size()); + SmallBitVector Worklist(IndexToBlock.size()); + bool Changed = false; + + for (bool Dirty = true, FirstPass = true; Dirty; FirstPass = false) { + Dirty = false; ---------------- arsenm wrote:
I can't follow this https://github.com/llvm/llvm-project/pull/214248 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
