8000 [c_api] Expose interpreter and JIT proc runtimes by cdleary · Pull Request #5531 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions xls/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,23 @@ cc_library(
":runtime_ir_opt_actions",
"//xls/common:init_xls",
"//xls/common/file:filesystem",
"//xls/common/status:status_macros",
"//xls/dslx:mangle",
"//xls/dslx/type_system:parametric_env",
"//xls/interpreter:channel_queue",
"//xls/interpreter:interpreter_proc_runtime",
"//xls/interpreter:ir_interpreter",
"//xls/interpreter:serial_proc_runtime",
"//xls/ir",
"//xls/ir:bit_push_buffer",
"//xls/ir:bits",
"//xls/ir:bits_ops",
"//xls/ir:channel",
"//xls/ir:evaluator_result_cc_proto",
"//xls/ir:events",
"//xls/ir:format_preference",
"//xls/ir:ir_parser",
"//xls/ir:proc_elaboration",
"//xls/ir:type",
"//xls/ir:value",
"//xls/ir:verifier",
Expand All @@ -353,13 +360,15 @@ cc_library(
"//xls/jit:function_base_jit",
"//xls/jit:function_jit",
"//xls/jit:jit_callbacks",
"//xls/jit:jit_proc_runtime",
"//xls/jit:jit_runtime",
"//xls/jit:llvm_type_converter",
"//xls/solvers:z3_ir_translator",
"//xls/tools:codegen_flags_cc_proto",
"//xls/tools:scheduling_options_flags_cc_proto",
"@abseil-cpp//absl/base",
"@abseil-cpp//absl/container:btree",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/status",
Expand Down
216 changes: 210 additions & 6 deletions xls/public/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "absl/base/casts.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
Expand All @@ -41,18 +42,26 @@
#include "llvm/include/llvm/IR/LLVMContext.h"
#include "xls/common/file/filesystem.h"
#include "xls/common/init_xls.h"
#include "xls/common/status/status_macros.h"
#include "xls/dslx/mangle.h"
#include "xls/dslx/type_system/parametric_env.h"
#include "xls/interpreter/channel_queue.h"
#include "xls/interpreter/function_interpreter.h"
#include "xls/interpreter/interpreter_proc_runtime.h"
#include "xls/interpreter/serial_proc_runtime.h"
#include "xls/ir/bit_push_buffer.h"
#include "xls/ir/bits.h"
#include "xls/ir/bits_ops.h"
#include "xls/ir/channel.h"
#include "xls/ir/evaluator_result.pb.h"
#include "xls/ir/events.h"
#include "xls/ir/format_preference.h"
#include "xls/ir/function.h"
#include "xls/ir/ir_parser.h"
#include "xls/ir/nodes.h"
#include "xls/ir/package.h"
#include "xls/ir/proc.h"
#include "xls/ir/proc_elaboration.h"
#include "xls/ir/type.h"
#include "xls/ir/value.h"
#include "xls/ir/verifier.h"
Expand All @@ -61,6 +70,7 @@
#include "xls/jit/function_base_jit.h"
#include "xls/jit/function_jit.h"
#include "xls/jit/jit_callbacks.h"
#include "xls/jit/jit_proc_runtime.h"
#include "xls/jit/jit_runtime.h"
#include "xls/jit/llvm_type_converter.h"
#include "xls/public/c_api_format_preference.h"
Expand All @@ -78,6 +88,100 @@ struct xls_aot_exec_context {
std::unique_ptr<xls::JitRuntime> jit_runtime;
};

namespace {

absl::Status CheckProcRuntimeChannelNames(xls::Package* package) {
if (!package->ChannelsAreProcScoped()) {
return absl::OkStatus();
}
std::optional<xls::ProcElaboration> elaboration;
for (const auto& proc : package->procs()) {
absl::flat_hash_map<std::string_view, xls::ChannelInterface*> interfaces;
for (xls::ChannelInterface* interface : proc->interface()) {
auto [it, inserted] = interfaces.emplace(interface->name(), interface);
if (inserted) {
continue;
}
// Both evaluators look up queues by name. A repeated name is safe when
// both interfaces bind to one loopback queue in that instance.
if (!elaboration.has_value()) {
XLS_ASSIGN_OR_RETURN(xls::Proc * top, package->GetTopAsProc());
XLS_ASSIGN_OR_RETURN(elaboration, xls::ProcElaboration::Elaborate(top));
}
for (xls::ProcInstance* instance :
elaboration->GetInstances(proc.get())) {
if (instance->GetChannelBinding(it->second).instance !=
instance->GetChannelBinding(interface).instance) {
return absl::UnimplementedError(absl::StrFormat(
"Proc instance %s has send and receive interfaces named `%s` "
"bound to distinct queues",
instance->GetName(), interface->name()));
}
}
}
}
return absl::OkStatus();
}

bool CreateProcRuntime(xls_package* package, bool use_jit, char** error_out,
xls_proc_runtime** runtime_out) {
CHECK_NE(package, nullptr);
CHECK_NE(error_out, nullptr);
CHECK_NE(runtime_out, nullptr);

auto* cpp_package = reinterpret_cast<xls::Package*>(package);
*runtime_out = nullptr;
if (absl::Status status = CheckProcRuntimeChannelNames(cpp_package);
!status.ok()) {
*error_out = xls::ToOwnedCString(status.ToString());
return false;
}
absl::StatusOr<std::unique_ptr<xls::SerialProcRuntime>> runtime =
use_jit ? xls::CreateJitSerialProcRuntime(cpp_package)
: xls::CreateInterpreterSerialProcRuntime(cpp_package);
if (!runtime.ok()) {
*error_out = xls::ToOwnedCString(runtime.status().ToString());
return false;
}
*runtime_out = reinterpret_cast<xls_proc_runtime*>(runtime->release());
*error_out = nullptr;
return true;
}

xls_trace_message ToOwnedTraceMessage(const xls::TraceMessageProto& trace) {
return {
.message = xls::ToOwnedCString(trace.message()),
.verbosity = trace.has_statement() ? trace.statement().verbosity() : 0};
}

absl::StatusOr<xls::ChannelQueue*> GetProcRuntimeQueue(
xls::SerialProcRuntime* runtime, std::string_view name,
xls::ChannelDirection direction) {
// The package top may have changed since the runtime elaborated its queues.
xls::ProcInstance* top = runtime->elaboration().top();
if (top == nullptr) {
return runtime->queue_manager().GetQueueByName(name);
}
XLS_ASSIGN_OR_RETURN(xls::ChannelInstance * instance,
top->GetChannelInstance(name));
if (!runtime->elaboration().IsTopInterfaceChannel(instance)) {
return absl::NotFoundError(
absl::StrFormat("Channel `%s` is internal to top proc `%s`", name,
top->proc()->name()));
}
if ((direction == xls::ChannelDirection::kReceive &&
!instance->channel->CanReceive()) ||
(direction == xls::ChannelDirection::kSend &&
!instance->channel->CanSend())) {
return absl::NotFoundError(absl::StrFormat(
"No %s interface named `%s` on top proc `%s`",
xls::ChannelDirectionToString(direction), name, top->proc()->name()));
}
return &runtime->queue_manager().GetQueue(instance);
}

} // namespace

extern "C" {

void xls_init_xls(const char* usage, int argc, char* argv[]) {
Expand Down Expand Up @@ -1368,6 +1472,111 @@ bool xls_function_get_param_name(struct xls_function* function, size_t index,
return true;
}

bool xls_proc_runtime_create(struct xls_package* package, char** error_out,
struct xls_proc_runtime** runtime_out) {
return CreateProcRuntime(package, /*use_jit=*/false, error_out, runtime_out);
}

bool xls_proc_runtime_create_jit(struct xls_package* package, char** error_out,
struct xls_proc_runtime** runtime_out) {
return CreateProcRuntime(package, /*use_jit=*/true, error_out, runtime_out);
}

void xls_proc_runtime_free(struct xls_proc_runtime* runtime) {
delete reinterpret_cast<xls::SerialProcRuntime*>(runtime);
}

bool xls_proc_runtime_tick(struct xls_proc_runtime* runtime, char** error_out) {
CHECK_NE(runtime, nullptr);
CHECK_NE(error_out, nullptr);

auto* cpp_runtime = reinterpret_cast<xls::SerialProcRuntime*>(runtime);
cpp_runtime->ClearInterpreterEvents();
absl::Status status = cpp_runtime->Tick();
if (!status.ok()) {
*error_out = xls::ToOwnedCString(status.ToString());
return false;
}
*error_out = nullptr;
return true;
}

void xls_proc_runtime_take_trace_messages(
struct xls_proc_runtime* runtime, struct xls_trace_message** messages_out,
size_t* count_out) {
CHECK_NE(runtime, nullptr);
CHECK_NE(messages_out, nullptr);
CHECK_NE(count_out, nullptr);

auto* cpp_runtime = reinterpret_cast<xls::SerialProcRuntime*>(runtime);
xls::InterpreterEvents events = cpp_runtime->GetGlobalEvents();
for (xls::ProcInstance* instance :
cpp_runtime->elaboration().proc_instances()) {
events.AppendFrom(cpp_runtime->GetInterpreterEvents(instance));
}
const auto& traces = events.GetTraceMessages();
*count_out = traces.size();
*messages_out = nullptr;
if (!traces.empty()) {
auto messages = std::make_unique<xls_trace_message[]>(traces.size());
for (int i = 0; i < traces.size(); ++i) {
messages[i] = ToOwnedTraceMessage(traces.Get(i));
}
*messages_out = messages.release();
}
cpp_runtime->ClearInterpreterEvents();
}

bool xls_proc_runtime_write(struct xls_proc_runtime* runtime,
const char* channel_name,
const struct xls_value* value, char** error_out) {
CHECK_NE(runtime, nullptr);
CHECK_NE(channel_name, nullptr);
CHECK_NE(value, nullptr);
CHECK_NE(error_out, nullptr);

auto* cpp_runtime = reinterpret_cast<xls::SerialProcRuntime*>(runtime);
absl::StatusOr<xls::ChannelQueue*> queue = GetProcRuntimeQueue(
cpp_runtime, channel_name, xls::ChannelDirection::kReceive);
if (!queue.ok()) {
*error_out = xls::ToOwnedCString(queue.status().ToString());
return false;
}
absl::Status status =
(*queue)->Write(*reinterpret_cast<const xls::Value*>(value));
if (!status.ok()) {
*error_out = xls::ToOwnedCString(status.ToString());
return false;
}
*error_out = nullptr;
return true;
}

bool xls_proc_runtime_try_read(struct xls_proc_runtime* runtime,
const char* channel_name, char** error_out,
struct xls_value** value_out) {
CHECK_NE(runtime, nullptr);
CHECK_NE(channel_name, nullptr);
CHECK_NE(error_out, nullptr);
CHECK_NE(value_out, nullptr);

auto* cpp_runtime = reinterpret_cast<xls::SerialProcRuntime*>(runtime);
absl::StatusOr<xls::ChannelQueue*> queue = GetProcRuntimeQueue(
cpp_runtime, channel_name, xls::ChannelDirection::kSend);
*value_out = nullptr;
if (!queue.ok()) {
*error_out = xls::ToOwnedCString(queue.status().ToString());
return false;
}
std::optional<xls::Value> value = (*queue)->Read();
if (value.has_value()) {
*value_out =
reinterpret_cast<xls_value*>(new xls::Value(*std::move(value)));
}
*error_out = nullptr;
return true;
}

bool xls_make_function_jit(struct xls_function* function, char** error_out,
struct xls_function_jit** result_out) {
CHECK_NE(function, nullptr);
Expand Down Expand Up @@ -1575,12 +1784,7 @@ bool xls_aot_exec_context_get_trace_message(
return false;
}

trace_message_out->message =
xls::ToOwnedCString(trace_messages.Get(index).message());
trace_message_out->verbosity =
trace_messages.Get(index).has_statement()
? trace_messages.Get(index).statement().verbosity()
: 0;
*trace_message_out = ToOwnedTraceMessage(trace_messages.Get(index));
*error_out = nullptr;
return true;
}
Expand Down
59 changes: 54 additions & 5 deletions xls/public/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct xls_function_base;
struct xls_function_jit;
struct xls_function_type;
struct xls_package;
struct xls_proc_runtime;
struct xls_schedule_and_codegen_result;
struct xls_type;
struct xls_value;
Expand Down Expand Up @@ -529,6 +530,59 @@ bool xls_interpret_function(struct xls_function* function, size_t argc,
const struct xls_value* const* args,
char** error_out, struct xls_value** result_out);

// Creates a caller-owned interpreter runtime that borrows `package` and its IR.
// The package must outlive the runtime. Call `xls_proc_runtime_free` before
// rewriting IR used by the runtime. While the runtime is live, use a separate
// package for `xls_schedule_and_codegen_package`.
//
// The caller may reselect the package top. For proc-scoped channels, the
// runtime stays bound to the top proc selected at creation. Creation fails
// if any proc instance binds same-name input and output interfaces to distinct
// queues. An input and output bound to one loopback queue are supported.
bool xls_proc_runtime_create(struct xls_package* package, char** error_out,
struct xls_proc_runtime** runtime_out);

// JIT-compiles the procs at creation so subsequent ticks execute compiled code.
// The same channel, lifetime and ownership rules as `xls_proc_runtime_create`
// apply.
bool xls_proc_runtime_create_jit(struct xls_package* package, char** error_out,
struct xls_proc_runtime** runtime_out);

void xls_proc_runtime_free(struct xls_proc_runtime* runtime);

// Discards events from the previous tick and executes up to one iteration of
// each proc. Returns false on deadlock or another execution error.
bool xls_proc_runtime_tick(struct xls_proc_runtime* runtime, char** error_out);

struct xls_trace_message {
char* message;
int64_t verbosity;
};

// Takes the pending trace messages from the latest successful or failed tick.
// Messages from each proc instance are in emission order. The relative order
// between instances is unspecified. The caller owns the array and frees it
// with `xls_trace_messages_free`. If no traces are pending, sets
// `*messages_out` to null and `*count_out` to zero.
void xls_proc_runtime_take_trace_messages(
struct xls_proc_runtime* runtime, struct xls_trace_message** messages_out,
size_t* count_out);

// Writes to the named package channel or the top proc's named input interface.
// The runtime borrows `value` for this call. The caller retains ownership.
bool xls_proc_runtime_write(struct xls_proc_runtime* runtime,
const char* channel_name,
const struct xls_value* value, char** error_out);

// Reads from the named package channel or the top proc's named output
// interface. On success, returns true and sets `*value_out` to an owned value
// to free with `xls_value_free`, or to null if the queue was empty. On error,
// returns false, sets `*value_out` to null, and sets `*error_out` to a string
// to free with `xls_c_str_free`.
bool xls_proc_runtime_try_read(struct xls_proc_runtime* runtime,
const char* channel_name, char** error_out,
struct xls_value** value_out);

bool xls_make_function_jit(struct xls_function* function, char** error_out,
struct xls_function_jit** result_out);

Expand Down Expand Up @@ -575,11 +629,6 @@ int64_t xls_aot_entrypoint_trampoline(
int64_t continuation_point, size_t* trace_messages_count_out,
size_t* assert_messages_count_out);

struct xls_trace_message {
char* message;
int64_t verbosity;
};

// Gets a trace message from `context` at `index`.
//
// On success, returns true and fills `trace_message_out`. The caller owns
Expand Down
Loading
Loading
0