Skip to content

Commit 7f32dd5

Browse files
committed
Merge commit '9809f5d21990d9e24b3e9876ea7da756fd4e9def' into libgccjit-codegen
2 parents df48731 + 9809f5d commit 7f32dd5

19 files changed

+559
-559
lines changed

compiler/rustc_codegen_gcc/Cargo.lock

+19-19
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
2525

2626
[[package]]
2727
name = "bitflags"
28-
version = "1.2.1"
28+
version = "1.3.2"
2929
source = "registry+https://github.com/rust-lang/crates.io-index"
30-
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
30+
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
3131

3232
[[package]]
3333
name = "cfg-if"
@@ -56,15 +56,15 @@ dependencies = [
5656
[[package]]
5757
name = "gccjit"
5858
version = "1.0.0"
59-
source = "git+https://github.com/antoyo/gccjit.rs#54be27e41fff7b6ab532e2e21a82df50a12b9ad3"
59+
source = "git+https://github.com/antoyo/gccjit.rs#2d4fea7319f80531b2e5d264fca9f1c498a3a62e"
6060
dependencies = [
6161
"gccjit_sys",
6262
]
6363

6464
[[package]]
6565
name = "gccjit_sys"
6666
version = "0.0.1"
67-
source = "git+https://github.com/antoyo/gccjit.rs#54be27e41fff7b6ab532e2e21a82df50a12b9ad3"
67+
source = "git+https://github.com/antoyo/gccjit.rs#2d4fea7319f80531b2e5d264fca9f1c498a3a62e"
6868
dependencies = [
6969
"libc 0.1.12",
7070
]
@@ -85,7 +85,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
8585
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
8686
dependencies = [
8787
"cfg-if",
88-
"libc 0.2.98",
88+
"libc 0.2.102",
8989
"wasi",
9090
]
9191

@@ -101,7 +101,7 @@ version = "0.1.19"
101101
source = "registry+https://github.com/rust-lang/crates.io-index"
102102
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
103103
dependencies = [
104-
"libc 0.2.98",
104+
"libc 0.2.102",
105105
]
106106

107107
[[package]]
@@ -122,7 +122,7 @@ checksum = "96bd995a092cac79868250589869b5a5d656b02a02bd74c8ebdc566dc7203090"
122122
dependencies = [
123123
"fm",
124124
"getopts",
125-
"libc 0.2.98",
125+
"libc 0.2.102",
126126
"num_cpus",
127127
"termcolor",
128128
"threadpool",
@@ -138,15 +138,15 @@ checksum = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122"
138138

139139
[[package]]
140140
name = "libc"
141-
version = "0.2.98"
141+
version = "0.2.102"
142142
source = "registry+https://github.com/rust-lang/crates.io-index"
143-
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
143+
checksum = "a2a5ac8f984bfcf3a823267e5fde638acc3325f6496633a5da6bb6eb2171e103"
144144

145145
[[package]]
146146
name = "memchr"
147-
version = "2.4.0"
147+
version = "2.4.1"
148148
source = "registry+https://github.com/rust-lang/crates.io-index"
149-
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
149+
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
150150

151151
[[package]]
152152
name = "num_cpus"
@@ -155,7 +155,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
155155
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
156156
dependencies = [
157157
"hermit-abi",
158-
"libc 0.2.98",
158+
"libc 0.2.102",
159159
]
160160

161161
[[package]]
@@ -181,7 +181,7 @@ version = "0.8.4"
181181
source = "registry+https://github.com/rust-lang/crates.io-index"
182182
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
183183
dependencies = [
184-
"libc 0.2.98",
184+
"libc 0.2.102",
185185
"rand_chacha",
186186
"rand_core",
187187
"rand_hc",
@@ -217,9 +217,9 @@ dependencies = [
217217

218218
[[package]]
219219
name = "redox_syscall"
220-
version = "0.2.9"
220+
version = "0.2.10"
221221
source = "registry+https://github.com/rust-lang/crates.io-index"
222-
checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee"
222+
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
223223
dependencies = [
224224
"bitflags",
225225
]
@@ -284,7 +284,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
284284
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
285285
dependencies = [
286286
"cfg-if",
287-
"libc 0.2.98",
287+
"libc 0.2.102",
288288
"rand",
289289
"redox_syscall",
290290
"remove_dir_all",
@@ -311,17 +311,17 @@ dependencies = [
311311

312312
[[package]]
313313
name = "unicode-width"
314-
version = "0.1.8"
314+
version = "0.1.9"
315315
source = "registry+https://github.com/rust-lang/crates.io-index"
316-
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
316+
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
317317

318318
[[package]]
319319
name = "wait-timeout"
320320
version = "0.2.0"
321321
source = "registry+https://github.com/rust-lang/crates.io-index"
322322
checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6"
323323
dependencies = [
324-
"libc 0.2.98",
324+
"libc 0.2.102",
325325
]
326326

327327
[[package]]

compiler/rustc_codegen_gcc/Readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,21 @@ p loc->m_line
115115
116116
* Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
117117
* Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.
118+
119+
### How to build a cross-compiling libgccjit
120+
121+
#### Building libgccjit
122+
123+
* Follow these instructions: https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/ with the following changes:
124+
* Configure gcc with `../gcc/configure --enable-host-shared --disable-multilib --enable-languages=c,jit,c++ --disable-bootstrap --enable-checking=release --prefix=/opt/m68k-gcc/ --target=m68k-linux --without-headers`.
125+
* Some shells, like fish, don't define the environment variable `$MACHTYPE`.
126+
* Add `CFLAGS="-Wno-error=attributes -g -O2"` at the end of the configure command for building glibc (`CFLAGS="-Wno-error=attributes -Wno-error=array-parameter -Wno-error=stringop-overflow -Wno-error=array-bounds -g -O2"` for glibc 2.31, which is useful for Debian).
127+
128+
#### Configuring rustc_codegen_gcc
129+
130+
* Set `TARGET_TRIPLE="m68k-unknown-linux-gnu"` in config.sh.
131+
* Since rustc doesn't support this architecture yet, set it back to `TARGET_TRIPLE="mips-unknown-linux-gnu"` (or another target having the same attributes). Alternatively, create a [target specification file](https://book.avr-rust.com/005.1-the-target-specification-json-file.html) (note that the `arch` specified in this file must be supported by the rust compiler).
132+
* Set `linker='-Clinker=m68k-linux-gcc'`.
133+
* Set the path to the cross-compiling libgccjit in `gcc_path`.
134+
* Disable the 128-bit integer types if the target doesn't support them by using `let i128_type = context.new_type::<i64>();` in `context.rs` (same for u128_type).
135+
* (might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?).

compiler/rustc_codegen_gcc/config.sh

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ fi
2121

2222
HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ")
2323
TARGET_TRIPLE=$HOST_TRIPLE
24-
#TARGET_TRIPLE="aarch64-unknown-linux-gnu"
24+
#TARGET_TRIPLE="m68k-unknown-linux-gnu"
2525

2626
linker=''
2727
RUN_WRAPPER=''
2828
if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
29-
if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then
29+
if [[ "$TARGET_TRIPLE" == "m68k-unknown-linux-gnu" ]]; then
30+
TARGET_TRIPLE="mips-unknown-linux-gnu"
31+
linker='-Clinker=m68k-linux-gcc'
32+
elif [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then
3033
# We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
3134
linker='-Clinker=aarch64-linux-gnu-gcc'
3235
RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu'
@@ -35,7 +38,7 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
3538
fi
3639
fi
3740

38-
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"
41+
export RUSTFLAGS="$linker -Cpanic=abort -Zsymbol-mangling-version=v0 -Cdebuginfo=2 -Clto=off -Zpanic-abort-tests -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot"
3942

4043
# FIXME(antoyo): remove once the atomic shim is gone
4144
if [[ `uname` == 'Darwin' ]]; then
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2021-09-17
1+
nightly-2021-09-28

compiler/rustc_codegen_gcc/src/asm.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_codegen_ssa::traits::{AsmBuilderMethods, AsmMethods, BaseTypeMethods,
66

77
use rustc_hir::LlvmInlineAsmInner;
88
use rustc_middle::{bug, ty::Instance};
9-
use rustc_span::Span;
9+
use rustc_span::{Span, Symbol};
1010
use rustc_target::asm::*;
1111

1212
use std::borrow::Cow;
@@ -173,7 +173,20 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
173173
continue
174174
},
175175
(Register(reg_name), None) => {
176-
clobbers.push(reg_name);
176+
// `clobber_abi` can add lots of clobbers that are not supported by the target,
177+
// such as AVX-512 registers, so we just ignore unsupported registers
178+
let is_target_supported = reg.reg_class().supported_types(asm_arch).iter()
179+
.any(|&(_, feature)| {
180+
if let Some(feature) = feature {
181+
self.tcx.sess.target_features.contains(&Symbol::intern(feature))
182+
} else {
183+
true // Register class is unconditionally supported
184+
}
185+
});
186+
187+
if is_target_supported && !clobbers.contains(&reg_name) {
188+
clobbers.push(reg_name);
189+
}
177190
continue
178191
}
179192
};
@@ -526,16 +539,20 @@ fn reg_to_gcc(reg: InlineAsmRegOrRegClass) -> ConstraintOrRegister {
526539
let constraint = match reg {
527540
// For vector registers LLVM wants the register name to match the type size.
528541
InlineAsmRegOrRegClass::Reg(reg) => {
529-
// TODO(antoyo): add support for vector register.
530-
match reg.name() {
531-
"ax" => "a",
532-
"bx" => "b",
533-
"cx" => "c",
534-
"dx" => "d",
535-
"si" => "S",
536-
"di" => "D",
537-
// For registers like r11, we have to create a register variable: https://stackoverflow.com/a/31774784/389119
538-
name => return ConstraintOrRegister::Register(name),
542+
match reg {
543+
InlineAsmReg::X86(_) => {
544+
// TODO(antoyo): add support for vector register.
545+
//
546+
// // For explicit registers, we have to create a register variable: https://stackoverflow.com/a/31774784/389119
547+
return ConstraintOrRegister::Register(match reg.name() {
548+
// Some of registers' names does not map 1-1 from rust to gcc
549+
"st(0)" => "st",
550+
551+
name => name,
552+
});
553+
}
554+
555+
_ => unimplemented!(),
539556
}
540557
},
541558
InlineAsmRegOrRegClass::RegClass(reg) => match reg {

compiler/rustc_codegen_gcc/src/base.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::env;
2-
use std::sync::Once;
32
use std::time::Instant;
43

54
use gccjit::{
65
Context,
76
FunctionType,
87
GlobalKind,
98
};
10-
use rustc_hir::def_id::LOCAL_CRATE;
119
use rustc_middle::dep_graph;
1210
use rustc_middle::middle::cstore::EncodedMetadata;
1311
use rustc_middle::middle::exported_symbols;
@@ -20,7 +18,7 @@ use rustc_codegen_ssa::traits::DebugInfoMethods;
2018
use rustc_session::config::DebugInfo;
2119
use rustc_span::Symbol;
2220

23-
use crate::{GccContext, create_function_calling_initializers};
21+
use crate::GccContext;
2422
use crate::builder::Builder;
2523
use crate::context::CodegenCx;
2624

@@ -97,15 +95,6 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
9795
{
9896
let cx = CodegenCx::new(&context, cgu, tcx);
9997

100-
static START: Once = Once::new();
101-
START.call_once(|| {
102-
let initializer_name = format!("__gccGlobalCrateInit{}", tcx.crate_name(LOCAL_CRATE));
103-
let func = context.new_function(None, FunctionType::Exported, context.new_type::<()>(), &[], initializer_name, false);
104-
let block = func.new_block("initial");
105-
create_function_calling_initializers(tcx, &context, block);
106-
block.end_with_void_return(None);
107-
});
108-
10998
let mono_items = cgu.items_in_deterministic_order(tcx);
11099
for &(mono_item, (linkage, visibility)) in &mono_items {
111100
mono_item.predefine::<Builder<'_, '_, '_>>(&cx, linkage, visibility);
@@ -124,8 +113,6 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol) -> (Modul
124113
if cx.sess().opts.debuginfo != DebugInfo::None {
125114
cx.debuginfo_finalize();
126115
}
127-
128-
cx.global_init_block.end_with_void_return(None);
129116
}
130117

131118
ModuleCodegen {

compiler/rustc_codegen_gcc/src/builder.rs

+39-7
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ use rustc_codegen_ssa::traits::{
3131
StaticBuilderMethods,
3232
};
3333
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
34-
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
34+
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
3535
use rustc_span::Span;
3636
use rustc_span::def_id::DefId;
3737
use rustc_target::abi::{
3838
self,
39+
call::FnAbi,
3940
Align,
4041
HasDataLayout,
4142
Size,
@@ -347,6 +348,20 @@ impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
347348
}
348349
}
349350

351+
impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
352+
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
353+
354+
#[inline]
355+
fn handle_fn_abi_err(
356+
&self,
357+
err: FnAbiError<'tcx>,
358+
span: Span,
359+
fn_abi_request: FnAbiRequest<'tcx>,
360+
) -> ! {
361+
self.cx.handle_fn_abi_err(err, span, fn_abi_request)
362+
}
363+
}
364+
350365
impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
351366
type Target = CodegenCx<'gcc, 'tcx>;
352367

@@ -505,7 +520,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
505520
// FIXME(antoyo): rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
506521
// should be the same.
507522
let typ = a.get_type().to_signed(self);
508-
let a = self.context.new_cast(None, a, typ);
509523
let b = self.context.new_cast(None, b, typ);
510524
a / b
511525
}
@@ -1052,11 +1066,18 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
10521066
}
10531067

10541068
/* Comparisons */
1055-
fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
1056-
if lhs.get_type() != rhs.get_type() {
1069+
fn icmp(&mut self, op: IntPredicate, mut lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
1070+
let left_type = lhs.get_type();
1071+
let right_type = rhs.get_type();
1072+
if left_type != right_type {
1073+
// NOTE: because libgccjit cannot compare function pointers.
1074+
if left_type.is_function_ptr_type().is_some() && right_type.is_function_ptr_type().is_some() {
1075+
lhs = self.context.new_cast(None, lhs, self.usize_type.make_pointer());
1076+
rhs = self.context.new_cast(None, rhs, self.usize_type.make_pointer());
1077+
}
10571078
// NOTE: hack because we try to cast a vector type to the same vector type.
1058-
if format!("{:?}", lhs.get_type()) != format!("{:?}", rhs.get_type()) {
1059-
rhs = self.context.new_cast(None, rhs, lhs.get_type());
1079+
else if format!("{:?}", left_type) != format!("{:?}", right_type) {
1080+
rhs = self.context.new_cast(None, rhs, left_type);
10601081
}
10611082
}
10621083
self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
@@ -1210,6 +1231,17 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12101231
else {
12111232
panic!("Unexpected type {:?}", value_type);
12121233
};
1234+
1235+
let lvalue_type = lvalue.to_rvalue().get_type();
1236+
let value =
1237+
// NOTE: sometimes, rustc will create a value with the wrong type.
1238+
if lvalue_type != value.get_type() {
1239+
self.context.new_cast(None, value, lvalue_type)
1240+
}
1241+
else {
1242+
value
1243+
};
1244+
12131245
self.llbb().add_assignment(None, lvalue, value);
12141246

12151247
aggregate_value
@@ -1413,7 +1445,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
14131445
impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
14141446
fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
14151447
// Forward to the `get_static` method of `CodegenCx`
1416-
self.cx().get_static(def_id)
1448+
self.cx().get_static(def_id).get_address(None)
14171449
}
14181450
}
14191451

0 commit comments

Comments
 (0)