CIRCT 22.0.0git
Loading...
Searching...
No Matches
ArcRuntime.cpp
Go to the documentation of this file.
1//===- ArcRuntime.cpp - Default implementation of the ArcRuntime-----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the default implementation of a runtime library for
10// arcilator simulations.
11//
12//===----------------------------------------------------------------------===//
13
14#define ARC_RUNTIME_ENABLE_EXPORT
15
20
21#ifdef ARC_RUNTIME_JIT_BIND
22#define ARC_RUNTIME_JITBIND_FNDECL
24#endif
25
26#include <cassert>
27#include <cstdlib>
28#include <iostream>
29
30using namespace circt::arc::runtime;
31
32[[noreturn]] static void internalError(const char *message) {
33 std::cerr << "[ArcRuntime] Internal Error: " << message << std::endl;
34 assert(false && "ArcRuntime Internal Error");
35 abort();
36}
37
38static inline ModelInstance *getModelInstance(ArcState *instance) {
39 assert(instance->impl != nullptr && "Instance is null");
40 return reinterpret_cast<ModelInstance *>(instance->impl);
41}
42
44 const char *args) {
45 if (model->apiVersion != ARC_RUNTIME_API_VERSION) {
46 internalError("API version mismatch.\nMake sure to use an ArcRuntime "
47 "release matching "
48 "the arcilator version used to build the hardware model.");
49 return nullptr;
50 }
51
52 auto *statePtr = reinterpret_cast<ArcState *>(
53 calloc(1, sizeof(ArcState) + model->numStateBytes));
54 assert(reinterpret_cast<intptr_t>(&statePtr->modelState[0]) % 16 == 0 &&
55 "Simulation state must be 16 byte aligned");
56 statePtr->impl = new ModelInstance(model, args, statePtr);
57 statePtr->magic = ARC_RUNTIME_MAGIC;
58 return statePtr;
59}
60
62 if (instance->impl)
63 delete reinterpret_cast<ModelInstance *>(instance->impl);
64 free(instance);
65}
66
68
69void arcRuntimeOnEval(ArcState *instance) {
70 getModelInstance(instance)->onEval();
71}
72
74 uint64_t offset) {
75 if (!modelState)
76 internalError("State pointer is null");
77 uint8_t *modPtr = static_cast<uint8_t *>(modelState) - offset;
78 ArcState *statePtr = reinterpret_cast<ArcState *>(modPtr - sizeof(ArcState));
79 if (statePtr->magic != ARC_RUNTIME_MAGIC)
80 internalError("Incorrect magic number for state");
81 return statePtr;
82}
83
84// --- IR Exports ---
85
87 const char *args) {
88 ArcState *statePtr = arcRuntimeAllocateInstance(model, args);
89 return statePtr->modelState;
90}
91
92void arcRuntimeIR_onEval(uint8_t *modelState) {
94}
95
99
100#ifdef ARC_RUNTIME_JIT_BIND
101namespace circt::arc::runtime {
102
103static const APICallbacks apiCallbacksGlobal{&arcRuntimeIR_allocInstance,
106
107const APICallbacks &getArcRuntimeAPICallbacks() { return apiCallbacksGlobal; }
108
109} // namespace circt::arc::runtime
110#endif
static void internalError(const char *message)
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.
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 ModelInstance * getModelInstance(ArcState *instance)
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.
void arcRuntimeIR_onEval(uint8_t *modelState)
Pre-Eval hook of the runtime library.
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.
#define ARC_RUNTIME_MAGIC
Magic number used to assert the presence of an ArcState struct.
Definition Common.h:26
#define ARC_RUNTIME_API_VERSION
Version of the combined public and internal API.
Definition Common.h:23
Static information for a compiled hardware model, generated by the MLIR lowering.
Definition Common.h:57
uint64_t apiVersion
Runtime API version used when compiling the model.
Definition Common.h:59
uint64_t numStateBytes
Number of bytes required for the model's state.
Definition Common.h:61
Combined runtime and model state for a hardware model instance.
Definition Common.h:40
void * impl
Runtime implementation specific data. Usually points to a custom struct.
Definition Common.h:42
uint8_t modelState[]
Definition Common.h:48
uint32_t magic
Runtime magic number. Must be set to ARC_RUNTIME_MAGIC.
Definition Common.h:46