Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a Pure LLVM Lit test framework #3596

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions build.sc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mill._
import mill.scalalib._
import mill.scalalib.TestModule._
import mill.scalalib.publish._
import mill.scalalib.scalafmt._
import coursier.maven.MavenRepository
import mill.define.Cross
import mill.scalalib.api.ZincWorkerUtil.matchingVersions
import $file.common
import $file.tests

object v {
val pluginScalaCrossVersions = Seq(
Expand Down Expand Up @@ -65,7 +65,7 @@ trait Svsim
object firrtlut extends Cross[FirrtlUnitTest](v.scalaCrossVersions)

trait FirrtlUnitTest
extends common.FirrtlUnitTestModule
extends tests.FirrtlUnitTestModule
with CrossModuleBase
with ScalafmtModule {
override def millSourcePath = firrtl(crossScalaVersion).millSourcePath
Expand Down Expand Up @@ -185,7 +185,7 @@ trait Chisel
object chiselut extends Cross[ChiselUnitTest](v.scalaCrossVersions)

trait ChiselUnitTest
extends common.ChiselUnitTestModule
extends tests.ChiselUnitTestModule
with CrossModuleBase
with ScalafmtModule {
override def millSourcePath = chisel(crossScalaVersion).millSourcePath
Expand Down Expand Up @@ -256,7 +256,7 @@ trait CIRCTPanamaBinder
object bindertest extends Cross[CIRCTPanamaBinderModuleTest](v.scalaCrossVersions)

trait CIRCTPanamaBinderModuleTest
extends common.CIRCTPanamaBinderModuleTestModule
extends tests.CIRCTPanamaBinderModuleTestModule
with CrossModuleBase
with ScalafmtModule {
override def millSourcePath = circtpanamabinder(crossScalaVersion).millSourcePath
Expand All @@ -271,3 +271,27 @@ trait CIRCTPanamaBinderModuleTest
Seq(PathRef(millSourcePath / "src" / "test"))
}
}

object litutility extends Cross[LitUtility](v.scalaCrossVersions)

trait LitUtility
extends tests.LitUtilityModule
with CrossModuleBase
with ScalafmtModule {
def millSourcePath = super.millSourcePath / os.up / "lit" / "utility"
def circtPanamaBinderModule = circtpanamabinder(crossScalaVersion)
}

object lit extends Cross[Lit](v.scalaCrossVersions)

trait Lit
extends tests.LitModule
with Cross.Module[String] {
def scalaVersion: T[String] = crossValue
def runClasspath: T[Seq[os.Path]] = T(litutility(crossValue).runClasspath().map(_.path))
def pluginJars: T[Seq[os.Path]] = T(Seq(litutility(crossValue).circtPanamaBinderModule.pluginModule.jar().path))
def javaLibraryPath: T[Seq[os.Path]] = T(litutility(crossValue).circtPanamaBinderModule.libraryPaths().map(_.path))
def javaHome: T[os.Path] = T(os.Path(sys.props("java.home")))
def chiselLitDir: T[os.Path] = T(millSourcePath)
def litConfigIn: T[PathRef] = T.source(millSourcePath / "tests" / "lit.site.cfg.py.in")
}
75 changes: 0 additions & 75 deletions common.sc
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,6 @@ trait SvsimModule
extends ScalaModule {
}

trait SvsimUnitTestModule
extends TestModule
with ScalaModule
with TestModule.ScalaTest {
def svsimModule: SvsimModule

def scalatestIvy: Dep

def scalacheckIvy: Dep

override def moduleDeps = Seq(svsimModule)

override def defaultCommandName() = "test"

override def ivyDeps = super.ivyDeps() ++ Agg(
scalatestIvy,
scalacheckIvy
)
}

trait FirrtlUnitTestModule
extends TestModule
with ScalaModule
with TestModule.ScalaTest {
def firrtlModule: FirrtlModule

def scalatestIvy: Dep

def scalacheckIvy: Dep

override def moduleDeps = Seq(firrtlModule)

override def defaultCommandName() = "test"

override def ivyDeps = super.ivyDeps() ++ Agg(
scalatestIvy,
scalacheckIvy
)
}

trait CoreModule
extends ScalaModule
with HasMacroAnnotations {
Expand Down Expand Up @@ -158,24 +118,6 @@ trait HasChisel
override def moduleDeps = super.moduleDeps ++ Some(chiselModule)
}

trait ChiselUnitTestModule
extends TestModule
with ScalaModule
with HasChisel
with HasMacroAnnotations
with TestModule.ScalaTest {
def scalatestIvy: Dep

def scalacheckIvy: Dep

override def defaultCommandName() = "test"

override def ivyDeps = super.ivyDeps() ++ Agg(
scalatestIvy,
scalacheckIvy
)
}

trait HasJextractGeneratedSources
extends JavaModule {
def includePaths: T[Seq[PathRef]]
Expand Down Expand Up @@ -280,20 +222,3 @@ trait HasCIRCTPanamaBinderModule
)
}

trait CIRCTPanamaBinderModuleTestModule
extends TestModule
with ScalaModule
with HasCIRCTPanamaBinderModule
with HasMacroAnnotations
with TestModule.ScalaTest {
def scalatestIvy: Dep

def scalacheckIvy: Dep

override def defaultCommandName() = "test"

override def ivyDeps = super.ivyDeps() ++ Agg(
scalatestIvy,
scalacheckIvy
)
}
3 changes: 3 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
mill
circt
jextract
lit
llvm
scala-cli
];
in
{
Expand Down
16 changes: 16 additions & 0 deletions lit/tests/CompilerPlugin/WithCompilerPlugin.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: scala-cli --server=false --java-home=%JAVAHOME --extra-jars=%RUNCLASSPATH --scala-version=%SCALAVERSION --scala-option="-Xplugin:%SCALAPLUGINJARS" --java-opt="--enable-native-access=ALL-UNNAMED --enable-preview -Djava.library.path=%JAVALIBRARYPATH" %s | FileCheck %s

import chisel3._
import circt.stage.ChiselStage
class FooBundle extends Bundle {
val foo = Input(UInt(3.W))
}
// CHECK-LABEL: module FooModule
// CHECK: input clock : Clock
// CHECK: input reset : UInt<1>
class FooModule extends Module {
// CHECK: output io : { flip foo : UInt<3>}
val io = IO(new FooBundle)
// CHECK: skip
}
println(ChiselStage.emitCHIRRTL(new FooModule))
12 changes: 12 additions & 0 deletions lit/tests/CompilerPlugin/WithoutCompilerPlugin.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: not scala-cli --server=false --java-home=%JAVAHOME --extra-jars=%RUNCLASSPATH --scala-version=%SCALAVERSION --java-opt="--enable-native-access=ALL-UNNAMED --enable-preview -Djava.library.path=%JAVALIBRARYPATH" %s 2>&1 | FileCheck %s

import chisel3._
import circt.stage.ChiselStage
class FooBundle extends Bundle {
val foo = Input(UInt(3.W))
}
class FooModule extends Module {
// CHECK: assertion failed: The Chisel compiler plugin is now required for compiling Chisel code.
val io = IO(new FooBundle)
}
println(ChiselStage.emitCHIRRTL(new FooModule))
16 changes: 16 additions & 0 deletions lit/tests/lit.cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import platform
import lit.formats
from lit.llvm import llvm_config
from lit.llvm.subst import ToolSubst

config.name = 'CHISEL'
config.test_format = lit.formats.ShTest(True)
config.suffixes = [".sc"]
config.substitutions = [
('%SCALAVERSION', config.scala_version),
('%RUNCLASSPATH', ':'.join(config.run_classpath)),
('%SCALAPLUGINJARS', ':'.join(config.scala_plugin_jars)),
('%JAVAHOME', config.java_home),
('%JAVALIBRARYPATH', ':'.join(config.java_library_path))
]
config.test_source_root = os.path.dirname(__file__)
9 changes: 9 additions & 0 deletions lit/tests/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
config.scala_version = "@SCALA_VERSION@"
config.run_classpath = "@RUN_CLASSPATH@".split(",")
config.scala_plugin_jars = "@SCALA_PLUGIN_JARS@".split(",")
config.java_home = "@JAVA_HOME@"
config.java_library_path = "@JAVA_LIBRARY_PATH@".split(",")
config.chisel_lit_dir = "@CHISEL_LIT_DIR@".split(",")
config.test_exec_root = os.path.dirname(__file__)
lit_config.load_config(config, "@CHISEL_LIT_DIR@/tests/lit.cfg.py")
23 changes: 23 additions & 0 deletions lit/utility/src/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chisel3._

package object utility {
object binding {
def streamString(module: => RawModule, stream: chisel3.internal.CIRCTConverter => geny.Writable): String = Seq(
new chisel3.stage.phases.Elaborate,
chisel3.internal.panama.Convert
).foldLeft(
firrtl.AnnotationSeq(Seq(chisel3.stage.ChiselGeneratorAnnotation(() => module)))
) { case (annos, phase) => phase.transform(annos) }
.collectFirst {
case chisel3.internal.panama.circt.PanamaCIRCTConverterAnnotation(converter) =>
val string = new java.io.ByteArrayOutputStream
stream(converter).writeBytesTo(string)
new String(string.toByteArray)
}
.get

def firrtlString(module: => RawModule): String = streamString(module, _.firrtlStream)

def verilogString(module: => RawModule): String = streamString(module, _.verilogStream)
}
}
88 changes: 88 additions & 0 deletions nix/scala-cli.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{ stdenv
, coreutils
, lib
, installShellFiles
, zlib
, autoPatchelfHook
, fetchurl
, makeWrapper
, callPackage
, jre
, testers
, scala-cli
}:

let
pname = "scala-cli";
sources = lib.importJSON ./sources.json;
inherit (sources) version assets;

platforms = builtins.attrNames assets;
in
stdenv.mkDerivation {
inherit pname version;
nativeBuildInputs = [ installShellFiles makeWrapper ]
++ lib.optional stdenv.isLinux autoPatchelfHook;
buildInputs =
assert lib.assertMsg (lib.versionAtLeast jre.version "17.0.0") ''
scala-cli requires Java 17 or newer, but ${jre.name} is ${jre.version}
'';
[ coreutils zlib stdenv.cc.cc ];
src =
let
asset = assets."${stdenv.hostPlatform.system}" or (throw "Unsupported platform ${stdenv.hostPlatform.system}");
in
fetchurl {
url = "https://github.com/Virtuslab/scala-cli/releases/download/v${version}/${asset.asset}";
sha256 = asset.sha256;
};
unpackPhase = ''
runHook preUnpack
gzip -d < $src > scala-cli
runHook postUnpack
'';

installPhase = ''
runHook preInstall
install -Dm755 scala-cli $out/bin/.scala-cli-wrapped
makeWrapper $out/bin/.scala-cli-wrapped $out/bin/scala-cli \
--set JAVA_HOME ${jre.home} \
--argv0 "$out/bin/scala-cli"
runHook postInstall
'';

# We need to call autopatchelf before generating completions
dontAutoPatchelf = true;

postFixup = lib.optionalString stdenv.isLinux ''
autoPatchelf $out
'' + ''
# hack to ensure the completion function looks right
# as $0 is used to generate the compdef directive
mkdir temp
cp $out/bin/.scala-cli-wrapped temp/scala-cli
PATH="./temp:$PATH"

installShellCompletion --cmd scala-cli \
--bash <(scala-cli completions bash) \
--zsh <(scala-cli completions zsh)
'';

meta = with lib; {
homepage = "https://scala-cli.virtuslab.org";
downloadPage = "https://github.com/VirtusLab/scala-cli/releases/v${version}";
sourceProvenance = with sourceTypes; [ binaryNativeCode ];
license = licenses.asl20;
description = "Command-line tool to interact with the Scala language";
maintainers = [ maintainers.kubukoz ];
inherit platforms;
};

passthru.updateScript = callPackage ./update.nix { } { inherit platforms pname version; };

passthru.tests.version = testers.testVersion {
package = scala-cli;
command = "scala-cli version --offline";
};
}

21 changes: 21 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "1.1.2",
"assets": {
"aarch64-darwin": {
"asset": "scala-cli-aarch64-apple-darwin.gz",
"sha256": "1m5ac0pkp68k446xd8p21hc8caaahpyd3vm5pn1i1cc176f6fm8b"
},
"aarch64-linux": {
"asset": "scala-cli-aarch64-pc-linux.gz",
"sha256": "1aqw8xmim7rmh8h98h10dlhm0hzl9bs89a51mbd3d99yxa5dq4z0"
},
"x86_64-darwin": {
"asset": "scala-cli-x86_64-apple-darwin.gz",
"sha256": "06473l04z3nfadi72klk3aw4i39qkyzl3qp7kb1ysn5wx58ci51w"
},
"x86_64-linux": {
"asset": "scala-cli-x86_64-pc-linux.gz",
"sha256": "1khdzv0s9l6wcb47hnv96prvzgzwcw0db34j6525bdgakj62fhf3"
}
}
}
1 change: 1 addition & 0 deletions overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ final: prev:
circt.llvm.lib
];
};
scala-cli = prev.callPackage ./nix/scala-cli.nix { };
}
Loading
Loading