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> {
171
172 LogicalResult
173 matchAndRewrite(ltl::PastOp op, OpAdaptor adaptor,
174 ConversionPatternRewriter &rewriter) const override {
175 Value clock =
176 seq::ToClockOp::create(rewriter, op.getLoc(), adaptor.getClk());
177 Value cur = adaptor.getInput();
178 Value ce =
179 hw::ConstantOp::create(rewriter, op.getLoc(), rewriter.getI1Type(), 1);
180 auto shiftreg =
181 seq::ShiftRegOp::create(rewriter, op.getLoc(), op.getDelayAttr(), cur,
182 clock, ce, {}, {}, {}, {}, {});
183 rewriter.replaceOp(op, shiftreg);
184 return success();
185 }
186};
187
188} // namespace
189
190//===----------------------------------------------------------------------===//
191// Lower LTL To Core pass
192//===----------------------------------------------------------------------===//
193
194namespace {
195struct LowerLTLToCorePass
196 : public circt::impl::LowerLTLToCoreBase<LowerLTLToCorePass> {
197 LowerLTLToCorePass() = default;
198 void runOnOperation() override;
199};
200} // namespace
201
202// Simply applies the conversion patterns defined above
203void LowerLTLToCorePass::runOnOperation() {
204 // Set target dialects: We don't want to see any ltl or verif that might
205 // come from an AssertProperty left in the result
206 ConversionTarget target(getContext());
207 target.addLegalDialect<hw::HWDialect>();
208 target.addLegalDialect<comb::CombDialect>();
209 target.addLegalDialect<sv::SVDialect>();
210 target.addLegalDialect<seq::SeqDialect>();
211 target.addLegalDialect<ltl::LTLDialect>();
212 target.addLegalDialect<verif::VerifDialect>();
213 target.addIllegalOp<verif::HasBeenResetOp>();
214 target.addIllegalOp<ltl::PastOp>();
215
216 auto isLegal = [](Operation *op) {
217 auto hasNonAssertUsers = std::any_of(
218 op->getUsers().begin(), op->getUsers().end(), [](Operation *user) {
219 return !isa<verif::AssertOp, verif::ClockedAssertOp>(user);
220 });
221 auto hasIntegerResultTypes =
222 std::all_of(op->getResultTypes().begin(), op->getResultTypes().end(),
223 [](Type type) { return isa<IntegerType>(type); });
224 // If there are users other than asserts, we can't map it to comb (unless
225 // the return type is already integer anyway)
226 if (hasNonAssertUsers && !hasIntegerResultTypes)
227 return true;
228
229 // Otherwise illegal if operands are i1
230 return std::any_of(
231 op->getOperands().begin(), op->getOperands().end(),
232 [](Value operand) { return !isa<IntegerType>(operand.getType()); });
233 };
234 target.addDynamicallyLegalOp<ltl::ImplicationOp>(isLegal);
235 target.addDynamicallyLegalOp<ltl::NotOp>(isLegal);
236 target.addDynamicallyLegalOp<ltl::AndOp>(isLegal);
237 target.addDynamicallyLegalOp<ltl::OrOp>(isLegal);
238
239 // Create type converters, mostly just to convert an ltl property to a bool
240 mlir::TypeConverter converter;
241
242 // Convert the ltl property type to a built-in type
243 converter.addConversion([](IntegerType type) { return type; });
244 converter.addConversion([](ltl::PropertyType type) {
245 return IntegerType::get(type.getContext(), 1);
246 });
247 converter.addConversion([](ltl::SequenceType type) {
248 return IntegerType::get(type.getContext(), 1);
249 });
250
251 // Basic materializations
252 converter.addTargetMaterialization(
253 [&](mlir::OpBuilder &builder, mlir::Type resultType,
254 mlir::ValueRange inputs, mlir::Location loc) -> mlir::Value {
255 if (inputs.size() != 1)
256 return Value();
257 return UnrealizedConversionCastOp::create(builder, loc, resultType,
258 inputs[0])
259 ->getResult(0);
260 });
261
262 converter.addSourceMaterialization(
263 [&](mlir::OpBuilder &builder, mlir::Type resultType,
264 mlir::ValueRange inputs, mlir::Location loc) -> mlir::Value {
265 if (inputs.size() != 1)
266 return Value();
267 return UnrealizedConversionCastOp::create(builder, loc, resultType,
268 inputs[0])
269 ->getResult(0);
270 });
271
272 // Create the operation rewrite patters
273 RewritePatternSet patterns(&getContext());
275 .add<HasBeenResetOpConversion, LTLImplicationConversion, LTLNotConversion,
276 LTLAndOpConversion, LTLOrOpConversion, LTLPastOpConversion>(
277 converter, patterns.getContext());
278 // Apply the conversions
279 if (failed(
280 applyPartialConversion(getOperation(), target, std::move(patterns))))
281 return signalPassFailure();
282
283 // Clean up remaining unrealized casts by changing assert argument types
284 getOperation().walk([&](Operation *op) {
285 if (!isa<verif::AssertOp, verif::ClockedAssertOp>(op))
286 return;
287 Value prop = op->getOperand(0);
288 if (auto cast = prop.getDefiningOp<UnrealizedConversionCastOp>()) {
289 // Make sure that the cast is from an i1, not something random that was
290 // in the input
291 if (auto intType = dyn_cast<IntegerType>(cast.getOperandTypes()[0]);
292 intType && intType.getWidth() == 1)
293 op->setOperand(0, cast.getInputs()[0]);
294 }
295 });
296}
297
298// Basic default constructor
299std::unique_ptr<mlir::Pass> circt::createLowerLTLToCorePass() {
300 return std::make_unique<LowerLTLToCorePass>();
301}
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
reg(value, clock, reset=None, reset_value=None, name=None, sym_name=None)
Definition seq.py:21