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 assume intrinsic #18080

Merged
merged 2 commits into from
Oct 25, 2014
Merged

Add assume intrinsic #18080

merged 2 commits into from
Oct 25, 2014

Conversation

veddan
Copy link
Contributor

@veddan veddan commented Oct 15, 2014

Adds an assume intrinsic that gets translated to llvm.assume. It is
used on a boolean expression and allows the optimizer to assume that
the expression is true.

This implements #18051.

Adds an `assume` intrinsic that gets translated to llvm.assume. It is
used on a boolean expression and allows the optimizer to assume that
the expression is true.

This implements rust-lang#18051.
@@ -256,6 +256,13 @@ extern "rust-intrinsic" {
/// NB: This is very different from the `unreachable!()` macro!
pub fn unreachable() -> !;

/// Inform the optimizer that a condition is always true.
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to point out that the optimizer will try to preserve the information, so it may hurt performance rather than improving it if not used carefully.

@brson
Copy link
Contributor

brson commented Oct 17, 2014

Do we have places were we intend to use this and know that it provides a worthwhile performance improvement? I do not want to add features to Rust just because LLVM has them.

@mahkoh
Copy link
Contributor

mahkoh commented Oct 17, 2014

I currently have some code that includes a loop containing slice operations whose checks won't get properly optimized away, even if I use the unreachable intrinsic. I have not tried this new intrinsic but I've been told that it might help in this situation. Otherwise I will have to use lots of raw pointer arithmetic in the code.

@mahkoh
Copy link
Contributor

mahkoh commented Oct 17, 2014

        assert!(SRC_MULT * div <= src_buf.len());
        assert!(DST_MULT * div <= dst_buf.len());
        for i in range(0, div) {
            let x = src_buf.slice(SRC_MULT * i, SRC_MULT * (i + 1));
            let chars = [(x[0] & 0xFC) >> 2,
                         (x[0] & 0x03) << 4 | (x[1] & 0xF0) >> 4,
                         (x[1] & 0x0F) << 2 | (x[2] & 0xC0) >> 6,
                         (x[2] & 0x3F)];
            let y = dst_buf.slice_mut(DST_MULT * i, DST_MULT * (i + 1));
            for i in range(0, chars.len()) {
                y[i] = unsafe { *char_set.unsafe_get(chars[i] as uint) };
            }
        }

This is the loop in question which contains 4 unnecessary branches right now.

bors added a commit that referenced this pull request Oct 25, 2014
Adds an `assume` intrinsic that gets translated to llvm.assume. It is
used on a boolean expression and allows the optimizer to assume that
the expression is true.

This implements #18051.
@bors bors closed this Oct 25, 2014
@bors bors merged commit 0c18da5 into rust-lang:master Oct 25, 2014
@veddan veddan deleted the assume branch October 25, 2014 02:38
lnicola pushed a commit to lnicola/rust that referenced this pull request Sep 25, 2024
lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 7, 2025
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.

5 participants