For writing SIMD code, I'd like to use templates to generate vector types with certain alignment. However, clang seems to ignore the alignment attribute when used with an alias template instead of a type alias.
Consider this code (godbolt):
#include <iostream>
#include <cstdint>
template <typename T=void>
using TemplatedT __attribute__((aligned(8))) = uint32_t;
using ManualT __attribute__((aligned(8))) = uint32_t;
int main() {
std::cout << "alignof Template: " << alignof(TemplatedT<>) << std::endl;
std::cout << "alignof Manual: " << alignof(ManualT) << std::endl;
}
With gcc 12.2, both aliases have an alignment of 8, as I would expect. With clang 15 and trunk, the templated alias has an alignment of 4, ignoring the attribute.
Is this a bug? I looked through the open llvm issues but couldn't find a matching one.
Is there any workaround I can use?
Edit: As @HolyBlackCat proposed in the comments, extracting the templating to a wrapping struct
seems to be a workaround:
template<typename ScalarT>
struct Vec {
using T __attribute__((aligned(8))) = ScalarT;
};
// and then use Vec<uint32_t>::T
Edit2:
I've opened an issue with the LLVM project at https://github.com/llvm/llvm-project/issues/59788.