-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LTL to Core] Add lowering for AssertProperty operations #6974
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa209ef
Add lowering for AssertProperty operations
dobios e4ea851
removed nl at start of file
dobios 2f4b71d
sorted cmake and removed unreachable branches
dobios 56b072b
Merge branch 'main' into dev/dobios/assertproperty-lowering
dobios fb4588c
Removed useless wire in test + removed symbol from hbr compreg
dobios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===- LTLToCore.h - LTL to Core conversion pass ----------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file declares passes which together will convert the LTL and Verif | ||
// operations to Core operations. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_CONVERSION_LTLTOCORE_H | ||
#define CIRCT_CONVERSION_LTLTOCORE_H | ||
|
||
#include "circt/Support/LLVM.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace circt { | ||
|
||
std::unique_ptr<mlir::Pass> createLowerLTLToCorePass(); | ||
|
||
} // namespace circt | ||
|
||
#endif // CIRCT_CONVERSION_LTLTOCORE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
add_circt_conversion_library(CIRCTLTLToCore | ||
LTLToCore.cpp | ||
|
||
DEPENDS | ||
CIRCTConversionPassIncGen | ||
|
||
LINK_COMPONENTS | ||
Core | ||
|
||
LINK_LIBS PUBLIC | ||
CIRCTHW | ||
CIRCTSV | ||
CIRCTComb | ||
CIRCTSeq | ||
CIRCTLTL | ||
CIRCTVerif | ||
MLIRTransforms | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
//===- LTLToCore.cpp -----------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Converts LTL and Verif operations to Core operations | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt/Conversion/LTLToCore.h" | ||
#include "../PassDetail.h" | ||
#include "circt/Conversion/HWToSV.h" | ||
#include "circt/Dialect/Comb/CombOps.h" | ||
#include "circt/Dialect/HW/HWOps.h" | ||
#include "circt/Dialect/LTL/LTLDialect.h" | ||
#include "circt/Dialect/LTL/LTLOps.h" | ||
#include "circt/Dialect/SV/SVDialect.h" | ||
#include "circt/Dialect/SV/SVOps.h" | ||
#include "circt/Dialect/Seq/SeqOps.h" | ||
#include "circt/Dialect/Verif/VerifOps.h" | ||
#include "circt/Support/BackedgeBuilder.h" | ||
#include "circt/Support/Namespace.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Dialect/SCF/IR/SCF.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "llvm/Support/MathExtras.h" | ||
|
||
using namespace mlir; | ||
using namespace circt; | ||
using namespace hw; | ||
|
||
static sv::EventControl ltlToSVEventControl(ltl::ClockEdge ce) { | ||
switch (ce) { | ||
case ltl::ClockEdge::Pos: | ||
return sv::EventControl::AtPosEdge; | ||
case ltl::ClockEdge::Neg: | ||
return sv::EventControl::AtNegEdge; | ||
case ltl::ClockEdge::Both: | ||
return sv::EventControl::AtEdge; | ||
} | ||
llvm_unreachable("Unknown event control kind"); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Conversion patterns | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
|
||
// Custom pattern matchers | ||
|
||
// Matches and records a boolean attribute | ||
struct I1ValueMatcher { | ||
Value *what; | ||
I1ValueMatcher(Value *what) : what(what) {} | ||
bool match(Value op) const { | ||
if (!op.getType().isSignlessInteger(1)) | ||
return false; | ||
*what = op; | ||
return true; | ||
} | ||
}; | ||
|
||
static inline I1ValueMatcher mBool(Value *const val) { | ||
return I1ValueMatcher(val); | ||
} | ||
|
||
// Matches and records an arbitrary op | ||
template <typename OpType, typename... OperandMatchers> | ||
struct BindingRecursivePatternMatcher | ||
: mlir::detail::RecursivePatternMatcher<OpType, OperandMatchers...> { | ||
|
||
using BaseMatcher = | ||
mlir::detail::RecursivePatternMatcher<OpType, OperandMatchers...>; | ||
BindingRecursivePatternMatcher(OpType *bop, OperandMatchers... matchers) | ||
: BaseMatcher(matchers...), opBind(bop) {} | ||
|
||
bool match(Operation *op) { | ||
if (BaseMatcher::match(op)) { | ||
*opBind = llvm::cast<OpType>(op); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
OpType *opBind; | ||
}; | ||
|
||
template <typename OpType, typename... Matchers> | ||
static inline auto mOpWithBind(OpType *op, Matchers... matchers) { | ||
return BindingRecursivePatternMatcher<OpType, Matchers...>(op, matchers...); | ||
} | ||
|
||
struct HasBeenResetOpConversion : OpConversionPattern<verif::HasBeenResetOp> { | ||
using OpConversionPattern<verif::HasBeenResetOp>::OpConversionPattern; | ||
|
||
// HasBeenReset generates a 1 bit register that is set to one once the reset | ||
// has been raised and lowered at at least once. | ||
LogicalResult | ||
matchAndRewrite(verif::HasBeenResetOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto i1 = rewriter.getI1Type(); | ||
// Generate the constant used to set the register value | ||
Value constZero = rewriter.create<hw::ConstantOp>(op.getLoc(), i1, 0); | ||
|
||
// Generate the constant used to enegate the | ||
Value constOne = rewriter.create<hw::ConstantOp>(op.getLoc(), i1, 1); | ||
|
||
// Create a backedge for the register to be used in the OrOp | ||
circt::BackedgeBuilder bb(rewriter, op.getLoc()); | ||
circt::Backedge reg = bb.get(rewriter.getI1Type()); | ||
|
||
// Generate an or between the reset and the register's value to store | ||
// whether or not the reset has been active at least once | ||
Value orReset = | ||
rewriter.create<comb::OrOp>(op.getLoc(), adaptor.getReset(), reg); | ||
|
||
// This register should not be reset, so we give it dummy reset and resetval | ||
// operands to fit the build signature | ||
Value reset, resetval; | ||
|
||
// Finally generate the register to set the backedge | ||
reg.setValue(rewriter.create<seq::CompRegOp>( | ||
op.getLoc(), orReset, | ||
rewriter.createOrFold<seq::ToClockOp>(op.getLoc(), adaptor.getClock()), | ||
reset, resetval, llvm::StringRef("hbr"), constZero)); | ||
|
||
// We also need to consider the case where we are currently in a reset cycle | ||
// in which case our hbr register should be down- | ||
// Practically this means converting it to (and hbr (not reset)) | ||
Value notReset = | ||
rewriter.create<comb::XorOp>(op.getLoc(), adaptor.getReset(), constOne); | ||
rewriter.replaceOpWithNewOp<comb::AndOp>(op, reg, notReset); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
struct AssertOpConversionPattern : OpConversionPattern<verif::AssertOp> { | ||
using OpConversionPattern<verif::AssertOp>::OpConversionPattern; | ||
|
||
Value visit(ltl::DisableOp op, ConversionPatternRewriter &rewriter, | ||
Value operand = nullptr) const { | ||
// Replace the ltl::DisableOp with an OR op as it represents a disabling | ||
// implication: (implies (not condition) input) is equivalent to | ||
// (or (not (not condition)) input) which becomes (or condition input) | ||
return rewriter.replaceOpWithNewOp<comb::OrOp>( | ||
op, op.getCondition(), operand ? operand : op.getInput()); | ||
} | ||
|
||
// Special case : we want to detect the Non-overlapping implication, | ||
// Overlapping Implication or simply AssertProperty patterns and reject | ||
// everything else for now: antecedent : ltl::concatOp || immediate predicate | ||
// consequent : any other non-sequence op | ||
// We want to support a ##n true |-> b and a |-> b | ||
LogicalResult | ||
matchAndRewrite(verif::AssertOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
|
||
Value ltlClock, disableCond, disableInput, disableVal; | ||
ltl::ClockOp clockOp; | ||
ltl::DisableOp disableOp; | ||
|
||
// Look for the Assert Property pattern | ||
bool matchedProperty = matchPattern( | ||
op.getProperty(), | ||
mOpWithBind<ltl::ClockOp>( | ||
&clockOp, | ||
mOpWithBind<ltl::DisableOp>(&disableOp, mBool(&disableInput), | ||
mBool(&disableCond)), | ||
mBool(<lClock))); | ||
|
||
if (!matchedProperty) | ||
return rewriter.notifyMatchFailure( | ||
op, " verif.assert used outside of an assert property!"); | ||
|
||
// Then visit the disable op | ||
disableVal = visit(disableOp, rewriter, disableInput); | ||
|
||
// Generate the parenting sv.always posedge clock from the ltl | ||
// clock, containing the generated sv.assert | ||
rewriter.create<sv::AlwaysOp>( | ||
clockOp.getLoc(), ltlToSVEventControl(clockOp.getEdge()), ltlClock, | ||
[&] { | ||
// Generate the sv assertion using the input to the | ||
// parenting clock | ||
rewriter.replaceOpWithNewOp<sv::AssertOp>( | ||
op, disableVal, | ||
sv::DeferAssertAttr::get(getContext(), | ||
sv::DeferAssert::Immediate), | ||
op.getLabelAttr()); | ||
}); | ||
|
||
// Erase Converted Ops | ||
rewriter.eraseOp(clockOp); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Lower LTL To Core pass | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
struct LowerLTLToCorePass : public LowerLTLToCoreBase<LowerLTLToCorePass> { | ||
LowerLTLToCorePass() = default; | ||
void runOnOperation() override; | ||
}; | ||
} // namespace | ||
|
||
// Simply applies the conversion patterns defined above | ||
void LowerLTLToCorePass::runOnOperation() { | ||
|
||
// Set target dialects: We don't want to see any ltl or verif that might | ||
// come from an AssertProperty left in the result | ||
ConversionTarget target(getContext()); | ||
target.addLegalDialect<hw::HWDialect>(); | ||
target.addLegalDialect<comb::CombDialect>(); | ||
target.addLegalDialect<sv::SVDialect>(); | ||
target.addLegalDialect<seq::SeqDialect>(); | ||
target.addLegalDialect<ltl::LTLDialect>(); | ||
target.addIllegalDialect<verif::VerifDialect>(); | ||
|
||
// Create type converters, mostly just to convert an ltl property to a bool | ||
mlir::TypeConverter converter; | ||
|
||
// Convert the ltl property type to a built-in type | ||
converter.addConversion([](IntegerType type) { return type; }); | ||
converter.addConversion([](ltl::PropertyType type) { | ||
return IntegerType::get(type.getContext(), 1); | ||
}); | ||
converter.addConversion([](ltl::SequenceType type) { | ||
return IntegerType::get(type.getContext(), 1); | ||
}); | ||
|
||
// Basic materializations | ||
converter.addTargetMaterialization( | ||
[&](mlir::OpBuilder &builder, mlir::Type resultType, | ||
mlir::ValueRange inputs, | ||
mlir::Location loc) -> std::optional<mlir::Value> { | ||
if (inputs.size() != 1) | ||
return std::nullopt; | ||
return inputs[0]; | ||
}); | ||
|
||
converter.addSourceMaterialization( | ||
[&](mlir::OpBuilder &builder, mlir::Type resultType, | ||
mlir::ValueRange inputs, | ||
mlir::Location loc) -> std::optional<mlir::Value> { | ||
if (inputs.size() != 1) | ||
return std::nullopt; | ||
return inputs[0]; | ||
}); | ||
|
||
// Create the operation rewrite patters | ||
RewritePatternSet patterns(&getContext()); | ||
patterns.add<AssertOpConversionPattern, HasBeenResetOpConversion>( | ||
converter, patterns.getContext()); | ||
|
||
// Apply the conversions | ||
if (failed( | ||
applyPartialConversion(getOperation(), target, std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
|
||
// Basic default constructor | ||
std::unique_ptr<mlir::Pass> circt::createLowerLTLToCorePass() { | ||
return std::make_unique<LowerLTLToCorePass>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to use the default builder instead, which accepts a parameter for every operand and attribute:
Something like the following could work: