15 #include "llvm/Support/JSON.h"
18 using namespace circt;
22 SmallVector<StateInfo> &states) {
23 struct StateCollectionJob {
24 mlir::Value::user_iterator nextToProcess;
25 mlir::Value::user_iterator
end;
28 StateCollectionJob(Value storage,
unsigned offset)
29 : nextToProcess(storage.user_begin()),
end(storage.user_end()),
33 SmallVector<StateCollectionJob, 4> jobStack{{storage, offset}};
35 while (!jobStack.empty()) {
36 StateCollectionJob &job = jobStack.back();
38 if (job.nextToProcess == job.end) {
43 Operation *op = *job.nextToProcess++;
44 unsigned offset = job.offset;
46 if (
auto substorage = dyn_cast<AllocStorageOp>(op)) {
47 if (!substorage.getOffset().has_value())
48 return substorage.emitOpError(
49 "without allocated offset; run state allocation first");
50 Value substorageOutput = substorage.getOutput();
51 jobStack.emplace_back(substorageOutput, offset + *substorage.getOffset());
55 if (!isa<AllocStateOp, RootInputOp, RootOutputOp, AllocMemoryOp>(op))
58 auto opName = op->getAttrOfType<StringAttr>(
"name");
59 if (!opName || opName.getValue().empty())
62 auto opOffset = op->getAttrOfType<IntegerAttr>(
"offset");
64 return op->emitOpError(
65 "without allocated offset; run state allocation first");
67 if (isa<AllocStateOp, RootInputOp, RootOutputOp>(op)) {
68 auto result = op->getResult(0);
69 auto &stateInfo = states.emplace_back();
70 stateInfo.type = StateInfo::Register;
71 if (isa<RootInputOp>(op))
73 else if (isa<RootOutputOp>(op))
75 else if (
auto alloc = dyn_cast<AllocStateOp>(op)) {
77 stateInfo.type = StateInfo::Wire;
79 stateInfo.name = opName.getValue();
80 stateInfo.offset = opOffset.getValue().getZExtValue() + offset;
81 stateInfo.numBits = cast<StateType>(result.getType()).getBitWidth();
85 if (
auto memOp = dyn_cast<AllocMemoryOp>(op)) {
86 auto stride = op->getAttrOfType<IntegerAttr>(
"stride");
88 return op->emitOpError(
89 "without allocated stride; run state allocation first");
90 auto memType = memOp.getType();
91 auto intType = memType.getWordType();
92 auto &stateInfo = states.emplace_back();
93 stateInfo.type = StateInfo::Memory;
94 stateInfo.name = opName.getValue();
95 stateInfo.offset = opOffset.getValue().getZExtValue() + offset;
96 stateInfo.numBits = intType.getWidth();
97 stateInfo.memoryStride = stride.getValue().getZExtValue();
98 stateInfo.memoryDepth = memType.getNumWords();
107 SmallVector<ModelInfo> &models) {
109 for (
auto modelOp : module.getOps<ModelOp>()) {
110 auto storageArg = modelOp.getBody().getArgument(0);
111 auto storageType = cast<StorageType>(storageArg.getType());
113 SmallVector<StateInfo> states;
116 llvm::sort(states, [](
auto &a,
auto &b) {
return a.offset < b.offset; });
118 models.emplace_back(std::string(modelOp.getName()), storageType.getSize(),
119 std::move(states), modelOp.getInitialFnAttr(),
120 modelOp.getFinalFnAttr());
127 ArrayRef<ModelInfo> models) {
128 llvm::json::OStream
json(outputStream, 2);
133 json.attribute(
"name", model.name);
134 json.attribute(
"numStateBytes", model.numStateBytes);
135 json.attribute(
"initialFnSym", !model.initialFnSym
137 : model.initialFnSym.getValue());
138 json.attribute(
"finalFnSym",
139 !model.finalFnSym ?
"" : model.finalFnSym.getValue());
140 json.attributeArray(
"states", [&] {
141 for (const auto &state : model.states) {
143 json.attribute(
"name", state.name);
144 json.attribute(
"offset", state.offset);
145 json.attribute(
"numBits", state.numBits);
146 auto typeStr = [](StateInfo::Type type) {
148 case StateInfo::Input:
150 case StateInfo::Output:
152 case StateInfo::Register:
154 case StateInfo::Memory:
156 case StateInfo::Wire:
161 json.attribute(
"type", typeStr(state.type));
162 if (state.type == StateInfo::Memory) {
163 json.attribute(
"stride", state.memoryStride);
164 json.attribute(
"depth", state.memoryDepth);
void serializeModelInfoToJson(llvm::raw_ostream &outputStream, llvm::ArrayRef< ModelInfo > models)
Serializes models to outputStream in JSON format.
mlir::LogicalResult collectModels(mlir::ModuleOp module, llvm::SmallVector< ModelInfo > &models)
Collects information about all Arc models in the provided module, and adds it to models.
mlir::LogicalResult collectStates(mlir::Value storage, unsigned offset, llvm::SmallVector< StateInfo > &states)
Collects information about states within the provided Arc model storage storage, assuming default off...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Gathers information about a given Arc model.