CIRCT 20.0.0git
Loading...
Searching...
No Matches
IMConstProp.cpp
Go to the documentation of this file.
1//===- IMConstProp.cpp - Intermodule ConstProp and DCE ----------*- 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// This implements SCCP:
10// https://www.cs.wustl.edu/~cytron/531Pages/f11/Resources/Papers/cprop.pdf
11//
12//===----------------------------------------------------------------------===//
13
20#include "circt/Support/APInt.h"
21#include "mlir/IR/Iterators.h"
22#include "mlir/IR/Threading.h"
23#include "mlir/Pass/Pass.h"
24#include "llvm/ADT/APSInt.h"
25#include "llvm/ADT/TinyPtrVector.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ScopedPrinter.h"
28
29namespace circt {
30namespace firrtl {
31#define GEN_PASS_DEF_IMCONSTPROP
32#include "circt/Dialect/FIRRTL/Passes.h.inc"
33} // namespace firrtl
34} // namespace circt
35
36using namespace circt;
37using namespace firrtl;
38
39#define DEBUG_TYPE "IMCP"
40
41/// Return true if this is a wire or register.
42static bool isWireOrReg(Operation *op) {
43 return isa<WireOp, RegResetOp, RegOp>(op);
44}
45
46/// Return true if this is an aggregate indexer.
47static bool isAggregate(Operation *op) {
48 return isa<SubindexOp, SubaccessOp, SubfieldOp, OpenSubfieldOp,
49 OpenSubindexOp, RefSubOp>(op);
50}
51
52// Return true if this forwards input to output.
53// Implies has appropriate visit method that propagates changes.
54static bool isNodeLike(Operation *op) {
55 return isa<NodeOp, RefResolveOp, RefSendOp>(op);
56}
57
58/// Return true if this is a wire or register we're allowed to delete.
59static bool isDeletableWireOrRegOrNode(Operation *op) {
60 if (!isWireOrReg(op) && !isa<NodeOp>(op))
61 return false;
62
63 // Always allow deleting wires of probe-type.
64 if (type_isa<RefType>(op->getResult(0).getType()))
65 return true;
66
67 // Otherwise, don't delete if has anything keeping it around or unknown.
68 return AnnotationSet(op).empty() && !hasDontTouch(op) &&
69 hasDroppableName(op) && !cast<Forceable>(op).isForceable();
70}
71
72//===----------------------------------------------------------------------===//
73// Pass Infrastructure
74//===----------------------------------------------------------------------===//
75
76namespace {
77/// This class represents a single lattice value. A lattive value corresponds to
78/// the various different states that a value in the SCCP dataflow analysis can
79/// take. See 'Kind' below for more details on the different states a value can
80/// take.
81class LatticeValue {
82 enum Kind {
83 /// A value with a yet-to-be-determined value. This state may be changed to
84 /// anything, it hasn't been processed by IMConstProp.
85 Unknown,
86
87 /// A value that is known to be a constant. This state may be changed to
88 /// overdefined.
89 Constant,
90
91 /// A value that cannot statically be determined to be a constant. This
92 /// state cannot be changed.
93 Overdefined
94 };
95
96public:
97 /// Initialize a lattice value with "Unknown".
98 /*implicit*/ LatticeValue() : valueAndTag(nullptr, Kind::Unknown) {}
99 /// Initialize a lattice value with a constant.
100 /*implicit*/ LatticeValue(IntegerAttr attr)
101 : valueAndTag(attr, Kind::Constant) {}
102 /*implicit*/ LatticeValue(StringAttr attr)
103 : valueAndTag(attr, Kind::Constant) {}
104
105 static LatticeValue getOverdefined() {
106 LatticeValue result;
107 result.markOverdefined();
108 return result;
109 }
110
111 bool isUnknown() const { return valueAndTag.getInt() == Kind::Unknown; }
112 bool isConstant() const { return valueAndTag.getInt() == Kind::Constant; }
113 bool isOverdefined() const {
114 return valueAndTag.getInt() == Kind::Overdefined;
115 }
116
117 /// Mark the lattice value as overdefined.
118 void markOverdefined() {
119 valueAndTag.setPointerAndInt(nullptr, Kind::Overdefined);
120 }
121
122 /// Mark the lattice value as constant.
123 void markConstant(IntegerAttr value) {
124 valueAndTag.setPointerAndInt(value, Kind::Constant);
125 }
126
127 /// If this lattice is constant or invalid value, return the attribute.
128 /// Returns nullptr otherwise.
129 Attribute getValue() const { return valueAndTag.getPointer(); }
130
131 /// If this is in the constant state, return the attribute.
132 Attribute getConstant() const {
134 return getValue();
135 }
136
137 /// Merge in the value of the 'rhs' lattice into this one. Returns true if the
138 /// lattice value changed.
139 bool mergeIn(LatticeValue rhs) {
140 // If we are already overdefined, or rhs is unknown, there is nothing to do.
141 if (isOverdefined() || rhs.isUnknown())
142 return false;
143
144 // If we are unknown, just take the value of rhs.
145 if (isUnknown()) {
146 valueAndTag = rhs.valueAndTag;
147 return true;
148 }
149
150 // Otherwise, if this value doesn't match rhs go straight to overdefined.
151 // This happens when we merge "3" and "4" from two different instance sites
152 // for example.
153 if (valueAndTag != rhs.valueAndTag) {
154 markOverdefined();
155 return true;
156 }
157 return false;
158 }
159
160 bool operator==(const LatticeValue &other) const {
161 return valueAndTag == other.valueAndTag;
162 }
163 bool operator!=(const LatticeValue &other) const {
164 return valueAndTag != other.valueAndTag;
165 }
166
167private:
168 /// The attribute value if this is a constant and the tag for the element
169 /// kind. The attribute is an IntegerAttr (or BoolAttr) or StringAttr.
170 llvm::PointerIntPair<Attribute, 2, Kind> valueAndTag;
171};
172} // end anonymous namespace
173
174LLVM_ATTRIBUTE_USED
175static llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
176 const LatticeValue &lattice) {
177 if (lattice.isUnknown())
178 return os << "<Unknown>";
179 if (lattice.isOverdefined())
180 return os << "<Overdefined>";
181 return os << "<" << lattice.getConstant() << ">";
182}
183
184namespace {
185struct IMConstPropPass
186 : public circt::firrtl::impl::IMConstPropBase<IMConstPropPass> {
187
188 void runOnOperation() override;
189 void rewriteModuleBody(FModuleOp module);
190
191 /// Returns true if the given block is executable.
192 bool isBlockExecutable(Block *block) const {
193 return executableBlocks.count(block);
194 }
195
196 bool isOverdefined(FieldRef value) const {
197 auto it = latticeValues.find(value);
198 return it != latticeValues.end() && it->second.isOverdefined();
199 }
200
201 // Mark the given value as overdefined. If the value is an aggregate,
202 // we mark all ground elements as overdefined.
203 void markOverdefined(Value value) {
204 FieldRef fieldRef = getOrCacheFieldRefFromValue(value);
205 auto firrtlType = type_dyn_cast<FIRRTLType>(value.getType());
206 if (!firrtlType || type_isa<PropertyType>(firrtlType)) {
207 markOverdefined(fieldRef);
208 return;
209 }
210
211 walkGroundTypes(firrtlType, [&](uint64_t fieldID, auto, auto) {
212 markOverdefined(fieldRef.getSubField(fieldID));
213 });
214 }
215
216 /// Mark the given value as overdefined. This means that we cannot refine a
217 /// specific constant for this value.
218 void markOverdefined(FieldRef value) {
219 auto &entry = latticeValues[value];
220 if (!entry.isOverdefined()) {
221 LLVM_DEBUG({
222 logger.getOStream()
223 << "Setting overdefined : (" << getFieldName(value).first << ")\n";
224 });
225 entry.markOverdefined();
226 changedLatticeValueWorklist.push_back(value);
227 }
228 }
229
230 /// Merge information from the 'from' lattice value into value. If it
231 /// changes, then users of the value are added to the worklist for
232 /// revisitation.
233 void mergeLatticeValue(FieldRef value, LatticeValue &valueEntry,
234 LatticeValue source) {
235 if (valueEntry.mergeIn(source)) {
236 LLVM_DEBUG({
237 logger.getOStream()
238 << "Changed to " << valueEntry << " : (" << value << ")\n";
239 });
240 changedLatticeValueWorklist.push_back(value);
241 }
242 }
243
244 void mergeLatticeValue(FieldRef value, LatticeValue source) {
245 // Don't even do a map lookup if from has no info in it.
246 if (source.isUnknown())
247 return;
248 mergeLatticeValue(value, latticeValues[value], source);
249 }
250
251 void mergeLatticeValue(FieldRef result, FieldRef from) {
252 // If 'from' hasn't been computed yet, then it is unknown, don't do
253 // anything.
254 auto it = latticeValues.find(from);
255 if (it == latticeValues.end())
256 return;
257 mergeLatticeValue(result, it->second);
258 }
259
260 void mergeLatticeValue(Value result, Value from) {
261 FieldRef fieldRefFrom = getOrCacheFieldRefFromValue(from);
262 FieldRef fieldRefResult = getOrCacheFieldRefFromValue(result);
263 if (!type_isa<FIRRTLType>(result.getType()))
264 return mergeLatticeValue(fieldRefResult, fieldRefFrom);
265 // Special-handle PropertyType's, walkGroundType's doesn't support.
266 if (type_isa<PropertyType>(result.getType()))
267 return mergeLatticeValue(fieldRefResult, fieldRefFrom);
268 walkGroundTypes(type_cast<FIRRTLType>(result.getType()),
269 [&](uint64_t fieldID, auto, auto) {
270 mergeLatticeValue(fieldRefResult.getSubField(fieldID),
271 fieldRefFrom.getSubField(fieldID));
272 });
273 }
274
275 /// setLatticeValue - This is used when a new LatticeValue is computed for
276 /// the result of the specified value that replaces any previous knowledge,
277 /// e.g. because a fold() function on an op returned a new thing. This should
278 /// not be used on operations that have multiple contributors to it, e.g.
279 /// wires or ports.
280 void setLatticeValue(FieldRef value, LatticeValue source) {
281 // Don't even do a map lookup if from has no info in it.
282 if (source.isUnknown())
283 return;
284
285 // If we've changed this value then revisit all the users.
286 auto &valueEntry = latticeValues[value];
287 if (valueEntry != source) {
288 changedLatticeValueWorklist.push_back(value);
289 valueEntry = source;
290 }
291 }
292
293 // This function returns a field ref of the given value. This function caches
294 // the result to avoid extra IR traversal if the value is an aggregate
295 // element.
296 FieldRef getOrCacheFieldRefFromValue(Value value) {
297 if (!value.getDefiningOp() || !isAggregate(value.getDefiningOp()))
298 return FieldRef(value, 0);
299 auto &fieldRef = valueToFieldRef[value];
300 if (fieldRef)
301 return fieldRef;
302 return fieldRef = getFieldRefFromValue(value);
303 }
304
305 /// Return the lattice value for the specified SSA value, extended to the
306 /// width of the specified destType. If allowTruncation is true, then this
307 /// allows truncating the lattice value to the specified type.
308 LatticeValue getExtendedLatticeValue(FieldRef value, FIRRTLType destType,
309 bool allowTruncation = false);
310
311 /// Mark the given block as executable.
312 void markBlockExecutable(Block *block);
313 void markWireOp(WireOp wireOrReg);
314 void markMemOp(MemOp mem);
315
316 void markInvalidValueOp(InvalidValueOp invalid);
317 void markAggregateConstantOp(AggregateConstantOp constant);
318 void markInstanceOp(InstanceOp instance);
319 void markObjectOp(ObjectOp object);
320 template <typename OpTy>
321 void markConstantValueOp(OpTy op);
322
323 void visitConnectLike(FConnectLike connect, FieldRef changedFieldRef);
324 void visitRefSend(RefSendOp send, FieldRef changedFieldRef);
325 void visitRefResolve(RefResolveOp resolve, FieldRef changedFieldRef);
326 void mergeOnlyChangedLatticeValue(Value dest, Value src,
327 FieldRef changedFieldRef);
328 void visitNode(NodeOp node, FieldRef changedFieldRef);
329 void visitOperation(Operation *op, FieldRef changedFieldRef);
330
331private:
332 /// This is the current instance graph for the Circuit.
333 InstanceGraph *instanceGraph = nullptr;
334
335 /// This keeps track of the current state of each tracked value.
336 DenseMap<FieldRef, LatticeValue> latticeValues;
337
338 /// The set of blocks that are known to execute, or are intrinsically live.
339 SmallPtrSet<Block *, 16> executableBlocks;
340
341 /// A worklist of values whose LatticeValue recently changed, indicating the
342 /// users need to be reprocessed.
343 SmallVector<FieldRef, 64> changedLatticeValueWorklist;
344
345 // A map to give operations to be reprocessed.
346 DenseMap<FieldRef, llvm::TinyPtrVector<Operation *>> fieldRefToUsers;
347
348 // A map to cache results of getFieldRefFromValue since it's costly traverse
349 // the IR.
350 llvm::DenseMap<Value, FieldRef> valueToFieldRef;
351
352 /// This keeps track of users the instance results that correspond to output
353 /// ports.
354 DenseMap<BlockArgument, llvm::TinyPtrVector<Value>>
355 resultPortToInstanceResultMapping;
356
357#ifndef NDEBUG
358 /// A logger used to emit information during the application process.
359 llvm::ScopedPrinter logger{llvm::dbgs()};
360#endif
361};
362} // end anonymous namespace
363
364// TODO: handle annotations: [[OptimizableExtModuleAnnotation]]
365void IMConstPropPass::runOnOperation() {
366 auto circuit = getOperation();
367 LLVM_DEBUG(
368 { logger.startLine() << "IMConstProp : " << circuit.getName() << "\n"; });
369
370 instanceGraph = &getAnalysis<InstanceGraph>();
371
372 // Mark input ports as overdefined where appropriate.
373 for (auto &op : circuit.getOps()) {
374 // Inputs of public modules are overdefined.
375 if (auto module = dyn_cast<FModuleOp>(op)) {
376 if (module.isPublic()) {
377 markBlockExecutable(module.getBodyBlock());
378 for (auto port : module.getBodyBlock()->getArguments())
379 markOverdefined(port);
380 }
381 continue;
382 }
383
384 // Otherwise we check whether the top-level operation contains any
385 // references to modules. Symbol uses in NLAs are ignored.
386 if (isa<hw::HierPathOp>(op))
387 continue;
388
389 // Inputs of modules referenced by unknown operations are overdefined, since
390 // we don't know how those operations affect the input port values. This
391 // handles things like `firrtl.formal`, which may may assign symbolic values
392 // to input ports of a private module.
393 auto symbolUses = SymbolTable::getSymbolUses(&op);
394 if (!symbolUses)
395 continue;
396 for (const auto &use : *symbolUses) {
397 if (auto symRef = dyn_cast<FlatSymbolRefAttr>(use.getSymbolRef())) {
398 if (auto *igNode = instanceGraph->lookupOrNull(symRef.getAttr())) {
399 if (auto module = dyn_cast<FModuleOp>(*igNode->getModule())) {
400 LLVM_DEBUG(llvm::dbgs()
401 << "Unknown use of " << module.getModuleNameAttr()
402 << " in " << op.getName()
403 << ", marking inputs as overdefined\n");
404 markBlockExecutable(module.getBodyBlock());
405 for (auto port : module.getBodyBlock()->getArguments())
406 markOverdefined(port);
407 }
408 }
409 }
410 }
411 }
412
413 // If a value changed lattice state then reprocess any of its users.
414 while (!changedLatticeValueWorklist.empty()) {
415 FieldRef changedFieldRef = changedLatticeValueWorklist.pop_back_val();
416 for (Operation *user : fieldRefToUsers[changedFieldRef]) {
417 if (isBlockExecutable(user->getBlock()))
418 visitOperation(user, changedFieldRef);
419 }
420 }
421
422 // Rewrite any constants in the modules.
423 mlir::parallelForEach(circuit.getContext(),
424 circuit.getBodyBlock()->getOps<FModuleOp>(),
425 [&](auto op) { rewriteModuleBody(op); });
426
427 // Clean up our state for next time.
428 instanceGraph = nullptr;
429 latticeValues.clear();
430 executableBlocks.clear();
431 assert(changedLatticeValueWorklist.empty());
432 fieldRefToUsers.clear();
433 valueToFieldRef.clear();
434 resultPortToInstanceResultMapping.clear();
435}
436
437/// Return the lattice value for the specified SSA value, extended to the width
438/// of the specified destType. If allowTruncation is true, then this allows
439/// truncating the lattice value to the specified type.
440LatticeValue IMConstPropPass::getExtendedLatticeValue(FieldRef value,
441 FIRRTLType destType,
442 bool allowTruncation) {
443 // If 'value' hasn't been computed yet, then it is unknown.
444 auto it = latticeValues.find(value);
445 if (it == latticeValues.end())
446 return LatticeValue();
447
448 auto result = it->second;
449 // Unknown/overdefined stay whatever they are.
450 if (result.isUnknown() || result.isOverdefined())
451 return result;
452
453 // No extOrTrunc for property types. Return what we have.
454 if (isa<PropertyType>(destType))
455 return result;
456
457 auto constant = result.getConstant();
458
459 // If not property, only support integers.
460 auto intAttr = dyn_cast<IntegerAttr>(constant);
461 assert(intAttr && "unsupported lattice attribute kind");
462 if (!intAttr)
463 return result;
464
465 // No extOrTrunc necessary for bools.
466 if (auto boolAttr = dyn_cast<BoolAttr>(intAttr))
467 return result;
468
469 // Non-base (or non-ref) types are overdefined.
470 auto baseType = getBaseType(destType);
471 if (!baseType)
472 return LatticeValue::getOverdefined();
473
474 // If destType is wider than the source constant type, extend it.
475 auto resultConstant = intAttr.getAPSInt();
476 auto destWidth = baseType.getBitWidthOrSentinel();
477 if (destWidth == -1) // We don't support unknown width FIRRTL.
478 return LatticeValue::getOverdefined();
479 if (resultConstant.getBitWidth() == (unsigned)destWidth)
480 return result; // Already the right width, we're done.
481
482 // Otherwise, extend the constant using the signedness of the source.
483 resultConstant = extOrTruncZeroWidth(resultConstant, destWidth);
484 return LatticeValue(IntegerAttr::get(destType.getContext(), resultConstant));
485}
486
487// NOLINTBEGIN(misc-no-recursion)
488/// Mark a block executable if it isn't already. This does an initial scan of
489/// the block, processing nullary operations like wires, instances, and
490/// constants that only get processed once.
491void IMConstPropPass::markBlockExecutable(Block *block) {
492 if (!executableBlocks.insert(block).second)
493 return; // Already executable.
494
495 // Mark block arguments, which are module ports, with don't touch as
496 // overdefined.
497 for (auto ba : block->getArguments())
498 if (hasDontTouch(ba))
499 markOverdefined(ba);
500
501 for (auto &op : *block) {
502 // Handle each of the special operations in the firrtl dialect.
503 TypeSwitch<Operation *>(&op)
504 .Case<RegOp, RegResetOp>(
505 [&](auto reg) { markOverdefined(op.getResult(0)); })
506 .Case<WireOp>([&](auto wire) { markWireOp(wire); })
507 .Case<ConstantOp, SpecialConstantOp, StringConstantOp,
508 FIntegerConstantOp, BoolConstantOp>(
509 [&](auto constOp) { markConstantValueOp(constOp); })
510 .Case<AggregateConstantOp>(
511 [&](auto aggConstOp) { markAggregateConstantOp(aggConstOp); })
512 .Case<InvalidValueOp>(
513 [&](auto invalid) { markInvalidValueOp(invalid); })
514 .Case<InstanceOp>([&](auto instance) { markInstanceOp(instance); })
515 .Case<ObjectOp>([&](auto obj) { markObjectOp(obj); })
516 .Case<MemOp>([&](auto mem) { markMemOp(mem); })
517 .Case<LayerBlockOp>(
518 [&](auto layer) { markBlockExecutable(layer.getBody(0)); })
519 .Default([&](auto _) {
520 if (isa<mlir::UnrealizedConversionCastOp, VerbatimExprOp,
521 VerbatimWireOp, SubaccessOp>(op) ||
522 op.getNumOperands() == 0) {
523 // Mark operations whose results cannot be tracked as overdefined.
524 // Mark unhandled operations with no operand as well since otherwise
525 // they will remain unknown states until the end.
526 for (auto result : op.getResults())
527 markOverdefined(result);
528 } else if (
529 // Operations that are handled when propagating values, or chasing
530 // indexing.
531 !isAggregate(&op) && !isNodeLike(&op) && op.getNumResults() > 0) {
532 // If an unknown operation has an aggregate operand, mark results as
533 // overdefined since we cannot track the dataflow. Similarly if the
534 // operations create aggregate values, we mark them overdefined.
535
536 // TODO: We should handle aggregate operations such as
537 // vector_create, bundle_create or vector operations.
538
539 bool hasAggregateOperand =
540 llvm::any_of(op.getOperandTypes(), [](Type type) {
541 return type_isa<FVectorType, BundleType>(type);
542 });
543
544 for (auto result : op.getResults())
545 if (hasAggregateOperand ||
546 type_isa<FVectorType, BundleType>(result.getType()))
547 markOverdefined(result);
548 }
549 });
550
551 // This tracks a dependency from field refs to operations which need
552 // to be added to worklist when lattice values change.
553 if (!isAggregate(&op)) {
554 for (auto operand : op.getOperands()) {
555 auto fieldRef = getOrCacheFieldRefFromValue(operand);
556 auto firrtlType = type_dyn_cast<FIRRTLType>(operand.getType());
557 if (!firrtlType)
558 continue;
559 // Special-handle PropertyType's, walkGroundTypes doesn't support.
560 if (type_isa<PropertyType>(firrtlType)) {
561 fieldRefToUsers[fieldRef].push_back(&op);
562 continue;
563 }
564 walkGroundTypes(firrtlType, [&](uint64_t fieldID, auto type, auto) {
565 fieldRefToUsers[fieldRef.getSubField(fieldID)].push_back(&op);
566 });
567 }
568 }
569 }
570}
571// NOLINTEND(misc-no-recursion)
572
573void IMConstPropPass::markWireOp(WireOp wire) {
574 auto type = type_dyn_cast<FIRRTLType>(wire.getResult().getType());
575 if (!type || hasDontTouch(wire.getResult()) || wire.isForceable()) {
576 for (auto result : wire.getResults())
577 markOverdefined(result);
578 return;
579 }
580
581 // Otherwise, this starts out as unknown and is upgraded by connects.
582}
583
584void IMConstPropPass::markMemOp(MemOp mem) {
585 for (auto result : mem.getResults())
586 markOverdefined(result);
587}
588
589template <typename OpTy>
590void IMConstPropPass::markConstantValueOp(OpTy op) {
591 mergeLatticeValue(getOrCacheFieldRefFromValue(op),
592 LatticeValue(op.getValueAttr()));
593}
594
595void IMConstPropPass::markAggregateConstantOp(AggregateConstantOp constant) {
596 walkGroundTypes(constant.getType(), [&](uint64_t fieldID, auto, auto) {
597 mergeLatticeValue(FieldRef(constant, fieldID),
598 LatticeValue(cast<IntegerAttr>(
599 constant.getAttributeFromFieldID(fieldID))));
600 });
601}
602
603void IMConstPropPass::markInvalidValueOp(InvalidValueOp invalid) {
604 markOverdefined(invalid.getResult());
605}
606
607/// Instances have no operands, so they are visited exactly once when their
608/// enclosing block is marked live. This sets up the def-use edges for ports.
609void IMConstPropPass::markInstanceOp(InstanceOp instance) {
610 // Get the module being reference or a null pointer if this is an extmodule.
611 Operation *op = instance.getReferencedModule(*instanceGraph);
612
613 // If this is an extmodule, just remember that any results and inouts are
614 // overdefined.
615 if (!isa<FModuleOp>(op)) {
616 auto module = dyn_cast<FModuleLike>(op);
617 for (size_t resultNo = 0, e = instance.getNumResults(); resultNo != e;
618 ++resultNo) {
619 auto portVal = instance.getResult(resultNo);
620 // If this is an input to the extmodule, we can ignore it.
621 if (module.getPortDirection(resultNo) == Direction::In)
622 continue;
623
624 // Otherwise this is a result from it or an inout, mark it as overdefined.
625 markOverdefined(portVal);
626 }
627 return;
628 }
629
630 // Otherwise this is a defined module.
631 auto fModule = cast<FModuleOp>(op);
632 markBlockExecutable(fModule.getBodyBlock());
633
634 // Ok, it is a normal internal module reference. Populate
635 // resultPortToInstanceResultMapping, and forward any already-computed values.
636 for (size_t resultNo = 0, e = instance.getNumResults(); resultNo != e;
637 ++resultNo) {
638 auto instancePortVal = instance.getResult(resultNo);
639 // If this is an input to the instance, it will
640 // get handled when any connects to it are processed.
641 if (fModule.getPortDirection(resultNo) == Direction::In)
642 continue;
643
644 // Otherwise we have a result from the instance. We need to forward results
645 // from the body to this instance result's SSA value, so remember it.
646 BlockArgument modulePortVal = fModule.getArgument(resultNo);
647
648 resultPortToInstanceResultMapping[modulePortVal].push_back(instancePortVal);
649
650 // If there is already a value known for modulePortVal make sure to forward
651 // it here.
652 mergeLatticeValue(instancePortVal, modulePortVal);
653 }
654}
655
656void IMConstPropPass::markObjectOp(ObjectOp obj) {
657 // Mark overdefined for now, not supported.
658 markOverdefined(obj);
659}
660
661static std::optional<uint64_t>
662getFieldIDOffset(FieldRef changedFieldRef, Type connectionType,
663 FieldRef connectedValueFieldRef) {
664 assert(!type_isa<RefType>(connectionType));
665 if (changedFieldRef.getValue() != connectedValueFieldRef.getValue())
666 return {};
667 if (changedFieldRef.getFieldID() >= connectedValueFieldRef.getFieldID() &&
668 changedFieldRef.getFieldID() <=
669 hw::FieldIdImpl::getMaxFieldID(connectionType) +
670 connectedValueFieldRef.getFieldID())
671 return changedFieldRef.getFieldID() - connectedValueFieldRef.getFieldID();
672 return {};
673}
674
675void IMConstPropPass::mergeOnlyChangedLatticeValue(Value dest, Value src,
676 FieldRef changedFieldRef) {
677
678 // Operate on inner type for refs.
679 auto destType = dest.getType();
680 if (auto refType = type_dyn_cast<RefType>(destType))
681 destType = refType.getType();
682
683 if (!isa<FIRRTLType>(destType)) {
684 // If the dest is not FIRRTL type, conservatively mark
685 // all of them overdefined.
686 markOverdefined(src);
687 return markOverdefined(dest);
688 }
689
690 auto fieldRefSrc = getOrCacheFieldRefFromValue(src);
691 auto fieldRefDest = getOrCacheFieldRefFromValue(dest);
692
693 // If a changed field ref is included the source value, find an offset in the
694 // connection.
695 if (auto srcOffset = getFieldIDOffset(changedFieldRef, destType, fieldRefSrc))
696 mergeLatticeValue(fieldRefDest.getSubField(*srcOffset),
697 fieldRefSrc.getSubField(*srcOffset));
698
699 // If a changed field ref is included the dest value, find an offset in the
700 // connection.
701 if (auto destOffset =
702 getFieldIDOffset(changedFieldRef, destType, fieldRefDest))
703 mergeLatticeValue(fieldRefDest.getSubField(*destOffset),
704 fieldRefSrc.getSubField(*destOffset));
705}
706
707void IMConstPropPass::visitConnectLike(FConnectLike connect,
708 FieldRef changedFieldRef) {
709 // Operate on inner type for refs.
710 auto destType = connect.getDest().getType();
711 if (auto refType = type_dyn_cast<RefType>(destType))
712 destType = refType.getType();
713
714 // Mark foreign types as overdefined.
715 if (!isa<FIRRTLType>(destType)) {
716 markOverdefined(connect.getSrc());
717 return markOverdefined(connect.getDest());
718 }
719
720 auto fieldRefSrc = getOrCacheFieldRefFromValue(connect.getSrc());
721 auto fieldRefDest = getOrCacheFieldRefFromValue(connect.getDest());
722 if (auto subaccess = fieldRefDest.getValue().getDefiningOp<SubaccessOp>()) {
723 // If the destination is subaccess, we give up to precisely track
724 // lattice values and mark entire aggregate as overdefined. This code
725 // should be dead unless we stop lowering of subaccess in LowerTypes.
726 Value parent = subaccess.getInput();
727 while (parent.getDefiningOp() &&
728 parent.getDefiningOp()->getNumOperands() > 0)
729 parent = parent.getDefiningOp()->getOperand(0);
730 return markOverdefined(parent);
731 }
732
733 auto propagateElementLattice = [&](uint64_t fieldID, FIRRTLType destType) {
734 auto fieldRefDestConnected = fieldRefDest.getSubField(fieldID);
735 assert(!firrtl::type_isa<FIRRTLBaseType>(destType) ||
736 firrtl::type_cast<FIRRTLBaseType>(destType).isGround());
737
738 // Handle implicit extensions.
739 auto srcValue =
740 getExtendedLatticeValue(fieldRefSrc.getSubField(fieldID), destType);
741 if (srcValue.isUnknown())
742 return;
743
744 // Driving result ports propagates the value to each instance using the
745 // module.
746 if (auto blockArg = dyn_cast<BlockArgument>(fieldRefDest.getValue())) {
747 for (auto userOfResultPort : resultPortToInstanceResultMapping[blockArg])
748 mergeLatticeValue(
749 FieldRef(userOfResultPort, fieldRefDestConnected.getFieldID()),
750 srcValue);
751 // Output ports are wire-like and may have users.
752 return mergeLatticeValue(fieldRefDestConnected, srcValue);
753 }
754
755 auto dest = cast<mlir::OpResult>(fieldRefDest.getValue());
756
757 // For wires and registers, we drive the value of the wire itself, which
758 // automatically propagates to users.
759 if (isWireOrReg(dest.getOwner()))
760 return mergeLatticeValue(fieldRefDestConnected, srcValue);
761
762 // Driving an instance argument port drives the corresponding argument
763 // of the referenced module.
764 if (auto instance = dest.getDefiningOp<InstanceOp>()) {
765 // Update the dest, when its an instance op.
766 mergeLatticeValue(fieldRefDestConnected, srcValue);
767 auto mod = instance.getReferencedModule<FModuleOp>(*instanceGraph);
768 if (!mod)
769 return;
770
771 BlockArgument modulePortVal = mod.getArgument(dest.getResultNumber());
772
773 return mergeLatticeValue(
774 FieldRef(modulePortVal, fieldRefDestConnected.getFieldID()),
775 srcValue);
776 }
777
778 // Driving a memory result is ignored because these are always treated
779 // as overdefined.
780 if (dest.getDefiningOp<MemOp>())
781 return;
782
783 // For now, don't support const prop into object fields.
784 if (isa_and_nonnull<ObjectSubfieldOp>(dest.getDefiningOp()))
785 return;
786
787 connect.emitError("connectlike operation unhandled by IMConstProp")
788 .attachNote(connect.getDest().getLoc())
789 << "connect destination is here";
790 };
791
792 if (auto srcOffset = getFieldIDOffset(changedFieldRef, destType, fieldRefSrc))
793 propagateElementLattice(
794 *srcOffset,
795 firrtl::type_cast<FIRRTLType>(
796 hw::FieldIdImpl::getFinalTypeByFieldID(destType, *srcOffset)));
797
798 if (auto relativeDest =
799 getFieldIDOffset(changedFieldRef, destType, fieldRefDest))
800 propagateElementLattice(
801 *relativeDest,
802 firrtl::type_cast<FIRRTLType>(
803 hw::FieldIdImpl::getFinalTypeByFieldID(destType, *relativeDest)));
804}
805
806void IMConstPropPass::visitRefSend(RefSendOp send, FieldRef changedFieldRef) {
807 // Send connects the base value (source) to the result (dest).
808 return mergeOnlyChangedLatticeValue(send.getResult(), send.getBase(),
809 changedFieldRef);
810}
811
812void IMConstPropPass::visitRefResolve(RefResolveOp resolve,
813 FieldRef changedFieldRef) {
814 // Resolve connects the ref value (source) to result (dest).
815 // If writes are ever supported, this will need to work differently!
816 return mergeOnlyChangedLatticeValue(resolve.getResult(), resolve.getRef(),
817 changedFieldRef);
818}
819
820void IMConstPropPass::visitNode(NodeOp node, FieldRef changedFieldRef) {
821 if (hasDontTouch(node.getResult()) || node.isForceable()) {
822 for (auto result : node.getResults())
823 markOverdefined(result);
824 return;
825 }
826
827 return mergeOnlyChangedLatticeValue(node.getResult(), node.getInput(),
828 changedFieldRef);
829}
830
831/// This method is invoked when an operand of the specified op changes its
832/// lattice value state and when the block containing the operation is first
833/// noticed as being alive.
834///
835/// This should update the lattice value state for any result values.
836///
837void IMConstPropPass::visitOperation(Operation *op, FieldRef changedField) {
838 // If this is a operation with special handling, handle it specially.
839 if (auto connectLikeOp = dyn_cast<FConnectLike>(op))
840 return visitConnectLike(connectLikeOp, changedField);
841 if (auto sendOp = dyn_cast<RefSendOp>(op))
842 return visitRefSend(sendOp, changedField);
843 if (auto resolveOp = dyn_cast<RefResolveOp>(op))
844 return visitRefResolve(resolveOp, changedField);
845 if (auto nodeOp = dyn_cast<NodeOp>(op))
846 return visitNode(nodeOp, changedField);
847
848 // The clock operand of regop changing doesn't change its result value. All
849 // other registers are over-defined. Aggregate operations also doesn't change
850 // its result value.
851 if (isa<RegOp, RegResetOp>(op) || isAggregate(op))
852 return;
853 // TODO: Handle 'when' operations.
854
855 // If all of the results of this operation are already overdefined (or if
856 // there are no results) then bail out early: we've converged.
857 auto isOverdefinedFn = [&](Value value) {
858 return isOverdefined(getOrCacheFieldRefFromValue(value));
859 };
860 if (llvm::all_of(op->getResults(), isOverdefinedFn))
861 return;
862
863 // To prevent regressions, mark values as overdefined when they are defined
864 // by operations with a large number of operands.
865 if (op->getNumOperands() > 128) {
866 for (auto value : op->getResults())
867 markOverdefined(value);
868 return;
869 }
870
871 // Collect all of the constant operands feeding into this operation. If any
872 // are not ready to be resolved, bail out and wait for them to resolve.
873 SmallVector<Attribute, 8> operandConstants;
874 operandConstants.reserve(op->getNumOperands());
875 bool hasUnknown = false;
876 for (Value operand : op->getOperands()) {
877
878 auto &operandLattice = latticeValues[getOrCacheFieldRefFromValue(operand)];
879
880 // If the operand is an unknown value, then we generally don't want to
881 // process it - we want to wait until the value is resolved to by the SCCP
882 // algorithm.
883 if (operandLattice.isUnknown())
884 hasUnknown = true;
885
886 // Otherwise, it must be constant, invalid, or overdefined. Translate them
887 // into attributes that the fold hook can look at.
888 if (operandLattice.isConstant())
889 operandConstants.push_back(operandLattice.getValue());
890 else
891 operandConstants.push_back({});
892 }
893
894 // Simulate the result of folding this operation to a constant. If folding
895 // fails mark the results as overdefined.
896 SmallVector<OpFoldResult, 8> foldResults;
897 foldResults.reserve(op->getNumResults());
898 if (failed(op->fold(operandConstants, foldResults))) {
899 LLVM_DEBUG({
900 logger.startLine() << "Folding Failed operation : '" << op->getName()
901 << "\n";
902 op->dump();
903 });
904 // If we had unknown arguments, hold off on overdefining
905 if (!hasUnknown)
906 for (auto value : op->getResults())
907 markOverdefined(value);
908 return;
909 }
910
911 LLVM_DEBUG({
912 logger.getOStream() << "\n";
913 logger.startLine() << "Folding operation : '" << op->getName() << "\n";
914 op->dump();
915 logger.getOStream() << "( ";
916 for (auto cst : operandConstants)
917 if (!cst)
918 logger.getOStream() << "{} ";
919 else
920 logger.getOStream() << cst << " ";
921 logger.unindent();
922 logger.getOStream() << ") -> { ";
923 logger.indent();
924 for (auto &r : foldResults) {
925 logger.getOStream() << r << " ";
926 }
927 logger.unindent();
928 logger.getOStream() << "}\n";
929 });
930
931 // If the folding was in-place, keep going. This is surprising, but since
932 // only folder that will do in-place updates is the commutative folder, we
933 // aren't going to stop. We don't update the results, since they didn't
934 // change, the op just got shuffled around.
935 if (foldResults.empty())
936 return visitOperation(op, changedField);
937
938 // Merge the fold results into the lattice for this operation.
939 assert(foldResults.size() == op->getNumResults() && "invalid result size");
940 for (unsigned i = 0, e = foldResults.size(); i != e; ++i) {
941 // Merge in the result of the fold, either a constant or a value.
942 LatticeValue resultLattice;
943 OpFoldResult foldResult = foldResults[i];
944 if (Attribute foldAttr = dyn_cast<Attribute>(foldResult)) {
945 if (auto intAttr = dyn_cast<IntegerAttr>(foldAttr))
946 resultLattice = LatticeValue(intAttr);
947 else if (auto strAttr = dyn_cast<StringAttr>(foldAttr))
948 resultLattice = LatticeValue(strAttr);
949 else // Treat unsupported constants as overdefined.
950 resultLattice = LatticeValue::getOverdefined();
951 } else { // Folding to an operand results in its value.
952 resultLattice =
953 latticeValues[getOrCacheFieldRefFromValue(cast<Value>(foldResult))];
954 }
955
956 mergeLatticeValue(getOrCacheFieldRefFromValue(op->getResult(i)),
957 resultLattice);
958 }
959}
960
961void IMConstPropPass::rewriteModuleBody(FModuleOp module) {
962 auto *body = module.getBodyBlock();
963 // If a module is unreachable, just ignore it.
964 if (!executableBlocks.count(body))
965 return;
966
967 auto builder = OpBuilder::atBlockBegin(body);
968
969 // Separate the constants we insert from the instructions we are folding and
970 // processing. Leave these as-is until we're done.
971 auto cursor = builder.create<firrtl::ConstantOp>(module.getLoc(), APSInt(1));
972 builder.setInsertionPoint(cursor);
973
974 // Unique constants per <Const,Type> pair, inserted at entry
975 DenseMap<std::pair<Attribute, Type>, Operation *> constPool;
976
977 std::function<Value(Attribute, Type, Location)> getConst =
978 [&](Attribute constantValue, Type type, Location loc) -> Value {
979 auto constIt = constPool.find({constantValue, type});
980 if (constIt != constPool.end()) {
981 auto *cst = constIt->second;
982 // Add location to the constant
983 cst->setLoc(builder.getFusedLoc({cst->getLoc(), loc}));
984 return cst->getResult(0);
985 }
986 OpBuilder::InsertionGuard x(builder);
987 builder.setInsertionPoint(cursor);
988
989 // Materialize reftype "constants" by materializing the constant
990 // and probing it.
991 Operation *cst;
992 if (auto refType = type_dyn_cast<RefType>(type)) {
993 assert(!type_cast<RefType>(type).getForceable() &&
994 "Attempting to materialize rwprobe of constant, shouldn't happen");
995 auto inner = getConst(constantValue, refType.getType(), loc);
996 assert(inner);
997 cst = builder.create<RefSendOp>(loc, inner);
998 } else
999 cst = module->getDialect()->materializeConstant(builder, constantValue,
1000 type, loc);
1001 assert(cst && "all FIRRTL constants can be materialized");
1002 constPool.insert({{constantValue, type}, cst});
1003 return cst->getResult(0);
1004 };
1005
1006 // If the lattice value for the specified value is a constant update it and
1007 // return true. Otherwise return false.
1008 auto replaceValueIfPossible = [&](Value value) -> bool {
1009 // Lambda to replace all uses of this value a replacement, unless this is
1010 // the destination of a connect. We leave connects alone to avoid upsetting
1011 // flow, i.e., to avoid trying to connect to a constant.
1012 auto replaceIfNotConnect = [&value](Value replacement) {
1013 value.replaceUsesWithIf(replacement, [](OpOperand &operand) {
1014 return !isa<FConnectLike>(operand.getOwner()) ||
1015 operand.getOperandNumber() != 0;
1016 });
1017 };
1018
1019 // TODO: Replace entire aggregate.
1020 auto it = latticeValues.find(getFieldRefFromValue(value));
1021 if (it == latticeValues.end() || it->second.isOverdefined() ||
1022 it->second.isUnknown())
1023 return false;
1024
1025 // Cannot materialize constants for certain types.
1026 // TODO: Let materializeConstant tell us what it supports instead of this.
1027 // Presently it asserts on unsupported combinations, so check this here.
1028 if (!type_isa<FIRRTLBaseType, RefType, FIntegerType, StringType, BoolType>(
1029 value.getType()))
1030 return false;
1031
1032 auto cstValue =
1033 getConst(it->second.getValue(), value.getType(), value.getLoc());
1034
1035 replaceIfNotConnect(cstValue);
1036 return true;
1037 };
1038
1039 // Constant propagate any ports that are always constant.
1040 for (auto &port : body->getArguments())
1041 replaceValueIfPossible(port);
1042
1043 // Walk the IR bottom-up when folding. We often fold entire chains of
1044 // operations into constants, which make the intermediate nodes dead. Going
1045 // bottom up eliminates the users of the intermediate ops, allowing us to
1046 // aggressively delete them.
1047 //
1048 // TODO: Handle WhenOps correctly.
1049 bool aboveCursor = false;
1050 module.walk<mlir::WalkOrder::PostOrder, mlir::ReverseIterator>(
1051 [&](Operation *op) {
1052 auto dropIfDead = [&](Operation *op, const Twine &debugPrefix) {
1053 if (op->use_empty() &&
1054 (wouldOpBeTriviallyDead(op) || isDeletableWireOrRegOrNode(op))) {
1055 LLVM_DEBUG(
1056 { logger.getOStream() << debugPrefix << " : " << op << "\n"; });
1057 ++numErasedOp;
1058 op->erase();
1059 return true;
1060 }
1061 return false;
1062 };
1063
1064 if (aboveCursor) {
1065 // Drop dead constants we materialized.
1066 dropIfDead(op, "Trivially dead materialized constant");
1067 return WalkResult::advance();
1068 }
1069 // Stop once hit the generated constants.
1070 if (op == cursor) {
1071 cursor.erase();
1072 aboveCursor = true;
1073 return WalkResult::advance();
1074 }
1075
1076 // Connects to values that we found to be constant can be dropped.
1077 if (auto connect = dyn_cast<FConnectLike>(op)) {
1078 if (auto *destOp = connect.getDest().getDefiningOp()) {
1079 auto fieldRef = getOrCacheFieldRefFromValue(connect.getDest());
1080 // Don't remove a field-level connection even if the src value is
1081 // constant. If other elements of the aggregate value are not
1082 // constant, the aggregate value cannot be replaced. We can forward
1083 // the constant to its users, so IMDCE (or SV/HW canonicalizer)
1084 // should remove the aggregate if entire aggregate is dead.
1085 auto type = type_dyn_cast<FIRRTLType>(connect.getDest().getType());
1086 if (!type)
1087 return WalkResult::advance();
1088 auto baseType = type_dyn_cast<FIRRTLBaseType>(type);
1089 if (baseType && !baseType.isGround())
1090 return WalkResult::advance();
1091 if (isDeletableWireOrRegOrNode(destOp) &&
1092 !isOverdefined(fieldRef)) {
1093 connect.erase();
1094 ++numErasedOp;
1095 }
1096 }
1097 return WalkResult::advance();
1098 }
1099
1100 // We only fold single-result ops and instances in practice, because
1101 // they are the expressions.
1102 if (op->getNumResults() != 1 && !isa<InstanceOp>(op))
1103 return WalkResult::advance();
1104
1105 // If this operation is already dead, then go ahead and remove it.
1106 if (dropIfDead(op, "Trivially dead"))
1107 return WalkResult::advance();
1108
1109 // Don't "fold" constants (into equivalent), also because they
1110 // may have name hints we'd like to preserve.
1111 if (op->hasTrait<mlir::OpTrait::ConstantLike>())
1112 return WalkResult::advance();
1113
1114 // If the op had any constants folded, replace them.
1115 builder.setInsertionPoint(op);
1116 bool foldedAny = false;
1117 for (auto result : op->getResults())
1118 foldedAny |= replaceValueIfPossible(result);
1119
1120 if (foldedAny)
1121 ++numFoldedOp;
1122
1123 // If the operation folded to a constant then we can probably nuke it.
1124 if (foldedAny && dropIfDead(op, "Made dead"))
1125 return WalkResult::advance();
1126
1127 return WalkResult::advance();
1128 });
1129}
1130
1131std::unique_ptr<mlir::Pass> circt::firrtl::createIMConstPropPass() {
1132 return std::make_unique<IMConstPropPass>();
1133}
assert(baseType &&"element must be base type")
static std::optional< APSInt > getConstant(Attribute operand)
Determine the value of a constant operand for the sake of constant folding.
static bool isNodeLike(Operation *op)
static bool isWireOrReg(Operation *op)
Return true if this is a wire or register.
static bool isAggregate(Operation *op)
Return true if this is an aggregate indexer.
static std::optional< uint64_t > getFieldIDOffset(FieldRef changedFieldRef, Type connectionType, FieldRef connectedValueFieldRef)
static bool isDeletableWireOrRegOrNode(Operation *op)
Return true if this is a wire or register we're allowed to delete.
static unsigned getFieldID(BundleType type, unsigned index)
static Block * getBodyBlock(FModuleLike mod)
This class represents a reference to a specific field or element of an aggregate value.
Definition FieldRef.h:28
FieldRef getSubField(unsigned subFieldID) const
Get a reference to a subfield.
Definition FieldRef.h:62
unsigned getFieldID() const
Get the field ID of this FieldRef, which is a unique identifier mapped to a specific field in a bundl...
Definition FieldRef.h:59
Value getValue() const
Get the Value which created this location.
Definition FieldRef.h:37
Location getLoc() const
Get the location associated with the value of this field ref.
Definition FieldRef.h:67
This class provides a read-only projection over the MLIR attributes that represent a set of annotatio...
This graph tracks modules and where they are instantiated.
connect(destination, source)
Definition support.py:39
FIRRTLBaseType getBaseType(Type type)
If it is a base type, return it as is.
FieldRef getFieldRefFromValue(Value value, bool lookThroughCasts=false)
Get the FieldRef from a value.
void walkGroundTypes(FIRRTLType firrtlType, llvm::function_ref< void(uint64_t, FIRRTLBaseType, bool)> fn)
Walk leaf ground types in the firrtlType and apply the function fn.
bool isConstant(Operation *op)
Return true if the specified operation has a constant value.
std::unique_ptr< mlir::Pass > createIMConstPropPass()
bool hasDontTouch(Value value)
Check whether a block argument ("port") or the operation defining a value has a DontTouch annotation,...
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, const InstanceInfo::LatticeValue &value)
bool hasDroppableName(Operation *op)
Return true if the name is droppable.
std::pair< std::string, bool > getFieldName(const FieldRef &fieldRef, bool nameSafe=false)
Get a string identifier representing the FieldRef.
bool type_isa(Type type)
::mlir::Type getFinalTypeByFieldID(Type type, uint64_t fieldID)
static bool operator==(const ModulePort &a, const ModulePort &b)
Definition HWTypes.h:35
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
APSInt extOrTruncZeroWidth(APSInt value, unsigned width)
A safe version of APSInt::extOrTrunc that will NOT assert on zero-width signed APSInts.
Definition APInt.cpp:22
bool operator!=(uint64_t a, const FVInt &b)
Definition FVInt.h:651
reg(value, clock, reset=None, reset_value=None, name=None, sym_name=None)
Definition seq.py:21