Hi!

On 2025-03-25T11:51:26+0100, Tom de Vries <tdevr...@suse.de> wrote:
> On 3/25/25 11:18, Thomas Schwinge wrote:
>> On 2022-03-22T14:41:46+0100, Tom de Vries via Gcc-patches 
>> <gcc-patches@gcc.gnu.org> wrote:
>>> Starting with ptx isa version 6.3, a ptx directive .alias is available.
>> 
>> Regarding the following item specifically:
>> 
>>> Unreferenced aliases are not emitted (these can occur f.i. when inlining a
>>> call to an alias).  This avoids driver link error "Internal error: reference
>>> to deleted section".
>> 
>>> --- a/gcc/config/nvptx/nvptx.cc
>>> +++ b/gcc/config/nvptx/nvptx.cc
>> 
>>> +void
>>> +nvptx_asm_output_def_from_decls (FILE *stream, tree name, tree value)
>>> +{
>>> +  [...]
>>> +  if (!cgraph_node::get (name)->referred_to_p ())
>>> +    /* Prevent "Internal error: reference to deleted section".  */
>>> +    return;
>>> +  [...]
>>> +}
>> 
>> I understand the high-level rationale (PR105019) behind this early
>> return, but I'm curious why you chose cgraph 'referred_to_p ()' here,
>> instead of 'TREE_USED', or 'TREE_SYMBOL_REFERENCED' (on identifier), or
>> some such?  (All untested.)  Is there any specific reason that you
>> remember, or did it just happen to do the right thing?
>
> sorry, I don't remember, my best guess is that it happened to DTRT.

That's fair.

>> In an offloading test case (PR106445), I'm running into the case that a
>> C++ constructor alias gets called (via 'nvptx_output_call_insn', and per
>> that one's 'assemble_name' call, 'TREE_SYMBOL_REFERENCED' gets set, for
>> example) -- but that alias isn't '[cgraph]->referred_to_p ()', so no
>> PTX '.alias' gets emitted, resulting in link failure.  To make that work,
>> I might just add '|| TREE_SYMBOL_REFERENCED ([identifier])' next to the
>> existing cgraph 'referred_to_p ()', but I'd like to understand this
>> better.  For example, is it actually the very problem that this alias
>> isn't cgraph 'referred_to_p ()', but it should it be?  (I have not much
>> clue about the cgraph machinery generally, and even less about the
>> applicability of it/of its state in offloading compilation,
>> specifically.)
>
> Grepping though the source code for referred_to_p, I see that it is 
> sometimes used in conjunction with needed_p.  So I wonder if that one 
> returns true for the case you're describing.

No, that one ICEs.  ;-)

    lto1: internal compiler error: in needed_p, at cgraphunit.cc:247

    236     /* Determine if symbol declaration is needed.  That is, visible to 
something
    237        either outside this translation unit, something magic in the 
system
    238        configury */
    239     bool
    240     symtab_node::needed_p (void)
    241     {
    242       /* Double check that no one output the function into assembly file
    243          early.  */
    244       if (!native_rtl_p ())
    245           gcc_checking_assert
    246             (!DECL_ASSEMBLER_NAME_SET_P (decl)
    247              || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));

I intend to further look into this topic, later on, but for now have
pushed to trunk branch commit ca9cffe737d20953082333dacebb65d4261e0d0c
"For nvptx offloading, make sure to emit C++ constructor, destructor aliases 
[PR97106]",
see attached.


Grüße
 Thomas


>From ca9cffe737d20953082333dacebb65d4261e0d0c Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwi...@baylibre.com>
Date: Wed, 16 Apr 2025 14:00:31 +0200
Subject: [PATCH] For nvptx offloading, make sure to emit C++ constructor,
 destructor aliases [PR97106]

	PR target/97106
	gcc/
	* config/nvptx/nvptx.cc (nvptx_asm_output_def_from_decls)
	[ACCEL_COMPILER]: Make sure to emit C++ constructor, destructor
	aliases.
	libgomp/
	* testsuite/libgomp.c++/pr96390.C: Un-XFAIL nvptx offloading.
	* testsuite/libgomp.c-c++-common/pr96390.c: Adjust.
---
 gcc/config/nvptx/nvptx.cc                        | 12 ++++++++++++
 libgomp/testsuite/libgomp.c++/pr96390.C          |  2 --
 libgomp/testsuite/libgomp.c-c++-common/pr96390.c |  2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/config/nvptx/nvptx.cc b/gcc/config/nvptx/nvptx.cc
index 28da43ca740..d1e25b99701 100644
--- a/gcc/config/nvptx/nvptx.cc
+++ b/gcc/config/nvptx/nvptx.cc
@@ -7789,6 +7789,18 @@ nvptx_asm_output_def_from_decls (FILE *stream, tree name,
 #endif
 
   cgraph_node *cnode = cgraph_node::get (name);
+#ifdef ACCEL_COMPILER
+  /* For nvptx offloading, make sure to emit C++ constructor, destructor aliases [PR97106]
+
+     For some reason (yet to be analyzed), they're not 'cnode->referred_to_p ()'.
+     (..., or that's not the right approach at all;
+     <https://inbox.sourceware.org/87v7rx8lbx....@euler.schwinge.ddns.net>
+     "Re: [committed][nvptx] Use .alias directive for mptx >= 6.3").  */
+  if (DECL_CXX_CONSTRUCTOR_P (name)
+      || DECL_CXX_DESTRUCTOR_P (name))
+    ;
+  else
+#endif
   if (!cnode->referred_to_p ())
     /* Prevent "Internal error: reference to deleted section".  */
     return;
diff --git a/libgomp/testsuite/libgomp.c++/pr96390.C b/libgomp/testsuite/libgomp.c++/pr96390.C
index 1f3c3e05661..be1960186ea 100644
--- a/libgomp/testsuite/libgomp.c++/pr96390.C
+++ b/libgomp/testsuite/libgomp.c++/pr96390.C
@@ -1,6 +1,4 @@
 /* { dg-additional-options "-O0 -fdump-tree-omplower" } */
-/* { dg-additional-options "-foffload=-Wa,--verify" { target offload_target_nvptx } } */
-/* { dg-xfail-if "PR 97106/PR 97102 - .alias not (yet) supported for nvptx" { offload_target_nvptx } } */
 
 #include <cstdlib>
 #include <type_traits>
diff --git a/libgomp/testsuite/libgomp.c-c++-common/pr96390.c b/libgomp/testsuite/libgomp.c-c++-common/pr96390.c
index b89f934811a..ca7865d322d 100644
--- a/libgomp/testsuite/libgomp.c-c++-common/pr96390.c
+++ b/libgomp/testsuite/libgomp.c-c++-common/pr96390.c
@@ -1,7 +1,7 @@
 /* { dg-additional-options "-O0 -fdump-tree-omplower" } */
 /* { dg-additional-options "-foffload=-Wa,--verify" { target offload_target_nvptx } } */
 /* { dg-require-alias "" } */
-/* { dg-xfail-if "PR 97102/PR 97106 - .alias not (yet) supported for nvptx" { offload_target_nvptx } } */
+/* { dg-xfail-if PR105018 { offload_target_nvptx } } */
 
 #ifdef __cplusplus
 extern "C" {
-- 
2.34.1

Reply via email to