Chisel high-level circuit design conversion and verification method and device
By generating Scala simulation code and combining the abstract syntax trees of Chisel and Scala, the problem of missing parameters and inconsistent design semantics in Chisel's high-level circuit design is solved, and complete verification of Chisel's high-level design and correctness proof under different parameter configurations are achieved.
Patent Information
- Application Number
- CN202410462685.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-17
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2044-04-17
AI Technical Summary
Existing technology cannot fully verify the correctness of Chisel's high-level circuit design. Parameters are lost during the instantiation process. As a result, the generated code can only represent the circuit design under specific parameters, cannot express the semantic and structural information of high-level design, and cannot interpret different designs. The circuit under the parameters is verified uniformly.
By analyzing Chisel high-level circuit design code, Scala simulation code is generated, using different parameter configurations for simulation testing or formal verification, retaining parameterized information and structural information, and using a statement reordering mechanism to solve the problem of inconsistency between hardware design semantics and software semantics, providing The Scala simulation library corresponding to the Chisel hardware design primitives combines the abstract syntax trees of Chisel and Scala to represent circuit design and control information.
It achieves complete verification of Chisel's high-level design, supports rapid testing and formal verification under different parameter configurations, retains the design's parameter and structural information, and ensures the correctness of the circuit design under different parameters.
Smart Images

Figure CN118428285B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a conversion and verification method and device for Chisel high-level circuit design. Technical Background
[0002] Chisel is an open source hardware construction language designed by the University of California, Berkeley, and plays an important role in the currently popular agile hardware development. Chisel adds hardware construction primitives to the Scala programming language, providing users with the capabilities of a modern programming language to write complex, parameterized circuit generators that generate synthesizable Verilog / SystemVerilog code. This generator method and parameter support can create reusable components and libraries that can be adapted to different specific application scenarios through parameters and other Scala syntax features. For example, the FIFO queue in the Chisel library can set different queue sizes and data types within the queue, thereby increasing the level of abstraction in the design while retaining fine-grained control.
[0003] Currently, verification of Chisel circuit design is performed on specific circuits generated by Chisel. Chisel code is executed to generate higher-level HiFIRRTL code; LoFIRRTL with simple structure and syntax is obtained by expanding, simplifying and arranging; LoFIRRTL is verified or converted to Verilog / System Verilog, BTOR2 and other formats for verification.
[0004] Use simulation test method for verification. For FIRRTL verification, you can use the Treadle engine officially provided by Chisel for simulation. This method is better integrated with the development process of Chisel circuit design; Treadle is also developed in Scala and can be directly called in the Chisel project code; the official also provides the ChiselTest library to facilitate writing test code. For Verilog / System Verilog verification, you can use its corresponding simulator, such as open source simulators such as Verilator or commercial simulators such as VCS. Verilog, as a hardware description language, is widely used in the field of hardware design, and has a wealth of tools for software and hardware simulation, including simulation using FPGA. After the above simulation is completed, you can use the constructed or generated examples for testing, and judge whether the circuit behavior meets expectations based on the circuit operation results, and then judge whether there are problems with the Chisel circuit design.
[0005] Use formal methods for verification. For the BTOR2 or SMT formula generated by Chisel circuit design, according to the properties and conditional constraints set in Chisel, it is handed over to the model checking tool or SMT formula solver for checking. This method can be carried out without test cases, and has a certain theoretical guarantee for the verification effect.
[0006] The above verification method cannot fully explain the correctness of the Chisel high-level circuit design itself. In the process of instantiating and generating circuits, the parameters in the Chisel design will be instantiated, and the control information of the parameters will be lost; the code structure in the Chisel high-level design will be destroyed. The final code generated can only represent a circuit design under specific parameters, and cannot express the semantics and structural information in the Chisel high-level design; the verification work based on the code can only explain the correctness of the Chisel design under specific parameters, and cannot explain the correctness of the Chisel high-level circuit design as a whole; for circuits under different parameters, it is necessary to enumerate the parameters for verification separately. Summary of the invention
[0007] In view of the above problems, the present invention proposes a method and device for converting and verifying Chisel high-level circuit design. The present invention converts and generates a Scala simulation code by analyzing and processing the Chisel high-level circuit design code itself; the converted code can be simulated and tested using different parameter configurations, and can also use formal methods to verify different parameters at the same time; the parameterization information and most of the structural information of the Chisel design can be retained; the problem of inconsistency between the semantics of Chisel hardware design and the semantics of sequential execution software is solved through the mechanism of statement reordering; a Scala simulation library corresponding to the hardware design primitives in Chisel is provided; the method internally uses an abstract syntax tree that combines the characteristics of Chisel and Scala to conveniently represent circuit design and control information.
[0008] The technical solution adopted by the present invention is as follows:
[0009] A method for converting and verifying a Chisel high-level circuit design comprises the following steps:
[0010] 1) Analyze the Chisel code content and generate the internal syntax tree structure;
[0011] 2) Analyze each statement and statement block on the internal syntax tree and identify the signals and variables involved;
[0012] 3) Expand and transform the internal syntax tree based on the identified signals, variables and internal syntax tree structure;
[0013] 4) Sorting the expanded and transformed internal syntax tree according to the dependency relationship between signals and variables to generate a sorted internal syntax tree;
[0014] 5) Based on the sorted internal syntax tree, the behavior of the Chisel circuit design is simulated through software modeling to generate Scala simulation code;
[0015] 6) Use the generated Scala simulation code for simulation testing and formal verification.
[0016] Furthermore, in step 1), the Chisel code content is analyzed to identify the common Scala code and Chisel hardware design primitives, and corresponding two types of internal syntax tree nodes are generated.
[0017] Furthermore, the Chisel hardware design primitives in step 1) include the definition of Module, the definition of Bundle, the definition and use of Wire, Reg, IO, Node, UInt, SInt, Bool, Vec hardware data types and their operators, the use of submodules, and the components and methods provided by Chisel.
[0018] Furthermore, for each statement in step 2), the signal updated through the hardware connection is identified as the connection signal, the signal used for the connection and conditional judgment is identified as the usage signal, the newly defined or updated Scala variables and functions are identified as the update variables, and the calls to the defined variables and functions are identified as the usage variables.
[0019] Furthermore, in step 2), for each statement block, its connection signal is the union of the connection signals of all sub-statements, its usage signal is the union of the usage signals of all sub-statements, its update variable is the union of the update variables of all sub-statements, and its usage variable is the union of the usage variables of all sub-statements.
[0020] Furthermore, in step 2), the rules followed for register connection signals and use signal identification include:
[0021] The use of registers is regarded as the use of their current clock cycle values. When analyzing register signals, any read operation on registers is based on the current values.
[0022] The connection to the register is regarded as the connection to the value of the next clock cycle. The connection operation performed in the current clock cycle takes effect in the next clock cycle.
[0023] The signal name suffix is used to distinguish signal values in different clock cycles.
[0024] Furthermore, in step 3), the expansion and transformation of the internal syntax tree specifically includes:
[0025] Replace the statements using enumeration values of hardware enumeration types with corresponding integer literals;
[0026] Expand the statements for defining registers and setting enable signals into two statements: register definition and connection assignment when the enable condition is met;
[0027] The submodule definition is expanded into submodule definition, submodule interface signal definition and submodule call, and all calls to the original submodule interface are replaced with calls to the new submodule interface signal.
[0028] Furthermore, the step of sorting statements according to the dependency relationship between signals and variables in step 4) includes:
[0029] Treat indivisible statements as minimal statements, and split divisible statements or statement blocks into indivisible minimal statements;
[0030] Number each minimum statement, the number is an integer sequence;
[0031] Generate a dependency graph based on the dependencies between signals and variables between minimal statements;
[0032] Perform topological sorting on the dependency graph, that is, sorting the nodes according to the progress of the dependency relationships between them to obtain a numbered topological sequence;
[0033] The statements from the same statement block in the topological sequence are merged according to the numbers, and a sorted internal syntax tree is generated according to the numbers of the merged topological sequence; if the number prefixes of two statements are the same and the topological sequences are adjacent, they are merged in topological order to generate a parent statement containing two child statements, and the merged statements are further merged according to the number prefixes.
[0034] Furthermore, the step of numbering each minimum statement includes:
[0035] For circuit modules or inseparable statement blocks, the top-level statements have only one serial number, starting from 1.
[0036] For separable statements and statement blocks, the sub-statement number is prefixed by the parent statement number, followed by the sub-statement's sequence number in the statement sequence;
[0037] For when-otherwise and if-else statements, add the suffix 1 to the branch where the condition is met, and add the suffix 2 to the branch where the condition is not met;
[0038] For each branch of the switch statement, add a numbered suffix in sequence starting at 1.
[0039] Furthermore, in the dependency graph, each node represents each minimum statement, and the directed edges represent the dependencies between statements caused by signals or variables. The dependent statements are executed first after sorting; the dependencies include: the connected signal depends on the previous signal connection, the used signal depends on the last connection of the signal, the write-after-read, write-after-write, and read-after-write dependencies between all signals and variables caused by assignment and use, and the dependencies on branch condition signals and variables on the path.
[0040] Further, the step of simulating the behavior of the Chisel circuit design in step 5) includes:
[0041] Use Scala types and operators to simulate computations between circuit signals in Chisel;
[0042] Simulate the connection between circuit signals in Chisel through Scala variable assignment;
[0043] Use Scala class to represent Chisel circuit module, and the construction parameters are the original Chisel circuit parameters;
[0044] A migration function is used to represent the calculation of the combinational circuit within one clock of the circuit. The parameters of the function are the circuit input signal and the register value of the previous clock. The return value of the function is the circuit output signal and the new register value.
[0045] For registers, two variables, the current clock value and the next clock value, are used to represent them;
[0046] Submodules are called as functions.
[0047] A conversion and verification device for Chisel high-level circuit design comprises a memory and a processor. The memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0048] Compared with the prior art, the present invention has at least the following beneficial effects:
[0049] 1) The present invention supports verification of Chisel high-level designs. The converted Scala simulation code expresses the high-level semantics of Chisel circuit design, and can verify the complete Chisel function including the generated circuit semantics.
[0050] 2) The present invention retains Chisel design parameter information. When performing simulation tests, the Scala simulation code can adjust the circuit design parameters at any time and quickly test the functions of the circuit under different parameter configurations; for formal verification methods, parameter verification can be performed to prove the correctness of the Chisel high-level circuit design under different parameters.
[0051] 3) The present invention retains Chisel design structure information. The structure of the Scala simulation code corresponds clearly to the original Chisel circuit design code structure, retains the designer's original functional modules and paragraph divisions, and reflects the design intent; it is conducive to directly analyzing the executable simulation Scala code, and is convenient for using software verification and analysis methods. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 It is a flow chart of the conversion and verification method of Chisel high-level circuit design proposed by the present invention. Specific implementation methods
[0053] In order to make the various technical features and advantages or technical effects in the above technical solutions of the present invention more obvious and easy to understand, they are described in detail below with reference to the accompanying drawings.
[0054] The embodiment of the present invention discloses a conversion and verification method for Chisel high-level circuit design. The method converts and generates a Scala simulation code by analyzing and processing the Chisel high-level circuit design code itself. The converted code can be simulated and tested using different parameter configurations, and can also use formal methods to verify different parameters at the same time. The parameterization information and most of the structural information of the Chisel design can be retained. The problem of inconsistency between the semantics of Chisel hardware design and the semantics of sequential execution software is solved through the mechanism of statement reordering. A Scala simulation library corresponding to the hardware design primitives in Chisel is provided. The method internally uses an abstract syntax tree (AST) that combines the characteristics of Chisel and Scala to conveniently represent circuit design and control information.
[0055] Figure 1 It is a flowchart of the conversion and verification method of Chisel high-level circuit design. The specific steps of this method include:
[0056] Step 1: Analyze the Chisel code content and generate the internal syntax tree structure.
[0057] The required code information is obtained during the Chisel code compilation process in the form of a Scala compilation plug-in. The abstract syntax tree of the Chisel code is analyzed to identify the common Scala code and Chisel hardware design primitives, and the corresponding types of internal syntax tree nodes are generated to form an internal syntax tree.
[0058] Chisel hardware design primitives include but are not limited to: ① Module definition, ② Bundle definition, ③ Wire, Reg, IO, Node definition and use, ④ UInt, SInt, Bool, Vec and other hardware data types and operators on them, ⑥ Use of sub-modules, ⑦ Some components and methods provided by Chisel.
[0059] Step 2: Analyze each statement and statement block on the internal syntax tree and identify the signals and variables involved.
[0060] For each statement on the internal syntax tree, identify the signal updated through the hardware connection as the connection signal, identify the signal used for connection and conditional judgment as the use signal, identify the newly defined or updated Scala variables and functions as the update variable, and identify the call to the defined variable and function as the use variable. For the statement block, the connection signal, use signal, update variable, and use variable involved are the union of the connection signal, use signal, update variable, and use variable corresponding to all sub-statements. Since the value of the register signal is not updated in the current clock, the use of the register is considered to be the use of the current clock value, the connection of the register is considered to be the connection of the next clock value, the connection and use of the register signal are considered to be the connection and use of different signals, and the signal value in different clock cycles is distinguished by the signal name suffix.
[0061] Step 3: Based on the identified signals, variables and internal syntax tree structure, the internal syntax tree is expanded and transformed.
[0062] Traverse the internal syntax tree and perform corresponding expansion or transformation for the following situations.
[0063] Hardware enumeration types are replaced with corresponding literal values: For hardware enumeration types involved in Chisel design, statements using enumeration values are directly replaced with corresponding integer literals.
[0064] Expand register enable statements: expand the statements for register definition and setting enable signals into two statements: register definition and connection assignment when the enable conditions are met.
[0065] Expand submodule definition and call: expand submodule definition into submodule definition, submodule interface signal definition, submodule call, and replace all calls to the original submodule interface with calls to the new submodule interface signal.
[0066] Step 4: For the expanded and transformed internal syntax tree, sort the statements according to the dependency relationship between signals and variables to generate a sorted internal syntax tree.
[0067] To ensure that the variable values of the subsequent Scala simulation code can meet the parallel semantics and last connection effectiveness characteristics of Chisel circuit design, the order of statements needs to be sorted. Sorting is performed on the smallest indivisible statement, and the same internal sorting as the following process is performed inside the indivisible statement block. when-otherwise, switch, if-else, overall connection, and block are separable statements (blocks) that can be split and expanded into multiple statements, each of which contains only one substatement. The function body (including the loop body of the loop) is an indivisible statement block.
[0068] The sorting process includes:
[0069] First, number each minimum statement, and the number of each statement is a sequence of integers. For the top-level statement sequence of the circuit module / indivisible statement block, the statement number only contains a serial number, which is numbered sequentially starting from 1 according to the statement sequence order; for the separable statements and statement blocks, the sub-statement numbers are prefixed with the parent statement number, followed by the sequential number in the statement sequence where the sub-statement is located; for when-otherwise and if-else statements, the branch where the condition is met is added with a number suffix of 1, and the other branch is added with a number suffix of 2; for each branch of the switch statement, number suffixes are added sequentially starting from 1. Finally, statements from the same statement block have the same number prefix and can be used in the subsequent statement merging process.
[0070] Secondly, the minimum statement generates a dependency graph based on the signals and variables involved; in the dependency graph, each node represents each minimum statement, and the directed edges represent the dependencies between statements caused by signals / variables. The dependent statements need to be executed first after sorting. Dependencies include: the connected signal depends on the previous signal connection; the used signal depends on the last connection of the signal to comply with Chisel's last connection effective semantics; all signals and variables are caused by assignment and use. Write-after-read, write-after-write, and read-after-write dependencies. Note that the substatements in when-otherwise, switch, and if-else are considered to use all signals and variables in their branch conditions.
[0071] Again, the dependency graph is topologically sorted according to the dependency relationships between nodes to obtain the topological sequence with the minimum statement number.
[0072] Finally, statements from the same block in the topological sequence are merged according to the number, and the sorted internal syntax tree is generated according to the merged topological sequence number. If two statements have the same number prefix and adjacent topological sequences, they are merged in topological order to generate a parent statement containing two child statements; the merged statements can be further merged according to the number prefix to restore the original statement nesting as much as possible.
[0073] Step 5: Based on the sorted internal syntax tree, the behavior of the Chisel circuit design is simulated through software modeling to generate Scala simulation code.
[0074] The ordering of statements in the previous step ensures that when the Scala code is executed sequentially, the signals used have completely calculated the final stable value within the current clock. The Scala simulation library performs software modeling of Chisel's hardware design primitives, including signal data types and method functions in Chisel, which uses two values, numerical value and bit width, to simulate the fixed bit width bit vector in the circuit, and calculates the bit width according to the bit width derivation rules in Chisel.
[0075] Based on software modeling, the Chisel circuit design behavior is simulated to generate Scala simulation code, including: the calculation between circuit signals in Chisel is simulated through corresponding Scala types and operators; the connection between signals is simulated through Scala variable assignment; Chisel circuit module is represented by a Scala class, and the construction parameters are the original Chisel circuit parameters; the combinational circuit calculation within one clock of the circuit is represented by a migration function, the function parameters are the circuit input signal and the register value of the previous clock, and the function return value is the circuit output signal and the new register value; the register is represented by two variables, the current value and the next clock value; the submodule is called as a function.
[0076] Step 6: Use the generated Scala simulation code for simulation testing and formal verification.
[0077] The converted Scala simulation code can be subjected to high-level simulation testing and formal verification. The simulation test can call the corresponding functions and types in the Scala simulation code, pass the parameters required for verification and the test case input signal to the circuit, repeatedly call the migration function to simulate the circuit clock by clock, and finally compare whether the output signal of the circuit meets the requirements. Formal verification can use tools such as Stainless that perform formal verification on Scala, insert the properties required for verification into the Scala simulation code, supplement the lemmas required for verification, and prove the correctness of the circuit under different parameters.
[0078] In summary, compared with other Chisel verification methods, the method of the present invention can retain the parameters and structural information of Chisel high-level circuit design, and more fully verify the Chisel circuit design. The testing and formal verification methods of software on Scala can be used to uniformly verify the functions of Chisel circuit design under different parameters. The Scala simulation code is closer to the Chisel circuit design code structure, which is convenient for verifiers to analyze the code.
[0079] The following is an example of the verification process of a divider circuit.
[0080] The divider inputs two numbers as the dividend and divisor, outputs the quotient and remainder, and supports parameter control of the bit width of the calculated value. The principle of the divider is similar to the binary vertical division. The number of calculation cycles required varies depending on the highest bit position of the dividend and divisor. The verification process of the divider is as follows:
[0081] 1. On the basis that the divider Chisel circuit can be compiled and run normally, the present invention is added as a compilation plug-in.
[0082] 2. Execute the compilation process, the present invention generates Scala simulation code, which can be verified with the support of the Scala simulation library.
[0083] 3. Test the Scala simulation code. Set different bit widths and generate random test cases, pass them in through the Scala simulation code interface and execute them, and stop after getting the calculation result. The Scala simulation circuit can correctly calculate the division result, indicating that the original Chisel design passes the test.
[0084] 4. Formal verification of the Scala simulation code. Add properties to require that the circuit calculation results comply with the division rules, add the required lemmas, and use Stainless to prove the Scala simulation code. The proof is passed, indicating that the original Chisel design can obtain correct division results under all bit width parameters.
[0085] Although the present invention has been disclosed as above by way of embodiments, it is not intended to limit the present invention. Appropriate modifications or equivalent substitutions of the technical solutions of the present invention made by ordinary technicians in the field should all be included in the protection scope of the present invention. The protection scope of the present invention shall be based on what is defined in the claims.
Claims
1. A method for converting and verifying Chisel high-level circuit design, characterized in that: The following steps are involved: 1) Analyze the Chisel code content and generate the internal syntax tree structure; 2) Analyze each statement and statement block on the internal syntax tree and identify the signals and variables involved; 3) Expand and transform the internal syntax tree based on the identified signals, variables and internal syntax tree structure; The expansion and transformation of the internal syntax tree specifically includes: replacing the statements using enumeration values of the hardware enumeration type with the corresponding integer literal values; expanding the statements for register definition and setting the enable signal into two statements: register definition and connection assignment when the enable condition is met; expanding the submodule definition into submodule definition, submodule interface signal definition and submodule call, and replacing all calls to the original submodule interface with calls to the new submodule interface signal; 4) Sorting the expanded and transformed internal syntax tree according to the dependency relationship between signals and variables to generate a sorted internal syntax tree; the step of sorting the statements according to the dependency relationship between signals and variables includes: taking the indivisible statements as the minimum statements, and splitting the divisible statements or statement blocks into indivisible minimum statements; numbering each minimum statement, where the number is an integer sequence; generating a dependency graph according to the dependency relationship between signals and variables between the minimum statements; topologically sorting the dependency graph, that is, sorting according to the dependency progress between nodes to obtain a numbered topological sequence; merging the statements from the same statement block in the topological sequence according to the number, and generating a sorted internal syntax tree according to the number of the merged topological sequence; wherein if the number prefixes of two statements are the same and the topological sequences are adjacent, then they are merged in topological order to generate a parent statement containing two child statements, and the merged statements are further merged according to the number prefix; 5) According to the sorted internal syntax tree, the behavior of Chisel circuit design is simulated by software modeling to generate Scala simulation code; the steps of simulating the behavior of Chisel circuit design include: using Scala types and operators to simulate the calculation between circuit signals in Chisel; simulating the connection between circuit signals in Chisel by assigning Scala variables; using Scala classes to represent Chisel circuit modules, and the construction parameters are the original Chisel circuit parameters; using a migration function to represent the combinational circuit calculation within one clock of the circuit, the function parameters are the circuit input signal and the register value of the previous clock, and the function return value is the circuit output signal and the new register value; for registers, two variables, the current clock value and the next clock value, are used to represent; the submodule is called as a function; 6) Use the generated Scala simulation code for simulation testing and formal verification.
2. The method according to claim 1, characterized in that In step 1), the Chisel code content is analyzed, ordinary Scala code and Chisel hardware design primitives are identified, and corresponding two types of internal syntax tree nodes are generated; Chisel hardware design primitives include the definition of Module, the definition of Bundle, the definition and use of Wire, Reg, IO, Node, UInt, SInt, Bool, Vec hardware data types and their operators, the use of submodules, and the components and methods provided by Chisel.
3. The method according to claim 1, characterized in that In step 2), for each statement, the signal updated through the hardware connection is identified as the connection signal, the signal used for the connection and condition judgment is identified as the usage signal, the newly defined or updated Scala variables and functions are identified as the update variables, and the call to the defined variables and functions is identified as the usage variable; for each statement block, its connection signal is the union of the connection signals of all sub-statements, its usage signal is the union of the usage signals of all sub-statements, its update variable is the union of the update variables of all sub-statements, and its usage variable is the union of the usage variables of all sub-statements.
4. The method according to claim 3, characterized in that In step 2), the rules to be followed for register connection signals and signal identification include: The use of registers is regarded as the use of their current clock cycle values. When analyzing register signals, any read operation on registers is based on the current values. The connection to the register is regarded as the connection to the value of the next clock cycle. The connection operation performed in the current clock cycle takes effect in the next clock cycle. The signal name suffix is used to distinguish signal values in different clock cycles.
5. The method according to claim 1, characterized in that The steps to number each minimal statement include: For circuit modules or inseparable statement blocks, the top-level statements have only one serial number, starting from 1. For separable statements and statement blocks, the sub-statement number is prefixed by the parent statement number, followed by the sub-statement's sequence number in the statement sequence; For when-otherwise and if-else statements, add the suffix 1 to the branch where the condition is met, and add the suffix 2 to the branch where the condition is not met; For each branch of the switch statement, add a numbered suffix in sequence starting at 1.
6. The method according to claim 1, characterized in that In the dependency graph, each node represents each minimum statement, and the directed edges represent the dependencies between statements caused by signals or variables. The dependent statements are executed first after sorting; Dependencies include: the connected signal depends on the previous signal connection, the used signal depends on the last connection of the signal, the write-after-read, write-after-write, and read-after-write dependencies between all signals and variables caused by assignment and use, and the dependency on branch condition signals and variables on the path.
7. A conversion and verification device for Chisel high-level circuit design, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method according to any one of claims 1 to 6 when executing the computer program.
