Skip to content

Commit

Permalink
[ExpandLargeFpConvert] Fix incorrect values in fp-to-int conversion. (#…
Browse files Browse the repository at this point in the history
…86514)

The IR for a double-to-i129 conversion looks like this in one of the
blocks in compiler-rt:

  %cmp5.i = icmp ult i16 %3, -129, !dbg !24

But in ExpandLargeFpConvert, it looks like:

  %13 = icmp ult i129 %12, 4294967167, !dbg !19

ExpandLargeFpConvert is wrong; the value should have been
signed before negating, but instead we get a very large
unsigned value. Another value in the same pass also has this
issue.
  • Loading branch information
bevin-hansson committed Mar 26, 2024
1 parent 6a6f9bf commit 14c3018
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 225 deletions.
11 changes: 6 additions & 5 deletions llvm/lib/CodeGen/ExpandLargeFpConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ static void expandFPToI(Instruction *FPToI) {
// if.end:
Builder.SetInsertPoint(IfEnd);
Value *Add1 = Builder.CreateAdd(
And2, ConstantInt::getSigned(IntTy, -int64_t(ExponentBias + BitWidth)));
Value *Cmp3 =
Builder.CreateICmpULT(Add1, ConstantInt::getSigned(IntTy, -BitWidth));
And2, ConstantInt::getSigned(
IntTy, -static_cast<int64_t>(ExponentBias + BitWidth)));
Value *Cmp3 = Builder.CreateICmpULT(
Add1, ConstantInt::getSigned(IntTy, -static_cast<int64_t>(BitWidth)));
Builder.CreateCondBr(Cmp3, IfThen5, IfEnd9);

// if.then5:
Expand All @@ -203,8 +204,8 @@ static void expandFPToI(Instruction *FPToI) {
// if.else:
Builder.SetInsertPoint(IfElse);
Value *Sub15 = Builder.CreateAdd(
And2,
ConstantInt::getSigned(IntTy, -(ExponentBias + FPMantissaWidth)));
And2, ConstantInt::getSigned(
IntTy, -static_cast<int64_t>(ExponentBias + FPMantissaWidth)));
Value *Shl = Builder.CreateShl(Or, Sub15);
Value *Mul16 = Builder.CreateMul(Shl, Sign);
Builder.CreateBr(End);
Expand Down

0 comments on commit 14c3018

Please sign in to comment.