[PATCH] align_value attribute in Clang

hfinkel at anl.gov hfinkel at anl.gov
Tue Jul 22 19:09:20 PDT 2014


Hi rsmith, aaron.ballman,

This patch introduces support for the align_value attribute. This attribute is supported by Intel's compiler (versions 14.0+), and several of my HPC users have requested support in Clang. It specifies an alignment assumption on the values to which a pointer points, and is used by numerical libraries to encourage efficient generation of vector code.

Of course, we already have an aligned attribute that can specify enhanced alignment for a type, so why is this additional attribute important? The problem is that if you want to specify that an input array of T is, say, 64-byte aligned, you could try this:

typedef double aligned_double __attribute__((aligned(64)));
void foo(aligned_double *P) {
  double x = P[0]; // This is fine.
  double y = P[1]; // What alignment did those doubles have again?
}

the access here to P[1] causes problems. P was specified as a pointer to type aligned_double, and any object of type aligned_double must be 64-byte aligned. But if P[0] is 64-byte aligned, then P[1] cannot be, and this access causes undefined behavior. Getting round this problem requires a lot of awkward casting and hand-unrolling of loops, all of which is bad.

With the align_value attribute, we can accomplish what we'd like in a well defined way:
typedef double *aligned_double_ptr __attribute__((align_value(64)));
void foo(aligned_double_ptr P) {
  double x = P[0]; // This is fine.
  double y = P[1]; // This is fine too.
}

This patch is not predicated on any uncommitted LLVM features -- CodeGen just adds the align attribute on function arguments, which has the desired effect as of r213670 -- (although, as follow-up work, can be enhanced to apply to more than just function parameters when combined with the in-development llvm.assume functionality).

Some documentation on the align_value attribute from Intel is on this page:
https://software.intel.com/en-us/articles/data-alignment-to-assist-vectorization

Thanks again!

P.S. I would have chosen to call this aligned_value, not align_value, for better naming consistency with the aligned attribute, but I think it would be more useful to users to adopt Intel's name.

http://reviews.llvm.org/D4635

Files:
  include/clang/Basic/Attr.td
  include/clang/Basic/AttrDocs.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/CodeGen/CGCall.cpp
  lib/Sema/SemaDeclAttr.cpp
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  test/CodeGen/align_value.cpp
  test/Sema/align_value.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4635.11798.patch
Type: text/x-patch
Size: 11268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140723/6ad81f04/attachment.bin>


More information about the cfe-commits mailing list