This is a re-upload of the patch from Joey GOULY, posted at: https://reviews.llvm.org/D53023 . I am re-uploading it because I will continue his work on this, and it is better if I have control on the post on phabricator.
This patch contains a prototype to generate OpenCL builtin functions with Tablegen. Not all builtin functions have been implemented. This prototype is intented to replace the use of the opencl-c.h file currently included in all OpenCL programs. To recall, this file contains contains all the overloaded builtin functions. Using clang-tblgen would allow to factorize all these function declarations and include them only if they are called.
A copy-paste from the original description:
This is the prototype for the approach that was mentioned by Anastasia in http://lists.llvm.org/pipermail/cfe-dev/2018-September/059529.html
The tablegen file describes the BIFs and all their overloads, in hopefully a concise manner.
There are 3 things generated from the OpenCLBuiltins.td file.
- OpenCLArgTypes[], this is a table containing all the different types of overloads. This is a separate table so it can be shared by the BIFs.
- OpenCLBuiltins[], this is a table that contains all the overloads for the BIFs.
- isOpenCLBuiltin, this is a function that uses a trie-like switch/case to determine if a StringRef is the name of a BIF.
Just a quick snippet of the above:
OpenCLType OpenCLArgTypes[] = { // 0 { OCLT_float, 0, 0, clang::LangAS::Default, }, // 1 { OCLT_float, 2, 0, clang::LangAS::Default, },
OpenCLBuiltinDecl OpenCLBuiltins[] = { // acos { { OCLT_float, 0, 0, clang::LangAS::Default, }, 1, 0, "", 100, }, { { OCLT_float, 2, 0, clang::LangAS::Default, }, 1, 1, "", 100, },
std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef name) { switch (name.size()) { default: break; case 3: // 1 string to match. if (memcmp(name.data()+0, "foo", 3) != 0) break; return std::make_pair(707, 2); // "foo" }
While it's a prototype, I have tried to keep it as clean as possible.
TODO:
- Bit-pack the tables to reduce the size.
- Include the return type in the ArgTypes table to reduce the size.
- Measure the performance / size impact
- Auto-generate parts of OCL2Qual, to reduce repeated typing
- OCL2Qual does not support pointers-to-pointers currently, but I believe no BIFs use that.
- InsertBuiltinDeclarations builds up an AST function declaration manually, perhaps there is a helper function for this.
- There is a FIXME in SemaDecl.cpp that needs to be implemented.