Skip to content

[r+] Return passed value from black_box #20463

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

Merged
merged 2 commits into from
Jan 16, 2015
Merged

[r+] Return passed value from black_box #20463

merged 2 commits into from
Jan 16, 2015

Conversation

amaranth
Copy link
Contributor

@amaranth amaranth commented Jan 3, 2015

By returning the passed value black_box can be used on data being
passed to a function being benchmarked. This ensures the compiler
does not optimize the function for the input which could result in
the entire function being optimized away.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@amaranth
Copy link
Contributor Author

amaranth commented Jan 3, 2015

I'm trying to do perlin2(&seed, black_box(&[0.0f32, 0.0])) to prevent the compiler from optimizing away perlin2 by knowing what its inputs are. There is currently no way to do this.

@huonw
Copy link
Member

huonw commented Jan 4, 2015

It can be done now, via

let data = [0.0f32, 0.0];
black_box(&mut data); // doesn't mutate, but LLVM doesn't know
perlin2(&seed, &data);

@huonw
Copy link
Member

huonw commented Jan 4, 2015

That said, that trick is somewhat annoying; I have certainly thought about making the same change as performed here to be able fluently opaquify values.

@reem
Copy link
Contributor

reem commented Jan 4, 2015

I was actually surprised that LLVM doesn't have a 0-cost intrinsic for this, since tricks like this do have the potential to slow down benchmarks (memcpy/memset(0)).

@thestinger
Copy link
Contributor

Inline assembly seems like it would work, as long as it's passed the right stuff.

@nikomatsakis
Copy link
Contributor

I guess I have no strong objection. Seems nice to be able to use it fluently.

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 5, 2015
By returning the passed value black_box can be used on data being
passed to a function being benchmarked. This ensures the compiler
does not optimize the function for the input which could result in
the entire function being optimized away.
@amaranth
Copy link
Contributor Author

Is there something more needed for this? If I'm reading github correctly it doesn't need a rebase still.

@alexcrichton
Copy link
Member

The automated tests failed and will need to be fixed before another r+ (can be reproduced with make check)

@amaranth
Copy link
Contributor Author

Weird, those didn't fail locally, guess the already existing failure on OS X hid them from me. The build log said it was killed so I thought maybe it just didn't get a chance to run the tests.

By returning the passed value black_box can be used on data being
passed to a function being benchmarked. This ensures the compiler
does not optimize the function for the input which could result in
the entire function being optimized away.
@amaranth
Copy link
Contributor Author

I think I've got the test failures sorted out, things pass locally now at least.

bors added a commit that referenced this pull request Jan 14, 2015
Return passed value from black_box

Reviewed-by: alexcrichton
@nikomatsakis nikomatsakis changed the title Return passed value from black_box [r+] Return passed value from black_box Jan 14, 2015
bors added a commit that referenced this pull request Jan 14, 2015
[r+] Return passed value from black_box

Reviewed-by: alexcrichton
bors added a commit that referenced this pull request Jan 15, 2015
[r+] Return passed value from black_box

Reviewed-by: alexcrichton
bors added a commit that referenced this pull request Jan 15, 2015
[r+] Return passed value from black_box

Reviewed-by: alexcrichton
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 15, 2015
By returning the passed value black_box can be used on data being
passed to a function being benchmarked. This ensures the compiler
does not optimize the function for the input which could result in
the entire function being optimized away.
@bors bors merged commit 9f5fc56 into rust-lang:master Jan 16, 2015
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.

8 participants