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#ifdef CIRCT_LIBFST_ENABLED
19#endif
20
21#include <cassert>
22#include <iostream>
23#include <string_view>
24#include <vector>
25
26using namespace circt::arc::runtime;
27
29
30// Global counter for instances
31static uint64_t instanceIDsGlobal = 0;
32
34 const char *args, ArcState *mutableState)
35 : instanceID(instanceIDsGlobal++), modelInfo(modelInfo),
36 state(mutableState) {
37 bool hasTraceInstrumentation = !!modelInfo->traceInfo;
39 parseArgs(args);
40
41 if (verbose) {
42 std::cout << "[ArcRuntime] "
43 << "Created instance"
44 << " of model \"" << getModelName() << "\""
45 << " with ID " << instanceID << std::endl;
46 std::cout << "[ArcRuntime] Model \"" << getModelName() << "\"";
47 if (hasTraceInstrumentation)
48 std::cout << " has trace instrumentation." << std::endl;
49 else
50 std::cout << " does not have trace instrumentation." << std::endl;
51 }
52
53 if (!hasTraceInstrumentation && traceMode != TraceMode::DUMMY)
54 std::cerr
55 << "[ArcRuntime] WARNING: "
56 << "Tracing has been requested but model \"" << getModelName()
57 << "\" contains no instrumentation."
58 << " No trace will be produced.\n\t\tMake sure to compile the model"
59 " with tracing enabled and that it contains observed signals."
60 << std::endl;
61
62 if (hasTraceInstrumentation) {
63 switch (traceMode) {
66 std::make_unique<DummyTraceEncoder>(modelInfo, mutableState);
67 break;
68 case TraceMode::VCD:
69 traceEncoder = std::make_unique<VCDTraceEncoder>(
70 modelInfo, mutableState, getTraceFilePath(".vcd"), verbose);
71 break;
72 case TraceMode::FST:
73#ifdef CIRCT_LIBFST_ENABLED
74 traceEncoder = std::make_unique<FSTTraceEncoder>(
75 modelInfo, mutableState, getTraceFilePath(".fst"), verbose);
76#else
77 std::cerr << "[ArcRuntime] ERROR: FST tracing was requested but CIRCT "
78 "was not built with FST support (CIRCT_LIBFST_ENABLED=OFF)."
79 << std::endl;
81 std::make_unique<DummyTraceEncoder>(modelInfo, mutableState);
82#endif
83 break;
84 }
85 } else {
86 traceEncoder = {};
87 }
88}
89
91 if (verbose) {
92 std::cout << "[ArcRuntime] "
93 << "Deleting instance"
94 << " of model \"" << getModelName() << "\""
95 << " with ID " << instanceID << " after " << stepCounter
96 << " step(s)" << std::endl;
97 }
98 assert(state->impl == static_cast<void *>(this) && "Inconsistent ArcState");
99 if (traceEncoder)
100 traceEncoder->finish(state);
101}
102
103std::filesystem::path
104ModelInstance::getTraceFilePath(const std::string &suffix) {
105 if (traceFileArg.has_value())
106 return std::filesystem::path(*traceFileArg);
107
108 std::string saneName;
109 if (modelInfo->modelName)
110 saneName = std::string(modelInfo->modelName);
111 for (auto &c : saneName) {
112 if (c == ' ' || c == '/' || c == '\\')
113 c = '_';
114 }
115 saneName += '_';
116 saneName += std::to_string(instanceID);
117 saneName += suffix;
118 return std::filesystem::current_path() / std::filesystem::path(saneName);
119}
120
121void ModelInstance::onEval(ArcState *mutableState) {
122 assert(mutableState == state);
123 ++stepCounter;
124 if (traceEncoder)
125 traceEncoder->step(state);
126}
127
129 assert(mutableState == state);
130 if (traceEncoder)
131 traceEncoder->run(mutableState);
132
133 if (verbose) {
134 std::cout << "[ArcRuntime] "
135 << "Instance with ID " << instanceID << " initialized"
136 << std::endl;
137 }
138}
139
141 if (!traceEncoder)
143 "swapTraceBuffer called on model without trace instrumentation");
144 if (verbose)
145 std::cout << "[ArcRuntime] Consuming trace buffer of size "
146 << state->traceBufferSize << " for instance ID " << instanceID
147 << std::endl;
148 return traceEncoder->dispatch(state->traceBufferSize);
149}
150
151// Try to parse argument with "`key`=`value`" syntax
152static bool parseKeyValueArg(const std::string_view &input, std::string &key,
153 std::string &value) {
154 key.clear();
155 value.clear();
156 auto eqPos = input.find('=');
157 if (eqPos == std::string_view::npos || eqPos == 0)
158 return false;
159 key = input.substr(0, eqPos);
160 value = input.substr(eqPos + 1);
161 return true;
162}
163
164void ModelInstance::parseArgs(const char *args) {
165 if (!args)
166 return;
167
168 // Split the argument string at semicolon delimiters
169 auto argStr = std::string_view(args);
170 if (argStr.empty())
171 return;
172 std::vector<std::string_view> options;
173 size_t start = 0;
174 size_t end = 0;
175
176 while ((end = argStr.find(";", start)) != std::string_view::npos) {
177 options.push_back(argStr.substr(start, end - start));
178 start = end + 1;
179 }
180 if (start <= argStr.size())
181 options.push_back(argStr.substr(start));
182
183 std::string key;
184 std::string value;
185
186 // Parse the individual options
187 for (auto &option : options) {
188
189 if (option == "debug") {
190 verbose = true;
191 } else if (option == "vcd") {
193 } else if (option == "fst") {
195 } else if (parseKeyValueArg(option, key, value) && !value.empty()) {
196 if (key == "traceFile")
197 traceFileArg = value;
198 }
199 }
200}
201
202} // 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