Skip to content

Commit 6c5a70d

Browse files
authored
Merge pull request #244 from rust-lang/feature/unwinding
Implement unwinding
2 parents 7c1d21c + 8e77fbf commit 6c5a70d

20 files changed

+388
-97
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
libgccjit_version:
22-
- { gcc: "libgccjit.so", extra: "", artifacts_branch: "master" }
23-
- { gcc: "libgccjit_without_int128.so", extra: "", artifacts_branch: "master-without-128bit-integers" }
24-
- { gcc: "libgccjit12.so", extra: "--no-default-features", artifacts_branch: "gcc12" }
22+
- { gcc: "libgccjit.so", extra: "", env_extra: "", artifacts_branch: "master" }
23+
- { gcc: "libgccjit_without_int128.so", extra: "", env_extra: "", artifacts_branch: "master-without-128bit-integers" }
24+
- { gcc: "libgccjit12.so", extra: "--no-default-features", env_extra: "TEST_FLAGS='-Cpanic=abort -Zpanic-abort-tests'", artifacts_branch: "gcc12" }
2525
commands: [
2626
"--mini-tests",
2727
"--std-tests",
@@ -120,8 +120,8 @@ jobs:
120120
- name: Build
121121
run: |
122122
./prepare_build.sh
123-
./build.sh ${{ matrix.libgccjit_version.extra }}
124-
cargo test ${{ matrix.libgccjit_version.extra }}
123+
${{ matrix.libgccjit_version.env_extra }} ./build.sh ${{ matrix.libgccjit_version.extra }}
124+
${{ matrix.libgccjit_version.env_extra }} cargo test ${{ matrix.libgccjit_version.extra }}
125125
./clean_all.sh
126126
127127
- name: Prepare dependencies
@@ -143,7 +143,7 @@ jobs:
143143

144144
- name: Run tests
145145
run: |
146-
./test.sh --release --clean --build-sysroot ${{ matrix.commands }} ${{ matrix.libgccjit_version.extra }}
146+
${{ matrix.libgccjit_version.env_extra }} ./test.sh --release --clean --build-sysroot ${{ matrix.commands }} ${{ matrix.libgccjit_version.extra }}
147147
148148
duplicates:
149149
runs-on: ubuntu-latest

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,26 @@ To print a debug representation of a tree:
162162
debug_tree(expr);
163163
```
164164

165+
(defined in print-tree.h)
166+
167+
To print a debug reprensentation of a gimple struct:
168+
169+
```c
170+
debug_gimple_stmt(gimple_struct)
171+
```
172+
165173
To get the `rustc` command to run in `gdb`, add the `--verbose` flag to `cargo build`.
166174
175+
To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++-v3/libsupc++/eh_personality.cc`, TODO
176+
177+
Maybe by calling the following at the beginning of gdb:
178+
179+
```
180+
set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
181+
```
182+
183+
TODO: but that's not what I remember I was doing.
184+
167185
### How to use a custom-build rustc
168186
169187
* Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).

build_sysroot/build_sysroot.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rm Cargo.lock test_target/Cargo.lock 2>/dev/null || true
1616
rm -r sysroot/ 2>/dev/null || true
1717

1818
# Build libs
19-
export RUSTFLAGS="$RUSTFLAGS -Z force-unstable-if-unmarked -Cpanic=abort"
19+
export RUSTFLAGS="$RUSTFLAGS -Z force-unstable-if-unmarked"
2020
if [[ "$1" == "--release" ]]; then
2121
sysroot_channel='release'
2222
RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=3" cargo build --target $TARGET_TRIPLE --release

config.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
3838
fi
3939
fi
4040

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

4343
# FIXME(antoyo): remove once the atomic shim is gone
4444
if [[ `uname` == 'Darwin' ]]; then

example/alloc_example.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler)]
1+
#![feature(start, box_syntax, core_intrinsics, alloc_error_handler, lang_items)]
22
#![no_std]
33

44
extern crate alloc;
@@ -26,6 +26,16 @@ fn alloc_error_handler(_: alloc::alloc::Layout) -> ! {
2626
core::intrinsics::abort();
2727
}
2828

29+
#[lang = "eh_personality"]
30+
fn eh_personality() -> ! {
31+
loop {}
32+
}
33+
34+
#[no_mangle]
35+
unsafe extern "C" fn _Unwind_Resume() {
36+
core::intrinsics::unreachable();
37+
}
38+
2939
#[start]
3040
fn main(_argc: isize, _argv: *const *const u8) -> isize {
3141
let world: Box<&str> = box "Hello World!\0";

failing-ui-tests.txt

+26
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,29 @@ src/test/ui/sse2.rs
4141
src/test/ui/statics/issue-91050-1.rs
4242
src/test/ui/statics/issue-91050-2.rs
4343
src/test/ui/target-feature/missing-plusminus.rs
44+
src/test/ui/asm/x86_64/may_unwind.rs
45+
src/test/ui/backtrace.rs
46+
src/test/ui/catch-unwind-bang.rs
47+
src/test/ui/cfg/cfg-panic-abort.rs
48+
src/test/ui/drop/dynamic-drop-async.rs
49+
src/test/ui/drop/repeat-drop.rs
50+
src/test/ui/fmt/format-args-capture.rs
51+
src/test/ui/generator/panic-drops-resume.rs
52+
src/test/ui/generator/panic-drops.rs
53+
src/test/ui/generator/panic-safe.rs
54+
src/test/ui/intrinsics/panic-uninitialized-zeroed.rs
55+
src/test/ui/issues/issue-14875.rs
56+
src/test/ui/issues/issue-29948.rs
57+
src/test/ui/issues/issue-43853.rs
58+
src/test/ui/iterators/iter-sum-overflow-debug.rs
59+
src/test/ui/iterators/iter-sum-overflow-overflow-checks.rs
60+
src/test/ui/mir/mir_calls_to_shims.rs
61+
src/test/ui/mir/mir_drop_order.rs
62+
src/test/ui/mir/mir_let_chains_drop_order.rs
63+
src/test/ui/oom_unwind.rs
64+
src/test/ui/panic-runtime/abort-link-to-unwinding-crates.rs
65+
src/test/ui/panic-runtime/abort.rs
66+
src/test/ui/panic-runtime/link-to-abort.rs
67+
src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
68+
src/test/ui/rfcs/rfc1857-drop-order.rs
69+
src/test/ui/unwind-no-uwtable.rs

failing-ui-tests12.txt

+9
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ src/test/ui/simd/intrinsic/inlining-issue67557.rs
2222
src/test/ui/simd/monomorphize-shuffle-index.rs
2323
src/test/ui/simd/shuffle.rs
2424
src/test/ui/simd/simd-bitmask.rs
25+
src/test/ui/binding/fn-arg-incomplete-pattern-drop-order.rs
26+
src/test/ui/drop/dynamic-drop.rs
27+
src/test/ui/generator/resume-after-return.rs
28+
src/test/ui/iterators/iter-step-overflow-debug.rs
29+
src/test/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs
30+
src/test/ui/numbers-arithmetic/next-power-of-two-overflow-debug.rs
31+
src/test/ui/panic-while-printing.rs
32+
src/test/ui/privacy/reachable-unnameable-items.rs
33+
src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs

src/asm.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
352352
inputs.push(AsmInOperand {
353353
constraint: "X".into(),
354354
rust_idx,
355-
val: self.cx.rvalue_as_function(get_fn(self.cx, instance))
356-
.get_address(None),
355+
val: get_fn(self.cx, instance).get_address(None),
357356
});
358357
}
359358

@@ -739,7 +738,7 @@ impl<'gcc, 'tcx> AsmMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
739738
}
740739

741740
GlobalAsmOperandRef::SymFn { instance } => {
742-
let function = self.rvalue_as_function(get_fn(self, instance));
741+
let function = get_fn(self, instance);
743742
self.add_used_function(function);
744743
// TODO(@Amanieu): Additional mangling is needed on
745744
// some targets to add a leading underscore (Mach-O)

src/base.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_
8787
// Instantiate monomorphizations without filling out definitions yet...
8888
//let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
8989
let context = Context::default();
90+
91+
context.add_command_line_option("-fexceptions");
92+
context.add_driver_option("-fexceptions");
93+
9094
// TODO(antoyo): only set on x86 platforms.
9195
context.add_command_line_option("-masm=intel");
9296
// TODO(antoyo): only add the following cli argument if the feature is supported.
@@ -146,7 +150,7 @@ pub fn compile_codegen_unit<'tcx>(tcx: TyCtxt<'tcx>, cgu_name: Symbol, supports_
146150
context.set_keep_intermediates(true);
147151
}
148152

149-
// TODO(bjorn3): Remove once unwinding is properly implemented
153+
// NOTE: The codegen generates unrechable blocks.
150154
context.set_allow_unreachable_blocks(true);
151155

152156
{

src/builder.rs

+77-9
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,11 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
372372
}
373373
}
374374

375-
impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
375+
impl<'a, 'gcc, 'tcx> Deref for Builder<'a, 'gcc, 'tcx> {
376376
type Target = CodegenCx<'gcc, 'tcx>;
377377

378-
fn deref(&self) -> &Self::Target {
378+
fn deref<'b>(&'b self) -> &'a Self::Target
379+
{
379380
self.cx
380381
}
381382
}
@@ -393,7 +394,7 @@ impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
393394
}
394395

395396
impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
396-
fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
397+
fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Builder<'a, 'gcc, 'tcx> {
397398
Builder::with_cx(cx, block)
398399
}
399400

@@ -450,8 +451,36 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
450451
self.block.end_with_switch(None, value, default_block, &gcc_cases);
451452
}
452453

454+
#[cfg(feature="master")]
455+
fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
456+
let try_block = self.current_func().new_block("try");
457+
458+
let current_block = self.block.clone();
459+
self.block = try_block;
460+
let call = self.call(typ, func, args, None); // TODO(antoyo): use funclet here?
461+
self.block = current_block;
462+
463+
let return_value = self.current_func()
464+
.new_local(None, call.get_type(), "invokeResult");
465+
466+
try_block.add_assignment(None, return_value, call);
467+
468+
try_block.end_with_jump(None, then);
469+
470+
if self.cleanup_blocks.borrow().contains(&catch) {
471+
self.block.add_try_finally(None, try_block, catch);
472+
}
473+
else {
474+
self.block.add_try_catch(None, try_block, catch);
475+
}
476+
477+
self.block.end_with_jump(None, then);
478+
479+
return_value.to_rvalue()
480+
}
481+
482+
#[cfg(not(feature="master"))]
453483
fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
454-
// TODO(bjorn3): Properly implement unwinding.
455484
let call_site = self.call(typ, func, args, None);
456485
let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
457486
self.llbb().end_with_conditional(None, condition, then, catch);
@@ -1161,22 +1190,61 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
11611190
}
11621191

11631192
fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1164-
// TODO(antoyo)
1193+
#[cfg(feature="master")]
1194+
{
1195+
let personality = self.rvalue_as_function(_personality);
1196+
self.current_func().set_personality_function(personality);
1197+
}
11651198
}
11661199

1200+
#[cfg(feature="master")]
1201+
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, pers_fn: RValue<'gcc>) -> RValue<'gcc> {
1202+
self.set_personality_fn(pers_fn);
1203+
1204+
// NOTE: insert the current block in a variable so that a later call to invoke knows to
1205+
// generate a try/finally instead of a try/catch for this block.
1206+
self.cleanup_blocks.borrow_mut().insert(self.block);
1207+
1208+
let eh_pointer_builtin = self.cx.context.get_target_builtin_function("__builtin_eh_pointer");
1209+
let zero = self.cx.context.new_rvalue_zero(self.int_type);
1210+
let ptr = self.cx.context.new_call(None, eh_pointer_builtin, &[zero]);
1211+
1212+
let field1_type = self.u8_type.make_pointer();
1213+
let field1 = self.context.new_field(None, field1_type, "landing_pad_field_1");
1214+
let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_2");
1215+
let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1216+
let value = self.current_func().new_local(None, struct_type.as_type(), "landing_pad");
1217+
let ptr = self.cx.context.new_cast(None, ptr, field1_type);
1218+
self.block.add_assignment(None, value.access_field(None, field1), ptr);
1219+
self.block.add_assignment(None, value.access_field(None, field2), zero); // TODO: set the proper value here (the type of exception?).
1220+
1221+
value.to_rvalue()
1222+
}
1223+
1224+
#[cfg(not(feature="master"))]
11671225
fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
11681226
let field1 = self.context.new_field(None, self.u8_type.make_pointer(), "landing_pad_field_1");
11691227
let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
11701228
let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
11711229
self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
11721230
.to_rvalue()
1173-
// TODO(antoyo): Properly implement unwinding.
1174-
// the above is just to make the compilation work as it seems
1175-
// rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
11761231
}
11771232

1233+
#[cfg(feature="master")]
1234+
fn resume(&mut self, exn: RValue<'gcc>) {
1235+
// TODO: check if this is normal that we need to dereference the value.
1236+
// NOTE: the type is wrong, so in order to get a pointer for parameter, cast it to a
1237+
// pointer of pointer that is later dereferenced.
1238+
let exn_type = exn.get_type().make_pointer();
1239+
let exn = self.context.new_cast(None, exn, exn_type);
1240+
let exn = exn.dereference(None).to_rvalue();
1241+
let unwind_resume = self.context.get_target_builtin_function("__builtin_unwind_resume");
1242+
self.llbb().add_eval(None, self.context.new_call(None, unwind_resume, &[exn]));
1243+
self.unreachable();
1244+
}
1245+
1246+
#[cfg(not(feature="master"))]
11781247
fn resume(&mut self, _exn: RValue<'gcc>) {
1179-
// TODO(bjorn3): Properly implement unwinding.
11801248
self.unreachable();
11811249
}
11821250

src/callee.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#[cfg(feature="master")]
22
use gccjit::{FnAttribute, Visibility};
3-
use gccjit::{FunctionType, RValue};
4-
use rustc_codegen_ssa::traits::BaseTypeMethods;
3+
use gccjit::{FunctionType, Function};
54
use rustc_middle::ty::{self, Instance, TypeVisitable};
65
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt};
76

8-
use crate::abi::FnAbiGccExt;
97
use crate::attributes;
108
use crate::context::CodegenCx;
119

@@ -16,22 +14,26 @@ use crate::context::CodegenCx;
1614
///
1715
/// - `cx`: the crate context
1816
/// - `instance`: the instance to be instantiated
19-
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> RValue<'gcc> {
17+
pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>) -> Function<'gcc> {
2018
let tcx = cx.tcx();
2119

2220
assert!(!instance.substs.needs_infer());
2321
assert!(!instance.substs.has_escaping_bound_vars());
2422

23+
let sym = tcx.symbol_name(instance).name;
24+
2525
if let Some(&func) = cx.function_instances.borrow().get(&instance) {
2626
return func;
2727
}
2828

29-
let sym = tcx.symbol_name(instance).name;
30-
3129
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
3230

3331
let func =
34-
if let Some(func) = cx.get_declared_value(&sym) {
32+
if let Some(_func) = cx.get_declared_value(&sym) {
33+
// FIXME: we never reach this because get_declared_value only returns global variables
34+
// and here we try to get a function.
35+
unreachable!();
36+
/*
3537
// Create a fn pointer with the new signature.
3638
let ptrty = fn_abi.ptr_to_gcc_type(cx);
3739
@@ -64,7 +66,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>)
6466
}
6567
else {
6668
func
67-
}
69+
}*/
6870
}
6971
else {
7072
cx.linkage.set(FunctionType::Extern);
@@ -163,8 +165,7 @@ pub fn get_fn<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, instance: Instance<'tcx>)
163165
}
164166
}
165167

166-
// FIXME(antoyo): this is a wrong cast. That requires changing the compiler API.
167-
unsafe { std::mem::transmute(func) }
168+
func
168169
};
169170

170171
cx.function_instances.borrow_mut().insert(instance, func);

0 commit comments

Comments
 (0)