14#define ARC_RUNTIME_ENABLE_EXPORT
23#include "llvm/ADT/APInt.h"
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/raw_ostream.h"
28#ifdef ARC_RUNTIME_JIT_BIND
29#define ARC_RUNTIME_JITBIND_FNDECL
42 assert(instance->
impl !=
nullptr &&
"Instance is null");
49 impl::fatalError(
"API version mismatch.\nMake sure to use an ArcRuntime "
51 "the arcilator version used to build the hardware model.");
55 auto *statePtr =
reinterpret_cast<ArcState *
>(
57 assert(
reinterpret_cast<intptr_t
>(&statePtr->modelState[0]) % 16 == 0 &&
58 "Simulation state must be 16 byte aligned");
83 impl::fatalError(
"State pointer is null");
84 uint8_t *modPtr =
static_cast<uint8_t *
>(modelState) - offset;
87 impl::fatalError(
"Incorrect magic number for state");
116 std::string_view s(va_arg(args,
const char *), fmt->
literal.
width);
126 uint64_t *words = va_arg(args, uint64_t *);
128 llvm::APInt apInt(fmt->
intFmt.
bitwidth, llvm::ArrayRef(words, numWords));
129 std::optional<int32_t> specifierWidth;
139 os << static_cast<char>(va_arg(args,
int));
160 impl::fatalError(
"Output stream pointer is null");
165 formatToStream(*
reinterpret_cast<llvm::raw_ostream *
>(stream), fmt, args);
171 return reinterpret_cast<uint8_t *
>(&llvm::outs());
175 return reinterpret_cast<uint8_t *
>(&llvm::errs());
179 auto *modPtr =
static_cast<const uint8_t *
>(modelState);
183 impl::fatalError(
"Incorrect magic number for state");
187#ifdef ARC_RUNTIME_JIT_BIND
197const APICallbacks &getArcRuntimeAPICallbacks() {
return apiCallbacksGlobal; }
ArcState * arcRuntimeAllocateInstance(const ArcRuntimeModelInfo *model, const char *args)
uint64_t arcRuntimeGetAPIVersion()
Return the API version of the runtime library.
void arcRuntimeDeleteInstance(ArcState *instance)
Destroy and deallocate the state of a model instance.
void arcRuntimeIR_formatToStream(uint8_t *stream, const FmtDescriptor *fmt,...)
Prints a formatted string to the given stream.
void arcRuntimeOnEval(ArcState *instance)
Pre-Eval hook. Must be called by the driver once before every eval step.
void arcRuntimeIR_format(const FmtDescriptor *fmt,...)
Prints a formatted string to stdout.
uint8_t * arcRuntimeIR_getStdoutStream()
Return the standard output stream.
uint8_t * arcRuntimeIR_allocInstance(const ArcRuntimeModelInfo *model, const char *args)
Allocate and initialize the state for a new instance of the given hardware model.
void arcRuntimeOnInitialized(ArcState *instance)
Must be called by the driver after the model's initial function and before the first onEval call.
uint64_t * arcRuntimeIR_swapTraceBuffer(const uint8_t *modelState)
Release the active trace buffer and request an empty new buffer.
uint8_t * arcRuntimeIR_getStderrStream()
Return the standard error stream.
void arcRuntimeIR_deleteInstance(uint8_t *modelState)
Destroy and deallocate the state of a model instance.
static void formatToStream(llvm::raw_ostream &os, const FmtDescriptor *fmt, va_list args)
ArcState * arcRuntimeGetStateFromModelState(uint8_t *modelState, uint64_t offset)
Project a pointer to the model state to its ArcState container.
static impl::ModelInstance * getModelInstance(const ArcState *instance)
void arcRuntimeIR_onEval(uint8_t *modelState)
Pre-Eval hook of the runtime library.
void arcRuntimeIR_onInitialized(uint8_t *modelState)
Post-initialization hook of the runtime library.
assert(baseType &&"element must be base type")
ARC_IR_EXPORT uint8_t * arcRuntimeIR_getStderrStream()
Return the standard error stream.
ARC_IR_EXPORT void arcRuntimeIR_formatToStream(uint8_t *stream, const circt::arc::runtime::FmtDescriptor *fmt,...)
Prints a formatted string to the given stream.
ARC_IR_EXPORT uint8_t * arcRuntimeIR_getStdoutStream()
Return the standard output stream.
ARC_IR_EXPORT uint8_t * arcRuntimeIR_allocInstance(const ArcRuntimeModelInfo *model, const char *args)
Allocate and initialize the state for a new instance of the given hardware model.
ARC_IR_EXPORT void arcRuntimeIR_deleteInstance(uint8_t *modelState)
Destroy and deallocate the state of a model instance.
ARC_IR_EXPORT void arcRuntimeIR_onEval(uint8_t *modelState)
Pre-Eval hook of the runtime library.
ARC_IR_EXPORT void arcRuntimeIR_onInitialized(uint8_t *modelState)
Post-initialization hook of the runtime library.
ARC_IR_EXPORT uint64_t * arcRuntimeIR_swapTraceBuffer(const uint8_t *modelState)
Release the active trace buffer and request an empty new buffer.
ARC_IR_EXPORT void arcRuntimeIR_format(const circt::arc::runtime::FmtDescriptor *fmt,...)
Prints a formatted string to stdout.
#define ARC_RUNTIME_MAGIC
Magic number used to assert the presence of an ArcState struct.
#define ARC_RUNTIME_API_VERSION
Version of the combined public and internal API.
void formatInteger(llvm::raw_ostream &os, const llvm::APInt &value, unsigned radix, bool isUpperCase, bool isLeftAligned, char paddingChar, std::optional< int32_t > specifierWidth, bool isSigned)
Format value in the given radix and emit it to os, padded to a field width.
Static information for a compiled hardware model, generated by the MLIR lowering.
uint64_t apiVersion
Runtime API version used when compiling the model.
uint64_t numStateBytes
Number of bytes required for the model's state.
Combined runtime and model state for a hardware model instance.
void * impl
Runtime implementation specific data. Usually points to a custom struct.
uint32_t magic
Runtime magic number. Must be set to ARC_RUNTIME_MAGIC.
bool isLeftAligned
Whether the value is left aligned.
bool isSigned
Whether to treat the value as signed.
bool isUpperCase
Whether to use uppercase hex letters.
char paddingChar
Padding character (NUL if no padding is desired).
int16_t specifierWidth
The minumum width of the output in characters.
int16_t bitwidth
The bitwidth of the integer value.
int8_t radix
The radix to use for formatting. Must be one of {2, 8, 10, 16}.
int64_t width
The width of the literal string in characters.
char data[8]
NUL-terminated string.
A format descriptor, to be given to arcRuntimeFormat.
@ Action_Char
Prints a character (c).
@ Action_Literal
Prints a literal string.
@ Action_End
End of the format string, no action to take.
@ Action_Int
Prints an integer.
@ Action_LiteralSmall
Prints a literal string (small string optimization).
SmallLiteral smallLiteral