Skip to content

Commit

Permalink
Fix clad development style builds. Add clang 10/git support.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-penev authored and vgvassilev committed Nov 28, 2019
1 parent 79f82b0 commit 4faf53e
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 105 deletions.
29 changes: 5 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
cmake_minimum_required(VERSION 3.7.0)

if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()

# If we are not building as a part of LLVM, build clad as an
# standalone project, using LLVM as an external library:
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
Expand Down Expand Up @@ -109,30 +113,6 @@ 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()

# Clang 9 remove add_version_info_from_vcs.
# Recomended is use of get_source_info
if (NOT CLANG_PACKAGE_VERSION VERSION_LESS 9.0)
macro(add_version_info_from_vcs VERS)
get_source_info(${CMAKE_CURRENT_SOURCE_DIR} VERS DUMMY_REP)
endmacro(add_version_info_from_vcs)
endif()


## Init

# In case this was a path to a build folder of llvm still try to find AddLLVM
Expand Down Expand Up @@ -194,6 +174,7 @@ if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
endif()

##
include(Compatibility.cmake)

set(C_INCLUDE_DIRS "" CACHE STRING
"Colon separated list of directories clad will search for headers.")
Expand Down
30 changes: 30 additions & 0 deletions Compatibility.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Compatibility

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

# Clang 9 remove add_version_info_from_vcs.
# Recomended is use of get_source_info
if (NOT COMMAND add_version_info_from_vcs)
set(clad_compat__isClang9 TRUE)
macro(add_version_info_from_vcs VERS)
get_source_info(${CMAKE_CURRENT_SOURCE_DIR} VERS DUMMY_REP)
endmacro(add_version_info_from_vcs)
endif()

# Clang 9 change find_first_existing_vc_file interface.
# find_first_existing_vc_file(var path) --> find_first_existing_vc_file(path var)
if (clad_compat__isClang9)
macro(clad_compat__find_first_existing_vc_file path out_var)
find_first_existing_vc_file(${path} out_var)
endmacro(clad_compat__find_first_existing_vc_file)
else()
macro(clad_compat__find_first_existing_vc_file path out_var)
find_first_existing_vc_file(out_var ${path})
endmacro(clad_compat__find_first_existing_vc_file)
endif()
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST
```
### Building from source LLVM, Clang and clad (development environment)
```
sudo -H pip install lit
LAST_KNOWN_GOOD_LLVM=$(wget https://raw.githubusercontent.com/vgvassilev/clad/master/LastKnownGoodLLVMRevision.txt -O - -q --no-check-certificate)
LAST_KNOWN_GOOD_CLANG=$(wget https://raw.githubusercontent.com/vgvassilev/clad/master/LastKnownGoodClangRevision.txt -O - -q --no-check-certificate)
git clone https://github.com/llvm-mirror/llvm.git src
Expand All @@ -175,7 +176,7 @@ Clad is a plugin for the Clang compiler. It relies on the Clang to build the AST
cd ../
mkdir obj inst
cd obj
cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DCMAKE_INSTALL_PREFIX=../inst ../src/
cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=host -DCMAKE_INSTALL_PREFIX=../inst -DLLVM_EXTERNAL_LIT="`which lit`" ../src/
make && make install
```
Expand Down
17 changes: 17 additions & 0 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,23 @@ static inline ConstexprSpecKind Function_GetConstexprKind(const FunctionDecl* F)
#endif


// Clang 10 change add new param in getConstantArrayType.

static inline QualType getConstantArrayType(const ASTContext &Ctx,
QualType EltTy,
const llvm::APInt &ArySize,
const Expr* SizeExpr,
ArrayType::ArraySizeModifier ASM,
unsigned IndexTypeQuals)
{
#if CLANG_VERSION_MAJOR < 10
return Ctx.getConstantArrayType(EltTy, ArySize, ASM, IndexTypeQuals);
#elif CLANG_VERSION_MAJOR >= 10
return Ctx.getConstantArrayType(EltTy, ArySize, SizeExpr, ASM, IndexTypeQuals);
#endif
}


} // namespace clad_compat

#endif //CLAD_COMPATIBILITY
2 changes: 1 addition & 1 deletion include/clad/Differentiator/DerivativeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace clad {
namespace utils {
class StmtClone;
}
class DiffRequest;
struct DiffRequest;
namespace plugin {
class CladPlugin;
clang::FunctionDecl* ProcessDiffRequest(CladPlugin& P, DiffRequest& request);
Expand Down
92 changes: 22 additions & 70 deletions lib/Differentiator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,77 +1,29 @@
# Figure out if we can track VC revisions.
function(find_first_existing_file out_var)
foreach(file ${ARGN})
if(EXISTS "${file}")
set(${out_var} "${file}" PARENT_SCOPE)
return()
endif()
endforeach()
endfunction()

macro(find_first_existing_vc_file out_var path)
set(git_path "${path}/.git")

# Normally '.git' is a directory that contains a 'logs/HEAD' file that
# is updated as modifications are made to the repository. In case the
# repository is a Git submodule, '.git' is a file that contains text that
# indicates where the repository's Git directory exists.
if (EXISTS "${git_path}" AND NOT IS_DIRECTORY "${git_path}")
FILE(READ "${git_path}" file_contents)
if("${file_contents}" MATCHES "^gitdir: ([^\n]+)")
# '.git' is indeed a link to the submodule's Git directory.
# Use the path to that Git directory.
set(git_path "${path}/${CMAKE_MATCH_1}")
endif()
endif()

find_first_existing_file(${out_var}
"${git_path}/logs/HEAD" # Git or Git submodule
"${path}/.svn/wc.db" # SVN 1.7
"${path}/.svn/entries" # SVN 1.6
)
endmacro()

set(CLANG_SOURCE_DIR ${CLAD_SOURCE_DIR}/../clang/)
find_first_existing_vc_file(clang_vc "${CLANG_SOURCE_DIR}")
find_first_existing_vc_file(clad_vc "${CLAD_SOURCE_DIR}")
clad_compat__find_first_existing_vc_file("${CLANG_SOURCE_DIR}" clang_vc)
clad_compat__find_first_existing_vc_file("${CLAD_SOURCE_DIR}" clad_vc)
set(last_known_good_rev "${CLAD_SOURCE_DIR}/LastKnownGoodLLVMRevision.txt")

# The VC revision include that we want to generate.
set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/SVNVersion.inc")

set(get_svn_script "${LLVM_CMAKE_PATH}/GetSVN.cmake")

if(DEFINED clang_vc AND DEFINED clad_vc)
# Create custom target to generate the VC revision include.
add_custom_command(OUTPUT "${version_inc}"
DEPENDS "${clang_vc}" "${clad_vc}" "${get_svn_script}" "${last_known_good_rev}"
COMMAND
${CMAKE_COMMAND} "-DFIRST_SOURCE_DIR=${CLANG_SOURCE_DIR}"
"-DFIRST_NAME=CLANG"
"-DSECOND_SOURCE_DIR=${CLAD_SOURCE_DIR}"
"-DSECOND_NAME=CLAD"
"-DHEADER_FILE=${version_inc}"
-P "${get_svn_script}")

# Mark the generated header as being generated.
set_source_files_properties("${version_inc}"
PROPERTIES GENERATED TRUE
HEADER_FILE_ONLY TRUE)

# Tell Version.cpp that it needs to build with -DHAVE_SVN_VERSION_INC.
set_source_files_properties(Version.cpp
PROPERTIES COMPILE_DEFINITIONS "HAVE_CLAD_VERSION_INC")
else()
# Not producing a VC revision include.
set(version_inc)

# Being able to force-set the SVN revision in cases where it isn't available
# is useful for performance tracking, and matches compatibility from autoconf.
if(SVN_REVISION)
set_source_files_properties(Version.cpp
PROPERTIES COMPILE_DEFINITIONS "CLAD_REVISION=\"${SVN_REVISION}\"")
endif()
endif()
set(version_inc "${CMAKE_CURRENT_BINARY_DIR}/VCSVersion.inc")

set(generate_vcs_version_script "${LLVM_CMAKE_PATH}/GenerateVersionFromVCS.cmake")

# Create custom target to generate the VC revision include.
add_custom_command(OUTPUT "${version_inc}"
DEPENDS "${clang_vc}" "${clad_vc}" "${generate_vcs_version_script}"
COMMAND ${CMAKE_COMMAND} "-DNAMES=\"LLVM;CLANG\""
"-DLLVM_SOURCE_DIR=${llvm_source_dir}"
"-DCLANG_SOURCE_DIR=${clang_source_dir}"
"-DHEADER_FILE=${version_inc}"
-P "${generate_vcs_version_script}")

# Mark the generated header as being generated.
set_source_files_properties("${version_inc}"
PROPERTIES GENERATED TRUE
HEADER_FILE_ONLY TRUE)

set_property(SOURCE Version.cpp APPEND PROPERTY
COMPILE_DEFINITIONS "HAVE_VCS_VERSION_INC")

file(READ ${last_known_good_rev} CLAD_CLANG_COMPAT_REVISION)
# Trim spaces
Expand Down
2 changes: 1 addition & 1 deletion lib/Differentiator/ConstantFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace clad {

Expr* ConstantFolder::synthesizeLiteral(QualType QT, ASTContext& C,
uint64_t val) {
SourceLocation noLoc;
//SourceLocation noLoc;
Expr* Result = 0;
if (QT->isIntegralType(C)) {
llvm::APInt APVal(C.getIntWidth(QT), val,
Expand Down
6 changes: 4 additions & 2 deletions lib/Differentiator/DerivativeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,10 @@ namespace clad {
// Converts an independent argument from VarDecl to a StringLiteral Expr
QualType CharTyConst = m_Context.CharTy.withConst();
QualType StrTy =
m_Context.getConstantArrayType(CharTyConst,
clad_compat::getConstantArrayType(m_Context, CharTyConst,
llvm::APInt(32,
independentArg->getNameAsString().size() + 1),
nullptr,
ArrayType::Normal,
/*IndexTypeQuals*/0);
StringLiteral* independentArgString =
Expand Down Expand Up @@ -2509,8 +2510,9 @@ namespace clad {
// We also need to create an array to store the result of gradient call.
auto size_type_bits = m_Context.getIntWidth(m_Context.getSizeType());
auto ArrayType =
m_Context.getConstantArrayType(CE->getType(),
clad_compat::getConstantArrayType(m_Context, CE->getType(),
llvm::APInt(size_type_bits, NArgs),
nullptr,
ArrayType::ArraySizeModifier::Normal,
0); // No IndexTypeQualifiers

Expand Down
5 changes: 3 additions & 2 deletions lib/Differentiator/DiffPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ namespace clad {
// the nul terminator character as well as the string length for pascal
// strings.
QualType StrTy =
C.getConstantArrayType(CharTyConst,
clad_compat::getConstantArrayType(C, CharTyConst,
llvm::APInt(32, Out.str().size() + 1),
nullptr,
ArrayType::Normal,
/*IndexTypeQuals*/0);

Expand Down Expand Up @@ -140,7 +141,7 @@ namespace clad {
call->setCallee(CladGradientExprNew);
}

DiffCollector::DiffCollector(DeclGroupRef DGR, DiffSchedule& plans, Sema& S)
DiffCollector::DiffCollector(DeclGroupRef DGR, DiffSchedule& plans, clang::Sema& S)
: m_DiffPlans(plans), m_TopMostFD(nullptr), m_Sema(S) {
if (DGR.isSingleDecl())
TraverseDecl(DGR.getSingleDecl());
Expand Down
4 changes: 2 additions & 2 deletions lib/Differentiator/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "clad/Differentiator/Version.h"

#ifdef HAVE_CLAD_VERSION_INC
# include "SVNVersion.inc"
#ifdef HAVE_VCS_VERSION_INC
#include "VCSVersion.inc"
#endif

namespace clad {
Expand Down
2 changes: 1 addition & 1 deletion tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_llvm_library(cladPlugin
RequiredSymbols.cpp
)
if (NOT CLAD_BUILD_STATIC_ONLY)
clad_compat_add_llvm_loadable_module(clad
add_llvm_loadable_module(clad
ClangPlugin.cpp
RequiredSymbols.cpp
PLUGIN_TOOL
Expand Down
2 changes: 1 addition & 1 deletion tools/ClangPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace clang {
}

namespace clad {
class DiffRequest;
struct DiffRequest;
namespace plugin {
struct DifferentiationOptions {
DifferentiationOptions()
Expand Down

0 comments on commit 4faf53e

Please sign in to comment.