Commit efb7539cb6

Robin Voetter <robin@voetter.nl>
2024-11-02 19:01:41
spirv: dont emit forward pointer for annotation instructions
1 parent 89bd987
Changed files (2)
src
codegen
link
src/codegen/spirv.zig
@@ -4370,13 +4370,24 @@ const NavGen = struct {
         defer self.gpa.free(ids);
 
         const result_id = self.spv.allocId();
-        try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{
-            .id_result_type = result_ty_id,
-            .id_result = result_id,
-            .base = base,
-            .element = element,
-            .indexes = ids,
-        });
+        const target = self.getTarget();
+        switch (target.os.tag) {
+            .opencl => try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{
+                .id_result_type = result_ty_id,
+                .id_result = result_id,
+                .base = base,
+                .element = element,
+                .indexes = ids,
+            }),
+            .vulkan => try self.func.body.emit(self.spv.gpa, .OpPtrAccessChain, .{
+                .id_result_type = result_ty_id,
+                .id_result = result_id,
+                .base = base,
+                .element = element,
+                .indexes = ids,
+            }),
+            else => unreachable,
+        }
         return result_id;
     }
 
src/link/SpirV/deduplicate.zig
@@ -511,6 +511,14 @@ pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule, progress: std.Pr
             }
 
             if (maybe_result_id_offset == null or maybe_result_id_offset.? != i) {
+                // Only emit forward pointers before type, constant, and global instructions.
+                // Debug and Annotation instructions don't need the forward pointer, and it
+                // messes up the logical layout of the module.
+                switch (inst.opcode.class()) {
+                    .TypeDeclaration, .ConstantCreation, .Memory => {},
+                    else => continue,
+                }
+
                 const id: ResultId = @enumFromInt(operand.*);
                 const index = info.entities.getIndex(id) orelse continue;
                 const entity = info.entities.values()[index];