llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.53k stars 11.79k forks source link

[ms] Make fewer assumptions about alignment for declspec(align) #26153

Open nico opened 8 years ago

nico commented 8 years ago
Bugzilla Link 25779
Version unspecified
OS Linux
CC @markmentovai,@rjmccall,@rnk

Extended Description

declspec(align) and attribute((aligned__)) have slightly different semantics: The former tells the compiler "try to align this type when you allocate memory for it yourself, i.e. for static and automatic variables, and if you did ensure alignment yourself you're free to exploit this". The latter tells the compiler "when you see an instance of this type anywhere, no matter if on stack or heap, you can assume that it will be aligned".

MSDN says "The compiler does not guarantee or attempt to preserve the alignment attribute of data during a copy or data transform operation. For example, memcpy can copy a struct declared with declspec(align(#)) to any location. Note that ordinary allocators—for example, malloc, C++ operator new, and the Win32 allocators—return memory that is usually not sufficiently aligned for declspec(align(#)) structures or arrays of structures. [...] This type now has a 32-byte alignment attribute. This means that all static and automatic instances start on a 32-byte boundary."

Currently, in clang-cl declspec(align) has the attribute((aligned)) semantics. We should let it have the semantics described on MSDN instead.

markmentovai commented 8 years ago

Helpful MSDN doc ref for __declspec(align(…)) while I Cc myself: https://msdn.microsoft.com/en-us/library/83ythb65.