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.