Skip to content

Commit

Permalink
Fix various issues raised in PR vgvassilev#824
Browse files Browse the repository at this point in the history
Co-Authored-By: Vassil Vassilev <[email protected]>
Co-Authored-By: Alexander Penev <[email protected]>
  • Loading branch information
3 people committed Mar 31, 2024
1 parent a37878b commit ef79ecf
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 208 deletions.
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 8.0.x - 18.x.x are supported.
At the moment, LLVM/Clang 8.0.x - 18.1.x are supported.

### Conda Installation

Expand Down
197 changes: 84 additions & 113 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,97 +21,76 @@ namespace clad_compat {
using namespace clang;
using namespace llvm;


//clang-18 CXXThisExpr got extra argument
// clang-18 CXXThisExpr got extra argument

#if CLANG_VERSION_MAJOR > 17
#define CLAD_COMPAT_CLANG17_CXXThisExpr_ExtraParam DEFINE_CREATE_EXPR(CXXThisExpr, (Ctx,
#define CLAD_COMPAT_CLANG17_CXXThisExpr_ExtraParam DEFINE_CREATE_EXPR(CXXThisExpr, (Ctx,
#else
#define CLAD_COMPAT_CLANG17_CXXThisExpr_ExtraParam DEFINE_CLONE_EXPR(CXXThisExpr, (
#endif
#define CLAD_COMPAT_CLANG17_CXXThisExpr_ExtraParam DEFINE_CLONE_EXPR(CXXThisExpr, (
#endif

//clang-18 ActOnLambdaExpr got extra argument
// clang-18 ActOnLambdaExpr got extra argument

#if CLANG_VERSION_MAJOR > 17
#define CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(V) /**/
#define CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(V) /**/
#else
#define CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(V) ,(V).getCurrentScope()
#endif
#define CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(V) \
, (V).getCurrentScope()
#endif

//Clang 18 ArrayType::Normal -> ArraySizeModifier::Normal
// Clang 18 ArrayType::Normal -> ArraySizeModifier::Normal

#if LLVM_VERSION_MAJOR < 18
#define ArrayType_Normal ArrayType::Normal
const auto ArraySizeModifier_Normal = clang::ArrayType::Normal;
#else
#define ArrayType_Normal ArraySizeModifier::Normal
const auto ArraySizeModifier_Normal = clang::ArraySizeModifier::Normal;
#endif

// Compatibility helper function for creation UnresolvedLookupExpr.
// Clang-18 extra argument knowndependent.
//FIXME: Knowndependent set to false temporarily until known value found for initialisation.

static inline Stmt* UnresolvedLookupExpr_Create(const ASTContext &Ctx,
CXXRecordDecl *NamingClass,
NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc,
const DeclarationNameInfo &NameInfo,
bool RequiresADL,
const TemplateArgumentListInfo *Args,
UnresolvedSetIterator Begin,
UnresolvedSetIterator End
)
{
// FIXME: Knowndependent set to false temporarily until known value found for
// initialisation.

static inline Stmt* UnresolvedLookupExpr_Create(
const ASTContext& Ctx, CXXRecordDecl* NamingClass,
NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
const DeclarationNameInfo& NameInfo, bool RequiresADL,
const TemplateArgumentListInfo* Args, UnresolvedSetIterator Begin,
UnresolvedSetIterator End) {

#if CLANG_VERSION_MAJOR < 18
return UnresolvedLookupExpr::Create(Ctx, NamingClass, QualifierLoc,
TemplateKWLoc, NameInfo, RequiresADL,
// They get copied again by
// OverloadExpr, so we are safe.
Args, Begin, End);

#else
bool KnownDependent = false;
return UnresolvedLookupExpr::Create(Ctx, NamingClass, QualifierLoc,
TemplateKWLoc, NameInfo, RequiresADL,
// They get copied again by
// OverloadExpr, so we are safe.
Args, Begin, End, KnownDependent);
#endif
}

#if CLANG_VERSION_MAJOR < 18
return UnresolvedLookupExpr::Create(Ctx,
NamingClass,
QualifierLoc,
TemplateKWLoc,
NameInfo,
RequiresADL,
// They get copied again by
// OverloadExpr, so we are safe.
Args,
Begin,
End
);

#else
bool KnownDependent=false;
return UnresolvedLookupExpr::Create(Ctx,
NamingClass,
QualifierLoc,
TemplateKWLoc,
NameInfo,
RequiresADL,
// They get copied again by
// OverloadExpr, so we are safe.
Args,
Begin,
End,
KnownDependent
);
#endif

}

//Clang 18 ETK_None -> ElaboratedTypeKeyword::None
// Clang 18 ETK_None -> ElaboratedTypeKeyword::None

#if LLVM_VERSION_MAJOR < 18
#define ElaboratedTypeKeyword_None ETK_None
const auto ElaboratedTypeKeyword_None = ETK_None;
#else
#define ElaboratedTypeKeyword_None ElaboratedTypeKeyword::None
const auto ElaboratedTypeKeyword_None = ElaboratedTypeKeyword::None;
#endif

//Clang 18 endswith->ends_with
//and starstwith->starts_with
// Clang 18 endswith->ends_with
// and starstwith->starts_with

#if LLVM_VERSION_MAJOR < 18
#define starts_with startswith
#define ends_with startswith
#endif


// Compatibility helper function for creation CompoundStmt.
// Clang 15 and above use a extra param FPFeatures in CompoundStmt::Create.

Expand Down Expand Up @@ -187,9 +166,10 @@ static inline IfStmt* IfStmt_Create(const ASTContext &Ctx,
{

#if CLANG_VERSION_MAJOR < 12
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, Then, EL, Else);
#elif CLANG_VERSION_MAJOR < 14
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, LPL, RPL, Then, EL, Else);
return IfStmt::Create(Ctx, IL, IsConstexpr, Init, Var, Cond, LPL, RPL, Then,
EL, Else);
#elif CLANG_VERSION_MAJOR >= 14
IfStatementKind kind = IfStatementKind::Ordinary;
if (IsConstexpr)
Expand All @@ -198,8 +178,6 @@ static inline IfStmt* IfStmt_Create(const ASTContext &Ctx,
#endif
}



// Compatibility helper function for creation CallExpr.
// Clang 12 and above use one extra param.

Expand All @@ -219,11 +197,11 @@ static inline CallExpr* CallExpr_Create(const ASTContext &Ctx, Expr *Fn, ArrayRe
}
#endif


// Clang 12 and above use one extra param.

#if CLANG_VERSION_MAJOR < 12
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams ,Node->getNumArgs(),Node->getADLCallKind()
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams \
, Node->getNumArgs(), Node->getADLCallKind()
#elif CLANG_VERSION_MAJOR >= 12
#define CLAD_COMPAT_CLANG8_CallExpr_ExtraParams ,Node->getFPFeatures(),Node->getNumArgs(),Node->getADLCallKind()
#endif
Expand Down Expand Up @@ -278,9 +256,6 @@ static inline void ExprSetDeps(Expr* result, Expr* Node) {
#define CLAD_COMPAT_CLANG11_WhileStmt_ExtraParams ,Node->getLParenLoc(),Node->getRParenLoc()
#endif




// Compatibility helper function for creation CXXMemberCallExpr.
// Clang 12 and above use two extra param.

Expand All @@ -300,7 +275,6 @@ static inline CXXMemberCallExpr* CXXMemberCallExpr_Create(ASTContext &Ctx,
}
#endif


// Compatibility helper function for creation SwitchStmt.
// Clang 12 and above use two extra params.

Expand Down Expand Up @@ -357,33 +331,29 @@ static inline ConstexprSpecKind Function_GetConstexprKind(const FunctionDecl* F)
#define PragmaIntroducer PragmaIntroducerKind
#endif


// Clang 10 change add new param in getConstantArrayType.
// clang 18 clang::ArrayType::ArraySizeModifier became clang::ArraySizeModifier
#if CLANG_VERSION_MAJOR < 18
static inline QualType getConstantArrayType(const ASTContext &Ctx,
QualType EltTy,
const APInt &ArySize,
const Expr* SizeExpr,
clang::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
}
static inline QualType
getConstantArrayType(const ASTContext& Ctx, QualType EltTy,
const APInt& ArySize, const Expr* SizeExpr,
clang::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
}
#else
static inline QualType getConstantArrayType(const ASTContext &Ctx,
QualType EltTy,
const APInt &ArySize,
const Expr* SizeExpr,
clang::ArraySizeModifier ASM,
unsigned IndexTypeQuals)
{
return Ctx.getConstantArrayType(EltTy, ArySize, SizeExpr, ASM, IndexTypeQuals);
}
static inline QualType
getConstantArrayType(const ASTContext& Ctx, QualType EltTy,
const APInt& ArySize, const Expr* SizeExpr,
clang::ArraySizeModifier ASM, unsigned IndexTypeQuals) {
return Ctx.getConstantArrayType(EltTy, ArySize, SizeExpr, ASM,
IndexTypeQuals);
}
#endif

// Clang 10 add new last param TrailingRequiresClause in FunctionDecl::Create
Expand Down Expand Up @@ -461,12 +431,11 @@ static inline ConstexprSpecKind Function_GetConstexprKind(const FunctionDecl* F)
#define CLAD_COMPAT_CLANG12_LR_ExtraParams(Node) ,Node->getLParenLoc(),Node->getRParenLoc()
#endif


/// Clang < 9, do not provide `Sema::BuildCXXThisExpr` function.
static inline CXXThisExpr* Sema_BuildCXXThisExpr(Sema& SemaRef,
const CXXMethodDecl* method) {
auto thisType = method->getThisType();
SourceLocation noLoc;
auto thisType = method->getThisType();
SourceLocation noLoc;
#if CLANG_VERSION_MAJOR >= 9
return cast<CXXThisExpr>(
SemaRef.BuildCXXThisExpr(noLoc, thisType, /*IsImplicit=*/true));
Expand Down Expand Up @@ -608,12 +577,12 @@ CXXMethodDecl_GetThisObjectType(Sema& semaRef, const CXXMethodDecl* MD) {
#else
static inline QualType
CXXMethodDecl_GetThisObjectType(Sema& semaRef, const CXXMethodDecl* MD) {
//clang-18 renamed getThisObjectType to getFunctionObjectParameterType
#if CLANG_VERSION_MAJOR < 18
return MD->getThisObjectType();
#else
return MD->getFunctionObjectParameterType();
#endif
// clang-18 renamed getThisObjectType to getFunctionObjectParameterType
#if CLANG_VERSION_MAJOR < 18
return MD->getThisObjectType();
#else
return MD->getFunctionObjectParameterType();
#endif
}
#endif

Expand Down Expand Up @@ -705,16 +674,18 @@ static inline bool IsPRValue(const Expr* E) { return E->isPRValue(); }
#endif

// Clang 15 renamed StringKind::Ascii to StringKind::Ordinary
// Clang 18 renamed clang::StringLiteral::StringKind::Ordinary became clang::StringLiteralKind::Ordinary;
// Clang 18 renamed clang::StringLiteral::StringKind::Ordinary became
// clang::StringLiteralKind::Ordinary;

#if CLANG_VERSION_MAJOR < 15
const auto StringKind_Ordinary = clang::StringLiteral::StringKind::Ascii;
const auto StringLiteralKind_Ordinary = clang::StringLiteral::StringKind::Ascii;
#elif CLANG_VERSION_MAJOR >= 15
#if CLANG_VERSION_MAJOR < 18
const auto StringKind_Ordinary = clang::StringLiteral::StringKind::Ordinary;
#else
const auto StringKind_Ordinary = clang::StringLiteralKind::Ordinary;
#endif
#if CLANG_VERSION_MAJOR < 18
const auto StringLiteralKind_Ordinary =
clang::StringLiteral::StringKind::Ordinary;
#else
const auto StringLiteralKind_Ordinary = clang::StringLiteralKind::Ordinary;
#endif
#endif

// Clang 15 add one extra param to Sema::CheckFunctionDeclaration
Expand Down
9 changes: 7 additions & 2 deletions include/clad/Differentiator/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,13 @@ namespace clad {
V.beginBlock();
func();
clang::CompoundStmt* body = V.endBlock();
clang::Expr* lambda = S.ActOnLambdaExpr(noLoc, body/*,*/
CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(V)).get();
clang::Expr* lambda =
S.ActOnLambdaExpr(
noLoc,
body /*,*/
CLAD_COMPAT_CLANG17_ActOnLambdaExpr_getCurrentScope_ExtraParam(
V))
.get();
V.endScope();
return S.ActOnCallExpr(V.getCurrentScope(), lambda, noLoc, {}, noLoc)
.get();
Expand Down
41 changes: 22 additions & 19 deletions lib/Differentiator/BaseForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,10 @@ BaseForwardModeVisitor::VisitArraySubscriptExpr(const ArraySubscriptExpr* ASE) {
llvm::APSInt index;
Expr* diffExpr = nullptr;
Expr::EvalResult res;
Expr::SideEffectsKind AllowSideEffects = Expr::SideEffectsKind::SE_NoSideEffects;
if (!clonedIndices.back()->EvaluateAsInt(res, m_Context, AllowSideEffects)) {
Expr::SideEffectsKind AllowSideEffects =
Expr::SideEffectsKind::SE_NoSideEffects;
if (!clonedIndices.back()->EvaluateAsInt(res, m_Context,
AllowSideEffects)) {
diffExpr =
BuildParens(BuildOp(BO_EQ, clonedIndices.back(),
ConstantFolder::synthesizeLiteral(
Expand Down Expand Up @@ -1726,8 +1728,9 @@ BaseForwardModeVisitor::DeriveSwitchStmtBodyHelper(const Stmt* stmt,
(newCaseSC->getLHS() ? Clone(newCaseSC->getLHS()) : nullptr);
Expr* rhsClone =
(newCaseSC->getRHS() ? Clone(newCaseSC->getRHS()) : nullptr);
newActiveSC = CaseStmt::Create(const_cast<ASTContext&>(m_Sema.getASTContext()), lhsClone, rhsClone, noLoc, noLoc, noLoc);

newActiveSC = CaseStmt::Create(m_Sema.getASTContext(), lhsClone, rhsClone,
noLoc, noLoc, noLoc);

} else if (isa<DefaultStmt>(SC)) {
newActiveSC =
new (m_Sema.getASTContext()) DefaultStmt(noLoc, noLoc, nullptr);
Expand Down Expand Up @@ -1866,20 +1869,20 @@ StmtDiff BaseForwardModeVisitor::VisitCXXTemporaryObjectExpr(
clonedArgs.push_back(argDiff.getExpr());
derivedArgs.push_back(argDiff.getExpr_dx());
}
Expr* clonedTOE = m_Sema
.ActOnCXXTypeConstructExpr(
OpaquePtr<QualType>::make(TOE->getType()),
utils::GetValidSLoc(m_Sema), clonedArgs,
utils::GetValidSLoc(m_Sema),
TOE->isListInitialization())
.get();
Expr* derivedTOE = m_Sema
.ActOnCXXTypeConstructExpr(
OpaquePtr<QualType>::make(TOE->getType()),
utils::GetValidSLoc(m_Sema), derivedArgs,
utils::GetValidSLoc(m_Sema),
TOE->isListInitialization())
.get();
Expr* clonedTOE =
m_Sema
.ActOnCXXTypeConstructExpr(OpaquePtr<QualType>::make(TOE->getType()),
utils::GetValidSLoc(m_Sema), clonedArgs,
utils::GetValidSLoc(m_Sema),
TOE->isListInitialization())
.get();
Expr* derivedTOE =
m_Sema
.ActOnCXXTypeConstructExpr(OpaquePtr<QualType>::make(TOE->getType()),
utils::GetValidSLoc(m_Sema), derivedArgs,
utils::GetValidSLoc(m_Sema),
TOE->isListInitialization())
.get();
return {clonedTOE, derivedTOE};
}

Expand Down Expand Up @@ -1993,4 +1996,4 @@ StmtDiff BaseForwardModeVisitor::VisitSubstNonTypeTemplateParmExpr(
const clang::SubstNonTypeTemplateParmExpr* NTTP) {
return Visit(NTTP->getReplacement());
}
} // end namespace clad
} // end namespace clad
Loading

0 comments on commit ef79ecf

Please sign in to comment.