23

I am trying to compile a program that uses the pclmulqdq instruction present in new Intel processors. I've installed GCC 4.6 using macports but when I compile my program (which uses the intrinsic _mm_clmulepi64_si128), I get

/var/folders/ps/sfjmtgx5771_qbqnh4c9xclr0000gn/T//ccEAWWhd.s:16:no such
instruction: `pclmulqdq $0, %xmm0,%xmm1'

It seems that GCC is able to generate the correct assembly code from the instrinsic, but the assembler does not recognize the instruction.

I've installed binutils using macports, but the problem persists. How do I know which assembler gcc is using? The XCode assembler probably does not support it, but the binutils assembler should.

jww
  • 97,681
  • 90
  • 411
  • 885
Conrado
  • 716
  • 5
  • 15
  • If you have Xcode 4 then you probably need to use clang rather than old skool gcc - see this question: http://stackoverflow.com/questions/5257375/does-xcode-4-have-support-for-avx – Paul R Mar 23 '12 at 16:43
  • Why did this get a downvote? It's a perfectly valid question. – void-pointer Aug 18 '12 at 06:24
  • 1
    void-pointer - some folks on stack overflow are idiots. Don't worry about them. And they hunt in packs, so as soon as one does it, others will follow. Check this one out for a laugh (I still chuckle when I read his action and reasoning): http://stackoverflow.com/questions/19017335/cmake-force-use-of-non-mt-boost-libraries. – jww Oct 01 '13 at 04:52

5 Answers5

24

A simpler solution that fixed this problem for me was adding -Wa,-q to the compiler flags. From the man pages for as (version 1.38):

-q

     Use the clang(1) integrated assembler instead of the GNU based system assembler.

The -Wa part passes it from the compiler driver to the assembler, much like -Wl passes arguments to the linker.

jww
  • 97,681
  • 90
  • 411
  • 885
capisce
  • 591
  • 5
  • 5
  • 4
    For anyone stumbling on this again after upgrading to 10.9 and using brew install gcc49, this is the way to go. Note that the CCFLAG is exactly "-Wa,-q", where -Wa passes flags to the assembler and -q is the relevant flag to make it play nice on Mavericks. – Evan Senter Oct 24 '13 at 20:55
  • It does not work with -flto. Without -flto it works fine. – Mohammad Alaggan Jan 30 '17 at 15:11
13

The GNU assembler (GAS) is not supported in Mac OS X.

In order to use AVX, I had to:

  • Install GCC using MacPorts;
  • Replace the native OS X assembler (/usr/bin/as) by a script which calls the clang assembler.
  • Compile the program with the installed GCC (e.g. gcc-mp-4.7)

The strange thing is that while the clang assembler supports AVX, the clang compiler does not recognize the AVX instrinsics, forcing the ugly workaround above.

void-pointer
  • 14,247
  • 11
  • 43
  • 61
Conrado
  • 716
  • 5
  • 15
  • This worked for me on some programs, but on others I get: error: invalid instruction mnemonic 'vcvttss2siq type of errors – Alec Jacobson Dec 10 '12 at 10:45
  • 1
    @mangledorf I had the same problem; it went away after I upgraded clang. – Artefacto Jan 14 '13 at 10:09
  • 1
    The link to above seems broken at present. The script is available [here](https://gist.github.com/xianyi/2957847). – Anders Sjögren Jan 09 '14 at 09:53
  • @AndersSjögren Thank you, you just saved me here. I just edited the answer to include the updated link. – void-pointer Apr 14 '14 at 15:56
  • I wonder, according to the manpage for as, it seems that just adding the '-q' flag will do the same thing. So why not make a script that just has '/usr/bin/as -q $@' in its body (without the single-quotes), instead of actually overwriting /usr/bin/as and using clang directly? – Rahul Manne Jul 22 '15 at 18:39
  • 1
    @Conrado - OS X comes with GNU's AS version 1.38: `as -version` results in `Apple Inc version cctools-855, GNU assembler version 1.38`. In practice, its probably more than 1.38 due to back-patching. The problem is, GAS is 15 years or so before SSE3, SSE4, AVX and friends. – jww Jun 16 '16 at 06:18
4

The built in version of as is outdated. (In OS X 10.8.3)

/usr/libexec/as/x86_64/as -v

Apple Inc version cctools-839, GNU assembler version 1.38

There does not seem to exist a version of gas for OS X. (See: Installing GNU Assembler in OSX)

Using the clang assembler via a script hack (as pointed out by Conrado PLG) is one workaround. However, it does require administrator privileges and overwrites OS X-bundled executables, causing a risk of it being overwritten by a new (yet possibly outdated) version of as bundled with a future version of OS X.

Is there then a better workaround?

As noted on Why does cross gcc invoke native 'as'? it seems to be possible to specify which "as"-executable and flags to use (using "-specs=..."). A cleaner workaround to the problem seems to be to pass the correct "-specs" flags to invoke the clang assembler. This does not require admin privileges and does not risk being broken by an OS X update. The exact details of how to perform this remains to be found out (anyone?).

If this workaround becomes trouble-free and transparent enough, it may be warranted to use those settings as a default (or at least variant) for the macport gcc (so that it supports "-march=native" and the like). There is such as configure.args setting ("--with-as=${prefix}/bin/as", as seen in https://trac.macports.org/browser/trunk/dports/lang/gcc48/Portfile ), which could be replaced.

Community
  • 1
  • 1
1

It appears that I fixed my issue by using the gcc / asm syntax where asm{} function is passed a string consisting of assembler statements surrounded by quotes and separated by a backslash and newline or backslash and quoted string containing another assembler statement.

https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html#s3

1

Just use

as --version

AVX appeared around version 2.18.50 in gas/binutils.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • where is the binutils as of gas installed (using macports)? – Walter Jan 29 '13 at 20:23
  • @Walter - AS and LD are not installed as part of Binutils. See [Where is AS located after Binutils is installed?](http://lists.macosforge.org/pipermail/macports-users/2015-April/038397.html) on the MacPorts mailing list and [Please enable AS and LD in configure.ac for OS X](http://sourceware.org/bugzilla/show_bug.cgi?id=18997) on the Binutils bug tracker. – jww Jun 16 '16 at 06:15