[PATCH] andgcn: SIMT malloc

Andrew Stubbs ams@baylibre.com
Wed Nov 11 11:15:35 GMT 2026


Ping.

BTW, I spotted a typo in my subject line; it should be "amdgcn", of course.

On 05/11/2026 13:47, Andrew Stubbs wrote:
> This patch serializes all calls to malloc functions when called from multiple
> SIMT threads simultaneously.
> 
> It would be nice to optimize such calls by coalescing all the requests into one
> malloc call, but that would require the free calls to be called in exactly the
> same combination, which cannot be guaranteed.
> 
> ---
> The SIMT support in GCC is still experimental and under review, but it'll be
> easier to proceed with that with the Newlib support already present.
> 
> I will probably get to other SIMT compatibility issues in the near future
> (e.g. printf).
> 
> Thanks
> 
> Andrew
> 
> ---
>   newlib/Makefile.in                       |  20 ++
>   newlib/configure                         |   4 +
>   newlib/libc/machine/amdgcn/Makefile.inc  |   1 +
>   newlib/libc/machine/amdgcn/mlock.c       |   8 +
>   newlib/libc/machine/amdgcn/simt-malloc.c | 276 +++++++++++++++++++++++
>   newlib/libm/machine/amdgcn/acinclude.m4  |   5 +
>   6 files changed, 314 insertions(+)
>   create mode 100644 newlib/libc/machine/amdgcn/simt-malloc.c
> 
> diff --git a/newlib/Makefile.in b/newlib/Makefile.in
> index 13a5d13ed..d46bb755e 100644
> --- a/newlib/Makefile.in
> +++ b/newlib/Makefile.in
> @@ -667,6 +667,7 @@ check_PROGRAMS =
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/abort.c \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/atexit.c \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/mlock.c \
> +@HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/simt-malloc.c \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/getreent.c \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/signal.c
>   
> @@ -1965,6 +1966,7 @@ am__objects_51 = libc/ssp/libc_a-chk_fail.$(OBJEXT) \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-abort.$(OBJEXT) \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-atexit.$(OBJEXT) \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-mlock.$(OBJEXT) \
> +@HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-simt-malloc.$(OBJEXT) \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-getreent.$(OBJEXT) \
>   @HAVE_LIBC_MACHINE_AMDGCN_TRUE@	libc/machine/amdgcn/libc_a-signal.$(OBJEXT)
>   @HAVE_LIBC_MACHINE_ARC_TRUE@am__objects_79 = libc/machine/arc/libc_a-memcmp.$(OBJEXT) \
> @@ -8346,6 +8348,9 @@ libc/machine/amdgcn/libc_a-atexit.$(OBJEXT):  \
>   libc/machine/amdgcn/libc_a-mlock.$(OBJEXT):  \
>   	libc/machine/amdgcn/$(am__dirstamp) \
>   	libc/machine/amdgcn/$(DEPDIR)/$(am__dirstamp)
> +libc/machine/amdgcn/libc_a-simt-malloc.$(OBJEXT):  \
> +	libc/machine/amdgcn/$(am__dirstamp) \
> +	libc/machine/amdgcn/$(DEPDIR)/$(am__dirstamp)
>   libc/machine/amdgcn/libc_a-getreent.$(OBJEXT):  \
>   	libc/machine/amdgcn/$(am__dirstamp) \
>   	libc/machine/amdgcn/$(DEPDIR)/$(am__dirstamp)
> @@ -13083,6 +13088,7 @@ distclean-compile:
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/amdgcn/$(DEPDIR)/libc_a-getreent.Po@am__quote@
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/amdgcn/$(DEPDIR)/libc_a-mlock.Po@am__quote@
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/amdgcn/$(DEPDIR)/libc_a-signal.Po@am__quote@
> +@AMDEP_TRUE@@am__include@ @am__quote@libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Po@am__quote@
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arc/$(DEPDIR)/libc_a-memcmp-bs-norm.Po@am__quote@
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arc/$(DEPDIR)/libc_a-memcmp-stub.Po@am__quote@
>   @AMDEP_TRUE@@am__include@ @am__quote@libc/machine/arc/$(DEPDIR)/libc_a-memcmp.Po@am__quote@
> @@ -34086,6 +34092,20 @@ libc/machine/amdgcn/libc_a-mlock.obj: libc/machine/amdgcn/mlock.c
>   @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
>   @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/amdgcn/libc_a-mlock.obj `if test -f 'libc/machine/amdgcn/mlock.c'; then $(CYGPATH_W) 'libc/machine/amdgcn/mlock.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/amdgcn/mlock.c'; fi`
>   
> +libc/machine/amdgcn/libc_a-simt-malloc.o: libc/machine/amdgcn/simt-malloc.c
> +@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/amdgcn/libc_a-simt-malloc.o -MD -MP -MF libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Tpo -c -o libc/machine/amdgcn/libc_a-simt-malloc.o `test -f 'libc/machine/amdgcn/simt-malloc.c' || echo '$(srcdir)/'`libc/machine/amdgcn/simt-malloc.c
> +@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Tpo libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Po
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='libc/machine/amdgcn/simt-malloc.c' object='libc/machine/amdgcn/libc_a-simt-malloc.o' libtool=no @AMDEPBACKSLASH@
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
> +@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/amdgcn/libc_a-simt-malloc.o `test -f 'libc/machine/amdgcn/simt-malloc.c' || echo '$(srcdir)/'`libc/machine/amdgcn/simt-malloc.c
> +
> +libc/machine/amdgcn/libc_a-simt-malloc.obj: libc/machine/amdgcn/simt-malloc.c
> +@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/amdgcn/libc_a-simt-malloc.obj -MD -MP -MF libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Tpo -c -o libc/machine/amdgcn/libc_a-simt-malloc.obj `if test -f 'libc/machine/amdgcn/simt-malloc.c'; then $(CYGPATH_W) 'libc/machine/amdgcn/simt-malloc.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/amdgcn/simt-malloc.c'; fi`
> +@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Tpo libc/machine/amdgcn/$(DEPDIR)/libc_a-simt-malloc.Po
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='libc/machine/amdgcn/simt-malloc.c' object='libc/machine/amdgcn/libc_a-simt-malloc.obj' libtool=no @AMDEPBACKSLASH@
> +@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
> +@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -c -o libc/machine/amdgcn/libc_a-simt-malloc.obj `if test -f 'libc/machine/amdgcn/simt-malloc.c'; then $(CYGPATH_W) 'libc/machine/amdgcn/simt-malloc.c'; else $(CYGPATH_W) '$(srcdir)/libc/machine/amdgcn/simt-malloc.c'; fi`
> +
>   libc/machine/amdgcn/libc_a-getreent.o: libc/machine/amdgcn/getreent.c
>   @am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libc_a_CPPFLAGS) $(CPPFLAGS) $(libc_a_CFLAGS) $(CFLAGS) -MT libc/machine/amdgcn/libc_a-getreent.o -MD -MP -MF libc/machine/amdgcn/$(DEPDIR)/libc_a-getreent.Tpo -c -o libc/machine/amdgcn/libc_a-getreent.o `test -f 'libc/machine/amdgcn/getreent.c' || echo '$(srcdir)/'`libc/machine/amdgcn/getreent.c
>   @am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) libc/machine/amdgcn/$(DEPDIR)/libc_a-getreent.Tpo libc/machine/amdgcn/$(DEPDIR)/libc_a-getreent.Po
> diff --git a/newlib/configure b/newlib/configure
> index d8fe00411..711fae515 100755
> --- a/newlib/configure
> +++ b/newlib/configure
> @@ -6264,6 +6264,10 @@ else
>   fi
>   
>   
> +if test "$newlib_cv_amdgcn_simt" = "yes"; then
> +  NEWLIB_CFLAGS="${NEWLIB_CFLAGS} -DMALLOC_PROVIDED"
> +fi
> +
>   if test "${libm_machine_dir}" = "nds32"; then
>       { $as_echo "$as_me:${as_lineno-$LINENO}: checking for nds32 FPU SP extension" >&5
>   $as_echo_n "checking for nds32 FPU SP extension... " >&6; }
> diff --git a/newlib/libc/machine/amdgcn/Makefile.inc b/newlib/libc/machine/amdgcn/Makefile.inc
> index e1fc5ae4d..5ce179da6 100644
> --- a/newlib/libc/machine/amdgcn/Makefile.inc
> +++ b/newlib/libc/machine/amdgcn/Makefile.inc
> @@ -3,5 +3,6 @@ libc_a_SOURCES += \
>   	%D%/abort.c \
>   	%D%/atexit.c \
>   	%D%/mlock.c \
> +	%D%/simt-malloc.c \
>   	%D%/getreent.c \
>   	%D%/signal.c
> diff --git a/newlib/libc/machine/amdgcn/mlock.c b/newlib/libc/machine/amdgcn/mlock.c
> index fbc4944e6..3e3d8e7bc 100644
> --- a/newlib/libc/machine/amdgcn/mlock.c
> +++ b/newlib/libc/machine/amdgcn/mlock.c
> @@ -73,6 +73,14 @@ __malloc_lock (struct _reent *reent)
>   {
>     void *id = reent;
>   
> +#ifdef __SIMT__
> +  /* The lanes of a wavefront cannot take turns at this lock, because they
> +     run in lockstep, so the allocator runs one lane at a time (see
> +     simt-malloc.c).  Catch any other caller rather than deadlock.  */
> +  if (__builtin_gcn_simt_vote_any (!__builtin_gcn_simt_first_lane_p ()))
> +    abort ();
> +#endif
> +
>     if (id == __heap_lock_id)
>       {
>         if (__heap_lock_cnt < 1)
> diff --git a/newlib/libc/machine/amdgcn/simt-malloc.c b/newlib/libc/machine/amdgcn/simt-malloc.c
> new file mode 100644
> index 000000000..d9a74dfa6
> --- /dev/null
> +++ b/newlib/libc/machine/amdgcn/simt-malloc.c
> @@ -0,0 +1,276 @@
> +/*
> + * Support file for AMDGCN in newlib.
> + * Copyright (c) 2026 BayLibre.
> + *
> + * The authors hereby grant permission to use, copy, modify, distribute,
> + * and license this software and its documentation for any purpose, provided
> + * that existing copyright notices are retained in all copies and that this
> + * notice is included verbatim in any distributions. No written agreement,
> + * license, or royalty fee is required for any of the authorized uses.
> + * Modifications to this software may be copyrighted by their authors
> + * and need not follow the licensing terms described here, provided that
> + * the new terms are clearly indicated on the first page of each file where
> + * they apply.
> + */
> +
> +/* Memory allocation in SIMT mode (-msimt).
> +
> +   In SIMT mode each lane of a wavefront is a separate thread, and any or
> +   all of the active lanes may call malloc at once, each expecting its own
> +   allocation.  The heap lock cannot serialize the lanes of one wavefront:
> +   they execute in lockstep, so the lane that wins the lock cannot go on to
> +   release it while the other lanes are still spinning, and the wavefront
> +   deadlocks.
> +
> +   Instead, the entry points below run the real allocator for one lane at
> +   a time.  The real allocator is the usual dlmalloc (stdlib/_mallocr.c),
> +   built here under private names.  The generic copy, and the generic
> +   wrappers in stdlib, are disabled by MALLOC_PROVIDED, which configure
> +   defines for SIMT mode (see libm/machine/amdgcn/acinclude.m4).  The heap
> +   lock (mlock.c) still serializes the wavefronts.  */
> +
> +#ifdef __SIMT__
> +
> +/* Build dlmalloc under private names.  */
> +
> +#undef MALLOC_PROVIDED
> +
> +#define _malloc_r		__simt_dl_malloc_r
> +#define _free_r			__simt_dl_free_r
> +#define _realloc_r		__simt_dl_realloc_r
> +#define _calloc_r		__simt_dl_calloc_r
> +#define _memalign_r		__simt_dl_memalign_r
> +#define _valloc_r		__simt_dl_valloc_r
> +#define _pvalloc_r		__simt_dl_pvalloc_r
> +#define _mallinfo_r		__simt_dl_mallinfo_r
> +#define _mallopt_r		__simt_dl_mallopt_r
> +#define _malloc_stats_r		__simt_dl_malloc_stats_r
> +#define _malloc_trim_r		__simt_dl_malloc_trim_r
> +#define _malloc_usable_size_r	__simt_dl_malloc_usable_size_r
> +
> +#define DEFINE_MALLOC
> +#define DEFINE_FREE
> +#define DEFINE_REALLOC
> +#define DEFINE_CALLOC
> +#define DEFINE_MEMALIGN
> +#define DEFINE_VALLOC
> +#define DEFINE_PVALLOC
> +#define DEFINE_MALLINFO
> +#define DEFINE_MALLOC_STATS
> +#define DEFINE_MALLOC_USABLE_SIZE
> +#define DEFINE_MALLOPT
> +
> +#include "../../stdlib/_mallocr.c"
> +
> +#undef _malloc_r
> +#undef _free_r
> +#undef _realloc_r
> +#undef _calloc_r
> +#undef _memalign_r
> +#undef _valloc_r
> +#undef _pvalloc_r
> +#undef _mallinfo_r
> +#undef _mallopt_r
> +#undef _malloc_stats_r
> +#undef _malloc_trim_r
> +#undef _malloc_usable_size_r
> +
> +/* _mallocr.c maps these onto the _r names.  */
> +#undef malloc_stats
> +#undef malloc_trim
> +#undef malloc_usable_size
> +
> +/* Run STMT for each active lane in turn, with only that lane active.  */
> +
> +#define ONE_LANE_AT_A_TIME(STMT)					\
> +  do									\
> +    {									\
> +      int lane_ = __builtin_gcn_dim_pos (2);				\
> +      for (int i_ = 0; i_ < 64; i_++)					\
> +	{								\
> +	  /* Run STMT in the right lane, but do *not* make this a	\
> +	     loop exit route or the compiler will unify the exits.  */	\
> +	  if (lane_ == i_)						\
> +	    STMT;							\
> +	  /* Stop when no higher lane is active.  */			\
> +	  if (!__builtin_gcn_simt_vote_any (lane_ > i_))		\
> +	    break;							\
> +	}								\
> +    }									\
> +  while (0)
> +
> +void *
> +_malloc_r (struct _reent *r, size_t n)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_malloc_r (r, n));
> +  return ret;
> +}
> +
> +void
> +_free_r (struct _reent *r, void *p)
> +{
> +  ONE_LANE_AT_A_TIME (__simt_dl_free_r (r, p));
> +}
> +
> +void *
> +_realloc_r (struct _reent *r, void *p, size_t n)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_realloc_r (r, p, n));
> +  return ret;
> +}
> +
> +void *
> +_calloc_r (struct _reent *r, size_t n, size_t s)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_calloc_r (r, n, s));
> +  return ret;
> +}
> +
> +void *
> +_memalign_r (struct _reent *r, size_t a, size_t n)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_memalign_r (r, a, n));
> +  return ret;
> +}
> +
> +void *
> +_valloc_r (struct _reent *r, size_t n)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_valloc_r (r, n));
> +  return ret;
> +}
> +
> +void *
> +_pvalloc_r (struct _reent *r, size_t n)
> +{
> +  void *ret = NULL;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_pvalloc_r (r, n));
> +  return ret;
> +}
> +
> +struct mallinfo
> +_mallinfo_r (struct _reent *r)
> +{
> +  struct mallinfo ret = { 0 };
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_mallinfo_r (r));
> +  return ret;
> +}
> +
> +int
> +_mallopt_r (struct _reent *r, int p, int v)
> +{
> +  int ret = 0;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_mallopt_r (r, p, v));
> +  return ret;
> +}
> +
> +void
> +_malloc_stats_r (struct _reent *r)
> +{
> +  ONE_LANE_AT_A_TIME (__simt_dl_malloc_stats_r (r));
> +}
> +
> +int
> +_malloc_trim_r (struct _reent *r, size_t pad)
> +{
> +  int ret = 0;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_malloc_trim_r (r, pad));
> +  return ret;
> +}
> +
> +size_t
> +_malloc_usable_size_r (struct _reent *r, void *p)
> +{
> +  size_t ret = 0;
> +  ONE_LANE_AT_A_TIME (ret = __simt_dl_malloc_usable_size_r (r, p));
> +  return ret;
> +}
> +
> +/* The public entry points, which MALLOC_PROVIDED removes from stdlib.  */
> +
> +#ifndef _REENT_ONLY
> +void *
> +malloc (size_t n)
> +{
> +  return _malloc_r (_REENT, n);
> +}
> +
> +void
> +free (void *p)
> +{
> +  _free_r (_REENT, p);
> +}
> +
> +void
> +cfree (void *p)
> +{
> +  _free_r (_REENT, p);
> +}
> +
> +void *
> +realloc (void *p, size_t n)
> +{
> +  return _realloc_r (_REENT, p, n);
> +}
> +
> +void *
> +calloc (size_t n, size_t s)
> +{
> +  return _calloc_r (_REENT, n, s);
> +}
> +
> +void *
> +memalign (size_t a, size_t n)
> +{
> +  return _memalign_r (_REENT, a, n);
> +}
> +
> +void *
> +valloc (size_t n)
> +{
> +  return _valloc_r (_REENT, n);
> +}
> +
> +void *
> +pvalloc (size_t n)
> +{
> +  return _pvalloc_r (_REENT, n);
> +}
> +
> +struct mallinfo
> +mallinfo (void)
> +{
> +  return _mallinfo_r (_REENT);
> +}
> +
> +int
> +mallopt (int p, int v)
> +{
> +  return _mallopt_r (_REENT, p, v);
> +}
> +
> +void
> +malloc_stats (void)
> +{
> +  _malloc_stats_r (_REENT);
> +}
> +
> +int
> +malloc_trim (size_t pad)
> +{
> +  return _malloc_trim_r (_REENT, pad);
> +}
> +
> +size_t
> +malloc_usable_size (void *p)
> +{
> +  return _malloc_usable_size_r (_REENT, p);
> +}
> +#endif /* !_REENT_ONLY */
> +
> +#endif /* __SIMT__ */
> diff --git a/newlib/libm/machine/amdgcn/acinclude.m4 b/newlib/libm/machine/amdgcn/acinclude.m4
> index d7bbdf451..a902f3c62 100644
> --- a/newlib/libm/machine/amdgcn/acinclude.m4
> +++ b/newlib/libm/machine/amdgcn/acinclude.m4
> @@ -10,3 +10,8 @@ if test "${libm_machine_dir}" = "amdgcn"; then
>   fi
>   
>   AM_CONDITIONAL(AMDGCN_SIMT, test "$newlib_cv_amdgcn_simt" = "yes")
> +
> +dnl In SIMT mode, libc/machine/amdgcn/simt-malloc.c provides malloc.
> +if test "$newlib_cv_amdgcn_simt" = "yes"; then
> +  NEWLIB_CFLAGS="${NEWLIB_CFLAGS} -DMALLOC_PROVIDED"
> +fi



More information about the Newlib mailing list