Skip to content

Commit e005a09

Browse files
authored
[RISCV][TypePromotion] Dont generate truncs if PromotedType is greater than Source Type (#86941)
We currently check if the source and promoted types are not equal before generating truncate instructions. This does not work for RV64 where the promoted type is i64 and this lead to a crash due to the generation of truncate instructions from i32 to i64. Fixes #86400
1 parent c64a328 commit e005a09

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

llvm/lib/CodeGen/TypePromotion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ void IRPromoter::ConvertTruncs() {
643643
ConstantInt *Mask =
644644
ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue());
645645
Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask);
646-
if (SrcTy != ExtTy)
646+
if (SrcTy->getBitWidth() > ExtTy->getBitWidth())
647647
Masked = Builder.CreateTrunc(Masked, ExtTy);
648648

649649
if (auto *I = dyn_cast<Instruction>(Masked))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -mtriple=riscv64 -passes=typepromotion -S %s | FileCheck %s
3+
4+
; Test that this does not crash
5+
define i16 @test(i8 %a, i32 %b) {
6+
; CHECK-LABEL: define i16 @test(
7+
; CHECK-SAME: i8 [[A:%.*]], i32 [[B:%.*]]) {
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[A]] to i32
10+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[B]] to i16
11+
; CHECK-NEXT: [[TMP2:%.*]] = zext i16 [[TMP1]] to i64
12+
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[TMP2]], 0
13+
; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 255
14+
; CHECK-NEXT: [[TMP5:%.*]] = zext i32 [[TMP4]] to i64
15+
; CHECK-NEXT: [[TMP6:%.*]] = xor i64 [[TMP5]], [[TMP2]]
16+
; CHECK-NEXT: [[TMP7:%.*]] = trunc i64 [[TMP6]] to i16
17+
; CHECK-NEXT: ret i16 [[TMP7]]
18+
;
19+
entry:
20+
%0 = zext i8 %a to i32
21+
%1 = trunc i32 %b to i16
22+
%2 = icmp eq i16 %1, 0
23+
%3 = trunc i32 %0 to i8
24+
%4 = zext i8 %3 to i16
25+
%5 = xor i16 %4, %1
26+
ret i16 %5
27+
}

0 commit comments

Comments
 (0)