From 34148b141cf8338b8c8d06cb2fb2aff4eeedfb49 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Sat, 7 Nov 2026 00:00:41 +0000 Subject: [PATCH] fix(gpu): link AIR modules deployed to other versions of the same target --- crates/stela-driver-gpu/src/compiler/air_calls.c | 16 +++++++++++++++- .../src/compiler/tests/metal_compiler.c | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/stela-driver-gpu/src/compiler/air_calls.c b/crates/stela-driver-gpu/src/compiler/air_calls.c index 02c2522b0b..c401e6cb18 100644 --- a/crates/stela-driver-gpu/src/compiler/air_calls.c +++ b/crates/stela-driver-gpu/src/compiler/air_calls.c @@ -29,6 +29,7 @@ #include #include #include +#include #include /* Beyond any call chain Metal compiles, it bounds the walk of hostile input, @@ -424,6 +425,19 @@ hide_entry_points(struct stela_air_metadata *m) return true; } +/* Whether two target triples name one target, whatever their deployment + * versions: air64_v29-apple-macosx27.0.0 links with ...-macosx27.2.0. */ +static bool +same_target(const char *a, const char *b) +{ + size_t a_bytes = strlen(a), b_bytes = strlen(b); + while (a_bytes && (isdigit((unsigned char)a[a_bytes - 1]) || a[a_bytes - 1] == '.')) + a_bytes--; + while (b_bytes && (isdigit((unsigned char)b[b_bytes - 1]) || b[b_bytes - 1] == '.')) + b_bytes--; + return a_bytes == b_bytes && !memcmp(a, b, a_bytes); +} + /* Whether module is valid AIR compiled as m's module was, after * specializing its function constants with values. */ static bool @@ -437,7 +451,7 @@ prepared(struct stela_air_metadata *m, LLVMModuleRef module, LLVMDisposeMessage(verification); if (!verified || !stela_air_metadata_layout(&linked) || !stela_air_metadata_validate(&linked)) return fail(m, STELA_AIR_INPUT, "a linked module is not valid AIR"); - if (strcmp(LLVMGetTarget(module), LLVMGetTarget(m->module)) || + if (!same_target(LLVMGetTarget(module), LLVMGetTarget(m->module)) || strcmp(LLVMGetDataLayoutStr(module), LLVMGetDataLayoutStr(m->module)) || linked.air_major != m->air_major || linked.air_minor != m->air_minor || linked.language_major != m->language_major || diff --git a/crates/stela-driver-gpu/src/compiler/tests/metal_compiler.c b/crates/stela-driver-gpu/src/compiler/tests/metal_compiler.c index cf03bbabb0..d71db0b82c 100644 --- a/crates/stela-driver-gpu/src/compiler/tests/metal_compiler.c +++ b/crates/stela-driver-gpu/src/compiler/tests/metal_compiler.c @@ -2592,6 +2592,18 @@ linked_functions_resolve_tables_and_externals(void) shader = compile_linked(sized, "tables", &linked, 1, functions, 3, &error); assert(shader); stela_metal_release(shader); + /* A library deployed to another version of the same target links. */ + static const char *redeployed_linked = + "target triple = \"air64_v29-apple-macosx27.0.0\"\n" + "define i32 @external(i32 %x) {\n%y = add i32 %x, 7\nret i32 %y\n}\n" + "define i32 @negated(i32 %x) {\n%y = sub i32 0, %x\nret i32 %y\n}\n" + "define i64 @wide(i64 %x) {\nret i64 %x\n}\n" + "!air.visible = !{}\n"; + shader = compile_linked(tables, "tables", &redeployed_linked, 1, functions, 3, &error); + if (!shader) + fprintf(stderr, "redeployed: %u %s\n", error.kind, error.detail); + assert(shader); + stela_metal_release(shader); shader = compile_linked(tested, "tables", &linked, 1, functions, 3, &error); if (!shader) fprintf(stderr, "tested: %u %s\n", error.kind, error.detail);