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

Initial support for memory operations in reverse mode #777

Merged
merged 6 commits into from
Feb 29, 2024
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
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Checks: >
-readability-function-cognitive-complexity,
-readability-implicit-bool-conversion,
-cppcoreguidelines-avoid-magic-numbers,
-clang-analyzer-cplusplus.NewDeleteLeaks,

CheckOptions:
- key: readability-identifier-naming.ClassCase
Expand Down
2 changes: 2 additions & 0 deletions include/clad/Differentiator/BaseForwardModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class BaseForwardModeVisitor
StmtDiff VisitPseudoObjectExpr(const clang::PseudoObjectExpr* POE);
StmtDiff VisitSubstNonTypeTemplateParmExpr(
const clang::SubstNonTypeTemplateParmExpr* NTTP);
StmtDiff VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr* IVIE);
StmtDiff VisitCStyleCastExpr(const clang::CStyleCastExpr* CSCE);

virtual clang::QualType
GetPushForwardDerivativeType(clang::QualType ParamType);
Expand Down
25 changes: 25 additions & 0 deletions include/clad/Differentiator/BuiltinDerivatives.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ CUDA_HOST_DEVICE void clamp_pullback(const T& v, const T& lo, const T& hi,
#endif

} // namespace std

// NOLINTBEGIN(cppcoreguidelines-no-malloc)
// NOLINTBEGIN(cppcoreguidelines-owning-memory)
inline ValueAndPushforward<void*, void*> malloc_pushforward(size_t sz,
size_t d_sz) {
return {malloc(sz), malloc(sz)};
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
}

inline ValueAndPushforward<void*, void*>
calloc_pushforward(size_t n, size_t sz, size_t d_n, size_t d_sz) {
return {calloc(n, sz), calloc(n, sz)};
}

inline ValueAndPushforward<void*, void*>
realloc_pushforward(void* ptr, size_t sz, void* d_ptr, size_t d_sz) {
vaithak marked this conversation as resolved.
Show resolved Hide resolved
return {realloc(ptr, sz), realloc(d_ptr, sz)};
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
}

inline void free_pushforward(void* ptr, void* d_ptr) {
free(ptr);
vaithak marked this conversation as resolved.
Show resolved Hide resolved
free(d_ptr);
vaithak marked this conversation as resolved.
Show resolved Hide resolved
}
// NOLINTEND(cppcoreguidelines-owning-memory)
// NOLINTEND(cppcoreguidelines-no-malloc)

// These are required because C variants of mathematical functions are
// defined in global namespace.
using std::abs_pushforward;
Expand Down
3 changes: 3 additions & 0 deletions include/clad/Differentiator/CladUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ namespace clad {
void SetSwitchCaseSubStmt(clang::SwitchCase* SC, clang::Stmt* subStmt);

bool IsLiteral(const clang::Expr* E);

bool IsMemoryFunction(const clang::FunctionDecl* FD);
bool IsMemoryDeallocationFunction(const clang::FunctionDecl* FD);
} // namespace utils
} // namespace clad

Expand Down
11 changes: 11 additions & 0 deletions include/clad/Differentiator/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,5 +766,16 @@ static inline const DeclSpec& Sema_ActOnStartOfLambdaDefinition_ScopeOrDeclSpec(
,Node->isTransparent()
#endif

// Clang 9 above added isa_and_nonnull.
#if CLANG_VERSION_MAJOR < 9
template <typename X, typename Y> bool isa_and_nonnull(const Y* Val) {
return Val && isa<X>(Val);
}
#else
template <typename X, typename Y> bool isa_and_nonnull(const Y* Val) {
return llvm::isa_and_nonnull<X>(Val);
}
#endif

} // namespace clad_compat
#endif //CLAD_COMPATIBILITY
7 changes: 7 additions & 0 deletions include/clad/Differentiator/ReverseModeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace clad {
/// the reverse mode we also accumulate Stmts for the reverse pass which
/// will be executed on return.
std::vector<Stmts> m_Reverse;
/// Storing expressions to delete/free memory in the reverse pass.
Stmts m_DeallocExprs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member variable 'm_DeallocExprs' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]

    Stmts m_DeallocExprs;
          ^

/// Stack is used to pass the arguments (dfdx) to further nodes
/// in the Visit method.
std::stack<clang::Expr*> m_Stack;
Expand Down Expand Up @@ -356,6 +358,9 @@ namespace clad {
StmtDiff VisitForStmt(const clang::ForStmt* FS);
StmtDiff VisitIfStmt(const clang::IfStmt* If);
StmtDiff VisitImplicitCastExpr(const clang::ImplicitCastExpr* ICE);
StmtDiff
VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr* IVIE);
StmtDiff VisitCStyleCastExpr(const clang::CStyleCastExpr* CSCE);
StmtDiff VisitInitListExpr(const clang::InitListExpr* ILE);
StmtDiff VisitIntegerLiteral(const clang::IntegerLiteral* IL);
StmtDiff VisitMemberExpr(const clang::MemberExpr* ME);
Expand All @@ -370,6 +375,8 @@ namespace clad {
StmtDiff VisitContinueStmt(const clang::ContinueStmt* CS);
StmtDiff VisitBreakStmt(const clang::BreakStmt* BS);
StmtDiff VisitCXXThisExpr(const clang::CXXThisExpr* CTE);
StmtDiff VisitCXXNewExpr(const clang::CXXNewExpr* CNE);
StmtDiff VisitCXXDeleteExpr(const clang::CXXDeleteExpr* CDE);
StmtDiff VisitCXXConstructExpr(const clang::CXXConstructExpr* CE);
StmtDiff
VisitMaterializeTemporaryExpr(const clang::MaterializeTemporaryExpr* MTE);
Expand Down
24 changes: 24 additions & 0 deletions lib/Differentiator/BaseForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,30 @@ BaseForwardModeVisitor::VisitImplicitCastExpr(const ImplicitCastExpr* ICE) {
return StmtDiff(subExprDiff.getExpr(), subExprDiff.getExpr_dx());
}

StmtDiff BaseForwardModeVisitor::VisitImplicitValueInitExpr(
const ImplicitValueInitExpr* E) {
return StmtDiff(Clone(E), Clone(E));
}

StmtDiff
BaseForwardModeVisitor::VisitCStyleCastExpr(const CStyleCastExpr* CSCE) {
StmtDiff subExprDiff = Visit(CSCE->getSubExpr());
// Create a new CStyleCastExpr with the same type and the same subexpression
// as the original one.
Expr* castExpr = m_Sema
.BuildCStyleCastExpr(
CSCE->getLParenLoc(), CSCE->getTypeInfoAsWritten(),
CSCE->getRParenLoc(), subExprDiff.getExpr())
.get();
Expr* castExprDiff =
m_Sema
.BuildCStyleCastExpr(CSCE->getLParenLoc(),
CSCE->getTypeInfoAsWritten(),
CSCE->getRParenLoc(), subExprDiff.getExpr_dx())
.get();
return StmtDiff(castExpr, castExprDiff);
}

StmtDiff
BaseForwardModeVisitor::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr* DE) {
// FIXME: Shouldn't we simply clone the CXXDefaultArgExpr?
Expand Down
39 changes: 39 additions & 0 deletions lib/Differentiator/CladUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Sema/Lookup.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -400,6 +401,12 @@
auto& C = semaRef.getASTContext();
if (!TSI)
TSI = C.getTrivialTypeSourceInfo(qType);
if (clad_compat::isa_and_nonnull<ImplicitValueInitExpr>(initializer))
// If the initializer is an implicit value init expression, then
// we don't need to pass it explicitly to the CXXNewExpr. As, clang
// internally adds it when initializer is ParenListExpr and
// DirectInitRange is valid.
initializer = semaRef.ActOnParenListExpr(noLoc, noLoc, {}).get();

Check warning on line 409 in lib/Differentiator/CladUtils.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Differentiator/CladUtils.cpp#L409

Added line #L409 was not covered by tests
auto newExpr =
semaRef
.BuildCXXNew(
Expand Down Expand Up @@ -641,5 +648,37 @@
isa<ObjCBoolLiteralExpr>(E) || isa<CXXBoolLiteralExpr>(E) ||
isa<GNUNullExpr>(E);
}

bool IsMemoryFunction(const clang::FunctionDecl* FD) {

#if CLANG_VERSION_MAJOR > 12
if (FD->getBuiltinID() == Builtin::BImalloc)
vaithak marked this conversation as resolved.
Show resolved Hide resolved
return true;
if (FD->getBuiltinID() == Builtin::ID::BIcalloc)
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
return true;
if (FD->getBuiltinID() == Builtin::ID::BIrealloc)
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
return true;
if (FD->getBuiltinID() == Builtin::ID::BImemset)
return true;
#else
if (FD->getNameAsString() == "malloc")
return true;
if (FD->getNameAsString() == "calloc")
return true;

Check warning on line 667 in lib/Differentiator/CladUtils.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Differentiator/CladUtils.cpp#L667

Added line #L667 was not covered by tests
if (FD->getNameAsString() == "realloc")
return true;
if (FD->getNameAsString() == "memset")
return true;
#endif
return false;
}

bool IsMemoryDeallocationFunction(const clang::FunctionDecl* FD) {
#if CLANG_VERSION_MAJOR > 12
return FD->getBuiltinID() == Builtin::ID::BIfree;
vaithak marked this conversation as resolved.
Show resolved Hide resolved
vaithak marked this conversation as resolved.
Show resolved Hide resolved
#else
return FD->getNameAsString() == "free";
#endif
}
} // namespace utils
} // namespace clad
Loading
Loading