Skip to content

Commit 66a3e4f

Browse files
committed
[LoongArch] Override TargetLowering::isShuffleMaskLegal
By default, `isShuffleMaskLegal` always returns true, which can result in the expansion of `BUILD_VECTOR` into a `VECTOR_SHUFFLE` node in certain situations. Subsequently, the `VECTOR_SHUFFLE` node is expanded again into a `BUILD_VECTOR`, leading to an infinite loop. To address this, we always return false, allowing the expansion of `BUILD_VECTOR` through the stack.
1 parent 19e068b commit 66a3e4f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
248248
setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
249249
}
250250
for (MVT VT : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64}) {
251+
setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
251252
setOperationAction({ISD::ADD, ISD::SUB}, VT, Legal);
252253
setOperationAction({ISD::UMAX, ISD::UMIN, ISD::SMAX, ISD::SMIN}, VT,
253254
Legal);
@@ -277,6 +278,7 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,
277278
setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
278279
}
279280
for (MVT VT : {MVT::v4i64, MVT::v8i32, MVT::v16i16, MVT::v32i8}) {
281+
setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
280282
setOperationAction({ISD::ADD, ISD::SUB}, VT, Legal);
281283
setOperationAction({ISD::UMAX, ISD::UMIN, ISD::SMAX, ISD::SMIN}, VT,
282284
Legal);
@@ -381,10 +383,18 @@ SDValue LoongArchTargetLowering::LowerOperation(SDValue Op,
381383
return lowerINSERT_VECTOR_ELT(Op, DAG);
382384
case ISD::BUILD_VECTOR:
383385
return lowerBUILD_VECTOR(Op, DAG);
386+
case ISD::VECTOR_SHUFFLE:
387+
return lowerVECTOR_SHUFFLE(Op, DAG);
384388
}
385389
return SDValue();
386390
}
387391

392+
SDValue LoongArchTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
393+
SelectionDAG &DAG) const {
394+
// TODO: custom shuffle.
395+
return SDValue();
396+
}
397+
388398
static bool isConstantOrUndef(const SDValue Op) {
389399
if (Op->isUndef())
390400
return true;

llvm/lib/Target/LoongArch/LoongArchISelLowering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class LoongArchTargetLowering : public TargetLowering {
230230
MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
231231
unsigned *Fast = nullptr) const override;
232232

233+
bool isShuffleMaskLegal(ArrayRef<int> Mask, EVT VT) const override {
234+
return false;
235+
}
236+
233237
private:
234238
/// Target-specific function used to lower LoongArch calling conventions.
235239
typedef bool LoongArchCCAssignFn(const DataLayout &DL, LoongArchABI::ABI ABI,
@@ -278,6 +282,7 @@ class LoongArchTargetLowering : public TargetLowering {
278282
SDValue lowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) const;
279283
SDValue lowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
280284
SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
285+
SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
281286

282287
bool isFPImmLegal(const APFloat &Imm, EVT VT,
283288
bool ForCodeSize) const override;

llvm/test/CodeGen/LoongArch/lsx/build-vector.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,25 @@ entry:
374374
store <2 x double> %ins1, ptr %dst
375375
ret void
376376
}
377+
378+
;; BUILD_VECTOR through stack.
379+
;; If `isShuffleMaskLegal` returns true, it will lead to an infinite loop.
380+
define void @extract1_i32_zext_insert0_i64_undef(ptr %src, ptr %dst) nounwind {
381+
; CHECK-LABEL: extract1_i32_zext_insert0_i64_undef:
382+
; CHECK: # %bb.0:
383+
; CHECK-NEXT: addi.d $sp, $sp, -16
384+
; CHECK-NEXT: vld $vr0, $a0, 0
385+
; CHECK-NEXT: vpickve2gr.w $a0, $vr0, 1
386+
; CHECK-NEXT: bstrpick.d $a0, $a0, 31, 0
387+
; CHECK-NEXT: st.d $a0, $sp, 0
388+
; CHECK-NEXT: vld $vr0, $sp, 0
389+
; CHECK-NEXT: vst $vr0, $a1, 0
390+
; CHECK-NEXT: addi.d $sp, $sp, 16
391+
; CHECK-NEXT: ret
392+
%v = load volatile <4 x i32>, ptr %src
393+
%e = extractelement <4 x i32> %v, i32 1
394+
%z = zext i32 %e to i64
395+
%r = insertelement <2 x i64> undef, i64 %z, i32 0
396+
store <2 x i64> %r, ptr %dst
397+
ret void
398+
}

0 commit comments

Comments
 (0)