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

Add as_rchunks (and friends) to slices #78818

Merged
merged 3 commits into from
Jan 17, 2021
Merged

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Nov 6, 2020

@est31 mentioned (#76354 (comment)) that, for completeness, there needed to be an as_chunks-like method that chunks from the end (with the remainder at the beginning) like rchunks does.

So here's a PR for as_rchunks: &[T] -> (&[T], &[[T; N]]) and as_rchunks_mut: &mut [T] -> (&mut [T], &mut [[T; N]]).

But as I was doing this and copy-pasting from_raw_parts calls, I thought that I should extract that into an unsafe method. It started out a private helper, but it seemed like as_chunks_unchecked could be reasonable as a "real" method, so I added docs and made it public. Let me know if you think it doesn't pull its weight.

@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 6, 2020
/// ```
#[unstable(feature = "slice_as_chunks", issue = "74985")]
#[inline]
pub unsafe fn as_chunks_mut_unchecked<const N: usize>(&mut self) -> &mut [[T; N]] {
Copy link
Member Author

Choose a reason for hiding this comment

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

It wasn't obvious to me whether this should be as_chunks_mut_unchecked or as_chunks_unchecked_mut. I could see precedent for both.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like unchecked_mut is more frequently used but mut_unchecked sounds better to me 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like unchecked_mut has more precedent in slice and iterator APIs so maybe we should roll with that.

I'm very sad that we have a nice mix of both unchecked_mut and mut_unchecked 🥲

pub fn chunks4_with_remainder(x: &[u8]) -> (&[[u8; 4]], &[u8]) {
// CHECK: and i64 %x.1, -4
// CHECK: and i64 %x.1, 3
// CHECK: lshr exact
Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to ensure I didn't accidentally introduce extra instructions with the refactor, so codegen test. It did change, but only from lshr/and/and to and/and/lshr exact, which is a thoroughly uninteresting difference.

@Dylan-DPC-zz
Copy link

r? @KodrAus

@rust-highfive rust-highfive assigned KodrAus and unassigned sfackler Nov 24, 2020
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 11, 2020
@KodrAus
Copy link
Contributor

KodrAus commented Jan 14, 2021

Thanks @scottmcm!

r=me with as_chunks_unchecked_mut.

@scottmcm
Copy link
Member Author

@bors r=KodrAus

@bors
Copy link
Contributor

bors commented Jan 16, 2021

📌 Commit 132307c has been approved by KodrAus

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 16, 2021
Add `as_rchunks` (and friends) to slices

`@est31` mentioned (rust-lang#76354 (comment)) that, for completeness, there needed to be an `as_chunks`-like method that chunks from the end (with the remainder at the beginning) like `rchunks` does.

So here's a PR for `as_rchunks: &[T] -> (&[T], &[[T; N]])` and `as_rchunks_mut: &mut [T] -> (&mut [T], &mut [[T; N]])`.

But as I was doing this and copy-pasting `from_raw_parts` calls, I thought that I should extract that into an unsafe method.  It started out a private helper, but it seemed like `as_chunks_unchecked` could be reasonable as a "real" method, so I added docs and made it public.  Let me know if you think it doesn't pull its weight.
@m-ou-se
Copy link
Member

m-ou-se commented Jan 16, 2021

@bors r-

This failed in #81085 (comment):


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

Click to see the possible cause of the failure (guessed by this bot)
Some tests failed in compiletest suite=codegen mode=codegen host=x86_64-unknown-linux-gnu target=x86_64-unknown-linux-gnu

failures:

---- [codegen] codegen/slice-as_chunks.rs stdout ----

error: verification with 'FileCheck' failed
status: exit code: 1
command: "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/llvm/build/bin/FileCheck" "--input-file" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/test/codegen/slice-as_chunks/slice-as_chunks.ll" "/checkout/obj/build/tmp/distcheck/src/test/codegen/slice-as_chunks.rs"
------------------------------------------

------------------------------------------
stderr:
stderr:
------------------------------------------
/checkout/obj/build/tmp/distcheck/src/test/codegen/slice-as_chunks.rs:12:17: error: CHECK-NEXT: expected string not found in input
 // CHECK-NEXT: lshr i64 %x.1, 2
                ^
/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/test/codegen/slice-as_chunks/slice-as_chunks.ll:15:2: note: scanning from here
 %len.i = and i64 %x.1, -4
 ^
/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/test/codegen/slice-as_chunks/slice-as_chunks.ll:15:9: note: possible intended match here
 %len.i = and i64 %x.1, -4


Input file: /checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/test/codegen/slice-as_chunks/slice-as_chunks.ll
Check file: /checkout/obj/build/tmp/distcheck/src/test/codegen/slice-as_chunks.rs

-dump-input=help explains the following input dump.
Input was:
<<<<<<
           .
           .
           .
           .
          10: @alloc47 = private unnamed_addr constant <{ i8*, [16 x i8] }> <{ i8* getelementptr inbounds (<{ [63 x i8] }>, <{ [63 x i8] }>* @alloc46, i32 0, i32 0, i32 0), [16 x i8] c"?\00\00\00\00\00\00\00[\00\00\00\05\00\00\00" }>, align 8
          11: 
          12: ; Function Attrs: nonlazybind uwtable
          13: define { [0 x [4 x i8]]*, i64 } @chunks4([0 x i8]* noalias nonnull readonly align 1 %x.0, i64 %x.1) unnamed_addr #0 {
          14: start:
          15:  %len.i = and i64 %x.1, -4
next:12'0      X~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
next:12'1             ?                  possible intended match
          16:  %_71.i.i.i = icmp slt i64 %len.i, 0
next:12'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          17:  br i1 %_71.i.i.i, label %bb6.i.i.i, label %"_ZN4core5slice29_$LT$impl$u20$$u5b$T$u5d$$GT$9as_chunks17h5eb20245ca5b2171E.exit"
next:12'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          18: 
next:12'0     ~
          19: bb6.i.i.i: ; preds = %start
next:12'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
          20: ; call core::panicking::panic
next:12'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           .
           .
>>>>>>

---
test result: FAILED. 207 passed; 1 failed; 33 ignored; 0 measured; 0 filtered out; finished in 4.09s



command did not execute successfully: "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "--src-base" "/checkout/obj/build/tmp/distcheck/src/test/codegen" "--build-base" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/test/codegen" "--stage-id" "stage2-x86_64-unknown-linux-gnu" "--suite" "codegen" "--mode" "codegen" "--target" "x86_64-unknown-linux-gnu" "--host" "x86_64-unknown-linux-gnu" "--llvm-filecheck" "/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/llvm/build/bin/FileCheck" "--host-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target-rustcflags" "-Crpath -O -Cdebuginfo=0 -Zunstable-options  -Lnative=/checkout/obj/build/tmp/distcheck/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--docck-python" "/usr/bin/python3" "--lldb-python" "/usr/bin/python3" "--gdb" "/usr/bin/gdb" "--llvm-version" "11.0.1-rust-1.51.0-nightly" "--llvm-components" "aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils aggressiveinstcombine all all-targets analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils asmparser asmprinter avr avrasmparser avrcodegen avrdesc avrdisassembler avrinfo binaryformat bitreader bitstreamreader bitwriter cfguard codegen core coroutines coverage debuginfocodeview debuginfodwarf debuginfogsym debuginfomsf debuginfopdb demangle dlltooldriver dwarflinker engine executionengine extensions frontendopenmp fuzzmutate globalisel gtest gtest_main hexagon hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo instcombine instrumentation interpreter ipo irreader jitlink libdriver lineeditor linker lto mc mca mcdisassembler mcjit mcparser mips mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo mirparser msp430 msp430asmparser msp430codegen msp430desc msp430disassembler msp430info native nativecodegen nvptx nvptxcodegen nvptxdesc nvptxinfo objcarcopts object objectyaml option orcerror orcjit passes powerpc powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo profiledata remarks riscv riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo riscvutils runtimedyld scalaropts selectiondag sparc sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo support symbolize systemz systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo tablegen target testingsupport textapi transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info xray" "--cc" "" "--cxx" "" "--cflags" "" "--adb-path" "adb" "--adb-test-dir" "/data/tmp/work" "--android-cross-path" "" "--color" "always"


failed to run: /checkout/obj/build/tmp/distcheck/build/bootstrap/debug/bootstrap test --stage 2
Build completed unsuccessfully in 0:19:44
Build completed unsuccessfully in 0:19:44
make: *** [check] Error 1
Makefile:42: recipe for target 'check' failed

command did not execute successfully: "make" "check"
expected success, got: exit code: 2

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 16, 2021
@scottmcm
Copy link
Member Author

@bors rollup=never

Sigh, codegen tests.

This fixed things the last time I had a problem like this.  And plausibly will here too -- the check it's failing on is for the high bit being set in the length of the slice, which is a check that's only in a debug_assert.
@scottmcm
Copy link
Member Author

@bors r=KodrAus

@bors
Copy link
Contributor

bors commented Jan 16, 2021

📌 Commit 6bcaba9 has been approved by KodrAus

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 16, 2021
@bors
Copy link
Contributor

bors commented Jan 17, 2021

⌛ Testing commit 6bcaba9 with merge 49d7889...

@bors
Copy link
Contributor

bors commented Jan 17, 2021

☀️ Test successful - checks-actions
Approved by: KodrAus
Pushing 49d7889 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 17, 2021
@bors bors merged commit 49d7889 into rust-lang:master Jan 17, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 17, 2021
@scottmcm scottmcm deleted the as_rchunks branch January 25, 2021 02:03
@scottmcm scottmcm mentioned this pull request Dec 3, 2021
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants