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

Link against CIRCT and use it for codegen #234

Merged
merged 52 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
bd1e194
mlir: Add a basic circt-sys crate
fabianschuiki Jul 22, 2021
7621f92
mlir: Add higher-level circt crate
fabianschuiki Aug 18, 2021
a80e56e
mlir: Add LLHD and standard dialect, restructure
fabianschuiki Sep 2, 2021
caca0d1
mlir: Add first codegen support
fabianschuiki Sep 2, 2021
8765141
mlir: Move circt crates into src directory
fabianschuiki Nov 13, 2021
65d8b0a
mlir: Add CIRCT submodule and build script
fabianschuiki Nov 14, 2021
a84e8c8
mlir: Compile order fixes
fabianschuiki Nov 15, 2021
f2633e5
mlir: Implement operation printing to a `Write`
fabianschuiki Nov 15, 2021
c0d27ec
mlir: Add LLVM lit-based tests
fabianschuiki Nov 15, 2021
1ff86d4
mlir: Add helper to go from MlirStringRef to &str
fabianschuiki Nov 15, 2021
bbe43a8
mlir: Move ModuleOp into a new `builtin` module
fabianschuiki Nov 15, 2021
ca620d5
mlir: Add support for debug info and location translation
fabianschuiki Nov 15, 2021
983e027
mlir: Add MLIR type emission
fabianschuiki Nov 15, 2021
ee28b3c
mlir: Add LLHD dialect type support
fabianschuiki Nov 15, 2021
639e56d
mlir: Emit proper entity type signatures
fabianschuiki Nov 15, 2021
dda21c6
mlir: First expression emissions
fabianschuiki Nov 15, 2021
f509127
mlir: Add first declaration support
fabianschuiki Nov 15, 2021
bc806e6
mlir: Add port default value driving
fabianschuiki Nov 15, 2021
ff32e20
mlir: Basic process emission
fabianschuiki Nov 16, 2021
864a88f
mlir: Add process prolog/epilog
fabianschuiki Nov 16, 2021
32bb626
mlir: Impl rvalue codegen
fabianschuiki Nov 17, 2021
e93cf19
mlir: Update CI to include CIRCT/LLVM and lit tests
fabianschuiki Nov 17, 2021
0390669
mlir: Add missing struct/array const emission
fabianschuiki Nov 18, 2021
da9ffec
mlir: Get new codegen to not crash for all tests
fabianschuiki Nov 18, 2021
730af9d
mlir: Stop using llhd.not and llhd.neg ops
fabianschuiki Nov 18, 2021
4385ed6
mlir: Add module and process instantiation
fabianschuiki Nov 18, 2021
c4c62c8
mlir: Impl if stmt codegen
fabianschuiki Nov 20, 2021
ec8c5cd
mlir: Impl loop codegen
fabianschuiki Nov 21, 2021
cda34a0
mlir: Impl procedural assignment codegen
fabianschuiki Nov 21, 2021
15d2982
mlir: Impl timed wait codegen
fabianschuiki Nov 21, 2021
abd95a5
mlir: Impl implicit event codegen
fabianschuiki Nov 21, 2021
71c0c76
mlir: Wait emission cleanup
fabianschuiki Nov 21, 2021
87b41e1
mlir: Impl case stmt codegen
fabianschuiki Nov 21, 2021
a0a3f8e
mlir: Impl explicit event trigger codegen
fabianschuiki Nov 21, 2021
825aaf6
mlir: Rearrange test cases
fabianschuiki Nov 21, 2021
7c99782
mlir: Remove obsolete codegen functions
fabianschuiki Nov 21, 2021
4b53323
mlir: Update circt submodule
fabianschuiki Nov 24, 2021
68651cb
mlir: Update README with CIRCT/LLVM/MLIR instructions
fabianschuiki Nov 24, 2021
5a35b81
mlir: Point CI build to install directories instead of build
fabianschuiki Nov 24, 2021
57f9f32
mlir: Drop xml2 from CIRCT system library list
fabianschuiki Nov 24, 2021
7d32f19
mlir: Report MLIR diagnostics through Moore's diagnostic engine
fabianschuiki Nov 24, 2021
eb13fdb
mlir: Remove unused ops, fix cond_br, fix typos
fabianschuiki Nov 24, 2021
591acfa
mlir: Pick a dummy name for process instances
fabianschuiki Nov 24, 2021
33a5b1f
mlir: Revert CI back to using build dir, don't install
fabianschuiki Nov 25, 2021
540e431
mlir: Fix ICmpOp return type and ExtractOp attr type
fabianschuiki Nov 25, 2021
bab3cf7
mlir: Uniquify signal and instance names
fabianschuiki Nov 25, 2021
09cf0e0
mlir: Improve MLIR location tracking
fabianschuiki Nov 25, 2021
874a108
mlir: Improve circt-sys rebuild on CIRCT recompilation
fabianschuiki Nov 25, 2021
1a88094
mlir: Properly truncate indices/offsets to clog2 of array/integer size
fabianschuiki Nov 25, 2021
1730612
mlir: Fix minor emission issues
fabianschuiki Nov 25, 2021
5416d84
mlir: Speed up location translations
fabianschuiki Jan 15, 2022
de5abbb
rustfmt: Fix vhdl/score/lower_hir.rs
fabianschuiki Jan 16, 2022
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
14 changes: 14 additions & 0 deletions .github/build-circt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -e

mkdir -p circt/build
mkdir -p circt/install
cd circt/build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=../install \
-DMLIR_DIR=$PWD/../llvm/build/lib/cmake/mlir \
-DLLVM_DIR=$PWD/../llvm/build/lib/cmake/llvm \
fabianschuiki marked this conversation as resolved.
Show resolved Hide resolved
-DLLVM_ENABLE_ASSERTIONS=ON

cmake --build . --target install -- -j$(nproc)
19 changes: 19 additions & 0 deletions .github/build-llvm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e

mkdir -p circt/llvm/build
mkdir -p circt/llvm/install
cd circt/llvm/build
cmake ../llvm \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=../install \
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_BINDINGS=OFF \
-DLLVM_ENABLE_OCAMLDOC=OFF \
-DLLVM_ENABLE_PROJECTS=mlir \
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_OPTIMIZED_TABLEGEN=ON \
-DLLVM_TARGETS_TO_BUILD=""

cmake --build . --target install -- -j$(nproc)
124 changes: 88 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,119 @@ on: [push, pull_request]
name: CI

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: "source checkout"
- uses: actions-rs/toolchain@v1
name: "toolchain installation"
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
name: "cargo check"
with:
command: check

lints:
name: Lints
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: "source checkout"
- uses: actions-rs/toolchain@v1
name: "toolchain installation"
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy

- uses: actions-rs/cargo@v1
name: "cargo fmt"
- name: Run rustfmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

- uses: actions-rs/cargo@v1
name: "cargo clippy"
continue-on-error: true # TODO: Remove once we have fixed the mall
- name: Run clippy
uses: actions-rs/cargo@v1
continue-on-error: true # TODO: Remove once we have fixed them all
with:
command: clippy

test:
name: Tests
check:
name: Checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: "source checkout"
- name: Checkout sources
uses: actions/checkout@v2
with:
submodules: recursive
- uses: actions-rs/toolchain@v1
name: "toolchain installation"

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
name: "cargo test"
components: rustfmt, clippy

- name: Configure Environment
id: env
run: |
echo "CIRCT_SYS_CIRCT_DIR=$GITHUB_WORKSPACE/circt" >> $GITHUB_ENV
echo "::set-output name=circt-hash::$(git rev-parse @:./circt)"
echo "::set-output name=llvm-hash::$(cd circt && git rev-parse @:./llvm)"

# Fetch LLVM from the cache if possible, otherwise rebuild it.
- name: Fetch LLVM from cache
id: cache-llvm
uses: martijnhols/actions-cache/restore@v3
with:
path: circt/llvm/build
fabianschuiki marked this conversation as resolved.
Show resolved Hide resolved
key: ${{ runner.os }}-llvm-${{ hashFiles('.github/build-llvm.sh') }}-${{ steps.env.outputs.llvm-hash }}

- name: Rebuild LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: .github/build-llvm.sh

- name: Upload LLVM to cache
if: steps.cache-llvm.outputs.cache-hit != 'true'
uses: martijnhols/actions-cache/save@v3
with:
path: circt/llvm/build
key: ${{ steps.cache-llvm.outputs.primary-key }}

# Fetch CIRCT from the cache if possible, otherwise rebuild it.
- name: Fetch CIRCT from cache
id: cache-circt
uses: martijnhols/actions-cache/restore@v3
with:
path: circt/build
key: ${{ runner.os }}-circt-${{ hashFiles('.github/build-circt.sh') }}-${{ steps.env.outputs.circt-hash }}

- name: Rebuild CIRCT
if: steps.cache-circt.outputs.cache-hit != 'true'
run: .github/build-circt.sh

- name: Upload CIRCT to cache
if: steps.cache-circt.outputs.cache-hit != 'true'
uses: martijnhols/actions-cache/save@v3
with:
path: circt/build
key: ${{ steps.cache-circt.outputs.primary-key }}

# Run the quick check build.
- name: Run "cargo check"
uses: actions-rs/cargo@v1
with:
command: check
args: --all

# Run the regular build.
- name: Run "cargo build"
uses: actions-rs/cargo@v1
with:
command: build

# Run the unit tests.
- name: Run "cargo test"
uses: actions-rs/cargo@v1
with:
command: test
args: --all
- run: python3 scripts/test.py --debug --verbose
name: "regression tests"

# Run the tests.
- name: Run lit tests
run: circt/llvm/build/bin/llvm-lit test -v
- name: Run regression tests (old)
run: env TERM=xterm-color test/run.sh
- name: Run regression tests (new)
run: python3 scripts/test.py --debug --verbose
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "test/third-party/common_cells"]
path = test/third-party/common_cells
url = https://github.com/pulp-platform/common_cells.git
[submodule "circt"]
path = circt
url = https://github.com/fabianschuiki/circt.git
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ moore-common = { path = "src/common", version = "0.13.2" }
moore-derive = { path = "src/derive", version = "0.13.2" }
moore-svlog = { path = "src/svlog", version = "0.13.2" }
moore-vhdl = { path = "src/vhdl", version = "0.13.2" }
circt = { path = "src/circt" }
clap = "2"
llhd = "0.16"
# llhd = { git = "https://github.com/fabianschuiki/llhd" }
Expand Down
1 change: 1 addition & 0 deletions circt
Submodule circt added at 82b184
3 changes: 2 additions & 1 deletion scripts/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def finish(self):
else:
suffices = ["sv", "v", "vhd"]
third_party = test_dir/"third-party"
globs = [[p for p in test_dir.glob("**/*."+suffix) if third_party not in p.parents] for suffix in suffices]
mlir = test_dir/"mlir"
globs = [[p for p in test_dir.glob("**/*."+suffix) if third_party not in p.parents and mlir not in p.parents] for suffix in suffices]
tests = [TestCase(p.relative_to(test_dir), p) for p in sorted(itertools.chain(*globs))]
sys.stdout.write("running {} tests\n".format(len(tests)))

Expand Down
49 changes: 35 additions & 14 deletions src/bin/moore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#[macro_use]
extern crate log;

use circt::{mlir, prelude::*};
use clap::{App, Arg, ArgMatches};
use llhd;
use llhd::opt::{Pass, PassContext};
// use llhd::opt::{Pass, PassContext};
use moore::common::score::NodeRef;
use moore::errors::*;
use moore::name::Name;
Expand Down Expand Up @@ -144,7 +145,13 @@ fn main() {
.long("format")
.help("Output format")
.takes_value(true)
.possible_values(&["llhd", "mlir"]),
.possible_values(&["llhd", "mlir", "mlir-native"]),
)
.arg(
Arg::with_name("debug-info")
.short("g")
.long("debug-info")
.help("Emit location information as part of the output"),
)
.arg(
Arg::with_name("INPUT")
Expand Down Expand Up @@ -487,21 +494,31 @@ fn elaborate_name(
svlog::InstVerbosityVisitor::new(ctx.svlog).visit_node_with_id(m, false);
}

let mut cg = svlog::CodeGenerator::new(ctx.svlog);
let mlir_cx = mlir::OwnedContext::new();
mlir_cx.load_dialect(circt::std::dialect());
mlir_cx.load_dialect(circt::hw::dialect());
mlir_cx.load_dialect(circt::comb::dialect());
mlir_cx.load_dialect(circt::llhd::dialect());
mlir_cx.load_dialect(circt::seq::dialect());

let mlir_module = circt::ModuleOp::new(*mlir_cx);

let mut cg = svlog::CodeGenerator::new(ctx.svlog, mlir_module);
cg.emit_module(m)?;
let mut module = cg.finalize();
let pass_ctx = PassContext;
if ctx.sess.opts.opt_level > 0 {
llhd::pass::ConstFolding::run_on_module(&pass_ctx, &mut module);
// llhd::pass::VarToPhiPromotion::run_on_module(&pass_ctx, &mut module); // broken in llhd 0.13
llhd::pass::DeadCodeElim::run_on_module(&pass_ctx, &mut module);
llhd::pass::GlobalCommonSubexprElim::run_on_module(&pass_ctx, &mut module);
llhd::pass::InstSimplification::run_on_module(&pass_ctx, &mut module);
llhd::pass::DeadCodeElim::run_on_module(&pass_ctx, &mut module);
}
let module = cg.finalize();
// let mut module = cg.finalize();
// let pass_ctx = PassContext;
// if ctx.sess.opts.opt_level > 0 {
// llhd::pass::ConstFolding::run_on_module(&pass_ctx, &mut module);
// llhd::pass::VarToPhiPromotion::run_on_module(&pass_ctx, &mut module); // broken in llhd 0.13
// llhd::pass::DeadCodeElim::run_on_module(&pass_ctx, &mut module);
// llhd::pass::GlobalCommonSubexprElim::run_on_module(&pass_ctx, &mut module);
// llhd::pass::InstSimplification::run_on_module(&pass_ctx, &mut module);
// llhd::pass::DeadCodeElim::run_on_module(&pass_ctx, &mut module);
// }

// Decide what format to use for the output.
emit_output(matches, ctx, &module)?;
emit_output(matches, ctx, &module, mlir_module)?
}
}
Ok(())
Expand All @@ -511,17 +528,20 @@ fn elaborate_name(
enum OutputFormat {
Llhd,
Mlir,
MlirNative,
}

fn emit_output(
matches: &ArgMatches,
ctx: &ScoreContext,
module: &llhd::ir::Module,
mlir_module: circt::ModuleOp,
) -> Result<(), ()> {
// Check if the user has provided an explicit output format.
let fmt = match matches.value_of("output-format") {
Some("llhd") => Some(OutputFormat::Llhd),
Some("mlir") => Some(OutputFormat::Mlir),
Some("mlir-native") => Some(OutputFormat::MlirNative),
Some(x) => {
ctx.sess.emit(DiagBuilder2::fatal(format!(
"unknown output format: `{}`",
Expand Down Expand Up @@ -566,6 +586,7 @@ fn emit_output(
match fmt {
OutputFormat::Llhd => llhd::assembly::write_module(output, &module),
OutputFormat::Mlir => llhd::mlir::write_module(output, &module),
OutputFormat::MlirNative => mlir_module.print(output, matches.is_present("debug-info")),
};
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions src/circt-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "circt-sys"
version = "0.1.0"
authors = ["Fabian Schuiki <fabian@schuiki.ch>"]
edition = "2018"

[dependencies]
libc = "0.2"

[build-dependencies]
bindgen = "0.59.0"
cc = "1.0"
Loading