[nvptx] Use .alias directive for mptx >= 6.3
authorTom de Vries <tdevries@suse.de>
Fri, 11 Mar 2022 12:41:01 +0000 (13:41 +0100)
committerTom de Vries <tdevries@suse.de>
Tue, 22 Mar 2022 13:35:34 +0000 (14:35 +0100)
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.

gcc/config/nvptx/nvptx-protos.h
gcc/config/nvptx/nvptx.cc
gcc/config/nvptx/nvptx.h
gcc/config/nvptx/nvptx.opt
gcc/testsuite/gcc.target/nvptx/alias-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/nvptx/alias-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/nvptx/alias-3.c [new file with mode: 0644]
gcc/testsuite/gcc.target/nvptx/alias-4.c [new file with mode: 0644]
gcc/testsuite/gcc.target/nvptx/nvptx.exp

index 0bf9af4..ca0a87e 100644 (file)
@@ -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);
index f83f98c..b2f7b4a 100644 (file)
@@ -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
 
index b55ade6..75ac7a6 100644 (file)
@@ -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 "//"
 
index fea99c5..980428b 100644 (file)
@@ -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 (file)
index 0000000..f68716e
--- /dev/null
@@ -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 (file)
index 0000000..e2dc9b1
--- /dev/null
@@ -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 (file)
index 0000000..60486e5
--- /dev/null
@@ -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 (file)
index 0000000..956150a
--- /dev/null
@@ -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;" } } */
index 284ba41..e69b6d3 100644 (file)
@@ -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 {