CIRCT  19.0.0git
PassiveWires.cpp
Go to the documentation of this file.
1 //===- PassiveWires.cpp - Make Wires Passive --------------------*- 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 file defines the PassiveWires pass. This pass eliminated flips from
10 // wires with aggregate types. Since flips only determine connect direction,
11 // they are unnecessary on wires and just get in the way.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "PassDetails.h"
16 
18 #include "circt/Support/Debug.h"
19 #include "mlir/IR/ImplicitLocOpBuilder.h"
20 #include "llvm/Support/Debug.h"
21 
22 #define DEBUG_TYPE "firrtl-passive-wires"
23 
24 using namespace circt;
25 using namespace firrtl;
26 
27 static bool hasFlip(Type t) {
28  if (auto type = type_dyn_cast<FIRRTLBaseType>(t))
29  return !type.isPassive();
30  return false;
31 }
32 
33 //===----------------------------------------------------------------------===//
34 // Pass Infrastructure
35 //===----------------------------------------------------------------------===//
36 
37 namespace {
38 struct PassiveWiresPass : public PassiveWiresBase<PassiveWiresPass> {
39  void runOnOperation() override;
40 };
41 } // end anonymous namespace
42 
43 // This is the main entrypoint for the lowering pass.
44 void PassiveWiresPass::runOnOperation() {
45  LLVM_DEBUG(debugPassHeader(this) << "\n";);
46  auto module = getOperation();
47 
48  // First, expand any connects to resolve flips.
49  SmallVector<Operation *> worklist;
50  module.walk([&](Operation *op) -> WalkResult {
51  if (auto wire = dyn_cast<WireOp>(op)) {
52  if (hasFlip(wire.getType(0)))
53  worklist.push_back(wire);
54  return WalkResult::advance();
55  }
56  if (!isa<ConnectOp, StrictConnectOp>(op))
57  return WalkResult::advance();
58  // connect/strictconnect
59  if (!hasFlip(op->getOperand(0).getType()))
60  return WalkResult::advance();
61 
62  mlir::ImplicitLocOpBuilder builder(op->getLoc(), op);
63  // This will "blow out" a connect to passive pieces
64  emitConnect(builder, op->getOperand(0), op->getOperand(1));
65  op->erase();
66  return WalkResult::advance();
67  });
68 
69  // Second, remove flips from most things.
70  while (!worklist.empty()) {
71  auto *op = worklist.back();
72  worklist.pop_back();
73  auto r = op->getResult(0);
74  if (!hasFlip(r.getType()))
75  continue;
76  for (auto users : r.getUsers())
77  worklist.push_back(users);
78  // In-place updates is safe as consumers don't care about flip.
79  r.setType(type_cast<FIRRTLBaseType>(r.getType()).getPassiveType());
80  }
81 }
82 
83 /// This is the pass constructor.
84 std::unique_ptr<mlir::Pass> circt::firrtl::createPassiveWiresPass() {
85  return std::make_unique<PassiveWiresPass>();
86 }
Builder builder
static bool hasFlip(Type t)
std::unique_ptr< mlir::Pass > createPassiveWiresPass()
This is the pass constructor.
void emitConnect(OpBuilder &builder, Location loc, Value lhs, Value rhs)
Emit a connect between two values.
Definition: FIRRTLUtils.cpp:24
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
llvm::raw_ostream & debugPassHeader(const mlir::Pass *pass, int width=80)
Write a boilerplate header for a pass to the debug stream.
Definition: Debug.cpp:31