Skip to content

Bounds check not eliminated #47888

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

Closed
jrmuizel opened this issue Dec 17, 2020 · 5 comments
Closed

Bounds check not eliminated #47888

jrmuizel opened this issue Dec 17, 2020 · 5 comments
Labels
bugzilla Issues migrated from bugzilla llvm:optimizations

Comments

@jrmuizel
Copy link

jrmuizel commented Dec 17, 2020

Bugzilla Link 48544
Version trunk
OS All
CC @fhahn,@RKSimon,@nikic

Extended Description

#include <stdlib.h>
char foo(char* data, size_t len, size_t i, size_t j) {
    if (i < len && j <= i) {
        if (j < len) {
            return data[j];
        } else {
            exit(1);
        }
    } else {
        return 0;
    }
}

compiles to:

foo(char*, unsigned long, unsigned long, unsigned long):
        cmp     rdx, rsi
        jnb     .L4
        cmp     rdx, rcx
        jb      .L4
        cmp     rsi, rcx
        jbe     .L3
        movzx   eax, BYTE PTR [rdi+rcx]
        ret
.L4:
        xor     eax, eax
        ret
.L3:
        push    rax
        mov     edi, 1
        call    exit

The call to exit should be eliminated.

@fhahn
Copy link
Contributor

fhahn commented Dec 17, 2020

This is exactly a case targeted by the new ConstraintElimination pass added recently.

Building your example with -O3 -mllvm -enable-constraint-elimination gets rid of the check. (https://godbolt.org/z/839v98)

The pass is currently disabled by default, so any feedback & additional testing would be extremely valuable, in case you have code that has lots of such runtime checks.

@jrmuizel
Copy link
Author

This test case is from rust-lang/rust#80075. I expect it will be interesting to see the results of this pass on Rust code.

@fhahn
Copy link
Contributor

fhahn commented Dec 17, 2020

This test case is from rust-lang/rust#80075. I
expect it will be interesting to see the results of this pass on Rust code.

Ok great. I'd expect it to be valuable for languages that automatically insert/generate range checks. Unfortunately I am not set up to do any evaluation with Rust. Is this something you might be able to do? There's a stats counter constraint-elimination.NumCondsRemoved which indicates how often this transform fires.

It currently is also a bit dependent on the pass order. I am not sure which pipeline Rust uses, but it might need some tweaks to the pass to make it work more effectively on IR that Rust produces.

@jrmuizel
Copy link
Author

Nikita might be better suited to trying that out.

@fhahn
Copy link
Contributor

fhahn commented Dec 23, 2020

Yeah, it would be good to have a frontend that generates a lot of such runtime-checks to motivate enabling the pass by default. Any data on that would be very valuable to work towards flipping the default and to iron out potential issues.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@fhahn fhahn closed this as completed in fb13dcf Jan 4, 2023
fhahn added a commit that referenced this issue Feb 6, 2023
This reverts commit 695ce48.

The compile-time regression causing the revert has been fixed. Recommit
the original patch.

Original commit message:

   The pass should help to close a functional gap when it comes to
    reasoning about related conditions in a relatively general way.

    It addresses multiple existing issues (linked below) and the need for a
    more powerful reasoning system was also discussed recently in
    https://discourse.llvm.org/t/rfc-alternative-approach-of-dealing-with-implications-from-comparisons-through-pos-analysis/65601/7

    On AArch64, the new pass performs ~2000 simplifications on
    MultiSource,SPEC2006,SPEC2017 with -O3.

    Compile-time impact:

    NewPM-O3: +0.20%
    NewPM-ReleaseThinLTO: +0.32%
    NewPM-ReleaseLTO-g: +0.28%

    https://llvm-compile-time-tracker.com/compare.php?from=f01a3a893c147c1594b9a3fbd817456b209dabbf&to=577688758ef64fb044215ec3e497ea901bb2db28&stat=instructions:u

    Fixes #49344.
    Fixes #47888.
    Fixes #48253.
    Fixes #49229.
    Fixes #58074.

    Reviewed By: asbirlea

    Differential Revision: https://reviews.llvm.org/D135915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:optimizations
Projects
None yet
Development

No branches or pull requests

3 participants