mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-17 01:16:41 +00:00

Reland #118503. Added a fix for builds with `-DBUILD_SHARED_LIBS=ON` (see last commit). Otherwise the changes are identical. --- ### New API Previous discussions at the LLVM/Offload meeting have brought up the need for a new API for exposing the functionality of the plugins. This change introduces a very small subset of a new API, which is primarily for testing the offload tooling and demonstrating how a new API can fit into the existing code base without being too disruptive. Exact designs for these entry points and future additions can be worked out over time. The new API does however introduce the bare minimum functionality to implement device discovery for Unified Runtime and SYCL. This means that the `urinfo` and `sycl-ls` tools can be used on top of Offload. A (rough) implementation of a Unified Runtime adapter (aka plugin) for Offload is available [here](https://github.com/callumfare/unified-runtime/tree/offload_adapter). Our intention is to maintain this and use it to implement and test Offload API changes with SYCL. ### Demoing the new API ```sh # From the runtime build directory $ ninja LibomptUnitTests $ OFFLOAD_TRACE=1 ./offload/unittests/OffloadAPI/offload.unittests ``` ### Open questions and future work * Only some of the available device info is exposed, and not all the possible device queries needed for SYCL are implemented by the plugins. A sensible next step would be to refactor and extend the existing device info queries in the plugins. The existing info queries are all strings, but the new API introduces the ability to return any arbitrary type. * It may be sensible at some point for the plugins to implement the new API directly, and the higher level code on top of it could be made generic, but this is more of a long-term possibility.
139 lines
4.8 KiB
C++
139 lines
4.8 KiB
C++
//===- 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("{0}_impl_result_t {1}_val(\n ", PrefixLower, 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";
|
|
|
|
OS << TAB_1 "if (true /*enableParameterValidation*/) {\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 {0};\n", Return.getValue());
|
|
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 {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";
|
|
|
|
// Emit pre-call prints
|
|
OS << TAB_1 "if (offloadConfig().TracingEnabled) {\n";
|
|
OS << formatv(TAB_2 "std::cout << \"---> {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 = {1}_val({2});\n\n", PrefixLower,
|
|
F.getName(), ParamNameList);
|
|
|
|
// Emit post-call prints
|
|
OS << TAB_1 "if (offloadConfig().TracingEnabled) {\n";
|
|
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 "std::cout << \"(\" << &Params << \")\";\n";
|
|
} else {
|
|
OS << TAB_2 "std::cout << \"()\";\n";
|
|
}
|
|
OS << TAB_2 "std::cout << \"-> \" << Result << \"\\n\";\n";
|
|
OS << TAB_2 "if (Result && Result->Details) {\n";
|
|
OS << TAB_3 "std::cout << \" *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;
|
|
for (auto *R : Records.getAllDerivedDefinitions("Function")) {
|
|
EmitValidationFunc(FunctionRec{R}, OS);
|
|
EmitEntryPointFunc(FunctionRec{R}, OS);
|
|
EmitCodeLocWrapper(FunctionRec{R}, OS);
|
|
}
|
|
}
|