A faster way to sort HDL files in HDLRegression
Marius Elveg?rd
Disiplinleder Digital ?stlandet, Lokasjonsleder Asker, Senior FPGA-utvikler hos Inventas, UVVM SG
HDLRegression has for a long time relied on a bubble sort-like routine to arrange HDL files in compile order. This approach simply swapped files around until it found a sequence that satisfied all dependencies. We are continuously working on improvements of this tool and some of these improvements are to accelerate compile order and more reliably detect any dependency loops.
The old approach
Originally, the code would scan the list of files multiple times, trying to “bubble” each file into its correct position based on its dependencies. If file A depends on file B, A must come after B. The sorting loop would swap files until the order looked right. The bubble sort algorithm is simple, but has some downsides:
Why we need a better method
For large designs the bubble-sort-like approach can became slow; each pass checked dependencies between pairs of files, leading to a worst-case time complexity similar to O(n2). Another challenge is the detection of circular dependencies—situations where files end up depending on each other, blocking a correct compile order.
Kahn’s Algorithm
There are several sorting algorithms that can be used for this problem, and one of these is the Kahn’s algorithm, which is a classic method for topological sorting of Directed Acyclic Graphs (DAGs). In the context of HDLRegression, we treat each HDL file as a node in a graph. If file A depends on file B, we draw a directed edge from B to A, meaning “B must come before A.”
Overview of Kahn’s Algorithm
A somewhat simplified description of how the algorithm is used in HDLRegression:
The figure below illustrates a small DAG example for Kahn's algorithm with three nodes and their edges indicating dependency directions. In this example item B has to come before item A, and item A has to come before item C. The topologic sorting is then B, A, C.
领英推荐
Adaptations in HDLRegression
While Kahn’s algorithm is straightforward, HDLRegression adds two small adjustments to prevent false circular errors and make the algorithm more efficient for typical HDL projects, where multiple design units can reside in the same file:
The figure below shows a possible scenario for an ignore same-file dependency. Here HDLRegression will create 3 VHDL module objects, an Entity, an Architecture and a Package, where all files are written in the same file. Normally the package, if used in the architecture, would have to be placed before the architecture in the topologic sorting, and the entity after the architecture. Because all of these module objects belong to the same file, the order does not matter for the compile order.
Switching from bubble-sort to Kahn’s algorithm has two major benefits:
Conclusion
By implementing Kahn’s algorithm, HDLRegression can now handle hundreds or even thousands of HDL files with complex dependency graphs even faster. The algorithm accelerates compilation order resolution and provides clear error messages when genuine dependency loops occur.
Read more about HDLRegression here and download from Github.
Inventas #FPGA #ASIC #HDLRegression #UVVM #Embedded #Methodology