Skip to content

Commit a644666

Browse files
Manolis Tsamisptomsich
authored andcommitted
[RISCV] XTHeadMemPair: Fix invalid mempair combine for types other than i32/i64
A mistake in the control flow of performMemPairCombine resulted in paired loads/stores for types that were not supported by the instructions (i8/i16). These loads/stores could not match the constraints of the patterns defined in the THead td file and the compiler would throw a 'Cannot select' error. This is now fixed and two new test functions have been added in xtheadmempair.ll which would previously crash the compiler. The compiler was additionally tested with a wide range of benchmarks and no issues were observed. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D144559
1 parent d0d6d78 commit a644666

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9783,16 +9783,20 @@ static SDValue performMemPairCombine(SDNode *N,
97839783
continue;
97849784

97859785
// Check if the offsets match the XTHeadMemPair encoding contraints.
9786+
bool Valid = false;
97869787
if (MemVT == MVT::i32) {
97879788
// Check for adjacent i32 values and a 2-bit index.
9788-
if ((Offset1 + 4 != Offset2) || !isShiftedUInt<2, 3>(Offset1))
9789-
continue;
9789+
if ((Offset1 + 4 == Offset2) && isShiftedUInt<2, 3>(Offset1))
9790+
Valid = true;
97909791
} else if (MemVT == MVT::i64) {
97919792
// Check for adjacent i64 values and a 2-bit index.
9792-
if ((Offset1 + 8 != Offset2) || !isShiftedUInt<2, 4>(Offset1))
9793-
continue;
9793+
if ((Offset1 + 8 == Offset2) && isShiftedUInt<2, 4>(Offset1))
9794+
Valid = true;
97949795
}
97959796

9797+
if (!Valid)
9798+
continue;
9799+
97969800
// Try to combine.
97979801
if (SDValue Res =
97989802
tryMemPairCombine(DAG, LSNode1, LSNode2, Base1, Offset1))

llvm/test/CodeGen/RISCV/xtheadmempair.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,51 @@ define void @sd128(i128* %a, i128 %b) {
293293
store i128 %b, i128* %1, align 8
294294
ret void
295295
}
296+
297+
define i32 @lh(i16* %a) {
298+
; RV32XTHEADMEMPAIR-LABEL: lh:
299+
; RV32XTHEADMEMPAIR: # %bb.0:
300+
; RV32XTHEADMEMPAIR-NEXT: lh a1, 0(a0)
301+
; RV32XTHEADMEMPAIR-NEXT: lh a0, 2(a0)
302+
; RV32XTHEADMEMPAIR-NEXT: add a0, a1, a0
303+
; RV32XTHEADMEMPAIR-NEXT: ret
304+
;
305+
; RV64XTHEADMEMPAIR-LABEL: lh:
306+
; RV64XTHEADMEMPAIR: # %bb.0:
307+
; RV64XTHEADMEMPAIR-NEXT: lh a1, 0(a0)
308+
; RV64XTHEADMEMPAIR-NEXT: lh a0, 2(a0)
309+
; RV64XTHEADMEMPAIR-NEXT: add a0, a1, a0
310+
; RV64XTHEADMEMPAIR-NEXT: ret
311+
%1 = getelementptr i16, i16* %a, i64 0
312+
%2 = load i16, i16* %1, align 4
313+
%3 = getelementptr i16, i16* %a, i64 1
314+
%4 = load i16, i16* %3, align 4
315+
%5 = sext i16 %2 to i32
316+
%6 = sext i16 %4 to i32
317+
%7 = add i32 %5, %6
318+
ret i32 %7
319+
}
320+
321+
define i32 @lb(i8* %a) {
322+
; RV32XTHEADMEMPAIR-LABEL: lb:
323+
; RV32XTHEADMEMPAIR: # %bb.0:
324+
; RV32XTHEADMEMPAIR-NEXT: lb a1, 0(a0)
325+
; RV32XTHEADMEMPAIR-NEXT: lb a0, 1(a0)
326+
; RV32XTHEADMEMPAIR-NEXT: add a0, a1, a0
327+
; RV32XTHEADMEMPAIR-NEXT: ret
328+
;
329+
; RV64XTHEADMEMPAIR-LABEL: lb:
330+
; RV64XTHEADMEMPAIR: # %bb.0:
331+
; RV64XTHEADMEMPAIR-NEXT: lb a1, 0(a0)
332+
; RV64XTHEADMEMPAIR-NEXT: lb a0, 1(a0)
333+
; RV64XTHEADMEMPAIR-NEXT: add a0, a1, a0
334+
; RV64XTHEADMEMPAIR-NEXT: ret
335+
%1 = getelementptr i8, i8* %a, i64 0
336+
%2 = load i8, i8* %1, align 4
337+
%3 = getelementptr i8, i8* %a, i64 1
338+
%4 = load i8, i8* %3, align 4
339+
%5 = sext i8 %2 to i32
340+
%6 = sext i8 %4 to i32
341+
%7 = add i32 %5, %6
342+
ret i32 %7
343+
}

0 commit comments

Comments
 (0)