xref: /llvm-project/offload/tools/offload-tblgen/FuncsGen.cpp (revision fd3907ccb583df99e9c19d2fe84e4e7c52d75de9)
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("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 
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("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 function
60 void EmitOffloadImplFuncDecls(const RecordKeeper &Records, raw_ostream &OS) {
61   OS << GenericHeader;
62   for (auto *R : Records.getAllDerivedDefinitions("Function")) {
63     FunctionRec F{R};
64     OS << formatv("{0}_impl_result_t {1}_impl(", PrefixLower, 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