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