SPIR - Built-ins and Name Mangling discussion

Hi All,

In this thread we would like to review the built-ins and name mangling approach which we chose for SPIR.
Specifically, I think a discussion on the atomics and memcpy should be interesting.

*****OpenCL Built-ins Introduction******
OpenCL provides a huge set of utility functions (>6000 built-ins) which are available for the developers of OpenCL.
These functions are called built-ins. The OpenCL specification bends the "C" specification and allows overloading for the built-in functions.

*******Why not make use of LLVM Intrinsics where applicable?********
Today, clang is not familiar with those built-ins at all.
They can be supplied as an additional module of helper functions that is used by OpenCL compiler backends when building OpenCL programs. SPIR specification adopts this approach.

During the definition stage we decided that those built-ins will be exposed in SPIR as regular llvm functions.
When in the context of SPIR, those functions will be resolved by OpenCL backends, and their names are reserved for SPIR and can't be implemented by SPIR developers.

Khronos members realize that a small portion of those built-ins exist in LLVM IR today and could have potentially been used. There are a few reasons behind the decision to the existing proposal in SPIR.
The first reason is that there are still a substantial amount of built-ins which exist in OpenCL but don't exist today in LLVM. One of SPIR design goals were to keep changes in LLVM minimal, the decision was not to add the missing ones into LLVM. Since we wanted all built-ins to share the same mechanism we chose not to use LLVM the relevant intrinsics.

The second reason is compatibility between the precision requirements of the built-ins in OpenCL vs. the ones that are defined in LLVM IR. The separation serves this purpose well.

The third reason is that an OpenCL backend can choose to implement those built-ins using the intrinsics where possible and achieve the same effect as generating them directly from CLANG.

******Name Mangling for the built-ins******
Since overloading is allowed for built-ins, and given the approach we chose for exposing SPIR built-ins to the developers (regular function calls and not llvm intrinsics), we had to define a name mangling scheme for the built-ins functions. We chose the Itanium name mangling which already exists today in CLANG and formalized it for SPIR.