Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LLVM17 support. #675

Merged
merged 3 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ jobs:
compiler: clang
clang-runtime: '16'

- name: osx-clang-runtime17
os: macos-latest
compiler: clang
clang-runtime: '17'

- name: win-msvc-runtime14
os: windows-latest
compiler: msvc
Expand Down Expand Up @@ -394,6 +399,10 @@ jobs:
clang-runtime: '16'
doc_build: true

- name: ubu22-clang16-runtime17
os: ubuntu-22.04
compiler: clang-16
clang-runtime: '17'
steps:
- uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -521,13 +530,13 @@ jobs:
echo "/usr/local/opt/ccache/libexec" >> $GITHUB_PATH
PATH_TO_LLVM_BUILD=/usr/local/opt/llvm@${{ matrix.clang-runtime }}/

# For now Package llvm@17 is unsuported on brew, llvm <=@11 are deprecated or deleted.
# For now Package llvm@18 is unsuported on brew, llvm <=@11 are deprecated or deleted.
# Install llvm from github releases.
if [[ '${{ matrix.clang-runtime }}' == '17' || ${{ matrix.clang-runtime }} -le 11 ]]; then
if [[ '${{ matrix.clang-runtime }}' == '18' || ${{ matrix.clang-runtime }} -le 11 ]]; then
PATH_TO_LLVM_BUILD=/usr/local/opt/llvm@${{ matrix.clang-runtime }}/
pushd /usr/local/opt
w="0"
[ '${{ matrix.clang-runtime }}' == '17' ] && w="5"
[ '${{ matrix.clang-runtime }}' == '17' ] && w="6"
sudo curl -L https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.clang-runtime }}.0.$w/clang+llvm-${{ matrix.clang-runtime }}.0.$w-x86_64-apple-darwin.tar.xz | sudo xz -d -c | sudo tar -x
sudo rm -fr /usr/local/clang*
sudo mv clang+llvm-${{ matrix.clang-runtime }}.0.$w-x86_64-apple-darwin/ llvm@${{ matrix.clang-runtime }}/
Expand All @@ -544,7 +553,7 @@ jobs:
popd
fi

if [[ '${{ matrix.clang-runtime }}' != '17' && ${{ matrix.clang-runtime }} -gt 11 ]]; then
if [[ '${{ matrix.clang-runtime }}' != '18' && ${{ matrix.clang-runtime }} -gt 11 ]]; then
brew install llvm@${{ matrix.clang-runtime }}
else
# Remove the c++ headers that come with the llvm package from homebrew
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Clad also provides certain flags to save and print the generated derivative code
- To print the Clad generated derivative: `-Xclang -plugin-arg-clad -Xclang -fdump-derived-fn`

## How to install
At the moment, LLVM/Clang 5.0.x - 16.0.x are supported.
At the moment, LLVM/Clang 5.0.x - 17.0.x are supported.

### Conda Installation

Expand Down
2 changes: 1 addition & 1 deletion docs/internalDocs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ described first.
External Dependencies
---------------------

* Clad now works with clang-5.0 to clang-16
* Clad now works with clang-5.0 to clang-17


Forward Mode & Reverse Mode
Expand Down
3 changes: 2 additions & 1 deletion include/clad/Differentiator/ReverseModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ namespace clad {

/// Returns `ConditionResult` object for the counter.
clang::Sema::ConditionResult getCounterConditionResult() {
return m_RMV.m_Sema.ActOnCondition(m_RMV.m_CurScope, noLoc, m_Ref,
return m_RMV.m_Sema.ActOnCondition(m_RMV.getCurrentScope(), noLoc,
m_Ref,
clang::Sema::ConditionKind::Boolean);
}
};
Expand Down
50 changes: 30 additions & 20 deletions include/clad/Differentiator/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ namespace clad {
VisitorBase(DerivativeBuilder& builder)
: m_Builder(builder), m_Sema(builder.m_Sema),
m_CladPlugin(builder.m_CladPlugin), m_Context(builder.m_Context),
m_CurScope(m_Sema.TUScope), m_DerivativeFnScope(nullptr),
m_DerivativeInFlight(false), m_Derivative(nullptr),
m_Function(nullptr) {}
m_DerivativeFnScope(nullptr), m_DerivativeInFlight(false),
m_Derivative(nullptr), m_Function(nullptr) {}

using Stmts = llvm::SmallVector<clang::Stmt*, 16>;

Expand All @@ -114,7 +113,6 @@ namespace clad {
plugin::CladPlugin& m_CladPlugin;
clang::ASTContext& m_Context;
/// Current Scope at the point of visiting.
clang::Scope* m_CurScope;
/// Pointer to the topmost Scope in the created derivative function.
clang::Scope* m_DerivativeFnScope;
bool m_DerivativeInFlight;
Expand Down Expand Up @@ -148,10 +146,12 @@ namespace clad {
// FIXME: Fix this inconsistency, by making `this` pointer derivative
// expression to be of object type in the reverse mode as well.
clang::Expr* m_ThisExprDerivative = nullptr;

/// A function used to wrap result of visiting E in a lambda. Returns a call
/// to the built lambda. Func is a functor that will be invoked inside
/// lambda scope and block. Statements inside lambda are expected to be
/// added by addToCurrentBlock from func invocation.
// FIXME: This will become problematic when we try to support C.
template <typename F>
static clang::Expr* wrapInLambda(VisitorBase& V, clang::Sema& S,
const clang::Expr* E, F&& func) {
Expand All @@ -168,11 +168,26 @@ namespace clad {
clang::Declarator D(DS,
CLAD_COMPAT_CLANG15_Declarator_DeclarationAttrs_ExtraParam
CLAD_COMPAT_CLANG12_Declarator_LambdaExpr);
#if CLANG_VERSION_MAJOR > 16
V.beginScope(clang::Scope::LambdaScope | clang::Scope::DeclScope |
vgvassilev marked this conversation as resolved.
Show resolved Hide resolved
clang::Scope::FunctionDeclarationScope |
clang::Scope::FunctionPrototypeScope);
#endif // CLANG_VERSION_MAJOR
S.PushLambdaScope();
#if CLANG_VERSION_MAJOR > 16
S.ActOnLambdaExpressionAfterIntroducer(Intro, V.getCurrentScope());
vgvassilev marked this conversation as resolved.
Show resolved Hide resolved

S.ActOnLambdaClosureParameters(V.getCurrentScope(), /*ParamInfo=*/{});
vgvassilev marked this conversation as resolved.
Show resolved Hide resolved
#endif // CLANG_VERSION_MAJOR

V.beginScope(clang::Scope::BlockScope | clang::Scope::FnScope |
clang::Scope::DeclScope);
clang::Scope::DeclScope | clang::Scope::CompoundStmtScope);
S.ActOnStartOfLambdaDefinition(Intro, D,
clad_compat::Sema_ActOnStartOfLambdaDefinition_ScopeOrDeclSpec(V.getCurrentScope(), DS));
#if CLANG_VERSION_MAJOR > 16
V.endScope();
#endif // CLANG_VERSION_MAJOR

V.beginBlock();
func();
clang::CompoundStmt* body = V.endBlock();
Expand Down Expand Up @@ -211,22 +226,17 @@ namespace clad {
bool addToBlock(clang::Stmt* S, Stmts& block);

/// Get a current scope.
clang::Scope* getCurrentScope() { return m_CurScope; }
/// FIXME: Remove the pointer-ref
// clang::Scope* getCurrentScope() { return m_Sema.getCurScope(); }
clang::Scope*& getCurrentScope();
void setCurrentScope(clang::Scope* S);
/// Returns the innermost enclosing file context which can be either a
/// namespace or the TU scope.
clang::Scope* getEnclosingNamespaceOrTUScope();

/// Enters a new scope.
void beginScope(unsigned ScopeFlags) {
// FIXME: since Sema::CurScope is private, we cannot access it and have
// to use separate member variable m_CurScope. The only options to set
// CurScope of Sema seemt to be through Parser or ContextAndScopeRAII.
m_CurScope =
new clang::Scope(getCurrentScope(), ScopeFlags, m_Sema.Diags);
}
void endScope() {
// This will remove all the decls in the scope from the IdResolver.
m_Sema.ActOnPopScope(noLoc, m_CurScope);
auto oldScope = m_CurScope;
m_CurScope = oldScope->getParent();
delete oldScope;
}
void beginScope(unsigned ScopeFlags);
void endScope();

/// A shorthand to simplify syntax for creation of new expressions.
/// This function uses m_Sema.BuildUnOp internally to build unary
Expand Down
18 changes: 10 additions & 8 deletions lib/Differentiator/BaseForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ BaseForwardModeVisitor::Derive(const FunctionDecl* FD,
SourceLocation loc{m_Function->getLocation()};
DeclarationNameInfo name(II, loc);
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> SaveScope(getCurrentScope());
DeclContext* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
m_Sema.CurContext = DC;
DeclWithContext result =
Expand Down Expand Up @@ -426,7 +426,8 @@ BaseForwardModeVisitor::DerivePushforward(const FunctionDecl* FD,
QualType derivedFnType = m_Context.getFunctionType(
returnType, paramTypes, originalFnType->getExtProtoInfo());
llvm::SaveAndRestore<DeclContext*> saveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> saveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> saveScope(getCurrentScope(),
getEnclosingNamespaceOrTUScope());
auto* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
m_Sema.CurContext = DC;

Expand Down Expand Up @@ -625,11 +626,11 @@ StmtDiff BaseForwardModeVisitor::VisitConditionalOperator(
StmtDiff ifFalseDiff = Visit(CO->getFalseExpr());

cond = StoreAndRef(cond);
cond =
m_Sema
.ActOnCondition(m_CurScope, noLoc, cond, Sema::ConditionKind::Boolean)
.get()
.second;
cond = m_Sema
.ActOnCondition(getCurrentScope(), noLoc, cond,
Sema::ConditionKind::Boolean)
.get()
.second;

Expr* condExpr =
m_Sema
Expand Down Expand Up @@ -737,7 +738,8 @@ StmtDiff BaseForwardModeVisitor::VisitReturnStmt(const ReturnStmt* RS) {

StmtDiff retValDiff = Visit(RS->getRetValue());
Stmt* returnStmt =
m_Sema.ActOnReturnStmt(noLoc, retValDiff.getExpr_dx(), m_CurScope).get();
m_Sema.ActOnReturnStmt(noLoc, retValDiff.getExpr_dx(), getCurrentScope())
.get();
return StmtDiff(returnStmt);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Differentiator/HessianModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ namespace clad {
// Create the gradient function declaration.
DeclContext* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> SaveScope(getCurrentScope(),
getEnclosingNamespaceOrTUScope());
m_Sema.CurContext = DC;

DeclWithContext result = m_Builder.cloneFunction(
Expand Down
3 changes: 2 additions & 1 deletion lib/Differentiator/ReverseModeForwPassVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ ReverseModeForwPassVisitor::Derive(const FunctionDecl* FD,
sourceFnType->getExtProtoInfo());

llvm::SaveAndRestore<DeclContext*> saveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> saveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> saveScope(getCurrentScope(),
getEnclosingNamespaceOrTUScope());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
m_Sema.CurContext = const_cast<DeclContext*>(m_Function->getDeclContext());

Expand Down
18 changes: 8 additions & 10 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,

// Create the gradient function declaration.
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> SaveScope(getCurrentScope(),
getEnclosingNamespaceOrTUScope());
auto* DC = const_cast<DeclContext*>(m_Function->getDeclContext());
m_Sema.CurContext = DC;
DeclWithContext result = m_Builder.cloneFunction(
Expand Down Expand Up @@ -488,7 +489,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
m_Context.VoidTy, paramTypes, originalFnType->getExtProtoInfo());

llvm::SaveAndRestore<DeclContext*> saveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> saveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> saveScope(getCurrentScope(),
getEnclosingNamespaceOrTUScope());
m_Sema.CurContext = const_cast<DeclContext*>(m_Function->getDeclContext());

DeclWithContext fnBuildRes = m_Builder.cloneFunction(
Expand Down Expand Up @@ -815,9 +817,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
for (Stmt*& S : cond.getBothStmts())
if (S)
S = m_Sema
.ActOnCondition(m_CurScope,
noLoc,
cast<Expr>(S),
.ActOnCondition(getCurrentScope(), noLoc, cast<Expr>(S),
Sema::ConditionKind::Boolean)
.get()
.second;
Expand Down Expand Up @@ -935,9 +935,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
// StmtDiff.
for (Stmt*& S : cond.getBothStmts())
S = m_Sema
.ActOnCondition(m_CurScope,
noLoc,
cast<Expr>(S),
.ActOnCondition(getCurrentScope(), noLoc, cast<Expr>(S),
Sema::ConditionKind::Boolean)
.get()
.second;
Expand Down Expand Up @@ -2842,9 +2840,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
// to let Sema::LookupName see the whole scope.
auto* identifier = CreateUniqueIdentifier(prefix);
// Save current scope and temporarily go to topmost function scope.
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> SaveScope(getCurrentScope());
assert(m_DerivativeFnScope && "must be set");
m_CurScope = m_DerivativeFnScope;
setCurrentScope(m_DerivativeFnScope);

VarDecl* Var = nullptr;
if (isa<ArrayType>(Type)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Differentiator/VectorForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ VectorForwardModeVisitor::DeriveVectorMode(const FunctionDecl* FD,

// Function declaration scope
llvm::SaveAndRestore<DeclContext*> SaveContext(m_Sema.CurContext);
llvm::SaveAndRestore<Scope*> SaveScope(m_CurScope);
llvm::SaveAndRestore<Scope*> SaveScope(getCurrentScope());
beginScope(Scope::FunctionPrototypeScope | Scope::FunctionDeclarationScope |
Scope::DeclScope);
m_Sema.PushFunctionScope();
Expand Down
Loading
Loading