From cad234174ae7ae990ebc526b6fcd92d18516e3c1 Mon Sep 17 00:00:00 2001 From: Matthew Knight Date: Wed, 22 Jul 2026 20:23:36 +0200 Subject: [PATCH] llvm: fix address space of global aliases #32108 contains my process for root causing this issue. In short, while the LLVM language does not associate aliases with address spaces, the address space does get serialized alongside the alias, and that value was hardcoded to `.default`, and the error observed was from a failed sanity check. I added an assertion on the serialization side and observed it get hit, showing the difference in address space. I followed that up with a patch to grab the address space from the aliasee instead of hardcoding it. The AVR repro now compiles: ```zig export fn anything() void {} ``` Fixes: https://codeberg.org/ziglang/zig/issues/32108 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36146 --- lib/std/zig/llvm/Builder.zig | 5 ++++- src/codegen/llvm.zig | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/std/zig/llvm/Builder.zig b/lib/std/zig/llvm/Builder.zig index 38c6a53cdd13c50fe4e6a0a172649647a94bfb3b..d0e86c80ac3baa8c9504095b8200b546789a39b2 100644 --- a/lib/std/zig/llvm/Builder.zig +++ b/lib/std/zig/llvm/Builder.zig @@ -13955,8 +13955,11 @@ pub fn toBitcode(self: *Builder, allocator: Allocator, producer: Producer) bitco } const strtab = alias.global.strtab(self); - const global = alias.global.ptrConst(self); + + // LLVM requires the types to match + assert(global.addr_space == alias.aliasee.typeOf(self).pointerAddrSpace(self)); + try module_block.writeAbbrev(ModuleBlock.Alias{ .strtab_offset = strtab.offset, .strtab_size = strtab.size, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 2405f51b3e90f9dab947bcef12f778445f2a3f64..12422f9f6f8ba5609730e1f38e37960951409174 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1661,7 +1661,7 @@ pub const Object = struct { const alias = try o.builder.addAlias( exp_name, llvm_global_ty, - .default, + global_index.ptrConst(&o.builder).addr_space, global_index.toConst(), ); break :global alias.ptrConst(&o.builder).global; @@ -1673,6 +1673,10 @@ pub const Object = struct { // We can just repurpose the existing alias. alias.setAliasee(global_index.toConst(), &o.builder); alias.ptrConst(&o.builder).global.ptr(&o.builder).type = global_index.typeOf(&o.builder); + // If the type the alias is pointing to can change, then + // it makes sense that we should update the address + // space too. + alias.ptrConst(&o.builder).global.ptr(&o.builder).addr_space = global_index.ptrConst(&o.builder).addr_space; break :global existing_global; }, .variable, .function => { @@ -1687,7 +1691,7 @@ pub const Object = struct { const alias = try o.builder.addAlias( exp_name, llvm_global_ty, - .default, + global_index.ptrConst(&o.builder).addr_space, global_index.toConst(), ); break :global alias.ptrConst(&o.builder).global; -- 2.54.0