Skip to content

Rust unable to optimize away assert vs. clang.  #93036

Closed
@jrmuizel

Description

@jrmuizel
pub fn f(arr: &mut [u32]) {
    for i in 0..arr.len() {
        for j in 0..i {
            assert!(j < arr.len());
        }
    }
}

Rustc does not optimize away the the assert. Adding -C passes=constraint-elimination doesn't help.

However, clang does optimize away this assert when compiled with -O2 -mllvm -enable-constraint-elimination

void f(int *ptr, size_t len) {
    for (size_t i = 0; i < len; i++) {
            for (size_t j = 0; j < i; j++) {
                assert(j < len);
            }
    }
}

GCC is also able to remove the assert with GCC trunk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions