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

Restore indexed formulation of clone_from_slice #31000

Merged
merged 1 commit into from
Jan 18, 2016

Conversation

bluss
Copy link
Member

@bluss bluss commented Jan 18, 2016

Restore indexed formulation of clone_from_slice

For good codegen here, we need a lock step iteration where the loop
bound is only checked once per iteration; .zip() unfortunately does not
optimize this way.

If we use a counted loop, and make sure that llvm sees that the bounds
check condition is the same as the loop bound condition, the bounds
checks are optimized out. For this reason we need to slice from
(apparently) redundantly.

This commit restores the old formulation of clone_from_slice. In this
shape, clone_from_slice will again optimize into calling memcpy where possible
(for example for &[u8] or &[i32]).

For good codegen here, we need a lock step iteration where the loop
bound is only checked once per iteration; .zip() unfortunately does not
optimize this way.

If we use a counted loop, and make sure that llvm sees that the bounds
check condition is the same as the loop bound condition, the bounds
checks are optimized out. For this reason we need to slice `from`
(apparently) redundantly.

This commit restores the old formulation of clone_from_slice. In this
shape, clone_from_slice will again optimize into calling memcpy where possible
(for example for &[u8] or &[i32]).
@rust-highfive
Copy link
Collaborator

r? @brson

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

@bluss
Copy link
Member Author

bluss commented Jan 18, 2016

r? @alexcrichton

Fixup of #30943

I used these testcases to verify that it produces the same code as the old clone_from_slice formulation: https://play.rust-lang.org/?gist=568728397b448713ce14&version=nightly

the optimizer is clearly very picky here, the reslicing of src is strictly needed for the loop idiom recognize pass to insert memcpy.

@rust-highfive rust-highfive assigned alexcrichton and unassigned brson Jan 18, 2016
@alexcrichton
Copy link
Member

@bors: r+ 6a7bc47

Really wish we could have a test for something like this...

@dotdash
Copy link
Contributor

dotdash commented Jan 18, 2016

I guess all we need is a way to have ignore tests in non-opt builds, then
this could be covered by a codegen tests.

2016-01-18 18:45 GMT+01:00 Alex Crichton [email protected]:

@bors https://github.com/bors: r+ 6a7bc47
6a7bc47

Really wish we could have a test for something like this...


Reply to this email directly or view it on GitHub
#31000 (comment).

@bluss
Copy link
Member Author

bluss commented Jan 18, 2016

@dotdash I can't force "opt-level = 3" in compile-flags since it then says it conflicts with the already given -O. Otherwise the test seems to work.

// compile-flags: -C no-prepopulate-passes

// Make sure that simple cases of dst.clone_from_slice(src) produce memcpy
// (relies on loop idiom recognize).

#![crate_type = "lib"]

// CHECK-LABEL: @copy_slice
#[no_mangle]
pub fn copy_slice(src: &[u8], dst: &mut [u8]) {
    // CHECK: memcpy
    dst.clone_from_slice(src);
}

@alexcrichton
Copy link
Member

@bors: p=1

@bors
Copy link
Contributor

bors commented Jan 18, 2016

⌛ Testing commit 6a7bc47 with merge 0888649...

bors added a commit that referenced this pull request Jan 18, 2016
Restore indexed formulation of clone_from_slice

For good codegen here, we need a lock step iteration where the loop
bound is only checked once per iteration; .zip() unfortunately does not
optimize this way.

If we use a counted loop, and make sure that llvm sees that the bounds
check condition is the same as the loop bound condition, the bounds
checks are optimized out. For this reason we need to slice `from`
(apparently) redundantly.

This commit restores the old formulation of clone_from_slice. In this
shape, clone_from_slice will again optimize into calling memcpy where possible
(for example for &[u8] or &[i32]).
@bors bors merged commit 6a7bc47 into rust-lang:master Jan 18, 2016
@bluss bluss deleted the efficient-clone-from-slice branch January 18, 2016 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants