CIRCT 23.0.0git
Loading...
Searching...
No Matches
ExtractInstances.cpp
Go to the documentation of this file.
1//===- ExtractInstances.cpp - Move instances up the hierarchy ---*- C++ -*-===//
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// Moves annotated instances upwards in the module hierarchy. Corresponds to the
10// `ExtractBlackBoxes`, `ExtractClockGates`, and `ExtractSeqMems` passes in the
11// Scala FIRRTL implementation.
12//
13// This pass will make no modifications if the circuit does not contain a
14// design-under-test (DUT). I.e., this pass does not use the "effecctive" DUT.
15// If a DUT exists, then anything in the design is extracted. Using the
16// standard interpretation of passes like this, layers are not in the design.
17// If a situation arise where a module is instantiated inside and outside the
18// design that needs to be extracted, then it will be extracted in both up to
19// the point where it no longer needs to be further extracted. See the tests
20// for examples of this.
21//
22//===----------------------------------------------------------------------===//
23
38#include "mlir/IR/Attributes.h"
39#include "mlir/IR/ImplicitLocOpBuilder.h"
40#include "mlir/Pass/Pass.h"
41#include "mlir/Support/FileUtilities.h"
42#include "llvm/Support/Debug.h"
43
44#define DEBUG_TYPE "firrtl-extract-instances"
45
46namespace circt {
47namespace firrtl {
48#define GEN_PASS_DEF_EXTRACTINSTANCES
49#include "circt/Dialect/FIRRTL/Passes.h.inc"
50} // namespace firrtl
51} // namespace circt
52
53using namespace circt;
54using namespace firrtl;
55using hw::InnerRefAttr;
56
57//===----------------------------------------------------------------------===//
58// Pass Implementation
59//===----------------------------------------------------------------------===//
60
61namespace {
62/// All information necessary to move instances about.
63struct ExtractionInfo {
64 /// A filename into which the performed hierarchy modifications are emitted.
65 StringRef traceFilename;
66 /// A prefix to attach to the wiring generated by the extraction.
67 StringRef prefix;
68 /// Optional name of the wrapper module that will hold the moved instance.
69 StringRef wrapperModule;
70 /// Whether the extraction should stop at the root of the DUT instead of going
71 /// past that and extracting into the test harness.
72 bool stopAtDUT;
73};
74
75struct ExtractInstancesPass
76 : public circt::firrtl::impl::ExtractInstancesBase<ExtractInstancesPass> {
77 void runOnOperation() override;
78 void collectAnnos();
79 void collectAnno(InstanceOp inst, Annotation anno);
80 void extractInstances();
81 void groupInstances();
82 void createTraceFiles(ClassOp &sifiveMetadata);
83 void createSchema();
84
85 /// Get the cached namespace for a module.
86 hw::InnerSymbolNamespace &getModuleNamespace(FModuleLike module) {
87 return moduleNamespaces.try_emplace(module, module).first->second;
88 }
89
90 /// Obtain an inner reference to an operation, possibly adding an `inner_sym`
91 /// to that operation.
92 InnerRefAttr getInnerRefTo(Operation *op) {
93 return ::getInnerRefTo(op,
94 [&](FModuleLike mod) -> hw::InnerSymbolNamespace & {
95 return getModuleNamespace(mod);
96 });
97 }
98
99 /// Create a clone of a `HierPathOp` with a new uniquified name.
100 hw::HierPathOp cloneWithNewNameAndPath(hw::HierPathOp pathOp,
101 ArrayRef<Attribute> newPath) {
102 OpBuilder builder(pathOp);
103 auto newPathOp = builder.cloneWithoutRegions(pathOp);
104 newPathOp.setSymNameAttr(builder.getStringAttr(
105 circuitNamespace.newName(newPathOp.getSymName())));
106 newPathOp.setNamepathAttr(builder.getArrayAttr(newPath));
107 return newPathOp;
108 }
109
110 /// Return a handle to the unique instance of file with a given name.
111 emit::FileOp getOrCreateFile(StringRef fileName) {
112 auto [it, inserted] = files.try_emplace(fileName, emit::FileOp{});
113 if (inserted) {
114 auto builder = ImplicitLocOpBuilder::atBlockEnd(
115 UnknownLoc::get(&getContext()), getOperation().getBodyBlock());
116 it->second = emit::FileOp::create(builder, fileName);
117 }
118 return it->second;
119 }
120
121 bool anythingChanged;
122 bool anyFailures;
123
124 CircuitOp circuitOp;
125 InstanceGraph *instanceGraph = nullptr;
126 InstanceInfo *instanceInfo = nullptr;
127 SymbolTable *symbolTable = nullptr;
128
129 /// The modules in the design that are annotated with one or more annotations
130 /// relevant for instance extraction.
131 DenseMap<Operation *, SmallVector<Annotation, 1>> annotatedModules;
132
133 /// A worklist of instances that need to be moved.
134 SmallVector<std::pair<InstanceOp, ExtractionInfo>> extractionWorklist;
135
136 /// A mapping from file names to file ops for de-duplication.
137 DenseMap<StringRef, emit::FileOp> files;
138
139 /// The path along which instances have been extracted. This essentially
140 /// documents the original location of the instance in reverse. Every push
141 /// upwards in the hierarchy adds another entry to this path documenting along
142 /// which instantiation path each instance was extracted.
143 DenseMap<Operation *, SmallVector<InnerRefAttr>> extractionPaths;
144
145 /// A map of the original parent modules of instances before they were
146 /// extracted. This is used in a corner case during trace file emission.
147 DenseMap<Operation *, StringAttr> originalInstanceParents;
148
149 /// All extracted instances in their position after moving upwards in the
150 /// hierarchy, but before being grouped into an optional submodule.
151 SmallVector<std::pair<InstanceOp, ExtractionInfo>> extractedInstances;
152
153 // The uniquified wiring prefix and original name for each instance.
154 DenseMap<Operation *, std::pair<SmallString<16>, StringAttr>>
155 instPrefixNamesPair;
156
157 /// The current circuit namespace valid within the call to `runOnOperation`.
158 CircuitNamespace circuitNamespace;
159 /// Cached module namespaces.
160 DenseMap<Operation *, hw::InnerSymbolNamespace> moduleNamespaces;
161 /// The metadata class ops.
162 ClassOp extractMetadataClass, schemaClass;
163 const unsigned prefixNameFieldId = 0, pathFieldId = 2, fileNameFieldId = 4,
164 instNameFieldId = 6;
165 /// Cache of the inner ref to the new instances created. Will be used to
166 /// create a path to the instance
167 DenseMap<InnerRefAttr, InstanceOp> innerRefToInstances;
168 Type stringType, pathType;
169
170 /// If set, this indicates that the `InjectDUTHierarchy` pass ran with the
171 /// `moveDut` parameter enabled. If true, then extraction continues outside
172 /// the DUT even when extraction annotations have a wrapper module specified.
173 bool moveDut;
174};
175} // end anonymous namespace
176
177/// Emit the annotated source code for black boxes in a circuit.
178void ExtractInstancesPass::runOnOperation() {
179 circuitOp = getOperation();
180 anythingChanged = false;
181 anyFailures = false;
182 annotatedModules.clear();
183 extractionWorklist.clear();
184 files.clear();
185 extractionPaths.clear();
186 originalInstanceParents.clear();
187 extractedInstances.clear();
188 instPrefixNamesPair.clear();
189 moduleNamespaces.clear();
190 circuitNamespace.clear();
191 circuitNamespace.add(circuitOp);
192 innerRefToInstances.clear();
193 extractMetadataClass = {};
194 schemaClass = {};
195 auto *context = circuitOp->getContext();
196 stringType = StringType::get(context);
197 pathType = PathType::get(context);
198 moveDut = false;
199
200 // Walk the IR and gather all the annotations relevant for extraction that
201 // appear on instances and the instantiated modules.
202 instanceGraph = &getAnalysis<InstanceGraph>();
203 instanceInfo = &getAnalysis<InstanceInfo>();
204 symbolTable = &getAnalysis<SymbolTable>();
205 collectAnnos();
206 if (anyFailures)
207 return signalPassFailure();
208
209 // Actually move instances upwards.
210 extractInstances();
211 if (anyFailures)
212 return signalPassFailure();
213
214 // Group instances into submodules, if requested.
215 groupInstances();
216 if (anyFailures)
217 return signalPassFailure();
218
219 ClassOp sifiveMetadata =
220 dyn_cast_or_null<ClassOp>(symbolTable->lookup("SiFive_Metadata"));
221
222 // Generate the trace files that list where each instance was extracted from.
223 createTraceFiles(sifiveMetadata);
224 if (anyFailures)
225 return signalPassFailure();
226
227 // If nothing has changed we can preserve the analysis.
228 LLVM_DEBUG(llvm::dbgs() << "\n");
229 if (!anythingChanged)
230 markAllAnalysesPreserved();
231}
232
233static bool isAnnoInteresting(Annotation anno) {
234 return anno.isClass(extractBlackBoxAnnoClass);
235}
236
237/// Gather the modules and instances annotated to be moved by this pass. This
238/// populates the corresponding lists and maps of the pass.
239void ExtractInstancesPass::collectAnnos() {
240 CircuitOp circuit = getOperation();
241
242 // Find an optional `InjectDUTHierarchyAnnotation`. If it exists, inspect the
243 // `moveDut` field. If this is `true`, then we moved the DUT from the
244 // original DUT to the wrapper. If this occurred, then this affecst the
245 // behavior of whether or not we stop at the DUT (now the wrapper) when we
246 // extract instances.
247 //
248 // TODO: This is tech debt. This was accepted on condition that work is done
249 // to remove this pass.
251 if (!anno.isClass(injectDUTHierarchyAnnoClass))
252 return false;
253
254 if (auto moveDutAnnoAttr = anno.getMember<BoolAttr>("moveDut"))
255 moveDut = moveDutAnnoAttr.getValue();
256 return true;
257 });
258
259 // Grab the clock gate extraction annotation on the circuit.
260 StringRef clkgateFileName;
261 StringRef clkgateWrapperModule;
263 if (!anno.isClass(extractClockGatesFileAnnoClass))
264 return false;
265 LLVM_DEBUG(llvm::dbgs()
266 << "Clock gate extraction config: " << anno.getDict() << "\n");
267 auto filenameAttr = anno.getMember<StringAttr>("filename");
268 auto groupAttr = anno.getMember<StringAttr>("group");
269 if (!filenameAttr) {
270 circuit.emitError("missing `filename` attribute in `")
271 << anno.getClass() << "` annotation";
272 anyFailures = true;
273 return true;
274 }
275
276 if (!clkgateFileName.empty()) {
277 circuit.emitError("multiple `")
278 << anno.getClass() << "` annotations on circuit";
279 anyFailures = true;
280 return true;
281 }
282
283 clkgateFileName = filenameAttr.getValue();
284 if (groupAttr)
285 clkgateWrapperModule = groupAttr.getValue();
286 return true;
287 });
288
289 // Grab the memory extraction annotation on the circuit.
290 StringRef memoryFileName;
291 StringRef memoryWrapperModule;
293 if (!anno.isClass(extractSeqMemsFileAnnoClass))
294 return false;
295 LLVM_DEBUG(llvm::dbgs()
296 << "Memory extraction config: " << anno.getDict() << "\n");
297 auto filenameAttr = anno.getMember<StringAttr>("filename");
298 auto groupAttr = anno.getMember<StringAttr>("group");
299 if (!filenameAttr) {
300 circuit.emitError("missing `filename` attribute in `")
301 << anno.getClass() << "` annotation";
302 anyFailures = true;
303 return true;
304 }
305
306 if (!memoryFileName.empty()) {
307 circuit.emitError("multiple `")
308 << anno.getClass() << "` annotations on circuit";
309 anyFailures = true;
310 return true;
311 }
312
313 memoryFileName = filenameAttr.getValue();
314 if (groupAttr)
315 memoryWrapperModule = groupAttr.getValue();
316 return true;
317 });
318
319 // Gather the annotations on modules. These complement the later per-instance
320 // annotations.
321 for (auto module : circuit.getOps<FModuleLike>()) {
323 if (!isAnnoInteresting(anno))
324 return false;
325 LLVM_DEBUG(llvm::dbgs() << "Annotated module `" << module.getModuleName()
326 << "`:\n " << anno.getDict() << "\n");
327 annotatedModules[module].push_back(anno);
328 return true;
329 });
330 }
331
332 // Gather the annotations on instances to be extracted.
333 circuit.walk([&](InstanceOp inst) {
334 SmallVector<Annotation, 1> instAnnos;
335 Operation *module = inst.getReferencedModule(*instanceGraph);
336
337 // Module-level annotations.
338 auto it = annotatedModules.find(module);
339 if (it != annotatedModules.end())
340 instAnnos.append(it->second);
341
342 // Instance-level annotations.
344 if (!isAnnoInteresting(anno))
345 return false;
346 LLVM_DEBUG(llvm::dbgs() << "Annotated instance `" << inst.getName()
347 << "`:\n " << anno.getDict() << "\n");
348 instAnnos.push_back(anno);
349 return true;
350 });
351
352 // No need to do anything about unannotated instances.
353 if (instAnnos.empty())
354 return;
355
356 // Ensure there are no conflicting annotations.
357 if (instAnnos.size() > 1) {
358 auto d = inst.emitError("multiple extraction annotations on instance `")
359 << inst.getName() << "`";
360 d.attachNote(inst.getLoc()) << "instance has the following annotations, "
361 "but at most one is allowed:";
362 for (auto anno : instAnnos)
363 d.attachNote(inst.getLoc()) << anno.getDict();
364 anyFailures = true;
365 return;
366 }
367
368 // Process the annotation.
369 collectAnno(inst, instAnnos[0]);
370 });
371
372 // If clock gate extraction is requested, find instances of extmodules which
373 // have a defname that ends with "EICG_wrapper". This also allows this to
374 // compose with Chisel-time module prefixing.
375 //
376 // TODO: This defname matching is a terrible hack and should be replaced with
377 // something better.
378 if (!clkgateFileName.empty()) {
379 for (auto module : circuit.getOps<FExtModuleOp>()) {
380 if (!module.getDefnameAttr().getValue().ends_with("EICG_wrapper"))
381 continue;
382 LLVM_DEBUG(llvm::dbgs()
383 << "Clock gate `" << module.getModuleName() << "`\n");
384 if (!instanceInfo->anyInstanceInDesign(module)) {
385 LLVM_DEBUG(llvm::dbgs() << "- Ignored (outside DUT)\n");
386 continue;
387 }
388
389 ExtractionInfo info;
390 info.traceFilename = clkgateFileName;
391 info.prefix = "clock_gate"; // TODO: Don't hardcode this
392 info.wrapperModule = clkgateWrapperModule;
393 for (auto *instRecord : instanceGraph->lookup(module)->uses()) {
394 if (auto inst = dyn_cast<InstanceOp>(*instRecord->getInstance())) {
395 LLVM_DEBUG(llvm::dbgs()
396 << "- Marking `"
397 << inst->getParentOfType<FModuleLike>().getModuleName()
398 << "." << inst.getName() << "`\n");
399 extractionWorklist.push_back({inst, info});
400 } else {
401 instRecord->getInstance()->emitError()
402 << "cannot extract clock gate instances through non-InstanceOp";
403 anyFailures = true;
404 }
405 }
406 }
407 }
408
409 // If memory extraction is requested, find instances of `FMemModuleOp` and
410 // mark them as to be extracted.
411 // somewhat configurable.
412 if (!memoryFileName.empty()) {
413 // Create a potentially empty file if a name is specified. This is done to
414 // align with the SFC implementation of this pass where the file is always
415 // created. This does introduce an additional leading newline in the file.
416 getOrCreateFile(memoryFileName);
417
418 for (auto module : circuit.getOps<FMemModuleOp>()) {
419 LLVM_DEBUG(llvm::dbgs() << "Memory `" << module.getModuleName() << "`\n");
420 if (!instanceInfo->anyInstanceInDesign(module)) {
421 LLVM_DEBUG(llvm::dbgs() << "- Ignored (outside DUT)\n");
422 continue;
423 }
424
425 ExtractionInfo info;
426 info.traceFilename = memoryFileName;
427 info.prefix = "mem_wiring"; // TODO: Don't hardcode this
428 info.wrapperModule = memoryWrapperModule;
429 for (auto *instRecord : instanceGraph->lookup(module)->uses()) {
430 if (auto inst = dyn_cast<InstanceOp>(*instRecord->getInstance())) {
431 LLVM_DEBUG(llvm::dbgs()
432 << "- Marking `"
433 << inst->getParentOfType<FModuleLike>().getModuleName()
434 << "." << inst.getName() << "`\n");
435 extractionWorklist.push_back({inst, info});
436 } else {
437 instRecord->getInstance()->emitError()
438 << "cannot extract memory instances through non-InstanceOp";
439 anyFailures = true;
440 }
441 }
442 }
443 }
444}
445
446/// Process an extraction annotation on an instance into a corresponding
447/// `ExtractionInfo` and a spot on the worklist for later moving things around.
448void ExtractInstancesPass::collectAnno(InstanceOp inst, Annotation anno) {
449 LLVM_DEBUG(llvm::dbgs() << "Processing instance `" << inst.getName() << "` "
450 << anno.getDict() << "\n");
451
452 auto getStringOrError = [&](StringRef member) {
453 auto attr = anno.getMember<StringAttr>(member);
454 if (!attr) {
455 inst.emitError("missing `")
456 << member << "` attribute in `" << anno.getClass() << "` annotation";
457 anyFailures = true;
458 }
459 return attr;
460 };
461
462 if (anno.isClass(extractBlackBoxAnnoClass)) {
463 auto filename = getStringOrError("filename");
464 auto prefix = getStringOrError("prefix");
465 auto dest = anno.getMember<StringAttr>("dest"); // optional
466 if (anyFailures)
467 return;
468
469 ExtractionInfo info;
470 info.traceFilename = filename;
471 info.prefix = prefix;
472 info.wrapperModule = (dest ? dest.getValue() : "");
473
474 // CAVEAT: If the instance has a wrapper module configured then extraction
475 // should stop at the DUT module instead of extracting past the DUT into the
476 // surrounding test harness. This is all very ugly and hacky.
477
478 extractionWorklist.push_back({inst, info});
479 return;
480 }
481}
482
483/// Find the location in an NLA that corresponds to a given instance (either by
484/// mentioning exactly the instance, or the instance's parent module). Returns a
485/// position within the NLA's path, or the length of the path if the instances
486/// was not found.
487static unsigned findInstanceInNLA(InstanceOp inst, hw::HierPathOp nla) {
488 unsigned nlaLen = nla.getNamepath().size();
489 auto instName = getInnerSymName(inst);
490 auto parentName = cast<FModuleOp>(inst->getParentOp()).getModuleNameAttr();
491 for (unsigned nlaIdx = 0; nlaIdx < nlaLen; ++nlaIdx) {
492 auto refPart = nla.refPart(nlaIdx);
493 if (nla.modPart(nlaIdx) == parentName && (!refPart || refPart == instName))
494 return nlaIdx;
495 }
496 return nlaLen;
497}
498
499/// Move instances in the extraction worklist upwards in the hierarchy. This
500/// iteratively pushes instances up one level of hierarchy until they have
501/// arrived in the desired container module.
502void ExtractInstancesPass::extractInstances() {
503 // The list of ports to be added to an instance's parent module. Cleared and
504 // reused across instances.
505 SmallVector<std::pair<unsigned, PortInfo>> newPorts;
506 // The number of instances with the same prefix. Used to uniquify prefices.
507 DenseMap<StringRef, unsigned> prefixUniqueIDs;
508
509 SmallPtrSet<Operation *, 4> nlasToRemove;
510
511 auto &nlaTable = getAnalysis<NLATable>();
512
513 // Keep track of where the instance was originally.
514 for (auto &[inst, info] : extractionWorklist)
515 originalInstanceParents[inst] =
516 inst->getParentOfType<FModuleLike>().getModuleNameAttr();
517
518 while (!extractionWorklist.empty()) {
519 InstanceOp inst;
520 ExtractionInfo info;
521 std::tie(inst, info) = extractionWorklist.pop_back_val();
522
523 auto parent = inst->getParentOfType<FModuleOp>();
524
525 // Figure out the wiring prefix to use for this instance. If we are supposed
526 // to use a wiring prefix (`info.prefix` is non-empty), we assemble a
527 // `<prefix>_<N>` string, where `N` is an unsigned integer used to uniquifiy
528 // the prefix. This is very close to what the original Scala implementation
529 // of the pass does, which would group instances to be extracted by prefix
530 // and then iterate over them with the index in the group being used as `N`.
531 StringRef prefix;
532 auto &instPrefixEntry = instPrefixNamesPair[inst];
533 instPrefixEntry.second = inst.getInstanceNameAttr();
534 if (!info.prefix.empty()) {
535 auto &prefixSlot = instPrefixEntry.first;
536 if (prefixSlot.empty()) {
537 auto idx = prefixUniqueIDs[info.prefix]++;
538 (Twine(info.prefix) + "_" + Twine(idx)).toVector(prefixSlot);
539 }
540 prefix = prefixSlot;
541 }
542
543 /// Return true if this extraction should stop at the DUT or if it should
544 /// continue beyond it.
545 bool stopAtDUT = !moveDut && !info.wrapperModule.empty();
546
547 // If the instance is already in the right place (outside the DUT, already
548 // in the root module, or has hit a layer), there's nothing left for us to
549 // do. Otherwise we proceed to bubble it up one level in the hierarchy and
550 // add the resulting instances back to the worklist.
551 if (inst->getParentOfType<LayerBlockOp>() ||
552 !instanceInfo->anyInstanceInDesign(parent) ||
553 instanceGraph->lookup(parent)->noUses() ||
554 (stopAtDUT && instanceInfo->isDut(parent))) {
555 LLVM_DEBUG(llvm::dbgs() << "\nNo need to further move " << inst << "\n");
556 extractedInstances.push_back({inst, info});
557 continue;
558 }
559 LLVM_DEBUG({
560 llvm::dbgs() << "\nMoving ";
561 if (!prefix.empty())
562 llvm::dbgs() << "`" << prefix << "` ";
563 llvm::dbgs() << inst << "\n";
564 });
565
566 // Add additional ports to the parent module as a replacement for the
567 // instance port signals once the instance is extracted.
568 unsigned numParentPorts = parent.getNumPorts();
569 unsigned numInstPorts = inst.getNumResults();
570
571 for (unsigned portIdx = 0; portIdx < numInstPorts; ++portIdx) {
572 // Assemble the new port name as "<prefix>_<name>", where the prefix is
573 // provided by the extraction annotation.
574 auto name = inst.getPortName(portIdx);
575 auto nameAttr = StringAttr::get(
576 &getContext(),
577 prefix.empty() ? Twine(name) : Twine(prefix) + "_" + name);
578
579 PortInfo newPort{nameAttr,
580 type_cast<FIRRTLType>(inst.getResult(portIdx).getType()),
581 direction::flip(inst.getPortDirection(portIdx))};
582 newPort.loc = inst.getResult(portIdx).getLoc();
583 newPorts.push_back({numParentPorts, newPort});
584 LLVM_DEBUG(llvm::dbgs()
585 << "- Adding port " << newPort.direction << " "
586 << newPort.name.getValue() << ": " << newPort.type << "\n");
587 }
588 parent.insertPorts(newPorts);
589 anythingChanged = true;
590
591 // Replace all uses of the existing instance ports with the newly-created
592 // module ports.
593 for (unsigned portIdx = 0; portIdx < numInstPorts; ++portIdx) {
594 inst.getResult(portIdx).replaceAllUsesWith(
595 parent.getArgument(numParentPorts + portIdx));
596 }
597 assert(inst.use_empty() && "instance ports should have been detached");
598 DenseSet<hw::HierPathOp> instanceNLAs;
599 // Get the NLAs that pass through the InstanceOp `inst`.
600 // This does not returns NLAs that have the `inst` as the leaf.
601 nlaTable.getInstanceNLAs(inst, instanceNLAs);
602 // Map of the NLAs, that are applied to the InstanceOp. That is the NLA
603 // terminates on the InstanceOp.
604 DenseMap<hw::HierPathOp, SmallVector<Annotation>> instNonlocalAnnos;
606 // Only consider annotations with a `circt.nonlocal` field.
607 auto nlaName = anno.getMember<FlatSymbolRefAttr>("circt.nonlocal");
608 if (!nlaName)
609 return false;
610 // Track the NLA.
611 if (hw::HierPathOp nla = nlaTable.getNLA(nlaName.getAttr())) {
612 instNonlocalAnnos[nla].push_back(anno);
613 instanceNLAs.insert(nla);
614 }
615 return true;
616 });
617
618 // Sort the instance NLAs we've collected by the NLA name to have a
619 // deterministic output.
620 SmallVector<hw::HierPathOp> sortedInstanceNLAs(instanceNLAs.begin(),
621 instanceNLAs.end());
622 llvm::sort(sortedInstanceNLAs,
623 [](auto a, auto b) { return a.getSymName() < b.getSymName(); });
624
625 // Move the original instance one level up such that it is right next to
626 // the instances of the parent module, and wire the instance ports up to
627 // the newly added parent module ports.
628 auto *instParentNode =
629 instanceGraph->lookup(cast<igraph::ModuleOpInterface>(*parent));
630 for (auto *instRecord : instParentNode->uses()) {
631 auto oldParentInst = dyn_cast<InstanceOp>(*instRecord->getInstance());
632 if (!oldParentInst) {
633 inst.emitError("cannot extract instance `")
634 << inst.getName() << "` through a non-InstanceOp parent";
635 anyFailures = true;
636 continue;
637 }
638 auto newParent = oldParentInst->getParentOfType<FModuleLike>();
639 LLVM_DEBUG(llvm::dbgs() << "- Updating " << oldParentInst << "\n");
640 auto newParentInst = cast<InstanceOp>(
641 oldParentInst.cloneWithInsertedPortsAndReplaceUses(newPorts));
642 if (newParentInst.getInnerSymAttr())
643 innerRefToInstances[getInnerRefTo(newParentInst)] = newParentInst;
644
645 auto newInst = cast<InstanceOp>(inst->clone());
646
647 // Ensure that the `inner_sym` of the instance is unique within the parent
648 // module we're extracting it to.
649 if (auto instSym = getInnerSymName(inst)) {
650 auto newName =
651 getModuleNamespace(newParent).newName(instSym.getValue());
652 if (newName != instSym.getValue())
653 newInst.setInnerSymAttr(
654 hw::InnerSymAttr::get(StringAttr::get(&getContext(), newName)));
655 }
656
657 // Add the moved instance and hook it up to the added ports.
658 ImplicitLocOpBuilder builder(inst.getLoc(), newParentInst);
659 builder.setInsertionPointAfter(newParentInst);
660 builder.insert(newInst);
661 if (newParentInst.getInnerSymAttr())
662 innerRefToInstances[getInnerRefTo(newInst)] = newInst;
663 for (unsigned portIdx = 0; portIdx < numInstPorts; ++portIdx) {
664 auto dst = newInst.getResult(portIdx);
665 auto src = newParentInst.getResult(numParentPorts + portIdx);
666 if (newPorts[portIdx].second.direction == Direction::In)
667 std::swap(src, dst);
668 MatchingConnectOp::create(builder, dst, src);
669 }
670
671 // Move the wiring prefix from the old to the new instance. We just look
672 // up the prefix for the old instance and if it exists, we remove it and
673 // assign it to the new instance. This has the effect of making the first
674 // new instance we create inherit the wiring prefix, and all additional
675 // new instances (e.g. through multiple instantiation of the parent) will
676 // pick a new prefix.
677 auto oldPrefix = instPrefixNamesPair.find(inst);
678 if (oldPrefix != instPrefixNamesPair.end()) {
679 LLVM_DEBUG(llvm::dbgs() << " - Reusing prefix `"
680 << oldPrefix->second.first << "`\n");
681 auto newPrefix = std::move(oldPrefix->second);
682 instPrefixNamesPair.erase(oldPrefix);
683 instPrefixNamesPair.insert({newInst, newPrefix});
684 }
685
686 // Inherit the old instance's extraction path.
687 extractionPaths.try_emplace(newInst); // (create entry first)
688 auto &extractionPath = (extractionPaths[newInst] = extractionPaths[inst]);
689 auto instInnerRef = getInnerRefTo(newParentInst);
690 innerRefToInstances[instInnerRef] = newParentInst;
691 extractionPath.push_back(instInnerRef);
692 originalInstanceParents.try_emplace(newInst); // (create entry first)
693 originalInstanceParents[newInst] = originalInstanceParents[inst];
694 // Record the Nonlocal annotations that need to be applied to the new
695 // Inst.
696 SmallVector<Annotation> newInstNonlocalAnnos;
697
698 // Update all NLAs that touch the moved instance.
699 for (auto nla : sortedInstanceNLAs) {
700 LLVM_DEBUG(llvm::dbgs() << " - Updating " << nla << "\n");
701
702 // Find the position of the instance in the NLA path. This is going to
703 // be the position at which we have to modify the NLA.
704 SmallVector<Attribute> nlaPath(nla.getNamepath().begin(),
705 nla.getNamepath().end());
706 unsigned nlaIdx = findInstanceInNLA(inst, nla);
707
708 // Handle the case where the instance no longer shows up in the NLA's
709 // path. This usually happens if the instance is extracted into multiple
710 // parents (because the current parent module is multiply instantiated).
711 // In that case NLAs that were specific to one instance may have been
712 // moved when we arrive at the second instance, and the NLA is already
713 // updated.
714 if (nlaIdx >= nlaPath.size()) {
715 LLVM_DEBUG(llvm::dbgs() << " - Instance no longer in path\n");
716 continue;
717 }
718 LLVM_DEBUG(llvm::dbgs() << " - Position " << nlaIdx << "\n");
719
720 // Handle the case where the NLA's path doesn't go through the
721 // instance's new parent module, which happens if the current parent
722 // module is multiply instantiated. In that case, we only move over NLAs
723 // that actually affect the instance through the new parent module.
724 if (nlaIdx > 0) {
725 auto innerRef = dyn_cast<InnerRefAttr>(nlaPath[nlaIdx - 1]);
726 if (innerRef &&
727 !(innerRef.getModule() == newParent.getModuleNameAttr() &&
728 innerRef.getName() == getInnerSymName(newParentInst))) {
729 LLVM_DEBUG(llvm::dbgs()
730 << " - Ignored since NLA parent " << innerRef
731 << " does not pass through extraction parent\n");
732 continue;
733 }
734 }
735
736 // There are two interesting cases now:
737 // - If `nlaIdx == 0`, the NLA is rooted at the module the instance was
738 // located in prior to extraction. This indicates that the NLA applies
739 // to all instances of that parent module. Since we are extracting
740 // *out* of that module, we have to create a new NLA rooted at the new
741 // parent module after extraction.
742 // - If `nlaIdx > 0`, the NLA is rooted further up in the hierarchy and
743 // we can simply remove the old parent module from the path.
744
745 // Handle the case where we need to come up with a new NLA for this
746 // instance since we've moved it past the module at which the old NLA
747 // was rooted at.
748 if (nlaIdx == 0) {
749 LLVM_DEBUG(llvm::dbgs() << " - Re-rooting " << nlaPath[0] << "\n");
750 assert(isa<InnerRefAttr>(nlaPath[0]) &&
751 "head of hierpath must be an InnerRefAttr");
752 nlaPath[0] = InnerRefAttr::get(newParent.getModuleNameAttr(),
753 getInnerSymName(newInst));
754
755 if (instParentNode->hasOneUse()) {
756 // Simply update the existing NLA since our parent is only
757 // instantiated once, and we therefore are not creating multiple
758 // instances through the extraction.
759 nlaTable.erase(nla);
760 nla.setNamepathAttr(builder.getArrayAttr(nlaPath));
761 for (auto anno : instNonlocalAnnos.lookup(nla))
762 newInstNonlocalAnnos.push_back(anno);
763 nlaTable.addNLA(nla);
764 LLVM_DEBUG(llvm::dbgs() << " - Modified to " << nla << "\n");
765 } else {
766 // Since we are extracting to multiple parent locations, create a
767 // new NLA for each instantiation site.
768 auto newNla = cloneWithNewNameAndPath(nla, nlaPath);
769 for (auto anno : instNonlocalAnnos.lookup(nla)) {
770 anno.setMember("circt.nonlocal",
771 FlatSymbolRefAttr::get(newNla.getSymNameAttr()));
772 newInstNonlocalAnnos.push_back(anno);
773 }
774
775 nlaTable.addNLA(newNla);
776 LLVM_DEBUG(llvm::dbgs() << " - Created " << newNla << "\n");
777 // CAVEAT(fschuiki): This results in annotations in the subhierarchy
778 // below `inst` with the old NLA symbol name, instead of those
779 // annotations duplicated for each of the newly-created NLAs. This
780 // shouldn't come up in our current use cases, but is a weakness of
781 // the current implementation. Instead, we should keep an NLA
782 // replication table that we fill with mappings from old NLA names
783 // to lists of new NLA names. A post-pass would then traverse the
784 // entire subhierarchy and go replicate all annotations with the old
785 // names.
786 inst.emitWarning("extraction of instance `")
787 << inst.getInstanceName()
788 << "` could break non-local annotations rooted at `"
789 << parent.getModuleName() << "`";
790 }
791 continue;
792 }
793
794 // In the subequent code block we are going to remove one element from
795 // the NLA path, corresponding to the fact that the extracted instance
796 // has moved up in the hierarchy by one level. Removing that element may
797 // leave the NLA in a degenerate state, with only a single element in
798 // its path. If that is the case we have to convert the NLA into a
799 // regular local annotation.
800 if (nlaPath.size() == 2) {
801 for (auto anno : instNonlocalAnnos.lookup(nla)) {
802 anno.removeMember("circt.nonlocal");
803 newInstNonlocalAnnos.push_back(anno);
804 LLVM_DEBUG(llvm::dbgs() << " - Converted to local "
805 << anno.getDict() << "\n");
806 }
807 nlaTable.erase(nla);
808 nlasToRemove.insert(nla);
809 continue;
810 }
811
812 // At this point the NLA looks like `NewParent::X, OldParent::BB`, and
813 // the `nlaIdx` points at `OldParent::BB`. To make our lives easier,
814 // since we know that `nlaIdx` is a `InnerRefAttr`, we'll modify
815 // `OldParent::BB` to be `NewParent::BB` and delete `NewParent::X`.
816 StringAttr parentName =
817 cast<InnerRefAttr>(nlaPath[nlaIdx - 1]).getModule();
818 Attribute newRef;
819 if (isa<InnerRefAttr>(nlaPath[nlaIdx]))
820 newRef = InnerRefAttr::get(parentName, getInnerSymName(newInst));
821 else
822 newRef = FlatSymbolRefAttr::get(parentName);
823 LLVM_DEBUG(llvm::dbgs()
824 << " - Replacing " << nlaPath[nlaIdx - 1] << " and "
825 << nlaPath[nlaIdx] << " with " << newRef << "\n");
826 nlaPath[nlaIdx] = newRef;
827 nlaPath.erase(nlaPath.begin() + nlaIdx - 1);
828
829 if (isa<FlatSymbolRefAttr>(newRef)) {
830 // Since the original NLA ended at the instance's parent module, there
831 // is no guarantee that the instance is the sole user of the NLA (as
832 // opposed to the original NLA explicitly naming the instance). Create
833 // a new NLA.
834 auto newNla = cloneWithNewNameAndPath(nla, nlaPath);
835 nlaTable.addNLA(newNla);
836 LLVM_DEBUG(llvm::dbgs() << " - Created " << newNla << "\n");
837 for (auto anno : instNonlocalAnnos.lookup(nla)) {
838 anno.setMember("circt.nonlocal",
839 FlatSymbolRefAttr::get(newNla.getSymNameAttr()));
840 newInstNonlocalAnnos.push_back(anno);
841 }
842 } else {
843 nla.setNamepathAttr(builder.getArrayAttr(nlaPath));
844 LLVM_DEBUG(llvm::dbgs() << " - Modified to " << nla << "\n");
845 for (auto anno : instNonlocalAnnos.lookup(nla))
846 newInstNonlocalAnnos.push_back(anno);
847 }
848
849 // No update to NLATable required, since it will be deleted from the
850 // parent, and it should already exist in the new parent module.
851 continue;
852 }
853 AnnotationSet newInstAnnos(newInst);
854 newInstAnnos.addAnnotations(newInstNonlocalAnnos);
855 newInstAnnos.applyToOperation(newInst);
856
857 // Add the moved instance to the extraction worklist such that it gets
858 // bubbled up further if needed.
859 extractionWorklist.push_back({newInst, info});
860 LLVM_DEBUG(llvm::dbgs() << " - Updated to " << newInst << "\n");
861
862 // Keep instance graph up-to-date.
863 instanceGraph->replaceInstance(oldParentInst, newParentInst);
864 oldParentInst.erase();
865 }
866 // Remove the obsolete NLAs from the instance of the parent module, since
867 // the extracted instance no longer resides in that module and any NLAs to
868 // it no longer go through the parent module.
869 nlaTable.removeNLAsfromModule(instanceNLAs, parent.getNameAttr());
870
871 // Clean up the original instance.
872 inst.erase();
873 newPorts.clear();
874 }
875
876 // Remove unused NLAs.
877 for (Operation *op : nlasToRemove) {
878 LLVM_DEBUG(llvm::dbgs() << "Removing obsolete " << *op << "\n");
879 op->erase();
880 }
881}
882
883/// Group instances into submodules after they have been moved upwards. This
884/// only occurs for instances that had the corresponding `dest` field of the
885/// annotation set.
886void ExtractInstancesPass::groupInstances() {
887 // Group the extracted instances by their wrapper module name and their parent
888 // module. Note that we cannot group instances that landed in different parent
889 // modules into the same submodule, so we use that parent module as a grouping
890 // key.
892 instsByWrapper;
893 for (auto &[inst, info] : extractedInstances) {
894 if (!info.wrapperModule.empty())
895 instsByWrapper[{inst->getParentOfType<FModuleOp>(), info.wrapperModule}]
896 .push_back(inst);
897 }
898 if (instsByWrapper.empty())
899 return;
900 LLVM_DEBUG(llvm::dbgs() << "\nGrouping instances into wrappers\n");
901
902 // Generate the wrappers.
903 SmallVector<PortInfo> ports;
904 auto &nlaTable = getAnalysis<NLATable>();
905
906 for (auto &[parentAndWrapperName, insts] : instsByWrapper) {
907 auto [parentOp, wrapperName] = parentAndWrapperName;
908 auto parent = cast<FModuleOp>(parentOp);
909 LLVM_DEBUG(llvm::dbgs() << "- Wrapper `" << wrapperName << "` in `"
910 << parent.getModuleName() << "` with "
911 << insts.size() << " instances\n");
912 OpBuilder builder(parentOp);
913
914 // Uniquify the wrapper name.
915 auto wrapperModuleName =
916 builder.getStringAttr(circuitNamespace.newName(wrapperName));
917 auto wrapperInstName =
918 builder.getStringAttr(getModuleNamespace(parent).newName(wrapperName));
919
920 // Assemble a list of ports for the wrapper module, which is basically just
921 // a concatenation of the wrapped instance ports. Also keep track of the
922 // NLAs that target the grouped instances since these will have to pass
923 // through the wrapper module.
924 ports.clear();
925 for (auto inst : insts) {
926 // Determine the ports for the wrapper.
927 StringRef prefix(instPrefixNamesPair[inst].first);
928 unsigned portNum = inst.getNumResults();
929 for (unsigned portIdx = 0; portIdx < portNum; ++portIdx) {
930 auto name = inst.getPortName(portIdx);
931 auto nameAttr = builder.getStringAttr(
932 prefix.empty() ? Twine(name) : Twine(prefix) + "_" + name);
933 PortInfo port{nameAttr,
934 type_cast<FIRRTLType>(inst.getResult(portIdx).getType()),
935 inst.getPortDirection(portIdx)};
936 port.loc = inst.getResult(portIdx).getLoc();
937 ports.push_back(port);
938 }
939
940 // Set of NLAs that have a reference to this InstanceOp `inst`.
941 DenseSet<hw::HierPathOp> instNlas;
942 // Get the NLAs that pass through the `inst`, and not end at it.
943 nlaTable.getInstanceNLAs(inst, instNlas);
944 AnnotationSet instAnnos(inst);
945 // Get the NLAs that end at the InstanceOp, that is the Nonlocal
946 // annotations that apply to the InstanceOp.
947 for (auto anno : instAnnos) {
948 auto nlaName = anno.getMember<FlatSymbolRefAttr>("circt.nonlocal");
949 if (!nlaName)
950 continue;
951 hw::HierPathOp nla = nlaTable.getNLA(nlaName.getAttr());
952 if (nla)
953 instNlas.insert(nla);
954 }
955 for (auto nla : instNlas) {
956 LLVM_DEBUG(llvm::dbgs() << " - Updating " << nla << "\n");
957
958 // Find the position of the instance in the NLA path. This is going to
959 // be the position at which we have to modify the NLA.
960 SmallVector<Attribute> nlaPath(nla.getNamepath().begin(),
961 nla.getNamepath().end());
962 unsigned nlaIdx = findInstanceInNLA(inst, nla);
963 assert(nlaIdx < nlaPath.size() && "instance not found in its own NLA");
964 LLVM_DEBUG(llvm::dbgs() << " - Position " << nlaIdx << "\n");
965
966 // The relevant part of the NLA is of the form `Top::bb`, which we want
967 // to expand to `Top::wrapperInst` and `Wrapper::bb`.
968 auto ref1 =
969 InnerRefAttr::get(parent.getModuleNameAttr(), wrapperInstName);
970 Attribute ref2;
971 if (auto innerRef = dyn_cast<InnerRefAttr>(nlaPath[nlaIdx]))
972 ref2 = InnerRefAttr::get(wrapperModuleName, innerRef.getName());
973 else
974 ref2 = FlatSymbolRefAttr::get(wrapperModuleName);
975 LLVM_DEBUG(llvm::dbgs() << " - Expanding " << nlaPath[nlaIdx]
976 << " to (" << ref1 << ", " << ref2 << ")\n");
977 nlaPath[nlaIdx] = ref1;
978 nlaPath.insert(nlaPath.begin() + nlaIdx + 1, ref2);
979 // CAVEAT: This is likely to conflict with additional users of `nla`
980 // that have nothing to do with this instance. Might need some NLATable
981 // machinery at some point to allow for these things to be updated.
982 nla.setNamepathAttr(builder.getArrayAttr(nlaPath));
983 LLVM_DEBUG(llvm::dbgs() << " - Modified to " << nla << "\n");
984 // Add the NLA to the wrapper module.
985 nlaTable.addNLAtoModule(nla, wrapperModuleName);
986 }
987 }
988
989 // Create the wrapper module.
990 auto wrapper = FModuleOp::create(
991 builder, builder.getUnknownLoc(), wrapperModuleName,
992 ConventionAttr::get(builder.getContext(), Convention::Internal), ports);
993 SymbolTable::setSymbolVisibility(wrapper, SymbolTable::Visibility::Private);
994
995 // Instantiate the wrapper module in the parent and replace uses of the
996 // extracted instances' ports with the corresponding wrapper module ports.
997 // This will essentially disconnect the extracted instances.
998 builder.setInsertionPointToStart(parent.getBodyBlock());
999 auto wrapperInst = InstanceOp::create(
1000 builder, wrapper.getLoc(), wrapper, wrapperName,
1001 NameKindEnum::DroppableName, ArrayRef<Attribute>{},
1002 /*portAnnotations=*/ArrayRef<Attribute>{}, /*lowerToBind=*/false,
1003 /*doNotPrint=*/false, hw::InnerSymAttr::get(wrapperInstName));
1004 unsigned portIdx = 0;
1005 for (auto inst : insts)
1006 for (auto result : inst.getResults())
1007 result.replaceAllUsesWith(wrapperInst.getResult(portIdx++));
1008
1009 // Move all instances into the wrapper module and wire them up to the
1010 // wrapper ports.
1011 portIdx = 0;
1012 builder.setInsertionPointToStart(wrapper.getBodyBlock());
1013 for (auto inst : insts) {
1014 inst->remove();
1015 builder.insert(inst);
1016 for (auto result : inst.getResults()) {
1017 Value dst = result;
1018 Value src = wrapper.getArgument(portIdx);
1019 if (ports[portIdx].direction == Direction::Out)
1020 std::swap(dst, src);
1021 MatchingConnectOp::create(builder, result.getLoc(), dst, src);
1022 ++portIdx;
1023 }
1024 }
1025 }
1026}
1027
1028/// Generate trace files, which are plain text metadata files that list the
1029/// hierarchical path where each instance was extracted from. The file lists one
1030/// instance per line in the form `<prefix> -> <original-path>`.
1031void ExtractInstancesPass::createTraceFiles(ClassOp &sifiveMetadataClass) {
1032 LLVM_DEBUG(llvm::dbgs() << "\nGenerating trace files\n");
1033
1034 // Group the extracted instances by their trace file name.
1036 for (auto &[inst, info] : extractedInstances)
1037 if (!info.traceFilename.empty())
1038 instsByTraceFile[info.traceFilename].push_back(inst);
1039
1040 // Generate the trace files.
1041 SmallVector<Attribute> symbols;
1043 if (sifiveMetadataClass && !extractMetadataClass)
1044 createSchema();
1045
1046 auto addPortsToClass = [&](ArrayRef<std::pair<Value, Twine>> objFields,
1047 ClassOp classOp) {
1048 auto builderOM = mlir::ImplicitLocOpBuilder::atBlockEnd(
1049 classOp.getLoc(), classOp.getBodyBlock());
1050 auto portIndex = classOp.getNumPorts();
1051 SmallVector<std::pair<unsigned, PortInfo>> newPorts;
1052 for (auto [index, port] : enumerate(objFields)) {
1053 portIndex += index;
1054 auto obj = port.first;
1055 newPorts.emplace_back(
1056 portIndex,
1057 PortInfo(builderOM.getStringAttr(port.second + Twine(portIndex)),
1058 obj.getType(), Direction::Out));
1059 auto blockarg =
1060 classOp.getBodyBlock()->addArgument(obj.getType(), obj.getLoc());
1061 PropAssignOp::create(builderOM, blockarg, obj);
1062 }
1063 classOp.insertPorts(newPorts);
1064 };
1065
1066 HierPathCache pathCache(circuitOp, *symbolTable);
1067 SmallVector<std::pair<Value, Twine>> classFields;
1068 for (auto &[fileName, insts] : instsByTraceFile) {
1069 LLVM_DEBUG(llvm::dbgs() << "- " << fileName << "\n");
1070 std::string buffer;
1071 llvm::raw_string_ostream os(buffer);
1072 symbols.clear();
1073 symbolIndices.clear();
1074
1075 auto addSymbol = [&](Attribute symbol) {
1076 unsigned id;
1077 auto it = symbolIndices.find(symbol);
1078 if (it != symbolIndices.end()) {
1079 id = it->second;
1080 } else {
1081 id = symbols.size();
1082 symbols.push_back(symbol);
1083 symbolIndices.insert({symbol, id});
1084 }
1085 os << "{{" << id << "}}";
1086 };
1087
1088 auto file = getOrCreateFile(fileName);
1089 auto builder = OpBuilder::atBlockEnd(file.getBody());
1090 for (auto inst : insts) {
1091 StringRef prefix(instPrefixNamesPair[inst].first);
1092 StringAttr origInstName(instPrefixNamesPair[inst].second);
1093 if (prefix.empty()) {
1094 LLVM_DEBUG(llvm::dbgs() << " - Skipping `" << inst.getName()
1095 << "` since it has no extraction prefix\n");
1096 continue;
1097 }
1098 ArrayRef<InnerRefAttr> path(extractionPaths[inst]);
1099 if (path.empty()) {
1100 LLVM_DEBUG(llvm::dbgs() << " - Skipping `" << inst.getName()
1101 << "` since it has not been moved\n");
1102 continue;
1103 }
1104 LLVM_DEBUG(llvm::dbgs()
1105 << " - " << prefix << ": " << inst.getName() << "\n");
1106 os << prefix << " -> ";
1107
1108 if (sifiveMetadataClass) {
1109 // Create the entry for this extracted instance in the metadata class.
1110 auto builderOM = mlir::ImplicitLocOpBuilder::atBlockEnd(
1111 inst.getLoc(), extractMetadataClass.getBodyBlock());
1112 auto prefixName = StringConstantOp::create(builderOM, prefix);
1113 auto object = ObjectOp::create(builderOM, schemaClass, prefix);
1114 auto fPrefix =
1115 ObjectSubfieldOp::create(builderOM, object, prefixNameFieldId);
1116 PropAssignOp::create(builderOM, fPrefix, prefixName);
1117
1118 auto targetInstance = innerRefToInstances[path.front()];
1119 SmallVector<Attribute> pathOpAttr(llvm::reverse(path));
1120 auto nla = pathCache.getOpFor(
1121 ArrayAttr::get(circuitOp->getContext(), pathOpAttr));
1122
1123 auto pathOp = createPathRef(targetInstance, nla, builderOM);
1124 auto fPath = ObjectSubfieldOp::create(builderOM, object, pathFieldId);
1125 PropAssignOp::create(builderOM, fPath, pathOp);
1126 auto fFile =
1127 ObjectSubfieldOp::create(builderOM, object, fileNameFieldId);
1128 auto fileNameOp = StringConstantOp::create(
1129 builderOM, builder.getStringAttr(fileName));
1130 PropAssignOp::create(builderOM, fFile, fileNameOp);
1131
1132 auto finstName =
1133 ObjectSubfieldOp::create(builderOM, object, instNameFieldId);
1134 auto instNameOp = StringConstantOp::create(builderOM, origInstName);
1135 PropAssignOp::create(builderOM, finstName, instNameOp);
1136
1137 // Now add this to the output field of the class.
1138 classFields.emplace_back(object, prefix + "_field");
1139 }
1140 // HACK: To match the Scala implementation, we strip all non-DUT modules
1141 // from the path and make the path look like it's rooted at the first DUT
1142 // module (so `TestHarness.dut.foo.bar` becomes `DUTModule.foo.bar`).
1143 while (!path.empty() &&
1144 !instanceInfo->anyInstanceInDesign(cast<igraph::ModuleOpInterface>(
1145 symbolTable->lookup(path.back().getModule())))) {
1146 LLVM_DEBUG(llvm::dbgs()
1147 << " - Dropping non-DUT segment " << path.back() << "\n");
1148 path = path.drop_back();
1149 }
1150 // HACK: This is extremely ugly. In case the instance was just moved by a
1151 // single level, the path may become empty. In that case we simply use the
1152 // instance's original parent before it was moved.
1153 addSymbol(FlatSymbolRefAttr::get(path.empty()
1154 ? originalInstanceParents[inst]
1155 : path.back().getModule()));
1156 for (auto sym : llvm::reverse(path)) {
1157 os << ".";
1158 addSymbol(sym);
1159 }
1160 os << "." << origInstName.getValue();
1161 // The final instance name is excluded as this does not provide useful
1162 // additional information and could conflict with a name inside the final
1163 // module.
1164 os << "\n";
1165 }
1166
1167 // Put the information in a verbatim operation.
1168 sv::VerbatimOp::create(builder, builder.getUnknownLoc(), buffer,
1169 ValueRange{}, builder.getArrayAttr(symbols));
1170 }
1171 if (!classFields.empty()) {
1172 addPortsToClass(classFields, extractMetadataClass);
1173 // This extract instances metadata class, now needs to be instantiated
1174 // inside the SifiveMetadata class. This also updates its signature, so keep
1175 // the object of the SifiveMetadata class updated.
1176 auto builderOM = mlir::ImplicitLocOpBuilder::atBlockEnd(
1177 sifiveMetadataClass->getLoc(), sifiveMetadataClass.getBodyBlock());
1178 SmallVector<std::pair<Value, Twine>> classFields = {
1179 {ObjectOp::create(
1180 builderOM, extractMetadataClass,
1181 builderOM.getStringAttr("extract_instances_metadata")),
1182 "extractedInstances_field"}};
1183
1184 addPortsToClass(classFields, sifiveMetadataClass);
1185 auto *node = instanceGraph->lookup(sifiveMetadataClass);
1186 assert(node && node->hasOneUse());
1187 ObjectOp metadataObj = (*node->usesBegin())->getInstance<ObjectOp>();
1188 assert(metadataObj &&
1189 "expected the class to be instantiated by an object op");
1190 builderOM.setInsertionPoint(metadataObj);
1191 auto newObj =
1192 ObjectOp::create(builderOM, sifiveMetadataClass, metadataObj.getName());
1193 metadataObj->replaceAllUsesWith(newObj);
1194 metadataObj->remove();
1195 }
1196}
1197
1198void ExtractInstancesPass::createSchema() {
1199
1200 auto *context = circuitOp->getContext();
1201 auto unknownLoc = mlir::UnknownLoc::get(context);
1202 auto builderOM = mlir::ImplicitLocOpBuilder::atBlockEnd(
1203 unknownLoc, circuitOp.getBodyBlock());
1204 mlir::Type portsType[] = {
1205 stringType, // name
1206 pathType, // extracted instance path
1207 stringType, // filename
1208 stringType // instance name
1209 };
1210 StringRef portFields[] = {"name", "path", "filename", "inst_name"};
1211
1212 schemaClass = ClassOp::create(builderOM, "ExtractInstancesSchema", portFields,
1213 portsType);
1214
1215 // Now create the class that will instantiate the schema objects.
1216 SmallVector<PortInfo> mports;
1217 extractMetadataClass = ClassOp::create(
1218 builderOM, builderOM.getStringAttr("ExtractInstancesMetadata"), mports);
1219}
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static unsigned findInstanceInNLA(InstanceOp inst, hw::HierPathOp nla)
Find the location in an NLA that corresponds to a given instance (either by mentioning exactly the in...
static bool isAnnoInteresting(Annotation anno)
static std::vector< mlir::Value > toVector(mlir::ValueRange range)
static Location getLoc(DefSlot slot)
Definition Mem2Reg.cpp:218
static Block * getBodyBlock(FModuleLike mod)
static InstancePath empty
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
bool removeAnnotations(llvm::function_ref< bool(Annotation)> predicate)
Remove all annotations from this annotation set for which predicate returns true.
This class provides a read-only projection of an annotation.
DictionaryAttr getDict() const
Get the data dictionary of this attribute.
AttrClass getMember(StringAttr name) const
Return a member of the annotation.
void setMember(StringAttr name, Attribute value)
Add or set a member of the annotation to a value.
void removeMember(StringAttr name)
Remove a member of the annotation.
StringRef getClass() const
Return the 'class' that this annotation is representing.
bool isClass(Args... names) const
Return true if this annotation matches any of the specified class names.
This graph tracks modules and where they are instantiated.
Direction flip(Direction direction)
Flip a port direction.
PathOp createPathRef(Operation *op, hw::HierPathOp nla, mlir::ImplicitLocOpBuilder &builderOM)
Add the tracker annotation to the op and get a PathOp to the op.
hw::InnerRefAttr getInnerRefTo(const hw::InnerSymTarget &target, GetNamespaceCallback getNamespace)
Obtain an inner reference to the target (operation or port), adding an inner symbol as necessary.
StringAttr getInnerSymName(Operation *op)
Return the StringAttr for the inner_sym name, if it exists.
Definition FIRRTLOps.h:108
void info(Twine message)
Definition LSPUtils.cpp:20
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
The namespace of a CircuitOp, generally inhabited by modules.
Definition Namespace.h:24
A cache of existing HierPathOps, mostly used to facilitate HierPathOp reuse.
This holds the name and type that describes the module's ports.