authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-09-25 01:15:33+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-10-12 20:36:15+02:00
loge90a42a80844f49e8755ab92d1c082e9ac906dee
treec782a403b60256c557be524a9db0f4f2d7687fbf
parentad747739594805546e0d52d112dfd4a75978c8c7
signature Commit is signed but in an unrecognized format.

stage2: improve globals with address spaces a little


1 files changed, 37 insertions(+), 35 deletions(-)

src/codegen/llvm.zig+37-35
......@@ -2399,8 +2399,7 @@ pub const DeclGen = struct {
23992399 // mismatch, because we don't have the LLVM type until the *value* is created,
24002400 // whereas the global needs to be created based on the type alone, because
24012401 // lowering the value may reference the global as a pointer.
2402 const llvm_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
2403 const llvm_global_addrspace = toLlvmGlobalAddressSpace(llvm_addrspace, target);
2402 const llvm_global_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target);
24042403 const new_global = dg.object.llvm_module.addGlobalInAddressSpace(
24052404 llvm_init.typeOf(),
24062405 "",
......@@ -2414,12 +2413,9 @@ pub const DeclGen = struct {
24142413 // replaceAllUsesWith requires the type to be unchanged. So we convert
24152414 // the new global to the old type and use that as the thing to replace
24162415 // old uses.
2417 const new_global_ptr = if (llvm_addrspace != llvm_global_addrspace)
2418 new_global.constAddrSpaceCast(llvm_init.typeOf().pointerType(llvm_addrspace))
2419 else
2420 new_global;
2421 const new_global_casted_ptr = new_global_ptr.constBitCast(global.typeOf());
2422 global.replaceAllUsesWith(new_global_casted_ptr);
2416 // TODO: How should this work then the address space of a global changed?
2417 const new_global_ptr = new_global.constBitCast(global.typeOf());
2418 global.replaceAllUsesWith(new_global_ptr);
24232419 dg.object.decl_map.putAssumeCapacity(decl_index, new_global);
24242420 new_global.takeName(global);
24252421 global.deleteGlobal();
......@@ -2617,11 +2613,12 @@ pub const DeclGen = struct {
26172613 const target = dg.module.getTarget();
26182614
26192615 const llvm_type = try dg.lowerType(decl.ty);
2620 const llvm_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
2616 const llvm_actual_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target);
2617
26212618 const llvm_global = dg.object.llvm_module.addGlobalInAddressSpace(
26222619 llvm_type,
26232620 fqn,
2624 toLlvmGlobalAddressSpace(llvm_addrspace, target),
2621 llvm_actual_addrspace,
26252622 );
26262623 gop.value_ptr.* = llvm_global;
26272624
......@@ -3241,16 +3238,18 @@ pub const DeclGen = struct {
32413238 const decl_index = tv.val.castTag(.variable).?.data.owner_decl;
32423239 const decl = dg.module.declPtr(decl_index);
32433240 dg.module.markDeclAlive(decl);
3241
3242 const llvm_wanted_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
3243 const llvm_actual_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target);
3244
32443245 const llvm_var_type = try dg.lowerType(tv.ty);
3245 const llvm_var_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
3246 const llvm_global_addrspace = toLlvmGlobalAddressSpace(llvm_var_addrspace, target);
3247 const llvm_var_ptr_type = llvm_var_type.pointerType(llvm_global_addrspace);
3246 const llvm_actual_ptr_type = llvm_var_type.pointerType(llvm_actual_addrspace);
32483247
32493248 const val = try dg.resolveGlobalDecl(decl_index);
3250 const val_ptr = val.constBitCast(llvm_var_ptr_type);
3251 if (llvm_global_addrspace != llvm_var_addrspace) {
3252 const llvm_ptr_type = llvm_var_type.pointerType(llvm_var_addrspace);
3253 return val_ptr.constAddrSpaceCast(llvm_ptr_type);
3249 const val_ptr = val.constBitCast(llvm_actual_ptr_type);
3250 if (llvm_actual_addrspace != llvm_wanted_addrspace) {
3251 const llvm_wanted_ptr_type = llvm_var_type.pointerType(llvm_wanted_addrspace);
3252 return val_ptr.constAddrSpaceCast(llvm_wanted_ptr_type);
32543253 }
32553254 return val_ptr;
32563255 },
......@@ -4055,12 +4054,12 @@ pub const DeclGen = struct {
40554054 try self.resolveGlobalDecl(decl_index);
40564055
40574056 const target = self.module.getTarget();
4058 const llvm_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
4059 const llvm_global_addrspace = toLlvmGlobalAddressSpace(llvm_addrspace, target);
4060 const llvm_val = if (llvm_addrspace != llvm_global_addrspace) blk: {
4057 const llvm_wanted_addrspace = toLlvmAddressSpace(decl.@"addrspace", target);
4058 const llvm_actual_addrspace = toLlvmGlobalAddressSpace(decl.@"addrspace", target);
4059 const llvm_val = if (llvm_wanted_addrspace != llvm_actual_addrspace) blk: {
40614060 const llvm_decl_ty = try self.lowerType(decl.ty);
4062 const llvm_decl_ptr_ty = llvm_decl_ty.pointerType(llvm_addrspace);
4063 break :blk llvm_decl_val.constAddrSpaceCast(llvm_decl_ptr_ty);
4061 const llvm_decl_wanted_ptr_ty = llvm_decl_ty.pointerType(llvm_wanted_addrspace);
4062 break :blk llvm_decl_val.constAddrSpaceCast(llvm_decl_wanted_ptr_ty);
40644063 } else llvm_decl_val;
40654064
40664065 const llvm_type = try self.lowerType(tv.ty);
......@@ -4328,9 +4327,9 @@ pub const FuncGen = struct {
43284327 // We have an LLVM value but we need to create a global constant and
43294328 // set the value as its initializer, and then return a pointer to the global.
43304329 const target = self.dg.module.getTarget();
4331 const llvm_addrspace = toLlvmAddressSpace(.generic, target);
4332 const llvm_global_addrspace = toLlvmGlobalAddressSpace(llvm_addrspace, target);
4333 const global = self.dg.object.llvm_module.addGlobalInAddressSpace(llvm_val.typeOf(), "", llvm_global_addrspace);
4330 const llvm_wanted_addrspace = toLlvmAddressSpace(.generic, target);
4331 const llvm_actual_addrspace = toLlvmGlobalAddressSpace(.generic, target);
4332 const global = self.dg.object.llvm_module.addGlobalInAddressSpace(llvm_val.typeOf(), "", llvm_actual_addrspace);
43344333 global.setInitializer(llvm_val);
43354334 global.setLinkage(.Private);
43364335 global.setGlobalConstant(.True);
......@@ -4340,10 +4339,13 @@ pub const FuncGen = struct {
43404339 // the type of global constants might not match the type it is supposed to
43414340 // be, and so we must bitcast the pointer at the usage sites.
43424341 const wanted_llvm_ty = try self.dg.lowerType(ty);
4343 const wanted_bitcasted_llvm_ptr_ty = wanted_llvm_ty.pointerType(llvm_global_addrspace);
4342 const wanted_bitcasted_llvm_ptr_ty = wanted_llvm_ty.pointerType(llvm_actual_addrspace);
43444343 const bitcasted_ptr = global.constBitCast(wanted_bitcasted_llvm_ptr_ty);
4345 const wanted_llvm_ptr_ty = wanted_llvm_ty.pointerType(llvm_addrspace);
4346 const casted_ptr = bitcasted_ptr.constAddrSpaceCast(wanted_llvm_ptr_ty);
4344 const wanted_llvm_ptr_ty = wanted_llvm_ty.pointerType(llvm_wanted_addrspace);
4345 const casted_ptr = if (llvm_wanted_addrspace != llvm_actual_addrspace)
4346 bitcasted_ptr.constAddrSpaceCast(wanted_llvm_ptr_ty)
4347 else
4348 bitcasted_ptr;
43474349 gop.value_ptr.* = casted_ptr;
43484350 return casted_ptr;
43494351 }
......@@ -9948,13 +9950,13 @@ fn llvmDefaultGlobalAddressSpace(target: std.Target) c_uint {
99489950 };
99499951}
99509952
9951/// If `llvm_addrspace` is generic, convert it to the actual address space that globals
9952/// should be stored in by default.
9953fn toLlvmGlobalAddressSpace(llvm_addrspace: c_uint, target: std.Target) c_uint {
9954 return if (llvm_addrspace == llvm.address_space.default)
9955 llvmDefaultGlobalAddressSpace(target)
9956 else
9957 llvm_addrspace;
9953/// Return the actual address space that a value should be stored in if its a global address space.
9954/// When a value is placed in the resulting address space, it needs to be cast back into wanted_address_space.
9955fn toLlvmGlobalAddressSpace(wanted_address_space: std.builtin.AddressSpace, target: std.Target) c_uint {
9956 return switch (wanted_address_space) {
9957 .generic => llvmDefaultGlobalAddressSpace(target),
9958 else => |as| toLlvmAddressSpace(as, target),
9959 };
99589960}
99599961
99609962/// Take into account 0 bit fields and padding. Returns null if an llvm