CIRCT 23.0.0git
Loading...
Searching...
No Matches
ModelInstance.cpp
Go to the documentation of this file.
1//===- ModelInstance.cpp - Instance of a model in 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 implements the context for a model instance in the ArcRuntime library.
10//
11//===----------------------------------------------------------------------===//
12
14
17
18#include <cassert>
19#include <iostream>
20#include <string_view>
21#include <vector>
22
23using namespace circt::arc::runtime;
24
26
27// Global counter for instances
28static uint64_t instanceIDsGlobal = 0;
29
31 const char *args, ArcState *mutableState)
32 : instanceID(instanceIDsGlobal++), modelInfo(modelInfo),
33 state(mutableState) {
34 bool hasTraceInstrumentation = !!modelInfo->traceInfo;
36 parseArgs(args);
37
38 if (verbose) {
39 std::cout << "[ArcRuntime] "
40 << "Created instance"
41 << " of model \"" << getModelName() << "\""
42 << " with ID " << instanceID << std::endl;
43 std::cout << "[ArcRuntime] Model \"" << getModelName() << "\"";
44 if (hasTraceInstrumentation)
45 std::cout << " has trace instrumentation." << std::endl;
46 else
47 std::cout << " does not have trace instrumentation." << std::endl;
48 }
49
50 if (!hasTraceInstrumentation && traceMode != TraceMode::DUMMY)
51 std::cerr
52 << "[ArcRuntime] WARNING: "
53 << "Tracing has been requested but model \"" << getModelName()
54 << "\" contains no instrumentation."
55 << " No trace will be produced.\n\t\tMake sure to compile the model"
56 " with tracing enabled and that it contains observed signals."
57 << std::endl;
58
59 if (hasTraceInstrumentation) {
60 switch (traceMode) {
63 std::make_unique<DummyTraceEncoder>(modelInfo, mutableState);
64 break;
65 case TraceMode::VCD:
66 traceEncoder = std::make_unique<VCDTraceEncoder>(
67 modelInfo, mutableState, getTraceFilePath(".vcd"), verbose);
68 break;
69 }
70 } else {
71 traceEncoder = {};
72 }
73}
74
76 if (verbose) {
77 std::cout << "[ArcRuntime] "
78 << "Deleting instance"
79 << " of model \"" << getModelName() << "\""
80 << " with ID " << instanceID << " after " << stepCounter
81 << " step(s)" << std::endl;
82 }
83 assert(state->impl == static_cast<void *>(this) && "Inconsistent ArcState");
84 if (traceEncoder)
85 traceEncoder->finish(state);
86}
87
88std::filesystem::path
89ModelInstance::getTraceFilePath(const std::string &suffix) {
90 if (traceFileArg.has_value())
91 return std::filesystem::path(*traceFileArg);
92
93 std::string saneName;
95 saneName = std::string(modelInfo->modelName);
96 for (auto &c : saneName) {
97 if (c == ' ' || c == '/' || c == '\\')
98 c = '_';
99 }
100 saneName += '_';
101 saneName += std::to_string(instanceID);
102 saneName += suffix;
103 return std::filesystem::current_path() / std::filesystem::path(saneName);
104}
105
106void ModelInstance::onEval(ArcState *mutableState) {
107 assert(mutableState == state);
108 ++stepCounter;
109 if (traceEncoder)
110 traceEncoder->step(state);
111}
112
114 assert(mutableState == state);
115 if (traceEncoder)
116 traceEncoder->run(mutableState);
117
118 if (verbose) {
119 std::cout << "[ArcRuntime] "
120 << "Instance with ID " << instanceID << " initialized"
121 << std::endl;
122 }
123}
124
126 if (!traceEncoder)
128 "swapTraceBuffer called on model without trace instrumentation");
129 if (verbose)
130 std::cout << "[ArcRuntime] Consuming trace buffer of size "
131 << state->traceBufferSize << " for instance ID " << instanceID
132 << std::endl;
133 return traceEncoder->dispatch(state->traceBufferSize);
134}
135
136// Try to parse argument with "`key`=`value`" syntax
137static bool parseKeyValueArg(const std::string_view &input, std::string &key,
138 std::string &value) {
139 key.clear();
140 value.clear();
141 auto eqPos = input.find('=');
142 if (eqPos == std::string_view::npos || eqPos == 0)
143 return false;
144 key = input.substr(0, eqPos);
145 value = input.substr(eqPos + 1);
146 return true;
147}
148
149void ModelInstance::parseArgs(const char *args) {
150 if (!args)
151 return;
152
153 // Split the argument string at semicolon delimiters
154 auto argStr = std::string_view(args);
155 if (argStr.empty())
156 return;
157 std::vector<std::string_view> options;
158 size_t start = 0;
159 size_t end = 0;
160
161 while ((end = argStr.find(";", start)) != std::string_view::npos) {
162 options.push_back(argStr.substr(start, end - start));
163 start = end + 1;
164 }
165 if (start <= argStr.size())
166 options.push_back(argStr.substr(start));
167
168 std::string key;
169 std::string value;
170
171 // Parse the individual options
172 for (auto &option : options) {
173
174 if (option == "debug") {
175 verbose = true;
176 } else if (option == "vcd") {
178 } else if (parseKeyValueArg(option, key, value) && !value.empty()) {
179 if (key == "traceFile")
180 traceFileArg = value;
181 }
182 }
183}
184
185} // namespace circt::arc::runtime::impl
assert(baseType &&"element must be base type")
std::filesystem::path getTraceFilePath(const std::string &suffix)
void onEval(ArcState *mutableState)
std::optional< std::string > traceFileArg
std::unique_ptr< TraceEncoder > traceEncoder
void onInitialized(ArcState *mutableState)
const ArcRuntimeModelInfo *const modelInfo
static void fatalError(const char *message)
Raise an irrecoverable error.
Definition Internal.h:23
static uint64_t instanceIDsGlobal
static bool parseKeyValueArg(const std::string_view &input, std::string &key, std::string &value)
Static information for a compiled hardware model, generated by the MLIR lowering.
Definition Common.h:70
struct ArcModelTraceInfo * traceInfo
Signal tracing information. NULL iff the model is not trace instrumented.
Definition Common.h:78
const char * modelName
Name of the compiled model.
Definition Common.h:76
Combined runtime and model state for a hardware model instance.
Definition Common.h:44
void * impl
Runtime implementation specific data. Usually points to a custom struct.
Definition Common.h:46
uint32_t traceBufferSize
Number of valid elements in the active trace buffer.
Definition Common.h:52