From https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
It says "-ftree-slp-vectorize: Perform basic block vectorization on trees. This flag is enabled by default at -O2 and by -ftree-vectorize, -fprofile-use, and -fauto-profile."
However it seems I have to pass a flag explicitly to turn on SIMD. Did I mis undertand something here? It is enabled at -O3 though.
https://www.godbolt.org/z/1ffzdqMoT
Is -ftree-slp-vectorize not enabled by -O2 in GCC?
Yes and no. It depends on the version of the compiler.
From https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
You have linked to the latest version of documentation. It applies to the version that is currently under development, which at the moment is version 12.
However it seems I have to pass a flag explicitly to turn on SIMD.
https://www.godbolt.org/z/1ffzdqMoT
Your example uses GCC version 11.
Did I mis undertand something here?
You read the wrong version of documentation, or used wrong version of compiler and hence your assumption didn't hold.
Related
I'm currently trying to build code using gcc / g++ like this:
g++-10 -std=c++2a -Wall -Wextra -c -o hw01.o hw01.cpp
(Also the same outcome with gcc-10, gcc, g++)
And I have a static check in my Code:
static_assert(__cplusplus >= 202002L);
which sadly fails every time. As far as I've reserched, the used C++ version depends on gcc and it's version. gcc-10 has version 10.3.0 and the normal gcc command uses version 9.4.0, so both should be able to use C++20 as specified in the build command.
Yet when looking in VsCode, the variable __cplusplus evaluates to 201402L, therefore making the assertion fail.
Even when uninstalling / reinstalling the compilers (or sudo apt remove cpp) this problem persists.
Any help? How do I get my system to use a newer C++ version?
PS: I'm working on a Ubuntu WSL (host system is Windows 10)
Edit: Since most recommondations are to neglect the static test and f.e. test for a different value of __cplusplus or simply throw out the test, i'm doing this for a university assignment. The satic test is non-negotiable, can not be changed and also not altered. I have to make the test pass by changing the build value of my local C++ version, I just don't know how.
Before a future standard version is standardized, the actual value for the future version is not known. Compilers that "support" the new version before standardization will have to pick a value larger than the old one but won't have the "standard" value.
What you can do, though, is check if __cplusplus is greater than the version of the previous standard. For instance, __cplusplus in C++17 has the value 201703, so the value GCC 10.3 returns of 201709 is greater than that, so you "know" that you have a version that is newer than C++17.
The __cplusplus is meant to provide an answer to the following question: "what version of C++ does this implementation claim to support?" This is a very broad question, particular in terms of the exact definition of "support".
GCC 10.3 does not feel that its support of C++20 is sufficient to meet some definition of "support". Therefore, it does not claim to "support" C++20, even if that's the version you asked for.
This is not something you get to change. There is no compiler option to force GCC to answer the question in a different way.
If you cannot upgrade GCC to a version that is willing to claim "support" for C++20, and you cannot remove or alter the change, then you're out of luck.
Are the following statements correct?
With GCC and clang, my code will be auto-vectorized if I compile with :
-O2 -ftree-vectorize -march=XYZ (XYZ being the target instruction set: native, SSE, AVX2, etc.)
-O3 -march=XYZ
With MSVC, my code will be auto-vectorized if I compile with:
/O2
This video seems to suggest that I do not need to specify the architecture with MSVC. Is that correct? The compiler will use the native architecture by default, and fall back on scalar operations at runtime if vector instructions can't be found.
I do not need to specify the architecture with MSVC. Is that correct?
Yes that is indeed correct. With MSVC, By default, the Auto-Vectorizer is enabled and picks up appropriate instructurion set for fastest vectorization. Moreover, even if you do specify arch, The Auto-Vectorizer may generate different instructions than specified by the /arch switch - as stated by documentation. For example, when you compile /arch:SSE2, SSE4.2 instructions may be emitted.
On another note, The VS vectorizer lacks quite a bit of features when compared to gcc or clang.
With GCC and clang, my code will be auto-vectorized if I compile with -O2 -ftree-vectorize -march=XYZ ? -O3 -march=XYZ ?
Not necessarily, To enable vectorization of floating point reductions you need to use -ffast-math or -fassociative-math as well. However, in general, Yes it'll be enabled. You may find same written in documentation, Vectorization is enabled by the flag -ftree-vectorize and by default at -O3
PS: You may use https://godbolt.org to see all this in action!
I was searching online to see if clang supported reproducible builds. I read that GCC guaranteed reproducible builds using the -frandom-seed flag. I wanted to know if clang supports that flag and I could not find anything regarding that.I then came here which had a statement such as:
...two consecutive builds of (GCC-built) Clang
My question is what is GCC built clang ? I am currently aware of only 2 compilers (Microsoft , GCC (Coudl be Cygwin/Mingw flavor) ) and the third one was suppose to be clang. My question is what does clang (GCC built) mean ? Built from source ? I would like to think that clang is a totally different compiler from GCC and Windows. Also this documentation here states
Clang has experimental support for targeting “Cygming” (Cygwin /
MinGW) platforms.
What does that mean ? Does clang mean that it uses Mingw GCC as a compiler ? What does targeting mean here ?
To my mind, this phrase means clang was built from source using GCC as a compiler. Then, clang is a compiler, so it can't use GCC as a compiler.
Compilers are written in programming languages to be able to compile code written in a programming language. This means, a compiler can compile a compiler or even itself.
If you don't know is feature X supported in product Y, please, read the docs on product Y. If this feature isn't mentioned, it's not supported and vice versa.
This question already has answers here:
How to determine the version of the C++ standard used by the compiler?
(9 answers)
Closed 6 years ago.
Recently I had faced compiling errors in a c++ code I wrote so I've been asked if I was using a C++11 compiler, but honestly I don't know how to check on my compiler version ! so any idea how to figure this out ??
Btw I'm using codeblocks as an IDE which includes the GCC compiler and GDB debugger from MinGW. also if I'm compiling my c++ code under Linux what command should I run to know my compiler version ?
That can be a tricky question. C++11 refers to a version of the
standard, not to a version of the compiler. Different compilers, and
different versions of any given compiler, will typically implement a mix
of versions of the standard, at least for recent versions. More or
less, because any implementation of C++11 will be fairly new, and thus
probably fairly buggy.
Most compilers have options to output the version; many will output it
systematically in verbose mode. For g++, try g++ --version. Recent
versions of g++ do have some support for C++11, but you have to activate
it with -std=c++0x (rather than the usual -std=c++03 or
-std=c++98). As the name (c++0x, rather than c++11) indicates, it
is not truly C++11; it is an implementation of some (most?) of the
major new features, in a preliminary version based on various working
papers, and not the final standard.
(FWIW: I don't think any compiler fully implements all of C++11, but I'd
love to be proven wrong.)
You can find out your compiler version like this:
g++ --version
That doesn't tell you if you are using c++11. To use c++11 features, you would have to call the compiler with thr -std=c++0x flag:
g++ -std=c++0x ....
Bear in mind that gcc doesn't implement 100% of c++11 yet, and how much it implements depends on the version. See here for a table of supported features.
EDIT: strictly speaking, if you are using GCC you cannot be using a fully compliant c++11 compiler due to the missing features. But versions 4.6.1 onwards cover a great deal of the standard.
If you're in linux, checking the version is easy.
> gcc --version
Will tell you the version you have. Note that GCC C++11 support is incomplete still, you can find the details here: http://gcc.gnu.org/projects/cxx0x.html
I've used a few C++11 features myself, namely initializer lists, and the nullptr constant. I'm using GCC 4.6 and it's working fine.
edit: And yes, as #jaunchopanza said, you'll need the -std=c++0x compiler flag to make it work. If you're using Code::Blocks, just right-click on your project, choose Build options..., and check the item that says Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]
Well... When i was searching for a good compiler I came across clang/LLVM. This compiler gives me same result as other compilers like icc, pgi. But the problem is there are very few tutorials on this compiler... Kindly let me know where can I find the tutorials on the clang compiler.
Note by:
I have compiled my c code using the following flags clang -O3 -mfpmath=sse file.c
Clang (the command line compiler) takes gcc-compatible options, but accepts and ignores a lot of flags that GCC takes (like -mfpmath=sse). We aim to generate good code out of the box. There are some flags that allow clang to violate the language standards that can be useful in some scenarios, like -ffast-math though.
If you're looking for good performance, I highly recommend experimenting with link-time-optimization, which allows clang to optimize across source files in your application. Depending on what platform you're on, this is enabled by passing -O4 to the compiler. If you're on linux, you need to use the "gold" linker (see http://llvm.org/docs/GoldPlugin.html). If you're on the mac, it should "just work" with any recent version of Xcode.
The clang is not a compiler, it is just frontend of LLVM compiler. So, when you calls clang, it parses c/c++ file but the optimization and code generation is handled in LLVM itself.
Here you can found a documentation of LLVM optimization and analysis options: http://llvm.org/docs/Passes.html
The full documentation is here http://llvm.org/docs/
Also useful options are listed here http://linux.die.net/man/1/llvmc (I suggest clang will accept most of them too)