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