Skip to content

Commit

Permalink
make >= 128-bit integer types 16-byte aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwaig committed Aug 4, 2019
1 parent 5687323 commit 5a41bc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/analyze.cpp
Expand Up @@ -5627,6 +5627,13 @@ ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
entry->llvm_type = LLVMIntType(size_in_bits);
entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
if (size_in_bits >= 128) {
// Override the alignment reported by LLVM like Clang does.
// On x86_64 there are some instructions like CMPXCHG16B which require this.
// See: https://github.com/ziglang/zig/issues/2987
assert(entry->abi_align == 8);
entry->abi_align = 16;
}
}

const char u_or_i = is_signed ? 'i' : 'u';
Expand Down Expand Up @@ -7267,7 +7274,6 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
return type->llvm_type;
}

Expand Down
14 changes: 14 additions & 0 deletions test/stage1/behavior/align.zig
Expand Up @@ -220,6 +220,20 @@ test "alignment of structs" {
}) == @alignOf(usize));
}

test "alignment of 128-bit integer type" {
expect(@alignOf(u128) == 16);
}

test "alignment of struct with 128-bit field" {
expect(@alignOf(struct { x: u128}) == 16);
}

test "comptime alignment of struct with 128-bit field" {
comptime {
expect(@alignOf(struct { x: u128}) == 16);
}
}

test "alignment of extern() void" {
var runtime_nothing = nothing;
const casted1 = @ptrCast(*const u8, runtime_nothing);
Expand Down

0 comments on commit 5a41bc1

Please sign in to comment.