CIRCT 22.0.0git
Loading...
Searching...
No Matches
LowerDomains.cpp
Go to the documentation of this file.
1//===- LowerDomains.cpp - Lower domain information to properties ----------===//
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 pass lowers all FIRRTL domain information into classes, objects, and
10// properties. This is part of the compilation of FIRRTL domains where they are
11// inferred and checked (See: the InferDomains pass) and then lowered (this
12// pass). After this pass runs, all domain information has been removed from
13// its original representation.
14//
15// Each domain is lowered into two classes: (1) a class that has the exact same
16// input/output properties as its corresponding domain and (2) a class that is
17// used to track the associations of the domain. Every input domain port is
18// lowered to an input of type (1) and an output of type (2). Every output
19// domain port is lowered to an output of type (2).
20//
21// Intuitively, (1) is the information that a user must specify about a domain
22// and (2) is the associations for that domain.
23//
24// This pass needs to run after InferDomains and before LowerClasses. This pass
25// assumes that all domain information is available. It is not written in such
26// a way that partial domain information can be lowered incrementally, e.g.,
27// interleaving InferDomains and LowerDomains with passes that incrementally add
28// domain information will not work. This is because LowerDomains is closer to
29// a conversion than a pass. It is expected that this is part of the FIRRTL to
30// HW pass pipeline.
31//
32// There are a number of limitations in this pass presently, much of which are
33// coupled to the representation of domains. Currently, domain information on
34// ports marks a port as being either a domain or having domain association
35// information, but not both. This precludes having aggregates that contain
36// domain types. (Or: this pass assumes that a pass like LowerOpenAggs has run
37// to do this splitting.) There are no requirements that LowerTypes has run,
38// assuming this post-LowerOpenAggs representation.
39//
40// As the representation of domains changes to allow for associations on fields
41// and domain types to be part of aggregates, this pass will require updates.
42//
43//===----------------------------------------------------------------------===//
44
47#include "circt/Support/Debug.h"
48#include "llvm/ADT/MapVector.h"
49#include "llvm/ADT/TypeSwitch.h"
50#include "llvm/Support/Debug.h"
51#include "llvm/Support/Threading.h"
52
53namespace circt {
54namespace firrtl {
55#define GEN_PASS_DEF_LOWERDOMAINS
56#include "circt/Dialect/FIRRTL/Passes.h.inc"
57} // namespace firrtl
58} // namespace circt
59
60using namespace circt;
61using namespace firrtl;
62using mlir::UnrealizedConversionCastOp;
63
64class LowerDomainsPass : public impl::LowerDomainsBase<LowerDomainsPass> {
65 using Base::Base;
66 void runOnOperation() override;
67};
68
69#define DEBUG_TYPE \
70 impl::LowerDomainsBase<LowerDomainsPass>::getArgumentName().data()
71
72namespace {
73/// Minimally track information about an association of a port to a domain.
74struct AssociationInfo {
75 /// The DistinctAttr (annotation) that is used to identify the port.
76 DistinctAttr distinctAttr;
77
78 /// The port's location. This is used to generate exact information about
79 /// certain property ops created later.
80 Location loc;
81};
82
83/// Track information about the lowering of a domain port.
84struct DomainInfo {
85 /// An instance of an object which will be used to track an instance of the
86 /// domain-lowered class (which is the identity of the domain) and all its
87 /// associations.
88 ObjectOp op;
89
90 /// The index of the optional input port that will be hooked up to a field of
91 /// the ObjectOp. This port is an instance of the domain-lowered class. If
92 /// this is created due to an output domain port, then this is nullopt.
93 std::optional<unsigned> inputPort;
94
95 /// The index of the output port that the ObjectOp will be connected to. This
96 /// port communicates back to the user information about the associations.
97 unsigned outputPort;
98
99 /// A conversion cast that is used to temporarily replace the port while it is
100 /// being deleted. If the port has no uses, then this will be empty. Note:
101 /// this is used as a true temporary and may be overwritten. During
102 /// `lowerModules()` this is used to store a module port temporary. During
103 /// `lowerInstances()`, this stores an instance port temporary.
104 UnrealizedConversionCastOp temp;
105
106 /// A vector of minimal association info that will be hooked up to the
107 /// associations of this ObjectOp.
108 SmallVector<AssociationInfo> associations{};
109
110 /// Return a DomainInfo for an input domain port. This will have both an
111 /// input port at the current index and an output port at the next index.
112 /// Other members are default-initialized and will be set later.
113 static DomainInfo input(unsigned portIndex) {
114 return DomainInfo({{}, portIndex, portIndex + 1, {}, {}});
115 }
116
117 /// Return a DomainInfo for an output domain port. This creates only an
118 /// output port at the current index. Other members are default-initialized
119 /// and will be set later.
120 static DomainInfo output(unsigned portIndex) {
121 return DomainInfo({{}, std::nullopt, portIndex, {}, {}});
122 }
123};
124
125/// Struct of the two classes created from a domain, an input class (which is
126/// one-to-one with the domain) and an output class (which tracks the input
127/// class and any associations).
128struct Classes {
129 /// The domain-lowered class.
130 ClassOp input;
131
132 /// A class tracking an instance of the input class and a list of
133 /// associations.
134 ClassOp output;
135};
136
137/// Thread safe, lazy pool of constant attributes
138class Constants {
139
140 /// Lazily constructed empty array attribute.
141 struct EmptyArray {
142 llvm::once_flag flag;
143 ArrayAttr attr;
144 };
145
146 /// Lazy constructed attributes necessary for building an output class.
147 struct ClassOut {
148 llvm::once_flag flag;
149 StringAttr domainInfoIn;
150 StringAttr domainInfoOut;
151 StringAttr associationsIn;
152 StringAttr associationsOut;
153 };
154
155public:
156 Constants(MLIRContext *context) : context(context) {}
157
158 /// Return an empty ArrayAttr.
159 ArrayAttr getEmptyArrayAttr() {
160 llvm::call_once(emptyArray.flag,
161 [&] { emptyArray.attr = ArrayAttr::get(context, {}); });
162 return emptyArray.attr;
163 }
164
165private:
166 /// Construct all the field info attributes.
167 void initClassOut() {
168 llvm::call_once(classOut.flag, [&] {
169 classOut.domainInfoIn = StringAttr::get(context, "domainInfo_in");
170 classOut.domainInfoOut = StringAttr::get(context, "domainInfo_out");
171 classOut.associationsIn = StringAttr::get(context, "associations_in");
172 classOut.associationsOut = StringAttr::get(context, "associations_out");
173 });
174 }
175
176public:
177 /// Return a "domainInfo_in" attr.
178 StringAttr getDomainInfoIn() {
179 initClassOut();
180 return classOut.domainInfoIn;
181 }
182
183 /// Return a "domainInfo_out" attr.
184 StringAttr getDomainInfoOut() {
185 initClassOut();
186 return classOut.domainInfoOut;
187 }
188
189 /// Return an "associations_in" attr.
190 StringAttr getAssociationsIn() {
191 initClassOut();
192 return classOut.associationsIn;
193 }
194
195 /// Return an "associations_out" attr.
196 StringAttr getAssociationsOut() {
197 initClassOut();
198 return classOut.associationsOut;
199 }
200
201private:
202 /// An MLIR context necessary for creating new attributes.
203 MLIRContext *context;
204
205 /// Lazily constructed attributes
206 EmptyArray emptyArray;
207 ClassOut classOut;
208};
209
210/// Replace the value with a temporary 0-1 unrealized conversion. Return the
211/// single conversion operation. This is used to "stub out" the users of a
212/// module or instance port while it is being converted from a domain port to a
213/// class port. This function lets us erase the port. This function is
214/// intended to be used with `splice` to replace the 0-1 conversion with a 1-1
215/// conversion once the new port, of the new type, is available.
216static UnrealizedConversionCastOp stubOut(Value value) {
217 if (!value.hasNUsesOrMore(1))
218 return {};
219
220 OpBuilder builder(value.getContext());
221 builder.setInsertionPointAfterValue(value);
222 auto temp = UnrealizedConversionCastOp::create(
223 builder, builder.getUnknownLoc(), {value.getType()}, {});
224 value.replaceAllUsesWith(temp.getResult(0));
225 return temp;
226}
227
228/// Replace a temporary 0-1 conversion cast with a 1-1 conversion cast. Erase
229/// the old conversion. Return the new conversion. This function is used to
230/// hook up a module or instance port to an operation user of the old type.
231/// After all ports have been spliced, the splice-operation-splice can be
232/// replaced with a new operation that handles the new port types.
233///
234/// Assumptions:
235///
236/// 1. `temp` is either null or a 0-1 conversion cast.
237/// 2. If `temp` is non-null, then `value` is non-null.
238static UnrealizedConversionCastOp splice(UnrealizedConversionCastOp temp,
239 Value value) {
240 if (!temp)
241 return {};
242
243 // This must be a 0-1 unrealized conversion. Anything else is unexpected.
244 assert(temp && temp.getNumResults() == 1 && temp.getNumOperands() == 0);
245
246 // Value must be non-null.
247 assert(value);
248
249 auto oldValue = temp.getResult(0);
250
251 OpBuilder builder(temp);
252 auto splice = UnrealizedConversionCastOp::create(
253 builder, builder.getUnknownLoc(), {oldValue.getType()}, {value});
254 oldValue.replaceAllUsesWith(splice.getResult(0));
255 temp.erase();
256 return splice;
257}
258
259/// Class that is used to lower a module that may contain domain ports. This is
260/// intended to be used by calling `lowerModule()` to lower the module. This
261/// builds up state in the class which is then used when calling
262/// `lowerInstances()` to update all instantiations of this module. If
263/// multi-threading, care needs to be taken to only call `lowerInstances()`
264/// when instantiating modules are not _also_ being updated.
265class LowerModule {
266
267public:
268 LowerModule(FModuleLike op, const DenseMap<Attribute, Classes> &classes,
269 Constants &constants, InstanceGraph &instanceGraph)
270 : op(op), eraseVector(op.getNumPorts()), domainToClasses(classes),
271 constants(constants), instanceGraph(instanceGraph) {}
272
273 /// Lower the associated module. Replace domain ports with input/output class
274 /// ports.
275 LogicalResult lowerModule();
276
277 /// Lower all instances of the associated module. This relies on state built
278 /// up during `lowerModule` and must be run _afterwards_.
279 LogicalResult lowerInstances();
280
281private:
282 /// The module this class is lowering
283 FModuleLike op;
284
285 /// Ports that should be erased
286 BitVector eraseVector;
287
288 /// The ports that should be inserted, _after deletion_ by application of
289 /// `eraseVector`.
290 SmallVector<std::pair<unsigned, PortInfo>> newPorts;
291
292 /// A mapping of old result to new result
293 SmallVector<std::pair<unsigned, unsigned>> resultMap;
294
295 /// Mapping of domain name to the lowered input and output class
296 const DenseMap<Attribute, Classes> &domainToClasses;
297
298 /// Lazy constant pool
299 Constants &constants;
300
301 /// Reference to an instance graph. This _will_ be mutated.
302 ///
303 /// TODO: The mutation of this is _not_ thread safe. This needs to be fixed
304 /// if this pass is parallelized.
305 InstanceGraph &instanceGraph;
306
307 // Information about a domain. This is built up during the first iteration
308 // over the ports. This needs to preserve insertion order.
309 llvm::MapVector<unsigned, DomainInfo> indexToDomain;
310};
311
312LogicalResult LowerModule::lowerModule() {
313 // TOOD: Is there an early exit condition here? It is not as simple as
314 // checking for domain ports as the module may have no domain ports, but have
315 // been modified by an earlier `lowerInstances()` call.
316
317 // Much of the lowering is conditioned on whether or not this module has a
318 // body. If it has a body, then we need to instantiate an object for each
319 // domain port and hook up all the domain ports to annotations added to each
320 // associated port. Skip modules which don't have domains.
321 auto shouldProcess =
322 TypeSwitch<Operation *, std::optional<Block *>>(op)
323 .Case<FModuleOp>([](auto op) { return op.getBodyBlock(); })
324 .Case<FExtModuleOp>([](auto) { return nullptr; })
325 // Skip all other modules.
326 .Default([](auto) { return std::nullopt; });
327 if (!shouldProcess)
328 return success();
329 Block *body = *shouldProcess;
330
331 auto *context = op.getContext();
332
333 // The new port annotations. These will be set after all deletions and
334 // insertions.
335 SmallVector<Attribute> portAnnotations;
336
337 // Iterate over the ports, staging domain ports for removal and recording the
338 // associations of non-domain ports. After this, domain ports will be deleted
339 // and then class ports will be inserted. This loop therefore needs to track
340 // three indices:
341 // 1. i tracks the original port index.
342 // 2. iDel tracks the port index after deletion.
343 // 3. iIns tracks the port index after insertion.
344 OpBuilder::InsertPoint insertPoint;
345 if (body)
346 insertPoint = {body, body->begin()};
347 auto ports = op.getPorts();
348 for (unsigned i = 0, iDel = 0, iIns = 0, e = op.getNumPorts(); i != e; ++i) {
349 auto port = cast<PortInfo>(ports[i]);
350
351 // Mark domain type ports for removal. Add information to `domainInfo`.
352 if (auto domain = dyn_cast_or_null<FlatSymbolRefAttr>(port.domains)) {
353 eraseVector.set(i);
354
355 // Instantiate a domain object with association information.
356 auto [classIn, classOut] = domainToClasses.at(domain.getAttr());
357
358 indexToDomain[i] = port.direction == Direction::In
359 ? DomainInfo::input(iIns)
360 : DomainInfo::output(iIns);
361
362 if (body) {
363 // Insert objects in-order at the top of the module's body. These
364 // cannot be inserted at the end as they may have users.
365 ImplicitLocOpBuilder builder(port.loc, context);
366 builder.restoreInsertionPoint(insertPoint);
367
368 // Create the object, add information about it to domain info.
369 auto object = ObjectOp::create(
370 builder, classOut,
371 StringAttr::get(context, Twine(port.name) + "_object"));
372 instanceGraph.lookup(op)->addInstance(object,
373 instanceGraph.lookup(classOut));
374 indexToDomain[i].op = object;
375 indexToDomain[i].temp = stubOut(body->getArgument(i));
376
377 // Save the insertion point for the next go-around. This allows the
378 // objects to be inserted in port order as opposed to reverse port
379 // order.
380 insertPoint = builder.saveInsertionPoint();
381 }
382
383 // Add input and output property ports that encode the property inputs
384 // (which the user must provide for the domain) and the outputs that
385 // encode this information and the associations. Keep iIns up to date
386 // based on the number of ports added.
387 if (port.direction == Direction::In) {
388 newPorts.push_back({iDel, PortInfo(port.name, classIn.getInstanceType(),
389 Direction::In)});
390 portAnnotations.push_back(constants.getEmptyArrayAttr());
391 ++iIns;
392 }
393 newPorts.push_back(
394 {iDel, PortInfo(StringAttr::get(context, Twine(port.name) + "_out"),
395 classOut.getInstanceType(), Direction::Out)});
396 ++iIns;
397
398 // Update annotations.
399 portAnnotations.push_back(constants.getEmptyArrayAttr());
400
401 // Don't increment the iDel since we deleted one port.
402 continue;
403 }
404
405 // This is a non-domain port. It will NOT be deleted. Increment both
406 // indices.
407 ++iDel;
408 ++iIns;
409
410 // If this port has domain associations, then we need to add port annotation
411 // trackers. These will be hooked up to the Object's associations later.
412 // However, if there is no domain information, then annotations do not need
413 // to be modified. Early continue first, adding trackers otherwise. Only
414 // create one tracker for all associations.
415 ArrayAttr domainAttr = cast_or_null<ArrayAttr>(port.domains);
416 if (!domainAttr || domainAttr.empty()) {
417 portAnnotations.push_back(port.annotations.getArrayAttr());
418 continue;
419 }
420
421 SmallVector<Annotation> newAnnotations;
422 DistinctAttr id;
423 for (auto indexAttr : domainAttr.getAsRange<IntegerAttr>()) {
424 if (!id) {
425 id = DistinctAttr::create(UnitAttr::get(context));
426 newAnnotations.push_back(Annotation(DictionaryAttr::getWithSorted(
427 context, {{"class", StringAttr::get(context, "circt.tracker")},
428 {"id", id}})));
429 }
430 indexToDomain[indexAttr.getUInt()].associations.push_back({id, port.loc});
431 }
432 if (!newAnnotations.empty())
433 port.annotations.addAnnotations(newAnnotations);
434 portAnnotations.push_back(port.annotations.getArrayAttr());
435 }
436
437 // Erase domain ports and clear domain association information.
438 op.erasePorts(eraseVector);
439 op.setDomainInfoAttr(constants.getEmptyArrayAttr());
440
441 // Insert new property ports and hook these up to the object that was
442 // instantiated earlier.
443 op.insertPorts(newPorts);
444
445 if (body) {
446 for (auto const &[_, info] : indexToDomain) {
447 auto [object, inputPort, outputPort, temp, associations] = info;
448 OpBuilder builder(object);
449 builder.setInsertionPointAfter(object);
450 // Hook up domain ports. If this was an input domain port, then drive the
451 // object's "domain in" port with the new input class port. Splice
452 // references to the old port with the new port---users (e.g., domain
453 // defines) will be updated later. If an output, then splice to the
454 // object's "domain in" port. If this participates in domain defines,
455 // this will be hoked up later.
456 auto subDomainInfoIn =
457 ObjectSubfieldOp::create(builder, object.getLoc(), object, 0);
458 if (inputPort) {
459 PropAssignOp::create(builder, object.getLoc(), subDomainInfoIn,
460 body->getArgument(*inputPort));
461 splice(temp, body->getArgument(*inputPort));
462 } else {
463 splice(temp, subDomainInfoIn);
464 }
465
466 // Hook up the "association in" port.
467 auto subAssociations =
468 ObjectSubfieldOp::create(builder, object.getLoc(), object, 2);
469 SmallVector<Value> paths;
470 for (auto [id, loc] : associations) {
471 paths.push_back(PathOp::create(
472 builder, loc, TargetKindAttr::get(context, TargetKind::Reference),
473 id));
474 }
475 auto list = ListCreateOp::create(
476 builder, object.getLoc(),
477 ListType::get(context, cast<PropertyType>(PathType::get(context))),
478 paths);
479 PropAssignOp::create(builder, object.getLoc(), subAssociations, list);
480
481 // Connect the object to the output port.
482 PropAssignOp::create(builder, object.getLoc(),
483 body->getArgument(outputPort), object);
484 }
485
486 // Remove all domain users. Delay deleting conversions until the module
487 // body is walked as it is possible that conversions have multiple users.
488 //
489 // This has the effect of removing all the conversions that we created.
490 // Note: this relies on the fact that we don't create conversions if there
491 // are no users. (See early exits in `stubOut` and `splice`.)
492 //
493 // Note: this cannot visit conversions directly as we don't have guarantees
494 // that there won't be other conversions flying around. E.g., LowerDPI
495 // leaves conversions that are cleaned up by LowerToHW. (This is likely
496 // wrong, but it doesn't cost us anything to do it this way.)
497 DenseSet<Operation *> conversionsToErase;
498 auto walkResult = op.walk([&conversionsToErase](Operation *walkOp) {
499 // Handle UnsafeDomainCastOp.
500 if (auto castOp = dyn_cast<UnsafeDomainCastOp>(walkOp)) {
501 for (auto value : castOp.getDomains()) {
502 auto *conversion = value.getDefiningOp();
503 assert(isa<UnrealizedConversionCastOp>(conversion));
504 conversionsToErase.insert(conversion);
505 }
506
507 castOp.getResult().replaceAllUsesWith(castOp.getInput());
508 castOp.erase();
509 return WalkResult::advance();
510 }
511
512 // Handle DomainDefineOp. Skip all other operations.
513 auto defineOp = dyn_cast<DomainDefineOp>(walkOp);
514 if (!defineOp)
515 return WalkResult::advance();
516
517 // Only visit domain define ops which have conversioncast source and
518 // destination.
519 auto src = dyn_cast<UnrealizedConversionCastOp>(
520 defineOp.getSrc().getDefiningOp());
521 auto dest = dyn_cast<UnrealizedConversionCastOp>(
522 defineOp.getDest().getDefiningOp());
523 if (!src || !dest)
524 return WalkResult::advance();
525 assert(src.getNumOperands() == 1 && src.getNumResults() == 1);
526 assert(dest.getNumOperands() == 1 && dest.getNumResults() == 1);
527
528 conversionsToErase.insert(src);
529 conversionsToErase.insert(dest);
530
531 OpBuilder builder(defineOp);
532 PropAssignOp::create(builder, defineOp.getLoc(), dest.getOperand(0),
533 src.getOperand(0));
534 defineOp->erase();
535 return WalkResult::advance();
536 });
537
538 // The walk, as written cannot fail. Defensively check in assert-enabled
539 // builds that this assumption isn't violated.
540 assert(!walkResult.wasInterrupted());
541
542 // Erase all the conversions.
543 for (auto *op : conversionsToErase)
544 op->erase();
545 }
546
547 // Set new port annotations.
548 op.setPortAnnotationsAttr(ArrayAttr::get(context, portAnnotations));
549
550 return success();
551}
552
553LogicalResult LowerModule::lowerInstances() {
554 // Early exit if there is no work to do.
555 if (eraseVector.none() && newPorts.empty())
556 return success();
557
558 // TODO: There is nothing to do unless this instance is a module or external
559 // module. This mirros code in the `lowerModule` member function. Figure out
560 // a way to clean this up, possible by making `LowerModule` a true noop if
561 // this is not one of these kinds of modulelikes.
562 if (!isa<FModuleOp, FExtModuleOp>(op))
563 return success();
564
565 auto *node = instanceGraph.lookup(cast<igraph::ModuleOpInterface>(*op));
566 for (auto *use : llvm::make_early_inc_range(node->uses())) {
567 auto instanceOp = dyn_cast<InstanceOp>(*use->getInstance());
568 if (!instanceOp) {
569 use->getInstance().emitOpError()
570 << "has an unimplemented lowering in LowerDomains";
571 return failure();
572 }
573 LLVM_DEBUG(llvm::dbgs()
574 << " - " << instanceOp.getInstanceName() << "\n");
575
576 for (auto i : eraseVector.set_bits())
577 indexToDomain[i].temp = stubOut(instanceOp.getResult(i));
578
579 auto erased = instanceOp.cloneWithErasedPortsAndReplaceUses(eraseVector);
580 auto inserted = erased.cloneWithInsertedPortsAndReplaceUses(newPorts);
581 instanceGraph.replaceInstance(instanceOp, inserted);
582
583 for (auto &[i, info] : indexToDomain) {
584 Value splicedValue;
585 if (info.inputPort) {
586 // Handle input port. Just hook it up.
587 splicedValue = inserted.getResult(*info.inputPort);
588 } else {
589 // Handle output port. Splice in the output field that contains the
590 // domain object. This requires creating an object subfield.
591 OpBuilder builder(inserted);
592 builder.setInsertionPointAfter(inserted);
593 splicedValue = ObjectSubfieldOp::create(
594 builder, inserted.getLoc(), inserted.getResult(info.outputPort), 1);
595 }
596
597 splice(info.temp, splicedValue);
598 }
599
600 instanceOp.erase();
601 erased.erase();
602 }
603
604 return success();
605}
606
607/// Class used to lwoer a circuit that contains domains. This hides any state
608/// that may need to be cleared across invocations of this pass to keep the
609/// actual pass code cleaner.
610class LowerCircuit {
611
612public:
613 LowerCircuit(CircuitOp circuit, InstanceGraph &instanceGraph,
614 llvm::Statistic &numDomains)
615 : circuit(circuit), instanceGraph(instanceGraph),
616 constants(circuit.getContext()), numDomains(numDomains) {}
617
618 /// Lower the circuit, removing all domains.
619 LogicalResult lowerCircuit();
620
621private:
622 /// Lower one domain.
623 LogicalResult lowerDomain(DomainOp);
624
625 /// The circuit this class is lowering.
626 CircuitOp circuit;
627
628 /// A reference to an instance graph. This will be mutated.
629 InstanceGraph &instanceGraph;
630
631 /// Internal store of lazily constructed constants.
632 Constants constants;
633
634 /// Mutable reference to the number of domains this class will lower.
635 llvm::Statistic &numDomains;
636
637 /// Store of the mapping from a domain name to the classes that it has been
638 /// lowered into.
639 DenseMap<Attribute, Classes> classes;
640};
641
642LogicalResult LowerCircuit::lowerDomain(DomainOp op) {
643 ImplicitLocOpBuilder builder(op.getLoc(), op);
644 auto *context = op.getContext();
645 auto name = op.getNameAttr();
646 SmallVector<PortInfo> classInPorts;
647 for (auto field : op.getFields().getAsRange<DomainFieldAttr>())
648 classInPorts.append({{/*name=*/builder.getStringAttr(
649 Twine(field.getName().getValue()) + "_in"),
650 /*type=*/field.getType(), /*dir=*/Direction::In},
651 {/*name=*/builder.getStringAttr(
652 Twine(field.getName().getValue()) + "_out"),
653 /*type=*/field.getType(), /*dir=*/Direction::Out}});
654 auto classIn = ClassOp::create(builder, name, classInPorts);
655 auto classInType = classIn.getInstanceType();
656 auto pathListType =
657 ListType::get(context, cast<PropertyType>(PathType::get(context)));
658 auto classOut =
659 ClassOp::create(builder, StringAttr::get(context, Twine(name) + "_out"),
660 {{/*name=*/constants.getDomainInfoIn(),
661 /*type=*/classInType,
662 /*dir=*/Direction::In},
663 {/*name=*/constants.getDomainInfoOut(),
664 /*type=*/classInType,
665 /*dir=*/Direction::Out},
666 {/*name=*/constants.getAssociationsIn(),
667 /*type=*/pathListType,
668 /*dir=*/Direction::In},
669 {/*name=*/constants.getAssociationsOut(),
670 /*type=*/pathListType,
671 /*dir=*/Direction::Out}});
672
673 auto connectPairWise = [&builder](ClassOp &classOp) {
674 builder.setInsertionPointToStart(classOp.getBodyBlock());
675 for (size_t i = 0, e = classOp.getNumPorts(); i != e; i += 2)
676 PropAssignOp::create(builder, classOp.getArgument(i + 1),
677 classOp.getArgument(i));
678 };
679 connectPairWise(classIn);
680 connectPairWise(classOut);
681
682 classes.insert({name, {classIn, classOut}});
683 instanceGraph.addModule(classIn);
684 instanceGraph.addModule(classOut);
685 op.erase();
686 ++numDomains;
687 return success();
688}
689
690LogicalResult LowerCircuit::lowerCircuit() {
691 LLVM_DEBUG(llvm::dbgs() << "Processing domains:\n");
692 for (auto domain : llvm::make_early_inc_range(circuit.getOps<DomainOp>())) {
693 LLVM_DEBUG(llvm::dbgs() << " - " << domain.getName() << "\n");
694 if (failed(lowerDomain(domain)))
695 return failure();
696 }
697
698 LLVM_DEBUG(llvm::dbgs() << "Processing modules:\n");
699 return instanceGraph.walkPostOrder([&](InstanceGraphNode &node) {
700 auto moduleOp = dyn_cast<FModuleLike>(node.getModule<Operation *>());
701 if (!moduleOp)
702 return success();
703 LLVM_DEBUG(llvm::dbgs() << " - module: " << moduleOp.getName() << "\n");
704 LowerModule lowerModule(moduleOp, classes, constants, instanceGraph);
705 if (failed(lowerModule.lowerModule()))
706 return failure();
707 LLVM_DEBUG(llvm::dbgs() << " instances:\n");
708 return lowerModule.lowerInstances();
709 });
710
711 return success();
712}
713} // namespace
714
717
718 LowerCircuit lowerCircuit(getOperation(), getAnalysis<InstanceGraph>(),
719 numDomains);
720 if (failed(lowerCircuit.lowerCircuit()))
721 return signalPassFailure();
722
723 markAnalysesPreserved<InstanceGraph>();
724}
assert(baseType &&"element must be base type")
static Location getLoc(DefSlot slot)
Definition Mem2Reg.cpp:216
static StringAttr append(StringAttr base, const Twine &suffix)
Return a attribute with the specified suffix appended.
#define CIRCT_DEBUG_SCOPED_PASS_LOGGER(PASS)
Definition Debug.h:70
void runOnOperation() override
This class provides a read-only projection of an annotation.
This graph tracks modules and where they are instantiated.
This is a Node in the InstanceGraph.
auto getModule()
Get the module that this node is tracking.
size_t getNumPorts(Operation *op)
Return the number of ports in a module-like thing (modules, memories, etc)
void info(Twine message)
Definition LSPUtils.cpp:20
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
This holds the name and type that describes the module's ports.