Issue 208606
Summary Apple clang version 21.0.0 (clang-2100.1.1.101) target arm64-apple-darwin25.5.0 optimizer causes crash.
Labels clang
Assignees
Reporter gwgill
    Updating to clang 21 on ARM mac triggers a crash in my open source application when compiled with -02. This does not occur in previous versions or using other compilers such as gcc or cl. My C code uses struct type punning extensively to allow inheritance  and polymorphism.

See attached example code distilled from the ArgyllCMS project gamut/gamut.c file.

Workaround is to lower optimization to -O or add -fno-strict-aliasing.

` 
/* Example code that triggers -O2 crash in clang V21 -O2 */
/* By Graeme W. Gill */
/* Distilled from ArgyllCMS gamut/gamut.c */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>

/* ------------------------------------ */
/* Common base class of BSP nodes */
#define BSP_STRUCT																			\
	int tag;			/* Type of node, 1 = bsp node, 2 = triangle, 3 = list */			\
	int id;			/* Node id number (for debugging) */		\

static int node_id = 0;

struct _gbsp {
	BSP_STRUCT
}; typedef struct _gbsp gbsp;

/* ------------------------------------ */

/* A BSP tree decision node */
struct _gbspn {
	BSP_STRUCT
}; typedef struct _gbspn gbspn;

/* Allocate a BSP decision node structure */
gbspn *new_gbspn(void) {
	gbspn *t;
	t = (gbspn *) calloc(1, sizeof(gbspn));
	t->id = node_id++;
	t->tag = 1;     /* bspn decision node */
	return t;
}

/* ------------------------------------ */

/* A triangle in the surface mesh */
struct _gtri {
	BSP_STRUCT
}; typedef struct _gtri gtri;

/* Allocate a triangle structure */
gtri *new_gtri(void) {
	gtri *t;
	t = (gtri *) calloc(1, sizeof(gtri));
	t->id = node_id++;
	t->tag = 2;     /* Triangle */
	return t;
}

/* ------------------------------------ */

/* A BSP tree triangle list node */
struct _gbspl {
	BSP_STRUCT
	int nt;					/* Number of triangles in the list */
	struct _gtri  *t[];		/* List of triangles - allocated with struct */
}; typedef struct _gbspl gbspl;

gbspl *new_gbspl(void) {
	gbspl *l;
	int i;
	int nt = 4;
	l = (gbspl *) calloc(1, sizeof(gbspl) + nt * sizeof(gtri *));
	l->id = node_id++;
	l->tag = 3;     /* bspl triangle list node */

	l->nt = nt;
	for (i = 0; i < nt; i++)
		l->t[i] = new_gtri();
	return l;
}

/* ------------------------------------ */

static gtri *radial_point_triang(
gbsp *np		/* BSP node pointer we're at */
) {
	gtri *rv;

	if (np->tag == 1) {		/* It's a BSP node */
		gbspn *n = (gbspn *)np;

		printf("This is a BSP node id = %d\n",n->id);
		return NULL;		/* Hmm */

	} else {			/* It's a triangle or list of triangles */
		int nt;			/* Number of triangles in list */
		gtri **tpp;		/* Pointer to list of triangles */
		int i;

		if (np->tag == 2) {			/* It's a triangle, fake a list of 1 */
			printf("This is a triangle id %d\n",np->id);
			tpp = (gtri **)&np;
			nt = 1;
		} else if (np->tag == 3) {	/* It's a triangle list */
			gbspl *n = (gbspl *)np;
			printf("This is a triangle list id %d\n",n->id);
			tpp = n->t;
			nt = n->nt;
		}

		for (i = 0; i < nt; i++, tpp++) {
			gtri *t = *tpp;

			printf(" list entry %d = id %d\n",i,t->id);
		}
	}

	return NULL;
}

/* ------------------------------------ */

int
main(int argc, char *argv[]) {
	gbspn *nd;
	gbspl *tl;
	gtri *tr;

	nd = new_gbspn();
	tl = new_gbspl();
	tr = new_gtri();

	radial_point_triang((gbsp *)nd);
	radial_point_triang((gbsp *)tl);
	radial_point_triang((gbsp *)tr);		// This crashes with -O2

	return 0;
}

`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to