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

Don't mark non-varied constant params #1148

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions include/clad/Differentiator/DerivativeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "clang/Sema/Sema.h"
#include "clad/Differentiator/DerivedFnCollector.h"
#include "clad/Differentiator/DiffPlanner.h"
#include "clad/Differentiator/DynamicGraph.h"

#include <array>
#include <stack>
Expand Down Expand Up @@ -72,6 +73,7 @@ namespace clad {
class DerivativeBuilder {
private:
friend class VisitorBase;
friend class DiffRequest;
friend class BaseForwardModeVisitor;
friend class PushForwardModeVisitor;
friend class VectorForwardModeVisitor;
Expand Down
23 changes: 19 additions & 4 deletions include/clad/Differentiator/DiffPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#include "clang/AST/RecursiveASTVisitor.h"
#include "llvm/ADT/SmallSet.h"
#include <iterator>
#include <set>
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: included header iterator is not used directly [misc-include-cleaner]

Suggested change
#include <set>
#include <set>

#include "clad/Differentiator/DerivativeBuilder.h"
#include "clad/Differentiator/DiffMode.h"
#include "clad/Differentiator/DynamicGraph.h"
#include "clad/Differentiator/ParseDiffArgsTypes.h"

#include <iterator>
#include <set>
namespace clang {
class CallExpr;
class CompilerInstance;
Expand All @@ -21,6 +22,7 @@ class Type;
} // namespace clang

namespace clad {
class DerivativeBuilder;

/// A struct containing information about request to differentiate a function.
struct DiffRequest {
Expand All @@ -34,11 +36,13 @@ struct DiffRequest {
} m_TbrRunInfo;

mutable struct ActivityRunInfo {
std::set<const clang::VarDecl*> ToBeRecorded;
std::set<const clang::VarDecl*> VariedDecls;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clang::VarDecl" is directly included [misc-include-cleaner]

include/clad/Differentiator/DiffPlanner.h:5:

- #include <iterator>
+ #include <clang/AST/Decl.h>
+ #include <iterator>

bool HasAnalysisRun = false;
} m_ActivityRunInfo;

public:
const DerivativeBuilder* Builder = nullptr;
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 'Builder' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  const DerivativeBuilder* Builder = nullptr;
                           ^

// static std::set<const clang::VarDecl*> AllVariedDecls;
/// Function to be differentiated.
const clang::FunctionDecl* Function = nullptr;
/// Name of the base function to be differentiated. Can be different from
Expand Down Expand Up @@ -127,7 +131,8 @@ struct DiffRequest {
Mode == other.Mode && EnableTBRAnalysis == other.EnableTBRAnalysis &&
EnableVariedAnalysis == other.EnableVariedAnalysis &&
DVI == other.DVI && use_enzyme == other.use_enzyme &&
DeclarationOnly == other.DeclarationOnly;
DeclarationOnly == other.DeclarationOnly &&
getVariedDecls() == other.getVariedDecls();
}

const clang::FunctionDecl* operator->() const { return Function; }
Expand All @@ -144,6 +149,16 @@ struct DiffRequest {

bool shouldBeRecorded(clang::Expr* E) const;
bool shouldHaveAdjoint(const clang::VarDecl* VD) const;

void setVariedDecls(std::set<const clang::VarDecl*> init) {
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: the parameter 'init' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]

Suggested change
void setVariedDecls(std::set<const clang::VarDecl*> init) {
void setVariedDecls(const std::set<const clang::VarDecl*>& init) {

for (auto* vd : init)
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: 'auto *vd' can be declared as 'const auto *vd' [readability-qualified-auto]

Suggested change
for (auto* vd : init)
for (const auto* vd : init)

this->m_ActivityRunInfo.VariedDecls.insert(vd);
}
std::set<const clang::VarDecl*> getVariedDecls() const {
return this->m_ActivityRunInfo.VariedDecls;
}
DiffRequest() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: use '= default' to define a trivial default constructor [modernize-use-equals-default]

Suggested change
DiffRequest() {}
DiffRequest() = default;

DiffRequest(DerivativeBuilder& builder) : Builder(&builder) {}
};

using DiffInterval = std::vector<clang::SourceRange>;
Expand Down
45 changes: 36 additions & 9 deletions lib/Differentiator/ActivityAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,56 @@ bool VariedAnalyzer::VisitCallExpr(CallExpr* CE) {
FunctionDecl* FD = CE->getDirectCallee();
bool noHiddenParam = (CE->getNumArgs() == FD->getNumParams());
if (noHiddenParam) {
bool restoreMarking = m_Marking;
bool restoreVaried = m_Varied;
MutableArrayRef<ParmVarDecl*> FDparam = FD->parameters();
for (std::size_t i = 0, e = CE->getNumArgs(); i != e; ++i) {
clang::Expr* par = CE->getArg(i);

QualType parType = FDparam[i]->getType();
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clang::QualType" is directly included [misc-include-cleaner]

lib/Differentiator/ActivityAnalyzer.cpp:1:

+ #include <clang/AST/Type.h>

QualType innermostType = parType;
while (innermostType->isPointerType())
innermostType = innermostType->getPointeeType();

m_Varied = false;
m_Marking = false;
TraverseStmt(par);
m_VariedDecls.insert(FDparam[i]);
if (m_Varied)
m_VariedDecls.insert(FDparam[i]);
else if ((parType->isReferenceType() ||
(utils::isArrayOrPointerType(parType) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clad::utils::isArrayOrPointerType" is directly included [misc-include-cleaner]

lib/Differentiator/ActivityAnalyzer.cpp:1:

+ #include <clad/Differentiator/CladUtils.h>

!innermostType.isConstQualified()))) {
m_Varied = true;
m_Marking = true;
TraverseStmt(par);
m_VariedDecls.insert(FDparam[i]);
}
}
m_Varied = restoreVaried;
m_Marking = restoreMarking;
}

return true;
}

bool VariedAnalyzer::VisitDeclStmt(DeclStmt* DS) {
for (Decl* D : DS->decls()) {

QualType VDTy = cast<VarDecl>(D)->getType();
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clang::cast" is directly included [misc-include-cleaner]

    QualType VDTy = cast<VarDecl>(D)->getType();
                    ^

QualType innermost = VDTy;
while (innermost->isPointerType())
innermost = innermost->getPointeeType();
if (VDTy->isArrayType() ||
(VDTy->isPointerType() && !innermost.isConstQualified())) {
copyVarToCurBlock(cast<VarDecl>(D));
m_Varied = true;
}
ovdiiuv marked this conversation as resolved.
Show resolved Hide resolved

if (Expr* init = cast<VarDecl>(D)->getInit()) {
m_Varied = false;
TraverseStmt(init);
m_Marking = true;
QualType VDTy = cast<VarDecl>(D)->getType();
if (m_Varied || utils::isArrayOrPointerType(VDTy))
if (m_Varied)
copyVarToCurBlock(cast<VarDecl>(D));
m_Marking = false;
}
Expand All @@ -147,12 +179,7 @@ bool VariedAnalyzer::VisitDeclStmt(DeclStmt* DS) {
}

bool VariedAnalyzer::VisitUnaryOperator(UnaryOperator* UnOp) {
const auto opCode = UnOp->getOpcode();
Expr* E = UnOp->getSubExpr();
if (opCode == UO_AddrOf || opCode == UO_Deref) {
m_Varied = true;
m_Marking = true;
}
TraverseStmt(E);
m_Marking = false;
return true;
Expand All @@ -163,7 +190,7 @@ bool VariedAnalyzer::VisitDeclRefExpr(DeclRefExpr* DRE) {
if (!VD)
return true;

if (isVaried(VD))
if (isVaried(VD) || VD->getType()->isArrayType())
m_Varied = true;

if (m_Varied && m_Marking)
Expand Down
Loading
Loading