[llvm-commits] [llvm] r157393 - in /llvm/trunk: docs/BitCodeFormat.html docs/LangRef.html lib/AsmParser/LLLexer.cpp lib/VMCore/AsmWriter.cpp test/Assembler/half-constprop.ll test/Assembler/half-conv.ll test/Assembler/half.ll

Tobias Grosser grosser at fim.uni-passau.de
Thu May 24 08:59:07 PDT 2012


Author: grosser
Date: Thu May 24 10:59:06 2012
New Revision: 157393

URL: http://llvm.org/viewvc/llvm-project?rev=157393&view=rev
Log:
Add half support to LLVM (for OpenCL)

Submitted by: Anton Lokhmotov  <Anton.Lokhmotov at arm.com>

Approved by: o Anton Korobeynikov
             o Micah Villmow
             o David Neto

Added:
    llvm/trunk/test/Assembler/half-constprop.ll
    llvm/trunk/test/Assembler/half-conv.ll
    llvm/trunk/test/Assembler/half.ll
Modified:
    llvm/trunk/docs/BitCodeFormat.html
    llvm/trunk/docs/LangRef.html
    llvm/trunk/lib/AsmParser/LLLexer.cpp
    llvm/trunk/lib/VMCore/AsmWriter.cpp

Modified: llvm/trunk/docs/BitCodeFormat.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/BitCodeFormat.html?rev=157393&r1=157392&r2=157393&view=diff
==============================================================================
--- llvm/trunk/docs/BitCodeFormat.html (original)
+++ llvm/trunk/docs/BitCodeFormat.html Thu May 24 10:59:06 2012
@@ -1145,6 +1145,18 @@
 </div>
 
 <!-- _______________________________________________________________________ -->
+<h4><a name="TYPE_CODE_HALF">TYPE_CODE_HALF Record</a></h4>
+
+<div>
+
+<p><tt>[HALF]</tt></p>
+
+<p>The <tt>HALF</tt> record (code 10) adds a <tt>half</tt> (16-bit
+floating point) type to the type table.
+</p>
+</div>
+
+<!-- _______________________________________________________________________ -->
 <h4><a name="TYPE_CODE_FLOAT">TYPE_CODE_FLOAT Record</a></h4>
 
 <div>

Modified: llvm/trunk/docs/LangRef.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.html?rev=157393&r1=157392&r2=157393&view=diff
==============================================================================
--- llvm/trunk/docs/LangRef.html (original)
+++ llvm/trunk/docs/LangRef.html Thu May 24 10:59:06 2012
@@ -2289,8 +2289,9 @@
    by <tt>0xM</tt> followed by 32 hexadecimal digits.  The IEEE 128-bit format
    is represented by <tt>0xL</tt> followed by 32 hexadecimal digits; no
    currently supported target uses this format.  Long doubles will only work if
-   they match the long double format on your target.  All hexadecimal formats
-   are big-endian (sign bit at the left).</p>
+   they match the long double format on your target. The IEEE 16-bit format
+   (half precision) is represented by <tt>0xH</tt> followed by 4 hexadecimal
+   digits. All hexadecimal formats are big-endian (sign bit at the left).</p>
 
 <p>There are no constants of type x86mmx.</p>
 </div>
@@ -7947,7 +7948,8 @@
 
 <div>
 
-<p>Half precision floating point is a storage-only format. This means that it is
+<p>For most target platforms, half precision floating point is a storage-only
+   format. This means that it is
    a dense encoding (in memory) but does not support computation in the
    format.</p>
    

Modified: llvm/trunk/lib/AsmParser/LLLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.cpp?rev=157393&r1=157392&r2=157393&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLLexer.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLLexer.cpp Thu May 24 10:59:06 2012
@@ -673,11 +673,12 @@
 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
+///    HexHalfConstant   0xH[0-9A-Fa-f]+
 lltok::Kind LLLexer::Lex0x() {
   CurPtr = TokStart + 2;
 
   char Kind;
-  if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
+  if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M' || CurPtr[0] == 'H') {
     Kind = *CurPtr++;
   } else {
     Kind = 'J';
@@ -718,6 +719,9 @@
     HexToIntPair(TokStart+3, CurPtr, Pair);
     APFloatVal = APFloat(APInt(128, Pair));
     return lltok::APFloat;
+  case 'H':
+    APFloatVal = APFloat(APInt(16,HexIntToVal(TokStart+3, CurPtr)));
+    return lltok::APFloat;
   }
 }
 

Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=157393&r1=157392&r2=157393&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Thu May 24 10:59:06 2012
@@ -708,8 +708,7 @@
   }
 
   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
-    if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf ||
-        &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
+    if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
         &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
       // We would like to output the FP constant value in exponential notation,
       // but we cannot do this if doing so will lose precision.  Check here to
@@ -759,16 +758,20 @@
       return;
     }
 
-    // Some form of long double.  These appear as a magic letter identifying
-    // the type, then a fixed number of hex digits.
+    // Either half, or some form of long double.
+    // These appear as a magic letter identifying the type, then a
+    // fixed number of hex digits.
     Out << "0x";
+    // Bit position, in the current word, of the next nibble to print.
+    int shiftcount;
+
     if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
       Out << 'K';
       // api needed to prevent premature destruction
       APInt api = CFP->getValueAPF().bitcastToAPInt();
       const uint64_t* p = api.getRawData();
       uint64_t word = p[1];
-      int shiftcount=12;
+      shiftcount = 12;
       int width = api.getBitWidth();
       for (int j=0; j<width; j+=4, shiftcount-=4) {
         unsigned int nibble = (word>>shiftcount) & 15;
@@ -784,17 +787,21 @@
         }
       }
       return;
-    } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
+    } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
+      shiftcount = 60;
       Out << 'L';
-    else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
+    } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
+      shiftcount = 60;
       Out << 'M';
-    else
+    } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
+      shiftcount = 12;
+      Out << 'H';
+    } else
       llvm_unreachable("Unsupported floating point type");
     // api needed to prevent premature destruction
     APInt api = CFP->getValueAPF().bitcastToAPInt();
     const uint64_t* p = api.getRawData();
     uint64_t word = *p;
-    int shiftcount=60;
     int width = api.getBitWidth();
     for (int j=0; j<width; j+=4, shiftcount-=4) {
       unsigned int nibble = (word>>shiftcount) & 15;

Added: llvm/trunk/test/Assembler/half-constprop.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/half-constprop.ll?rev=157393&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/half-constprop.ll (added)
+++ llvm/trunk/test/Assembler/half-constprop.ll Thu May 24 10:59:06 2012
@@ -0,0 +1,17 @@
+; RUN: llvm-as < %s | opt -O3 | llvm-dis | FileCheck %s
+; Testing half constant propagation.
+
+define half @abc() nounwind {
+entry:
+  %a = alloca half, align 2
+  %b = alloca half, align 2
+  %.compoundliteral = alloca float, align 4
+  store half 0xH4200, half* %a, align 2
+  store half 0xH4B9A, half* %b, align 2
+  %tmp = load half* %a, align 2
+  %tmp1 = load half* %b, align 2
+  %add = fadd half %tmp, %tmp1
+; CHECK: 0xH4C8D
+  ret half %add
+}
+

Added: llvm/trunk/test/Assembler/half-conv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/half-conv.ll?rev=157393&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/half-conv.ll (added)
+++ llvm/trunk/test/Assembler/half-conv.ll Thu May 24 10:59:06 2012
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s | opt -O3 | llvm-dis | FileCheck %s
+; Testing half to float conversion.
+
+define float @abc() nounwind {
+entry:
+  %a = alloca half, align 2
+  %.compoundliteral = alloca float, align 4
+  store half 0xH4C8D, half* %a, align 2
+  %tmp = load half* %a, align 2
+  %conv = fpext half %tmp to float
+; CHECK: 0x4032340000000000
+  ret float %conv
+}

Added: llvm/trunk/test/Assembler/half.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/half.ll?rev=157393&view=auto
==============================================================================
--- llvm/trunk/test/Assembler/half.ll (added)
+++ llvm/trunk/test/Assembler/half.ll Thu May 24 10:59:06 2012
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; Basic smoke test for half type.
+
+; CHECK: define half @halftest
+define half  @halftest(half %A0) {
+; CHECK: ret half %A0
+        ret half %A0
+}





More information about the llvm-commits mailing list