CIRCT 21.0.0git
Loading...
Searching...
No Matches
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};
38} // namespace
39
40static Value lookupOrCreateStringGlobal(OpBuilder &builder, ModuleOp moduleOp,
41 StringRef str) {
42 Location loc = moduleOp.getLoc();
43 auto global = moduleOp.lookupSymbol<LLVM::GlobalOp>(str);
44 if (!global) {
45 OpBuilder b = OpBuilder::atBlockEnd(moduleOp.getBody());
46 auto arrayTy = LLVM::LLVMArrayType::get(b.getI8Type(), str.size() + 1);
47 global = b.create<LLVM::GlobalOp>(
48 loc, arrayTy, /*isConstant=*/true, LLVM::linkage::Linkage::Private, str,
49 StringAttr::get(b.getContext(), Twine(str).concat(Twine('\00'))));
50 }
51
52 // FIXME: sanity check the fetched global: do all the attributes match what
53 // we expect?
54
55 return builder.create<LLVM::AddressOfOp>(loc, global);
56}
57
58hw::HWModuleOp ConstructLECPass::lookupModule(StringRef name) {
59 Operation *expectedModule = SymbolTable::lookupNearestSymbolFrom(
60 getOperation(), StringAttr::get(&getContext(), name));
61 if (!expectedModule || !isa<hw::HWModuleOp>(expectedModule)) {
62 getOperation().emitError("module named '") << name << "' not found";
63 return {};
64 }
65 return cast<hw::HWModuleOp>(expectedModule);
66}
67
68void ConstructLECPass::runOnOperation() {
69 // Create necessary function declarations and globals
70 OpBuilder builder = OpBuilder::atBlockEnd(getOperation().getBody());
71 Location loc = getOperation()->getLoc();
72 auto ptrTy = LLVM::LLVMPointerType::get(builder.getContext());
73 auto voidTy = LLVM::LLVMVoidType::get(&getContext());
74
75 // Lookup or declare printf function.
76 auto printfFunc =
77 LLVM::lookupOrCreateFn(getOperation(), "printf", ptrTy, voidTy, true);
78 if (failed(printfFunc)) {
79 getOperation()->emitError("failed to lookup or create printf");
80 return signalPassFailure();
81 }
82
83 // Lookup the modules.
84 auto moduleA = lookupModule(firstModule);
85 if (!moduleA)
86 return signalPassFailure();
87 auto moduleB = lookupModule(secondModule);
88 if (!moduleB)
89 return signalPassFailure();
90
91 if (moduleA.getModuleType() != moduleB.getModuleType()) {
92 moduleA.emitError("module's IO types don't match second modules: ")
93 << moduleA.getModuleType() << " vs " << moduleB.getModuleType();
94 return signalPassFailure();
95 }
96
97 // Reuse the name of the first module for the entry function, so we don't have
98 // to do any uniquing and the LEC driver also already knows this name.
99 FunctionType functionType = FunctionType::get(&getContext(), {}, {});
100 func::FuncOp entryFunc =
101 builder.create<func::FuncOp>(loc, firstModule, functionType);
102
103 if (insertMainFunc) {
104 OpBuilder::InsertionGuard guard(builder);
105 auto i32Ty = builder.getI32Type();
106 auto mainFunc = builder.create<func::FuncOp>(
107 loc, "main", builder.getFunctionType({i32Ty, ptrTy}, {i32Ty}));
108 builder.createBlock(&mainFunc.getBody(), {}, {i32Ty, ptrTy}, {loc, loc});
109 builder.create<func::CallOp>(loc, entryFunc, ValueRange{});
110 // TODO: don't use LLVM here
111 Value constZero = builder.create<LLVM::ConstantOp>(loc, i32Ty, 0);
112 builder.create<func::ReturnOp>(loc, constZero);
113 }
114
115 builder.createBlock(&entryFunc.getBody());
116
117 auto lecOp = builder.create<verif::LogicEquivalenceCheckingOp>(loc);
118 Value areEquivalent = lecOp.getAreEquivalent();
119 builder.cloneRegionBefore(moduleA.getBody(), lecOp.getFirstCircuit(),
120 lecOp.getFirstCircuit().end());
121 builder.cloneRegionBefore(moduleB.getBody(), lecOp.getSecondCircuit(),
122 lecOp.getSecondCircuit().end());
123
124 moduleA->erase();
125 if (moduleA != moduleB)
126 moduleB->erase();
127
128 {
129 auto *term = lecOp.getFirstCircuit().front().getTerminator();
130 OpBuilder::InsertionGuard guard(builder);
131 builder.setInsertionPoint(term);
132 builder.create<verif::YieldOp>(loc, term->getOperands());
133 term->erase();
134 term = lecOp.getSecondCircuit().front().getTerminator();
135 builder.setInsertionPoint(term);
136 builder.create<verif::YieldOp>(loc, term->getOperands());
137 term->erase();
138 }
139
140 sortTopologically(&lecOp.getFirstCircuit().front());
141 sortTopologically(&lecOp.getSecondCircuit().front());
142
143 // TODO: we should find a more elegant way of reporting the result than
144 // already inserting some LLVM here
145 Value eqFormatString =
146 lookupOrCreateStringGlobal(builder, getOperation(), "c1 == c2\n");
147 Value neqFormatString =
148 lookupOrCreateStringGlobal(builder, getOperation(), "c1 != c2\n");
149 Value formatString = builder.create<LLVM::SelectOp>(
150 loc, areEquivalent, eqFormatString, neqFormatString);
151 builder.create<LLVM::CallOp>(loc, printfFunc.value(),
152 ValueRange{formatString});
153
154 builder.create<func::ReturnOp>(loc, ValueRange{});
155}
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