@@ -238,18 +238,11 @@ getVectorLoweringShape(EVT VectorEVT, bool CanLowerTo256Bit) {
238
238
return std::nullopt;
239
239
LLVM_FALLTHROUGH;
240
240
case MVT::v2i8:
241
- case MVT::v2i16:
242
241
case MVT::v2i32:
243
242
case MVT::v2i64:
244
- case MVT::v2f16:
245
- case MVT::v2bf16:
246
243
case MVT::v2f32:
247
244
case MVT::v2f64:
248
- case MVT::v4i8:
249
- case MVT::v4i16:
250
245
case MVT::v4i32:
251
- case MVT::v4f16:
252
- case MVT::v4bf16:
253
246
case MVT::v4f32:
254
247
// This is a "native" vector type
255
248
return std::pair (NumElts, EltVT);
@@ -262,6 +255,13 @@ getVectorLoweringShape(EVT VectorEVT, bool CanLowerTo256Bit) {
262
255
if (!CanLowerTo256Bit)
263
256
return std::nullopt;
264
257
LLVM_FALLTHROUGH;
258
+ case MVT::v2i16: // <1 x i16x2>
259
+ case MVT::v2f16: // <1 x f16x2>
260
+ case MVT::v2bf16: // <1 x bf16x2>
261
+ case MVT::v4i8: // <1 x i8x4>
262
+ case MVT::v4i16: // <2 x i16x2>
263
+ case MVT::v4f16: // <2 x f16x2>
264
+ case MVT::v4bf16: // <2 x bf16x2>
265
265
case MVT::v8i8: // <2 x i8x4>
266
266
case MVT::v8f16: // <4 x f16x2>
267
267
case MVT::v8bf16: // <4 x bf16x2>
@@ -845,7 +845,8 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
845
845
// We have some custom DAG combine patterns for these nodes
846
846
setTargetDAGCombine ({ISD::ADD, ISD::AND, ISD::EXTRACT_VECTOR_ELT, ISD::FADD,
847
847
ISD::MUL, ISD::SHL, ISD::SREM, ISD::UREM, ISD::VSELECT,
848
- ISD::BUILD_VECTOR, ISD::ADDRSPACECAST});
848
+ ISD::BUILD_VECTOR, ISD::ADDRSPACECAST, ISD::LOAD,
849
+ ISD::STORE});
849
850
850
851
// setcc for f16x2 and bf16x2 needs special handling to prevent
851
852
// legalizer's attempt to scalarize it due to v2i1 not being legal.
@@ -3464,19 +3465,16 @@ SDValue NVPTXTargetLowering::LowerFormalArguments(
3464
3465
unsigned I = 0 ;
3465
3466
for (const unsigned NumElts : VectorInfo) {
3466
3467
const EVT EltVT = VTs[I];
3467
- const EVT LoadVT = [&]() -> EVT {
3468
- // i1 is loaded/stored as i8.
3469
- if (EltVT == MVT::i1)
3470
- return MVT::i8 ;
3471
- // getLoad needs a vector type, but it can't handle
3472
- // vectors which contain v2f16 or v2bf16 elements. So we must load
3473
- // using i32 here and then bitcast back.
3474
- if (EltVT.isVector ())
3475
- return MVT::getIntegerVT (EltVT.getFixedSizeInBits ());
3476
- return EltVT;
3477
- }();
3468
+ // i1 is loaded/stored as i8
3469
+ const EVT LoadVT = EltVT == MVT::i1 ? MVT::i8 : EltVT;
3470
+ // If the element is a packed type (ex. v2f16, v4i8, etc) holding
3471
+ // multiple elements.
3472
+ const unsigned PackingAmt =
3473
+ LoadVT.isVector () ? LoadVT.getVectorNumElements () : 1 ;
3474
+
3475
+ const EVT VecVT = EVT::getVectorVT (
3476
+ F->getContext (), LoadVT.getScalarType (), NumElts * PackingAmt);
3478
3477
3479
- const EVT VecVT = EVT::getVectorVT (F->getContext (), LoadVT, NumElts);
3480
3478
SDValue VecAddr = DAG.getObjectPtrOffset (
3481
3479
dl, ArgSymbol, TypeSize::getFixed (Offsets[I]));
3482
3480
@@ -3496,8 +3494,10 @@ SDValue NVPTXTargetLowering::LowerFormalArguments(
3496
3494
if (P.getNode ())
3497
3495
P.getNode ()->setIROrder (Arg.getArgNo () + 1 );
3498
3496
for (const unsigned J : llvm::seq (NumElts)) {
3499
- SDValue Elt = DAG.getNode (ISD::EXTRACT_VECTOR_ELT, dl, LoadVT, P,
3500
- DAG.getIntPtrConstant (J, dl));
3497
+ SDValue Elt =
3498
+ DAG.getNode (LoadVT.isVector () ? ISD::EXTRACT_SUBVECTOR
3499
+ : ISD::EXTRACT_VECTOR_ELT,
3500
+ dl, LoadVT, P, DAG.getIntPtrConstant (J * PackingAmt, dl));
3501
3501
3502
3502
// Extend or truncate the element if necessary (e.g. an i8 is loaded
3503
3503
// into an i16 register)
@@ -3511,9 +3511,6 @@ SDValue NVPTXTargetLowering::LowerFormalArguments(
3511
3511
Elt);
3512
3512
} else if (ExpactedVT.bitsLT (Elt.getValueType ())) {
3513
3513
Elt = DAG.getNode (ISD::TRUNCATE, dl, ExpactedVT, Elt);
3514
- } else {
3515
- // v2f16 was loaded as an i32. Now we must bitcast it back.
3516
- Elt = DAG.getBitcast (EltVT, Elt);
3517
3514
}
3518
3515
InVals.push_back (Elt);
3519
3516
}
@@ -5047,26 +5044,244 @@ PerformFADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
5047
5044
return SDValue ();
5048
5045
}
5049
5046
5050
- static SDValue PerformStoreCombineHelper (SDNode *N, std::size_t Front,
5051
- std::size_t Back) {
5047
+ // / Combine extractelts into a load by increasing the number of return values.
5048
+ static SDValue
5049
+ combineUnpackingMovIntoLoad (SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
5050
+ // Don't run this optimization before the legalizer
5051
+ if (!DCI.isAfterLegalizeDAG ())
5052
+ return SDValue ();
5053
+
5054
+ EVT ElemVT = N->getValueType (0 );
5055
+ if (!Isv2x16VT (ElemVT))
5056
+ return SDValue ();
5057
+
5058
+ // Check whether all outputs are either used by an extractelt or are
5059
+ // glue/chain nodes
5060
+ if (!all_of (N->uses (), [&](SDUse &U) {
5061
+ // Skip glue, chain nodes
5062
+ if (U.getValueType () == MVT::Glue || U.getValueType () == MVT::Other)
5063
+ return true ;
5064
+ if (U.getUser ()->getOpcode () == ISD::EXTRACT_VECTOR_ELT) {
5065
+ if (N->getOpcode () != ISD::LOAD)
5066
+ return true ;
5067
+ // Since this is an ISD::LOAD, check all extractelts are used. If
5068
+ // any are not used, we don't want to defeat another optimization that
5069
+ // will narrow the load.
5070
+ //
5071
+ // For example:
5072
+ //
5073
+ // L: v2f16,ch = load <p>
5074
+ // e0: f16 = extractelt L:0, 0
5075
+ // e1: f16 = extractelt L:0, 1 <-- unused
5076
+ // store e0
5077
+ //
5078
+ // Can be optimized by DAGCombiner to:
5079
+ //
5080
+ // L: f16,ch = load <p>
5081
+ // store L:0
5082
+ return !U.getUser ()->use_empty ();
5083
+ }
5084
+
5085
+ // Otherwise, this use prevents us from splitting a value.
5086
+ return false ;
5087
+ }))
5088
+ return SDValue ();
5089
+
5090
+ auto *LD = cast<MemSDNode>(N);
5091
+ EVT MemVT = LD->getMemoryVT ();
5092
+ SDLoc DL (LD);
5093
+
5094
+ // the new opcode after we double the number of operands
5095
+ NVPTXISD::NodeType Opcode;
5096
+ SmallVector<SDValue> Operands (LD->ops ());
5097
+ unsigned OldNumValues;
5098
+ switch (LD->getOpcode ()) {
5099
+ case ISD::LOAD:
5100
+ OldNumValues = 1 ;
5101
+ // Any packed type is legal, so the legalizer will not have lowered
5102
+ // ISD::LOAD -> NVPTXISD::Load (unless it's under-aligned). We have to do it
5103
+ // here.
5104
+ Opcode = NVPTXISD::LoadV2;
5105
+ Operands.push_back (DCI.DAG .getIntPtrConstant (
5106
+ cast<LoadSDNode>(LD)->getExtensionType (), DL));
5107
+ break ;
5108
+ case NVPTXISD::LoadParamV2:
5109
+ OldNumValues = 2 ;
5110
+ Opcode = NVPTXISD::LoadParamV4;
5111
+ break ;
5112
+ case NVPTXISD::LoadV2:
5113
+ OldNumValues = 2 ;
5114
+ Opcode = NVPTXISD::LoadV4;
5115
+ break ;
5116
+ case NVPTXISD::LoadV4:
5117
+ // PTX doesn't support v8 for 16-bit values
5118
+ case NVPTXISD::LoadV8:
5119
+ // PTX doesn't support the next doubling of outputs
5120
+ return SDValue ();
5121
+ }
5122
+
5123
+ SmallVector<EVT> NewVTs (OldNumValues * 2 , ElemVT.getVectorElementType ());
5124
+ // add remaining chain and glue values
5125
+ for (unsigned I = OldNumValues; I < LD->getNumValues (); ++I)
5126
+ NewVTs.push_back (LD->getValueType (I));
5127
+
5128
+ // Create the new load
5129
+ SDValue NewLoad =
5130
+ DCI.DAG .getMemIntrinsicNode (Opcode, DL, DCI.DAG .getVTList (NewVTs),
5131
+ Operands, MemVT, LD->getMemOperand ());
5132
+
5133
+ // Now we use a combination of BUILD_VECTORs and a MERGE_VALUES node to keep
5134
+ // the outputs the same. These nodes will be optimized away in later
5135
+ // DAGCombiner iterations.
5136
+ SmallVector<SDValue> Results;
5137
+ for (unsigned I = 0 ; I < NewLoad->getNumValues ();) {
5138
+ if (NewLoad->getValueType (I) == ElemVT.getVectorElementType ()) {
5139
+ Results.push_back (DCI.DAG .getBuildVector (
5140
+ ElemVT, DL, {NewLoad.getValue (I), NewLoad.getValue (I + 1 )}));
5141
+ I += 2 ;
5142
+ } else {
5143
+ Results.push_back (NewLoad.getValue (I));
5144
+ I += 1 ;
5145
+ }
5146
+ }
5147
+
5148
+ return DCI.DAG .getMergeValues (Results, DL);
5149
+ }
5150
+
5151
+ // / Fold a packing mov into a store. This may help lower register pressure.
5152
+ // /
5153
+ // / ex:
5154
+ // / v: v2f16 = build_vector a:f16, b:f16
5155
+ // / StoreRetval v
5156
+ // /
5157
+ // / ...is turned into...
5158
+ // /
5159
+ // / StoreRetvalV2 a:f16, b:f16
5160
+ static SDValue combinePackingMovIntoStore (SDNode *N,
5161
+ TargetLowering::DAGCombinerInfo &DCI,
5162
+ unsigned Front, unsigned Back) {
5163
+ // We want to run this as late as possible since other optimizations may
5164
+ // eliminate the BUILD_VECTORs.
5165
+ if (!DCI.isAfterLegalizeDAG ())
5166
+ return SDValue ();
5167
+
5168
+ // Get the type of the operands being stored.
5169
+ EVT ElementVT = N->getOperand (Front).getValueType ();
5170
+
5171
+ if (!Isv2x16VT (ElementVT))
5172
+ return SDValue ();
5173
+
5174
+ auto *ST = cast<MemSDNode>(N);
5175
+ EVT MemVT = ElementVT.getVectorElementType ();
5176
+
5177
+ // The new opcode after we double the number of operands.
5178
+ NVPTXISD::NodeType Opcode;
5179
+ switch (N->getOpcode ()) {
5180
+ case ISD::STORE:
5181
+ // Any packed type is legal, so the legalizer will not have lowered
5182
+ // ISD::STORE -> NVPTXISD::Store (unless it's under-aligned). We have to do
5183
+ // it here.
5184
+ MemVT = ST->getMemoryVT ();
5185
+ Opcode = NVPTXISD::StoreV2;
5186
+ break ;
5187
+ case NVPTXISD::StoreParam:
5188
+ Opcode = NVPTXISD::StoreParamV2;
5189
+ break ;
5190
+ case NVPTXISD::StoreParamV2:
5191
+ Opcode = NVPTXISD::StoreParamV4;
5192
+ break ;
5193
+ case NVPTXISD::StoreRetval:
5194
+ Opcode = NVPTXISD::StoreRetvalV2;
5195
+ break ;
5196
+ case NVPTXISD::StoreRetvalV2:
5197
+ Opcode = NVPTXISD::StoreRetvalV4;
5198
+ break ;
5199
+ case NVPTXISD::StoreV2:
5200
+ MemVT = ST->getMemoryVT ();
5201
+ Opcode = NVPTXISD::StoreV4;
5202
+ break ;
5203
+ case NVPTXISD::StoreV4:
5204
+ // PTX doesn't support v8 for 16-bit values
5205
+ case NVPTXISD::StoreParamV4:
5206
+ case NVPTXISD::StoreRetvalV4:
5207
+ case NVPTXISD::StoreV8:
5208
+ // PTX doesn't support the next doubling of operands for these opcodes.
5209
+ return SDValue ();
5210
+ default :
5211
+ llvm_unreachable (" Unhandled store opcode" );
5212
+ }
5213
+
5214
+ // Scan the operands and if they're all BUILD_VECTORs, we'll have gathered
5215
+ // their elements.
5216
+ SmallVector<SDValue, 4 > Operands (N->ops ().take_front (Front));
5217
+ for (SDValue BV : N->ops ().drop_front (Front).drop_back (Back)) {
5218
+ if (BV.getOpcode () != ISD::BUILD_VECTOR)
5219
+ return SDValue ();
5220
+
5221
+ // If the operand has multiple uses, this optimization can increase register
5222
+ // pressure.
5223
+ if (!BV.hasOneUse ())
5224
+ return SDValue ();
5225
+
5226
+ // DAGCombiner visits nodes bottom-up. Check the BUILD_VECTOR operands for
5227
+ // any signs they may be folded by some other pattern or rule.
5228
+ for (SDValue Op : BV->ops ()) {
5229
+ // Peek through bitcasts
5230
+ if (Op.getOpcode () == ISD::BITCAST)
5231
+ Op = Op.getOperand (0 );
5232
+
5233
+ // This may be folded into a PRMT.
5234
+ if (Op.getValueType () == MVT::i16 && Op.getOpcode () == ISD::TRUNCATE &&
5235
+ Op->getOperand (0 ).getValueType () == MVT::i32 )
5236
+ return SDValue ();
5237
+
5238
+ // This may be folded into cvt.bf16x2
5239
+ if (Op.getOpcode () == ISD::FP_ROUND)
5240
+ return SDValue ();
5241
+ }
5242
+ Operands.insert (Operands.end (), {BV.getOperand (0 ), BV.getOperand (1 )});
5243
+ }
5244
+ for (SDValue Op : N->ops ().take_back (Back))
5245
+ Operands.push_back (Op);
5246
+
5247
+ // Now we replace the store
5248
+ return DCI.DAG .getMemIntrinsicNode (Opcode, SDLoc (N), N->getVTList (),
5249
+ Operands, MemVT, ST->getMemOperand ());
5250
+ }
5251
+
5252
+ static SDValue PerformStoreCombineHelper (SDNode *N,
5253
+ TargetLowering::DAGCombinerInfo &DCI,
5254
+ unsigned Front, unsigned Back) {
5052
5255
if (all_of (N->ops ().drop_front (Front).drop_back (Back),
5053
5256
[](const SDUse &U) { return U.get ()->isUndef (); }))
5054
5257
// Operand 0 is the previous value in the chain. Cannot return EntryToken
5055
5258
// as the previous value will become unused and eliminated later.
5056
5259
return N->getOperand (0 );
5057
5260
5058
- return SDValue ( );
5261
+ return combinePackingMovIntoStore (N, DCI, Front, Back );
5059
5262
}
5060
5263
5061
- static SDValue PerformStoreParamCombine (SDNode *N) {
5264
+ static SDValue PerformSTORECombine (SDNode *N,
5265
+ TargetLowering::DAGCombinerInfo &DCI) {
5266
+ return combinePackingMovIntoStore (N, DCI, 1 , 2 );
5267
+ }
5268
+
5269
+ static SDValue PerformStoreCombine (SDNode *N,
5270
+ TargetLowering::DAGCombinerInfo &DCI) {
5271
+ return combinePackingMovIntoStore (N, DCI, 1 , 2 );
5272
+ }
5273
+
5274
+ static SDValue PerformStoreParamCombine (SDNode *N,
5275
+ TargetLowering::DAGCombinerInfo &DCI) {
5062
5276
// Operands from the 3rd to the 2nd last one are the values to be stored.
5063
5277
// {Chain, ArgID, Offset, Val, Glue}
5064
- return PerformStoreCombineHelper (N, 3 , 1 );
5278
+ return PerformStoreCombineHelper (N, DCI, 3 , 1 );
5065
5279
}
5066
5280
5067
- static SDValue PerformStoreRetvalCombine (SDNode *N) {
5281
+ static SDValue PerformStoreRetvalCombine (SDNode *N,
5282
+ TargetLowering::DAGCombinerInfo &DCI) {
5068
5283
// Operands from the 2nd to the last one are the values to be stored
5069
- return PerformStoreCombineHelper (N, 2 , 0 );
5284
+ return PerformStoreCombineHelper (N, DCI, 2 , 0 );
5070
5285
}
5071
5286
5072
5287
// / PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
@@ -5697,14 +5912,24 @@ SDValue NVPTXTargetLowering::PerformDAGCombine(SDNode *N,
5697
5912
return PerformREMCombine (N, DCI, OptLevel);
5698
5913
case ISD::SETCC:
5699
5914
return PerformSETCCCombine (N, DCI, STI.getSmVersion ());
5915
+ case ISD::LOAD:
5916
+ case NVPTXISD::LoadParamV2:
5917
+ case NVPTXISD::LoadV2:
5918
+ case NVPTXISD::LoadV4:
5919
+ return combineUnpackingMovIntoLoad (N, DCI);
5920
+ case ISD::STORE:
5921
+ return PerformSTORECombine (N, DCI);
5700
5922
case NVPTXISD::StoreRetval:
5701
5923
case NVPTXISD::StoreRetvalV2:
5702
5924
case NVPTXISD::StoreRetvalV4:
5703
- return PerformStoreRetvalCombine (N);
5925
+ return PerformStoreRetvalCombine (N, DCI );
5704
5926
case NVPTXISD::StoreParam:
5705
5927
case NVPTXISD::StoreParamV2:
5706
5928
case NVPTXISD::StoreParamV4:
5707
- return PerformStoreParamCombine (N);
5929
+ return PerformStoreParamCombine (N, DCI);
5930
+ case NVPTXISD::StoreV2:
5931
+ case NVPTXISD::StoreV4:
5932
+ return PerformStoreCombine (N, DCI);
5708
5933
case ISD::EXTRACT_VECTOR_ELT:
5709
5934
return PerformEXTRACTCombine (N, DCI);
5710
5935
case ISD::VSELECT:
0 commit comments