Files
clang-p2996/offload/tools/offload-tblgen/EntryPointGen.cpp
Callum Fare 800d949bb3 [Offload] Implement the remaining initial Offload API (#122106)
Implement the complete initial version of the Offload API, to the extent
that is usable for simple offloading programs. Tested with a basic SYCL
program.

As far as possible, these are simple wrappers over existing
functionality in the plugins.

* Allocating and freeing memory (host, device, shared).
* Creating a program 
* Creating a queue (wrapper over asynchronous stream resource)
* Enqueuing memcpy operations
* Enqueuing kernel executions
* Waiting on (optional) output events from the enqueue operations
* Waiting on a queue to finish

Objects created with the API have reference counting semantics to handle
their lifetime. They are created with an initial reference count of 1,
which can be incremented and decremented with retain and release
functions. They are freed when their reference count reaches 0. Platform
and device objects are not reference counted, as they are expected to
persist as long as the library is in use, and it's not meaningful for
users to create or destroy them.

Tests have been added to `offload.unittests`, including device code for
testing program and kernel related functionality.

The API should still be considered unstable and it's very likely we will
need to change the existing entry points.
2025-04-22 13:27:50 -05:00

140 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 (offloadConfig().ValidationEnabled) {\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 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";
// Emit pre-call prints
OS << TAB_1 "if (offloadConfig().TracingEnabled) {\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 = {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 "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;
for (auto *R : Records.getAllDerivedDefinitions("Function")) {
EmitValidationFunc(FunctionRec{R}, OS);
EmitEntryPointFunc(FunctionRec{R}, OS);
EmitCodeLocWrapper(FunctionRec{R}, OS);
}
}