18#include "mlir/Analysis/TopologicalSortUtils.h"
19#include "mlir/Dialect/Func/IR/FuncOps.h"
20#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
21#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
22#include "mlir/IR/Builders.h"
23#include "mlir/IR/Location.h"
24#include "mlir/IR/SymbolTable.h"
25#include "llvm/Support/LogicalResult.h"
32#define GEN_PASS_DEF_LOWERTOBMC
33#include "circt/Tools/circt-bmc/Passes.h.inc"
41struct LowerToBMCPass :
public circt::impl::LowerToBMCBase<LowerToBMCPass> {
42 using LowerToBMCBase::LowerToBMCBase;
43 void runOnOperation()
override;
47void LowerToBMCPass::runOnOperation() {
50 auto moduleOp = getOperation();
53 moduleOp.emitError(
"hw.module named '") << topModule <<
"' not found";
54 return signalPassFailure();
58 bool noProperties =
false;
59 if (hwModule.getOps<verif::AssertOp>().empty() &&
60 hwModule.getOps<hw::InstanceOp>().empty()) {
61 hwModule.emitWarning(
"no property provided to check in module - will "
62 "trivially find no assertions.");
66 if (!sortTopologically(&hwModule.getBodyRegion().front())) {
67 hwModule->emitError(
"could not resolve cycles in module");
68 return signalPassFailure();
71 if (bound < ignoreAssertionsUntil) {
73 "number of ignored cycles must be less than or equal to bound");
74 return signalPassFailure();
78 auto *ctx = &getContext();
79 OpBuilder builder(ctx);
80 Location loc = moduleOp->getLoc();
81 builder.setInsertionPointToEnd(moduleOp.getBody());
82 auto ptrTy = LLVM::LLVMPointerType::get(ctx);
83 auto voidTy = LLVM::LLVMVoidType::get(ctx);
87 LLVM::lookupOrCreateFn(builder, moduleOp,
"printf", ptrTy, voidTy,
true);
88 if (failed(printfFunc)) {
89 moduleOp->emitError(
"failed to lookup or create printf");
90 return signalPassFailure();
94 auto entryFunc = func::FuncOp::create(builder, loc, topModule,
95 builder.getFunctionType({}, {}));
96 builder.createBlock(&entryFunc.getBody());
99 OpBuilder::InsertionGuard guard(builder);
100 auto *terminator = hwModule.getBody().front().getTerminator();
101 builder.setInsertionPoint(terminator);
102 verif::YieldOp::create(builder, loc, terminator->getOperands());
108 verif::BoundedModelCheckingOp bmcOp;
109 auto numRegs = hwModule->getAttrOfType<IntegerAttr>(
"num_regs");
110 auto initialValues = hwModule->getAttrOfType<ArrayAttr>(
"initial_values");
111 if (numRegs && initialValues) {
112 for (
auto value : initialValues) {
113 if (!isa<IntegerAttr, UnitAttr>(value)) {
114 hwModule->emitError(
"initial_values attribute must contain only "
115 "integer or unit attributes");
116 return signalPassFailure();
119 bmcOp = verif::BoundedModelCheckingOp::create(
120 builder, loc, risingClocksOnly ? bound : 2 * bound,
121 cast<IntegerAttr>(numRegs).getValue().getZExtValue(), initialValues);
124 if (ignoreAssertionsUntil)
125 bmcOp->setAttr(
"ignore_asserts_until",
126 builder.getI32IntegerAttr(
127 risingClocksOnly ? ignoreAssertionsUntil
128 : 2 * ignoreAssertionsUntil));
130 hwModule->emitOpError(
"no num_regs or initial_values attribute found - "
131 "please run externalize "
132 "registers pass first");
133 return signalPassFailure();
140 for (
auto input : hwModule.getInputTypes()) {
141 if (isa<seq::ClockType>(input)) {
143 hwModule.emitError(
"designs with multiple clocks not yet supported");
144 return signalPassFailure();
148 if (
auto hwStruct = dyn_cast<hw::StructType>(input)) {
149 for (
auto field : hwStruct.getElements()) {
150 if (isa<seq::ClockType>(field.type)) {
153 "designs with multiple clocks not yet supported");
154 return signalPassFailure();
162 OpBuilder::InsertionGuard guard(builder);
165 auto *initBlock = builder.createBlock(&bmcOp.getInit());
166 builder.setInsertionPointToStart(initBlock);
169 risingClocksOnly ? 1 : 0);
170 auto toClk = seq::ToClockOp::create(builder, loc, initVal);
171 verif::YieldOp::create(builder, loc, ValueRange{toClk});
173 verif::YieldOp::create(builder, loc, ValueRange{});
177 auto *loopBlock = builder.createBlock(&bmcOp.getLoop());
178 builder.setInsertionPointToStart(loopBlock);
180 loopBlock->addArgument(seq::ClockType::get(ctx), loc);
181 if (risingClocksOnly) {
183 verif::YieldOp::create(builder, loc,
184 ValueRange{loopBlock->getArgument(0)});
187 seq::FromClockOp::create(builder, loc, loopBlock->getArgument(0));
190 auto nClk = comb::XorOp::create(builder, loc, fromClk, cNeg1);
191 auto toClk = seq::ToClockOp::create(builder, loc, nClk);
193 verif::YieldOp::create(builder, loc, ValueRange{toClk});
196 verif::YieldOp::create(builder, loc, ValueRange{});
199 bmcOp.getCircuit().takeBody(hwModule.getBody());
203 auto createUniqueStringGlobal = [&](StringRef str) -> FailureOr<Value> {
204 Location loc = moduleOp.getLoc();
206 OpBuilder b = OpBuilder::atBlockEnd(moduleOp.getBody());
207 auto arrayTy = LLVM::LLVMArrayType::get(b.getI8Type(), str.size() + 1);
208 auto global = LLVM::GlobalOp::create(
209 b, loc, arrayTy,
true, LLVM::linkage::Linkage::Private,
211 StringAttr::get(b.getContext(), Twine(str).
concat(Twine(
'\00'))));
212 SymbolTable symTable(moduleOp);
213 if (failed(symTable.renameToUnique(global, {&symTable}))) {
214 return mlir::failure();
218 LLVM::AddressOfOp::create(builder, loc, global)->getResult(0));
223 auto messageString = createUniqueStringGlobal(
224 "No properties provided - trivially no violations.\n");
225 if (failed(messageString)) {
226 moduleOp->emitOpError(
"could not create result message string");
227 return signalPassFailure();
229 formatString = messageString.value();
231 auto successStrAddr =
232 createUniqueStringGlobal(
"Bound reached with no violations!\n");
233 auto failureStrAddr =
234 createUniqueStringGlobal(
"Assertion can be violated!\n");
236 if (failed(successStrAddr) || failed(failureStrAddr)) {
237 moduleOp->emitOpError(
"could not create result message strings");
238 return signalPassFailure();
242 LLVM::SelectOp::create(builder, loc, bmcOp.getResult(),
243 successStrAddr.value(), failureStrAddr.value());
246 LLVM::CallOp::create(builder, loc, printfFunc.value(),
247 ValueRange{formatString});
248 func::ReturnOp::create(builder, loc);
250 if (insertMainFunc) {
251 builder.setInsertionPointToEnd(getOperation().getBody());
252 Type i32Ty = builder.getI32Type();
253 auto mainFunc = func::FuncOp::create(
254 builder, loc,
"main", builder.getFunctionType({i32Ty, ptrTy}, {i32Ty}));
255 builder.createBlock(&mainFunc.getBody(), {}, {i32Ty, ptrTy}, {loc, loc});
256 func::CallOp::create(builder, loc, entryFunc, ValueRange{});
258 Value constZero = LLVM::ConstantOp::create(builder, loc, i32Ty, 0);
259 func::ReturnOp::create(builder, loc, constZero);
static SmallVector< T > concat(const SmallVectorImpl< T > &a, const SmallVectorImpl< T > &b)
Returns a new vector containing the concatenation of vectors a and b.
A namespace that is used to store existing names and generate new names in some scope within the IR.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.