[LLVMdev] __fp16 suport in llvm back-end

Wan, Xiaofei
Thu, 19 Jun 2014 07:49:56 -0000

Hi, all:

I am trying to test half float point support in llvm, I found clang can generate
bitcode for __fp16, while llc can't generate code for it, the error message
is like this

LLVM ERROR: Cannot select: 0x26a68e0: i16 = fp32_to_fp16 0x26a67d8 [ORD=2]
[ID=4]
  0x26a67d8: f32,ch = CopyFromReg 0x2693060, 0x26a66d0 [ORD=2] [ID=3]
    0x26a66d0: f32 = Register %vreg1 [ID=1]
In function: test

Anyone know what is the problem; I just suspect intrinsic "
llvm.convert.to.fp16" is not well implemented?

Attaching my test:

#test.c  clang -cc1 -O0 test.c -emit-llvm -o test.bc
typedef __fp16 half;
void test()
{
    half x = 0.1f;
    x += 2.0f;
    half y = x + x;
}

The generated bitcode:

llc -O0 test.bc

; ModuleID = 'test.c'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind
define void @test() #0 {
  %x = alloca i16, align 2
  %y = alloca i16, align 2
  %1 = call i16 @llvm.convert.to.fp16(float 0x3FB99999A0000000)
  store i16 %1, i16* %x, align 2
  %2 = load i16* %x, align 2
  %3 = call float @llvm.convert.from.fp16(i16 %2)
  %4 = fadd float %3, 2.000000e+00
  %5 = call i16 @llvm.convert.to.fp16(float %4)
  store i16 %5, i16* %x, align 2
  %6 = load i16* %x, align 2
  %7 = call float @llvm.convert.from.fp16(i16 %6)
  %8 = load i16* %x, align 2
  %9 = call float @llvm.convert.from.fp16(i16 %8)
  %10 = fadd float %7, %9
  %11 = call i16 @llvm.convert.to.fp16(float %10)
  store i16 %11, i16* %y, align 2
  ret void
}

; Function Attrs: nounwind readnone
declare i16 @llvm.convert.to.fp16(float) #1

; Function Attrs: nounwind readnone
declare float @llvm.convert.from.fp16(i16) #1

attributes #0 = { nounwind "less-precise-fpmad"="false"
"no-frame-pointer-elim"="false"
"no-infs-fp-math"="false"
"no-nans-fp-math"="false" "no-realign-stack"
"stack-protector-buffer-size"="8"
"unsafe-fp-math"="false"
"use-soft-float"="false" }
attributes #1 = { nounwind readnone }

!llvm.ident = !{!0}

!0 = metadata !{metadata !"clang version 3.5.0 (trunk 211249)"}

Thanks
Wan Xiaofei