[llvm-bugs] [Bug 52016] New: LLVM doesn't eliminate bounds check inside loop

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Sep 29 12:13:31 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=52016

            Bug ID: 52016
           Summary: LLVM doesn't eliminate bounds check inside loop
           Product: new-bugs
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: peetlugen19 at yandex.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Code:

#include <cstddef>
#include <cstdint>

struct Memory {
    const uint8_t* start;
    const uint8_t* end;
};

extern void panic();

#define IN_BOUNDS(p) ((p) >= global.start && (p) < global.end)

#define CHECK(a, b) do { if (!IN_BOUNDS(a) || !IN_BOUNDS(b)) { panic(); return;
} } while (0)

void cpy(const Memory global, uint8_t* dst, const uint8_t* src, size_t len) {
    if (!dst || !src) {
        panic();
        return;
    }
    if (len == 0) {
        return;
    }

    CHECK(src, src + len - 1);
    CHECK(dst, dst + len - 1);

    for (size_t i = 0; i < len; ++i) {
        // this check is redundant
        CHECK(src + i, dst + i);
        dst[i] = src[i];
    }
}


https://godbolt.org/z/KWro949jh


LLVM doesn't eliminate the bounds check inside the loop.

It looks like LLVM does have a pass for exactly this problem: LoopPredication.
However it only support the experimental guard intrinsic.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210929/8ac95e26/attachment.html>


More information about the llvm-bugs mailing list