CIRCT 23.0.0git
Loading...
Searching...
No Matches
LTLToCore.cpp
Go to the documentation of this file.
1//===- LTLToCore.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//
9// Converts LTL and Verif operations to Core operations
10//
11//===----------------------------------------------------------------------===//
12
25#include "mlir/Dialect/Func/IR/FuncOps.h"
26#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
27#include "mlir/Dialect/SCF/IR/SCF.h"
28#include "mlir/IR/BuiltinTypes.h"
29#include "mlir/Pass/Pass.h"
30#include "mlir/Transforms/DialectConversion.h"
31#include "llvm/Support/MathExtras.h"
32
33namespace circt {
34#define GEN_PASS_DEF_LOWERLTLTOCORE
35#include "circt/Conversion/Passes.h.inc"
36} // namespace circt
37
38using namespace mlir;
39using namespace circt;
40using namespace hw;
41
42//===----------------------------------------------------------------------===//
43// Conversion patterns
44//===----------------------------------------------------------------------===//
45
46namespace {
47struct HasBeenResetOpConversion : OpConversionPattern<verif::HasBeenResetOp> {
48 using OpConversionPattern<verif::HasBeenResetOp>::OpConversionPattern;
49
50 // HasBeenReset generates a 1 bit register that is set to one once the reset
51 // has been raised and lowered at at least once.
52 LogicalResult
53 matchAndRewrite(verif::HasBeenResetOp op, OpAdaptor adaptor,
54 ConversionPatternRewriter &rewriter) const override {
55 auto i1 = rewriter.getI1Type();
56 // Generate the constant used to set the register value
57 Value constZero = seq::createConstantInitialValue(
58 rewriter, op->getLoc(), rewriter.getIntegerAttr(i1, 0));
59
60 // Generate the constant used to negate the reset value
61 Value constOne = hw::ConstantOp::create(rewriter, op.getLoc(), i1, 1);
62
63 // Create a backedge for the register to be used in the OrOp
64 circt::BackedgeBuilder bb(rewriter, op.getLoc());
65 circt::Backedge reg = bb.get(rewriter.getI1Type());
66
67 // Generate an or between the reset and the register's value to store
68 // whether or not the reset has been active at least once
69 Value orReset =
70 comb::OrOp::create(rewriter, op.getLoc(), adaptor.getReset(), reg);
71
72 // This register should not be reset, so we give it dummy reset and resetval
73 // operands to fit the build signature
74 Value reset, resetval;
75
76 // Finally generate the register to set the backedge
78 rewriter, op.getLoc(), orReset,
79 rewriter.createOrFold<seq::ToClockOp>(op.getLoc(), adaptor.getClock()),
80 rewriter.getStringAttr("hbr"), reset, resetval, constZero,
81 InnerSymAttr{} // inner_sym
82 ));
83
84 // We also need to consider the case where we are currently in a reset cycle
85 // in which case our hbr register should be down-
86 // Practically this means converting it to (and hbr (not reset))
87 Value notReset = comb::XorOp::create(rewriter, op.getLoc(),
88 adaptor.getReset(), constOne);
89 rewriter.replaceOpWithNewOp<comb::AndOp>(op, reg, notReset);
90
91 return success();
92 }
93};
94
95struct LTLImplicationConversion
96 : public OpConversionPattern<ltl::ImplicationOp> {
97 using OpConversionPattern<ltl::ImplicationOp>::OpConversionPattern;
98
99 LogicalResult
100 matchAndRewrite(ltl::ImplicationOp op, OpAdaptor adaptor,
101 ConversionPatternRewriter &rewriter) const override {
102 // Can only lower boolean implications to comb ops
103 if (!isa<IntegerType>(op.getAntecedent().getType()) ||
104 !isa<IntegerType>(op.getConsequent().getType()))
105 return failure();
106 /// A -> B = !A || B
107 auto loc = op.getLoc();
108 auto notA = comb::createOrFoldNot(rewriter, loc, adaptor.getAntecedent());
109 auto orOp =
110 comb::OrOp::create(rewriter, loc, notA, adaptor.getConsequent());
111 rewriter.replaceOp(op, orOp);
112 return success();
113 }
114};
115
116struct LTLNotConversion : public OpConversionPattern<ltl::NotOp> {
118
119 LogicalResult
120 matchAndRewrite(ltl::NotOp op, OpAdaptor adaptor,
121 ConversionPatternRewriter &rewriter) const override {
122 // Can only lower boolean nots to comb ops
123 if (!isa<IntegerType>(op.getInput().getType()))
124 return failure();
125 auto loc = op.getLoc();
126 auto inverted = comb::createOrFoldNot(rewriter, loc, adaptor.getInput());
127 rewriter.replaceOp(op, inverted);
128 return success();
129 }
130};
131
132struct LTLAndOpConversion : public OpConversionPattern<ltl::AndOp> {
134
135 LogicalResult
136 matchAndRewrite(ltl::AndOp op, OpAdaptor adaptor,
137 ConversionPatternRewriter &rewriter) const override {
138 // Can only lower boolean ands to comb ops
139 if (!isa<IntegerType>(op->getOperandTypes()[0]) ||
140 !isa<IntegerType>(op->getOperandTypes()[1]))
141 return failure();
142 auto loc = op.getLoc();
143 // Explicit twoState value to disambiguate builders
144 auto andOp =
145 comb::AndOp::create(rewriter, loc, adaptor.getOperands(), false);
146 rewriter.replaceOp(op, andOp);
147 return success();
148 }
149};
150
151struct LTLOrOpConversion : public OpConversionPattern<ltl::OrOp> {
153
154 LogicalResult
155 matchAndRewrite(ltl::OrOp op, OpAdaptor adaptor,
156 ConversionPatternRewriter &rewriter) const override {
157 // Can only lower boolean ors to comb ops
158 if (!isa<IntegerType>(op->getOperandTypes()[0]) ||
159 !isa<IntegerType>(op->getOperandTypes()[1]))
160 return failure();
161 auto loc = op.getLoc();
162 // Explicit twoState value to disambiguate builders
163 auto orOp = comb::OrOp::create(rewriter, loc, adaptor.getOperands(), false);
164 rewriter.replaceOp(op, orOp);
165 return success();
166 }
167};
168
169struct LTLPastOpConversion : public OpConversionPattern<ltl::PastOp> {
170 LTLPastOpConversion(MLIRContext *context, bool assumeFirstClock)
171 : OpConversionPattern<ltl::PastOp>(context),
172 assumeFirstClock(assumeFirstClock) {}
173
174 LogicalResult
175 matchAndRewrite(ltl::PastOp op, OpAdaptor adaptor,
176 ConversionPatternRewriter &rewriter) const override {
177 Value clock;
178 if (!adaptor.getClk()) {
179 if (!assumeFirstClock)
180 return failure();
181 // Find the first clock, looking at inputs then at to_clock operations
182 auto module = op->getParentOfType<HWModuleOp>();
183 std::optional<size_t> clockArgNum;
184 for (auto &port : module.getPortList()) {
185 if (port.isInput() && isa<seq::ClockType>(port.type)) {
186 clockArgNum = port.argNum;
187 break;
188 }
189 }
190 if (clockArgNum) {
191 clock = module.getArgumentForInput(*clockArgNum);
192 } else {
193 // If there are no clock ports, we try to_clock operations
194 auto toClockOps = module.getOps<seq::ToClockOp>();
195 if (toClockOps.empty())
196 return failure();
197 clock = (*toClockOps.begin()).getResult();
198 }
199 } else {
200 clock = seq::ToClockOp::create(rewriter, op.getLoc(), adaptor.getClk());
201 }
202
203 Value cur = adaptor.getInput();
204 Value ce =
205 hw::ConstantOp::create(rewriter, op.getLoc(), rewriter.getI1Type(), 1);
206 auto shiftreg =
207 seq::ShiftRegOp::create(rewriter, op.getLoc(), op.getDelayAttr(), cur,
208 clock, ce, {}, {}, {}, {}, {});
209 rewriter.replaceOp(op, shiftreg);
210 return success();
211 }
212
213 bool assumeFirstClock;
214};
215
216} // namespace
217
218//===----------------------------------------------------------------------===//
219// Lower LTL To Core pass
220//===----------------------------------------------------------------------===//
221
222namespace {
223struct LowerLTLToCorePass
224 : public circt::impl::LowerLTLToCoreBase<LowerLTLToCorePass> {
225 LowerLTLToCorePass() = default;
226 void runOnOperation() override;
227};
228} // namespace
229
230// Simply applies the conversion patterns defined above
231void LowerLTLToCorePass::runOnOperation() {
232 // Emit an explicit error for unsupported past ops, rather than a confusing
233 // 'failed to legalize' error
234 if (!assumeFirstClock) {
235 auto res = getOperation().walk([&](ltl::PastOp op) {
236 if (!op.getClk()) {
237 op.emitError(
238 "ltl.past operations without a clock operand are not supported.");
239 return WalkResult::interrupt();
240 }
241 return WalkResult::advance();
242 });
243 if (res.wasInterrupted())
244 return signalPassFailure();
245 }
246
247 // Set target dialects: We don't want to see any ltl or verif that might
248 // come from an AssertProperty left in the result
249 ConversionTarget target(getContext());
250 target.addLegalDialect<hw::HWDialect>();
251 target.addLegalDialect<comb::CombDialect>();
252 target.addLegalDialect<sv::SVDialect>();
253 target.addLegalDialect<seq::SeqDialect>();
254 target.addLegalDialect<ltl::LTLDialect>();
255 target.addLegalDialect<verif::VerifDialect>();
256 target.addIllegalOp<verif::HasBeenResetOp>();
257 target.addIllegalOp<ltl::PastOp>();
258
259 auto isLegal = [](Operation *op) {
260 auto hasNonAssertUsers = std::any_of(
261 op->getUsers().begin(), op->getUsers().end(), [](Operation *user) {
262 return !isa<verif::AssertOp, verif::ClockedAssertOp>(user);
263 });
264 auto hasIntegerResultTypes =
265 std::all_of(op->getResultTypes().begin(), op->getResultTypes().end(),
266 [](Type type) { return isa<IntegerType>(type); });
267 // If there are users other than asserts, we can't map it to comb (unless
268 // the return type is already integer anyway)
269 if (hasNonAssertUsers && !hasIntegerResultTypes)
270 return true;
271
272 // Otherwise illegal if operands are i1
273 return std::any_of(
274 op->getOperands().begin(), op->getOperands().end(),
275 [](Value operand) { return !isa<IntegerType>(operand.getType()); });
276 };
277 target.addDynamicallyLegalOp<ltl::ImplicationOp>(isLegal);
278 target.addDynamicallyLegalOp<ltl::NotOp>(isLegal);
279 target.addDynamicallyLegalOp<ltl::AndOp>(isLegal);
280 target.addDynamicallyLegalOp<ltl::OrOp>(isLegal);
281
282 // Create type converters, mostly just to convert an ltl property to a bool
283 mlir::TypeConverter converter;
284
285 // Convert the ltl property type to a built-in type
286 converter.addConversion([](IntegerType type) { return type; });
287 converter.addConversion([](ltl::PropertyType type) {
288 return IntegerType::get(type.getContext(), 1);
289 });
290 converter.addConversion([](ltl::SequenceType type) {
291 return IntegerType::get(type.getContext(), 1);
292 });
293
294 // Basic materializations
295 converter.addTargetMaterialization(
296 [&](mlir::OpBuilder &builder, mlir::Type resultType,
297 mlir::ValueRange inputs, mlir::Location loc) -> mlir::Value {
298 if (inputs.size() != 1)
299 return Value();
300 return UnrealizedConversionCastOp::create(builder, loc, resultType,
301 inputs[0])
302 ->getResult(0);
303 });
304
305 converter.addSourceMaterialization(
306 [&](mlir::OpBuilder &builder, mlir::Type resultType,
307 mlir::ValueRange inputs, mlir::Location loc) -> mlir::Value {
308 if (inputs.size() != 1)
309 return Value();
310 return UnrealizedConversionCastOp::create(builder, loc, resultType,
311 inputs[0])
312 ->getResult(0);
313 });
314
315 // Create the operation rewrite patters
316 RewritePatternSet patterns(&getContext());
317 patterns.add<HasBeenResetOpConversion, LTLImplicationConversion,
318 LTLNotConversion, LTLAndOpConversion, LTLOrOpConversion>(
319 converter, patterns.getContext());
320 patterns.add<LTLPastOpConversion>(patterns.getContext(), assumeFirstClock);
321 // Apply the conversions
322 if (failed(
323 applyPartialConversion(getOperation(), target, std::move(patterns))))
324 return signalPassFailure();
325
326 // Clean up remaining unrealized casts by changing assert argument types
327 getOperation().walk([&](Operation *op) {
328 if (!isa<verif::AssertOp, verif::ClockedAssertOp>(op))
329 return;
330 Value prop = op->getOperand(0);
331 if (auto cast = prop.getDefiningOp<UnrealizedConversionCastOp>()) {
332 // Make sure that the cast is from an i1, not something random that was
333 // in the input
334 if (auto intType = dyn_cast<IntegerType>(cast.getOperandTypes()[0]);
335 intType && intType.getWidth() == 1)
336 op->setOperand(0, cast.getInputs()[0]);
337 }
338 });
339}
340
341// Basic default constructor
342std::unique_ptr<mlir::Pass> circt::createLowerLTLToCorePass() {
343 return std::make_unique<LowerLTLToCorePass>();
344}
static std::unique_ptr< Context > context
static SmallVector< PortInfo > getPortList(ModuleTy &mod)
Definition HWOps.cpp:1428
Instantiate one of these and use it to build typed backedges.
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
Backedge is a wrapper class around a Value.
create(data_type, value)
Definition hw.py:433
create(cls, result_type, reset=None, reset_value=None, name=None, sym_name=None, **kwargs)
Definition seq.py:157
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< mlir::Pass > createLowerLTLToCorePass()
Definition hw.py:1
Definition ltl.py:1
reg(value, clock, reset=None, reset_value=None, name=None, sym_name=None)
Definition seq.py:21