diff mbox series

nvptx: Add a __PTX_ISA__ predefined macro based on target ISA.

Message ID 001b01d7954d$32192c90$964b85b0$@nextmovesoftware.com
State New
Headers show
Series nvptx: Add a __PTX_ISA__ predefined macro based on target ISA. | expand

Commit Message

Roger Sayle Aug. 19, 2021, 10:54 p.m. UTC
This patch adds a __PTX_ISA__ predefined macro to the nvptx backend that
allows code to check the compute model being targeted by the compiler.
This is equivalent to the __CUDA_ARCH__ macro defined by CUDA's nvcc
compiler, but to avoid causing problems for source code that checks
for that compiler, this macro uses GCC's nomenclature; it's easy
enough for users to "#define __CUDA_ARCH__ __PTX_ISA__", but I'm
also happy to modify this patch to define __CUDA_ARCH__ if that's
the preference of the nvptx backend maintainers.

What might have been a four line patch is actually a little more
complicated, as this patch takes the opportunity to upgrade the
nvptx backend to use the now preferred nvptx-c.c idiom.

This patch has been tested with a cross-compiler from
x86_64-pc-linux-gnu to nvptx-none, and tested with
"make -k check" with no new failures.  This feature is
useful for implementing clock() on nvptx in newlib.

Ok for mainline?


2021-08-19  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* config.gcc (nvptx-*-*): Define {c,c++}_target_objs.
	* config/nvptx/nvptx-protos.h (nvptx_cpu_cpp_builtins): Prototype.
	* config/nvptx/nvptx.h (TARGET_CPU_CPP_BUILTINS): Implement with
	a call to the new nvptx_cpu_cpp_builtins function in nvptx-c.c.
	* config/nvptx/t-nvptx (nvptx-c.o): New rule.
	* config/nvptx/nvptx-c.c: New source file.
	(nvptx_cpu_cpp_builtins): Move implementation here.

Roger
--
Roger Sayle
NextMove Software
Cambridge, UK

Comments

Tom de Vries Aug. 24, 2021, 4:01 p.m. UTC | #1
On 8/20/21 12:54 AM, Roger Sayle wrote:
> 
> This patch adds a __PTX_ISA__ predefined macro to the nvptx backend that
> allows code to check the compute model being targeted by the compiler.

Hi Roger,

The naming __PTX_ISA__ is consistent with the naming of -misa=sm_30/sm_35.

The -misa=sm_30/sm_35 naming was very unfortunate given that the ptx
format actually defines an ISA version which gcc now accepts using
-mptx=3.1/6.3.

We really should have had something like:
- -march=sm_30/sm_35
- -mptx-isa=3.1/6.3
but I suppose it's too late to change that now.

Having said that, the __PTX_ISA__ name very much suggests that it's the
ptx ISA version, which, as explained above, it's not.  Sigh.

We could go for __PTX_ARCH__ instead, but it would be very
counterintuitive to have -misa=sm_30/sm_35 set this.

So I propose __PTX_SM__ instead.  [ I also considered __PTX_ISA_SM__ but
if we ever decide to change the name of the switch then that doesn't
make sense anymore. ]

> This is equivalent to the __CUDA_ARCH__ macro defined by CUDA's nvcc
> compiler, but to avoid causing problems for source code that checks
> for that compiler, this macro uses GCC's nomenclature; it's easy
> enough for users to "#define __CUDA_ARCH__ __PTX_ISA__", but I'm
> also happy to modify this patch to define __CUDA_ARCH__ if that's
> the preference of the nvptx backend maintainers.
> 

I agree with this approach.  The definition of the __CUDA_ARCH__ macro
in the cuda documentation is nvcc-specific, so let's not define it.

> What might have been a four line patch is actually a little more
> complicated, as this patch takes the opportunity to upgrade the
> nvptx backend to use the now preferred nvptx-c.c idiom.
> 

Ack.  You could split it up, but not strictly necessary.

> This patch has been tested with a cross-compiler from
> x86_64-pc-linux-gnu to nvptx-none, and tested with
> "make -k check" with no new failures.  This feature is
> useful for implementing clock() on nvptx in newlib.
> 

I see, thanks for working on that.

> Ok for mainline?

OK with name of predefined macro updated to __PTX_SM__ .

Thanks,
- Tom

> 2021-08-19  Roger Sayle  <roger@nextmovesoftware.com>
> 
> gcc/ChangeLog
> 	* config.gcc (nvptx-*-*): Define {c,c++}_target_objs.
> 	* config/nvptx/nvptx-protos.h (nvptx_cpu_cpp_builtins): Prototype.
> 	* config/nvptx/nvptx.h (TARGET_CPU_CPP_BUILTINS): Implement with
> 	a call to the new nvptx_cpu_cpp_builtins function in nvptx-c.c.
> 	* config/nvptx/t-nvptx (nvptx-c.o): New rule.
> 	* config/nvptx/nvptx-c.c: New source file.
> 	(nvptx_cpu_cpp_builtins): Move implementation here.
> 
> Roger
> --
> Roger Sayle
> NextMove Software
> Cambridge, UK
>
diff mbox series

Patch

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 93e2b32..cbad989 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -468,6 +468,8 @@  nios2-*-*)
 	;;
 nvptx-*-*)
 	cpu_type=nvptx
+	c_target_objs="nvptx-c.o"
+	cxx_target_objs="nvptx-c.o"
 	;;
 or1k*-*-*)
 	cpu_type=or1k
diff --git a/gcc/config/nvptx/nvptx-protos.h b/gcc/config/nvptx/nvptx-protos.h
index b7e6ae2..b29ddc9 100644
--- a/gcc/config/nvptx/nvptx-protos.h
+++ b/gcc/config/nvptx/nvptx-protos.h
@@ -40,6 +40,7 @@  extern void nvptx_output_aligned_decl (FILE *file, const char *name,
 extern void nvptx_function_end (FILE *);
 extern void nvptx_output_skip (FILE *, unsigned HOST_WIDE_INT);
 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);
 
diff --git a/gcc/config/nvptx/nvptx.h b/gcc/config/nvptx/nvptx.h
index fdaacdd..d367174 100644
--- a/gcc/config/nvptx/nvptx.h
+++ b/gcc/config/nvptx/nvptx.h
@@ -34,17 +34,7 @@ 
    nvptx-as.  */
 #define ASM_SPEC "%{misa=*:-m %*; :-m sm_35}"
 
-#define TARGET_CPU_CPP_BUILTINS()		\
-  do						\
-    {						\
-      builtin_assert ("machine=nvptx");		\
-      builtin_assert ("cpu=nvptx");		\
-      builtin_define ("__nvptx__");		\
-      if (TARGET_SOFT_STACK)			\
-        builtin_define ("__nvptx_softstack__");	\
-      if (TARGET_UNIFORM_SIMT)			\
-        builtin_define ("__nvptx_unisimt__");	\
-    } while (0)
+#define TARGET_CPU_CPP_BUILTINS() nvptx_cpu_cpp_builtins ()
 
 /* Avoid the default in ../../gcc.c, which adds "-pthread", which is not
    supported for nvptx.  */
diff --git a/gcc/config/nvptx/t-nvptx b/gcc/config/nvptx/t-nvptx
index 6c1010d..d33bacd 100644
--- a/gcc/config/nvptx/t-nvptx
+++ b/gcc/config/nvptx/t-nvptx
@@ -1,3 +1,7 @@ 
+nvptx-c.o: $(srcdir)/config/nvptx/nvptx-c.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
+
 CFLAGS-mkoffload.o += $(DRIVER_DEFINES) \
 	-DGCC_INSTALL_NAME=\"$(GCC_INSTALL_NAME)\"
 mkoffload.o: $(srcdir)/config/nvptx/mkoffload.c
diff --git a/gcc/config/nvptx/nvptx-c.c b/gcc/config/nvptx/nvptx-c.c
new file mode 100644
index 0000000..5a7a0ef
--- /dev/null
+++ b/gcc/config/nvptx/nvptx-c.c
@@ -0,0 +1,47 @@ 
+/* Subroutines for the C front end on the NVPTX architecture.
+ * Copyright (C) 2021 Free Software Foundation, Inc.
+ *
+ * This file is part of GCC.
+ *
+ * GCC is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 3, or (at your
+ * option) any later version.
+ *
+ * GCC is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GCC; see the file COPYING3.  If not see
+ * <http://www.gnu.org/licenses/>.  */
+
+#define IN_TARGET_CODE 1
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "target.h"
+#include "c-family/c-common.h"
+#include "memmodel.h"
+#include "tm_p.h"
+#include "c-family/c-pragma.h"
+
+/* Function to tell the preprocessor about the defines for the target.  */
+void
+nvptx_cpu_cpp_builtins (void)
+{
+  cpp_assert (parse_in, "machine=nvptx");
+  cpp_assert (parse_in, "cpu=nvptx");
+  cpp_define (parse_in, "__nvptx__");
+  if (TARGET_SOFT_STACK)
+    cpp_define (parse_in, "__nvptx_softstack__");
+  if (TARGET_UNIFORM_SIMT)
+    cpp_define (parse_in,"__nvptx_unisimt__");
+  if (TARGET_SM35)
+    cpp_define (parse_in, "__PTX_ISA__=350");
+  else
+    cpp_define (parse_in,"__PTX_ISA__=300");
+}
+