Browse Source

Correctly align global and static variables.

This can be important if matrices or SIMD vectors are being used in
global or static variables, as otherwise it may result in crashes due to
aligned instructions accessing misaligned variables.
Barinzaya 4 months ago
parent
commit
b41a776027
2 changed files with 2 additions and 0 deletions
  1. 1 0
      src/llvm_backend.cpp
  2. 1 0
      src/llvm_backend_stmt.cpp

+ 1 - 0
src/llvm_backend.cpp

@@ -2598,6 +2598,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) {
 			LLVMSetLinkage(g.value, USE_SEPARATE_MODULES ? LLVMWeakAnyLinkage : LLVMInternalLinkage);
 		}
 		lb_set_linkage_from_entity_flags(m, g.value, e->flags);
+		LLVMSetAlignment(g.value, type_align_of(e->type));
 		
 		if (e->Variable.link_section.len > 0) {
 			LLVMSetSection(g.value, alloc_cstring(permanent_allocator(), e->Variable.link_section));

+ 1 - 0
src/llvm_backend_stmt.cpp

@@ -1984,6 +1984,7 @@ gb_internal void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) {
 		char *c_name = alloc_cstring(permanent_allocator(), mangled_name);
 
 		LLVMValueRef global = LLVMAddGlobal(p->module->mod, lb_type(p->module, e->type), c_name);
+		LLVMSetAlignment(global, type_align_of(e->type));
 		LLVMSetInitializer(global, LLVMConstNull(lb_type(p->module, e->type)));
 		if (value.value != nullptr) {
 			LLVMSetInitializer(global, value.value);