Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConstructLEC.cpp
Go to the documentation of this file.
1//===- ConstructLEC.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
12#include "mlir/Analysis/TopologicalSortUtils.h"
13#include "mlir/Dialect/Func/IR/FuncOps.h"
14#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
15#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16#include "mlir/Transforms/DialectConversion.h"
17
18using namespace mlir;
19using namespace circt;
20using namespace hw;
21
22namespace circt {
23#define GEN_PASS_DEF_CONSTRUCTLEC
24#include "circt/Tools/circt-lec/Passes.h.inc"
25} // namespace circt
26
27//===----------------------------------------------------------------------===//
28// ConstructLEC pass
29//===----------------------------------------------------------------------===//
30
31namespace {
32struct ConstructLECPass
33 : public circt::impl::ConstructLECBase<ConstructLECPass> {
34 using circt::impl::ConstructLECBase<ConstructLECPass>::ConstructLECBase;
35 void runOnOperation() override;
36 hw::HWModuleOp lookupModule(StringRef name);
37 Value constructMiter(OpBuilder builder, Location loc, hw::HWModuleOp moduleA,
38 hw::HWModuleOp moduleB);
39};
40} // namespace
41
42static Value lookupOrCreateStringGlobal(OpBuilder &builder, ModuleOp moduleOp,
43 StringRef str) {
44 Location loc = moduleOp.getLoc();
45 auto global = moduleOp.lookupSymbol<LLVM::GlobalOp>(str);
46 if (!global) {
47 OpBuilder b = OpBuilder::atBlockEnd(moduleOp.getBody());
48 auto arrayTy = LLVM::LLVMArrayType::get(b.getI8Type(), str.size() + 1);
49 global = b.create<LLVM::GlobalOp>(
50 loc, arrayTy, /*isConstant=*/true, LLVM::linkage::Linkage::Private, str,
51 StringAttr::get(b.getContext(), Twine(str).concat(Twine('\00'))));
52 }
53
54 // FIXME: sanity check the fetched global: do all the attributes match what
55 // we expect?
56
57 return builder.create<LLVM::AddressOfOp>(loc, global);
58}
59
60hw::HWModuleOp ConstructLECPass::lookupModule(StringRef name) {
61 Operation *expectedModule = SymbolTable::lookupNearestSymbolFrom(
62 getOperation(), StringAttr::get(&getContext(), name));
63 if (!expectedModule || !isa<hw::HWModuleOp>(expectedModule)) {
64 getOperation().emitError("module named '") << name << "' not found";
65 return {};
66 }
67 return cast<hw::HWModuleOp>(expectedModule);
68}
69
70Value ConstructLECPass::constructMiter(OpBuilder builder, Location loc,
71 hw::HWModuleOp moduleA,
72 hw::HWModuleOp moduleB) {
73 // Create the miter circuit that return equivalence result.
74 auto lecOp = builder.create<verif::LogicEquivalenceCheckingOp>(loc);
75 Value areEquivalent = lecOp.getAreEquivalent();
76 builder.cloneRegionBefore(moduleA.getBody(), lecOp.getFirstCircuit(),
77 lecOp.getFirstCircuit().end());
78 builder.cloneRegionBefore(moduleB.getBody(), lecOp.getSecondCircuit(),
79 lecOp.getSecondCircuit().end());
80
81 moduleA->erase();
82 if (moduleA != moduleB)
83 moduleB->erase();
84
85 {
86 auto *term = lecOp.getFirstCircuit().front().getTerminator();
87 OpBuilder::InsertionGuard guard(builder);
88 builder.setInsertionPoint(term);
89 builder.create<verif::YieldOp>(loc, term->getOperands());
90 term->erase();
91 term = lecOp.getSecondCircuit().front().getTerminator();
92 builder.setInsertionPoint(term);
93 builder.create<verif::YieldOp>(loc, term->getOperands());
94 term->erase();
95 }
96
97 sortTopologically(&lecOp.getFirstCircuit().front());
98 sortTopologically(&lecOp.getSecondCircuit().front());
99
100 return areEquivalent;
101}
102
103void ConstructLECPass::runOnOperation() {
104 // Create necessary function declarations and globals
105 OpBuilder builder = OpBuilder::atBlockEnd(getOperation().getBody());
106 Location loc = getOperation()->getLoc();
107
108 // Lookup the modules.
109 auto moduleA = lookupModule(firstModule);
110 if (!moduleA)
111 return signalPassFailure();
112 auto moduleB = lookupModule(secondModule);
113 if (!moduleB)
114 return signalPassFailure();
115
116 if (moduleA.getModuleType() != moduleB.getModuleType()) {
117 moduleA.emitError("module's IO types don't match second modules: ")
118 << moduleA.getModuleType() << " vs " << moduleB.getModuleType();
119 return signalPassFailure();
120 }
121
122 // Only construct the miter with no additional insertions.
123 if (insertMode == lec::InsertAdditionalModeEnum::None) {
124 constructMiter(builder, loc, moduleA, moduleB);
125 return;
126 }
127
128 mlir::FailureOr<mlir::LLVM::LLVMFuncOp> printfFunc;
129 auto ptrTy = LLVM::LLVMPointerType::get(builder.getContext());
130 auto voidTy = LLVM::LLVMVoidType::get(&getContext());
131 // Lookup or declare printf function.
132 printfFunc = LLVM::lookupOrCreateFn(builder, getOperation(), "printf", ptrTy,
133 voidTy, true);
134 if (failed(printfFunc)) {
135 getOperation()->emitError("failed to lookup or create printf");
136 return signalPassFailure();
137 }
138
139 // Reuse the name of the first module for the entry function, so we don't
140 // have to do any uniquing and the LEC driver also already knows this name.
141 FunctionType functionType = FunctionType::get(&getContext(), {}, {});
142 func::FuncOp entryFunc =
143 builder.create<func::FuncOp>(loc, firstModule, functionType);
144
145 if (insertMode == lec::InsertAdditionalModeEnum::Main) {
146 OpBuilder::InsertionGuard guard(builder);
147 auto i32Ty = builder.getI32Type();
148 auto mainFunc = builder.create<func::FuncOp>(
149 loc, "main", builder.getFunctionType({i32Ty, ptrTy}, {i32Ty}));
150 builder.createBlock(&mainFunc.getBody(), {}, {i32Ty, ptrTy}, {loc, loc});
151 builder.create<func::CallOp>(loc, entryFunc, ValueRange{});
152 // TODO: don't use LLVM here
153 Value constZero = builder.create<LLVM::ConstantOp>(loc, i32Ty, 0);
154 builder.create<func::ReturnOp>(loc, constZero);
155 }
156
157 builder.createBlock(&entryFunc.getBody());
158
159 // Create the miter circuit that returns equivalence result.
160 auto areEquivalent = constructMiter(builder, loc, moduleA, moduleB);
161
162 // TODO: we should find a more elegant way of reporting the result than
163 // already inserting some LLVM here
164 Value eqFormatString =
165 lookupOrCreateStringGlobal(builder, getOperation(), "c1 == c2\n");
166 Value neqFormatString =
167 lookupOrCreateStringGlobal(builder, getOperation(), "c1 != c2\n");
168 Value formatString = builder.create<LLVM::SelectOp>(
169 loc, areEquivalent, eqFormatString, neqFormatString);
170 builder.create<LLVM::CallOp>(loc, printfFunc.value(),
171 ValueRange{formatString});
172
173 builder.create<func::ReturnOp>(loc, ValueRange{});
174}
static SmallVector< T > concat(const SmallVectorImpl< T > &a, const SmallVectorImpl< T > &b)
Returns a new vector containing the concatenation of vectors a and b.
Definition CalyxOps.cpp:540
static Value lookupOrCreateStringGlobal(OpBuilder &builder, ModuleOp moduleOp, StringRef str)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1