| ... | @@ -52,9 +52,12 @@ const Block = struct { | ... | @@ -52,9 +52,12 @@ const Block = struct { |
| 52 | | 52 | |
| 53 | const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, *Block); | 53 | const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, *Block); |
| 54 | | 54 | |
| 55 | /// Maps Zig decl indices to linking SPIR-V linking information. | 55 | /// Maps Zig decl indices to SPIR-V linking information. |
| 56 | pub const DeclLinkMap = std.AutoHashMap(Module.Decl.Index, SpvModule.Decl.Index); | 56 | pub const DeclLinkMap = std.AutoHashMap(Module.Decl.Index, SpvModule.Decl.Index); |
| 57 | | 57 | |
| | 58 | /// Maps anon decl indices to SPIR-V linking information. |
| | 59 | pub const AnonDeclLinkMap = std.AutoHashMap(struct { InternPool.Index, StorageClass }, SpvModule.Decl.Index); |
| | 60 | |
| 58 | /// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. | 61 | /// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. |
| 59 | pub const DeclGen = struct { | 62 | pub const DeclGen = struct { |
| 60 | /// A general-purpose allocator that can be used for any allocations for this DeclGen. | 63 | /// A general-purpose allocator that can be used for any allocations for this DeclGen. |
| ... | @@ -77,9 +80,12 @@ pub const DeclGen = struct { | ... | @@ -77,9 +80,12 @@ pub const DeclGen = struct { |
| 77 | /// Note: If the declaration is not a function, this value will be undefined! | 80 | /// Note: If the declaration is not a function, this value will be undefined! |
| 78 | liveness: Liveness, | 81 | liveness: Liveness, |
| 79 | | 82 | |
| 80 | /// Maps Zig Decl indices to SPIR-V globals. | 83 | /// Maps Zig Decl indices to SPIR-V decl indices. |
| 81 | decl_link: *DeclLinkMap, | 84 | decl_link: *DeclLinkMap, |
| 82 | | 85 | |
| | 86 | /// Maps Zig anon decl indices to SPIR-V decl indices. |
| | 87 | anon_decl_link: *AnonDeclLinkMap, |
| | 88 | |
| 83 | /// An array of function argument result-ids. Each index corresponds with the | 89 | /// An array of function argument result-ids. Each index corresponds with the |
| 84 | /// function argument of the same index. | 90 | /// function argument of the same index. |
| 85 | args: std.ArrayListUnmanaged(IdRef) = .{}, | 91 | args: std.ArrayListUnmanaged(IdRef) = .{}, |
| ... | @@ -182,6 +188,7 @@ pub const DeclGen = struct { | ... | @@ -182,6 +188,7 @@ pub const DeclGen = struct { |
| 182 | module: *Module, | 188 | module: *Module, |
| 183 | spv: *SpvModule, | 189 | spv: *SpvModule, |
| 184 | decl_link: *DeclLinkMap, | 190 | decl_link: *DeclLinkMap, |
| | 191 | anon_decl_link: *AnonDeclLinkMap, |
| 185 | ) DeclGen { | 192 | ) DeclGen { |
| 186 | return .{ | 193 | return .{ |
| 187 | .gpa = allocator, | 194 | .gpa = allocator, |
| ... | @@ -191,6 +198,7 @@ pub const DeclGen = struct { | ... | @@ -191,6 +198,7 @@ pub const DeclGen = struct { |
| 191 | .air = undefined, | 198 | .air = undefined, |
| 192 | .liveness = undefined, | 199 | .liveness = undefined, |
| 193 | .decl_link = decl_link, | 200 | .decl_link = decl_link, |
| | 201 | .anon_decl_link = anon_decl_link, |
| 194 | .next_arg_index = undefined, | 202 | .next_arg_index = undefined, |
| 195 | .current_block_label_id = undefined, | 203 | .current_block_label_id = undefined, |
| 196 | .error_msg = undefined, | 204 | .error_msg = undefined, |
| ... | @@ -301,6 +309,89 @@ pub const DeclGen = struct { | ... | @@ -301,6 +309,89 @@ pub const DeclGen = struct { |
| 301 | return entry.value_ptr.*; | 309 | return entry.value_ptr.*; |
| 302 | } | 310 | } |
| 303 | | 311 | |
| | 312 | fn resolveAnonDecl(self: *DeclGen, val: InternPool.Index, storage_class: StorageClass) !IdRef { |
| | 313 | // TODO: This cannot be a function at this point, but it should probably be handled anyway. |
| | 314 | const spv_decl_index = blk: { |
| | 315 | const entry = try self.anon_decl_link.getOrPut(.{ val, storage_class }); |
| | 316 | if (entry.found_existing) { |
| | 317 | try self.func.decl_deps.put(self.spv.gpa, entry.value_ptr.*, {}); |
| | 318 | return self.spv.declPtr(entry.value_ptr.*).result_id; |
| | 319 | } |
| | 320 | |
| | 321 | const spv_decl_index = try self.spv.allocDecl(.global); |
| | 322 | try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); |
| | 323 | entry.value_ptr.* = spv_decl_index; |
| | 324 | break :blk spv_decl_index; |
| | 325 | }; |
| | 326 | |
| | 327 | const mod = self.module; |
| | 328 | const ty = mod.intern_pool.typeOf(val).toType(); |
| | 329 | const ty_ref = try self.resolveType(ty, .indirect); |
| | 330 | const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class); |
| | 331 | |
| | 332 | const var_id = self.spv.declPtr(spv_decl_index).result_id; |
| | 333 | |
| | 334 | const section = &self.spv.sections.types_globals_constants; |
| | 335 | try section.emit(self.spv.gpa, .OpVariable, .{ |
| | 336 | .id_result_type = self.typeId(ptr_ty_ref), |
| | 337 | .id_result = var_id, |
| | 338 | .storage_class = storage_class, |
| | 339 | }); |
| | 340 | |
| | 341 | // TODO: At some point we will be able to generate this all constant here, but then all of |
| | 342 | // constant() will need to be implemented such that it doesn't generate any at-runtime code. |
| | 343 | // NOTE: Because this is a global, we really only want to initialize it once. Therefore the |
| | 344 | // constant lowering of this value will need to be deferred to some other function, which |
| | 345 | // is then added to the list of initializers using endGlobal(). |
| | 346 | |
| | 347 | // Save the current state so that we can temporarily generate into a different function. |
| | 348 | // TODO: This should probably be made a little more robust. |
| | 349 | const func = self.func; |
| | 350 | defer self.func = func; |
| | 351 | const block_label_id = self.current_block_label_id; |
| | 352 | defer self.current_block_label_id = block_label_id; |
| | 353 | |
| | 354 | self.func = .{}; |
| | 355 | |
| | 356 | // TODO: Merge this with genDecl? |
| | 357 | const begin = self.spv.beginGlobal(); |
| | 358 | |
| | 359 | const void_ty_ref = try self.resolveType(Type.void, .direct); |
| | 360 | const initializer_proto_ty_ref = try self.spv.resolve(.{ .function_type = .{ |
| | 361 | .return_type = void_ty_ref, |
| | 362 | .parameters = &.{}, |
| | 363 | } }); |
| | 364 | |
| | 365 | const initializer_id = self.spv.allocId(); |
| | 366 | try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ |
| | 367 | .id_result_type = self.typeId(void_ty_ref), |
| | 368 | .id_result = initializer_id, |
| | 369 | .function_control = .{}, |
| | 370 | .function_type = self.typeId(initializer_proto_ty_ref), |
| | 371 | }); |
| | 372 | const root_block_id = self.spv.allocId(); |
| | 373 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| | 374 | .id_result = root_block_id, |
| | 375 | }); |
| | 376 | self.current_block_label_id = root_block_id; |
| | 377 | |
| | 378 | const val_id = try self.constant(ty, val.toValue(), .indirect); |
| | 379 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| | 380 | .pointer = var_id, |
| | 381 | .object = val_id, |
| | 382 | }); |
| | 383 | |
| | 384 | self.spv.endGlobal(spv_decl_index, begin, var_id, initializer_id); |
| | 385 | try self.func.body.emit(self.spv.gpa, .OpReturn, {}); |
| | 386 | try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); |
| | 387 | try self.spv.addFunction(spv_decl_index, self.func); |
| | 388 | |
| | 389 | try self.spv.debugNameFmt(var_id, "__anon_{d}", .{@intFromEnum(val)}); |
| | 390 | try self.spv.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); |
| | 391 | |
| | 392 | return var_id; |
| | 393 | } |
| | 394 | |
| 304 | /// Start a new SPIR-V block, Emits the label of the new block, and stores which | 395 | /// Start a new SPIR-V block, Emits the label of the new block, and stores which |
| 305 | /// block we are currently generating. | 396 | /// block we are currently generating. |
| 306 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to | 397 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to |
| ... | @@ -767,7 +858,7 @@ pub const DeclGen = struct { | ... | @@ -767,7 +858,7 @@ pub const DeclGen = struct { |
| 767 | switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) { | 858 | switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) { |
| 768 | .decl => |decl| return try self.constantDeclRef(ptr_ty, decl), | 859 | .decl => |decl| return try self.constantDeclRef(ptr_ty, decl), |
| 769 | .mut_decl => |decl_mut| return try self.constantDeclRef(ptr_ty, decl_mut.decl), | 860 | .mut_decl => |decl_mut| return try self.constantDeclRef(ptr_ty, decl_mut.decl), |
| 770 | .anon_decl => @panic("TODO"), | 861 | .anon_decl => |anon_decl| return try self.constantAnonDeclRef(ptr_ty, anon_decl), |
| 771 | .int => |int| { | 862 | .int => |int| { |
| 772 | const ptr_id = self.spv.allocId(); | 863 | const ptr_id = self.spv.allocId(); |
| 773 | // TODO: This can probably be an OpSpecConstantOp Bitcast, but | 864 | // TODO: This can probably be an OpSpecConstantOp Bitcast, but |
| ... | @@ -813,6 +904,69 @@ pub const DeclGen = struct { | ... | @@ -813,6 +904,69 @@ pub const DeclGen = struct { |
| 813 | } | 904 | } |
| 814 | } | 905 | } |
| 815 | | 906 | |
| | 907 | fn constantAnonDeclRef(self: *DeclGen, ty: Type, decl_val: InternPool.Index) !IdRef { |
| | 908 | // TODO: Merge this function with constantDeclRef. |
| | 909 | |
| | 910 | const mod = self.module; |
| | 911 | const ip = &mod.intern_pool; |
| | 912 | const ty_ref = try self.resolveType(ty, .direct); |
| | 913 | const decl_ty = ip.typeOf(decl_val).toType(); |
| | 914 | |
| | 915 | if (decl_val.toValue().getFunction(mod)) |func| { |
| | 916 | _ = func; |
| | 917 | unreachable; // TODO |
| | 918 | } else if (decl_val.toValue().getExternFunc(mod)) |func| { |
| | 919 | _ = func; |
| | 920 | unreachable; |
| | 921 | } |
| | 922 | |
| | 923 | // const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn; |
| | 924 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| | 925 | // Pointer to nothing - return undefoined |
| | 926 | return self.spv.constUndef(ty_ref); |
| | 927 | } |
| | 928 | |
| | 929 | if (decl_ty.zigTypeTag(mod) == .Fn) { |
| | 930 | unreachable; // TODO |
| | 931 | } |
| | 932 | |
| | 933 | const final_storage_class = spvStorageClass(ty.ptrAddressSpace(mod)); |
| | 934 | const actual_storage_class = switch (final_storage_class) { |
| | 935 | .Generic => .CrossWorkgroup, |
| | 936 | else => |other| other, |
| | 937 | }; |
| | 938 | |
| | 939 | const decl_id = try self.resolveAnonDecl(decl_val, actual_storage_class); |
| | 940 | const decl_ty_ref = try self.resolveType(decl_ty, .indirect); |
| | 941 | const decl_ptr_ty_ref = try self.spv.ptrType(decl_ty_ref, final_storage_class); |
| | 942 | |
| | 943 | const ptr_id = switch (final_storage_class) { |
| | 944 | .Generic => blk: { |
| | 945 | const result_id = self.spv.allocId(); |
| | 946 | try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{ |
| | 947 | .id_result_type = self.typeId(decl_ptr_ty_ref), |
| | 948 | .id_result = result_id, |
| | 949 | .pointer = decl_id, |
| | 950 | }); |
| | 951 | break :blk result_id; |
| | 952 | }, |
| | 953 | else => decl_id, |
| | 954 | }; |
| | 955 | |
| | 956 | if (decl_ptr_ty_ref != ty_ref) { |
| | 957 | // Differing pointer types, insert a cast. |
| | 958 | const casted_ptr_id = self.spv.allocId(); |
| | 959 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| | 960 | .id_result_type = self.typeId(ty_ref), |
| | 961 | .id_result = casted_ptr_id, |
| | 962 | .operand = ptr_id, |
| | 963 | }); |
| | 964 | return casted_ptr_id; |
| | 965 | } else { |
| | 966 | return ptr_id; |
| | 967 | } |
| | 968 | } |
| | 969 | |
| 816 | fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: Decl.Index) !IdRef { | 970 | fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: Decl.Index) !IdRef { |
| 817 | const mod = self.module; | 971 | const mod = self.module; |
| 818 | const ty_ref = try self.resolveType(ty, .direct); | 972 | const ty_ref = try self.resolveType(ty, .direct); |