OpenMP on macOS with Xcode tools

For those impatient, skip to how to enable OpenMP in packages.

OpenMP support in Xcode

Apple has explicitly disabled OpenMP support in compilers that they ship in Xcode:

   $ clang -c omp.c -fopenmp
   clang: error: unsupported option '-fopenmp'
even though clang had OpenMP support for quite a long time now (great thanks to the folks at Intel providing their library as open source!). In fact, the clang compiler in Xcode can generate all the necessary code for OpenMP. It can be tricked into performing its designed function by using -Xclang -fopenmp flags.

The unfortunate part about this is that Apple is not shipping the necesssary libomp.dylib run-time library needed for OpenMP support. Fortunately, some clever folks were able to match the versions so we can build the binaries that correspond to the clang version used. It is sometimes possible to use a more recent version of the runtime than the version of Apple clang, so picking the latest is often sufficient.

OpenMP run-time downloads

The follwing are links to libomp OpenMP run-time built from official LLVM release sources using Xcode compilers. They are signed and support macOS 10.13 (High Sierra) and higher [arm64 support on macOS 11 (Big Sur) or higher]. All tar-balls contain the system tree usr/local/lib and usr/local/include so the recommended installation is to type in Terminal:
    curl -O https://mac.r-project.org/openmp/openmp-14.0.6-darwin20-Release.tar.gz
    sudo tar fvxz openmp-14.0.6-darwin20-Release.tar.gz -C /

NOTE: Do NOT use a browser to download the tar balls, because it will quarantine the downloaded file and its contents. Modern macOS security doesn't allow the use of quarantined libraries so you'd have to remove the quarantine first with xattr -c if you do so.

The contained set of files is the same in all tar balls:

    usr/local/lib/libomp.dylib
    usr/local/include/ompt.h
    usr/local/include/omp.h
    usr/local/include/omp-tools.h
so you can simply remove those to uninstall. Note that any package you compile against libomp.dylib will need that run-time so you have to ship it with your package or have users install it. (Note: CRAN R ships with libomp.dylib from here in $R_HOME/lib so if you link against that location you don't have to ship it if users are using CRAN binaries of R.) You can verify the signature in the library via codetool (see below).

BuildDownloadSHA1 checksum
LLVM 18.1.8
 
openmp-18.1.8-darwin20-Release.tar.gz (Release)
openmp-18.1.8-darwin20-Debug.tar.gz (Debug)
b8e7b79d265310ba12672e117df48ccfd9ce0366
115629b4ca79af7155a0bbe76932e7a2c9f3c313
LLVM 17.0.6
Xcode 16+ (Apple clang 1600.x)
openmp-17.0.6-darwin20-Release.tar.gz (Release)
openmp-17.0.6-darwin20-Debug.tar.gz (Debug)
a89cab4e763025f03a5d12a93a609ff771ad209c
d74afdd50cd1fb4d17bdb45c2467ffeea530b1a2
LLVM 16.0.4
Xcode 15.x (Apple clang 1500.x)
openmp-16.0.4-darwin20-Release.tar.gz (Release)
openmp-16.0.4-darwin20-Debug.tar.gz (Debug)
591136d3c1cc26f3a21f1202a652be911bf1a2ad
1253f7157f590804031095440ffd80aac016b101
LLVM 15.0.7
Xcode 14.3.x (Apple clang 1403.x)
openmp-15.0.7-darwin20-Release.tar.gz (Release)
openmp-15.0.7-darwin20-Debug.tar.gz (Debug)
31f0be747101b2bdce3c01b4d1c9041959bb3b27
17728c6592d341400bb6076add9c2524a95305b7
LLVM 14.0.6
Xcode 14.0-14.2 (Apple clang 1400.x)
openmp-14.0.6-darwin20-Release.tar.gz (Release)
openmp-14.0.6-darwin20-Debug.tar.gz (Debug)
19912991431ecf032f037b6e8aea19dbd490f1ba
6b96a15db9329ea6f605449a630575036fa20aae
LLVM 13.0.0
Xcode 13.3-13.4.1 (Apple clang 1316.x)
openmp-13.0.0-darwin21-Release.tar.gz (Release)
openmp-13.0.0-darwin21-Debug.tar.gz (Debug)
47af4cb0d1f3554969f2ec9dee450d728ea30024
f6f8f1f49c02d5ec0729b56ddc7eaf51e7f04714
LLVM 12.0.1
Xcode 13.0-13.2.1 (Apple clang 1300.x)
openmp-12.0.1-darwin20-Release.tar.gz (Release)
openmp-12.0.1-darwin20-Debug.tar.gz (Debug)
4fab53ccc420ab882119256470af15c210d19e5e
58b4323e7933e12cba5c2996b4c9ef27567c41d9
LLVM 11.0.1 (+M1 patch)
Xcode 12.5 (Apple clang 1205.x)
openmp-11.0.1-darwin20-Release.tar.gz (Release)
openmp-11.0.1-darwin20-Debug.tar.gz (Debug)
0dcd19042f01c4f552914e2cf7a53186de397aa1
65e83ea667c72bbe44fea699776564d2f03a080f
(All binaries above include both arm64 and x86_64 and require macOS 11 or higher. Binaries below require macOS 10.13 or higher and are Intel-only)
LLVM 10.0.0
Xcode 12.0-12.4 (Apple clang 1200.x)
openmp-10.0.0-darwin17-Release.tar.gz (Release)
openmp-10.0.0-darwin17-Debug.tar.gz (Debug)
9bf16a64ab747528c5de7005a1ea1a9e318b3cf0
d4508d3f0c2952c3f984393b088e0b4beab33b58
LLVM 9.0.1
Xcode 11.4-11.7 (Apple clang 1103.x)
openmp-9.0.1-darwin17-Release.tar.gz (Release)
openmp-9.0.1-darwin17-Debug.tar.gz (Debug)
e5bd8501a3f957b4babe27b0a266d4fa15dbc23f
c4c8491631504fb060f7c25ec14324d02d617d5b
LLVM 8.0.1
Xcode 11.0-11.3.1 (Apple clang 1100.x)
openmp-8.0.1-darwin17-Release.tar.gz (Release)
openmp-8.0.1-darwin17-Debug.tar.gz (Debug)
e4612bfcb1bf520bf22844f7db764cadb7577c28
d6c83918b28405d43950d4b864ca8d1687eed4d1
LLVM 7.1.0
Xcode 10.2-10.3 (Apple clang 1001.x)
openmp-7.1.0-darwin17-Release.tar.gz (Release)
openmp-7.1.0-darwin17-Debug.tar.gz (Debug)
6891ff6f83f2ed83eeed42160de819b50cf643cd
34456adde62b9a1047f906e1d7f54990a1c15a34

How to enable OpenMP in packages

How you do the latter depends on the package, but if the package does not set these environment variables itself, you can try
    PKG_CPPFLAGS='-Xclang -fopenmp' PKG_LIBS=-lomp R CMD INSTALL myPackage
If that doesn't work, please consult the package's documentation, and liaise with its maintainer. It is also possible to add those flags globally by adding the following to ~/.R/Makevars:
    CPPFLAGS += -Xclang -fopenmp
    LDFLAGS += -lomp
but be very careful when doing this, always check your ~/.R/Makevars whenever you upgrade R, macOS or Xcode!

Side notes

It may be possible in principle to build static version of the run-time. That can be done via -DLIBOMP_ENABLE_SHARED=OFF, but has not been tested. There is a potential for chaos when more OMP run-times get loaded into one process. Another interesting test would be to try -DLIBOMP_FORTRAN_MODULES=ON and see which versions are compatible with the GNU Fortran used in R. We are intentionally removing the gomp symlink from the binary so that iomp and gomp don't conflict.

License and sources

All sources were obtained directly from the LLVM releases and copies are also available in the src directory (including the build script). See LICENSE.txt in the sources for the corresponding license and https://openmp.llvm.org/ for details on the OpenMP run-time.

Verifying code signatures

You can use codetools to verify the signature, for example you should see:
$ codesign -d -vv /usr/local/lib/libomp.dylib 
Executable=/usr/local/lib/libomp.dylib
Identifier=libomp
Format=Mach-O universal (x86_64 arm64)
CodeDirectory v=20400 size=5514 flags=0x0(none) hashes=167+2 location=embedded
Signature size=8927
Authority=Developer ID Application: Simon Urbanek (VZLD955F6P)
Authority=Developer ID Certification Authority
Authority=Apple Root CA
Timestamp=11/11/2021 at 1:29:04 PM
Info.plist=not bound
TeamIdentifier=VZLD955F6P
Sealed Resources=none
Internal requirements count=1 size=168

Acknowledgements

Thanks to John Clayden, Kevin Ushey and others who contributed to the discussion and testing.

Trademark notices

Last modified on 2024/11/10 by Simon Urbanek