CIRCT 21.0.0git
Loading...
Searching...
No Matches
LowerVerifSimulations.cpp
Go to the documentation of this file.
1//===- LowerVerifSimulations.cpp ------------------------------------------===//
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
13#include "mlir/Dialect/Arith/IR/Arith.h"
14#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
15#include "mlir/Dialect/Func/IR/FuncOps.h"
16#include "mlir/Dialect/SCF/IR/SCF.h"
17#include "llvm/Support/Debug.h"
18
19#define DEBUG_TYPE "arc-lower-verif-simulations"
20
21using namespace mlir;
22using namespace circt;
23using namespace arc;
24
25using hw::PortInfo;
26
27namespace circt {
28namespace arc {
29#define GEN_PASS_DEF_LOWERVERIFSIMULATIONSPASS
30#include "circt/Dialect/Arc/ArcPasses.h.inc"
31} // namespace arc
32} // namespace circt
33
34namespace {
35struct LowerVerifSimulationsPass
36 : public arc::impl::LowerVerifSimulationsPassBase<
37 LowerVerifSimulationsPass> {
38 void runOnOperation() override;
39 void lowerSimulation(verif::SimulationOp op, SymbolTable &symbolTable);
40};
41} // namespace
42
43void LowerVerifSimulationsPass::runOnOperation() {
44 SymbolTableCollection symbolTables;
45
46 // Declare the `exit` function if it does not yet exist.
47 auto builder = OpBuilder::atBlockBegin(getOperation().getBody());
48 auto exitFuncType = builder.getFunctionType({builder.getI32Type()}, {});
49 auto &symbolTable = symbolTables.getSymbolTable(getOperation());
50 if (auto *exitOp = symbolTable.lookup("exit")) {
51 auto func = dyn_cast<func::FuncOp>(exitOp);
52 if (!func) {
53 exitOp->emitOpError() << "expected to be a `func.func`";
54 return signalPassFailure();
55 }
56 if (func.getFunctionType() != exitFuncType) {
57 func.emitOpError() << "expected to have function type " << exitFuncType
58 << ", got " << func.getFunctionType() << " instead";
59 return signalPassFailure();
60 }
61 } else {
62 auto func = builder.create<func::FuncOp>(
63 getOperation().getLoc(), builder.getStringAttr("exit"), exitFuncType);
64 SymbolTable::setSymbolVisibility(func, SymbolTable::Visibility::Private);
65 }
66
67 getOperation().walk([&](verif::SimulationOp op) {
68 auto *symbolTableOp = SymbolTable::getNearestSymbolTable(op);
69 assert(symbolTableOp);
70 lowerSimulation(op, symbolTables.getSymbolTable(symbolTableOp));
71 });
72}
73
74void LowerVerifSimulationsPass::lowerSimulation(verif::SimulationOp op,
75 SymbolTable &symbolTable) {
76 LLVM_DEBUG(llvm::dbgs() << "Lowering " << op.getSymName() << "\n");
77 auto *context = &getContext();
78 auto i1Type = IntegerType::get(context, 1);
79
80 // Assemble the ports of the implementation module.
81 auto &body = *op.getBody();
82 auto *yieldOp = body.getTerminator();
83 std::array<PortInfo, 4> implPorts;
84
85 auto clockName = StringAttr::get(context, "clock");
86 implPorts[0].name = clockName;
87 implPorts[0].type = seq::ClockType::get(context);
88 implPorts[0].dir = PortInfo::Input;
89 implPorts[0].loc = body.getArgument(0).getLoc();
90
91 auto initName = StringAttr::get(context, "init");
92 implPorts[1].name = initName;
93 implPorts[1].type = i1Type;
94 implPorts[1].dir = PortInfo::Input;
95 implPorts[1].loc = body.getArgument(1).getLoc();
96
97 auto doneName = StringAttr::get(context, "done");
98 implPorts[2].name = doneName;
99 implPorts[2].type = i1Type;
100 implPorts[2].dir = PortInfo::Output;
101 implPorts[2].loc = yieldOp->getOperand(0).getLoc();
102
103 auto successName = StringAttr::get(context, "success");
104 implPorts[3].name = successName;
105 implPorts[3].type = i1Type;
106 implPorts[3].dir = PortInfo::Output;
107 implPorts[3].loc = yieldOp->getOperand(1).getLoc();
108
109 // Replace the `verif.yield` operation with an `hw.output`.
110 OpBuilder builder(yieldOp);
111 builder.create<hw::OutputOp>(yieldOp->getLoc(), yieldOp->getOperands());
112 yieldOp->erase();
113
114 // Move the body of the simulation into a separate HW module.
115 builder.setInsertionPoint(op);
116 auto implName = StringAttr::get(context, Twine("verif.simulation.impl.") +
117 op.getSymName());
118 auto loc = op.getLoc();
119 auto implOp = builder.create<hw::HWModuleOp>(loc, implName, implPorts);
120 symbolTable.insert(implOp);
121 implOp.getBody().takeBody(op.getBodyRegion());
122
123 // Create a new function for the verification op.
124 auto funcType = builder.getFunctionType({}, {});
125 auto funcOp =
126 builder.create<func::FuncOp>(loc, op.getSymNameAttr(), funcType);
127 auto *funcBody = builder.createBlock(&funcOp.getBody());
128
129 auto falseOp = builder.create<hw::ConstantOp>(loc, i1Type, 0);
130 auto trueOp = builder.create<hw::ConstantOp>(loc, i1Type, 1);
131 auto lowOp = builder.create<seq::ToClockOp>(loc, falseOp);
132 auto highOp = builder.create<seq::ToClockOp>(loc, trueOp);
133
134 // Instantiate the implementation module.
135 auto instType = SimModelInstanceType::get(
136 context, FlatSymbolRefAttr::get(implOp.getSymNameAttr()));
137 auto instOp = builder.create<SimInstantiateOp>(loc);
138 auto *instBody =
139 builder.createBlock(&instOp.getBody(), {}, {instType}, {loc});
140 auto instArg = instBody->getArgument(0);
141
142 // Create an `scf.execute_region` op inside such that we can use simple
143 // control flow in the `arc.sim.instantiate` op body. This is simpler than
144 // setting up an `scf.while` op.
145 auto execOp = builder.create<scf::ExecuteRegionOp>(loc, TypeRange{});
146 builder.setInsertionPointToEnd(&execOp.getRegion().emplaceBlock());
147
148 // Apply the initial clock tick to the design.
149 builder.create<SimSetInputOp>(loc, instArg, clockName, lowOp);
150 builder.create<SimSetInputOp>(loc, instArg, initName, trueOp);
151 builder.create<SimStepOp>(loc, instArg);
152 builder.create<SimSetInputOp>(loc, instArg, clockName, highOp);
153 builder.create<SimStepOp>(loc, instArg);
154 builder.create<SimSetInputOp>(loc, instArg, clockName, lowOp);
155 builder.create<SimSetInputOp>(loc, instArg, initName, falseOp);
156 builder.create<SimStepOp>(loc, instArg);
157
158 // Create the block that will perform a single clock tick.
159 auto &loopBlock = execOp.getRegion().emplaceBlock();
160 builder.create<cf::BranchOp>(loc, &loopBlock);
161 builder.setInsertionPointToEnd(&loopBlock);
162
163 // Sample the done and success signals.
164 auto doneSample =
165 builder.create<SimGetPortOp>(loc, i1Type, instArg, doneName);
166 auto successSample =
167 builder.create<SimGetPortOp>(loc, i1Type, instArg, successName);
168
169 // Apply a full clock cycle to the design.
170 builder.create<SimSetInputOp>(loc, instArg, clockName, highOp);
171 builder.create<SimStepOp>(loc, instArg);
172 builder.create<SimSetInputOp>(loc, instArg, clockName, lowOp);
173 builder.create<SimStepOp>(loc, instArg);
174
175 // If done, exit the loop.
176 auto &exitBlock = execOp.getRegion().emplaceBlock();
177 builder.create<cf::CondBranchOp>(loc, doneSample, &exitBlock, &loopBlock);
178 builder.setInsertionPointToEnd(&exitBlock);
179
180 // Convert the i1 success signal into an i32 failure signal that can be used
181 // as an exit code.
182 auto i32Type = builder.getI32Type();
183 auto failureI32 = builder.create<arith::ExtUIOp>(
184 loc, i32Type,
185 builder.create<arith::XOrIOp>(
186 loc, successSample, builder.create<hw::ConstantOp>(loc, i1Type, 1)));
187
188 // Call exit with the computed exit code.
189 builder.create<func::CallOp>(loc, TypeRange{}, builder.getStringAttr("exit"),
190 ValueRange{failureI32});
191 builder.create<scf::YieldOp>(loc);
192
193 // Create the final function return.
194 builder.setInsertionPointToEnd(funcBody);
195 builder.create<func::ReturnOp>(loc);
196
197 // Get rid of the original simulation op.
198 op.erase();
199}
assert(baseType &&"element must be base type")
create(data_type, value)
Definition hw.py:433
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
This holds the name, type, direction of a module's ports.