[cfe-dev] [RFC][OpenCL] Implement OpenCL builtin functions in Clang
Anastasia Stulova via cfe-dev
cfe-dev at lists.llvm.org
Thu Sep 27 09:37:49 PDT 2018
Potential problems with this approach are:
- The maintenance costs as it's harder to modify Clang than just change the header in case we want to extend the list of builtins or modify them. Meaning that you have to add logic to clang to handle what is essentially just a list of declarations. Clang builtins weren't really intended to solve this problem.
- It is more risky in terms of bugs because it might not be easy to cover all use cases.
- Affects parsing time to check all the extra switch cases.
- We will need to replace the builtins from the header with macros aliasing Clang builtins. I find macros really bad in terms of error reporting. But also they will still need to be parsed.
Also something important to consider is the impact on parsing for other languages, i.e. C/C++. I think we should evaluate this carefully. Do you have any numbers for this already?
My team has a slightly different solution to this problem. It is based on generation of tries. Something similar to attribute parsing, see generated AttrParsedAttrKinds.inc. Unfortunately it's not in a shape that we can currently share. But it should not take longer than 2-3 weeks to upload a prototype if there is interest.
To give a rough idea, it uses a TableGen mechanism to describe the OpenCL builtin functions (similar to intrinsics in LLVM). During the Clang build phase, a trie file is generated. There is a hook in Clang that uses the generated trie to lookup the builtin function name during call resolution in Sema. After a successful lookup it generates the AST declaration of a function with overloads and the rest just follows the unmodified Clang flow (i.e. checking overloading, mangling, etc). This will cover all builtins, not just conversions and maths. It is easy to extend. It doesn't need to change much of Clang code, as it allows to follow normal compilation flow.
Do you think it would make sense to evaluate this solution as well?
From: cfe-dev <cfe-dev-bounces at lists.llvm.org> on behalf of Andrew Savonichev via cfe-dev <cfe-dev at lists.llvm.org>
Sent: 25 September 2018 16:02
To: cfe-dev at lists.llvm.org
Subject: [cfe-dev] [RFC][OpenCL] Implement OpenCL builtin functions in Clang
OpenCL C language has ~200 builtin functions defined in the
specification: math builtins (sin, cos, tan, etc.), conversion builtins
(float -> half, char2 -> int2), image builtins, and many others.
Currently, all these builtins are declared in `opencl-c.h' header for
each supported combination of types (using `overloadable' attribute), so
~200 builtins turn into ~14000 function declarations.
It takes a significant amount of time to parse these ~14000
declarations, and we have to do it for *every* OpenCL C program. This
leads to bad compile time performance for small OpenCL C programs, and
most of the time is eaten by `opencl-c.h' header.
There were several of attempts to improve this design by pre-compilation
(see D51544: Split opencl-c.h header), but pre-compiled headers are
difficult to manage, and they make overall design more complicated.
Alternatively, we can implement all these ~200 builtins in clang. With a
custom type checking we can handle different argument types. Custom
codegen can generate mangled function calls, so IR should be identical
to what we had before.
In the future, I think we can add a switch to replace mangled functions
with LLVM IR intrinsics, as it could be useful for SPIR-V translation
(see SPIR-V discussion for rationale).
I implemented several builtins as a POC:
- D52457: [OpenCL] Implement OpenCL convert builtin 
- D52458: [OpenCL] Implement OpenCL math builtins: fp -> fp 
Implementation of the remaining ~150 builtins should be straightforward,
but it is time-consuming, so I'd like to make sure that:
1) this approach fits the needs of OpenCL community
2) the rest of the Clang community is OK with adding a few hundred
builtins to Clang.
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
cfe-dev mailing list
cfe-dev at lists.llvm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the cfe-dev