Skip to content

Commit 10f13e0

Browse files
leonardtmaerhartfabianschuiki
authored
[Verif] LowerContractsPass (#7870)
Co-authored-by: Martin Erhart <martin.erhart@sifive.com> Co-authored-by: Fabian Schuiki <fabian@schuiki.ch>
1 parent 17c7fc8 commit 10f13e0

File tree

13 files changed

+709
-1
lines changed

13 files changed

+709
-1
lines changed

include/circt/Dialect/Verif/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mlir_tablegen(VerifEnums.h.inc -gen-enum-decls)
66
mlir_tablegen(VerifEnums.cpp.inc -gen-enum-defs)
77
add_public_tablegen_target(CIRCTVerifEnumsIncGen)
88
add_dependencies(circt-headers CIRCTVerifEnumsIncGen)
9+
add_circt_interface(VerifOpInterfaces)
910

1011
set(LLVM_TARGET_DEFINITIONS Passes.td)
1112
mlir_tablegen(Passes.h.inc -gen-pass-decls)

include/circt/Dialect/Verif/Passes.td

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,33 @@ def LowerFormalToHWPass : Pass<"lower-formal-to-hw", "mlir::ModuleOp"> {
4242
}];
4343
}
4444

45+
def LowerContractsPass : Pass<"lower-contracts", "mlir::ModuleOp"> {
46+
let summary = "Lower contracts into formal tests";
47+
let description = [{
48+
For `hw.module` ops containing any `verif.contract` ops, apply the
49+
contracts in the `hw.module` body and emit a `verif.formal` op that checks
50+
each applied contract.
51+
52+
Contracts are checked by turning `require`s into `assume`s and `ensure`s
53+
into `asserts`s. Inputs to the `hw.module` op are replaced with
54+
`verif.symbolic_value` ops and other contracts are assumed to hold by
55+
inlining the contract body.
56+
57+
Contracts are applied by turning `require`s into `assert`s and `ensure`s
58+
into `assume`s in the original `hw.module` op body. Results of the
59+
contract are replaced with `verif.symbolic_value` ops. In some cases,
60+
using the `SimplifyAssumeEqPass` after lowering contracts and before
61+
canonicalization and cse can result in a greatly simplified `hw.module` op
62+
body.
63+
}];
64+
}
65+
66+
def SimplifyAssumeEqPass : Pass<"simplify-assume-eq", "hw::HWModuleOp"> {
67+
let summary = "Use assume equal statements to simplify symbolic values";
68+
let description = [{
69+
When a symbolic value is assumed equal to another value, the symbolic value
70+
is replaced with its equal value to simplify.
71+
}];
72+
}
73+
4574
#endif // CIRCT_DIALECT_VERIF_PASSES_TD
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===- VerifOpInterfaces.h - TODO ---------------===//
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+
// This file contains the Object Model operation declarations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_H
14+
#define CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_H
15+
16+
#include "circt/Dialect/LTL/LTLTypes.h"
17+
#include "mlir/IR/OpDefinition.h"
18+
#include "mlir/IR/OpImplementation.h"
19+
20+
namespace circt {
21+
namespace verif {
22+
class RequireLike;
23+
} // namespace verif
24+
} // namespace circt
25+
26+
#include "circt/Dialect/Verif/VerifOpInterfaces.h.inc"
27+
28+
#endif // CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_TD
2+
#define CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_TD
3+
4+
include "mlir/IR/OpBase.td"
5+
6+
def RequireLike : OpInterface<"RequireLike"> {
7+
let cppNamespace = "circt::verif";
8+
9+
let description = [{
10+
Common functionality for require-like operations.
11+
}];
12+
13+
let methods = [
14+
InterfaceMethod<"Get the label",
15+
"std::optional<llvm::StringRef>", "getLabel", (ins)>,
16+
InterfaceMethod<"Get the label attribute",
17+
"mlir::StringAttr", "getLabelAttr", (ins)>,
18+
InterfaceMethod<"Get the property",
19+
"mlir::Value", "getProperty", (ins)>,
20+
InterfaceMethod<"Get the enable",
21+
"mlir::Value", "getEnable", (ins)>,
22+
];
23+
}
24+
25+
#endif // CIRCT_DIALECT_VERIF_VERIFOPINTERFACES_TD

include/circt/Dialect/Verif/VerifOps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "circt/Dialect/HW/HWTypes.h"
1616
#include "circt/Dialect/Seq/SeqTypes.h"
1717
#include "circt/Dialect/Verif/VerifDialect.h"
18+
#include "circt/Dialect/Verif/VerifOpInterfaces.h"
1819
#include "mlir/Bytecode/BytecodeOpInterface.h"
1920
#include "mlir/Interfaces/InferTypeOpInterface.h"
2021

include/circt/Dialect/Verif/VerifOps.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define CIRCT_DIALECT_VERIF_VERIFOPS_TD
1111

1212
include "circt/Dialect/Verif/VerifDialect.td"
13+
include "circt/Dialect/Verif/VerifOpInterfaces.td"
1314
include "circt/Dialect/LTL/LTLDialect.td"
1415
include "circt/Dialect/LTL/LTLTypes.td"
1516
include "circt/Dialect/HW/HWTypes.td"
@@ -346,7 +347,7 @@ def FormalOp : VerifOp<"formal", [
346347
}
347348

348349
def SymbolicValueOp : VerifOp<"symbolic_value", [
349-
HasParent<"verif::FormalOp">,
350+
ParentOneOf<["verif::FormalOp", "hw::HWModuleOp"]>,
350351
]>{
351352
let summary = "Create a symbolic value for formal verification";
352353
let description = [{
@@ -405,6 +406,7 @@ def ContractOp : VerifOp<"contract", [
405406
class RequireLikeOp<string mnemonic> : VerifOp<mnemonic, [
406407
AttrSizedOperandSegments,
407408
HasParent<"verif::ContractOp">,
409+
DeclareOpInterfaceMethods<RequireLike>
408410
]> {
409411
let arguments = (ins
410412
LTLAnyPropertyType:$property,

include/circt/Dialect/Verif/VerifPasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace circt {
2222
namespace verif {
2323
class FormalOp;
24+
class RequireLike;
2425
} // namespace verif
2526
} // namespace circt
2627

lib/Dialect/Verif/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
add_circt_dialect_library(CIRCTVerif
22
VerifDialect.cpp
33
VerifOps.cpp
4+
VerifOpInterfaces.cpp
45

56
ADDITIONAL_HEADER_DIRS
67
${CIRCT_MAIN_INCLUDE_DIR}/circt/Dialect/Verif
78

89
DEPENDS
910
MLIRVerifIncGen
11+
MLIRVerifOpInterfacesIncGen
1012

1113
LINK_COMPONENTS
1214
Support

lib/Dialect/Verif/Transforms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ add_circt_dialect_library(CIRCTVerifTransforms
22
VerifyClockedAssertLike.cpp
33
PrepareForFormal.cpp
44
LowerFormalToHW.cpp
5+
LowerContracts.cpp
6+
SimplifyAssumeEq.cpp
57

68
DEPENDS
79
CIRCTVerifTransformsIncGen

0 commit comments

Comments
 (0)