Hi!

On 2025-03-21T08:37:16-0400, Jason Merrill <ja...@redhat.com> wrote:
> On 3/20/25 12:48 PM, Thomas Schwinge wrote:
>> On 2025-03-20T11:59:07-0400, Jason Merrill <ja...@redhat.com> wrote:
>>> On 3/20/25 11:35 AM, Thomas Schwinge wrote:
>>>> Appears that I'm too dumb to implement
>>>> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101544#c22>:
>>>>
>>>> | We "simply" need to transform any symbol aliases into thunks calling the 
>>>> aliasee (or duplicate the bodies, if we must).
>>>>
>>>> ..., and implementing proper support for symbol aliases via the nvptx
>>>> 'ld': <https://gcc.gnu.org/PR105018> "[nvptx] Need better alias support",
>>>> won't be complete any time soon, but fortunately there's a way to handle
>>>> the cases that we're concerned about here in an even simpler way, for
>>>> reasonably modern configurations; any comments before I push the attached
>>>> "nvptx: In offloading compilation, special-case certain host-setup symbol 
>>>> aliases [PR101544]"?
>>>> In particular, is the use of a new 'c++ cdtor alias' attribute OK?
>>>
>>> Hmm, seems weird to use aliases in that one case but not others.
>> 
>> Yeah...  The problem is the mismatch between host and offload target:
>> the host of course typically is 'TARGET_SUPPORTS_ALIASES', but nvptx
>> offloading typically is '!TARGET_SUPPORTS_ALIASES', because:
>> 
>>> If
>>> you're going to use aliases, why not just turn on alias support?
>> 
>> We can't unconditionally enable GCC/nvptx' '-malias', because the support
>> is incomplete, and if enabled, will cause GCC target library build
>> failures, for example.
>> 
>>> If aliases aren't supported, I think you want to fix can_alias_cdtor to
>>> return false.
>> 
>> We can't: we don't want to pessimize host code generation.  Therefore, we
>> have to handle this in the nvptx offloading flow.
>
> Ah.
>
> Can you use DECL_CXX_{CON,DE}STRUCTOR_P instead of a new attribute?

Yes, thanks!  After re-testing, I've pushed to trunk branch
"nvptx: In offloading compilation, special-case certain host-setup symbol 
aliases [PR101544]",
see attached.


Grüße
 Thomas


>From 65b31b3fff2fced015ded1026733605f34053796 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwi...@baylibre.com>
Date: Thu, 20 Mar 2025 14:21:26 +0100
Subject: [PATCH] nvptx: In offloading compilation, special-case certain
 host-setup symbol aliases [PR101544]

Namely, use PTX '.alias' even for (default) '-mno-alias' if the host made the
C++ "base and complete [cd]tor aliases".

	PR target/101544
	gcc/
	* config/nvptx/nvptx.cc (nvptx_asm_output_def_from_decls)
	[ACCEL_COMPILER]: Special-case certain host-setup symbol aliases.
	* varasm.cc (do_assemble_alias) [ACCEL_COMPILER]: Adjust.
---
 gcc/config/nvptx/nvptx.cc | 28 +++++++++++++++++++++++++++-
 gcc/varasm.cc             |  7 ++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/gcc/config/nvptx/nvptx.cc b/gcc/config/nvptx/nvptx.cc
index 5cd4b2427714..022037f13627 100644
--- a/gcc/config/nvptx/nvptx.cc
+++ b/gcc/config/nvptx/nvptx.cc
@@ -245,7 +245,7 @@ default_ptx_version_option (void)
      warp convergence.  */
   res = MAX (res, PTX_VERSION_6_0);
 
-  /* Pick at least 6.3.  */
+  /* Pick at least 6.3, to enable PTX '.alias'.  */
   res = MAX (res, PTX_VERSION_6_3);
 
   /* For sm_52+, pick at least 7.3, to enable PTX 'alloca'.  */
@@ -7713,6 +7713,30 @@ nvptx_asm_output_def_from_decls (FILE *stream, tree name,
 {
   if (nvptx_alias == 0 || !TARGET_PTX_6_3)
     {
+      /* Symbol aliases are not supported here.  */
+
+#ifdef ACCEL_COMPILER
+      if (DECL_CXX_CONSTRUCTOR_P (name)
+	  || DECL_CXX_DESTRUCTOR_P (name))
+	{
+	  /* ..., but symbol aliases are supported and used in the host system,
+	     via 'gcc/cp/optimize.cc:can_alias_cdtor'.  */
+
+	  gcc_assert (!lookup_attribute ("weak", DECL_ATTRIBUTES (name)));
+	  gcc_assert (TREE_CODE (name) == FUNCTION_DECL);
+
+	  /* In this specific case, use PTX '.alias', if available, even for
+	     (default) '-mno-alias'.  */
+	  if (TARGET_PTX_6_3)
+	    {
+	      DECL_ATTRIBUTES (name)
+		= tree_cons (get_identifier ("symbol alias handled"),
+			     NULL_TREE, DECL_ATTRIBUTES (name));
+	      goto emit_ptx_alias;
+	    }
+	}
+#endif
+
       /* Copied from assemble_alias.  */
       error_at (DECL_SOURCE_LOCATION (name),
 		"alias definitions not supported in this configuration");
@@ -7744,6 +7768,8 @@ nvptx_asm_output_def_from_decls (FILE *stream, tree name,
       return;
     }
 
+ emit_ptx_alias:
+
   cgraph_node *cnode = cgraph_node::get (name);
   if (!cnode->referred_to_p ())
     /* Prevent "Internal error: reference to deleted section".  */
diff --git a/gcc/varasm.cc b/gcc/varasm.cc
index eddfb6a3524d..10c1d2e31371 100644
--- a/gcc/varasm.cc
+++ b/gcc/varasm.cc
@@ -6525,7 +6525,12 @@ do_assemble_alias (tree decl, tree target)
 		  IDENTIFIER_POINTER (target));
 # endif
   /* If symbol aliases aren't actually supported...  */
-  if (!TARGET_SUPPORTS_ALIASES)
+  if (!TARGET_SUPPORTS_ALIASES
+# ifdef ACCEL_COMPILER
+      /* ..., and unless special-cased...  */
+      && !lookup_attribute ("symbol alias handled", DECL_ATTRIBUTES (decl))
+# endif
+      )
     /* ..., 'ASM_OUTPUT_DEF{,_FROM_DECLS}' better have raised an error.  */
     gcc_checking_assert (seen_error ());
 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
-- 
2.34.1

Reply via email to