forked from llvm/circt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowerToBMC.cpp
More file actions
230 lines (207 loc) · 8.83 KB
/
LowerToBMC.cpp
File metadata and controls
230 lines (207 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//===- LowerToBMC.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "circt/Dialect/Comb/CombOps.h"
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/HW/HWTypes.h"
#include "circt/Dialect/Seq/SeqOps.h"
#include "circt/Dialect/Seq/SeqTypes.h"
#include "circt/Dialect/Verif/VerifOps.h"
#include "circt/Support/LLVM.h"
#include "circt/Support/Namespace.h"
#include "circt/Tools/circt-bmc/Passes.h"
#include "mlir/Analysis/TopologicalSortUtils.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/SymbolTable.h"
#include "llvm/Support/LogicalResult.h"
using namespace mlir;
using namespace circt;
using namespace hw;
namespace circt {
#define GEN_PASS_DEF_LOWERTOBMC
#include "circt/Tools/circt-bmc/Passes.h.inc"
} // namespace circt
//===----------------------------------------------------------------------===//
// Convert Lower To BMC pass
//===----------------------------------------------------------------------===//
namespace {
struct LowerToBMCPass : public circt::impl::LowerToBMCBase<LowerToBMCPass> {
using LowerToBMCBase::LowerToBMCBase;
void runOnOperation() override;
};
} // namespace
void LowerToBMCPass::runOnOperation() {
Namespace names;
// Fetch the 'hw.module' operation to model check.
auto moduleOp = getOperation();
auto hwModule = moduleOp.lookupSymbol<hw::HWModuleOp>(topModule);
if (!hwModule) {
moduleOp.emitError("hw.module named '") << topModule << "' not found";
return signalPassFailure();
}
if (!sortTopologically(&hwModule.getBodyRegion().front())) {
hwModule->emitError("could not resolve cycles in module");
return signalPassFailure();
}
if (bound < ignoreAssertionsUntil) {
hwModule->emitError(
"number of ignored cycles must be less than or equal to bound");
return signalPassFailure();
}
// Create necessary function declarations and globals
auto *ctx = &getContext();
OpBuilder builder(ctx);
Location loc = moduleOp->getLoc();
builder.setInsertionPointToEnd(moduleOp.getBody());
auto ptrTy = LLVM::LLVMPointerType::get(ctx);
auto voidTy = LLVM::LLVMVoidType::get(ctx);
// Lookup or declare printf function.
auto printfFunc =
LLVM::lookupOrCreateFn(builder, moduleOp, "printf", ptrTy, voidTy, true);
if (failed(printfFunc)) {
moduleOp->emitError("failed to lookup or create printf");
return signalPassFailure();
}
// Replace the top-module with a function performing the BMC
auto entryFunc = func::FuncOp::create(builder, loc, topModule,
builder.getFunctionType({}, {}));
builder.createBlock(&entryFunc.getBody());
{
OpBuilder::InsertionGuard guard(builder);
auto *terminator = hwModule.getBody().front().getTerminator();
builder.setInsertionPoint(terminator);
verif::YieldOp::create(builder, loc, terminator->getOperands());
terminator->erase();
}
// Double the bound given to the BMC op unless in rising clocks only mode, as
// a clock cycle involves two negations
verif::BoundedModelCheckingOp bmcOp;
auto numRegs = hwModule->getAttrOfType<IntegerAttr>("num_regs");
auto initialValues = hwModule->getAttrOfType<ArrayAttr>("initial_values");
if (numRegs && initialValues) {
for (auto value : initialValues) {
if (!isa<IntegerAttr, UnitAttr>(value)) {
hwModule->emitError("initial_values attribute must contain only "
"integer or unit attributes");
return signalPassFailure();
}
}
bmcOp = verif::BoundedModelCheckingOp::create(
builder, loc, risingClocksOnly ? bound : 2 * bound,
cast<IntegerAttr>(numRegs).getValue().getZExtValue(), initialValues);
// Annotate the op with how many cycles to ignore - again, we may need to
// double this to account for rising and falling edges
if (ignoreAssertionsUntil)
bmcOp->setAttr("ignore_asserts_until",
builder.getI32IntegerAttr(
risingClocksOnly ? ignoreAssertionsUntil
: 2 * ignoreAssertionsUntil));
} else {
hwModule->emitOpError("no num_regs or initial_values attribute found - "
"please run externalize "
"registers pass first");
return signalPassFailure();
}
// Count top-level clock inputs. Each gets an independent toggle entry in the
// init/loop regions so the BMC explores all synchronous interleavings.
// Struct-embedded clocks are not yet supported.
unsigned numClocks = 0;
for (auto input : hwModule.getInputTypes()) {
if (isa<seq::ClockType>(input)) {
++numClocks;
}
if (auto hwStruct = dyn_cast<hw::StructType>(input)) {
for (auto field : hwStruct.getElements()) {
if (isa<seq::ClockType>(field.type)) {
hwModule.emitError(
"designs with struct-embedded clocks not yet supported");
return signalPassFailure();
}
}
}
}
{
OpBuilder::InsertionGuard guard(builder);
// Initialize all clocks to 0 (or 1 in rising-clocks-only mode).
auto *initBlock = builder.createBlock(&bmcOp.getInit());
builder.setInsertionPointToStart(initBlock);
SmallVector<Value> initClocks;
for (unsigned i = 0; i < numClocks; ++i) {
auto initVal = hw::ConstantOp::create(builder, loc, builder.getI1Type(),
risingClocksOnly ? 1 : 0);
initClocks.push_back(seq::ToClockOp::create(builder, loc, initVal));
}
verif::YieldOp::create(builder, loc, initClocks);
// Toggle every clock each step (synchronous multi-clock interleaving).
auto *loopBlock = builder.createBlock(&bmcOp.getLoop());
builder.setInsertionPointToStart(loopBlock);
for (unsigned i = 0; i < numClocks; ++i)
loopBlock->addArgument(seq::ClockType::get(ctx), loc);
SmallVector<Value> nextClocks;
if (risingClocksOnly) {
// Rising-clocks-only mode: pass clocks through unchanged.
for (auto arg : loopBlock->getArguments())
nextClocks.push_back(arg);
} else {
for (auto arg : loopBlock->getArguments()) {
auto fromClk = seq::FromClockOp::create(builder, loc, arg);
auto cNeg1 =
hw::ConstantOp::create(builder, loc, builder.getI1Type(), -1);
auto nClk = comb::XorOp::create(builder, loc, fromClk, cNeg1);
nextClocks.push_back(seq::ToClockOp::create(builder, loc, nClk));
}
}
verif::YieldOp::create(builder, loc, nextClocks);
}
bmcOp.getCircuit().takeBody(hwModule.getBody());
hwModule->erase();
// Define global string constants to print on success/failure
auto createUniqueStringGlobal = [&](StringRef str) -> FailureOr<Value> {
Location loc = moduleOp.getLoc();
OpBuilder b = OpBuilder::atBlockEnd(moduleOp.getBody());
auto arrayTy = LLVM::LLVMArrayType::get(b.getI8Type(), str.size() + 1);
auto global = LLVM::GlobalOp::create(
b, loc, arrayTy, /*isConstant=*/true, LLVM::linkage::Linkage::Private,
"resultString",
StringAttr::get(b.getContext(), Twine(str).concat(Twine('\00'))));
SymbolTable symTable(moduleOp);
if (failed(symTable.renameToUnique(global, {&symTable}))) {
return mlir::failure();
}
return success(
LLVM::AddressOfOp::create(builder, loc, global)->getResult(0));
};
auto successStrAddr =
createUniqueStringGlobal("Bound reached with no violations!\n");
auto failureStrAddr =
createUniqueStringGlobal("Assertion can be violated!\n");
if (failed(successStrAddr) || failed(failureStrAddr)) {
moduleOp->emitOpError("could not create result message strings");
return signalPassFailure();
}
auto formatString =
LLVM::SelectOp::create(builder, loc, bmcOp.getResult(),
successStrAddr.value(), failureStrAddr.value());
LLVM::CallOp::create(builder, loc, printfFunc.value(),
ValueRange{formatString});
func::ReturnOp::create(builder, loc);
if (insertMainFunc) {
builder.setInsertionPointToEnd(getOperation().getBody());
Type i32Ty = builder.getI32Type();
auto mainFunc = func::FuncOp::create(
builder, loc, "main", builder.getFunctionType({i32Ty, ptrTy}, {i32Ty}));
builder.createBlock(&mainFunc.getBody(), {}, {i32Ty, ptrTy}, {loc, loc});
func::CallOp::create(builder, loc, entryFunc, ValueRange{});
// TODO: don't use LLVM here
Value constZero = LLVM::ConstantOp::create(builder, loc, i32Ty, 0);
func::ReturnOp::create(builder, loc, constZero);
}
}