Skip to content

Commit

Permalink
Clad support Clang 5.0-8.0.1 (vgvassilev#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-penev authored and vgvassilev committed Nov 21, 2019
1 parent 09832b6 commit 056b7d9
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 117 deletions.
209 changes: 163 additions & 46 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ addons-5: &addons-5
'llvm-5.0-tools'
]
homebrew: &brew-5
# <<: *common-brew
# packages: &brew-packages-5 [*common-brew-packages,
<<: *common-brew
packages: &brew-packages-5 [*common-brew-packages
# 'llvm@5'
# ]
packages: &brew-packages-5 []
]
update: true

addons-6: &addons-6
<<: *common-addons
Expand Down Expand Up @@ -121,6 +121,23 @@ addons-7: &addons-7
]
update: true

addons-8: &addons-8
<<: *common-addons
apt: &apt-8
<<: *common-apt
packages: &apt-packages-8 [*common-apt-packages,
'llvm-8-dev',
'libclang-8-dev',
'clang-8',
'llvm-8-tools'
]
homebrew: &brew-8
<<: *common-brew
packages: &brew-packages-8 [*common-brew-packages,
'llvm@8'
]
update: true


before_cache:
- brew cleanup
Expand Down Expand Up @@ -168,7 +185,8 @@ matrix:
<<: *addons-5
homebrew:
<<: *brew-5
packages: [*common-brew]
packages: [*common-brew, llvm@6]
update: true
- os: osx
env: CXX=clang++ CC=clang BUILD_TYPE=Debug CLANG_RUNTIME=5.0
osx_image: xcode9.4
Expand All @@ -177,7 +195,8 @@ matrix:
<<: *addons-5
homebrew:
<<: *brew-5
packages: [*common-brew]
packages: [*common-brew, llvm@6]
update: true

# Ubuntu, GCC 5
- env: CXX=g++-5 CC=gcc BUILD_TYPE=Debug CLANG_RUNTIME=5.0
Expand Down Expand Up @@ -468,10 +487,114 @@ matrix:
<<: *apt-7
packages: [*apt-packages-7, 'clang-5.0', 'g++-multilib', 'libc6-dbg']

##################### Clad for clang 8 #####################
# OSX 10.13, Clang and XCode 9.4
- os: osx
env: CXX=clang++ CC=clang BUILD_TYPE=Release CLANG_RUNTIME=8
osx_image: xcode9.4
compiler: clang
addons:
<<: *addons-8
- os: osx
env: CXX=clang++ CC=clang BUILD_TYPE=Debug CLANG_RUNTIME=8
osx_image: xcode9.4
compiler: clang
addons:
<<: *addons-8

# Ubuntu, GCC 5
- env: CXX=g++-5 CC=gcc BUILD_TYPE=Debug CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-5', 'g++-5-multilib', 'libc6-dbg']
- env: CXX=g++-5 CC=gcc BUILD_TYPE=Release CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-5', 'g++-5-multilib', 'libc6-dbg']

# Ubuntu, GCC 6
- env: CXX=g++-6 CC=gcc BUILD_TYPE=Debug CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-6', 'g++-6-multilib', 'libc6-dbg']
- env: CXX=g++-6 CC=gcc BUILD_TYPE=Release CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-6', 'g++-6-multilib', 'libc6-dbg']

# GCC 7
- env: CXX=g++-7 CC=gcc BUILD_TYPE=Debug CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-7', 'g++-7-multilib', 'libc6-dbg']
- env: CXX=g++-7 CC=gcc BUILD_TYPE=Release CLANG_RUNTIME=8
compiler: gcc
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'g++-7', 'g++-7-multilib', 'libc6-dbg']

# Clang 4
- env: CXX=clang++-4.0 CC=clang-4.0 BUILD_TYPE=Debug CLANG_RUNTIME=8
compiler: clang
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'clang-4.0', 'g++-multilib', 'libc6-dbg']
- env: CXX=clang++-4.0 CC=clang-4.0 BUILD_TYPE=Release CLANG_RUNTIME=8
compiler: clang
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'clang-4.0', 'g++-multilib', 'libc6-dbg']

# Clang 5
- env: CXX=clang++-5.0 CC=clang-5.0 BUILD_TYPE=Debug CLANG_RUNTIME=8
compiler: clang
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'clang-5.0', 'g++-multilib', 'libc6-dbg']
- env: CXX=clang++-5.0 CC=clang-5.0 BUILD_TYPE=Release CLANG_RUNTIME=8
compiler: clang
dist: bionic
addons:
<<: *addons-8
apt:
<<: *apt-8
packages: [*apt-packages-8, 'clang-5.0', 'g++-multilib', 'libc6-dbg']


before_install:
- export
#- ls -la -R /usr/local
- |
if [ "${TRAVIS_BRANCH}" == 'coverity_scan' ]; then
if [ "$RUN_COVERITY" != 1 ]; then
Expand All @@ -496,7 +619,10 @@ install:
sudo curl http://releases.llvm.org/5.0.2/clang+llvm-5.0.2-x86_64-apple-darwin.tar.xz | sudo xz -d -c | sudo tar -x
sudo rm -fr /usr/local/clang*
sudo mv clang+llvm-5.0.2-x86_64-apple-darwin/ llvm@5/
sudo ls -la
if [ ! -f $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck ]; then
# Use llvm@6 Filecheck
sudo ln -s /usr/local/opt/llvm\@6/bin/FileCheck ${TRAVIS_PATH_TO_LLVM_BUILD}bin/FileCheck
fi
popd
fi
# Package llvm@6
Expand All @@ -507,77 +633,63 @@ install:
if [ $CLANG_RUNTIME == '7' ]; then
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/opt/llvm@7/
fi
# Package llvm@8
if [ $CLANG_RUNTIME == '8' ]; then
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/opt/llvm@8/
fi
fi
if [ $TRAVIS_OS_NAME == 'linux' ]; then
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/llvm-${CLANG_RUNTIME}/
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/lib/llvm-${CLANG_RUNTIME}/
# Ubuntu 16.04 & 18.04 llvm/clang-5.0 package is broken - install it from binary build.
if [ $CLANG_RUNTIME == '5.0' ]; then
# Install clang-5.0 to run the plugin with.
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/opt/llvm-${CLANG_RUNTIME}/
sudo mkdir -p /usr/local/opt
pushd /usr/local/opt
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/lib/opt/llvm-${CLANG_RUNTIME}/
sudo mkdir -p /usr/lib/opt
pushd /usr/lib/opt
sudo curl http://releases.llvm.org/5.0.2/clang+llvm-5.0.2-x86_64-linux-gnu-ubuntu-16.04.tar.xz | sudo xz -d -c | sudo tar -x
sudo rm -fr /usr/local/clang*
sudo rm -fr /usr/lib/clang*
sudo mv clang+llvm-5.0.2-x86_64-linux-gnu-ubuntu-16.04/ llvm-5.0/
sudo ls -la
if [ ! -f $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck ]; then
sudo ln -s /usr/lib/llvm-$CLANG_RUNTIME/bin/FileCheck $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck
fi
popd
fi
# Ubuntu 16.04 & 18.04 llvm/clang-6.0 package is broken - install it from binary build.
if [ $CLANG_RUNTIME == '6.0' ]; then
# Install clang-6.0 to run the plugin with.
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/opt/llvm-${CLANG_RUNTIME}/
sudo mkdir -p /usr/local/opt
pushd /usr/local/opt
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/lib/opt/llvm-${CLANG_RUNTIME}/
sudo mkdir -p /usr/lib/opt
pushd /usr/lib/opt
sudo curl http://releases.llvm.org/6.0.1/clang+llvm-6.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz | sudo xz -d -c | sudo tar -x
sudo rm -fr /usr/local/clang*
sudo rm -fr /usr/lib/clang*
sudo mv clang+llvm-6.0.1-x86_64-linux-gnu-ubuntu-16.04/ llvm-6.0/
sudo ls -la
if [ ! -f $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck ]; then
sudo ln -s /usr/lib/llvm-$CLANG_RUNTIME/bin/FileCheck $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck
fi
popd
fi
# Ubuntu 18.04 llvm/clang-7 package.
if [ $CLANG_RUNTIME == '7' ]; then
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/local/clang-7.0.0/
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/lib/llvm-7/
fi
# Ubuntu 18.04 llvm/clang-8 package.
if [ $CLANG_RUNTIME == '8' ]; then
export TRAVIS_PATH_TO_LLVM_BUILD=/usr/lib/llvm-8/
fi
fi
# Add -H to silence 'The directory '/Users/travis/.../pip/http' or its parent
# directory is not owned by the current user and the cache has been disabled.
sudo -H pip install lit # LLVM lit is not part of the llvm releases...
before_script:
- cmake --version
- $CC --version
- $CXX --version
- |
if [ $TRAVIS_OS_NAME == 'osx' ]; then
# LLVM utils such as FileCheck and not are not part of the llvm release on unix and some version for osx.
cd tools && for tool in FileCheck not; do
svn export \
http://llvm.org/svn/llvm-project/llvm/branches/release_50/utils/${tool} ${tool} ;
echo "add_subdirectory(\"${tool}\")" >> CMakeLists.txt
done
cd ../
fi
- |
if [ $TRAVIS_OS_NAME == 'linux' ]; then
# Linux specific commands, fixes and settings
:
fi
- mkdir obj && cd obj
- echo "Use Clang/LLVM in $TRAVIS_PATH_TO_LLVM_BUILD"
- cmake -DClang_DIR="$TRAVIS_PATH_TO_LLVM_BUILD" -DLLVM_DIR="$TRAVIS_PATH_TO_LLVM_BUILD" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DLLVM_EXTERNAL_LIT="`which lit`" $TRAVIS_BUILD_DIR

script:
- |
# lit's substitution mechanism will use llvm_obj_root to resolve FileCheck.
# This expands FileCheck to the location of installed llvm.
if [ $TRAVIS_OS_NAME == 'osx' ]; then
cmake --build . --target FileCheck -- -j4
sudo ln -s `pwd`/bin/FileCheck $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck
fi
if [ $TRAVIS_OS_NAME == 'linux' ]; then
sudo ln -s /usr/lib/llvm-${CLANG_RUNTIME}/bin/FileCheck $TRAVIS_PATH_TO_LLVM_BUILD/bin/FileCheck
fi
- cmake --build . --target check-clad -- -j4

branches:
Expand Down Expand Up @@ -617,6 +729,11 @@ after_failure:
- dpkg-query -L llvm-7-dev
- find /usr/lib/llvm-7/
- find /usr/local/opt/llvm@7/
- dpkg-query -L libclang-8-dev
- dpkg-query -L clang-8
- dpkg-query -L llvm-8-dev
- find /usr/lib/llvm-8/
- find /usr/local/opt/llvm@8/
- brew search llvm
- pip show lit
- cat /home/travis/build/vgvassilev/clad/obj/CMakeFiles/*.log
22 changes: 20 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
project(clad)
cmake_minimum_required(VERSION 3.7.0)

## Define clad supported version of clang and llvm

set(CLANG_MIN_SUPPORTED 5.0)
set(CLANG_MAX_SUPPORTED 7.1)
set(CLANG_MAX_SUPPORTED 8.0.1)
set(LLVM_MIN_SUPPORTED 5.0)
set(LLVM_MAX_SUPPORTED 7.1)
set(LLVM_MAX_SUPPORTED 8.0.1)

if (NOT DEFINED Clang_DIR)
set(Clang_DIR ${LLVM_DIR})
Expand Down Expand Up @@ -106,6 +108,22 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
# For consistency we should set it to the correct value.
set(LLVM_CONFIG_HAS_RTTI NO CACHE BOOL "" FORCE)


## Compatibility

# Clang 8 remove add_llvm_loadable_module for cmake files.
# Recomended is use of add_llvm_library with MODULE argument.
if (CLANG_PACKAGE_VERSION VERSION_LESS 8.0)
macro(clad_compat_add_llvm_loadable_module module_name)
add_llvm_loadable_module(${module_name} ${ARGN})
endmacro(clad_compat_add_llvm_loadable_module)
else()
macro(clad_compat_add_llvm_loadable_module module_name)
add_llvm_library(${module_name} MODULE ${ARGN})
endmacro(clad_compat_add_llvm_loadable_module)
endif()


## Init

# In case this was a path to a build folder of llvm still try to find AddLLVM
Expand Down
2 changes: 1 addition & 1 deletion LastKnownGoodClangRevision.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26cac19a0d622afc91cd52a002921074bccc6a27
release_80
2 changes: 1 addition & 1 deletion LastKnownGoodLLVMRevision.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
da4a2839d80ac52958be0129b871beedfe90136e
release_80
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Note: Clad provides custom derivatives for some mathematical functions from `<cm
Note: *the concept of custom_derivatives will be reviewed soon, we intend to provide a different interface and avoid function name-based specifications and by-name lookups*.
## How to install
At the moment, only LLVM/Clang 5.0.x - 7.1.x are supported.
At the moment, only LLVM/Clang 5.0.x - 8.0.x are supported.
## How Clad works
Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST ([Clang AST](https://clang.llvm.org/docs/IntroductionToTheClangAST.html)) of user's source code. Then, [CladPlugin](https://github.com/vgvassilev/clad/blob/a264195f00792feeebe63ac7a8ab815c02d20eee/tools/ClangPlugin.h#L48), implemented as `clang::ASTConsumer` analyzes the AST to find differentiation requests for clad and process those requests by building Clang AST for derivative functions. The whole clad's operation sequence is the following:
Expand All @@ -138,15 +138,26 @@ Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST
* `ForwardModeVisitor` and `ReverseModeVisitor` are derived from `clang::StmtVisitor`. In the `Derive` method they analyze the AST of the declaration of the original function and create the AST for the declaration of derivative function. Then they proceed to recursively `Visit` every Stmt in original function's body and build the body for the derivative function. Forward/Reverse mode AD algorithm is implemented in `Visit...` methods, which are executed depending on the kind of AST node visited.
* The AST of the newly built derivative function's declaration is returned to `CladPlugin`, where the call to `clad::differentiate(f, ...)/clad::gradient(f, ...)` is [updated](https://github.com/vgvassilev/clad/blob/a264195f00792feeebe63ac7a8ab815c02d20eee/tools/ClangPlugin.cpp#L122) and `f` is replaced by a reference to the newly created derivative function `f_...`. This effectively results in the execution of `clad::differentiate(f_...)/clad::gradient(f_...)`, which constructs `CladFunction` with a pointer to the newly created derivative. Therefore, user's calls to `.execute` method will invoke the newly generated derivative.
* Finally, derivative's AST is [passed](https://github.com/vgvassilev/clad/blob/a264195f00792feeebe63ac7a8ab815c02d20eee/tools/ClangPlugin.cpp#L145) for further processing by Clang compiler (LLVM IR generation, optimizations, machine code generation, etc.).
### Building from source (example was tested on Ubuntu 18.04 LTS, tests are disabled)
### Building from source (example was tested on Ubuntu 18.04 LTS)
```
sudo apt install clang-7 llvm-7 clang-7-dev llvm-7-dev libllvm7 llvm-7-runtime llvm-7-tools
sudo apt install clang-8 llvm-8 clang-8-dev llvm-8-dev libllvm8 llvm-8-runtime llvm-8-tools
sudo -H pip install lit
git clone https://github.com/vgvassilev/clad.git clad
mkdir build_dir inst; cd build_dir
cmake ../clad -DClad_DIR=/usr/lib/llvm-7 -DLLVM_DIR=/usr/lib/llvm-7 -DCMAKE_INSTALL_PREFIX=../inst
cmake ../clad -DClad_DIR=/usr/lib/llvm-8 -DLLVM_DIR=/usr/lib/llvm-8 -DCMAKE_INSTALL_PREFIX=../inst -DLLVM_EXTERNAL_LIT="`which lit`"
make && make install
```
### Building from source (example was tested on MacOS Mojave 10.14.1)
```
brew install llvm@8
brew install python
sudo -H pip install lit
git clone https://github.com/vgvassilev/clad.git clad
mkdir build_dir inst; cd build_dir
cmake ../clad -DClad_DIR=/usr/local/opt/llvm@8 -DCMAKE_INSTALL_PREFIX=../inst -DLLVM_EXTERNAL_LIT="`which lit`"
make && make install
make check-clad
```
### Building from source LLVM, Clang and clad (development environment)
```
LAST_KNOWN_GOOD_LLVM=$(wget https://raw.githubusercontent.com/vgvassilev/clad/master/LastKnownGoodLLVMRevision.txt -O - -q --no-check-certificate)
Expand Down
Loading

0 comments on commit 056b7d9

Please sign in to comment.