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

            Bug ID: 119573
           Summary: nvptx: PTX '.const', constant state space
           Product: gcc
           Version: 15.0
               URL: https://inbox.sourceware.org/87a590lv1b.fsf@dem-tschwi
                    ng-1.schwinge.ddns.net
            Status: UNCONFIRMED
          Keywords: testsuite-fail, wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tschwinge at gcc dot gnu.org
  Target Milestone: ---
            Target: nvptx

See
<https://inbox.sourceware.org/87a590lv1b....@dem-tschwing-1.schwinge.ddns.net>
"'TREE_READONLY' for 'const' array in C vs. C++".

In GCC/nvptx, we use '.const' for 'TREE_READONLY' variables, otherwise
'.global'.

Consider separate translation units.

Consider the consumer side, for example:

    extern int * const arr[];

..., 'arr' in C is 'TREE_READONLY' (-> '.const' access), however in C++ it is
'!TREE_READONLY' (-> '.global' access).

Consider the producer side, in C++, for example:

    static int a = 123;
    extern int * const arr_a[] = { &a };
    extern int * f();
    extern int * const arr_f[] = { f() };

..., 'arr_a' is 'TREE_READONLY' (-> '.const' definition) vs. 'arr_f' is
'!TREE_READONLY' (-> '.global' definition; initialized at run time via global
constructor).

If the state space of definition and access doesn't match, we fault an run
time:

    error   : Memory space doesn't match for 'arr' in 'input file 2 at offset
2712', first specified in 'input file 1 at offset 1924'
    nvptx-run: cuLinkAddData failed: device kernel image is invalid
(CUDA_ERROR_INVALID_SOURCE, 300)

(This is reduced from actual C++ test cases, where libstdc++ provides '.const'
definitions, whereas test case code is compiled to access these with
'ld.global'; execution test FAILs.)

The consumer side only has its local declaration available, and from this
(without introducing any special machinery), at least in the case of C++, it
cannot tell whether the producer side uses '.const' or '.global'.  It follows
that we must be defensive, and never deduce '.const' from 'TREE_READONLY'.

Avoiding automatic use of '.const' will also resolve a number of compile-time
or run-time failures, where we currently run into:

    ptxas error   : File uses too much global constant data ([...] bytes,
0x10000 max)

And, this is also in line with another comment made, that '.const' has special
properties, and instead of automatically using it, as we currently do, it
should rather be handled similar to '.shared'.

Reply via email to