Commit 24d4bfb666

LemonBoy <thatlemon@gmail.com>
2020-11-24 10:14:16
stage1: Fix ICE when generating struct fields with padding
Make gen_const_ptr_struct_recursive aware of the possible presence of some trailing padding by always bitcasting the pointer to its expected type. Not an elegant solution but makes LLVM happy and is consistent with how the other callsites are handling this case. Fixes #5398
1 parent 06a75c1
Changed files (3)
src
test
stage1
behavior
src/stage1/codegen.cpp
@@ -7018,7 +7018,14 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ZigValue *struct_
         LLVMConstNull(get_llvm_type(g, u32)),
         LLVMConstInt(get_llvm_type(g, u32), field_index, false),
     };
-    return LLVMConstInBoundsGEP(base_ptr, indices, 2);
+
+    // The structure pointed by base_ptr may include trailing padding for
+    // alignment purposes and have the following LLVM type: <{ %T, [N x i8] }>.
+    // Add an extra bitcast as we're only interested in the %T part.
+    assert(handle_is_ptr(g, struct_const_val->type));
+    LLVMValueRef casted_base_ptr = LLVMConstBitCast(base_ptr,
+            LLVMPointerType(get_llvm_type(g, struct_const_val->type), 0));
+    return LLVMConstInBoundsGEP(casted_base_ptr, indices, 2);
 }
 
 static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ZigValue *err_union_const_val) {
test/stage1/behavior/bugs/5398.zig
@@ -0,0 +1,31 @@
+const std = @import("std");
+const testing = std.testing;
+
+pub const Mesh = struct {
+    id: u32,
+};
+pub const Material = struct {
+    transparent: bool = true,
+    emits_shadows: bool = true,
+    render_color: bool = true,
+};
+pub const Renderable = struct {
+    material: Material,
+    // The compiler inserts some padding here to ensure Mesh is correctly aligned.
+    mesh: Mesh,
+};
+
+var renderable: Renderable = undefined;
+
+test "assignment of field with padding" {
+    renderable = Renderable{
+        .mesh = Mesh{ .id = 0 },
+        .material = Material{
+            .transparent = false,
+            .emits_shadows = false,
+        },
+    };
+    testing.expectEqual(false, renderable.material.transparent);
+    testing.expectEqual(false, renderable.material.emits_shadows);
+    testing.expectEqual(true, renderable.material.render_color);
+}
test/stage1/behavior.zig
@@ -50,6 +50,7 @@ comptime {
     _ = @import("behavior/bugs/4769_b.zig");
     _ = @import("behavior/bugs/4769_c.zig");
     _ = @import("behavior/bugs/4954.zig");
+    _ = @import("behavior/bugs/5398.zig");
     _ = @import("behavior/bugs/5413.zig");
     _ = @import("behavior/bugs/5474.zig");
     _ = @import("behavior/bugs/5487.zig");