alignof(T) with T=__m512 is not equal to alignof(__m512) - c++

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

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

Related

Apple's clang can't use <=> with std::tuple

The following compiles fine with GCC and clang on on godbolt, but on my MacBook, in Xcode 14 it dies:
#include <iostream>
#include <compare>
#include <tuple>
using std::cout; using std::tuple; using std::endl;
int main() {
tuple<float, float> tuplee = {1.0,2.0};
tuple<float, float> tuploo = {3.0,4.0};
cout << (tuplee < tuploo) << endl;
auto res = (tuplee <=> tuploo);
cout << (res < 0) << endl;
return 0;
}
The error is:
invalid operands to binary expression ('std::tuple<float, float>' and 'std::tuple<float, float>')
It points to the <=> on the tuples. Do you think it's a bug in Apple's clang, or am I missing something?
Command line on my MacBook:
% clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
% clang++ -std=c++2b main.cpp
main.cpp:11:21: error: invalid operands to binary expression ('tuple<float, float>' and 'tuple<float, float>')
cout << (tuplee <=> tuploo) << endl;
~~~~~~ ^ ~~~~~~
1 error generated.
I think it is a bug. The bug was fixed in llvm (relevant change). But by checking the tuple header in Macos SDK, one can find apple do not implement <=> for tuple.
The bug also affects arm64 variants of Macos. Clang version on my mac:
➜ test clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
PS. Homebrew llvm#14 compiles fine. Just do not use apple clang

GCC and Clang differ in static_assert related to __builtin_constant_p

I happen to find that GCC and Clang differ in the compilation of the following code:
struct Foo
{
int mem = 42;
};
int main()
{
constexpr Foo foo;
static_assert(__builtin_constant_p(foo));
return 0;
}
I compile with g++ -std=c++17 and clang++ -std=c++17.
In particular,
g++ g++-9 (Homebrew GCC 9.3.0_1) 9.3.0 compiles, whereas
clang++ Apple clang version 11.0.3 (clang-1103.0.32.62) fails to compile, complaining that
error: static_assert failed due to requirement '__builtin_constant_p(foo)'
static_assert(__builtin_constant_p(foo));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
I didn't find any hint that there should be any difference regarding __builtin_constant_p.
For __builtin_constant_p
GCC says
You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile time ...
Clang
says
Clang supports a number of builtin library functions with the same syntax as GCC, including things like __builtin_nan, __builtin_constant_p, __builtin_choose_expr, __builtin_types_compatible_p, __builtin_assume_aligned, __sync_fetch_and_add, etc.
Question: While I know __builtin_constant_p is a compiler extension, which one should be the correct one?
Both are poorly documented so I doubt there is a proper answer to your question.
If you need a workaround: it seems that clang gives up if the argument is not an int.
So this works:
struct Foo
{
int mem = 42;
};
int main()
{
constexpr Foo foo;
static_assert(__builtin_constant_p(foo.mem));
return 0;
}

clang rejects derived reference argument (just fine with g++)

Why does the following not compile in clang, but compiles just as expected in g++?
#include <valarray>
class Array : public std::valarray<double> {
public:
Array(unsigned i=0) : std::valarray<double>(i) {}
};
int main()
{
Array a(3);
for(int j=0; j<3; ++j) a[j]=j + 1;
std::valarray<double> b = std::exp(a);
}
This example was distilled to bare bones from a much more complicated situation, but the usual experiments show no issues:
After changing:
Array a(3)
to
std::valarray<double> a(3)
the code compiles just fine.
Using
static_cast< std::valarray >(a)
also works fine.
As requested by M.M. in the comments, the clang version is:
> clang++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Indeed as pointed out by "Baum mit Augen" if I add
-stdlib=libstdc++
to the compilation arguments it compiles just fine.

C++ accessing nested type using alias (using vs typedef)

Today we have found a confusing C++11 alias-declaration behavior.
Here is the example:
template<typename T>
struct Q
{
typedef T t;
};
template<typename T>
void foo(Q<T> q)
{
using q_t = Q<T>;
//typedef Q<T> q_t; // if we uncomment this and comment 'using' the example compiles
typename q_t::t qwe; // <<<<<<<<<< Error: no type named ‘t’ in ‘using q_t = struct Q<T>’
}
int main(int argc, char *argv[])
{
Q<int> q;
foo(q);
return 0;
}
The ISO 14882 (C++11) says that these two declarations must have the same semantics (page 145).
However if we have q_t declared with 'using' the example does not compile using GCC 4.7.2 (Debian Wheezy) and GCC 4.7.3 (Ubuntu 13.04) but the replacement of 'using' statement with 'typedef' statement makes it compiled.
Is it a bug in GCC or we just misunderstood the standards?
This seems to be a GCC 4.7 bug.
Here is my test to compile your code, and it works using gcc 4.8.1
So as the spec says this:
using q_t = Q<T>;
// is equivalent to this
typedef Q<T> q_t;
Compiles fine for me with g++ 4.8.1 using --std=c++11

std::function<> and the Intel compiler version 11.1

I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.