123 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This is a Tablegen backend that handles generation of various small files10// 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 20using namespace llvm;21using namespace offload::tblgen;22 23// Emit a list of just the API function names24void EmitOffloadFuncNames(const RecordKeeper &Records, raw_ostream &OS) {25 OS << GenericHeader;26 OS << R"(27#ifndef OFFLOAD_FUNC28#error Please define the macro OFFLOAD_FUNC(Function)29#endif30 31)";32 for (auto *R : Records.getAllDerivedDefinitions("Function")) {33 FunctionRec FR{R};34 OS << formatv("OFFLOAD_FUNC({0})", FR.getName()) << "\n";35 }36 for (auto *R : Records.getAllDerivedDefinitions("Function")) {37 FunctionRec FR{R};38 OS << formatv("OFFLOAD_FUNC({0}WithCodeLoc)", FR.getName()) << "\n";39 }40 41 OS << "\n#undef OFFLOAD_FUNC\n";42}43 44void 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("Function")) {49 OS << formatv(TAB_2 "{0};\n", FunctionRec(R).getName());50 }51 for (auto *R : Records.getAllDerivedDefinitions("Function")) {52 OS << formatv(TAB_2 "{0}WithCodeLoc;\n", 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 function60void EmitOffloadImplFuncDecls(const RecordKeeper &Records, raw_ostream &OS) {61 OS << GenericHeader;62 for (auto *R : Records.getAllDerivedDefinitions("Function")) {63 FunctionRec F{R};64 OS << formatv("Error {0}_impl(", 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 enum77void EmitOffloadErrcodes(const RecordKeeper &Records, raw_ostream &OS) {78 OS << GenericHeader;79 OS << R"(80#ifndef OFFLOAD_ERRC81#error Please define the macro OFFLOAD_ERRCODE(Name, Desc, Value)82#endif83 84// Error codes are shared between PluginInterface and liboffload.85// To add new error codes, add them to offload/liboffload/API/Common.td.86 87)";88 89 auto ErrorCodeEnum = EnumRec{Records.getDef("ol_errc_t")};90 uint32_t EtorVal = 0;91 for (const auto &EnumVal : ErrorCodeEnum.getValues()) {92 OS << formatv(TAB_1 "OFFLOAD_ERRC({0}, \"{1}\", {2})\n", EnumVal.getName(),93 EnumVal.getDesc(), EtorVal++);94 }95}96 97// Emit macro calls for each info98void EmitOffloadInfo(const RecordKeeper &Records, raw_ostream &OS) {99 OS << GenericHeader;100 OS << R"(101#ifndef OFFLOAD_DEVINFO102#error Please define the macro OFFLOAD_DEVINFO(Name, Desc, Value)103#endif104 105// Device info codes are shared between PluginInterface and liboffload.106// To add new error codes, add them to offload/liboffload/API/Device.td.107 108)";109 110 auto Enum = EnumRec{Records.getDef("ol_device_info_t")};111 // Bitfields start from 1, other enums from 0112 uint32_t EtorVal = Enum.isBitField();113 for (const auto &EnumVal : Enum.getValues()) {114 OS << formatv(TAB_1 "OFFLOAD_DEVINFO({0}, \"{1}\", {2})\n",115 EnumVal.getName(), EnumVal.getDesc(), EtorVal);116 if (Enum.isBitField()) {117 EtorVal <<= 1u;118 } else {119 ++EtorVal;120 }121 }122}123