-
Notifications
You must be signed in to change notification settings - Fork 486
[circt-bmc] handle 'verif.formal' / 'verif.contract' op in module #10772
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
Open
Clo91eaf
wants to merge
3
commits into
llvm:main
Choose a base branch
from
xinpian-tech:circt-bmc-verif-formal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| #include "circt/Dialect/SV/SVOps.h" | ||
| #include "circt/Dialect/Verif/VerifOps.h" | ||
| #include "circt/Dialect/Verif/VerifPasses.h" | ||
| #include "llvm/ADT/DenseSet.h" | ||
|
|
||
| using namespace circt; | ||
| using namespace mlir; | ||
|
|
@@ -29,6 +30,7 @@ struct LowerSymbolicValuesPass | |
| void runOnOperation() override; | ||
| LogicalResult lowerToExtModule(); | ||
| void lowerToAnySeqWire(); | ||
| LogicalResult lowerToHWInputs(); | ||
| }; | ||
| } // namespace | ||
|
|
||
|
|
@@ -41,6 +43,10 @@ void LowerSymbolicValuesPass::runOnOperation() { | |
| case SymbolicValueLowering::Yosys: | ||
| lowerToAnySeqWire(); | ||
| break; | ||
| case SymbolicValueLowering::HWInput: | ||
| if (failed(lowerToHWInputs())) | ||
| signalPassFailure(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -113,3 +119,50 @@ void LowerSymbolicValuesPass::lowerToAnySeqWire() { | |
| op.erase(); | ||
| }); | ||
| } | ||
|
|
||
| /// Replace `SymbolicValueOp`s in `hw.module` bodies with input ports. | ||
| LogicalResult LowerSymbolicValuesPass::lowerToHWInputs() { | ||
| auto module = getOperation(); | ||
| DenseSet<StringAttr> referencedModules; | ||
| module.walk([&](InstanceOp op) { | ||
| referencedModules.insert(op.getReferencedModuleNameAttr()); | ||
| }); | ||
|
|
||
| auto result = module.walk([&](SymbolicValueOp op) -> WalkResult { | ||
| if (!op->getParentOfType<HWModuleOp>()) | ||
| return op.emitError() | ||
| << "cannot lower symbolic value to hw.module input outside of an " | ||
| "hw.module"; | ||
| return WalkResult::advance(); | ||
| }); | ||
| if (result.wasInterrupted()) | ||
| return failure(); | ||
|
|
||
| for (auto hwModule : module.getOps<HWModuleOp>()) { | ||
| SmallVector<SymbolicValueOp> symbolicValues; | ||
| hwModule.walk([&](SymbolicValueOp op) { symbolicValues.push_back(op); }); | ||
| if (symbolicValues.empty()) | ||
| continue; | ||
|
|
||
| if (referencedModules.contains(hwModule.getModuleNameAttr())) { | ||
| hwModule.emitError() | ||
| << "cannot lower symbolic values in instantiated module '" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we clarify that this is only the case for this particular lowering strategy? |
||
| << hwModule.getModuleName() | ||
| << "' to HW inputs; run the 'hw-input' lowering strategy after " | ||
| "flattening modules"; | ||
| return failure(); | ||
| } | ||
|
|
||
| for (auto symbolicValue : symbolicValues) { | ||
| auto [name, arg] = hwModule.insertInput( | ||
| hwModule.getNumInputPorts(), | ||
| StringAttr::get(module.getContext(), "symbolic_value"), | ||
| symbolicValue.getType()); | ||
| (void)name; | ||
| symbolicValue.getResult().replaceAllUsesWith(arg); | ||
| symbolicValue.erase(); | ||
| } | ||
| } | ||
|
|
||
| return success(); | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,31 @@ | ||
| // RUN: circt-opt --verif-lower-symbolic-values=mode=extmodule --split-input-file --verify-diagnostics %s | ||
| // RUN: split-file %s %t | ||
| // RUN: circt-opt --verif-lower-symbolic-values=mode=extmodule --verify-diagnostics %t/extmodule.mlir | ||
| // RUN: circt-opt --verif-lower-symbolic-values=mode=hw-input --verify-diagnostics %t/hw-input-instantiated.mlir | ||
| // RUN: circt-opt --verif-lower-symbolic-values=mode=hw-input --verify-diagnostics %t/hw-input-formal.mlir | ||
|
|
||
| //--- extmodule.mlir | ||
|
|
||
| hw.module @Foo() { | ||
| // expected-error @below {{symbolic value bit width unknown}} | ||
| verif.symbolic_value : !hw.string | ||
| } | ||
|
|
||
| //--- hw-input-instantiated.mlir | ||
|
|
||
| hw.module @Top(out y : i8) { | ||
| %0 = hw.instance "m" @Instantiated() -> (y: i8) | ||
| hw.output %0 : i8 | ||
| } | ||
|
|
||
| // expected-error @+1 {{cannot lower symbolic values in instantiated module 'Instantiated' to HW inputs; run the 'hw-input' lowering strategy after flattening modules}} | ||
| hw.module @Instantiated(out y : i8) { | ||
| %0 = verif.symbolic_value : i8 | ||
| hw.output %0 : i8 | ||
| } | ||
|
|
||
| //--- hw-input-formal.mlir | ||
|
|
||
| verif.formal @Formal {} { | ||
| // expected-error @below {{cannot lower symbolic value to hw.module input outside of an hw.module}} | ||
| verif.symbolic_value : i8 | ||
| } |
This file contains hidden or 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 hidden or 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,41 @@ | ||
| // RUN: circt-opt %s --lower-contracts | circt-bmc - -b 1 --module Caller_CheckContract_0 --emit-mlir -o - | FileCheck %s | ||
|
|
||
| // CHECK-NOT: verif.symbolic_value | ||
| // CHECK-LABEL: func.func @Callee( | ||
| // CHECK-SAME: %[[CALLEE_INPUT:.+]]: !smt.bv<8>, %[[CALLEE_RESULT:.+]]: !smt.bv<8> | ||
| // CHECK: %[[CALLEE_EQ:.+]] = smt.eq %[[CALLEE_RESULT]], %[[CALLEE_INPUT]] : !smt.bv<8> | ||
| // CHECK: %[[CALLEE_ITE:.+]] = smt.ite %[[CALLEE_EQ]], | ||
| // CHECK: %[[CALLEE_ASSUME:.+]] = smt.eq %[[CALLEE_ITE]], | ||
| // CHECK: smt.assert %[[CALLEE_ASSUME]] | ||
| hw.module @Callee(in %a : i8, out y : i8) { | ||
| %0 = verif.contract %a : i8 { | ||
| %1 = comb.icmp eq %0, %a : i8 | ||
| verif.ensure %1 : i1 | ||
| } | ||
| hw.output %0 : i8 | ||
| } | ||
|
|
||
| // CHECK-NOT: func.func @Caller( | ||
| // CHECK-LABEL: func.func @Caller_CheckContract_0() | ||
| // CHECK: smt.solver | ||
| hw.module @Caller(in %a : i8, out y : i8) { | ||
| %0 = hw.instance "callee" @Callee(a: %a: i8) -> (y: i8) | ||
| %1 = verif.contract %0 : i8 { | ||
| %2 = comb.icmp eq %1, %a : i8 | ||
| verif.ensure %2 : i1 | ||
| } | ||
| hw.output %1 : i8 | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @bmc_circuit( | ||
| // CHECK-SAME: %[[INPUT:.+]]: !smt.bv<8>, %[[RESULT:.+]]: !smt.bv<8>) | ||
| // CHECK: %[[ASSUME_EQ:.+]] = smt.eq %[[RESULT]], %[[INPUT]] : !smt.bv<8> | ||
| // CHECK: %[[ASSUME_ITE:.+]] = smt.ite %[[ASSUME_EQ]], | ||
| // CHECK: %[[ASSUME:.+]] = smt.eq %[[ASSUME_ITE]], | ||
| // CHECK: smt.assert %[[ASSUME]] | ||
| // CHECK: %[[ASSERT_EQ:.+]] = smt.eq %[[RESULT]], %[[INPUT]] : !smt.bv<8> | ||
| // CHECK: %[[ASSERT_ITE:.+]] = smt.ite %[[ASSERT_EQ]], | ||
| // CHECK: %[[ASSERT:.+]] = smt.eq %[[ASSERT_ITE]], | ||
| // CHECK: %[[VIOLATED:.+]] = smt.not %[[ASSERT]] | ||
| // CHECK: smt.assert %[[VIOLATED]] | ||
| // CHECK-NOT: verif.symbolic_value |
This file contains hidden or 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
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.
I think this is missing a test
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.
fixed! Thanks!