@@ -2841,6 +2841,95 @@ void SITargetLowering::insertCopiesSplitCSR(
28412841 }
28422842}
28432843
2844+ /// Classes for spilling inreg VGPR arguments.
2845+ ///
2846+ /// When an argument marked inreg is pushed to a VGPR, it indicates that the
2847+ /// available SGPRs for argument passing have been exhausted. In such cases, it
2848+ /// is preferable to pack multiple inreg arguments into individual lanes of
2849+ /// VGPRs instead of assigning each directly to separate VGPRs.
2850+ ///
2851+ /// Spilling involves two parts: the caller-side (call site) and the
2852+ /// callee-side. Both must follow the same method for selecting registers and
2853+ /// lanes, ensuring that an argument written at the call site matches exactly
2854+ /// with the one read at the callee.
2855+
2856+ /// The spilling class for the caller-side that lowers packing of call site
2857+ /// arguments.
2858+ class InregVPGRSpillerCallee {
2859+ CCState &State;
2860+ SelectionDAG &DAG;
2861+ MachineFunction &MF;
2862+
2863+ Register SrcReg;
2864+ SDValue SrcVal;
2865+ unsigned CurLane = 0;
2866+
2867+ public:
2868+ InregVPGRSpillerCallee(SelectionDAG &DAG, MachineFunction &MF, CCState &State)
2869+ : State(State), DAG(DAG), MF(MF) {}
2870+
2871+ SDValue readLane(SDValue Chain, const SDLoc &SL, Register &Reg, EVT VT) {
2872+ if (SrcVal) {
2873+ State.DeallocateReg(Reg);
2874+ } else {
2875+ Reg = MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
2876+ SrcReg = Reg;
2877+ SrcVal = DAG.getCopyFromReg(Chain, SL, Reg, VT);
2878+ }
2879+ // According to the calling convention, only SGPR4–SGPR29 should be used for
2880+ // passing 'inreg' function arguments. Therefore, the number of 'inreg' VGPR
2881+ // arguments must not exceed 26.
2882+ assert(CurLane < 26 && "more than expected VGPR inreg arguments");
2883+ SmallVector<SDValue, 4> Operands{
2884+ DAG.getTargetConstant(Intrinsic::amdgcn_readlane, SL, MVT::i32),
2885+ DAG.getRegister(SrcReg, VT),
2886+ DAG.getTargetConstant(CurLane++, SL, MVT::i32)};
2887+ return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, VT, Operands);
2888+ }
2889+ };
2890+
2891+ /// The spilling class for the caller-side that lowers packing of call site
2892+ /// arguments.
2893+ class InregVPGRSpillerCallSite {
2894+ Register DstReg;
2895+ SDValue LastWrite;
2896+ unsigned CurLane = 0;
2897+
2898+ SelectionDAG &DAG;
2899+ MachineFunction &MF;
2900+
2901+ public:
2902+ InregVPGRSpillerCallSite(SelectionDAG &DAG, MachineFunction &MF)
2903+ : DAG(DAG), MF(MF) {}
2904+
2905+ void writeLane(const SDLoc &SL, Register &Reg, SDValue Val, EVT VT) {
2906+ if (DstReg.isValid())
2907+ Reg = DstReg;
2908+ else
2909+ DstReg = Reg;
2910+ // According to the calling convention, only SGPR4–SGPR29 should be used for
2911+ // passing 'inreg' function arguments. Therefore, the number of 'inreg' VGPR
2912+ // arguments must not exceed 26.
2913+ assert(CurLane < 26 && "more than expected VGPR inreg arguments");
2914+ SmallVector<SDValue, 4> Operands{
2915+ DAG.getTargetConstant(Intrinsic::amdgcn_writelane, SL, MVT::i32), Val,
2916+ DAG.getTargetConstant(CurLane++, SL, MVT::i32)};
2917+ if (!LastWrite) {
2918+ Register VReg = MF.getRegInfo().getLiveInVirtReg(DstReg);
2919+ Operands.push_back(DAG.getRegister(VReg, VT));
2920+ } else {
2921+ Operands.push_back(LastWrite);
2922+ }
2923+ LastWrite = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, VT, Operands);
2924+ }
2925+
2926+ SDValue finalize(SDValue Chain, const SDLoc &SL, SDValue InGlue) {
2927+ if (!LastWrite)
2928+ return LastWrite;
2929+ return DAG.getCopyToReg(Chain, SL, DstReg, LastWrite, InGlue);
2930+ }
2931+ };
2932+
28442933SDValue SITargetLowering::LowerFormalArguments(
28452934 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
28462935 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
@@ -2963,6 +3052,7 @@ SDValue SITargetLowering::LowerFormalArguments(
29633052 // FIXME: Alignment of explicit arguments totally broken with non-0 explicit
29643053 // kern arg offset.
29653054 const Align KernelArgBaseAlign = Align(16);
3055+ InregVPGRSpillerCallee Spiller(DAG, MF, CCInfo);
29663056
29673057 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
29683058 const ISD::InputArg &Arg = Ins[i];
@@ -3130,8 +3220,17 @@ SDValue SITargetLowering::LowerFormalArguments(
31303220 llvm_unreachable("Unexpected register class in LowerFormalArguments!");
31313221 EVT ValVT = VA.getValVT();
31323222
3133- Reg = MF.addLiveIn(Reg, RC);
3134- SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3223+ SDValue Val;
3224+ // If an argument is marked inreg but gets pushed to a VGPR, it indicates
3225+ // we've run out of SGPRs for argument passing. In such cases, we'd prefer
3226+ // to start packing inreg arguments into individual lanes of VGPRs, rather
3227+ // than placing them directly into VGPRs.
3228+ if (RC == &AMDGPU::VGPR_32RegClass && Arg.Flags.isInReg()) {
3229+ Val = Spiller.readLane(Chain, DL, Reg, VT);
3230+ } else {
3231+ Reg = MF.addLiveIn(Reg, RC);
3232+ Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3233+ }
31353234
31363235 if (Arg.Flags.isSRet()) {
31373236 // The return object should be reasonably addressable.
@@ -3373,7 +3472,7 @@ SDValue SITargetLowering::LowerCallResult(
33733472// from the explicit user arguments present in the IR.
33743473void SITargetLowering::passSpecialInputs(
33753474 CallLoweringInfo &CLI, CCState &CCInfo, const SIMachineFunctionInfo &Info,
3376- SmallVectorImpl<std::pair<unsigned , SDValue>> &RegsToPass,
3475+ SmallVectorImpl<std::pair<Register , SDValue>> &RegsToPass,
33773476 SmallVectorImpl<SDValue> &MemOpChains, SDValue Chain) const {
33783477 // If we don't have a call site, this was a call inserted by
33793478 // legalization. These can never use special inputs.
@@ -3817,7 +3916,7 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
38173916 }
38183917
38193918 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
3820- SmallVector<std::pair<unsigned , SDValue>, 8> RegsToPass;
3919+ SmallVector<std::pair<Register , SDValue>, 8> RegsToPass;
38213920 SmallVector<SDValue, 8> MemOpChains;
38223921
38233922 // Analyze operands of the call, assigning locations to each operand.
@@ -3875,6 +3974,8 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
38753974
38763975 MVT PtrVT = MVT::i32;
38773976
3977+ InregVPGRSpillerCallSite Spiller(DAG, MF);
3978+
38783979 // Walk the register/memloc assignments, inserting copies/loads.
38793980 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
38803981 CCValAssign &VA = ArgLocs[i];
@@ -3988,8 +4089,8 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
39884089 SDValue InGlue;
39894090
39904091 unsigned ArgIdx = 0;
3991- for (auto [Reg, Val] : RegsToPass) {
3992- if (ArgIdx++ >= NumSpecialInputs &&
4092+ for (auto & [Reg, Val] : RegsToPass) {
4093+ if (ArgIdx >= NumSpecialInputs &&
39934094 (IsChainCallConv || !Val->isDivergent()) && TRI->isSGPRPhysReg(Reg)) {
39944095 // For chain calls, the inreg arguments are required to be
39954096 // uniform. Speculatively Insert a readfirstlane in case we cannot prove
@@ -4008,7 +4109,21 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
40084109 ReadfirstlaneArgs);
40094110 }
40104111
4011- Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4112+ if (ArgIdx >= NumSpecialInputs &&
4113+ Outs[ArgIdx - NumSpecialInputs].Flags.isInReg() &&
4114+ AMDGPU::VGPR_32RegClass.contains(Reg)) {
4115+ Spiller.writeLane(DL, Reg, Val,
4116+ ArgLocs[ArgIdx - NumSpecialInputs].getLocVT());
4117+ } else {
4118+ Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4119+ InGlue = Chain.getValue(1);
4120+ }
4121+
4122+ ++ArgIdx;
4123+ }
4124+
4125+ if (SDValue R = Spiller.finalize(Chain, DL, InGlue)) {
4126+ Chain = R;
40124127 InGlue = Chain.getValue(1);
40134128 }
40144129
0 commit comments