Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sitofp conversion to half-precision float generates single-precision float without truncating #27245

Closed
llvmbot opened this issue Mar 8, 2016 · 1 comment
Labels
bugzilla Issues migrated from bugzilla llvm:codegen

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 8, 2016

Bugzilla Link 26871
Resolution FIXED
Resolved on May 05, 2016 20:02
Version trunk
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @ahmedbougacha,@RKSimon

Extended Description

The following program converts -4095 (not precisely representable as half-precision float) to a half-precision float, once at runtime and once at compile time, and adds 1.0:

$ cat ../example.ll
define half @​foo(i32 %x) {
%r = sitofp i32 %x to half
%rr = fadd half %r, 1.0
ret half %rr
}

define i1 @​main() {
%x = sitofp i32 -4095 to half
%xx = fadd half %x, 1.0
%y = call half @​foo(i32 -4095)
%rr = fcmp oeq half %xx, %y
ret i1 %rr
}

One would expect the result to be true (1) but the program returns false (0):

$ bin/llc -filetype=obj ../example.ll && clang ../example.o -o a.out && ./a.out; echo $?
0

The reason seems to be that cvtsi2ssl is used to convert the integer to a single-precision float but then the addition is done without truncating the value to a half-precision float before extending it to a single-precision float for the addition (Note: the problem seems to appear on multiple architectures (x86, ARM, ...)):

$ bin/llc -o - ../example.ll
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 11
.globl _foo
.p2align 4, 0x90
_foo: ## @​foo
.cfi_startproc

BB#0:

pushq	%rax

Ltmp0:
.cfi_def_cfa_offset 16
cvtsi2ssl %edi, %xmm0
movss %xmm0, 4(%rsp) ## 4-byte Spill
movl $15360, %edi ## imm = 0x3C00
callq ___extendhfsf2
addss 4(%rsp), %xmm0 ## 4-byte Folded Reload
popq %rax
retq
.cfi_endproc

.globl	_main
.p2align	4, 0x90

_main: ## @​main
.cfi_startproc

BB#0:

pushq	%rax

Ltmp1:
.cfi_def_cfa_offset 16
movl $-4095, %edi ## imm = 0xFFFFFFFFFFFFF001
callq _foo
callq ___truncsfhf2
movzwl %ax, %edi
callq ___extendhfsf2
movss %xmm0, 4(%rsp) ## 4-byte Spill
movl $60416, %edi ## imm = 0xEC00
callq ___extendhfsf2
movss 4(%rsp), %xmm1 ## 4-byte Reload
## xmm1 = mem[0],zero,zero,zero
cmpeqss %xmm0, %xmm1
movd %xmm1, %eax
andl $1, %eax
popq %rcx
retq
.cfi_endproc

.subsections_via_symbols

@ahmedbougacha
Copy link
Member

Should be fixed by:
r268700 [CodeGen] Round [SU]INT_TO_FP result when promoting from f16.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:codegen
Projects
None yet
Development

No branches or pull requests

2 participants