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

Tiny fixes #325

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
28 changes: 22 additions & 6 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,41 @@ on:

name: Workflow checks for rust code

env:
LLVM_VERSION: 18.0

jobs:
test:
runs-on: ubuntu-latest
runs-on: macos-latest
strategy:
matrix:
test-code:
- "cd bril-rs && make test TURNTARGS=-v"
- "cd bril-rs && make features"
- "cd brilirs && make test TURNTARGS=-v"
- "cd brilirs && make benchmark TURNTARGS=-v"
- "cd brilift && cargo build --release && make rt.o && make test TURNTARGS=-v"
- "cd brilift && cargo build --release && make rt.o && make benchmark TURNTARGS=-v"
# Code requiring additional dependencies are separated out
include:
- test-code: "cd bril-rs && make features"
needs_cargo_hack: true
- test-code: "cd bril-rs/brillvm && make build"
needs_llvm: true
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@v1
with:
toolchain: stable

- name: Install LLVM and Clang
uses: KyleMayes/[email protected]
if: matrix.needs_llvm
with:
version: ${{ env.LLVM_VERSION }}

- name: Install cargo-hack for feature-flag checking
run: cargo install cargo-hack
if: matrix.needs_cargo_hack

- uses: actions/setup-python@v4
with:
Expand All @@ -65,7 +80,8 @@ jobs:
repository: cucapra/turnt
path: './turnt'
- name: Install Turnt part 2
run: cd turnt ; flit install --symlink
run: flit install --symlink
working-directory: ./turnt

- name: Problem matcher
run: echo '::add-matcher::.github/tap-matcher.json'
Expand All @@ -74,7 +90,7 @@ jobs:
run: ${{ matrix.test-code }}

rust-ci:
runs-on: ubuntu-latest
runs-on: macos-latest
env:
RUSTDOCFLAGS: -D warnings
RUSTFLAGS: -D warnings
Expand All @@ -88,10 +104,10 @@ jobs:
toolchain: stable

- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
uses: KyleMayes/install-llvm-action@v2.0.2
if: matrix.path == 'bril-rs/brillvm/Cargo.toml'
with:
version: "16.0"
version: ${{ env.LLVM_VERSION }}

- name: cargo check
run: cargo check --manifest-path ${{ matrix.path }} --all-targets
Expand Down
1 change: 0 additions & 1 deletion bril-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
description = "A rust representation of the Bril language"
readme = "README.md"
repository = "https://github.com/sampsyo/bril"
# license = "MIT"
license-file = "../LICENSE"
categories = ["command-line-utilities", "compilers", "data-structures", "parser-implementations"]
keywords = ["compiler", "bril", "parser", "data-structures", "language"]
Expand Down
11 changes: 0 additions & 11 deletions bril-rs/brillvm/.cargo/config

This file was deleted.

20 changes: 6 additions & 14 deletions bril-rs/brillvm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
workspace = { members = ["runtime"] }
[package]
name = "brillvm"
version = "0.1.0"
authors = ["Patrick LaFontaine <[email protected]>"]
edition = "2021"
description = "A compiler for Bril into LLVM IR"
readme = "README.md"
repository = "https://github.com/sampsyo/bril"
repository = "https://github.com/uwplse/bril"
# license = "MIT"
license-file = "../../LICENSE"
categories = ["command-line-utilities", "compilers"]
Expand All @@ -16,21 +17,12 @@ default-run = "main"

[dependencies]
clap = { version = "4.4", features = ["derive"] }
inkwell = { version = "0.2", features = ["llvm16-0"] }
libc-print = "0.1"
inkwell = { git = "https://github.com/TheDan64/inkwell.git", features = [
"llvm18-0",
] }

# For overriding parking_lot_core to latest version from 0.9.7
# To pull in the removal of `window-sys` which was causing duplicate dependencies
[patch.crates-io]
parking_lot_core = { git = "https://github.com/Amanieu/parking_lot" }
bril-rs = { git = "https://github.com/uwplse/bril", features = ["float", "ssa", "memory"] }

[dependencies.bril-rs]
path = ".."
features = ["float", "ssa", "memory"]

[[bin]]
name = "rt"
path = "bin/rt.rs"

# Need to set a default `main` to build `rt` bin
[[bin]]
Expand Down
5 changes: 4 additions & 1 deletion bril-rs/brillvm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ BENCHMARKS := ../../benchmarks/core/*.bril \
../../benchmarks/mem/*.bril \
../../benchmarks/mixed/*.bril

build:
cargo build

clean:
cargo clean
rm rt.bc
rm tmp.ll

.PHONY: rt
rt:
cargo rustc --bin rt --release -- --emit=llvm-bc && mv target/release/deps/rt-*.bc rt.bc
cd runtime && cargo rustc --release -- --emit=llvm-bc && mv ../target/release/deps/runtime-*.bc ../rt.bc

example:
# bril2json < ../../benchmarks/mem/sieve.bril | cargo run -- -i 100
Expand Down
12 changes: 12 additions & 0 deletions bril-rs/brillvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ This tool relies on the `llvm-sys` crate to find the correct version of LLVM to
```shell
LLVM_SYS_150_PREFIX="/opt/homebrew/Cellar/llvm/15.0.7_1/"
```

### zstd not found

```shell
ld: Library 'zstd' not found
```

Mac specific: Assuming you have zstd installed via `brew install zstd`, googling
around has found that you might need
`export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/`. Apparently this
also hit the ruby community at some point so, they have many issues with similar
resolutions to this problem.
9 changes: 9 additions & 0 deletions bril-rs/brillvm/runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "runtime"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc-print = "0.1"
11 changes: 11 additions & 0 deletions bril-rs/brillvm/runtime/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-arg=-nostdlib");
#[cfg(target_os = "macos")]
println!("cargo:rustc-link-arg=-undefined");
#[cfg(target_os = "macos")]
println!("cargo:rustc-link-arg=dynamic_lookup");

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
assert!(false)
}
File renamed without changes.
55 changes: 54 additions & 1 deletion bril-rs/brillvm/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use crate::llvm::create_module_from_program;
use bril_rs::load_program_from_read;
use clap::Parser;
use inkwell::{
context::Context,
module::Module,
targets::{InitializationConfig, Target},
};
use std::io::Read;

#[derive(Parser)]
#[derive(Parser, Debug)]
#[command(about, version, author)] // keeps the cli synced with Cargo.toml
#[command(allow_hyphen_values(true))] // allows for negative numbers
pub struct Cli {
/// The bril file to be compiled to LLVM. stdin is assumed if file is not provided.
#[arg(short, long, action)]
pub file: Option<String>,
/// The bril file to be compiled to LLVM. stdin is assumed if this string is not provided.
#[arg(short, long, action)]
pub program: Option<String>,

/// The path to the runtime library. Defaults to rt.bc
#[arg(short, long, action)]
Expand All @@ -20,3 +31,45 @@ pub struct Cli {
#[arg(action)]
pub args: Vec<String>,
}

pub fn run(args: &Cli) -> String {
let mut src = String::new();
if let Some(f) = &args.file {
let path = std::fs::canonicalize(f).unwrap();
let mut file = std::fs::File::open(path).unwrap();
file.read_to_string(&mut src).unwrap();
} else if let Some(prog) = &args.program {
src.clone_from(prog);
} else {
std::io::stdin().read_to_string(&mut src).unwrap();
};
let prog = load_program_from_read(src.as_bytes());

let context = Context::create();
let runtime_path = args.runtime.as_ref().map_or("rt.bc", |f| f);
// create a module from the runtime library for functions like printing/parsing
let runtime_module = Module::parse_bitcode_from_path(runtime_path, &context).unwrap();
let llvm_prog = create_module_from_program(&context, &prog, runtime_module);

//println!("{}", prog);
//llvm_prog.print_to_file("tmp.ll").unwrap();
llvm_prog.verify().unwrap();

if args.interpreter {
Target::initialize_native(&InitializationConfig::default())
.expect("Failed to initialize native target");

let engine = llvm_prog
.create_jit_execution_engine(inkwell::OptimizationLevel::None)
.unwrap();

let mut args: Vec<&str> = args.args.iter().map(std::convert::AsRef::as_ref).collect();
args.insert(0, "bril_prog");
unsafe {
engine.run_function_as_main(llvm_prog.get_function("main").unwrap(), &args);
}
String::new()
} else {
llvm_prog.to_string()
}
}
Loading
Loading