Allow using __attribute__((shared)) to place static variables in '.shared'
memory space.

Changes in v2:
- reword diagnostic message in nvptx_handle_shared_attribute to follow other
  backends ("... attribute not allowed with auto storage class");
- reject explicit initialization of ".shared" memory variables;
- add testcases.

testsuite/

2016-05-06  Alexander Monakov  <amona...@ispras.ru>

        * gcc.target/nvptx/decl-shared.c: New test.
        * gcc.target/nvptx/decl-shared-init.c: New test.

gcc/

2016-05-06  Alexander Monakov  <amona...@ispras.ru>

        * config/nvptx/nvptx.c (nvptx_encode_section_info): Diagnose explicit
        static initialization of variables in .shared memory. 
        (nvptx_handle_shared_attribute): Reword diagnostic message.       

2016-04-19  Alexander Monakov  <amona...@ispras.ru>

        * doc/extend.texi (Nvidia PTX Variable Attributes): New section.

2016-01-17  Alexander Monakov  <amona...@ispras.ru>

        * config/nvptx/nvptx.c (nvptx_encode_section_info): Handle "shared"
        attribute.
        (nvptx_handle_shared_attribute): New.  Use it...
        (nvptx_attribute_table): ... here (new entry).


diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c
index 2d4dad1..e9e4d06 100644
--- a/gcc/config/nvptx/nvptx.c
+++ b/gcc/config/nvptx/nvptx.c
@@ -234,9 +224,17 @@ nvptx_encode_section_info (tree decl, rtx rtl, int first)
       if (TREE_CONSTANT (decl))
        area = DATA_AREA_CONST;
       else if (TREE_CODE (decl) == VAR_DECL)
-       /* TODO: This would be a good place to check for a .shared or
-          other section name.  */
-       area = TREE_READONLY (decl) ? DATA_AREA_CONST : DATA_AREA_GLOBAL;
+       {
+         if (lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
+           {
+             area = DATA_AREA_SHARED;
+             if (DECL_INITIAL (decl))
+               error ("static initialization of variable %q+D in %<.shared%>"
+                      " memory is not supported", decl);
+           }
+         else
+           area = TREE_READONLY (decl) ? DATA_AREA_CONST : DATA_AREA_GLOBAL;
+       }

       SET_SYMBOL_DATA_AREA (XEXP (rtl, 0), area);
     }
@@ -3805,12 +4025,36 @@ nvptx_handle_kernel_attribute (tree *node, tree name, 
tree ARG_UNUSED (args),
   return NULL_TREE;
 }
 
+/* Handle a "shared" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+nvptx_handle_shared_attribute (tree *node, tree name, tree ARG_UNUSED (args),
+                              int ARG_UNUSED (flags), bool *no_add_attrs)
+{
+  tree decl = *node;
+
+  if (TREE_CODE (decl) != VAR_DECL)
+    {
+      error ("%qE attribute only applies to variables", name);
+      *no_add_attrs = true;
+    }
+  else if (current_function_decl && !TREE_STATIC (decl))
+    {
+      error ("%qE attribute not allowed with auto storage class", name);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
+
 /* Table of valid machine attributes.  */
 static const struct attribute_spec nvptx_attribute_table[] =
 {
   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
        affects_type_identity } */
   { "kernel", 0, 0, true, false,  false, nvptx_handle_kernel_attribute, false 
},
+  { "shared", 0, 0, true, false,  false, nvptx_handle_shared_attribute, false 
},
   { NULL, 0, 0, false, false, false, NULL, false }
 };
 
diff --git a/gcc/testsuite/gcc.target/nvptx/decl-shared-init.c 
b/gcc/testsuite/gcc.target/nvptx/decl-shared-init.c
new file mode 100644
index 0000000..6a99b1c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/decl-shared-init.c
@@ -0,0 +1 @@
+int var __attribute__((shared)) = 0; /* { dg-error "static initialization .* 
not supported" } */
diff --git a/gcc/testsuite/gcc.target/nvptx/decl-shared.c 
b/gcc/testsuite/gcc.target/nvptx/decl-shared.c
new file mode 100644
index 0000000..367075c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/decl-shared.c
@@ -0,0 +1,14 @@
+static int v_internal __attribute__((shared,used));
+int v_common __attribute__((shared));
+int v_extdef __attribute__((shared,nocommon));
+extern int v_extdecl __attribute__((shared));
+
+int use()
+{
+  return v_extdecl;
+}
+
+/* { dg-final { scan-assembler "\[\r\n\]\[\t \]*.shared \[^,\r\n\]*v_internal" 
} } */
+/* { dg-final { scan-assembler "\[\r\n\]\[\t \]*.weak .shared 
\[^,\r\n\]*v_common" } } */
+/* { dg-final { scan-assembler "\[\r\n\]\[\t \]*.visible .shared 
\[^,\r\n\]*v_extdef" } } */
+/* { dg-final { scan-assembler "\[\r\n\]\[\t \]*.extern .shared 
\[^,\r\n\]*v_extdecl" } } */
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index e11ce4d..5eeb179 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5469,6 +5469,7 @@ attributes.
 * MeP Variable Attributes::
 * Microsoft Windows Variable Attributes::
 * MSP430 Variable Attributes::
+* Nvidia PTX Variable Attributes::
 * PowerPC Variable Attributes::
 * RL78 Variable Attributes::
 * SPU Variable Attributes::
@@ -6099,6 +6100,20 @@ same name (@pxref{MSP430 Function Attributes}).
 These attributes can be applied to both functions and variables.
 @end table
 
+@node Nvidia PTX Variable Attributes
+@subsection Nvidia PTX Variable Attributes
+
+These variable attributes are supported by the Nvidia PTX back end:
+
+@table @code
+@item shared
+@cindex @code{shared} attribute, Nvidia PTX
+Use this attribute to place a variable in the @code{.shared} memory space.
+This memory space is private to each cooperative thread array; only threads
+within one thread block refer to the same instance of the variable.
+The runtime does not initialize variables in this memory space.
+@end table
+
 @node PowerPC Variable Attributes
 @subsection PowerPC Variable Attributes
 

Reply via email to