| 1 | //===- offload-tblgen/APIGen.cpp - Tablegen backend for Offload functions -===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is a Tablegen backend that handles generation of various small files |
| 10 | // pertaining to the API functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/FormatVariadic.h" |
| 15 | #include "llvm/TableGen/Record.h" |
| 16 | |
| 17 | #include "GenCommon.hpp" |
| 18 | #include "RecordTypes.hpp" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace offload::tblgen; |
| 22 | |
| 23 | // Emit a list of just the API function names |
| 24 | void EmitOffloadFuncNames(const RecordKeeper &Records, raw_ostream &OS) { |
| 25 | OS << GenericHeader; |
| 26 | OS << R"( |
| 27 | #ifndef OFFLOAD_FUNC |
| 28 | #error Please define the macro OFFLOAD_FUNC(Function) |
| 29 | #endif |
| 30 | |
| 31 | )" ; |
| 32 | for (auto *R : Records.getAllDerivedDefinitions(ClassName: "Function" )) { |
| 33 | FunctionRec FR{R}; |
| 34 | OS << formatv(Fmt: "OFFLOAD_FUNC({0})" , Vals: FR.getName()) << "\n" ; |
| 35 | } |
| 36 | for (auto *R : Records.getAllDerivedDefinitions(ClassName: "Function" )) { |
| 37 | FunctionRec FR{R}; |
| 38 | OS << formatv(Fmt: "OFFLOAD_FUNC({0}WithCodeLoc)" , Vals: FR.getName()) << "\n" ; |
| 39 | } |
| 40 | |
| 41 | OS << "\n#undef OFFLOAD_FUNC\n" ; |
| 42 | } |
| 43 | |
| 44 | void EmitOffloadExports(const RecordKeeper &Records, raw_ostream &OS) { |
| 45 | OS << "VERS1.0 {\n" ; |
| 46 | OS << TAB_1 "global:\n" ; |
| 47 | |
| 48 | for (auto *R : Records.getAllDerivedDefinitions(ClassName: "Function" )) { |
| 49 | OS << formatv(TAB_2 "{0};\n" , Vals: FunctionRec(R).getName()); |
| 50 | } |
| 51 | for (auto *R : Records.getAllDerivedDefinitions(ClassName: "Function" )) { |
| 52 | OS << formatv(TAB_2 "{0}WithCodeLoc;\n" , Vals: FunctionRec(R).getName()); |
| 53 | } |
| 54 | OS << TAB_1 "local:\n" ; |
| 55 | OS << TAB_2 "*;\n" ; |
| 56 | OS << "};\n" ; |
| 57 | } |
| 58 | |
| 59 | // Emit declarations for every implementation function |
| 60 | void EmitOffloadImplFuncDecls(const RecordKeeper &Records, raw_ostream &OS) { |
| 61 | OS << GenericHeader; |
| 62 | for (auto *R : Records.getAllDerivedDefinitions(ClassName: "Function" )) { |
| 63 | FunctionRec F{R}; |
| 64 | OS << formatv(Fmt: "Error {0}_impl(" , Vals: F.getName()); |
| 65 | auto Params = F.getParams(); |
| 66 | for (auto &Param : Params) { |
| 67 | OS << Param.getType() << " " << Param.getName(); |
| 68 | if (Param != Params.back()) { |
| 69 | OS << ", " ; |
| 70 | } |
| 71 | } |
| 72 | OS << ");\n\n" ; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Emit macro calls for each error enum |
| 77 | void EmitOffloadErrcodes(const RecordKeeper &Records, raw_ostream &OS) { |
| 78 | OS << GenericHeader; |
| 79 | OS << R"( |
| 80 | #ifndef OFFLOAD_ERRC |
| 81 | #error Please define the macro OFFLOAD_ERRCODE(Name, Desc, Value) |
| 82 | #endif |
| 83 | |
| 84 | // Error codes are shared between PluginInterface and liboffload. |
| 85 | // To add new error codes, add them to offload/liboffload/API/Common.td and run the GenerateOffload target. |
| 86 | |
| 87 | )" ; |
| 88 | |
| 89 | auto ErrorCodeEnum = EnumRec{Records.getDef(Name: "ErrorCode" )}; |
| 90 | uint32_t EtorVal = 0; |
| 91 | for (const auto &EnumVal : ErrorCodeEnum.getValues()) { |
| 92 | OS << formatv(TAB_1 "OFFLOAD_ERRC({0}, \"{1}\", {2})\n" , Vals: EnumVal.getName(), |
| 93 | Vals: EnumVal.getDesc(), Vals: EtorVal++); |
| 94 | } |
| 95 | } |
| 96 | |