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

Libgccjit codegen #87260

Merged
merged 26 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
afae271
Initial commit
antoyo May 10, 2020
f7237f1
Add 'compiler/rustc_codegen_gcc/' from commit 'afae271d5d3719eeb92c18…
antoyo Aug 13, 2021
8841e9e
Fix tidy
antoyo Jul 19, 2021
7132ce6
Exclude rustc_codegen_gcc from namespace
antoyo Jul 19, 2021
0c89065
Update to nightly-2021-08-12 (#61)
antoyo Aug 14, 2021
6fc0696
Merge commit '0c89065b934397b62838fe3e4ef6f6352fc52daf' into libgccji…
antoyo Aug 14, 2021
e228f0c
Cleanup (#67)
antoyo Aug 15, 2021
3d5d4e3
Merge commit 'e228f0c16ea8c34794a6285bf57aab627c26b147' into libgccji…
antoyo Aug 15, 2021
6f50986
Empty gcc_path
antoyo Aug 15, 2021
87f8525
Merge commit '6f50986667debbfc67776304a8ee23fe0158613f' into libgccji…
antoyo Aug 15, 2021
5dad13c
Update custom rustc instructions (#73)
antoyo Aug 28, 2021
7c707e4
Implement basic inline asm support (#72)
Sep 5, 2021
4d3dcd4
Remove FUNDING.yml
antoyo Sep 16, 2021
8ec7976
fix: gh origin on readme (#83)
emersonlaurentino Sep 17, 2021
48d60ab
Update to nightly-2021-09-11 (#79)
Sep 17, 2021
df48731
Merge commit '48d60ab7c505c6c1ebb042eacaafd8dc9f7a9267' into libgccji…
antoyo Sep 17, 2021
0f4b616
Add notes for cross-compilation to gcc-only targets (#68)
antoyo Sep 26, 2021
4e7e822
Impove handling of registers in inline asm (#82)
Sep 26, 2021
64c561d
Fix global initialization (#91)
antoyo Sep 26, 2021
ab4ff2d
Cleanup fix for global initialization (#93)
antoyo Sep 27, 2021
63608ac
Fix/mismatch types (#94)
antoyo Sep 27, 2021
11c2023
Fix/count trailing zeroes (#95)
antoyo Sep 28, 2021
9809f5d
Update to nightly-2021-09-28 (#97)
antoyo Sep 28, 2021
7f32dd5
Merge commit '9809f5d21990d9e24b3e9876ea7da756fd4e9def' into libgccji…
antoyo Sep 28, 2021
cd4810d
Fix warnings (#98)
antoyo Sep 28, 2021
90be409
Merge commit 'cd4810de42c57b64b74dae09c530a4c3a41f87b9' into libgccji…
antoyo Sep 28, 2021
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
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_gcc/.github/FUNDING.yml

This file was deleted.

5 changes: 4 additions & 1 deletion compiler/rustc_codegen_gcc/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ jobs:
ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0

- name: Set LIBRARY_PATH
run: echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV
run: |
echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV

# https://github.com/actions/cache/issues/133
- name: Fixup owner of ~/.cargo/
Expand Down Expand Up @@ -66,6 +68,7 @@ jobs:
run: |
./prepare_build.sh
./build.sh
cargo test
./clean_all.sh

- name: Prepare dependencies
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_codegen_gcc/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ perf.data.old
*.events
*.string*
/build_sysroot/sysroot
/build_sysroot/sysroot_src
/build_sysroot/Cargo.lock
/build_sysroot/test_target/Cargo.lock
/rust
/simple-raytracer
/regex
gimple*
*asm
res
test-backend
gcc_path
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ dependencies = [
[[package]]
name = "gccjit"
version = "1.0.0"
source = "git+https://github.com/antoyo/gccjit.rs#0572117c7ffdfcb0e6c6526d45266c3f34796bea"
source = "git+https://github.com/antoyo/gccjit.rs#54be27e41fff7b6ab532e2e21a82df50a12b9ad3"
dependencies = [
"gccjit_sys",
]

[[package]]
name = "gccjit_sys"
version = "0.0.1"
source = "git+https://github.com/antoyo/gccjit.rs#0572117c7ffdfcb0e6c6526d45266c3f34796bea"
source = "git+https://github.com/antoyo/gccjit.rs#54be27e41fff7b6ab532e2e21a82df50a12b9ad3"
dependencies = [
"libc 0.1.12",
]
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_codegen_gcc/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can also use my [fork of gcc](https://github.com/antoyo/gcc) which already i
**Put the path to your custom build of libgccjit in the file `gcc_path`.**

```bash
$ git clone https://github.com/antoyo/rustc_codegen_gcc.git
$ git clone https://github.com/rust-lang/rustc_codegen_gcc.git
$ cd rustc_codegen_gcc
$ ./prepare_build.sh # download and patch sysroot src
$ ./build.sh --release
Expand Down Expand Up @@ -113,6 +113,5 @@ p loc->m_line

### How to use a custom-build rustc

* Build the stage1 compiler (`rustup toolchain link debug-current stage2 build/x86_64-unknown-linux-gnu/stage1`).
* Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
* Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.
* Add `~/.rustup/toolchains/debug-current/lib/rustlib/x86_64-unknown-linux-gnu/lib` to `LD_LIBRARY_PATH`.
7 changes: 6 additions & 1 deletion compiler/rustc_codegen_gcc/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
#set -x
set -e

export GCC_PATH=$(cat gcc_path)
if [ -f ./gcc_path ]; then
export GCC_PATH=$(cat gcc_path)
else
echo 'Please put the path to your custom build of libgccjit in the file `gcc_path`, see Readme.md for details'
exit 1
fi

export LD_LIBRARY_PATH="$GCC_PATH"
export LIBRARY_PATH="$GCC_PATH"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ fi
cmd=$1
shift

RUSTDOCFLAGS=$RUSTFLAGS cargo +${TOOLCHAIN} $cmd --target $TARGET_TRIPLE $@
RUSTDOCFLAGS="$RUSTFLAGS" cargo +${TOOLCHAIN} $cmd --target $TARGET_TRIPLE $@
9 changes: 7 additions & 2 deletions compiler/rustc_codegen_gcc/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ set -e

export CARGO_INCREMENTAL=0

export GCC_PATH=$(cat gcc_path)
if [ -f ./gcc_path ]; then
export GCC_PATH=$(cat gcc_path)
else
echo 'Please put the path to your custom build of libgccjit in the file `gcc_path`, see Readme.md for details'
exit 1
fi

unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
Expand Down Expand Up @@ -30,7 +35,7 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
fi
fi

export RUSTFLAGS=$linker' -Cpanic=abort -Cdebuginfo=2 -Zpanic-abort-tests -Zcodegen-backend='$(pwd)'/target/'$CHANNEL'/librustc_codegen_gcc.'$dylib_ext' --sysroot '$(pwd)'/build_sysroot/sysroot'
export RUSTFLAGS="$linker -Cpanic=abort -Cdebuginfo=2 -Clto=off -Zpanic-abort-tests -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot"

# FIXME(antoyo): remove once the atomic shim is gone
if [[ `uname` == 'Darwin' ]]; then
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2021-08-12
nightly-2021-09-17
5 changes: 4 additions & 1 deletion compiler/rustc_codegen_gcc/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_span::symbol::sym;

use crate::GccContext;

pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, kind: AllocatorKind, has_alloc_error_handler: bool) {
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
let context = &mods.context;
let usize =
match tcx.sess.target.pointer_width {
Expand Down Expand Up @@ -77,6 +77,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, kind: Alloc
else {
block.end_with_void_return(None);
}

// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
}

let types = [usize, usize];
Expand Down
94 changes: 21 additions & 73 deletions compiler/rustc_codegen_gcc/src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use std::fs::File;
use std::path::{Path, PathBuf};

use rustc_session::Session;
use rustc_codegen_ssa::back::archive::{find_library, ArchiveBuilder};
use rustc_codegen_ssa::METADATA_FILENAME;
use rustc_codegen_ssa::back::archive::ArchiveBuilder;

use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_middle::middle::cstore::DllImport;
use rustc_span::symbol::Symbol;


struct ArchiveConfig<'a> {
sess: &'a Session,
dst: PathBuf,
lib_search_paths: Vec<PathBuf>,
use_native_ar: bool,
use_gnu_style_archive: bool,
}
Expand All @@ -35,11 +34,9 @@ pub struct ArArchiveBuilder<'a> {

impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
fn new(sess: &'a Session, output: &Path, input: Option<&Path>) -> Self {
use rustc_codegen_ssa::back::link::archive_search_paths;
let config = ArchiveConfig {
sess,
dst: output.to_path_buf(),
lib_search_paths: archive_search_paths(sess),
use_native_ar: false,
// FIXME test for linux and System V derivatives instead
use_gnu_style_archive: sess.target.options.archive_format == "gnu",
Expand Down Expand Up @@ -94,47 +91,27 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
));
}

fn add_native_library(&mut self, name: Symbol, verbatim: bool) {
let location = find_library(name, verbatim, &self.config.lib_search_paths, self.config.sess);
self.add_archive(location.clone(), |_| false)
.unwrap_or_else(|e| {
panic!(
"failed to add native library {}: {}",
location.to_string_lossy(),
e
);
});
}

fn add_rlib(
&mut self,
rlib: &Path,
name: &str,
lto: bool,
skip_objects: bool,
) -> std::io::Result<()> {
let obj_start = name.to_owned();

self.add_archive(rlib.to_owned(), move |fname: &str| {
// Ignore metadata files, no matter the name.
if fname == METADATA_FILENAME {
return true;
}

// Don't include Rust objects if LTO is enabled
if lto && fname.starts_with(&obj_start) && fname.ends_with(".o") {
return true;
}
fn add_archive<F>(&mut self, archive_path: &Path, mut skip: F) -> std::io::Result<()>
where
F: FnMut(&str) -> bool + 'static,
{
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
let archive_index = self.src_archives.len();

// Otherwise if this is *not* a rust object and we're skipping
// objects then skip this file
if skip_objects && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
return true;
let mut i = 0;
while let Some(entry) = archive.next_entry() {
let entry = entry?;
let file_name = String::from_utf8(entry.header().identifier().to_vec())
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
if !skip(&file_name) {
self.entries
.push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i }));
}
i += 1;
}

// ok, don't skip this
return false;
})
self.src_archives.push((archive_path.to_owned(), archive));
Ok(())
}

fn update_symbols(&mut self) {
Expand Down Expand Up @@ -239,32 +216,3 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
unimplemented!();
}
}

impl<'a> ArArchiveBuilder<'a> {
fn add_archive<F>(&mut self, archive_path: PathBuf, mut skip: F) -> std::io::Result<()>
where
F: FnMut(&str) -> bool + 'static,
{
let mut archive = ar::Archive::new(std::fs::File::open(&archive_path)?);
let archive_index = self.src_archives.len();

let mut i = 0;
while let Some(entry) = archive.next_entry() {
let entry = entry.unwrap();
let file_name = String::from_utf8(entry.header().identifier().to_vec()).unwrap();
if !skip(&file_name) {
self.entries.push((
file_name,
ArchiveEntry::FromArchive {
archive_index,
entry_index: i,
},
));
}
i += 1;
}

self.src_archives.push((archive_path, archive));
Ok(())
}
}
Loading