https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115527

--- Comment #11 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:8b5919bae11754f4b65a17e63663d3143f9615ac

commit r15-2090-g8b5919bae11754f4b65a17e63663d3143f9615ac
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Wed Jul 17 11:38:33 2024 +0200

    gimple-fold: Fix up __builtin_clear_padding lowering [PR115527]

    The builtin-clear-padding-6.c testcase fails as clear_padding_type
    doesn't correctly recompute the buf->size and buf->off members after
    expanding clearing of an array using a runtime loop.
    buf->size should be in that case the offset after which it should continue
    with next members or padding before them modulo UNITS_PER_WORD and
    buf->off that offset minus buf->size.  That is what the code was doing,
    but with off being the start of the loop cleared array, not its end.
    So, the last hunk in gimple-fold.cc fixes that.
    When adding the testcase, I've noticed that the
    c-c++-common/torture/builtin-clear-padding-* tests, although clearly
    written as runtime tests to test the builtins at runtime, didn't have
    { dg-do run } directive and were just compile tests because of that.
    When adding that to the tests, builtin-clear-padding-1.c was already
    failing without that clear_padding_type hunk too, but
    builtin-clear-padding-5.c was still failing even after the change.
    That is due to a bug in clear_padding_flush which the patch fixes as
    well - when clear_padding_flush is called with full=true (that happens
    at the end of the whole __builtin_clear_padding or on those array
    padding clears done by a runtime loop), it wants to flush all the pending
    padding clearings rather than just some.  If it is at the end of the whole
    object, it decreases wordsize when needed to make sure the code never
writes
    including RMW cycles to something outside of the object:
          if ((unsigned HOST_WIDE_INT) (buf->off + i + wordsize)
              > (unsigned HOST_WIDE_INT) buf->sz)
            {
              gcc_assert (wordsize > 1);
              wordsize /= 2;
              i -= wordsize;
              continue;
            }
    but if it is full==true flush in the middle, this doesn't happen, but we
    still process just the buffer bytes before the current end.  If that end
    is not on a wordsize boundary, e.g. on the builtin-clear-padding-5.c test
    the last chunk is 2 bytes, '\0', '\xff', i is 16 and end is 18,
    nonzero_last might be equal to the end - i, i.e. 2 here, but still all_ones
    might be true, so in some spots we just didn't emit any clearing in that
    last chunk.

    2024-07-17  Jakub Jelinek  <ja...@redhat.com>

            PR middle-end/115527
            * gimple-fold.cc (clear_padding_flush): Introduce endsize
            variable and use it instead of wordsize when comparing it against
            nonzero_last.
            (clear_padding_type): Increment off by sz.

            * c-c++-common/torture/builtin-clear-padding-1.c: Add dg-do run
            directive.
            * c-c++-common/torture/builtin-clear-padding-2.c: Likewise.
            * c-c++-common/torture/builtin-clear-padding-3.c: Likewise.
            * c-c++-common/torture/builtin-clear-padding-4.c: Likewise.
            * c-c++-common/torture/builtin-clear-padding-5.c: Likewise.
            * c-c++-common/torture/builtin-clear-padding-6.c: New test.

Reply via email to