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

Debug builds of compiler_builtins fail to link due to references to precondition_check in libcore #121552

Closed
Amanieu opened this issue Feb 24, 2024 · 29 comments · Fixed by #121421 or #122580
Assignees
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@Amanieu
Copy link
Member

Amanieu commented Feb 24, 2024

Likely caused by #120594. cc @saethlin

CI for compiler_builtins is failing with:

error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Tools\\MSVC\\14.38.33130\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\rustccNjGuE\\symbols.o" "D:\\a\\compiler-builtins\\compiler-builtins\\target\\x86_64-pc-windows-msvc\\debug\\examples\\intrinsics.intrinsics.94b4869361004a03-cgu.0.rcgu.o" "/LIBPATH:D:\\a\\compiler-builtins\\compiler-builtins\\target\\x86_64-pc-windows-msvc\\debug\\deps" "/LIBPATH:D:\\a\\compiler-builtins\\compiler-builtins\\target\\debug\\deps" "/LIBPATH:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "kernel32.lib" "msvcrt.lib" "D:\\a\\compiler-builtins\\compiler-builtins\\target\\x86_64-pc-windows-msvc\\debug\\deps\\libcompiler_builtins-b84df12ffea9ddc5.rlib" "/NXCOMPAT" "/LIBPATH:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/OUT:D:\\a\\compiler-builtins\\compiler-builtins\\target\\x86_64-pc-windows-msvc\\debug\\examples\\intrinsics.exe" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis"
  = note: libcompiler_builtins-b84df12ffea9ddc5.rlib(compiler_builtins-b84df12ffea9ddc5.compiler_builtins.ca00aaabc6ad635b-cgu.2.rcgu.o) : error LNK2019: unresolved external symbol _ZN4core3ptr13read_volatile18precondition_check17ha1e11d6217b519e2E referenced in function _ZN4core3ptr13read_volatile17h3fd78456f42a46ecE
          libcompiler_builtins-b84df12ffea9ddc5.rlib(compiler_builtins-b84df12ffea9ddc5.compiler_builtins.ca00aaabc6ad635b-cgu.2.rcgu.o) : error LNK2019: unresolved external symbol _ZN4core10intrinsics19copy_nonoverlapping18precondition_check17h5ef2e74b4a074627E referenced in function _ZN4core3ptr14read_unaligned17h327557c7a8527effE
          D:\a\compiler-builtins\compiler-builtins\target\x86_64-pc-windows-msvc\debug\examples\intrinsics.exe : fatal error LNK1120: 2 unresolved externals

The cause seems to be from a combination of factors:

  • compiler-builtins cannot reference any symbols in libcore. It can only call functions marked with #[inline], since those are codegen'ed into compiler_builtins.rlib.
  • precondition_check is marked as #[inline(never)], which breaks this.
  • The call is not eliminated because core::intrinsics::debug_assertions is resolved late in the codegen backend, after any MIR optimizations.
  • This failure happens in debug builds, so LLVM isn't eliminating the if branch either.
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 24, 2024
@Amanieu Amanieu changed the title Debug builds of compiler_builtins fail to link due to references to precondition_check libcore Debug builds of compiler_builtins fail to link due to references to precondition_check in libcore Feb 24, 2024
@RalfJung
Copy link
Member

read_volatile can probably be avoided by just calling the intrinsic directly.

You mentioned copy_nonoverlapping is only called indirectly from compiler-builtins; do you know what the entry point into libcore is?

@saethlin saethlin added A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. labels Feb 24, 2024
@saethlin
Copy link
Member

I've verified that this is fixed by my latest iteration of #121421, which is neatly part of my plan for these checks: #120848

@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 24, 2024
@saethlin saethlin self-assigned this Feb 24, 2024
@saethlin saethlin linked a pull request Feb 24, 2024 that will close this issue
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 25, 2024
Avoid lowering code under dead SwitchInt targets

The objective of this PR is to detect and eliminate code which is guarded by an `if false`, even if that `false` is a constant which is not known until monomorphization, or is `intrinsics::debug_assertions()`.

The effect of this is that we generate no LLVM IR the standard library's unsafe preconditions, when they are compiled in a build where they should be immediately optimized out. This mono-time optimization ensures that builds which disable debug assertions do not grow a linkage requirement against `core`, which compiler-builtins currently needs: rust-lang#121552

This revives the codegen side of rust-lang#91222 as planned in rust-lang#120848.
@Amanieu Amanieu added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 12, 2024
@apiraino
Copy link
Contributor

Hm didn't notice this because it was not marked as a regression.

Assigning P-high (Zulip discussion) after reading @Amanieu comment (and following).

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Mar 12, 2024
@RalfJung
Copy link
Member

It's a nightly-to-nightly regression, I don't think we have a label for that? Nightly features can break after all. It's just that compiler-builtins relies on an incredibly fragile undocumented contract that is not checked by rustc CI.

@apiraino
Copy link
Contributor

It's a nightly-to-nightly regression, I don't think we have a label for that? Nightly features can break after all. It's just that compiler-builtins relies on an incredibly fragile undocumented contract that is not checked by rustc CI.

Thanks for the additional context. IIUC in this case it's nothing risking to reach beta if it's shielded by a nightly-only flag.

@rustbot label -I-prioritize -P-high +P-medium

@rustbot rustbot added P-medium Medium priority and removed P-high High priority labels Mar 12, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 13, 2024
Avoid lowering code under dead SwitchInt targets

The objective of this PR is to detect and eliminate code which is guarded by an `if false`, even if that `false` is a constant which is not known until monomorphization, or is `intrinsics::debug_assertions()`.

The effect of this is that we generate no LLVM IR the standard library's unsafe preconditions, when they are compiled in a build where they should be immediately optimized out. This mono-time optimization ensures that builds which disable debug assertions do not grow a linkage requirement against `core`, which compiler-builtins currently needs: rust-lang#121552

This revives the codegen side of rust-lang#91222 as planned in rust-lang#120848.
@saethlin saethlin reopened this Mar 13, 2024
@saethlin
Copy link
Member

See #121421 (comment)

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

I checked this with the latest nightly: it seems new failures are due to copy_nonoverlapping and unreachable_unchecked:

RELOCATION RECORDS FOR [.text.core::intrinsics::is_nonoverlapping::runtime]:
OFFSET           TYPE              VALUE
00000000000000a2 R_X86_64_PC32     .rodata..Lanon.564567b94c98be8ca95432021ca6f120.1-0x0000000000000004
00000000000000a9 R_X86_64_GOTPCREL  core::panicking::panic_nounwind-0x0000000000000004


RELOCATION RECORDS FOR [.text.core::intrinsics::copy_nonoverlapping::precondition_check]:
OFFSET           TYPE              VALUE
0000000000000044 R_X86_64_PLT32    .text.core::intrinsics::is_aligned_and_not_null-0x0000000000000004
000000000000004f R_X86_64_PC32     .rodata..Lanon.564567b94c98be8ca95432021ca6f120.2-0x0000000000000004
0000000000000056 R_X86_64_GOTPCREL  core::panicking::panic_nounwind-0x0000000000000004
0000000000000071 R_X86_64_PLT32    .text.core::intrinsics::is_aligned_and_not_null-0x0000000000000004
0000000000000090 R_X86_64_PLT32    .text.core::intrinsics::is_nonoverlapping::runtime-0x0000000000000004


RELOCATION RECORDS FOR [.text.core::intrinsics::is_aligned_and_not_null]:
OFFSET           TYPE              VALUE
00000000000000c8 R_X86_64_PC32     .data.rel.ro..Lanon.564567b94c98be8ca95432021ca6f120.4-0x0000000000000004
00000000000000eb R_X86_64_PC32     .Lanon.564567b94c98be8ca95432021ca6f120.6-0x0000000000000004
00000000000000f2 R_X86_64_PC32     .Lanon.564567b94c98be8ca95432021ca6f120.6+0x0000000000000004
0000000000000103 R_X86_64_PC32     .rodata..Lanon.564567b94c98be8ca95432021ca6f120.5-0x0000000000000004
0000000000000118 R_X86_64_PC32     .data.rel.ro..Lanon.564567b94c98be8ca95432021ca6f120.8-0x0000000000000004
000000000000011f R_X86_64_GOTPCREL  core::panicking::panic_fmt-0x0000000000000004


RELOCATION RECORDS FOR [.text.core::hint::unreachable_unchecked::precondition_check]:
OFFSET           TYPE              VALUE
0000000000000004 R_X86_64_PC32     .rodata..Lanon.564567b94c98be8ca95432021ca6f120.9-0x0000000000000004
000000000000000b R_X86_64_GOTPCREL  core::panicking::panic_nounwind-0x0000000000000004

However I would have expected #121421 to address this since all of these are gated around code like this:

            if ::core::intrinsics::$kind() {
                precondition_check($($arg,)*);
            }

@saethlin
Copy link
Member

Do you know why the linker errors only happen on Windows?

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

It now happens on Linux as well.

@saethlin
Copy link
Member

This works fine on my Linux machine:

RUSTFLAGS="-Cdebug-assertions=no" CARGO_INCREMENTAL=0 CARGO_PROFILE_DEV_LTO=true RUST_COMPILER_RT_ROOT=./compiler-rt cargo +nightly rustc --features=mem,c --target=x86_64-unknown-linux-gnu --example intrinsics
rustc 1.78.0-nightly (f4b771bf1 2024-03-14)
binary: rustc
commit-hash: f4b771bf1fb836392e1c510a625cdc81be09c952
commit-date: 2024-03-14
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

The link failure doesn't happen, but that's because (I think) the function is pruned by the linker. If you look at the rlib with objdump -r target/debug/deps/libcompiler_builtins-67b23b0a4300aa45.rlib | rustfilt then you will see references to panic functions in core. If I understand #121421 correctly then these functions should never have reached codegen because they are unreachable.

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

Hmm actually now that I'm re-reading #121421 it only applies to basic blocks within a function and doesn't affect function-level reachability analysis.

Perhaps the failure was only observed on msvc because it uses a different linker (MSVC link.exe)?

@saethlin
Copy link
Member

Right. That PR only avoids lowering the calls, it doesn't touch reachability analysis.

The previous PR that I based this on did change reachability analysis and MIR collection, but that approach means that some kind of errors under and if false would not be reported. Which is bad.

@saethlin saethlin added the O-windows-msvc Toolchain: MSVC, Operating system: Windows label Mar 15, 2024
@saethlin
Copy link
Member

Is there a way to print the linker invocation when I'm building for the MSVC target? As far as I can tell from the code in cg_ssa:

fn gc_sections(&mut self, _keep_metadata: bool) {
// MSVC's ICF (Identical COMDAT Folding) link optimization is
// slow for Rust and thus we disable it by default when not in
// optimization build.
if self.sess.opts.optimize != config::OptLevel::No {
self.cmd.arg("/OPT:REF,ICF");
} else {
// It is necessary to specify NOICF here, because /OPT:REF
// implies ICF by default.
self.cmd.arg("/OPT:REF,NOICF");
}
}

which is called from here:

// Try to strip as much out of the generated object by removing unused
// sections if possible. See more comments in linker.rs
if !sess.link_dead_code() {
// If PGO is enabled sometimes gc_sections will remove the profile data section
// as it appears to be unused. This can then cause the PGO profile file to lose
// some functions. If we are generating a profile we shouldn't strip those metadata
// sections to ensure we have all the data for PGO.
let keep_metadata =
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
{
cmd.gc_sections(keep_metadata);
} else {
cmd.no_gc_sections();
}
}

/OPT:REF eliminates functions and data that are never referenced; /OPT:NOREF keeps functions and data that are never referenced.

we are already asking MSVC to eliminate the failing-to-link code. So either I'm reading this wrong (quite possible) or MSVC's linker optimization here is quite poor, or this is an MSVC linker bug.

@ChrisDenton
Copy link
Contributor

Is there a way to print the linker invocation when I'm building for the MSVC target?

Maybe try something like:

RUSTC_LOG="rustc_codegen_ssa::back::link=info"

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

I'm doing a new CI run in rust-lang/compiler-builtins#582, this time specifically ignoring whether the symbols are referenced from the .rlib and instead only checking whether linking succeeds.

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

Still failing on MSVC:

  = note: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Tools\\MSVC\\14.39.33519\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\rustcbIdbQR\\symbols.o" "D:\\a\\rustc-builtins\\rustc-builtins\\target\\x86_64-pc-windows-msvc\\debug\\examples\\intrinsics.intrinsics.ee7fc2860a069eb-cgu.0.rcgu.o" "/LIBPATH:D:\\a\\rustc-builtins\\rustc-builtins\\target\\x86_64-pc-windows-msvc\\debug\\deps" "/LIBPATH:D:\\a\\rustc-builtins\\rustc-builtins\\target\\debug\\deps" "/LIBPATH:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "kernel32.lib" "msvcrt.lib" "D:\\a\\rustc-builtins\\rustc-builtins\\target\\x86_64-pc-windows-msvc\\debug\\deps\\libcompiler_builtins-b84df12ffea9ddc5.rlib" "/NXCOMPAT" "/LIBPATH:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/OUT:D:\\a\\rustc-builtins\\rustc-builtins\\target\\x86_64-pc-windows-msvc\\debug\\examples\\intrinsics.exe" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\runneradmin\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis"
  = note: libcompiler_builtins-b84df12ffea9ddc5.rlib(compiler_builtins-b84df12ffea9ddc5.compiler_builtins.2e85d28818ca4c95-cgu.2.rcgu.o) : error LNK2019: unresolved external symbol _ZN4core9panicking14panic_nounwind17h6699847a7f3add57E referenced in function _ZN4core3ptr13read_volatile18precondition_check17he785f423889d7c99E
          libcompiler_builtins-b84df12ffea9ddc5.rlib(compiler_builtins-b84df12ffea9ddc5.compiler_builtins.2e85d28818ca4c95-cgu.3.rcgu.o) : error LNK2001: unresolved external symbol _ZN4core9panicking14panic_nounwind17h6699847a7f3add57E
          libcompiler_builtins-b84df12ffea9ddc5.rlib(compiler_builtins-b84df12ffea9ddc5.compiler_builtins.2e85d28818ca4c95-cgu.3.rcgu.o) : error LNK2019: unresolved external symbol _ZN4core9panicking9panic_fmt17hd6fe3dd19326274fE referenced in function _ZN4core10intrinsics23is_aligned_and_not_null17hc6482b774bbdedc1E

@Amanieu
Copy link
Member Author

Amanieu commented Mar 15, 2024

I disabled fast-fast in CI so we can get a clearer picture of the failures: https://github.com/Amanieu/rustc-builtins/actions/runs/8301447504

It seems to also fail on windows-gnu targets:

           C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\a\rustc-builtins\rustc-builtins\target\x86_64-pc-windows-gnu\debug\deps\libcompiler_builtins-d95e003c46722478.rlib(compiler_builtins-d95e003c46722478.compiler_builtins.95c73df5ba01d296-cgu.2.rcgu.o): in function `core::hint::unreachable_unchecked::precondition_check':
          /rustc/f4b771bf1fb836392e1c510a625cdc81be09c952\library\core\src/intrinsics.rs:2799: undefined reference to `core::panicking::panic_nounwind'
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\a\rustc-builtins\rustc-builtins\target\x86_64-pc-windows-gnu\debug\deps\libcompiler_builtins-d95e003c46722478.rlib(compiler_builtins-d95e003c46722478.compiler_builtins.95c73df5ba01d296-cgu.3.rcgu.o): in function `core::intrinsics::is_nonoverlapping::runtime':
          /rustc/f4b771bf1fb836392e1c510a625cdc81be09c952\library\core\src/intrinsics.rs:2847: undefined reference to `core::panicking::panic_nounwind'
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\a\rustc-builtins\rustc-builtins\target\x86_64-pc-windows-gnu\debug\deps\libcompiler_builtins-d95e003c46722478.rlib(compiler_builtins-d95e003c46722478.compiler_builtins.95c73df5ba01d296-cgu.3.rcgu.o): in function `core::intrinsics::copy_nonoverlapping::precondition_check':
          /rustc/f4b771bf1fb836392e1c510a625cdc81be09c952\library\core\src/intrinsics.rs:2799: undefined reference to `core::panicking::panic_nounwind'
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\a\rustc-builtins\rustc-builtins\target\x86_64-pc-windows-gnu\debug\deps\libcompiler_builtins-d95e003c46722478.rlib(compiler_builtins-d95e003c46722478.compiler_builtins.95c73df5ba01d296-cgu.3.rcgu.o): in function `core::ptr::const_ptr::<impl *const T>::is_aligned_to':
          /rustc/f4b771bf1fb836392e1c510a625cdc81be09c952\library\core\src\ptr/const_ptr.rs:1634: undefined reference to `core::panicking::panic_fmt'
          C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\a\rustc-builtins\rustc-builtins\target\x86_64-pc-windows-gnu\debug\deps\libcompiler_builtins-d95e003c46722478.rlib(compiler_builtins-d95e003c46722478.compiler_builtins.95c73df5ba01d296-cgu.3.rcgu.o): in function `core::ptr::read_volatile::precondition_check':
          /rustc/f4b771bf1fb836392e1c510a625cdc81be09c952\library\core\src/intrinsics.rs:2799: undefined reference to `core::panicking::panic_nounwind'

@saethlin
Copy link
Member

saethlin commented Mar 15, 2024

We're passing the linker flags /OPT:REF,NOICF /DEBUG in this compiler-builtins configuration. Even if I make that /OPT:REF,ICF we still have these undefined references. So as far as I can tell, we are indeed asking the linker to do this optimization and it is not doing what we want.

Some of the undefined references here double-checking situations that ought to be impossible. We could simply delete these checks: master...saethlin:rust:some-bad-ideas

diff --git a/library/core/src/intrinsics.rs b/library/core/src/intrinsics.rs
index 86b9a39d68a..2d1a81c3193 100644
--- a/library/core/src/intrinsics.rs
+++ b/library/core/src/intrinsics.rs
@@ -2844,9 +2844,7 @@ fn runtime(src: *const (), dst: *const (), size: usize, count: usize) -> bool {
         let src_usize = src.addr();
         let dst_usize = dst.addr();
         let Some(size) = size.checked_mul(count) else {
-            crate::panicking::panic_nounwind(
-                "is_nonoverlapping: `size_of::<T>() * count` overflows a usize",
-            )
+            crate::intrinsics::abort()
         };
         let diff = src_usize.abs_diff(dst_usize);
         // If the absolute distance between the ptrs is at least as big as the size of the buffer,
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index a0c04d3f65d..e57c64df9c0 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -1630,10 +1630,6 @@ pub const fn is_aligned(self) -> bool
     #[unstable(feature = "pointer_is_aligned", issue = "96284")]
     #[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "104203")]
     pub const fn is_aligned_to(self, align: usize) -> bool {
-        if !align.is_power_of_two() {
-            panic!("is_aligned_to: align is not a power-of-two");
-        }
-
         #[inline]
         fn runtime_impl(ptr: *const (), align: usize) -> bool {
             ptr.addr() & (align - 1) == 0
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index 6a9033a144d..f069cce94e9 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -1888,10 +1888,6 @@ pub const fn is_aligned(self) -> bool
     #[unstable(feature = "pointer_is_aligned", issue = "96284")]
     #[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "104203")]
     pub const fn is_aligned_to(self, align: usize) -> bool {
-        if !align.is_power_of_two() {
-            panic!("is_aligned_to: align is not a power-of-two");
-        }
-
         #[inline]
         fn runtime_impl(ptr: *mut (), align: usize) -> bool {
             ptr.addr() & (align - 1) == 0

I would send a PR like that, except that does not remove all the linker errors. On MSVC I still see one error... except that if I also add -Ccodegen-units=4096 it goes away. I do not yet understand why.

@saethlin
Copy link
Member

Actually, it looks like with -Ccodegen-units=89 or higher, we don't need any compiler changes to make the linker errors go away. I suppose the next thing to do is dig into what's different about the CGUs between 88 and 89.

@ChrisDenton
Copy link
Contributor

Presumably that would have the effect of splitting it up into more object files which causes the linker to simply drop ones that do not provide a necessary symbol.

@dpaoliello
Copy link
Contributor

Trying to rely on MSVC linker to not pull in external symbols via /OPT:REF is not going to work - the linker tries to resolve external symbols first, then it performs optimizations.

@saethlin Messing with the code gen units means that the external symbol for panics is landing in an obj that happens to not be pulled in, hence MSVC doesn't complain about it being missing. This is an incredibly fragile solution...

If we can't rely on libcore, would it be possible to implement a "mini panic" in compiler-builtins instead?

@saethlin
Copy link
Member

This is an incredibly fragile solution...

I'm not proposing any solution.

@saethlin
Copy link
Member

Presumably that would have the effect of splitting it up into more object files which causes the linker to simply drop ones that do not provide a necessary symbol.

The undefined symbols in this case are only referenced by functions that are never called. So that sounds right.

@saethlin
Copy link
Member

I think I have a solution. I'll put up a PR soon...

@saethlin
Copy link
Member

This is my idea: #122580

@RalfJung
Copy link
Member

This was marked as fixed by #122580, but that PR doesn't change the library or how code is generated, so it can't by itself change anything about the linker errors, can it?

@Amanieu
Copy link
Member Author

Amanieu commented Mar 23, 2024

It does fix the issue by replacing all "external" calls with abort, which means that compiler_builtins no longer depends on libcore at link-time.

@RalfJung
Copy link
Member

Ah, makes sense, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows-msvc Toolchain: MSVC, Operating system: Windows P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
7 participants