public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7766] [nvptx] Use .alias directive for mptx >= 6.3
@ 2022-03-22 13:40 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2022-03-22 13:40 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f8b15e177155960017ac0c5daef8780d1127f91c

commit r12-7766-gf8b15e177155960017ac0c5daef8780d1127f91c
Author: Tom de Vries <tdevries@suse.de>
Date:   Fri Mar 11 13:41:01 2022 +0100

    [nvptx] Use .alias directive for mptx >= 6.3
    
    Starting with ptx isa version 6.3, a ptx directive .alias is available.
    Use this directive to support symbol aliases, as far as possible.
    
    The alias support is off by default.  It can be turned on using a switch
    -malias.
    
    Furthermore, for pre-sm_75, it's not effective unless the ptx version is
    bumped to 6.3 or higher using -mptx (given that the default for pre-sm_75 is
    6.0).
    
    The alias support has the following limitations.
    
    Only function aliases are supported.
    
    Weak aliases are not supported.  That is, if I disable the check in
    nvptx_asm_output_def_from_decls that disallows this, a weak alias is emitted
    and parsed by the driver.  But the test gcc.dg/globalalias.c starts failing,
    with the behaviour matching the comment about "weird behavior of AIX's .set
    pseudo-op": a weak alias may resolve to different functions in different
    files.
    
    Aliases to weak symbols are not supported (see gcc.dg/localalias.c).  This is
    currently not prohibited by the compiler, but with the driver link we run
    into: "error: Function test with .weak scope cannot be aliased".
    
    Aliases to aliases are not supported (see libgomp.c-c++-common/pr96390.c).
    This is currently not prohibited by the compiler, but with the driver link we
    run into:  "Internal error: alias to unknown symbol" .
    
    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".
    
    When enabling malias by default, libgomp detects alias support and
    consequently libgomp.a will contains a few uses of .alias.  This however
    results in aforementioned "Internal error: reference to deleted section" in
    many test-cases.  Either there's some error with how .alias is used, or
    there's a driver bug.  While this issue is not resolved, we keep malias
    off-by-default.
    
    At some point we may add support in the nvptx-tools linker for symbol
    aliases, and define f.i. malias=ptx and malias=ld to choose between the two in
    the compiler.
    
    An example of where this support is useful, is the OvO (OpenMP vs Offload)
    testsuite.  The testsuite passes already at -O2.  But at -O0, there are errors
    in some c++ test-cases due to missing symbol alias support.  By compiling with
    -malias, the whole testsuite passes also at -O0.
    
    This patch causes a regression:
    ...
    -PASS: gcc.dg/pr60797.c  (test for errors, line 4)
    +FAIL: gcc.dg/pr60797.c  (test for errors, line 4)
    ...
    The test-case is skipped for effective target alias, and both without and with
    this patch the nvptx target is considered to not support it, so the test-case is
    executed.  The test-case expects an error message along the lines of "alias
    definitions not supported in this configuration", but instead we run into:
    ...
    gcc.dg/pr60797.c:4:12: error: foo aliased to undefined symbol
    ...
    This is probably due to the fact that the nvptx backend now defines macros
    ASM_OUTPUT_DEF and ASM_OUTPUT_DEF_FROM_DECLS, so from the point of view of the
    common part of the compiler, aliases are supported.
    
    gcc/ChangeLog:
    
    2022-03-18  Tom de Vries  <tdevries@suse.de>
    
            PR target/104957
            * config/nvptx/nvptx-protos.h (nvptx_asm_output_def_from_decls): Declare.
            * config/nvptx/nvptx.cc (write_fn_proto_1): Don't add function marker
            for alias.
            (SET_ASM_OP, NVPTX_ASM_OUTPUT_DEF): New macro def.
            (nvptx_asm_output_def_from_decls): New function.
            * config/nvptx/nvptx.h (ASM_OUTPUT_DEF): New macro def, define to
            gcc_unreachable ().
            (ASM_OUTPUT_DEF_FROM_DECLS): New macro def, define to
            nvptx_asm_output_def_from_decls.
            * config/nvptx/nvptx.opt (malias): New opt.
    
    gcc/testsuite/ChangeLog:
    
    2022-03-18  Tom de Vries  <tdevries@suse.de>
    
            PR target/104957
            * gcc.target/nvptx/alias-1.c: New test.
            * gcc.target/nvptx/alias-2.c: New test.
            * gcc.target/nvptx/alias-3.c: New test.
            * gcc.target/nvptx/alias-4.c: New test.
            * gcc.target/nvptx/nvptx.exp
            (check_effective_target_runtime_ptx_isa_version_6_3): New proc.

Diff:
---
 gcc/config/nvptx/nvptx-protos.h          |  1 +
 gcc/config/nvptx/nvptx.cc                | 74 +++++++++++++++++++++++++++++++-
 gcc/config/nvptx/nvptx.h                 | 17 ++++++++
 gcc/config/nvptx/nvptx.opt               |  3 ++
 gcc/testsuite/gcc.target/nvptx/alias-1.c | 27 ++++++++++++
 gcc/testsuite/gcc.target/nvptx/alias-2.c | 13 ++++++
 gcc/testsuite/gcc.target/nvptx/alias-3.c | 29 +++++++++++++
 gcc/testsuite/gcc.target/nvptx/alias-4.c | 12 ++++++
 gcc/testsuite/gcc.target/nvptx/nvptx.exp |  7 +++
 9 files changed, 182 insertions(+), 1 deletion(-)

diff --git a/gcc/config/nvptx/nvptx-protos.h b/gcc/config/nvptx/nvptx-protos.h
index 0bf9af406a2..ca0a87ee4bd 100644
--- a/gcc/config/nvptx/nvptx-protos.h
+++ b/gcc/config/nvptx/nvptx-protos.h
@@ -43,6 +43,7 @@ extern void nvptx_output_ascii (FILE *, const char *, unsigned HOST_WIDE_INT);
 extern void nvptx_cpu_cpp_builtins (void);
 extern void nvptx_register_pragmas (void);
 extern unsigned int nvptx_data_alignment (const_tree, unsigned int);
+extern void nvptx_asm_output_def_from_decls (FILE *, tree, tree);
 
 #ifdef RTX_CODE
 extern void nvptx_expand_oacc_fork (unsigned);
diff --git a/gcc/config/nvptx/nvptx.cc b/gcc/config/nvptx/nvptx.cc
index f83f98c3ab5..b2f7b4af392 100644
--- a/gcc/config/nvptx/nvptx.cc
+++ b/gcc/config/nvptx/nvptx.cc
@@ -77,6 +77,7 @@
 #include "opts.h"
 #include "tree-pretty-print.h"
 #include "rtl-iter.h"
+#include "cgraph.h"
 
 /* This file should be included last.  */
 #include "target-def.h"
@@ -968,7 +969,8 @@ static void
 write_fn_proto_1 (std::stringstream &s, bool is_defn,
 		  const char *name, const_tree decl)
 {
-  write_fn_marker (s, is_defn, TREE_PUBLIC (decl), name);
+  if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) == NULL)
+    write_fn_marker (s, is_defn, TREE_PUBLIC (decl), name);
 
   /* PTX declaration.  */
   if (DECL_EXTERNAL (decl))
@@ -7393,6 +7395,76 @@ nvptx_mem_local_p (rtx mem)
   return false;
 }
 
+/* Define locally, for use in NVPTX_ASM_OUTPUT_DEF.  */
+#define SET_ASM_OP ".alias "
+
+/* Define locally, for use in nvptx_asm_output_def_from_decls.  Add NVPTX_
+   prefix to avoid clash with ASM_OUTPUT_DEF from nvptx.h.
+   Copy of ASM_OUTPUT_DEF from defaults.h, with added terminating
+   semicolon.  */
+#define NVPTX_ASM_OUTPUT_DEF(FILE,LABEL1,LABEL2)	\
+  do							\
+    {							\
+      fprintf ((FILE), "%s", SET_ASM_OP);		\
+      assemble_name (FILE, LABEL1);			\
+      fprintf (FILE, ",");				\
+      assemble_name (FILE, LABEL2);			\
+      fprintf (FILE, ";\n");				\
+    }							\
+  while (0)
+
+void
+nvptx_asm_output_def_from_decls (FILE *stream, tree name, tree value)
+{
+  if (nvptx_alias == 0 || !TARGET_PTX_6_3)
+    {
+      /* Copied from assemble_alias.  */
+      error_at (DECL_SOURCE_LOCATION (name),
+		"alias definitions not supported in this configuration");
+      TREE_ASM_WRITTEN (name) = 1;
+      return;
+    }
+
+  if (lookup_attribute ("weak", DECL_ATTRIBUTES (name)))
+    {
+      /* Prevent execution FAILs for gcc.dg/globalalias.c and
+	 gcc.dg/pr77587.c.  */
+      error_at (DECL_SOURCE_LOCATION (name),
+		"weak alias definitions not supported in this configuration");
+      TREE_ASM_WRITTEN (name) = 1;
+      return;
+    }
+
+  /* Ptx also doesn't support value having weak linkage, but we can't detect
+     that here, so we'll end up with:
+     "error: Function test with .weak scope cannot be aliased".
+     See gcc.dg/localalias.c.  */
+
+  if (TREE_CODE (name) != FUNCTION_DECL)
+    {
+      error_at (DECL_SOURCE_LOCATION (name),
+		"non-function alias definitions not supported"
+		" in this configuration");
+      TREE_ASM_WRITTEN (name) = 1;
+      return;
+    }
+
+  if (!cgraph_node::get (name)->referred_to_p ())
+    /* Prevent "Internal error: reference to deleted section".  */
+    return;
+
+  std::stringstream s;
+  write_fn_proto (s, false, get_fnname_from_decl (name), name);
+  fputs (s.str ().c_str (), stream);
+
+  tree id = DECL_ASSEMBLER_NAME (name);
+  NVPTX_ASM_OUTPUT_DEF (stream, IDENTIFIER_POINTER (id),
+			IDENTIFIER_POINTER (value));
+}
+
+#undef NVPTX_ASM_OUTPUT_DEF
+#undef SET_ASM_OP
+
 #undef TARGET_OPTION_OVERRIDE
 #define TARGET_OPTION_OVERRIDE nvptx_option_override
 
diff --git a/gcc/config/nvptx/nvptx.h b/gcc/config/nvptx/nvptx.h
index b55ade65cc5..75ac7a666b1 100644
--- a/gcc/config/nvptx/nvptx.h
+++ b/gcc/config/nvptx/nvptx.h
@@ -315,6 +315,23 @@ struct GTY(()) machine_function
   ((VALUE) = GET_MODE_BITSIZE ((MODE)), 2)
 
 #define SUPPORTS_WEAK 1
+
+/* The documentation states that ASM_OUTPUT_DEF_FROM_DECLS is used in
+   preference to ASM_OUTPUT_DEF if the tree nodes are available.  However, we
+   need the tree nodes to emit the prototype, so at this point it's not clear
+   how we can support ASM_OUTPUT_DEF.  Still, we need to define it, or
+   ASM_OUTPUT_DEF_FROM_DECLS is ignored.  For now, assert, and once we run
+   into it possibly improve by somehow emitting the prototype elsewhere, or
+   emitting a reasonable error message.  */
+#define ASM_OUTPUT_DEF(FILE,LABEL1,LABEL2)	\
+  do						\
+    {						\
+      gcc_unreachable ();			\
+    }						\
+  while (0)
+#define ASM_OUTPUT_DEF_FROM_DECLS(STREAM, NAME, VALUE)	\
+  nvptx_asm_output_def_from_decls (STREAM, NAME, VALUE)
+
 #define NO_DOT_IN_LABEL
 #define ASM_COMMENT_START "//"
 
diff --git a/gcc/config/nvptx/nvptx.opt b/gcc/config/nvptx/nvptx.opt
index fea99c5d406..980428b58cc 100644
--- a/gcc/config/nvptx/nvptx.opt
+++ b/gcc/config/nvptx/nvptx.opt
@@ -85,3 +85,6 @@ Initialize ptx registers.
 
 mptx-comment
 Target Var(nvptx_comment) Init(1) Undocumented
+
+malias
+Target Var(nvptx_alias) Init(0) Undocumented
diff --git a/gcc/testsuite/gcc.target/nvptx/alias-1.c b/gcc/testsuite/gcc.target/nvptx/alias-1.c
new file mode 100644
index 00000000000..f68716e77dd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/alias-1.c
@@ -0,0 +1,27 @@
+/* { dg-do link } */
+/* { dg-do run { target runtime_ptx_isa_version_6_3 } } */
+/* { dg-options "-save-temps -malias -mptx=6.3" } */
+
+int v;
+
+void __f ()
+{
+  v = 1;
+}
+
+void f () __attribute__ ((alias ("__f")));
+
+int
+main (void)
+{
+  if (v != 0)
+    __builtin_abort ();
+  f ();
+  if (v != 1)
+    __builtin_abort ();
+  return 0;
+}
+
+/* { dg-final { scan-assembler-times "\\.alias f,__f;" 1 } } */
+/* { dg-final { scan-assembler-times "\\.visible \\.func __f;" 1 } } */
+/* { dg-final { scan-assembler-times "\\.visible \\.func f;" 1 } } */
diff --git a/gcc/testsuite/gcc.target/nvptx/alias-2.c b/gcc/testsuite/gcc.target/nvptx/alias-2.c
new file mode 100644
index 00000000000..e2dc9b1f5ac
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/alias-2.c
@@ -0,0 +1,13 @@
+/* { dg-do link } */
+/* { dg-do run { target runtime_ptx_isa_version_6_3 } } */
+/* { dg-options "-save-temps -malias -mptx=6.3 -O2" } */
+
+#include "alias-1.c"
+
+/* Inlined, so no alias.  */
+/* { dg-final { scan-assembler-not "\\.alias.*;" } } */
+/* { dg-final { scan-assembler-not "\\.visible \\.func f;" } } */
+
+/* Note static and inlined, so still there.  */
+/* { dg-final { scan-assembler-times "\\.visible \\.func __f;" 1 } } */
+
diff --git a/gcc/testsuite/gcc.target/nvptx/alias-3.c b/gcc/testsuite/gcc.target/nvptx/alias-3.c
new file mode 100644
index 00000000000..60486e50826
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/alias-3.c
@@ -0,0 +1,29 @@
+/* { dg-do link } */
+/* { dg-do run { target runtime_ptx_isa_version_6_3 } } */
+/* { dg-options "-save-temps -malias -mptx=6.3" } */
+
+/* Copy of alias-1.c, with static __f and f.  */
+
+int v;
+
+static void __f ()
+{
+  v = 1;
+}
+
+static void f () __attribute__ ((alias ("__f")));
+
+int
+main (void)
+{
+  if (v != 0)
+    __builtin_abort ();
+  f ();
+  if (v != 1)
+    __builtin_abort ();
+  return 0;
+}
+
+/* { dg-final { scan-assembler-times "\\.alias f,__f;" 1 } } */
+/* { dg-final { scan-assembler-times "\\.func __f;" 1 } } */
+/* { dg-final { scan-assembler-times "\\.func f;" 1 } } */
diff --git a/gcc/testsuite/gcc.target/nvptx/alias-4.c b/gcc/testsuite/gcc.target/nvptx/alias-4.c
new file mode 100644
index 00000000000..956150a6b3f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/alias-4.c
@@ -0,0 +1,12 @@
+/* { dg-do link } */
+/* { dg-do run { target runtime_ptx_isa_version_6_3 } } */
+/* { dg-options "-save-temps -malias -mptx=6.3 -O2" } */
+
+#include "alias-3.c"
+
+/* Inlined, so no alias.  */
+/* { dg-final { scan-assembler-not "\\.alias.*;" } } */
+/* { dg-final { scan-assembler-not "\\.func f;" } } */
+
+/* Static and inlined, so it's deleted.  */
+/* { dg-final { scan-assembler-not "\\.func __f;" } } */
diff --git a/gcc/testsuite/gcc.target/nvptx/nvptx.exp b/gcc/testsuite/gcc.target/nvptx/nvptx.exp
index 284ba41908e..e69b6d35fed 100644
--- a/gcc/testsuite/gcc.target/nvptx/nvptx.exp
+++ b/gcc/testsuite/gcc.target/nvptx/nvptx.exp
@@ -25,6 +25,13 @@ if ![istarget nvptx*-*-*] then {
 # Load support procs.
 load_lib gcc-dg.exp
 
+# Return 1 if code with -mptx=6.3 can be run.
+proc check_effective_target_runtime_ptx_isa_version_6_3 { args } {
+    return [check_runtime run_ptx_isa_6_3 {
+	int main (void) { return 0; }
+    } "-mptx=6.3"]
+}
+
 # If a testcase doesn't have special options, use these.
 global DEFAULT_CFLAGS
 if ![info exists DEFAULT_CFLAGS] then {


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-22 13:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22 13:40 [gcc r12-7766] [nvptx] Use .alias directive for mptx >= 6.3 Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).