| ... | ... | @@ -14,16 +14,19 @@ const LazySrcLoc = Module.LazySrcLoc; |
| 14 | 14 | const ir = @import("../ir.zig"); |
| 15 | 15 | const Inst = ir.Inst; |
| 16 | 16 | |
| 17 | | pub const TypeMap = std.HashMap(Type, u32, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); |
| 18 | | pub const InstMap = std.AutoHashMap(*Inst, u32); |
| 17 | pub const Word = u32; |
| 18 | pub const ResultId = u32; |
| 19 | 19 | |
| 20 | | pub fn writeOpcode(code: *std.ArrayList(u32), opcode: Opcode, arg_count: u32) !void { |
| 21 | | const word_count = arg_count + 1; |
| 20 | pub const TypeMap = std.HashMap(Type, ResultId, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); |
| 21 | pub const InstMap = std.AutoHashMap(*Inst, ResultId); |
| 22 | |
| 23 | pub fn writeOpcode(code: *std.ArrayList(Word), opcode: Opcode, arg_count: u16) !void { |
| 24 | const word_count: Word = arg_count + 1; |
| 22 | 25 | try code.append((word_count << 16) | @enumToInt(opcode)); |
| 23 | 26 | } |
| 24 | 27 | |
| 25 | | pub fn writeInstruction(code: *std.ArrayList(u32), opcode: Opcode, args: []const u32) !void { |
| 26 | | try writeOpcode(code, opcode, @intCast(u32, args.len)); |
| 28 | pub fn writeInstruction(code: *std.ArrayList(Word), opcode: Opcode, args: []const Word) !void { |
| 29 | try writeOpcode(code, opcode, @intCast(u16, args.len)); |
| 27 | 30 | try code.appendSlice(args); |
| 28 | 31 | } |
| 29 | 32 | |
| ... | ... | @@ -31,11 +34,11 @@ pub fn writeInstruction(code: *std.ArrayList(u32), opcode: Opcode, args: []const |
| 31 | 34 | /// That includes the actual instructions, the current result-id bound, and data structures for querying result-id's |
| 32 | 35 | /// of data which needs to be persistent over different calls to Decl code generation. |
| 33 | 36 | pub const SPIRVModule = struct { |
| 34 | | next_result_id: u32, |
| 37 | next_result_id: ResultId, |
| 35 | 38 | |
| 36 | 39 | binary: struct { |
| 37 | | types_globals_constants: std.ArrayList(u32), |
| 38 | | fn_decls: std.ArrayList(u32), |
| 40 | types_globals_constants: std.ArrayList(Word), |
| 41 | fn_decls: std.ArrayList(Word), |
| 39 | 42 | }, |
| 40 | 43 | |
| 41 | 44 | types: TypeMap, |
| ... | ... | @@ -44,8 +47,8 @@ pub const SPIRVModule = struct { |
| 44 | 47 | return .{ |
| 45 | 48 | .next_result_id = 1, // 0 is an invalid SPIR-V result ID. |
| 46 | 49 | .binary = .{ |
| 47 | | .types_globals_constants = std.ArrayList(u32).init(gpa), |
| 48 | | .fn_decls = std.ArrayList(u32).init(gpa), |
| 50 | .types_globals_constants = std.ArrayList(Word).init(gpa), |
| 51 | .fn_decls = std.ArrayList(Word).init(gpa), |
| 49 | 52 | }, |
| 50 | 53 | .types = TypeMap.init(gpa), |
| 51 | 54 | }; |
| ... | ... | @@ -57,12 +60,12 @@ pub const SPIRVModule = struct { |
| 57 | 60 | self.types.deinit(); |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | | pub fn allocResultId(self: *SPIRVModule) u32 { |
| 63 | pub fn allocResultId(self: *SPIRVModule) Word { |
| 61 | 64 | defer self.next_result_id += 1; |
| 62 | 65 | return self.next_result_id; |
| 63 | 66 | } |
| 64 | 67 | |
| 65 | | pub fn resultIdBound(self: *SPIRVModule) u32 { |
| 68 | pub fn resultIdBound(self: *SPIRVModule) Word { |
| 66 | 69 | return self.next_result_id; |
| 67 | 70 | } |
| 68 | 71 | }; |
| ... | ... | @@ -76,7 +79,7 @@ pub const DeclGen = struct { |
| 76 | 79 | spv: *SPIRVModule, |
| 77 | 80 | |
| 78 | 81 | /// An array of function argument result-ids. Each index corresponds with the function argument of the same index. |
| 79 | | args: std.ArrayList(u32), |
| 82 | args: std.ArrayList(ResultId), |
| 80 | 83 | |
| 81 | 84 | /// A counter to keep track of how many `arg` instructions we've seen yet. |
| 82 | 85 | next_arg_index: u32, |
| ... | ... | @@ -145,7 +148,7 @@ pub const DeclGen = struct { |
| 145 | 148 | return error.AnalysisFail; |
| 146 | 149 | } |
| 147 | 150 | |
| 148 | | fn resolve(self: *DeclGen, inst: *Inst) !u32 { |
| 151 | fn resolve(self: *DeclGen, inst: *Inst) !ResultId { |
| 149 | 152 | if (inst.value()) |val| { |
| 150 | 153 | return self.genConstant(inst.ty, val); |
| 151 | 154 | } |
| ... | ... | @@ -249,21 +252,21 @@ pub const DeclGen = struct { |
| 249 | 252 | |
| 250 | 253 | /// Generate a constant representing `val`. |
| 251 | 254 | /// TODO: Deduplication? |
| 252 | | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { |
| 255 | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!ResultId { |
| 253 | 256 | const target = self.module.getTarget(); |
| 254 | 257 | const code = &self.spv.binary.types_globals_constants; |
| 255 | 258 | const result_id = self.spv.allocResultId(); |
| 256 | 259 | const result_type_id = try self.getOrGenType(ty); |
| 257 | 260 | |
| 258 | 261 | if (val.isUndef()) { |
| 259 | | try writeInstruction(code, .OpUndef, &[_]u32{ result_type_id, result_id }); |
| 262 | try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id }); |
| 260 | 263 | return result_id; |
| 261 | 264 | } |
| 262 | 265 | |
| 263 | 266 | switch (ty.zigTypeTag()) { |
| 264 | 267 | .Bool => { |
| 265 | 268 | const opcode: Opcode = if (val.toBool()) .OpConstantTrue else .OpConstantFalse; |
| 266 | | try writeInstruction(code, opcode, &[_]u32{ result_type_id, result_id }); |
| 269 | try writeInstruction(code, opcode, &[_]Word{ result_type_id, result_id }); |
| 267 | 270 | }, |
| 268 | 271 | .Float => { |
| 269 | 272 | // At this point we are guaranteed that the target floating point type is supported, otherwise the function |
| ... | ... | @@ -272,15 +275,15 @@ pub const DeclGen = struct { |
| 272 | 275 | // f16 and f32 require one word of storage. f64 requires 2, low-order first. |
| 273 | 276 | |
| 274 | 277 | switch (ty.floatBits(target)) { |
| 275 | | 16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), |
| 276 | | 32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), |
| 278 | 16 => try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), |
| 279 | 32 => try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), |
| 277 | 280 | 64 => { |
| 278 | 281 | const float_bits = @bitCast(u64, val.toFloat(f64)); |
| 279 | | try writeInstruction(code, .OpConstant, &[_]u32{ |
| 282 | try writeInstruction(code, .OpConstant, &[_]Word{ |
| 280 | 283 | result_type_id, |
| 281 | 284 | result_id, |
| 282 | | @truncate(u32, float_bits), |
| 283 | | @truncate(u32, float_bits >> 32), |
| 285 | @truncate(Word, float_bits), |
| 286 | @truncate(Word, float_bits >> 32), |
| 284 | 287 | }); |
| 285 | 288 | }, |
| 286 | 289 | 128 => unreachable, // Filtered out in the call to getOrGenType. |
| ... | ... | @@ -294,7 +297,7 @@ pub const DeclGen = struct { |
| 294 | 297 | return result_id; |
| 295 | 298 | } |
| 296 | 299 | |
| 297 | | fn getOrGenType(self: *DeclGen, ty: Type) Error!u32 { |
| 300 | fn getOrGenType(self: *DeclGen, ty: Type) Error!ResultId { |
| 298 | 301 | // We can't use getOrPut here so we can recursively generate types. |
| 299 | 302 | if (self.spv.types.get(ty)) |already_generated| { |
| 300 | 303 | return already_generated; |
| ... | ... | @@ -305,8 +308,8 @@ pub const DeclGen = struct { |
| 305 | 308 | const result_id = self.spv.allocResultId(); |
| 306 | 309 | |
| 307 | 310 | switch (ty.zigTypeTag()) { |
| 308 | | .Void => try writeInstruction(code, .OpTypeVoid, &[_]u32{result_id}), |
| 309 | | .Bool => try writeInstruction(code, .OpTypeBool, &[_]u32{result_id}), |
| 311 | .Void => try writeInstruction(code, .OpTypeVoid, &[_]Word{result_id}), |
| 312 | .Bool => try writeInstruction(code, .OpTypeBool, &[_]Word{result_id}), |
| 310 | 313 | .Int => { |
| 311 | 314 | const int_info = ty.intInfo(target); |
| 312 | 315 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| ... | ... | @@ -315,7 +318,7 @@ pub const DeclGen = struct { |
| 315 | 318 | }; |
| 316 | 319 | |
| 317 | 320 | // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here. |
| 318 | | try writeInstruction(code, .OpTypeInt, &[_]u32{ |
| 321 | try writeInstruction(code, .OpTypeInt, &[_]Word{ |
| 319 | 322 | result_id, |
| 320 | 323 | backing_bits, |
| 321 | 324 | switch (int_info.signedness) { |
| ... | ... | @@ -340,7 +343,7 @@ pub const DeclGen = struct { |
| 340 | 343 | return self.fail(.{ .node_offset = 0 }, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); |
| 341 | 344 | } |
| 342 | 345 | |
| 343 | | try writeInstruction(code, .OpTypeFloat, &[_]u32{ result_id, bits }); |
| 346 | try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits }); |
| 344 | 347 | }, |
| 345 | 348 | .Fn => { |
| 346 | 349 | // We only support zig-calling-convention functions, no varargs. |
| ... | ... | @@ -360,7 +363,7 @@ pub const DeclGen = struct { |
| 360 | 363 | const return_type_id = try self.getOrGenType(ty.fnReturnType()); |
| 361 | 364 | |
| 362 | 365 | // result id + result type id + parameter type ids. |
| 363 | | try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u32, ty.fnParamLen())); |
| 366 | try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen())); |
| 364 | 367 | try code.appendSlice(&.{ result_id, return_type_id }); |
| 365 | 368 | |
| 366 | 369 | i = 0; |
| ... | ... | @@ -397,7 +400,6 @@ pub const DeclGen = struct { |
| 397 | 400 | return result_id; |
| 398 | 401 | } |
| 399 | 402 | |
| 400 | | <<<<<<< HEAD |
| 401 | 403 | pub fn gen(self: *DeclGen) !void { |
| 402 | 404 | const decl = self.decl; |
| 403 | 405 | const result_id = decl.fn_link.spirv.id; |
| ... | ... | @@ -405,21 +407,10 @@ pub const DeclGen = struct { |
| 405 | 407 | if (decl.val.castTag(.function)) |func_payload| { |
| 406 | 408 | std.debug.assert(decl.ty.zigTypeTag() == .Fn); |
| 407 | 409 | const prototype_id = try self.getOrGenType(decl.ty); |
| 408 | | try writeInstruction(&self.spv.fn_decls, .OpFunction, &[_]u32{ |
| 409 | | self.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. |
| 410 | | ======= |
| 411 | | pub fn gen(self: *DeclGen) Error!void { |
| 412 | | const result_id = self.decl.fn_link.spirv.id; |
| 413 | | const tv = self.decl.typed_value.most_recent.typed_value; |
| 414 | | |
| 415 | | if (tv.val.castTag(.function)) |func_payload| { |
| 416 | | std.debug.assert(tv.ty.zigTypeTag() == .Fn); |
| 417 | | const prototype_id = try self.getOrGenType(tv.ty); |
| 418 | | try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]u32{ |
| 419 | | self.spv.types.get(tv.ty.fnReturnType()).?, // This type should be generated along with the prototype. |
| 420 | | >>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) |
| 410 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ |
| 411 | self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. |
| 421 | 412 | result_id, |
| 422 | | @bitCast(u32, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it. |
| 413 | @bitCast(Word, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it. |
| 423 | 414 | prototype_id, |
| 424 | 415 | }); |
| 425 | 416 | |
| ... | ... | @@ -428,22 +419,18 @@ pub const DeclGen = struct { |
| 428 | 419 | |
| 429 | 420 | try self.args.ensureCapacity(params); |
| 430 | 421 | while (i < params) : (i += 1) { |
| 431 | | <<<<<<< HEAD |
| 432 | | const param_type_id = self.types.get(decl.ty.fnParamType(i)).?; |
| 433 | | ======= |
| 434 | | const param_type_id = self.spv.types.get(tv.ty.fnParamType(i)).?; |
| 435 | | >>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) |
| 422 | const param_type_id = self.spv.types.get(decl.ty.fnParamType(i)).?; |
| 436 | 423 | const arg_result_id = self.spv.allocResultId(); |
| 437 | | try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionParameter, &[_]u32{ param_type_id, arg_result_id }); |
| 424 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionParameter, &[_]Word{ param_type_id, arg_result_id }); |
| 438 | 425 | self.args.appendAssumeCapacity(arg_result_id); |
| 439 | 426 | } |
| 440 | 427 | |
| 441 | 428 | // TODO: This could probably be done in a better way... |
| 442 | 429 | const root_block_id = self.spv.allocResultId(); |
| 443 | | _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]u32{root_block_id}); |
| 430 | _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{root_block_id}); |
| 444 | 431 | try self.genBody(func_payload.data.body); |
| 445 | 432 | |
| 446 | | try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]u32{}); |
| 433 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]Word{}); |
| 447 | 434 | } else { |
| 448 | 435 | return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: generate decl type {}", .{decl.ty.zigTypeTag()}); |
| 449 | 436 | } |
| ... | ... | @@ -457,7 +444,7 @@ pub const DeclGen = struct { |
| 457 | 444 | } |
| 458 | 445 | } |
| 459 | 446 | |
| 460 | | fn genInst(self: *DeclGen, inst: *Inst) !?u32 { |
| 447 | fn genInst(self: *DeclGen, inst: *Inst) !?ResultId { |
| 461 | 448 | return switch (inst.tag) { |
| 462 | 449 | .add, .addwrap => try self.genBinOp(inst.castTag(.add).?), |
| 463 | 450 | .sub, .subwrap => try self.genBinOp(inst.castTag(.sub).?), |
| ... | ... | @@ -487,7 +474,7 @@ pub const DeclGen = struct { |
| 487 | 474 | }; |
| 488 | 475 | } |
| 489 | 476 | |
| 490 | | fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !u32 { |
| 477 | fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !ResultId { |
| 491 | 478 | // TODO: Will lhs and rhs have the same type? |
| 492 | 479 | const lhs_id = try self.resolve(inst.lhs); |
| 493 | 480 | const rhs_id = try self.resolve(inst.rhs); |
| ... | ... | @@ -546,7 +533,7 @@ pub const DeclGen = struct { |
| 546 | 533 | else => unreachable, |
| 547 | 534 | }; |
| 548 | 535 | |
| 549 | | try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, lhs_id, rhs_id }); |
| 536 | try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); |
| 550 | 537 | |
| 551 | 538 | // TODO: Trap on overflow? Probably going to be annoying. |
| 552 | 539 | // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. |
| ... | ... | @@ -557,7 +544,7 @@ pub const DeclGen = struct { |
| 557 | 544 | return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: strange integer operation mask", .{}); |
| 558 | 545 | } |
| 559 | 546 | |
| 560 | | fn genUnOp(self: *DeclGen, inst: *Inst.UnOp) !u32 { |
| 547 | fn genUnOp(self: *DeclGen, inst: *Inst.UnOp) !ResultId { |
| 561 | 548 | const operand_id = try self.resolve(inst.operand); |
| 562 | 549 | |
| 563 | 550 | const result_id = self.spv.allocResultId(); |
| ... | ... | @@ -571,32 +558,32 @@ pub const DeclGen = struct { |
| 571 | 558 | else => unreachable, |
| 572 | 559 | }; |
| 573 | 560 | |
| 574 | | try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, operand_id }); |
| 561 | try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, operand_id }); |
| 575 | 562 | |
| 576 | 563 | return result_id; |
| 577 | 564 | } |
| 578 | 565 | |
| 579 | | fn genArg(self: *DeclGen) u32 { |
| 566 | fn genArg(self: *DeclGen) ResultId { |
| 580 | 567 | defer self.next_arg_index += 1; |
| 581 | 568 | return self.args.items[self.next_arg_index]; |
| 582 | 569 | } |
| 583 | 570 | |
| 584 | | fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?u32 { |
| 571 | fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { |
| 585 | 572 | const operand_id = try self.resolve(inst.operand); |
| 586 | 573 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? |
| 587 | | try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]u32{operand_id}); |
| 574 | try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]Word{operand_id}); |
| 588 | 575 | return null; |
| 589 | 576 | } |
| 590 | 577 | |
| 591 | | fn genRetVoid(self: *DeclGen) !?u32 { |
| 578 | fn genRetVoid(self: *DeclGen) !?ResultId { |
| 592 | 579 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? |
| 593 | | try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]u32{}); |
| 580 | try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]Word{}); |
| 594 | 581 | return null; |
| 595 | 582 | } |
| 596 | 583 | |
| 597 | | fn genUnreach(self: *DeclGen) !?u32 { |
| 584 | fn genUnreach(self: *DeclGen) !?ResultId { |
| 598 | 585 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? |
| 599 | | try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]u32{}); |
| 586 | try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); |
| 600 | 587 | return null; |
| 601 | 588 | } |
| 602 | 589 | }; |