From a023e29d5187b1a751fd3bdb3a83e6857d583728 Mon Sep 17 00:00:00 2001 From: mlokr Date: Mon, 2 Sep 2024 03:21:39 +0200 Subject: [PATCH] fixing inlining related bugs --- hblang/README.md | 37 +++++++++++++ hblang/src/codegen.rs | 26 ++++++---- hblang/tests/codegen_tests_inline_test.txt | 0 rustc-ice-2024-09-02T01_00_44-1001645.txt | 60 ++++++++++++++++++++++ 4 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 hblang/tests/codegen_tests_inline_test.txt create mode 100644 rustc-ice-2024-09-02T01_00_44-1001645.txt diff --git a/hblang/README.md b/hblang/README.md index 0d7bdee..42ba7aa 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -626,3 +626,40 @@ min := fn(a: int, b: int): int { return b + (c & c >> SHIFT) } ``` + +#### inline_test +```hb +Point := struct {x: int, y: int} +Buffer := struct {} +ColorBGRA := Point + +line := fn(buffer: Buffer, p0: Point, p1: Point, color: ColorBGRA, thickness: int): void { + if true { + if p0.x > p1.x { + @inline(line_low, buffer, p1, p0, color) + } else { + @inline(line_low, buffer, p0, p1, color) + } + } else { + if p0.y > p1.y { + @inline(line_high, buffer, p1, p0, color) + } else { + @inline(line_high, buffer, p0, p1, color) + } + } + return +} + +line_low := fn(buffer: Buffer, p0: Point, p1: Point, color: ColorBGRA): void { + return +} + +line_high := fn(buffer: Buffer, p0: Point, p1: Point, color: ColorBGRA): void { + return +} + +main := fn(): int { + line(.(), .(0, 0), .(0, 0), .(0, 0), 10) + return +} +``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 8d7836e..a1f73ce 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -637,7 +637,7 @@ impl Loc { fn get_reg(&self) -> reg::Id { match self { Self::Rt { reg, .. } => reg.as_ref(), - _ => unreachable!(), + _ => reg::Id::from(0), } } @@ -744,6 +744,7 @@ struct ItemCtx { id: ty::Kind, ret: Option, ret_reg: reg::Id, + inline_ret_loc: Loc, task_base: usize, snap: Snapshot, @@ -1535,17 +1536,17 @@ impl Codegen { let ret_reloc_base = self.ci.ret_relocs.len(); - let loc = self.alloc_ret(sig.ret, ctx, false); - let prev_ret_reg = std::mem::replace(&mut self.ci.ret_reg, loc.get_reg()); + let loc = self.alloc_ret(sig.ret, ctx, true); + let prev_ret_reg = std::mem::replace(&mut self.ci.inline_ret_loc, loc); self.expr(body); - self.ci.ret_reg = prev_ret_reg; + let loc = std::mem::replace(&mut self.ci.inline_ret_loc, prev_ret_reg); - //if let Some(last_ret) = self.ci.ret_relocs.last() - // && last_ret.offset as usize + self.ci.snap.code == self.output.code.len() - 5 - //{ - // self.output.code.truncate(self.output.code.len() - 5); - // self.ci.ret_relocs.pop(); - //} + if let Some(last_ret) = self.ci.ret_relocs.last() + && last_ret.offset as usize + self.ci.snap.code == self.output.code.len() - 5 + { + self.output.code.truncate(self.output.code.len() - 5); + self.ci.ret_relocs.pop(); + } let len = self.output.code.len() as u32; for mut rel in self.ci.ret_relocs.drain(ret_reloc_base..) { rel.offset += self.ci.snap.code as u32; @@ -1975,6 +1976,9 @@ impl Codegen { if let Some(val) = val { let size = self.ci.ret.map_or(17, |ty| self.tys.size_of(ty)); let loc = match size { + _ if self.ci.inline_ret_loc != Loc::default() => { + Some(self.ci.inline_ret_loc.as_ref()) + } 0 => None, 1..=16 => Some(Loc::reg(1)), _ => Some(Loc::reg(self.ci.ret_reg.as_ref()).into_derefed()), @@ -2006,6 +2010,7 @@ impl Codegen { let jump_offset = self.local_offset(); self.output.emit(jeq(reg.get(), 0, 0)); self.ci.free_loc(cond.loc); + self.ci.regs.free(reg); log::dbg!("if-then"); let then_unreachable = self.expr(then).is_none(); @@ -3465,5 +3470,6 @@ mod tests { // structs_in_registers => README; comptime_function_from_another_file => README; inline => README; + inline_test => README; } } diff --git a/hblang/tests/codegen_tests_inline_test.txt b/hblang/tests/codegen_tests_inline_test.txt new file mode 100644 index 0000000..e69de29 diff --git a/rustc-ice-2024-09-02T01_00_44-1001645.txt b/rustc-ice-2024-09-02T01_00_44-1001645.txt new file mode 100644 index 0000000..6642437 --- /dev/null +++ b/rustc-ice-2024-09-02T01_00_44-1001645.txt @@ -0,0 +1,60 @@ +thread 'main' panicked at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/thread/mod.rs:1592:40: +called `Option::unwrap()` on a `None` value +stack backtrace: + 0: 0x72eebd94e655 - std::backtrace_rs::backtrace::libunwind::trace::h80e3b94cbd6b7880 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5 + 1: 0x72eebd94e655 - std::backtrace_rs::backtrace::trace_unsynchronized::h16ad4e2ce618cca4 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 + 2: 0x72eebd94e655 - std::backtrace::Backtrace::create::h2237e34e4d94fd3f + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/backtrace.rs:331:13 + 3: 0x72eebd94e5a5 - std::backtrace::Backtrace::force_capture::h546bf0691b546a53 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/backtrace.rs:312:9 + 4: 0x72eeb9e08481 - std[4f18a6f40454bd49]::panicking::update_hook::>::{closure#0} + 5: 0x72eebd96967f - as core::ops::function::Fn>::call::haa44c2956b1bd51c + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/alloc/src/boxed.rs:2078:9 + 6: 0x72eebd96967f - std::panicking::rust_panic_with_hook::hbf6178baa52721f9 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:804:13 + 7: 0x72eebd969273 - std::panicking::begin_panic_handler::{{closure}}::hab58fc1731670d4c + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:663:13 + 8: 0x72eebd966af9 - std::sys::backtrace::__rust_end_short_backtrace::h5237252f6772769b + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/sys/backtrace.rs:171:18 + 9: 0x72eebd968f34 - rust_begin_unwind + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:661:5 + 10: 0x72eebd9b22e3 - core::panicking::panic_fmt::hea2003cd03a74d6c + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/core/src/panicking.rs:74:14 + 11: 0x72eebd9b236c - core::panicking::panic::hb580cc4fc421f3c8 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/core/src/panicking.rs:148:5 + 12: 0x72eebd9b20d9 - core::option::unwrap_failed::ha8ca2f24e398bc62 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/core/src/option.rs:2013:5 + 13: 0x72eebc2d9e4f - rustc_driver_impl[af16f08a9d11636]::run_compiler + 14: 0x72eebc2d7bec - rustc_driver_impl[af16f08a9d11636]::main + 15: 0x5b4126957c07 - rustc_main[ab1568c316c01211]::main + 16: 0x5b4126957bf3 - std[4f18a6f40454bd49]::sys::backtrace::__rust_begin_short_backtrace:: + 17: 0x5b4126957be9 - ::{closure#0} as core[bcb08d550d2d303]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} + 18: 0x72eebd94b43d - core::ops::function::impls:: for &F>::call_once::h8ee6b536c2e4e076 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/core/src/ops/function.rs:284:13 + 19: 0x72eebd94b43d - std::panicking::try::do_call::h5c8c98de8ed5bd5b + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:553:40 + 20: 0x72eebd94b43d - std::panicking::try::h6315052de0e5fa0e + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:517:19 + 21: 0x72eebd94b43d - std::panic::catch_unwind::h1530d3793f92a4bb + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panic.rs:350:14 + 22: 0x72eebd94b43d - std::rt::lang_start_internal::{{closure}}::he545ff4063dfc2c8 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/rt.rs:141:48 + 23: 0x72eebd94b43d - std::panicking::try::do_call::h09c77e8b42da26d9 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:553:40 + 24: 0x72eebd94b43d - std::panicking::try::h7a9b2c58b7302b3b + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panicking.rs:517:19 + 25: 0x72eebd94b43d - std::panic::catch_unwind::h464a2cd7183a7af5 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/panic.rs:350:14 + 26: 0x72eebd94b43d - std::rt::lang_start_internal::h99fdbebdafe8d634 + at /rustc/20ae37c18df95f9246c019b04957d23b4164bf7a/library/std/src/rt.rs:141:20 + 27: 0x5b4126957d27 - main + 28: 0x72eeb6834e08 - + 29: 0x72eeb6834ecc - __libc_start_main + 30: 0x5b4126957c30 - + 31: 0x0 - + + +rustc version: 1.81.0-nightly (20ae37c18 2024-07-07) +platform: x86_64-unknown-linux-gnu \ No newline at end of file