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

[WIP] Implement unchecked_disjoint_bitor #124601

Closed
wants to merge 9 commits into from

Conversation

alion02
Copy link

@alion02 alion02 commented May 2, 2024

ACP

AFAICT this doesn't compile right now (claims unrecognized intrinsic), and I don't exactly know why, because it did compile once already (or maybe there was a user error somehow). I would appreciate some help.

Beyond that this needs tests, non-intrinsic fns, and at least one todo resolved.

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jieyouxu (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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. labels May 2, 2024
@rustbot
Copy link
Collaborator

rustbot commented May 2, 2024

Some changes occurred in rustc_ty_utils::consts.rs

cc @BoxyUwU

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@alion02 alion02 marked this pull request as draft May 2, 2024 02:38
@alion02
Copy link
Author

alion02 commented May 2, 2024

Also, I think this PR will have to wait until the lowest supported LLVM is 18 or maybe even 19 (I can't figure out which version adds LLVMSetIsDisjoint, which seems necessary).

@@ -308,6 +308,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
BitAnd => ImmTy::from_uint(l & r, left.layout),
BitXor => ImmTy::from_uint(l ^ r, left.layout),

BitOrDisjoint => {
if l & r != 0 {
todo!(); // Can't figure out what to do here based on the surrounding code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to throw_ub!. To that end, you first need to add a new error variant to UndefinedBehaviorInfo, and then use that here.

Make sure to also add a Miri testcase that checks that we detect the UB here.

@@ -1478,6 +1478,8 @@ pub enum BinOp {
BitAnd,
/// The `|` operator (bitwise or)
BitOr,
/// Like `BitOr` and `BitXor`, but UB if results don't match.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Like `BitOr` and `BitXor`, but UB if results don't match.
/// Like `BitOr`, but UB if any bit is set both in the left and right operand (`left & right != 0`).

Let's express the requirement directly, in a way that matches the name, rather than indirectly.

@RalfJung
Copy link
Member

RalfJung commented May 2, 2024

Also, I think this PR will have to wait until the lowest supported LLVM is 18 or maybe even 19 (I can't figure out which version adds LLVMSetIsDisjoint, which seems necessary).

On older LLVM, it can simply compile to regular BitOr. We don't get optimization benefits but things will still compile correctly.

@alion02
Copy link
Author

alion02 commented May 3, 2024

On older LLVM, it can simply compile to regular BitOr.

Not sure how I'd do that. Do we have something like #[cfg(llvm_version = 17)]?

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-17 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 exporting to docker image format
#16 sending tarball 28.5s done
#16 DONE 40.6s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-17]
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-17', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-17/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---
   Compiling rustc_driver_impl v0.0.0 (/checkout/compiler/rustc_driver_impl)
   Compiling rustc_driver v0.0.0 (/checkout/compiler/rustc_driver)
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-sysroot/lib/rustlib/x86_64-unknown-linux-gnu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcXOnmR0/symbols.o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_main-0cbfe03da77376ae.rustc_main.bd086be8ad5323f2-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/release/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/build/psm-2415bf1bc99c7146/out" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/build/rustc_llvm-80e082dda5b39402/out" "-L" "/usr/lib/llvm-17/lib" "-L" "/usr/lib/gcc/x86_64-linux-gnu/13" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps" "-Wl,-Bdynamic" "-lrustc_driver-54aa5590a1ff09f7" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-lstd-db5ec79db612332a" "-Wl,-Bstatic" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-f2b398c36a164e96.rlib" "-Wl,-Bdynamic" "-lLLVM-17" "-ldl" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/rustc_main-0cbfe03da77376ae" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "-Wl,-z,origin" "-Wl,-rpath,$ORIGIN/../lib"
  = note: /usr/bin/ld: /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_driver-54aa5590a1ff09f7.so: undefined reference to `LLVMSetIsDisjoint'
          
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)
error: could not compile `rustc-main` (bin "rustc-main") due to 1 previous error
Build completed unsuccessfully in 0:02:40
  local time: Fri May  3 01:59:16 UTC 2024
  network time: Fri, 03 May 2024 01:59:16 GMT

@RalfJung
Copy link
Member

RalfJung commented May 3, 2024

If you grep for LLVM_VERSION_GE in RustWrapper.cpp you can find existing examples.

@bors
Copy link
Collaborator

bors commented May 20, 2024

☔ The latest upstream changes (presumably #125331) made this pull request unmergeable. Please resolve the merge conflicts.

#[cfg(not(bootstrap))]
#[rustc_const_unstable(feature = "disjoint_bitor", issue = "none")]
#[rustc_nounwind]
pub fn disjoint_bitor<T: Copy>(x: T, y: T) -> T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, as a good way to split up the PR, this PR should just add this as an intrinsic with fallback.

You can see

pub const unsafe fn assume(b: bool) {
if !b {
// SAFETY: the caller must guarantee the argument is never `false`
unsafe { unreachable() }
}
}
as an example of that. That will let you remove all the codegen changes, and most of the compiler changes too -- it'll only need just enough compiler changes to pass the intrinsic typechecking part. And we can then approve that fairly simply, since it doesn't need to worry about LLVM version checks and such.

Then a follow-up PR can change just the llvm backend to do something different, without needing to update cg_gcc nor cg_clif nor smir nor ...

This this would be something like

Suggested change
pub fn disjoint_bitor<T: Copy>(x: T, y: T) -> T;
// The `assume` in the body is enough for MIRI to see any UB from this.
#[cfg_attr(not(bootstrap), miri::intrinsic_fallback_checks_ub)]
pub unsafe fn disjoint_bitor<T: Copy + Default + Eq>(x: T, y: T) -> T
where
T: std::ops::BitAnd<Output = T> + std::ops::BitOr<Output = T>,
{
assume(x & y == T::default());
x | y
}

@scottmcm scottmcm self-assigned this May 21, 2024
@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 12, 2024
@Dylan-DPC
Copy link
Member

@alion02 any updates on this? thanks

@alion02
Copy link
Author

alion02 commented Aug 15, 2024

I've decided not to pursue this anymore. There are other scenarios where you might want to express "use any of these computations, whichever is most convenient" to your optimizer. Offering just this specific one simply because LLVM decided to special-case it feels like a clunky design.

@alion02 alion02 closed this Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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
Development

Successfully merging this pull request may close these issues.

8 participants