diff mbox series

[committed,nvptx] Add __PTX_ISA_VERSION_{MAJOR,MINOR}__

Message ID 20220329141741.GA11440@delia.home
State New
Headers show
Series [committed,nvptx] Add __PTX_ISA_VERSION_{MAJOR,MINOR}__ | expand

Commit Message

Tom de Vries March 29, 2022, 2:17 p.m. UTC
Hi,

Add preprocessor macros __PTX_ISA_VERSION_MAJOR__ and
__PTX_ISA_VERSION_MINOR__.

For the default 6.0, we have:
...
 $ echo | cc1 -E -dD - 2>&1 | grep PTX_ISA_VERSION
 #define __PTX_ISA_VERSION_MAJOR__ 6
 #define __PTX_ISA_VERSION_MINOR__ 0
...
and for 3.1, we have:
...
 $ echo | cc1 -mptx=3.1 -E -dD - 2>&1 | grep PTX_ISA_VERSION
 #define __PTX_ISA_VERSION_MAJOR__ 3
 #define __PTX_ISA_VERSION_MINOR__ 1
...

These can be used to express things like:
...
 #if __PTX_ISA_VERSION_MAJOR__ >= 4 && __PTX_ISA_VERSION_MAJOR__ >= 1
   /* Code using %dynamic_smem_size.  */
 #else
   /* Fallback code.  */
 #endif
...

Tested on nvptx.

Committed to trunk.

Thanks,
- Tom

[nvptx] Add __PTX_ISA_VERSION_{MAJOR,MINOR}__

gcc/ChangeLog:

2022-03-29  Tom de Vries  <tdevries@suse.de>

	PR target/104857
	* config/nvptx/nvptx-c.cc (nvptx_cpu_cpp_builtins): Emit
	__PTX_ISA_VERSION_MAJOR__ and __PTX_ISA_VERSION_MINOR__.
	* config/nvptx/nvptx.cc (ptx_version_to_number): New function.
	* config/nvptx/nvptx-protos.h (ptx_version_to_number): Declare.

gcc/testsuite/ChangeLog:

2022-03-29  Tom de Vries  <tdevries@suse.de>

	PR target/104857
	* gcc.target/nvptx/ptx31.c: New test.
	* gcc.target/nvptx/ptx60.c: New test.
	* gcc.target/nvptx/ptx63.c: New test.
	* gcc.target/nvptx/ptx70.c: New test.

---
 gcc/config/nvptx/nvptx-c.cc            |  9 +++++++++
 gcc/config/nvptx/nvptx-protos.h        |  1 +
 gcc/config/nvptx/nvptx.cc              | 22 ++++++++++++++++++++++
 gcc/testsuite/gcc.target/nvptx/ptx31.c | 10 ++++++++++
 gcc/testsuite/gcc.target/nvptx/ptx60.c | 10 ++++++++++
 gcc/testsuite/gcc.target/nvptx/ptx63.c | 10 ++++++++++
 gcc/testsuite/gcc.target/nvptx/ptx70.c | 10 ++++++++++
 7 files changed, 72 insertions(+)
diff mbox series

Patch

diff --git a/gcc/config/nvptx/nvptx-c.cc b/gcc/config/nvptx/nvptx-c.cc
index 02f75625064..f060a8ab1d4 100644
--- a/gcc/config/nvptx/nvptx-c.cc
+++ b/gcc/config/nvptx/nvptx-c.cc
@@ -49,5 +49,14 @@  nvptx_cpu_cpp_builtins (void)
 #include "nvptx-sm.def"
 #undef NVPTX_SM
   cpp_define (parse_in, ptx_sm);
+
+  {
+    unsigned major
+      = ptx_version_to_number ((ptx_version)ptx_version_option, true);
+    unsigned minor
+      = ptx_version_to_number ((ptx_version)ptx_version_option, false);
+    cpp_define_formatted (parse_in, "__PTX_ISA_VERSION_MAJOR__=%u", major);
+    cpp_define_formatted (parse_in, "__PTX_ISA_VERSION_MINOR__=%u", minor);
+  }
 }
 
diff --git a/gcc/config/nvptx/nvptx-protos.h b/gcc/config/nvptx/nvptx-protos.h
index ca0a87ee4bd..dfa08ec8319 100644
--- a/gcc/config/nvptx/nvptx-protos.h
+++ b/gcc/config/nvptx/nvptx-protos.h
@@ -44,6 +44,7 @@  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);
+extern unsigned int ptx_version_to_number (enum ptx_version, bool);
 
 #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 87efc23bd96..e4297e2d6c3 100644
--- a/gcc/config/nvptx/nvptx.cc
+++ b/gcc/config/nvptx/nvptx.cc
@@ -272,6 +272,28 @@  ptx_version_to_string (enum ptx_version v)
     }
 }
 
+unsigned int
+ptx_version_to_number (enum ptx_version v, bool major_p)
+{
+  switch (v)
+    {
+    case PTX_VERSION_3_0:
+      return major_p ? 3 : 0;
+    case PTX_VERSION_3_1:
+      return major_p ? 3 : 1;
+    case PTX_VERSION_4_2:
+      return major_p ? 4 : 2;
+    case PTX_VERSION_6_0:
+      return major_p ? 6 : 0;
+    case PTX_VERSION_6_3:
+      return major_p ? 6 : 3;
+    case PTX_VERSION_7_0:
+      return major_p ? 7 : 0;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 static const char *
 sm_version_to_string (enum ptx_isa sm)
 {
diff --git a/gcc/testsuite/gcc.target/nvptx/ptx31.c b/gcc/testsuite/gcc.target/nvptx/ptx31.c
new file mode 100644
index 00000000000..46b5e1ba405
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/ptx31.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=sm_30 -mptx=3.1" } */
+
+#if __PTX_ISA_VERSION_MAJOR__ != 3
+#error wrong value for __PTX_ISA_VERSION_MAJOR__
+#endif
+
+#if __PTX_ISA_VERSION_MINOR__ != 1
+#error wrong value for __PTX_ISA_VERSION_MINOR__
+#endif
diff --git a/gcc/testsuite/gcc.target/nvptx/ptx60.c b/gcc/testsuite/gcc.target/nvptx/ptx60.c
new file mode 100644
index 00000000000..267a9c64f1e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/ptx60.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=sm_30 -mptx=6.0" } */
+
+#if __PTX_ISA_VERSION_MAJOR__ != 6
+#error wrong value for __PTX_ISA_VERSION_MAJOR__
+#endif
+
+#if __PTX_ISA_VERSION_MINOR__ != 0
+#error wrong value for __PTX_ISA_VERSION_MINOR__
+#endif
diff --git a/gcc/testsuite/gcc.target/nvptx/ptx63.c b/gcc/testsuite/gcc.target/nvptx/ptx63.c
new file mode 100644
index 00000000000..13d02e132ae
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/ptx63.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=sm_30 -mptx=6.3" } */
+
+#if __PTX_ISA_VERSION_MAJOR__ != 6
+#error wrong value for __PTX_ISA_VERSION_MAJOR__
+#endif
+
+#if __PTX_ISA_VERSION_MINOR__ != 3
+#error wrong value for __PTX_ISA_VERSION_MINOR__
+#endif
diff --git a/gcc/testsuite/gcc.target/nvptx/ptx70.c b/gcc/testsuite/gcc.target/nvptx/ptx70.c
new file mode 100644
index 00000000000..15df13604bd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/nvptx/ptx70.c
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=sm_30 -mptx=7.0" } */
+
+#if __PTX_ISA_VERSION_MAJOR__ != 7
+#error wrong value for __PTX_ISA_VERSION_MAJOR__
+#endif
+
+#if __PTX_ISA_VERSION_MINOR__ != 0
+#error wrong value for __PTX_ISA_VERSION_MINOR__
+#endif