@@ -2841,6 +2841,91 @@ void SITargetLowering::insertCopiesSplitCSR(
2841
2841
}
2842
2842
}
2843
2843
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 read(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
+ CCState &State;
2895
+
2896
+ Register DstReg;
2897
+ SDValue Glue;
2898
+ unsigned CurLane = 0;
2899
+
2900
+ SelectionDAG &DAG;
2901
+ MachineFunction &MF;
2902
+
2903
+ public:
2904
+ InregVPGRSpillerCallSite(SelectionDAG &DAG, MachineFunction &MF,
2905
+ CCState &State)
2906
+ : State(State), DAG(DAG), MF(MF) {}
2907
+
2908
+ std::pair<SDValue, SDValue> write(SDValue Chain, const SDLoc &SL,
2909
+ Register &Reg, SDValue Val, SDValue InGlue,
2910
+ EVT VT) {
2911
+ if (DstReg.isValid()) {
2912
+ Reg = DstReg;
2913
+ } else {
2914
+ DstReg = Reg;
2915
+ Glue = DAG.getCopyToReg(Chain, SL, Reg, Val, InGlue).getValue(1);
2916
+ }
2917
+ // According to the calling convention, only SGPR4–SGPR29 should be used for
2918
+ // passing 'inreg' function arguments. Therefore, the number of 'inreg' VGPR
2919
+ // arguments must not exceed 26.
2920
+ assert(CurLane < 26 && "more than expected VGPR inreg arguments");
2921
+ SmallVector<SDValue, 4> Operands{
2922
+ DAG.getTargetConstant(Intrinsic::amdgcn_writelane, SL, MVT::i32),
2923
+ DAG.getRegister(DstReg, VT), Val,
2924
+ DAG.getTargetConstant(CurLane++, SL, MVT::i32)};
2925
+ return {DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SL, VT, Operands), Glue};
2926
+ }
2927
+ };
2928
+
2844
2929
SDValue SITargetLowering::LowerFormalArguments(
2845
2930
SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2846
2931
const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
@@ -2963,6 +3048,7 @@ SDValue SITargetLowering::LowerFormalArguments(
2963
3048
// FIXME: Alignment of explicit arguments totally broken with non-0 explicit
2964
3049
// kern arg offset.
2965
3050
const Align KernelArgBaseAlign = Align(16);
3051
+ InregVPGRSpillerCallee Spiller(DAG, MF, CCInfo);
2966
3052
2967
3053
for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
2968
3054
const ISD::InputArg &Arg = Ins[i];
@@ -3130,8 +3216,17 @@ SDValue SITargetLowering::LowerFormalArguments(
3130
3216
llvm_unreachable("Unexpected register class in LowerFormalArguments!");
3131
3217
EVT ValVT = VA.getValVT();
3132
3218
3133
- Reg = MF.addLiveIn(Reg, RC);
3134
- SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3219
+ SDValue Val;
3220
+ // If an argument is marked inreg but gets pushed to a VGPR, it indicates
3221
+ // we've run out of SGPRs for argument passing. In such cases, we'd prefer
3222
+ // to start packing inreg arguments into individual lanes of VGPRs, rather
3223
+ // than placing them directly into VGPRs.
3224
+ if (RC == &AMDGPU::VGPR_32RegClass && Arg.Flags.isInReg()) {
3225
+ Val = Spiller.read(Chain, DL, Reg, VT);
3226
+ } else {
3227
+ Reg = MF.addLiveIn(Reg, RC);
3228
+ Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
3229
+ }
3135
3230
3136
3231
if (Arg.Flags.isSRet()) {
3137
3232
// The return object should be reasonably addressable.
@@ -3373,7 +3468,7 @@ SDValue SITargetLowering::LowerCallResult(
3373
3468
// from the explicit user arguments present in the IR.
3374
3469
void SITargetLowering::passSpecialInputs(
3375
3470
CallLoweringInfo &CLI, CCState &CCInfo, const SIMachineFunctionInfo &Info,
3376
- SmallVectorImpl<std::pair<unsigned , SDValue>> &RegsToPass,
3471
+ SmallVectorImpl<std::pair<Register , SDValue>> &RegsToPass,
3377
3472
SmallVectorImpl<SDValue> &MemOpChains, SDValue Chain) const {
3378
3473
// If we don't have a call site, this was a call inserted by
3379
3474
// legalization. These can never use special inputs.
@@ -3817,7 +3912,7 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
3817
3912
}
3818
3913
3819
3914
const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
3820
- SmallVector<std::pair<unsigned , SDValue>, 8> RegsToPass;
3915
+ SmallVector<std::pair<Register , SDValue>, 8> RegsToPass;
3821
3916
SmallVector<SDValue, 8> MemOpChains;
3822
3917
3823
3918
// Analyze operands of the call, assigning locations to each operand.
@@ -3875,6 +3970,8 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
3875
3970
3876
3971
MVT PtrVT = MVT::i32;
3877
3972
3973
+ InregVPGRSpillerCallSite Spiller(DAG, MF, CCInfo);
3974
+
3878
3975
// Walk the register/memloc assignments, inserting copies/loads.
3879
3976
for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3880
3977
CCValAssign &VA = ArgLocs[i];
@@ -3988,8 +4085,8 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
3988
4085
SDValue InGlue;
3989
4086
3990
4087
unsigned ArgIdx = 0;
3991
- for (auto [Reg, Val] : RegsToPass) {
3992
- if (ArgIdx++ >= NumSpecialInputs &&
4088
+ for (auto & [Reg, Val] : RegsToPass) {
4089
+ if (ArgIdx >= NumSpecialInputs &&
3993
4090
(IsChainCallConv || !Val->isDivergent()) && TRI->isSGPRPhysReg(Reg)) {
3994
4091
// For chain calls, the inreg arguments are required to be
3995
4092
// uniform. Speculatively Insert a readfirstlane in case we cannot prove
@@ -4008,8 +4105,18 @@ SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI,
4008
4105
ReadfirstlaneArgs);
4009
4106
}
4010
4107
4011
- Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4012
- InGlue = Chain.getValue(1);
4108
+ if (ArgIdx >= NumSpecialInputs &&
4109
+ Outs[ArgIdx - NumSpecialInputs].Flags.isInReg() &&
4110
+ AMDGPU::VGPR_32RegClass.contains(Reg)) {
4111
+ std::tie(Chain, InGlue) =
4112
+ Spiller.write(Chain, DL, Reg, Val, InGlue,
4113
+ ArgLocs[ArgIdx - NumSpecialInputs].getLocVT());
4114
+ } else {
4115
+ Chain = DAG.getCopyToReg(Chain, DL, Reg, Val, InGlue);
4116
+ InGlue = Chain.getValue(1);
4117
+ }
4118
+
4119
+ ++ArgIdx;
4013
4120
}
4014
4121
4015
4122
// We don't usually want to end the call-sequence here because we would tidy
0 commit comments