14#define ARC_RUNTIME_ENABLE_EXPORT
22#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/raw_ostream.h"
27#ifdef ARC_RUNTIME_JIT_BIND
28#define ARC_RUNTIME_JITBIND_FNDECL
41 assert(instance->
impl !=
nullptr &&
"Instance is null");
48 impl::fatalError(
"API version mismatch.\nMake sure to use an ArcRuntime "
50 "the arcilator version used to build the hardware model.");
54 auto *statePtr =
reinterpret_cast<ArcState *
>(
56 assert(
reinterpret_cast<intptr_t
>(&statePtr->modelState[0]) % 16 == 0 &&
57 "Simulation state must be 16 byte aligned");
82 impl::fatalError(
"State pointer is null");
83 uint8_t *modPtr =
static_cast<uint8_t *
>(modelState) - offset;
86 impl::fatalError(
"Incorrect magic number for state");
114 const llvm::APInt &value,
bool isUpperCase,
115 bool isLeftAligned,
char paddingChar,
116 int specifierWidth,
bool isSigned) {
117 llvm::SmallVector<char, 32> strBuf;
118 value.toString(strBuf, radix, isSigned,
false, isUpperCase);
119 int strBufSize =
static_cast<int>(strBuf.size());
127 padWidth = (width + 2) / 3;
130 padWidth = (width + 3) / 4;
138 if (specifierWidth >= 0 &&
139 (specifierWidth > std::max(padWidth, strBufSize))) {
140 numSpaces = std::max(0, specifierWidth - std::max(padWidth, strBufSize));
143 llvm::SmallVector<char, 1> spacePadding(numSpaces,
' ');
145 padWidth = padWidth > strBufSize ? padWidth - strBufSize : 0;
147 llvm::SmallVector<char, 32> padding(padWidth, paddingChar);
150 os << padding << strBuf << spacePadding;
152 os << spacePadding << padding << strBuf;
160 llvm::raw_ostream &os = llvm::outs();
164 std::string_view s(va_arg(args,
const char *), fmt->
literal.
width);
174 uint64_t *words = va_arg(args, uint64_t *);
176 llvm::APInt apInt(fmt->
intFmt.
bitwidth, llvm::ArrayRef(words, numWords));
184 os << static_cast<char>(va_arg(args,
int));
196 auto *modPtr =
static_cast<const uint8_t *
>(modelState);
200 impl::fatalError(
"Incorrect magic number for state");
204#ifdef ARC_RUNTIME_JIT_BIND
212const 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 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_allocInstance(const ArcRuntimeModelInfo *model, const char *args)
Allocate and initialize the state for a new instance of the given hardware model.
static void formatIntegersByRadix(llvm::raw_ostream &os, int width, int radix, const llvm::APInt &value, bool isUpperCase, bool isLeftAligned, char paddingChar, int specifierWidth, bool isSigned)
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.
void arcRuntimeIR_deleteInstance(uint8_t *modelState)
Destroy and deallocate the state of a model instance.
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)
assert(baseType &&"element must be base type")
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 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.
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