Skip to content

Commit

Permalink
[build system] add panama circt converter as an optional elaboration …
Browse files Browse the repository at this point in the history
…flow
  • Loading branch information
SpriteOvO committed Mar 29, 2024
1 parent c6d6203 commit 925e01e
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 31 deletions.
15 changes: 15 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,28 @@ trait Subsystem
def chiselIvy = None
}

object panamaconverter extends PanamaConverter

trait PanamaConverter
extends millbuild.dependencies.chisel.build.PanamaConverter {
def crossValue = v.scala

override def millSourcePath = os.pwd / "dependencies" / "chisel" / "panamaconverter"

def scalaVersion = T(v.scala)
}

// Module to generate RTL from json config
object elaborator extends Elaborator

trait Elaborator
extends millbuild.common.ElaboratorModule {
def scalaVersion = T(v.scala)

def panamaconverterModule = panamaconverter

def circtInstallPath = T.input(PathRef(os.Path(T.ctx().env("CIRCT_INSTALL_PATH"))))

def generators = Seq(
t1,
ipemu,
Expand Down
8 changes: 7 additions & 1 deletion common.sc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ trait ElaboratorModule
extends ScalaModule
with HasChisel {
def generators: Seq[ScalaModule]
override def moduleDeps = super.moduleDeps ++ generators
def panamaconverterModule: ScalaModule
def circtInstallPath: T[PathRef]
override def moduleDeps = super.moduleDeps ++ Seq(panamaconverterModule) ++ generators
def mainargsIvy: Dep
override def ivyDeps = T(super.ivyDeps() ++ Seq(mainargsIvy))
override def javacOptions = T(super.javacOptions() ++ Seq("--enable-preview", "--release", "21"))
override def forkArgs: T[Seq[String]] = T(
super.forkArgs() ++ Seq("--enable-native-access=ALL-UNNAMED", "--enable-preview", s"-Djava.library.path=${ circtInstallPath().path / "lib"}")
)
}
33 changes: 26 additions & 7 deletions elaborator/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package org.chipsalliance.t1.elaborator

import mainargs._
import org.chipsalliance.t1.rtl.T1Parameter
import chisel3.panamalib.option._

object Main {
implicit object PathRead extends TokensReader.Simple[os.Path] {
Expand All @@ -14,22 +15,33 @@ object Main {

@main
case class ElaborateConfig(
@arg(name = "target-dir", short = 't') targetDir: os.Path) {
@arg(name = "target-dir", short = 't') targetDir: os.Path,
@arg(name = "binder-mlirbc-out") binderMlirbcOut: Option[String] = None) {
def elaborate(gen: () => chisel3.RawModule): Unit = {
var topName: String = null
var fir: firrtl.ir.Circuit = null
var panamaCIRCTConverter: chisel3.panamaconverter.PanamaCIRCTConverter = null

val annos = Seq(
new chisel3.stage.phases.Elaborate,
new chisel3.stage.phases.Convert
if (binderMlirbcOut.isEmpty) new chisel3.stage.phases.Convert else chisel3.panamaconverter.stage.Convert
).foldLeft(
Seq(
chisel3.stage.ChiselGeneratorAnnotation(gen)
chisel3.stage.ChiselGeneratorAnnotation(gen),
chisel3.panamaconverter.stage.FirtoolOptionsAnnotation(FirtoolOptions(Set(
BuildMode(BuildModeDebug),
PreserveValues(PreserveValuesModeNamed),
DisableUnknownAnnotations(true)
))),
): firrtl.AnnotationSeq
) { case (annos, stage) => stage.transform(annos) }
.flatMap {
case firrtl.stage.FirrtlCircuitAnnotation(circuit) =>
topName = circuit.main
os.write(targetDir / s"$topName.fir", circuit.serialize)
if (binderMlirbcOut.isEmpty) fir = circuit
None
case chisel3.panamaconverter.stage.PanamaCIRCTConverterAnnotation(converter) =>
if (binderMlirbcOut.nonEmpty) panamaCIRCTConverter = converter
None
case _: chisel3.panamaconverter.stage.FirtoolOptionsAnnotation => None
case _: chisel3.stage.DesignAnnotation[_] => None
case _: chisel3.stage.ChiselCircuitAnnotation => None
case _: freechips.rocketchip.util.ParamsAnnotation => None
Expand All @@ -39,7 +51,14 @@ object Main {
case _: freechips.rocketchip.util.SRAMAnnotation => None
case a => Some(a)
}
os.write(targetDir / s"$topName.anno.json", firrtl.annotations.JsonProtocol.serialize(annos))

binderMlirbcOut match {
case Some(outFile) =>
os.write(targetDir / s"$outFile.mlirbc", panamaCIRCTConverter.mlirBytecodeStream)
case None =>
os.write(targetDir / s"${fir.main}.fir", fir.serialize)
os.write(targetDir / s"${fir.main}.anno.json", firrtl.annotations.JsonProtocol.serialize(annos))
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in
libspike = final.callPackage ./pkgs/libspike.nix { };
buddy-mlir = final.callPackage ./pkgs/buddy-mlir.nix { };
fetchMillDeps = final.callPackage ./pkgs/mill-builder.nix { };
circt-full = final.callPackage ./pkgs/circt-full.nix { };

mill = let jre = final.jdk21; in
(prev.mill.override { inherit jre; }).overrideAttrs (_: {
Expand Down
15 changes: 15 additions & 0 deletions nix/pkgs/circt-full.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ symlinkJoin, circt }:
symlinkJoin {
name = "circt-full";
paths = [
circt
circt.lib
circt.dev

circt.llvm
circt.llvm.lib
circt.llvm.dev
];

inherit (circt) meta;
}
8 changes: 4 additions & 4 deletions nix/t1/_sources/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"chisel": {
"cargoLocks": null,
"date": "2024-02-26",
"date": "2024-03-27",
"extract": null,
"name": "chisel",
"passthru": null,
Expand All @@ -73,11 +73,11 @@
"name": null,
"owner": "chipsalliance",
"repo": "chisel",
"rev": "344bdd636a36e6c3933cac527c61b5671d271534",
"sha256": "sha256-SZNs0AeAQtenGatRbUftxAZOLI2G7KgEbAeW0ciAl0c=",
"rev": "9177535ff1be47ffd99034bf0154c0f1ec637419",
"sha256": "sha256-smqNuOnmz+MeVGyS7mdIzegniQ/6EJH4CFqK4JntvrI=",
"type": "github"
},
"version": "344bdd636a36e6c3933cac527c61b5671d271534"
"version": "9177535ff1be47ffd99034bf0154c0f1ec637419"
},
"diplomacy": {
"cargoLocks": null,
Expand Down
8 changes: 4 additions & 4 deletions nix/t1/_sources/generated.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
};
chisel = {
pname = "chisel";
version = "344bdd636a36e6c3933cac527c61b5671d271534";
version = "9177535ff1be47ffd99034bf0154c0f1ec637419";
src = fetchFromGitHub {
owner = "chipsalliance";
repo = "chisel";
rev = "344bdd636a36e6c3933cac527c61b5671d271534";
rev = "9177535ff1be47ffd99034bf0154c0f1ec637419";
fetchSubmodules = false;
sha256 = "sha256-SZNs0AeAQtenGatRbUftxAZOLI2G7KgEbAeW0ciAl0c=";
sha256 = "sha256-smqNuOnmz+MeVGyS7mdIzegniQ/6EJH4CFqK4JntvrI=";
};
date = "2024-02-26";
date = "2024-03-27";
};
diplomacy = {
pname = "diplomacy";
Expand Down
6 changes: 3 additions & 3 deletions nix/t1/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ lib.makeScope newScope
ip = {
recurseForDerivations = true;

mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "ip"; };
mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "ip"; /* use-binder = true; */ };
rtl = innerSelf.callPackage ./rtl.nix { mlirbc = innerSelf.ip.mlirbc; };

emu-mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "ipemu"; };
emu-mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "ipemu"; /* use-binder = true; */ };
emu-rtl = innerSelf.callPackage ./rtl.nix { mlirbc = innerSelf.ip.emu-mlirbc; };

emu = innerSelf.callPackage ./ipemu.nix { rtl = innerSelf.ip.emu-rtl; stdenv = moldStdenv; };
Expand All @@ -78,7 +78,7 @@ lib.makeScope newScope
subsystem = {
recurseForDerivations = true;

mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "subsystem"; };
mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "subsystem"; use-binder = true; };
rtl = innerSelf.callPackage ./rtl.nix { mlirbc = innerSelf.subsystem.mlirbc; };
};
})
Expand Down
9 changes: 6 additions & 3 deletions nix/t1/mlirbc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
, elaborator
, configName
, target
, use-binder ? false
}:

assert lib.assertMsg
(lib.elem target [ "ip" "ipemu" "subsystem" ])
"Unknown elaborate target ${target}";

let
elaborateArgs = [
elaborateArgs = lib.filter (s: s != "") [
"--ip-config"
# Can't use `toString` here, or due to some shell escape issue, Java nio cannot find the path
"${elaborateConfigJson}"
"--target-dir"
"elaborate"
(if use-binder then (placeholder "out") else "elaborate")
(lib.optionalString (use-binder) "--binder-mlirbc-out")
(lib.optionalString (use-binder) "${target}-${configName}")
];
in
stdenvNoCC.mkDerivation {
Expand All @@ -37,7 +40,7 @@ stdenvNoCC.mkDerivation {
mkdir -p elaborate $out
${elaborator}/bin/elaborator ${target} ${lib.escapeShellArgs elaborateArgs}
'' + lib.optionalString (!use-binder) ''
firtool elaborate/*.fir \
--annotation-file elaborate/*.anno.json \
-O=debug \
Expand Down
12 changes: 9 additions & 3 deletions nix/t1/t1.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
, stdenv
, fetchMillDeps
, makeWrapper
, jre
, jdk21

# chisel deps
, mill
, espresso
, circt-full
, jextract
, strip-nondeterminism

, submodules
Expand Down Expand Up @@ -57,6 +59,8 @@ let

nativeBuildInputs = [
mill
circt-full
jextract
strip-nondeterminism

makeWrapper
Expand All @@ -65,6 +69,8 @@ let
submodules.setupHook
];

env.CIRCT_INSTALL_PATH = circt-full;

outputs = [ "out" "configgen" "elaborator" ];

buildPhase = ''
Expand All @@ -81,8 +87,8 @@ let
mv out/elaborator/assembly.dest/out.jar $out/share/java/elaborator.jar
mkdir -p $configgen/bin $elaborator/bin
makeWrapper ${jre}/bin/java $configgen/bin/configgen --add-flags "-jar $out/share/java/configgen.jar"
makeWrapper ${jre}/bin/java $elaborator/bin/elaborator --add-flags "-jar $out/share/java/elaborator.jar"
makeWrapper ${jdk21}/bin/java $configgen/bin/configgen --add-flags "-jar $out/share/java/configgen.jar"
makeWrapper ${jdk21}/bin/java $elaborator/bin/elaborator --add-flags "--enable-preview -Djava.library.path=${circt-full}/lib -jar $out/share/java/elaborator.jar"
'';
};
in
Expand Down

0 comments on commit 925e01e

Please sign in to comment.