Skip to content
Open
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
11 changes: 11 additions & 0 deletions include/clad/Differentiator/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "clang/Basic/OperatorKinds.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Sema/DeclSpec.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
Expand All @@ -34,6 +35,7 @@
#include <cstddef>
#include <stack>
#include <unordered_map>
#include <vector>

namespace clang {
class NestedNameSpecifier;
Expand Down Expand Up @@ -151,6 +153,15 @@ namespace clad {
// expression to be of object type in the reverse mode as well.
clang::Expr* m_ThisExprDerivative = nullptr;

private:
/// Per-instance cache for tape lookup results. These are tied to the
/// current Sema/ASTContext and must not be shared across translation units.
clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup;
clad_compat::llvm_Optional<clang::LookupResult> m_TapePopLookup;
clad_compat::llvm_Optional<clang::LookupResult> m_TapeBackLookup;
clad_compat::llvm_Optional<clang::LookupResult> m_TapeZeroInitLookup;

protected:
/// The currently visited statement. Useful for crash pretty-printing.
const clang::Stmt* m_CurVisitedStmt = nullptr;

Expand Down
29 changes: 13 additions & 16 deletions lib/Differentiator/VisitorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,9 @@ namespace clad {
}

LookupResult& VisitorBase::GetCladTapePush() {
static clad_compat::llvm_Optional<LookupResult> Result{};
if (!Result)
Result = LookupCladTapeMethod("push");
return clad_compat::llvm_Optional_GetValue(Result);
if (!m_TapePushLookup)
m_TapePushLookup = LookupCladTapeMethod("push");
return clad_compat::llvm_Optional_GetValue(m_TapePushLookup);
}

Expr* VisitorBase::GetFunctionCall(const std::string& funcName,
Expand Down Expand Up @@ -562,17 +561,15 @@ namespace clad {
}

LookupResult& VisitorBase::GetCladTapePop() {
static clad_compat::llvm_Optional<LookupResult> Result{};
if (!Result)
Result = LookupCladTapeMethod("pop");
return clad_compat::llvm_Optional_GetValue(Result);
if (!m_TapePopLookup)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]

Suggested change
if (!m_TapePopLookup)
auto* pushDRE = m_Sema.BuildDeclarationNameExpr(CSS, pushLR, false)

m_TapePopLookup = LookupCladTapeMethod("pop");
return clad_compat::llvm_Optional_GetValue(m_TapePopLookup);
}

LookupResult& VisitorBase::GetCladTapeBack() {
static clad_compat::llvm_Optional<LookupResult> Result{};
if (!Result)
Result = LookupCladTapeMethod("back");
return clad_compat::llvm_Optional_GetValue(Result);
if (!m_TapeBackLookup)
m_TapeBackLookup = LookupCladTapeMethod("back");
return clad_compat::llvm_Optional_GetValue(m_TapeBackLookup);
}

QualType VisitorBase::GetCladTapeOfType(QualType T) {
Expand Down Expand Up @@ -832,10 +829,10 @@ namespace clad {
}

Stmt* VisitorBase::GetCladZeroInit(llvm::MutableArrayRef<Expr*> args) {
static clad_compat::llvm_Optional<LookupResult> Result{};
if (!Result)
Result = LookupCladTapeMethod("zero_init");
LookupResult& init = clad_compat::llvm_Optional_GetValue(Result);
if (!m_TapeZeroInitLookup)
m_TapeZeroInitLookup = LookupCladTapeMethod("zero_init");
LookupResult& init =
clad_compat::llvm_Optional_GetValue(m_TapeZeroInitLookup);
CXXScopeSpec CSS;
CSS.Extend(m_Context, utils::GetCladNamespace(m_Sema), noLoc, noLoc);
auto* pushDRE =
Expand Down
Loading