alignof(T) with T=__m512 is not equal to alignof(__m512)
Asked Answered
M

1

73

I ran into a strange situation that alignof(__m512) is not equal to std::alignment_of<__m512>::value compiled by Apple's clang. After some testing I found that when alignof(T) is evaluated inside a template with T=__m512, the result is different with direct alignof(__m512). I also ran several test compiled by g++ and non-Apple's clang on ubuntu(WSL) and got correct(I thought) behavior. Is this a bug of Apple's clang or an issue about implemented behavior?

#include <immintrin.h> //avx headers

#include <cstdio>
#include <typeinfo>
#include <type_traits>

void test_directly() {
  printf("directly: typeid %s alignof %zu\n", typeid(__m512).name(), alignof(__m512));
}

template<typename T>
void test_as_template_argument() {
  static_assert(std::is_same<T, __m512>::value, "assert");
  printf("template: typeid %s alignof %zu\n", typeid(T).name(), alignof(T));
}

int main() {
  test_directly();
  test_as_template_argument<__m512>();
  return 0;
}

output(compiled with clang++ -std=c++17 -march=native):

directly: typeid Dv16_f alignof 64
template: typeid Dv16_f alignof 32

clang's version:

Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

macOS's version: macOS Catalina 10.15.4 (19E2269) Darwin 19.4.0

Marlenmarlena answered 10/9, 2020 at 20:15 Comment(5)
"Regular clang" also works correctly.Faulty
Are you compiling it on a machine that has AVX512?Cosmopolitan
@Cosmopolitan Yes.Marlenmarlena
Related: D53207 & std::alignment_of<T>::value might be different from alignof(T) #39061, but it dates back to late 2018, yet clang v11 came out late 2020.With
@Cosmopolitan why would that be relevant? I would expect to be able to cross-compile for a machine that has AVX512 even on a machine that does not.Dish
C
1

This appears to be a bug. You can get the desired behavior by wrapping __m512 in a struct like this:

struct X { __m512 m; }
Cowbird answered 21/8, 2022 at 0:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.