Skip to content

Commit 3d12f45

Browse files
dtcxzywtru
authored andcommitted
[SDAG][ISel][TableGen][LoongArch] Report error for trivial bitcasts when there are predicate calls (#116075)
On loongarch64 with lsx extension, we select `VBITREV_W` for `v4i32 (xor X, (shl splat(1), Y))`: https://github.com/llvm/llvm-project/blob/8e6630391699116641cf390a10476295b7d4b95c/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td#L1583-L1584 And `vsplat_imm_eq_1` is defined as: https://github.com/llvm/llvm-project/blob/8e6630391699116641cf390a10476295b7d4b95c/llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td#L77-L87 For the `(bitconvert (v4i32 (build_vector)))` case, the pattern is expected to be: ``` PATTERN: (xor:{ *:[v4i32] } v4i32:{ *:[v4i32] }:$vj, (shl:{ *:[v4i32] } (bitconvert:{ *:[v4i32] } (build_vector:{ *:[v4i32] }))<<P:Predicate_vsplat_imm_eq_1>>, v4i32:{ *:[v4i32] }:$vk)) RESULT: (VBITREV_W:{ *:[v4i32] } v4i32:{ *:[v4i32] }:$vj, v4i32:{ *:[v4i32] }:$vk) ``` However, `simplifyTree` drops the `bitconvert` node and its predicates: https://github.com/llvm/llvm-project/blob/8e6630391699116641cf390a10476295b7d4b95c/llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp#L3036-L3062 Then llvm will match `vsplat_imm_eq_1` for any v4i32 splats and cause a miscompilation: ``` PATTERN: (xor:{ *:[v4i32] } v4i32:{ *:[v4i32] }:$vj, (shl:{ *:[v4i32] } (build_vector:{ *:[v4i32] }), v4i32:{ *:[v4i32] }:$vk)) RESULT: (VBITREV_W:{ *:[v4i32] } v4i32:{ *:[v4i32] }:$vj, v4i32:{ *:[v4i32] }:$vk) ``` This patch adds additional checks for predicates associated with the trivial bitconvert node. Unused patterns in the LoongArch target are also removed. Fixes #116008. (cherry picked from commit c727b48)
1 parent ea96040 commit 3d12f45

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

llvm/lib/Target/LoongArch/LoongArchLSXInstrInfo.td

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class VecCond<SDPatternOperator OpNode, ValueType TyNode,
6767
let usesCustomInserter = 1;
6868
}
6969

70-
def vsplat_imm_eq_1 : PatFrags<(ops), [(build_vector),
71-
(bitconvert (v4i32 (build_vector)))], [{
70+
def vsplat_imm_eq_1 : PatFrags<(ops), [(build_vector)], [{
7271
APInt Imm;
7372
EVT EltTy = N->getValueType(0).getVectorElementType();
7473

@@ -109,8 +108,7 @@ def vsplati32_imm_eq_31 : PatFrags<(ops), [(build_vector)], [{
109108
return selectVSplat(N, Imm, EltTy.getSizeInBits()) &&
110109
Imm.getBitWidth() == EltTy.getSizeInBits() && Imm == 31;
111110
}]>;
112-
def vsplati64_imm_eq_63 : PatFrags<(ops), [(build_vector),
113-
(bitconvert (v4i32 (build_vector)))], [{
111+
def vsplati64_imm_eq_63 : PatFrags<(ops), [(build_vector)], [{
114112
APInt Imm;
115113
EVT EltTy = N->getValueType(0).getVectorElementType();
116114

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc --mtriple=loongarch64 --mattr=+lsx < %s | FileCheck %s
3+
4+
define <4 x i32> @xor_shl_splat_vec_one(i32 %x, <4 x i32> %y) nounwind {
5+
; CHECK-LABEL: xor_shl_splat_vec_one:
6+
; CHECK: # %bb.0: # %entry
7+
; CHECK-NEXT: vreplgr2vr.w $vr1, $a0
8+
; CHECK-NEXT: vsll.w $vr0, $vr1, $vr0
9+
; CHECK-NEXT: vbitrevi.w $vr0, $vr0, 0
10+
; CHECK-NEXT: ret
11+
entry:
12+
%ins = insertelement <4 x i32> poison, i32 %x, i64 0
13+
%splat = shufflevector <4 x i32> %ins, <4 x i32> poison, <4 x i32> zeroinitializer
14+
%shl = shl <4 x i32> %splat, %y
15+
%xor = xor <4 x i32> %shl, splat (i32 1)
16+
ret <4 x i32> %xor
17+
}

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,14 @@ static bool SimplifyTree(TreePatternNodePtr &N) {
30423042
!N->getExtType(0).empty() &&
30433043
N->getExtType(0) == N->getChild(0).getExtType(0) &&
30443044
N->getName().empty()) {
3045+
if (!N->getPredicateCalls().empty()) {
3046+
std::string Str;
3047+
raw_string_ostream OS(Str);
3048+
OS << *N
3049+
<< "\n trivial bitconvert node should not have predicate calls\n";
3050+
PrintFatalError(Str);
3051+
return false;
3052+
}
30453053
N = N->getChildShared(0);
30463054
SimplifyTree(N);
30473055
return true;

0 commit comments

Comments
 (0)