[llvm-bugs] [Bug 26871] New: sitofp conversion to half-precision float generates single-precision float without truncating

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Mar 7 16:02:44 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26871

            Bug ID: 26871
           Summary: sitofp conversion to half-precision float generates
                    single-precision float without truncating
           Product: libraries
           Version: trunk
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: andres.noetzli at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160308/39cbcd5a/attachment.html>


More information about the llvm-bugs mailing list