Skip to content

Commit 4250ac6

Browse files
committed
[ChiselSim] Better file include in LayerControl
Provide initial work that better handles the problem of needing to filter files in order to enable specific layers. This adds a new member function, `shouldIncludeFile` which returns a `PartialFunction` that is defined only if the file being examined is a layer file (as defined in the FIRRTL ABI for extract layers) and returns true if that layer file should be included in order to enable the requested layers. In subsequent commits, this will be made load bearing. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent bc063c0 commit 4250ac6

File tree

2 files changed

+86
-22
lines changed

2 files changed

+86
-22
lines changed

src/main/scala-2/chisel3/simulator/LayerControl.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ object LayerControl {
5454
case _ => None
5555
}
5656
}
57+
58+
/** Return a partial function that will return true if a file should be included
59+
* in the build to enable a layer. This partial function is not defined if
60+
* the file is not a layer file.
61+
*
62+
* @param module an elaborated module
63+
* @return a partial function to test if layer files should be included
64+
*/
65+
final def shouldIncludeFile(module: ElaboratedModule[_ <: RawModule]): PartialFunction[File, Boolean] = {
66+
val layerFilenames: Seq[String] = getLayerSubset(module).flatMap {
67+
case layer =>
68+
layer.config.abi match {
69+
case abi: chisel3.layer.ABI.FileInclude.type =>
70+
Some(abi.toFilename(layer, module.wrapped.circuitName))
71+
case _ => None
72+
}
73+
}
74+
75+
{
76+
case a if a.getName().startsWith("layers-") =>
77+
layerFilenames.mkString("|").r.matches(a.getName())
78+
}
79+
}
80+
5781
}
5882

5983
/** Enable all layers */

src/test/scala/chiselTests/simulator/LayerControlSpec.scala

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,82 @@ package chiselTests.simulator
55
import java.io.File
66
import org.scalatest.funspec.AnyFunSpec
77
import org.scalatest.matchers.should.Matchers
8-
import chisel3.layer.{Layer, LayerConfig}
9-
import chisel3.simulator.LayerControl
8+
import chisel3.RawModule
9+
import chisel3.layer.{addLayer, Layer, LayerConfig}
10+
import chisel3.simulator.{ChiselWorkspace, LayerControl}
11+
import svsim.Workspace
1012

1113
class LayerControlSpec extends AnyFunSpec with Matchers {
14+
15+
object A extends Layer(LayerConfig.Extract())
16+
object B extends Layer(LayerConfig.Extract()) {
17+
object C extends Layer(LayerConfig.Extract())
18+
}
19+
class Foo extends RawModule {
20+
Seq(A, B, B.C).foreach(addLayer)
21+
}
22+
23+
val workspace = new Workspace(path = "test_run_dir/LayerControlSpec")
24+
workspace.reset()
25+
val elaboratedModule = workspace.elaborateGeneratedModule({ () => new Foo })
26+
1227
describe("LayerControl.EnableAll") {
13-
it("should always filter to true") {
28+
it("should include all layer files") {
1429
val layerControl = LayerControl.EnableAll
15-
layerControl.filter(new File("foo")) should be(true)
16-
layerControl.filter(new File("layers-foo-bar.sv")) should be(true)
30+
31+
info("non-layer files are ignored")
32+
layerControl.shouldIncludeFile(elaboratedModule).isDefinedAt(new File("Foo.sv")) should be(false)
33+
34+
Seq("layers-Foo-A.sv", "layers-Foo-B.sv", "layers-Foo-B-C.sv").map(new File(_)).foreach {
35+
case filename =>
36+
info(s"$filename is included")
37+
layerControl.shouldIncludeFile(elaboratedModule)(filename) should be(true)
38+
}
1739
}
1840
}
1941
describe("LayerControl.Enable()") {
20-
it("should return true for non-layers and false for layers") {
42+
it("should include no layer files") {
2143
val layerControl = LayerControl.Enable()
22-
layerControl.filter(new File("foo")) should be(true)
23-
layerControl.filter(new File("layers-foo-bar.sv")) should be(false)
44+
45+
info("non-layer files are ignored")
46+
layerControl.shouldIncludeFile(elaboratedModule).isDefinedAt(new File("Foo.sv")) should be(false)
47+
48+
Seq("layers-Foo-A.sv", "layers-Foo-B.sv", "layers-Foo-B-C.sv").map(new File(_)).foreach {
49+
case filename =>
50+
info(s"$filename is excluded")
51+
layerControl.shouldIncludeFile(elaboratedModule)(filename) should be(false)
52+
}
2453
}
2554
}
2655
describe("LayerControl.DisableAll") {
27-
it("should return true for non-layers and false for layers") {
28-
LayerControl.DisableAll.filter(new File("foo")) should be(true)
29-
LayerControl.DisableAll.filter(new File("layers-foo-bar.sv")) should be(false)
56+
it("should include no layer files") {
57+
val layerControl = LayerControl.Enable()
58+
59+
info("non-layer files are ignored")
60+
layerControl.shouldIncludeFile(elaboratedModule).isDefinedAt(new File("Foo.sv")) should be(false)
61+
62+
Seq("layers-Foo-A.sv", "layers-Foo-B.sv", "layers-Foo-B-C.sv").map(new File(_)).foreach {
63+
case filename =>
64+
info(s"$filename is excluded")
65+
layerControl.shouldIncludeFile(elaboratedModule)(filename) should be(false)
66+
}
3067
}
3168
}
32-
describe("LayerControl.Enable") {
33-
it("should return true for non-layers and filter layers properly") {
34-
object A extends Layer(LayerConfig.Extract())
35-
object B extends Layer(LayerConfig.Extract()) {
36-
object C extends Layer(LayerConfig.Extract())
37-
}
69+
describe("LayerControl.Enable(A, B.C)") {
70+
it("should include only specified layers") {
3871
val layerControl = LayerControl.Enable(A, B.C)
39-
layerControl.filter(new File("foo")) should be(true)
40-
layerControl.filter(new File("layers-foo.sv")) should be(false)
41-
layerControl.filter(new File("layers-foo-A.sv")) should be(true)
42-
layerControl.filter(new File("layers-foo-A-B.sv")) should be(false)
43-
layerControl.filter(new File("layers-foo-B-C.sv")) should be(true)
72+
73+
info("non-layer files are ignored")
74+
layerControl.shouldIncludeFile(elaboratedModule).isDefinedAt(new File("foo")) should be(false)
75+
76+
Seq("layers-Foo-A.sv", "layers-Foo-B-C.sv").map(new File(_)).foreach {
77+
case filename =>
78+
info(s"$filename is included")
79+
layerControl.shouldIncludeFile(elaboratedModule)(filename) should be(true)
80+
}
81+
82+
info("layers-Foo-A-B.sv is excluded")
83+
layerControl.shouldIncludeFile(elaboratedModule)(new File("layers-Foo-A-B.sv")) should be(false)
4484
}
4585
}
4686
}

0 commit comments

Comments
 (0)