[PATCH] D81999: libclc: Fix rounding during type conversion

Daniel Stone via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 03:13:01 PDT 2020


daniels created this revision.
daniels added reviewers: jvesely, tstellar.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
daniels added a comment.

I don't have a good way to test this, but tbh I don't think anyone else does either, given it came from POCL so has been broken for as long as libclc's had any support for it. Looking into POCL just now, it looks like they applied the same fix <https://github.com/pocl/pocl/commit/fc9d276b266f5abe7a5d7715f8c6018a3865ed2f#diff-a3e10b0030c6b350f3a77976c0e72f9a> a few years ago. There are other fixes as well which might be good to take up, but I'm not going to propose any invasive changes, since SPIR-V doesn't (can't) use these helpers at all: the LLVM-SPIRV-Translator specifically looks for calls to the `convert_*` functions and replaces them with equivalently-decorated SPIR-V `OpConvert*`.


The rounding during type conversion uses multiple conversions, selecting
between them to try to discover if rounding occurred. This appears to
not have been tested, since it would generate code of the form:

  float convert_float_rtp(char x)
  {
    float r = convert_float(x);
    char y = convert_char(y);
    [...]
  }

which will access uninitialised data. The idea appears to have been to
have done a char -> float -> char roundtrip in order to discover the
rounding, so do this.

Discovered by inspection.

Signed-off-by: Daniel Stone <daniels at collabora.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81999

Files:
  libclc/generic/lib/gen_convert.py


Index: libclc/generic/lib/gen_convert.py
===================================================================
--- libclc/generic/lib/gen_convert.py
+++ libclc/generic/lib/gen_convert.py
@@ -357,7 +357,7 @@
     print("  return convert_{DST}{N}(x);".format(DST=dst, N=size))
   else:
     print("  {DST}{N} r = convert_{DST}{N}(x);".format(DST=dst, N=size))
-    print("  {SRC}{N} y = convert_{SRC}{N}(y);".format(SRC=src, N=size))
+    print("  {SRC}{N} y = convert_{SRC}{N}(r);".format(SRC=src, N=size))
     if mode == '_rtz':
       if src in int_types:
         print("  {USRC}{N} abs_x = abs(x);".format(USRC=unsigned_type[src], N=size))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81999.271317.patch
Type: text/x-patch
Size: 642 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200617/bf5ac2cd/attachment.bin>


More information about the llvm-commits mailing list