aboutsummaryrefslogtreecommitdiff
path: root/offload/tools/offload-tblgen/EntryPointGen.cpp
blob: 4f76100ed2dc3ca31435a1cc0195cb3439669aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//===- offload-tblgen/EntryPointGen.cpp - Tablegen backend for Offload ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This is a Tablegen backend that produces the actual entry points for the
// Offload API. It serves as a place to integrate functionality like tracing
// and validation before dispatching to the actual implementations.
//===----------------------------------------------------------------------===//

#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Record.h"

#include "GenCommon.hpp"
#include "RecordTypes.hpp"

using namespace llvm;
using namespace offload::tblgen;

static void EmitValidationFunc(const FunctionRec &F, raw_ostream &OS) {
  OS << CommentsHeader;
  // Emit preamble
  OS << formatv("llvm::Error {0}_val(\n  ", F.getName());
  // Emit arguments
  std::string ParamNameList = "";
  for (auto &Param : F.getParams()) {
    OS << Param.getType() << " " << Param.getName();
    if (Param != F.getParams().back()) {
      OS << ", ";
    }
    ParamNameList += Param.getName().str() + ", ";
  }
  OS << ") {\n";

  bool HasValidation = llvm::any_of(F.getReturns(), [](auto &R) {
    return llvm::any_of(R.getConditions(), [](auto &C) {
      return C.starts_with("`") && C.ends_with("`");
    });
  });

  if (HasValidation) {
    OS << TAB_1 "if (llvm::offload::isValidationEnabled()) {\n";
    // Emit validation checks
    for (const auto &Return : F.getReturns()) {
      for (auto &Condition : Return.getConditions()) {
        if (Condition.starts_with("`") && Condition.ends_with("`")) {
          auto ConditionString = Condition.substr(1, Condition.size() - 2);
          OS << formatv(TAB_2 "if ({0}) {{\n", ConditionString);
          OS << formatv(TAB_3
                        "return createOffloadError(error::ErrorCode::{0}, "
                        "\"validation failure: {1}\");\n",
                        Return.getUnprefixedValue(), ConditionString);
          OS << TAB_2 "}\n\n";
        }
      }
    }
    OS << TAB_1 "}\n\n";
  }

  // Perform actual function call to the implementation
  ParamNameList = ParamNameList.substr(0, ParamNameList.size() - 2);
  OS << formatv(TAB_1 "return llvm::offload::{0}_impl({1});\n\n", F.getName(),
                ParamNameList);
  OS << "}\n";
}

static void EmitEntryPointFunc(const FunctionRec &F, raw_ostream &OS) {
  // Emit preamble
  OS << formatv("{1}_APIEXPORT {0}_result_t {1}_APICALL {2}(\n  ", PrefixLower,
                PrefixUpper, F.getName());
  // Emit arguments
  std::string ParamNameList = "";
  for (auto &Param : F.getParams()) {
    OS << Param.getType() << " " << Param.getName();
    if (Param != F.getParams().back()) {
      OS << ", ";
    }
    ParamNameList += Param.getName().str() + ", ";
  }
  OS << ") {\n";

  // Check offload is initialized
  if (F.getName() != "olInit") {
    OS << "if (!llvm::offload::isOffloadInitialized()) return &UninitError;";

    // Emit pre-call prints
    // Postpone pre-calls for olInit as tracing requires liboffload to be initialized
    OS << TAB_1 "if (llvm::offload::isTracingEnabled()) {\n";
    OS << formatv(TAB_2 "llvm::errs() << \"---> {0}\";\n", F.getName());
    OS << TAB_1 "}\n\n";
  }

  // Perform actual function call to the validation wrapper
  ParamNameList = ParamNameList.substr(0, ParamNameList.size() - 2);
  OS << formatv(
      TAB_1 "{0}_result_t Result = llvmErrorToOffloadError({1}_val({2}));\n\n",
      PrefixLower, F.getName(), ParamNameList);

  // Emit post-call prints
  OS << TAB_1 "if (llvm::offload::isTracingEnabled()) {\n";
  // postponed pre-call print for olInit
  if (F.getName() == "olInit")
    OS << formatv(TAB_2 "llvm::errs() << \"---> {0}\";\n", F.getName());

  if (F.getParams().size() > 0) {
    OS << formatv(TAB_2 "{0} Params = {{", F.getParamStructName());
    for (const auto &Param : F.getParams()) {
      OS << "&" << Param.getName();
      if (Param != F.getParams().back()) {
        OS << ", ";
      }
    }
    OS << formatv("};\n");
    OS << TAB_2 "llvm::errs() << \"(\" << &Params << \")\";\n";
  } else {
    OS << TAB_2 "llvm::errs() << \"()\";\n";
  }
  OS << TAB_2 "llvm::errs() << \"-> \" << Result << \"\\n\";\n";
  OS << TAB_2 "if (Result && Result->Details) {\n";
  OS << TAB_3 "llvm::errs() << \"     *Error Details* \" << Result->Details "
              "<< \" \\n\";\n";
  OS << TAB_2 "}\n";
  OS << TAB_1 "}\n";

  OS << TAB_1 "return Result;\n";
  OS << "}\n";
}

static void EmitCodeLocWrapper(const FunctionRec &F, raw_ostream &OS) {
  // Emit preamble
  OS << formatv("{0}_result_t {1}WithCodeLoc(\n  ", PrefixLower, F.getName());
  // Emit arguments
  std::string ParamNameList = "";
  for (auto &Param : F.getParams()) {
    OS << Param.getType() << " " << Param.getName() << ", ";
    ParamNameList += Param.getName().str();
    if (Param != F.getParams().back()) {
      ParamNameList += ", ";
    }
  }
  OS << "ol_code_location_t *CodeLocation";
  OS << ") {\n";
  OS << TAB_1 "currentCodeLocation() = CodeLocation;\n";
  OS << formatv(TAB_1 "{0}_result_t Result = ::{1}({2});\n\n", PrefixLower,
                F.getName(), ParamNameList);
  OS << TAB_1 "currentCodeLocation() = nullptr;\n";
  OS << TAB_1 "return Result;\n";
  OS << "}\n";
}

void EmitOffloadEntryPoints(const RecordKeeper &Records, raw_ostream &OS) {
  OS << GenericHeader;

  constexpr const char *UninitMessage =
      "liboffload has not been initialized - please call olInit before using "
      "this API";
  OS << formatv("static {0}_error_struct_t UninitError = "
                "{{{1}_ERRC_UNINITIALIZED, \"{2}\"};",
                PrefixLower, PrefixUpper, UninitMessage);

  for (auto *R : Records.getAllDerivedDefinitions("Function")) {
    EmitValidationFunc(FunctionRec{R}, OS);
    EmitEntryPointFunc(FunctionRec{R}, OS);
    EmitCodeLocWrapper(FunctionRec{R}, OS);
  }
}