aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-11-25 19:50:48 +0000
committergingerBill <bill@gingerbill.org>2020-11-25 19:50:48 +0000
commit70f5d7a1c99680f93eb81c6c67fb7d1a0b5cef5e (patch)
treef291b09380d39044785a397662a45f08d6354419 /src/llvm_backend.cpp
parent1acd5acd70d08b6e22b346b3b59a5ff55fb5553b (diff)
Enforce zeroing through memset to ensure padding is zeroed with llvm api
Diffstat (limited to 'src/llvm_backend.cpp')
-rw-r--r--src/llvm_backend.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index 2d5e14c68..2f84ce263 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -2961,11 +2961,29 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p
LLVMTypeRef llvm_type = lb_type(p->module, type);
LLVMValueRef ptr = LLVMBuildAlloca(p->builder, llvm_type, name);
- LLVMSetAlignment(ptr, 16); // TODO(bill): Make this configurable
+
+ // unsigned alignment = 16; // TODO(bill): Make this configurable
+ unsigned alignment = cast(unsigned)lb_alignof(llvm_type);
+ LLVMSetAlignment(ptr, alignment);
LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
if (zero_init) {
- LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr);
+ LLVMTypeKind kind = LLVMGetTypeKind(llvm_type);
+
+ switch (kind) {
+ case LLVMStructTypeKind:
+ case LLVMArrayTypeKind:
+ {
+ // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too
+ LLVMTypeRef type_i8 = LLVMInt8TypeInContext(p->module->ctx);
+ LLVMTypeRef type_i32 = LLVMInt32TypeInContext(p->module->ctx);
+ i32 sz = cast(i32)type_size_of(type);
+ LLVMBuildMemSet(p->builder, ptr, LLVMConstNull(type_i8), LLVMConstInt(type_i32, sz, false), alignment);
+ }
+ break;
+ default:
+ LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr);
+ }
}
lbValue val = {};