| ... | ... | @@ -366,6 +366,8 @@ const ModuleBuilder = struct { |
| 366 | 366 | /// The first ID of the new entry points. Entry points are allocated from |
| 367 | 367 | /// here according to their index in `info.entry_points`. |
| 368 | 368 | entry_point_new_id_base: u32, |
| 369 | /// OpName operands saved for invocation globals to re-emit. |
| 370 | global_names: std.array_hash_map.Auto(ResultId, []const Word) = .empty, |
| 369 | 371 | /// A set of all function types in the new program. SPIR-V mandates that these are unique, |
| 370 | 372 | /// and until a general type deduplication pass is programmed, we just handle it here via this. |
| 371 | 373 | function_types: std.array_hash_map.Custom(FunctionType, ResultId, FunctionType.Context, true) = .empty, |
| ... | ... | @@ -403,14 +405,41 @@ const ModuleBuilder = struct { |
| 403 | 405 | binary.functions_start = self.new_functions_section orelse binary.instructions.len; |
| 404 | 406 | } |
| 405 | 407 | |
| 408 | fn emitGlobalNames(self: *ModuleBuilder, info: ModuleInfo) !void { |
| 409 | for (info.functions.keys(), info.functions.values()) |func, fn_info| { |
| 410 | if (info.dead_initializers.contains(func)) continue; |
| 411 | const new_info = self.function_new_info.get(func) orelse continue; |
| 412 | for (fn_info.invocation_globals.keys(), 0..) |global, i| { |
| 413 | if (!info.live_invocation_globals.contains(global)) continue; |
| 414 | const name_words = self.global_names.get(global) orelse continue; |
| 415 | const id = new_info.invocationGlobalId(i); |
| 416 | try self.section.emitRaw(self.arena, .OpName, 1 + name_words.len); |
| 417 | self.section.writeOperand(ResultId, id); |
| 418 | self.section.writeWords(name_words); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 406 | 423 | /// Process everything from `binary` up to the first function and emit it into the builder. |
| 407 | 424 | fn processPreamble(self: *ModuleBuilder, binary: BinaryModule, info: ModuleInfo) !void { |
| 425 | var emitted_global_names = false; |
| 408 | 426 | var it = binary.iterateInstructions(); |
| 409 | 427 | while (it.next()) |inst| { |
| 428 | if (!emitted_global_names) switch (inst.opcode.class()) { |
| 429 | .annotation, .type_declaration, .constant_creation => { |
| 430 | try self.emitGlobalNames(info); |
| 431 | emitted_global_names = true; |
| 432 | }, |
| 433 | else => {}, |
| 434 | }; |
| 435 | |
| 410 | 436 | switch (inst.opcode) { |
| 411 | 437 | .OpName => { |
| 412 | 438 | const id: ResultId = @enumFromInt(inst.operands[0]); |
| 413 | | if (info.invocation_globals.contains(id)) continue; |
| 439 | if (info.invocation_globals.contains(id)) { |
| 440 | try self.global_names.put(self.arena, id, inst.operands[1..]); |
| 441 | continue; |
| 442 | } |
| 414 | 443 | if (info.dead_initializers.contains(id)) continue; |
| 415 | 444 | }, |
| 416 | 445 | .OpExtInstImport => { |
| ... | ... | @@ -446,11 +475,6 @@ const ModuleBuilder = struct { |
| 446 | 475 | continue; |
| 447 | 476 | }, |
| 448 | 477 | .OpTypeFunction => { |
| 449 | | // Re-emitted in `emitFunctionTypes()`. We can do this because |
| 450 | | // OpTypeFunction's may not currently be used anywhere that is not |
| 451 | | // directly with an OpFunction. For now we ignore Intels function |
| 452 | | // pointers extension, that is not a problem with a generalized |
| 453 | | // pass anyway. |
| 454 | 478 | continue; |
| 455 | 479 | }, |
| 456 | 480 | .OpFunction => break, |
| ... | ... | @@ -459,6 +483,10 @@ const ModuleBuilder = struct { |
| 459 | 483 | |
| 460 | 484 | try self.section.emitRawInstruction(self.arena, inst.opcode, inst.operands); |
| 461 | 485 | } |
| 486 | |
| 487 | if (!emitted_global_names) { |
| 488 | try self.emitGlobalNames(info); |
| 489 | } |
| 462 | 490 | } |
| 463 | 491 | |
| 464 | 492 | /// Derive new information required for further emitting this module, |