Skip to content

Commit 58d8805

Browse files
authored
[RISCV] Always use signed APSInt in getExactInteger. (#84070)
We were setting based on whether the FP value is positive/negative, but we really want to know whether the resulting integer will be treated as a signed or unsigned value. Since we use SINT_TO_FP to convert the integer to FP, we should always used signed here. Without this we convert +2147483648.0 to an integer 0x80000000 and convert it using sint_to_fp which produces -2147483648.0.
1 parent e96c0c1 commit 58d8805

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3233,7 +3233,9 @@ struct VIDSequence {
32333233

32343234
static std::optional<uint64_t> getExactInteger(const APFloat &APF,
32353235
uint32_t BitWidth) {
3236-
APSInt ValInt(BitWidth, !APF.isNegative());
3236+
// We will use a SINT_TO_FP to materialize this constant so we should use a
3237+
// signed APSInt here.
3238+
APSInt ValInt(BitWidth, /*IsUnsigned*/ false);
32373239
// We use an arbitrary rounding mode here. If a floating-point is an exact
32383240
// integer (e.g., 1.0), the rounding mode does not affect the output value. If
32393241
// the rounding mode changes the output value, then it is not an exact

llvm/test/CodeGen/RISCV/rvv/vle_vid-vfcvt.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,15 @@ entry:
113113
ret void
114114
}
115115

116-
; FIXME: This is miscompiled. This will create -2147483648.0 instead of
117-
; 2147483648.0 for the 4th element.
116+
; Make sure we don't try to use vid+vsll+vfcvt. We previously flipped the sign
117+
; of 2147483648.0.
118118
define void @foo_9(ptr nocapture noundef writeonly %t) {
119119
; CHECK-LABEL: foo_9:
120120
; CHECK: # %bb.0: # %entry
121+
; CHECK-NEXT: lui a1, %hi(.LCPI8_0)
122+
; CHECK-NEXT: addi a1, a1, %lo(.LCPI8_0)
121123
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
122-
; CHECK-NEXT: vid.v v8
123-
; CHECK-NEXT: vsll.vi v8, v8, 31
124-
; CHECK-NEXT: vrsub.vi v8, v8, 0
125-
; CHECK-NEXT: vfcvt.f.x.v v8, v8
124+
; CHECK-NEXT: vle32.v v8, (a1)
126125
; CHECK-NEXT: vse32.v v8, (a0)
127126
; CHECK-NEXT: ret
128127
entry:

0 commit comments

Comments
 (0)