Skip to content

Commit c727b48

Browse files
authored
[SDAG][ISel][TableGen][LoongArch] Report error for trivial bitcasts when there are predicate calls (llvm#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 llvm#116008.
1 parent 9e0ea8c commit c727b48

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
@@ -74,8 +74,7 @@ class VecCond<SDPatternOperator OpNode, ValueType TyNode,
7474
let usesCustomInserter = 1;
7575
}
7676

77-
def vsplat_imm_eq_1 : PatFrags<(ops), [(build_vector),
78-
(bitconvert (v4i32 (build_vector)))], [{
77+
def vsplat_imm_eq_1 : PatFrags<(ops), [(build_vector)], [{
7978
APInt Imm;
8079
EVT EltTy = N->getValueType(0).getVectorElementType();
8180

@@ -116,8 +115,7 @@ def vsplati32_imm_eq_31 : PatFrags<(ops), [(build_vector)], [{
116115
return selectVSplat(N, Imm, EltTy.getSizeInBits()) &&
117116
Imm.getBitWidth() == EltTy.getSizeInBits() && Imm == 31;
118117
}]>;
119-
def vsplati64_imm_eq_63 : PatFrags<(ops), [(build_vector),
120-
(bitconvert (v4i32 (build_vector)))], [{
118+
def vsplati64_imm_eq_63 : PatFrags<(ops), [(build_vector)], [{
121119
APInt Imm;
122120
EVT EltTy = N->getValueType(0).getVectorElementType();
123121

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
@@ -3056,6 +3056,14 @@ static bool SimplifyTree(TreePatternNodePtr &N) {
30563056
!N->getExtType(0).empty() &&
30573057
N->getExtType(0) == N->getChild(0).getExtType(0) &&
30583058
N->getName().empty()) {
3059+
if (!N->getPredicateCalls().empty()) {
3060+
std::string Str;
3061+
raw_string_ostream OS(Str);
3062+
OS << *N
3063+
<< "\n trivial bitconvert node should not have predicate calls\n";
3064+
PrintFatalError(Str);
3065+
return false;
3066+
}
30593067
N = N->getChildShared(0);
30603068
SimplifyTree(N);
30613069
return true;

0 commit comments

Comments
 (0)