Skip to content

Commit

Permalink
[om] add omreader and omreaderlib
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Apr 30, 2024
1 parent 4d00977 commit 93be2ca
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 9 deletions.
36 changes: 36 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,42 @@ trait Elaborator
def chiselIvy = None
}

object omreaderlib extends OMReaderLib

trait OMReaderLib
extends millbuild.common.OMReaderLibModule {
def scalaVersion = T(v.scala)

def panamaconverterModule = panamaconverter

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

def mainargsIvy = v.mainargs

def chiselModule = Some(chisel)
def chiselPluginJar = T(Some(chisel.pluginModule.jar()))
def chiselPluginIvy = None
def chiselIvy = None
}

object omreader extends OMReader

trait OMReader
extends millbuild.common.OMReaderModule {
def scalaVersion = T(v.scala)

def panamaconverterModule = panamaconverter
def omreaderlibModule = omreaderlib

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

def mainargsIvy = v.mainargs

def chiselModule = Some(chisel)
def chiselPluginJar = T(Some(chisel.pluginModule.jar()))
def chiselPluginIvy = None
def chiselIvy = None
}

/** A simple release flow for T1 generator:
* package required dependency to flat jar.
Expand Down
29 changes: 29 additions & 0 deletions common.sc
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,32 @@ trait ElaboratorModule
super.forkArgs() ++ Seq("--enable-native-access=ALL-UNNAMED", "--enable-preview", s"-Djava.library.path=${ circtInstallPath().path / "lib"}")
)
}

trait OMReaderLibModule
extends ScalaModule
with HasChisel {
def panamaconverterModule: ScalaModule
def circtInstallPath: T[PathRef]
override def moduleDeps = super.moduleDeps ++ Seq(panamaconverterModule)
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"}")
)
}

trait OMReaderModule
extends ScalaModule
with HasChisel {
def panamaconverterModule: ScalaModule
def omreaderlibModule: ScalaModule
def circtInstallPath: T[PathRef]
override def moduleDeps = super.moduleDeps ++ Seq(panamaconverterModule, omreaderlibModule)
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"}")
)
}
6 changes: 6 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ in
find ./test -name '*.mlir' -exec sed -i 's|/usr/bin/env|${final.coreutils}/bin/env|g' {} \;
substituteInPlace cmake/modules/GenVersionFile.cmake --replace "unknown git version" "nightly"
'';
patches = [
(prev.fetchpatch {
url = "https://github.com/llvm/circt/pull/6944.patch";
sha256 = "sha256-+sW/IGgOYtgHqqQ/qsaEG0/iA8fg267oSfR9FzPnbCg=";
})
];
});
espresso = final.callPackage ./pkgs/espresso.nix { };
dramsim3 = final.callPackage ./pkgs/dramsim3.nix { };
Expand Down
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-04-20",
"date": "2024-04-30",
"extract": null,
"name": "chisel",
"passthru": null,
Expand All @@ -73,11 +73,11 @@
"name": null,
"owner": "chipsalliance",
"repo": "chisel",
"rev": "30ee35d7a4c3fd3d86b22a53ab60d6025a75c98e",
"sha256": "sha256-OXvENkPlkXvjDPsNNoBNSRSLVHNrHlm6+EqHqGyDRuQ=",
"rev": "17398c1657705a0733a1768f0e84672c39962327",
"sha256": "sha256-mSUWcORyStuQ6LzonVqkCmTS/hKQ0cn2tqUEhQm/+Ag=",
"type": "github"
},
"version": "30ee35d7a4c3fd3d86b22a53ab60d6025a75c98e"
"version": "17398c1657705a0733a1768f0e84672c39962327"
},
"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 = "30ee35d7a4c3fd3d86b22a53ab60d6025a75c98e";
version = "17398c1657705a0733a1768f0e84672c39962327";
src = fetchFromGitHub {
owner = "chipsalliance";
repo = "chisel";
rev = "30ee35d7a4c3fd3d86b22a53ab60d6025a75c98e";
rev = "17398c1657705a0733a1768f0e84672c39962327";
fetchSubmodules = false;
sha256 = "sha256-OXvENkPlkXvjDPsNNoBNSRSLVHNrHlm6+EqHqGyDRuQ=";
sha256 = "sha256-mSUWcORyStuQ6LzonVqkCmTS/hKQ0cn2tqUEhQm/+Ag=";
};
date = "2024-04-20";
date = "2024-04-30";
};
diplomacy = {
pname = "diplomacy";
Expand Down
2 changes: 2 additions & 0 deletions nix/t1/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lib.makeScope newScope
configgen = _millOutput.configgen // { meta.mainProgram = "configgen"; };
t1package = _millOutput.t1package;

omreader = self.callPackage ./omreader.nix { };
submodules = self.callPackage ./submodules.nix { };

riscv-opcodes-src = self.submodules.sources.riscv-opcodes.src;
Expand Down Expand Up @@ -70,6 +71,7 @@ lib.makeScope newScope

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

emu-mlirbc = innerSelf.callPackage ./mlirbc.nix { target = "ipemu"; /* use-binder = true; */ };
emu-rtl = innerSelf.callPackage ./rtl.nix { mlirbc = innerSelf.ip.emu-mlirbc; };
Expand Down
2 changes: 1 addition & 1 deletion nix/t1/nvfetcher.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fetch.github = "chipsalliance/rocket-chip-inclusive-cache"

[chisel]
src.git = "https://github.com/chipsalliance/chisel"
src.branch = "instantiate-class"
src.branch = "main"
fetch.github = "chipsalliance/chisel"

[rvdecoderdb]
Expand Down
25 changes: 25 additions & 0 deletions nix/t1/om.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{ stdenvNoCC
, lib

, omreader
, circt
, mlirbc
}:

let
omReaderArgs = lib.filter (s: s != "") [
"--mlirbc-file"
"${mlirbc}/${mlirbc.elaborateTarget}-${mlirbc.elaborateConfig}.mlirbc"
];
in
stdenvNoCC.mkDerivation {
name = "t1-${mlirbc.elaborateConfig}-${mlirbc.elaborateTarget}-om";

nativeBuildInputs = [ circt omreader ];

buildCommand = ''
omreader ${lib.escapeShellArgs omReaderArgs}
'';

meta.description = "TODO.";
}
89 changes: 89 additions & 0 deletions nix/t1/omreader.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{ lib
, stdenv
, fetchMillDeps
, makeWrapper
, jdk21

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

, submodules
}:

let
self = stdenv.mkDerivation rec {
name = "omreader";

src = with lib.fileset; toSource {
root = ./../..;
fileset = unions [
./../../build.sc
./../../common.sc
./../../omreader
./../../omreaderlib
];
};

passthru.millDeps = fetchMillDeps {
inherit name;
src = with lib.fileset; toSource {
root = ./../..;
fileset = unions [
./../../build.sc
./../../common.sc
];
};
millDepsHash = "sha256-rkS/bTDnjnyzdQyTIhfLj3e0mMdDn4fzv/660rO3qYg=";
nativeBuildInputs = [ submodules.setupHook ];
};

passthru.editable = self.overrideAttrs (_: {
shellHook = ''
setupSubmodulesEditable
mill mill.bsp.BSP/install 0
'';
});

shellHook = ''
setupSubmodules
'';

nativeBuildInputs = [
mill
jextract
strip-nondeterminism
circt-full

makeWrapper
passthru.millDeps.setupHook

submodules.setupHook
];

env = {
CIRCT_INSTALL_PATH = circt-full;
JAVA_TOOL_OPTIONS = "--enable-preview";
};

buildPhase = ''
mill -i 'omreader.assembly'
'';

installPhase = ''
mkdir -p "$out"/bin "$out"/share/java
strip-nondeterminism out/omreader/assembly.dest/out.jar
mv out/omreader/assembly.dest/out.jar "$out"/share/java/omreader.jar
makeWrapper ${jdk21}/bin/java "$out"/bin/omreader --add-flags "--enable-preview --enable-native-access=ALL-UNNAMED -Djava.library.path=${circt-full}/lib -jar $out/share/java/omreader.jar"
'';

meta = {
description = "omreader";
mainProgram = "omreader";
};
};
in
self
56 changes: 56 additions & 0 deletions omreader/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2022 Jiuyang Liu <[email protected]>

package org.chipsalliance.t1.omreader

import java.io.BufferedInputStream
import mainargs._
import chisel3.panamaom._
import org.chipsalliance.t1.omreaderlib._

object Main {
implicit object PathRead extends TokensReader.Simple[os.Path] {
def shortName = "path"
def read(strs: Seq[String]): Either[String, os.Path] = Right(os.Path(strs.head, os.pwd))
}

@main
def run(
@arg(name = "mlirbc-file") mlirbcFile: Option[os.Path],
@arg(name = "class-name") className: String,
@arg(name = "dump-methods") dumpMethods: Flag,
@arg(name = "eval") eval: Option[String],
) = {
val omReader = mlirbcFile match {
case Some(path) => OMReader.fromFile(path, className)
case None =>
val stdin = new BufferedInputStream(System.in)
val bytes = Stream.continually(stdin.read).takeWhile(_ != -1).map(_.toByte).toArray
OMReader.fromBytes(bytes, className)
}
val t1Reader = omReader.t1Reader

if (eval.nonEmpty) {
println(SimpleInputEval(omReader.entryUnderlying, eval.get))
} else if (dumpMethods.value) {
t1Reader.dumpMethods()
} else {
t1Reader.dumpAll()
}
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}

object SimpleInputEval {
def apply(entry: PanamaCIRCTOMEvaluatorValue, input: String): PanamaCIRCTOMEvaluatorValue = {
input.split("\\.").foldLeft(entry) {
case (obj, field) =>
if (field.forall(_.isDigit)) {
obj.asInstanceOf[PanamaCIRCTOMEvaluatorValueList].getElement(field.toLong)
} else {
obj.asInstanceOf[PanamaCIRCTOMEvaluatorValueObject].field(field)
}
}
}
}
61 changes: 61 additions & 0 deletions omreaderlib/src/Interface.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.chipsalliance.t1.omreaderlib

import scala.reflect.runtime.universe._
import chisel3.panamalib.option._
import chisel3.panamaom._
import chisel3.panamaconverter.PanamaCIRCTConverter

object OMReader {
def fromFile(mlirbcFile: os.Path, className: String): OMReader = {
new OMReader(os.read.bytes(mlirbcFile), className)
}

def fromBytes(mlirbc: Array[Byte], className: String): OMReader = {
new OMReader(mlirbc, className)
}
}

class OMReader private(mlirbc: Array[Byte], className: String) {
private val cvt = PanamaCIRCTConverter.newWithMlirBc(mlirbc)

private val pm = cvt.passManager()
assert(pm.populatePreprocessTransforms())
assert(pm.populateCHIRRTLToLowFIRRTL())
assert(pm.populateLowFIRRTLToHW())
assert(pm.populateLowHWToSV())
assert(pm.populateExportVerilog(_ => ()))
assert(pm.populateFinalizeIR())
assert(pm.run())

private val om = cvt.om()
private val evaluator = om.evaluator()
private val entry = evaluator.instantiate(className, Seq(om.newBasePathEmpty))

def entryUnderlying: PanamaCIRCTOMEvaluatorValueObject = entry
def t1Reader: T1Reader = new T1Reader(entry)
}

class T1Reader private[omreaderlib](entry: PanamaCIRCTOMEvaluatorValueObject) {
private val om = entry.field("om").asInstanceOf[PanamaCIRCTOMEvaluatorValueObject]
private val t1 = om.field("t1").asInstanceOf[PanamaCIRCTOMEvaluatorValueObject]

def vlen: Long = t1.field("vlen").asInstanceOf[PanamaCIRCTOMEvaluatorValuePrimitiveInteger].integer
def dlen: Long = t1.field("dlen").asInstanceOf[PanamaCIRCTOMEvaluatorValuePrimitiveInteger].integer

def dumpMethods(): Unit = {
val mirror = runtimeMirror(getClass.getClassLoader).reflect(this)
val methods = typeOf[T1Reader].decls.toList.filter(
m => m.isPublic && m.isMethod && !m.isConstructor && !m.asMethod.isGetter
)
methods.foreach(method => {
if (!method.name.toString.startsWith("dump")) {
val value = mirror.reflectMethod(method.asMethod)()
println(s"${method.name} = $value")
}
})
}

def dumpAll(): Unit = {
entry.foreachField((name, value) => println(s".$name => $value"))
}
}
Loading

0 comments on commit 93be2ca

Please sign in to comment.