Skip to content

Commit d439c9e

Browse files
committed
Revert "[NVPTX] Legalize aext-load to zext-load to expose more DAG combines (#154251)"
Causes failures in the LLVM libc test suite https://lab.llvm.org/buildbot/#/builders/69/builds/26327/steps/12/logs/stdio. This reverts commit a3ed96b.
1 parent 1bf31c3 commit d439c9e

File tree

12 files changed

+868
-752
lines changed

12 files changed

+868
-752
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15132,7 +15132,7 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
1513215132
return foldedExt;
1513315133
} else if (ISD::isNON_EXTLoad(N0.getNode()) &&
1513415134
ISD::isUNINDEXEDLoad(N0.getNode()) &&
15135-
TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, VT, N0.getValueType())) {
15135+
TLI.isLoadExtLegal(ISD::EXTLOAD, VT, N0.getValueType())) {
1513615136
bool DoXform = true;
1513715137
SmallVector<SDNode *, 4> SetCCs;
1513815138
if (!N0.hasOneUse())

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 100 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -702,56 +702,57 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
702702
// intrinsics.
703703
setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
704704

705-
// FP extload/truncstore is not legal in PTX. We need to expand all these.
706-
for (auto FloatVTs :
707-
{MVT::fp_valuetypes(), MVT::fp_fixedlen_vector_valuetypes()}) {
708-
for (MVT ValVT : FloatVTs) {
709-
for (MVT MemVT : FloatVTs) {
710-
setLoadExtAction(ISD::EXTLOAD, ValVT, MemVT, Expand);
711-
setTruncStoreAction(ValVT, MemVT, Expand);
712-
}
713-
}
714-
}
715-
716-
// To improve CodeGen we'll legalize any-extend loads to zext loads. This is
717-
// how they'll be lowered in ISel anyway, and by doing this a little earlier
718-
// we allow for more DAG combine opportunities.
719-
for (auto IntVTs :
720-
{MVT::integer_valuetypes(), MVT::integer_fixedlen_vector_valuetypes()})
721-
for (MVT ValVT : IntVTs)
722-
for (MVT MemVT : IntVTs)
723-
if (isTypeLegal(ValVT))
724-
setLoadExtAction(ISD::EXTLOAD, ValVT, MemVT, Custom);
705+
// Turn FP extload into load/fpextend
706+
setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
707+
setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
708+
setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::bf16, Expand);
709+
setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::bf16, Expand);
710+
setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
711+
setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
712+
setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
713+
setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2bf16, Expand);
714+
setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2bf16, Expand);
715+
setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
716+
setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
717+
setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
718+
setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4bf16, Expand);
719+
setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4bf16, Expand);
720+
setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
721+
setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
722+
setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
723+
setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8bf16, Expand);
724+
setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8bf16, Expand);
725+
// Turn FP truncstore into trunc + store.
726+
// FIXME: vector types should also be expanded
727+
setTruncStoreAction(MVT::f32, MVT::f16, Expand);
728+
setTruncStoreAction(MVT::f64, MVT::f16, Expand);
729+
setTruncStoreAction(MVT::f32, MVT::bf16, Expand);
730+
setTruncStoreAction(MVT::f64, MVT::bf16, Expand);
731+
setTruncStoreAction(MVT::f64, MVT::f32, Expand);
732+
setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
733+
setTruncStoreAction(MVT::v2f32, MVT::v2bf16, Expand);
725734

726735
// PTX does not support load / store predicate registers
727-
setOperationAction({ISD::LOAD, ISD::STORE}, MVT::i1, Custom);
736+
setOperationAction(ISD::LOAD, MVT::i1, Custom);
737+
setOperationAction(ISD::STORE, MVT::i1, Custom);
738+
728739
for (MVT VT : MVT::integer_valuetypes()) {
729-
setLoadExtAction({ISD::SEXTLOAD, ISD::ZEXTLOAD, ISD::EXTLOAD}, VT, MVT::i1,
730-
Promote);
740+
setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
741+
setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
742+
setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
731743
setTruncStoreAction(VT, MVT::i1, Expand);
732744
}
733745

734-
// Register custom handling for illegal type loads/stores. We'll try to custom
735-
// lower almost all illegal types and logic in the lowering will discard cases
736-
// we can't handle.
737-
setOperationAction({ISD::LOAD, ISD::STORE}, {MVT::i128, MVT::f128}, Custom);
738-
for (MVT VT : MVT::fixedlen_vector_valuetypes())
739-
if (!isTypeLegal(VT) && VT.getStoreSizeInBits() <= 256)
740-
setOperationAction({ISD::STORE, ISD::LOAD}, VT, Custom);
741-
742-
// Custom legalization for LDU intrinsics.
743-
// TODO: The logic to lower these is not very robust and we should rewrite it.
744-
// Perhaps LDU should not be represented as an intrinsic at all.
745-
setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
746-
for (MVT VT : MVT::fixedlen_vector_valuetypes())
747-
if (IsPTXVectorType(VT))
748-
setOperationAction(ISD::INTRINSIC_W_CHAIN, VT, Custom);
749-
750746
setCondCodeAction({ISD::SETNE, ISD::SETEQ, ISD::SETUGE, ISD::SETULE,
751747
ISD::SETUGT, ISD::SETULT, ISD::SETGT, ISD::SETLT,
752748
ISD::SETGE, ISD::SETLE},
753749
MVT::i1, Expand);
754750

751+
// expand extload of vector of integers.
752+
setLoadExtAction({ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}, MVT::v2i16,
753+
MVT::v2i8, Expand);
754+
setTruncStoreAction(MVT::v2i16, MVT::v2i8, Expand);
755+
755756
// This is legal in NVPTX
756757
setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
757758
setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
@@ -766,12 +767,24 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
766767
// DEBUGTRAP can be lowered to PTX brkpt
767768
setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
768769

770+
// Register custom handling for vector loads/stores
771+
for (MVT VT : MVT::fixedlen_vector_valuetypes())
772+
if (IsPTXVectorType(VT))
773+
setOperationAction({ISD::LOAD, ISD::STORE, ISD::INTRINSIC_W_CHAIN}, VT,
774+
Custom);
775+
776+
setOperationAction({ISD::LOAD, ISD::STORE, ISD::INTRINSIC_W_CHAIN},
777+
{MVT::i128, MVT::f128}, Custom);
778+
769779
// Support varargs.
770780
setOperationAction(ISD::VASTART, MVT::Other, Custom);
771781
setOperationAction(ISD::VAARG, MVT::Other, Custom);
772782
setOperationAction(ISD::VACOPY, MVT::Other, Expand);
773783
setOperationAction(ISD::VAEND, MVT::Other, Expand);
774784

785+
// Custom handling for i8 intrinsics
786+
setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i8, Custom);
787+
775788
setOperationAction({ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX},
776789
{MVT::i16, MVT::i32, MVT::i64}, Legal);
777790

@@ -3079,14 +3092,39 @@ static void replaceLoadVector(SDNode *N, SelectionDAG &DAG,
30793092
SmallVectorImpl<SDValue> &Results,
30803093
const NVPTXSubtarget &STI);
30813094

3095+
SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
3096+
if (Op.getValueType() == MVT::i1)
3097+
return LowerLOADi1(Op, DAG);
3098+
3099+
EVT VT = Op.getValueType();
3100+
3101+
if (NVPTX::isPackedVectorTy(VT)) {
3102+
// v2f32/v2f16/v2bf16/v2i16/v4i8 are legal, so we can't rely on legalizer to
3103+
// handle unaligned loads and have to handle it here.
3104+
LoadSDNode *Load = cast<LoadSDNode>(Op);
3105+
EVT MemVT = Load->getMemoryVT();
3106+
if (!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
3107+
MemVT, *Load->getMemOperand())) {
3108+
SDValue Ops[2];
3109+
std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
3110+
return DAG.getMergeValues(Ops, SDLoc(Op));
3111+
}
3112+
}
3113+
3114+
return SDValue();
3115+
}
3116+
30823117
// v = ld i1* addr
30833118
// =>
30843119
// v1 = ld i8* addr (-> i16)
30853120
// v = trunc i16 to i1
3086-
static SDValue lowerLOADi1(LoadSDNode *LD, SelectionDAG &DAG) {
3087-
SDLoc dl(LD);
3121+
SDValue NVPTXTargetLowering::LowerLOADi1(SDValue Op, SelectionDAG &DAG) const {
3122+
SDNode *Node = Op.getNode();
3123+
LoadSDNode *LD = cast<LoadSDNode>(Node);
3124+
SDLoc dl(Node);
30883125
assert(LD->getExtensionType() == ISD::NON_EXTLOAD);
3089-
assert(LD->getValueType(0) == MVT::i1 && "Custom lowering for i1 load only");
3126+
assert(Node->getValueType(0) == MVT::i1 &&
3127+
"Custom lowering for i1 load only");
30903128
SDValue newLD = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i16, LD->getChain(),
30913129
LD->getBasePtr(), LD->getPointerInfo(),
30923130
MVT::i8, LD->getAlign(),
@@ -3095,27 +3133,8 @@ static SDValue lowerLOADi1(LoadSDNode *LD, SelectionDAG &DAG) {
30953133
// The legalizer (the caller) is expecting two values from the legalized
30963134
// load, so we build a MergeValues node for it. See ExpandUnalignedLoad()
30973135
// in LegalizeDAG.cpp which also uses MergeValues.
3098-
return DAG.getMergeValues({result, LD->getChain()}, dl);
3099-
}
3100-
3101-
SDValue NVPTXTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
3102-
LoadSDNode *LD = cast<LoadSDNode>(Op);
3103-
3104-
if (Op.getValueType() == MVT::i1)
3105-
return lowerLOADi1(LD, DAG);
3106-
3107-
// To improve CodeGen we'll legalize any-extend loads to zext loads. This is
3108-
// how they'll be lowered in ISel anyway, and by doing this a little earlier
3109-
// we allow for more DAG combine opportunities.
3110-
if (LD->getExtensionType() == ISD::EXTLOAD) {
3111-
assert(LD->getValueType(0).isInteger() && LD->getMemoryVT().isInteger() &&
3112-
"Unexpected fpext-load");
3113-
return DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(Op), Op.getValueType(),
3114-
LD->getChain(), LD->getBasePtr(), LD->getMemoryVT(),
3115-
LD->getMemOperand());
3116-
}
3117-
3118-
llvm_unreachable("Unexpected custom lowering for load");
3136+
SDValue Ops[] = { result, LD->getChain() };
3137+
return DAG.getMergeValues(Ops, dl);
31193138
}
31203139

31213140
SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
@@ -3125,6 +3144,17 @@ SDValue NVPTXTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
31253144
if (VT == MVT::i1)
31263145
return LowerSTOREi1(Op, DAG);
31273146

3147+
// v2f32/v2f16/v2bf16/v2i16/v4i8 are legal, so we can't rely on legalizer to
3148+
// handle unaligned stores and have to handle it here.
3149+
if (NVPTX::isPackedVectorTy(VT) &&
3150+
!allowsMemoryAccessForAlignment(*DAG.getContext(), DAG.getDataLayout(),
3151+
VT, *Store->getMemOperand()))
3152+
return expandUnalignedStore(Store, DAG);
3153+
3154+
// v2f16/v2bf16/v2i16 don't need special handling.
3155+
if (NVPTX::isPackedVectorTy(VT) && VT.is32BitVector())
3156+
return SDValue();
3157+
31283158
// Lower store of any other vector type, including v2f32 as we want to break
31293159
// it apart since this is not a widely-supported type.
31303160
return LowerSTOREVector(Op, DAG);
@@ -3980,8 +4010,14 @@ bool NVPTXTargetLowering::getTgtMemIntrinsic(
39804010
case Intrinsic::nvvm_ldu_global_i:
39814011
case Intrinsic::nvvm_ldu_global_f:
39824012
case Intrinsic::nvvm_ldu_global_p: {
4013+
auto &DL = I.getDataLayout();
39834014
Info.opc = ISD::INTRINSIC_W_CHAIN;
3984-
Info.memVT = getValueType(I.getDataLayout(), I.getType());
4015+
if (Intrinsic == Intrinsic::nvvm_ldu_global_i)
4016+
Info.memVT = getValueType(DL, I.getType());
4017+
else if(Intrinsic == Intrinsic::nvvm_ldu_global_p)
4018+
Info.memVT = getPointerTy(DL);
4019+
else
4020+
Info.memVT = getValueType(DL, I.getType());
39854021
Info.ptrVal = I.getArgOperand(0);
39864022
Info.offset = 0;
39874023
Info.flags = MachineMemOperand::MOLoad;

llvm/lib/Target/NVPTX/NVPTXISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ class NVPTXTargetLowering : public TargetLowering {
309309
SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
310310

311311
SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const;
312+
SDValue LowerLOADi1(SDValue Op, SelectionDAG &DAG) const;
313+
312314
SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
313315
SDValue LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const;
314316
SDValue LowerSTOREVector(SDValue Op, SelectionDAG &DAG) const;

llvm/test/CodeGen/Mips/implicit-sret.ll

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ define internal void @test() unnamed_addr nounwind {
1919
; CHECK-NEXT: ld $6, 24($sp)
2020
; CHECK-NEXT: ld $5, 16($sp)
2121
; CHECK-NEXT: ld $7, 32($sp)
22-
; CHECK-NEXT: lw $4, 0($sp)
22+
; CHECK-NEXT: lw $1, 0($sp)
23+
; CHECK-NEXT: # implicit-def: $a0_64
24+
; CHECK-NEXT: move $4, $1
2325
; CHECK-NEXT: jal use_sret
2426
; CHECK-NEXT: nop
2527
; CHECK-NEXT: ld $ra, 56($sp) # 8-byte Folded Reload
@@ -62,9 +64,15 @@ define internal void @test2() unnamed_addr nounwind {
6264
; CHECK-NEXT: daddiu $4, $sp, 0
6365
; CHECK-NEXT: jal implicit_sret_decl2
6466
; CHECK-NEXT: nop
65-
; CHECK-NEXT: lw $6, 20($sp)
66-
; CHECK-NEXT: lw $5, 12($sp)
67-
; CHECK-NEXT: lw $4, 4($sp)
67+
; CHECK-NEXT: lw $1, 20($sp)
68+
; CHECK-NEXT: lw $2, 12($sp)
69+
; CHECK-NEXT: lw $3, 4($sp)
70+
; CHECK-NEXT: # implicit-def: $a0_64
71+
; CHECK-NEXT: move $4, $3
72+
; CHECK-NEXT: # implicit-def: $a1_64
73+
; CHECK-NEXT: move $5, $2
74+
; CHECK-NEXT: # implicit-def: $a2_64
75+
; CHECK-NEXT: move $6, $1
6876
; CHECK-NEXT: jal use_sret2
6977
; CHECK-NEXT: nop
7078
; CHECK-NEXT: ld $ra, 24($sp) # 8-byte Folded Reload

llvm/test/CodeGen/Mips/msa/basic_operations.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ define void @insert_v16i8_vidx(i32 signext %a) nounwind {
19041904
; N64-NEXT: daddu $1, $1, $25
19051905
; N64-NEXT: daddiu $1, $1, %lo(%neg(%gp_rel(insert_v16i8_vidx)))
19061906
; N64-NEXT: ld $2, %got_disp(i32)($1)
1907-
; N64-NEXT: lwu $2, 0($2)
1907+
; N64-NEXT: lw $2, 0($2)
19081908
; N64-NEXT: andi $2, $2, 15
19091909
; N64-NEXT: ld $1, %got_disp(v16i8)($1)
19101910
; N64-NEXT: daddu $1, $1, $2
@@ -1953,7 +1953,7 @@ define void @insert_v8i16_vidx(i32 signext %a) nounwind {
19531953
; N64-NEXT: daddu $1, $1, $25
19541954
; N64-NEXT: daddiu $1, $1, %lo(%neg(%gp_rel(insert_v8i16_vidx)))
19551955
; N64-NEXT: ld $2, %got_disp(i32)($1)
1956-
; N64-NEXT: lwu $2, 0($2)
1956+
; N64-NEXT: lw $2, 0($2)
19571957
; N64-NEXT: andi $2, $2, 7
19581958
; N64-NEXT: ld $1, %got_disp(v8i16)($1)
19591959
; N64-NEXT: dlsa $1, $2, $1, 1
@@ -2002,7 +2002,7 @@ define void @insert_v4i32_vidx(i32 signext %a) nounwind {
20022002
; N64-NEXT: daddu $1, $1, $25
20032003
; N64-NEXT: daddiu $1, $1, %lo(%neg(%gp_rel(insert_v4i32_vidx)))
20042004
; N64-NEXT: ld $2, %got_disp(i32)($1)
2005-
; N64-NEXT: lwu $2, 0($2)
2005+
; N64-NEXT: lw $2, 0($2)
20062006
; N64-NEXT: andi $2, $2, 3
20072007
; N64-NEXT: ld $1, %got_disp(v4i32)($1)
20082008
; N64-NEXT: dlsa $1, $2, $1, 2
@@ -2053,7 +2053,7 @@ define void @insert_v2i64_vidx(i64 signext %a) nounwind {
20532053
; N64-NEXT: daddu $1, $1, $25
20542054
; N64-NEXT: daddiu $1, $1, %lo(%neg(%gp_rel(insert_v2i64_vidx)))
20552055
; N64-NEXT: ld $2, %got_disp(i32)($1)
2056-
; N64-NEXT: lwu $2, 0($2)
2056+
; N64-NEXT: lw $2, 0($2)
20572057
; N64-NEXT: andi $2, $2, 1
20582058
; N64-NEXT: ld $1, %got_disp(v2i64)($1)
20592059
; N64-NEXT: dlsa $1, $2, $1, 3

llvm/test/CodeGen/NVPTX/bf16x2-instructions.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,11 @@ define <2 x bfloat> @test_copysign(<2 x bfloat> %a, <2 x bfloat> %b) #0 {
711711
; CHECK-NEXT: .reg .b32 %r<6>;
712712
; CHECK-EMPTY:
713713
; CHECK-NEXT: // %bb.0:
714-
; CHECK-NEXT: ld.param.b32 %r1, [test_copysign_param_1];
715-
; CHECK-NEXT: and.b32 %r2, %r1, -2147450880;
716-
; CHECK-NEXT: ld.param.b32 %r3, [test_copysign_param_0];
717-
; CHECK-NEXT: and.b32 %r4, %r3, 2147450879;
718-
; CHECK-NEXT: or.b32 %r5, %r4, %r2;
714+
; CHECK-NEXT: ld.param.b32 %r1, [test_copysign_param_0];
715+
; CHECK-NEXT: ld.param.b32 %r2, [test_copysign_param_1];
716+
; CHECK-NEXT: and.b32 %r3, %r2, -2147450880;
717+
; CHECK-NEXT: and.b32 %r4, %r1, 2147450879;
718+
; CHECK-NEXT: or.b32 %r5, %r4, %r3;
719719
; CHECK-NEXT: st.param.b32 [func_retval0], %r5;
720720
; CHECK-NEXT: ret;
721721
%r = call <2 x bfloat> @llvm.copysign.f16(<2 x bfloat> %a, <2 x bfloat> %b)

0 commit comments

Comments
 (0)