forked from llvm/circt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcirct-bmc.cpp
More file actions
391 lines (339 loc) · 14.4 KB
/
circt-bmc.cpp
File metadata and controls
391 lines (339 loc) · 14.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//===- circt-bmc.cpp - The circt-bmc bounded model checker ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the 'circt-bmc' tool
//
//===----------------------------------------------------------------------===//
#include "circt/Conversion/CombToSMT.h"
#include "circt/Conversion/HWToSMT.h"
#include "circt/Conversion/Passes.h"
#include "circt/Conversion/SMTToZ3LLVM.h"
#include "circt/Conversion/VerifToSMT.h"
#include "circt/Dialect/Comb/CombDialect.h"
#include "circt/Dialect/Emit/EmitDialect.h"
#include "circt/Dialect/Emit/EmitPasses.h"
#include "circt/Dialect/HW/HWDialect.h"
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/HW/HWPasses.h"
#include "circt/Dialect/LTL/LTLDialect.h"
#include "circt/Dialect/OM/OMDialect.h"
#include "circt/Dialect/OM/OMPasses.h"
#include "circt/Dialect/Seq/SeqDialect.h"
#include "circt/Dialect/Verif/VerifDialect.h"
#include "circt/Dialect/Verif/VerifPasses.h"
#include "circt/Support/Passes.h"
#include "circt/Support/Version.h"
#include "circt/Tools/circt-bmc/Passes.h"
#include "circt/Transforms/Passes.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/Extensions/InlinerExtension.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h"
#include "mlir/Dialect/SMT/IR/SMTDialect.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/OwningOpRef.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
#ifdef CIRCT_BMC_ENABLE_JIT
#include "mlir/ExecutionEngine/ExecutionEngine.h"
#include "mlir/ExecutionEngine/OptUtils.h"
#include "llvm/Support/TargetSelect.h"
#endif
namespace cl = llvm::cl;
using namespace mlir;
using namespace circt;
//===----------------------------------------------------------------------===//
// Command-line options declaration
//===----------------------------------------------------------------------===//
static cl::OptionCategory mainCategory("circt-bmc Options");
static cl::opt<std::string>
moduleName("module",
cl::desc("Specify a named module (or verif.formal op) to verify "
"properties over."),
cl::value_desc("module name"), cl::cat(mainCategory));
static cl::opt<int> clockBound(
"b", cl::Required,
cl::desc("Specify a number of clock cycles to model check up to."),
cl::value_desc("clock cycle count"), cl::cat(mainCategory));
static cl::opt<int> ignoreAssertionsUntil(
"ignore-asserts-until", cl::Optional,
cl::desc("Specify a number of initial clock cycles for which assertions "
"should be ignored (e.g. so that a circuit can stabilize)."),
cl::value_desc("number of cycles to ignore assertions for"),
cl::cat(mainCategory));
static cl::opt<std::string> inputFilename(cl::Positional, cl::Required,
cl::desc("<input file>"),
cl::cat(mainCategory));
static cl::opt<std::string> outputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
cl::init("-"),
cl::cat(mainCategory));
static cl::opt<bool>
verifyPasses("verify-each",
cl::desc("Run the verifier after each transformation pass"),
cl::init(true), cl::cat(mainCategory));
static cl::opt<bool> printSolverOutput(
"print-solver-output",
cl::desc("Print the output (counterexample or proof) produced by the "
"solver on each invocation and the assertion set that they "
"prove/disprove."),
cl::init(false), cl::cat(mainCategory));
static cl::opt<bool>
verbosePassExecutions("verbose-pass-executions",
cl::desc("Log executions of toplevel module passes"),
cl::init(false), cl::cat(mainCategory));
static cl::opt<bool> risingClocksOnly(
"rising-clocks-only",
cl::desc("Only consider the circuit and property on rising clock edges"),
cl::init(false), cl::cat(mainCategory));
static cl::opt<bool> flattenModules(
"flatten-modules",
cl::desc("Flatten all module instances before processing (which will "
"increase code size but allows multiple assertions across module "
"boundaries to be supported)"),
cl::init(true), cl::cat(mainCategory));
#ifdef CIRCT_BMC_ENABLE_JIT
enum OutputFormat { OutputMLIR, OutputLLVM, OutputSMTLIB, OutputRunJIT };
static cl::opt<OutputFormat> outputFormat(
cl::desc("Specify output format"),
cl::values(clEnumValN(OutputMLIR, "emit-mlir", "Emit LLVM MLIR dialect"),
clEnumValN(OutputLLVM, "emit-llvm", "Emit LLVM"),
clEnumValN(OutputSMTLIB, "emit-smtlib", "Emit SMT-LIB file"),
clEnumValN(OutputRunJIT, "run",
"Perform BMC and output result")),
cl::init(OutputRunJIT), cl::cat(mainCategory));
static cl::list<std::string> sharedLibs{
"shared-libs", llvm::cl::desc("Libraries to link dynamically"),
cl::MiscFlags::CommaSeparated, llvm::cl::cat(mainCategory)};
#else
enum OutputFormat { OutputMLIR, OutputLLVM, OutputSMTLIB };
static cl::opt<OutputFormat> outputFormat(
cl::desc("Specify output format"),
cl::values(clEnumValN(OutputMLIR, "emit-mlir", "Emit LLVM MLIR dialect"),
clEnumValN(OutputLLVM, "emit-llvm", "Emit LLVM"),
clEnumValN(OutputSMTLIB, "emit-smtlib", "Emit SMT-LIB file")),
cl::init(OutputLLVM), cl::cat(mainCategory));
#endif
//===----------------------------------------------------------------------===//
// Tool implementation
//===----------------------------------------------------------------------===//
/// This function initializes the various components of the tool and
/// orchestrates the work to be done.
static LogicalResult executeBMC(MLIRContext &context) {
// Create the timing manager we use to sample execution times.
DefaultTimingManager tm;
applyDefaultTimingManagerCLOptions(tm);
auto ts = tm.getRootScope();
OwningOpRef<ModuleOp> module;
{
auto parserTimer = ts.nest("Parse MLIR input");
// Parse the provided input files.
module = parseSourceFile<ModuleOp>(inputFilename, &context);
}
if (!module)
return failure();
// Create the output directory or output file depending on our mode.
std::optional<std::unique_ptr<llvm::ToolOutputFile>> outputFile;
std::string errorMessage;
// Create an output file.
outputFile.emplace(openOutputFile(outputFilename, &errorMessage));
if (!outputFile.value()) {
llvm::errs() << errorMessage << "\n";
return failure();
}
PassManager pm(&context);
pm.enableVerifier(verifyPasses);
pm.enableTiming(ts);
if (failed(applyPassManagerCLOptions(pm)))
return failure();
if (verbosePassExecutions)
pm.addInstrumentation(
std::make_unique<VerbosePassInstrumentation<mlir::ModuleOp>>(
"circt-bmc"));
pm.addPass(om::createStripOMPass());
pm.addPass(emit::createStripEmitPass());
pm.addPass(verif::createLowerTestsPass());
if (flattenModules) {
hw::FlattenModulesOptions options;
// We can inline public hw.modules since we're only operating over one
// builtin.module
options.inlinePublic = true;
pm.addPass(hw::createFlattenModules(options));
}
pm.addNestedPass<hw::HWModuleOp>(createLowerLTLToCorePass());
pm.addNestedPass<hw::HWModuleOp>(verif::createCombineAssertLikePass());
pm.addPass(createExternalizeRegisters());
LowerToBMCOptions lowerToBMCOptions;
lowerToBMCOptions.bound = clockBound;
lowerToBMCOptions.ignoreAssertionsUntil = ignoreAssertionsUntil;
lowerToBMCOptions.topModule = moduleName;
lowerToBMCOptions.risingClocksOnly = risingClocksOnly;
pm.addPass(createLowerToBMC(lowerToBMCOptions));
pm.addPass(createConvertHWToSMT());
pm.addPass(createConvertCombToSMT());
ConvertVerifToSMTOptions convertVerifToSMTOptions;
convertVerifToSMTOptions.risingClocksOnly = risingClocksOnly;
pm.addPass(createConvertVerifToSMT(convertVerifToSMTOptions));
pm.addPass(createSimpleCanonicalizerPass());
if (outputFormat != OutputMLIR && outputFormat != OutputSMTLIB) {
LowerSMTToZ3LLVMOptions options;
options.debug = printSolverOutput;
pm.addPass(createLowerSMTToZ3LLVM(options));
pm.addPass(createCSEPass());
pm.addPass(createSimpleCanonicalizerPass());
pm.addPass(LLVM::createDIScopeForLLVMFuncOpPass());
}
if (failed(pm.run(module.get())))
return failure();
if (outputFormat == OutputMLIR) {
auto timer = ts.nest("Print MLIR output");
OpPrintingFlags printingFlags;
module->print(outputFile.value()->os(), printingFlags);
outputFile.value()->keep();
return success();
}
if (outputFormat == OutputSMTLIB) {
auto timer = ts.nest("Print SMT-LIB output");
llvm::errs() << "Printing SMT-LIB not yet supported!\n";
return failure();
}
if (outputFormat == OutputLLVM) {
auto timer = ts.nest("Translate to and print LLVM output");
llvm::LLVMContext llvmContext;
auto llvmModule = mlir::translateModuleToLLVMIR(module.get(), llvmContext);
if (!llvmModule)
return failure();
llvmModule->print(outputFile.value()->os(), nullptr);
outputFile.value()->keep();
return success();
}
#ifdef CIRCT_BMC_ENABLE_JIT
auto handleErr = [](llvm::Error error) -> LogicalResult {
llvm::handleAllErrors(std::move(error),
[](const llvm::ErrorInfoBase &info) {
llvm::errs() << "Error: ";
info.log(llvm::errs());
llvm::errs() << '\n';
});
return failure();
};
std::unique_ptr<mlir::ExecutionEngine> engine;
std::function<llvm::Error(llvm::Module *)> transformer =
mlir::makeOptimizingTransformer(
/*optLevel*/ 3, /*sizeLevel=*/0, /*targetMachine=*/nullptr);
{
auto timer = ts.nest("Setting up the JIT");
auto entryPoint =
dyn_cast_or_null<LLVM::LLVMFuncOp>(module->lookupSymbol(moduleName));
if (!entryPoint || entryPoint.empty()) {
llvm::errs() << "no valid entry point found, expected 'llvm.func' named '"
<< moduleName << "'\n";
return failure();
}
if (entryPoint.getNumArguments() != 0) {
llvm::errs() << "entry point '" << moduleName
<< "' must have no arguments";
return failure();
}
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
SmallVector<StringRef, 4> sharedLibraries(sharedLibs.begin(),
sharedLibs.end());
mlir::ExecutionEngineOptions engineOptions;
engineOptions.transformer = transformer;
engineOptions.jitCodeGenOptLevel = llvm::CodeGenOptLevel::Aggressive;
engineOptions.sharedLibPaths = sharedLibraries;
engineOptions.enableObjectDump = true;
auto expectedEngine =
mlir::ExecutionEngine::create(module.get(), engineOptions);
if (!expectedEngine)
return handleErr(expectedEngine.takeError());
engine = std::move(*expectedEngine);
}
auto timer = ts.nest("JIT Execution");
if (auto err = engine->invokePacked(moduleName))
return handleErr(std::move(err));
return success();
#else
return failure();
#endif
}
/// The entry point for the `circt-bmc` tool:
/// configures and parses the command-line options,
/// registers all dialects within a MLIR context,
/// and calls the `executeBMC` function to do the actual work.
int main(int argc, char **argv) {
llvm::InitLLVM y(argc, argv);
// Hide default LLVM options, other than for this tool.
// MLIR options are added below.
cl::HideUnrelatedOptions(mainCategory);
// Register any pass manager command line options.
registerMLIRContextCLOptions();
registerPassManagerCLOptions();
registerDefaultTimingManagerCLOptions();
registerAsmPrinterCLOptions();
cl::AddExtraVersionPrinter(
[](llvm::raw_ostream &os) { os << circt::getCirctVersion() << '\n'; });
// Parse the command-line options provided by the user.
cl::ParseCommandLineOptions(
argc, argv,
"circt-bmc - bounded model checker\n\n"
"\tThis tool checks all possible executions of a hardware module up to a "
"given time bound to check whether any asserted properties can be "
"violated.\n");
// Set the bug report message to indicate users should file issues on
// llvm/circt and not llvm/llvm-project.
llvm::setBugReportMsg(circt::circtBugReportMsg);
// Register the supported CIRCT dialects and create a context to work with.
DialectRegistry registry;
// clang-format off
registry.insert<
circt::comb::CombDialect,
circt::emit::EmitDialect,
circt::hw::HWDialect,
circt::om::OMDialect,
circt::seq::SeqDialect,
mlir::smt::SMTDialect,
circt::verif::VerifDialect,
circt::ltl::LTLDialect,
mlir::arith::ArithDialect,
mlir::BuiltinDialect,
mlir::func::FuncDialect,
mlir::LLVM::LLVMDialect
>();
// clang-format on
mlir::func::registerInlinerExtension(registry);
mlir::LLVM::registerInlinerInterface(registry);
mlir::registerBuiltinDialectTranslation(registry);
mlir::registerLLVMDialectTranslation(registry);
MLIRContext context(registry);
// Setup of diagnostic handling.
llvm::SourceMgr sourceMgr;
SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
// Avoid printing a superfluous note on diagnostic emission.
context.printOpOnDiagnostic(false);
// Perform the logical equivalence checking; using `exit` to avoid the slow
// teardown of the MLIR context.
exit(failed(executeBMC(context)));
}