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

            Bug ID: 128096
           Summary: RFE: use explicit writability contracts for
                    pointer-based conditional stores
           Product: gcc
           Version: 17.0
               URL: https://godbolt.org/z/c1hGe7xE5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adamant.pwn at gmail dot com
  Target Milestone: ---
            Target: x86_64-linux-gnu

Created attachment 65930
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65930&action=edit
Header-free testcase with GCC version and configure options

Please consider using access(read_write), where its semantics permit, or a
stronger explicit writable-range contract to enable conditional-store
if-conversion through pointer parameters.

GCC 17.0.0 20261022 (experimental), revision
42df6e316f0a924ead4dce9dd9e26f8c07e53d8b, targeting x86_64-linux-gnu:

    gcc -O3 -fallow-store-data-races -Wall -Wextra -fopt-info-vec-all -S
repro.c

__attribute__((access(read_write, 1, 2)))
void through_pointer(int *p, int n) {
    for (int i = 0; i < n; ++i)
        if (p[i] < 100)
            p[i] = 100;
}

int a[1024];
void known_writable(int n) {
    for (int i = 0; i < n; ++i)
        if (a[i] < 100)
            a[i] = 100;
}

through_pointer remains scalar: "not vectorized: unsupported control flow in
loop." known_writable vectorizes using 16-byte vectors. Without
-fallow-store-data-races, both remain scalar.

In tree-if-conv.cc, ifcvt_memrefs_wont_trap checks for an unconditional write
to the base or base_object_writable, then separately checks
ref_can_have_store_data_races. base_object_writable only accepts a declaration
bound to its current definition with TREE_READONLY unset; it does not consume
access attributes on pointer parameters.

https://github.com/gcc-mirror/gcc/blob/42df6e316f0a924ead4dce9dd9e26f8c07e53d8b/gcc/tree-if-conv.cc#L860-L950

An arbitrary pointer, including a restrict-qualified one, can refer to
read-only storage on paths without stores. The requested contract would
explicitly guarantee that the range is writable, allowing
-fallow-store-data-races to remove the remaining data-race restriction.

Related: PR54935 discusses writability of external arrays; PR98817 explains why
arbitrary pointer/reference stores cannot simply become unconditional.

Reply via email to