Commit 13c0624f23

Robin Voetter <robin@voetter.nl>
2023-07-01 20:25:25
llvm: cast optional null ptr representation to generic address space
The panic handler expects that this value is represented with the generic address space, so cast the global to the generic address- space before caching and returning the value.
1 parent 0a6cd25
Changed files (1)
src
codegen
src/codegen/llvm.zig
@@ -2435,18 +2435,25 @@ pub const Object = struct {
             .ty = ty.toType(),
             .val = null_opt_usize.toValue(),
         });
+        const llvm_wanted_addrspace = toLlvmAddressSpace(.generic, target);
+        const llvm_actual_addrspace = toLlvmGlobalAddressSpace(.generic, target);
         const global = o.llvm_module.addGlobalInAddressSpace(
             llvm_init.typeOf(),
             "",
-            toLlvmGlobalAddressSpace(.generic, target),
+            llvm_actual_addrspace,
         );
         global.setLinkage(.Internal);
         global.setUnnamedAddr(.True);
         global.setAlignment(ty.toType().abiAlignment(mod));
         global.setInitializer(llvm_init);
 
-        o.null_opt_addr = global;
-        return global;
+        const addrspace_casted_global = if (llvm_wanted_addrspace != llvm_actual_addrspace)
+            global.constAddrSpaceCast(o.context.pointerType(llvm_wanted_addrspace))
+        else
+            global;
+
+        o.null_opt_addr = addrspace_casted_global;
+        return addrspace_casted_global;
     }
 
     /// If the llvm function does not exist, create it.