[PATCH] Emulate clock and other stubs on nvptx

"Roger Sayle" <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Please be gentle, this is my first patch to newlib.

This patch to the libc/machine/nvptx port of newlib implements an
approximation
of "clock" and provides some additional stub routines.  These changes not
only
reduce the number of (link) failures in the GCC testsuite when targeting
nvptx-none,
but also allow the NIST scimark4 benchmark to compile and run without
modification.

newlib already contains support for backends to provide their own clock
implementations
via -DCLOCK_PROVIDED.  That functionality is used here to return an
approximate
elapsed time based on the NVidia GPU's clock64 cycle counter.  Although not
great,
this is better than the current behaviour of link error from the unresolved
symbol
_times_r.

The other part of the patch is to add a small number of stub functions to
nvptx's
misc.c.  Adding isatty, for example, resolves linking problems in libc from
the
dependency in __smakebuf_r, and the sync stub, for example, fixes the
failure
with GCC's testsuite/gfortran.dg/ISO_Fortran_binding_14.f90 [which simply
tests that gfortran can call a/any C function].

Hopefully, these changes are acceptable.  If so, if someone could please
commit
them for me that would be much appreciated (as I don't have repository
access).
Likewise, these changes should be obvious enough to not require a copyright
assignment, but to avoid any objections, I'm happy to put them in the public
domain.

2021-08-18  Roger Sayle  <[email protected]>

newlib/
	configure.host: Add -DCLOCK_PROVIDED to newlib_cflags on nvptx*.

newlib/libc/machine/nvptx
	Makefile.am: Add clock.c to lib_a_SOURCES.
	clock.c: New source file to implement/approximate clock().
	misc.c: Add stubs for fstat, isatty, open, sync and unlink.
	Makefile.in: Regenerate.

Many thanks in advance,
Roger
--
Roger Sayle
NextMove Software Limited
Cambridge, UK
newlib.patch (application/octet-stream, 1.6 KB)
diff --git a/newlib/configure.host b/newlib/configure.host
index 4ac5334..ef481ff 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -289,7 +289,7 @@ case "${host_cpu}" in
 	;;
   nvptx*)
 	machine_dir=nvptx
-	newlib_cflags="${newlib_cflags} -DMALLOC_PROVIDED"
+	newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED"
 	;;
   or1k*|or1knd*)
 	machine_dir=or1k
diff --git a/newlib/libc/machine/nvptx/Makefile.am b/newlib/libc/machine/nvptx/Makefile.am
index 66664d3..0cb20cc 100644
--- a/newlib/libc/machine/nvptx/Makefile.am
+++ b/newlib/libc/machine/nvptx/Makefile.am
@@ -10,7 +10,7 @@ noinst_LIBRARIES = lib.a
 
 lib_a_SOURCES = calloc.c callocr.c malloc.c mallocr.c realloc.c reallocr.c \
 		free.c write.c assert.c puts.c putchar.c printf.c abort.c \
-		exit.c misc.c
+		exit.c misc.c clock.c
 lib_a_CFLAGS = $(AM_CFLAGS)
 
 ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
diff --git a/newlib/libc/machine/nvptx/misc.c b/newlib/libc/machine/nvptx/misc.c
index ed1bc34..3609241 100644
--- a/newlib/libc/machine/nvptx/misc.c
+++ b/newlib/libc/machine/nvptx/misc.c
@@ -15,6 +15,7 @@
 
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #undef errno
 extern int errno;
 
@@ -23,12 +24,37 @@ close(int fd) {
   return -1;
 }
 
+int
+fstat (int fd, struct stat *buf) {
+  return -1;
+}
+
+int
+isatty (int fd) {
+  return fd == 1;
+}
+
 off_t
 lseek(int fd, off_t offset, int whence) {
   return 0;
 }
 
 int
+open (const char *pathname, int flags, ...) {
+  return -1;
+}
+
+int
 read(int fd, void *buf, size_t count) {
   return 0;
 }
+
+void
+sync (void) {
+}
+
+int
+unlink (const char *pathname) {
+  return -1;
+}
+
clock.c (text/plain, 776 B)
/* clock.c
 * Support file for nvptx in newlib.
 */
#include <time.h>

/* PTX's clock64 is a cycle count, whose frequency varies with the GPU's
   operating frequency, which may be boosted or thermally throttled at
   run-time.  The value provided is suitable for relative timings, but
   must be corrected [by the host] for accurate elapsed/wall clock times.  */
/* NVidia RTX-2080 Super	1650-1815 MHz.  */
/* NVidia GTX-1070		1506-1683 MHz.  */
/* NVidia RTX-2070		1410 MHz.  */
/* NVidia GTX-1050 Ti		1290-1392 MHz.  */
/* NVidia Quadro P400		1228-1252 MHz.  */
/* NVidia GTX 660		823 MHz.   */

#define NVPTX_MHZ 1250

clock_t
clock ()
{
  unsigned long long now;
  asm volatile("mov.u64 %0, %%clock64;" : "=r"(now));
  return now/((NVPTX_MHZ*1000000)/CLOCKS_PER_SEC);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.