-
Notifications
You must be signed in to change notification settings - Fork 123
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
[WIP] Add Useful Analysis to the forward mode #1120
Open
ovdiiuv
wants to merge
1
commit into
vgvassilev:master
Choose a base branch
from
ovdiiuv:useful
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1063,7 +1063,14 @@ | |||||
// If DRE is of type pointer, then the derivative is a null pointer. | ||||||
if (clonedDRE->getType()->isPointerType()) | ||||||
return StmtDiff(clonedDRE, nullptr); | ||||||
|
||||||
if (auto* i = dyn_cast<VarDecl>(DRE->getDecl())) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: 'auto *i' can be declared as 'const auto *i' [readability-qualified-auto]
Suggested change
|
||||||
if (!m_DiffReq.shouldHaveAdjointForw(i)) | ||||||
return StmtDiff(clonedDRE, nullptr); | ||||||
} | ||||||
|
||||||
QualType literalTy = utils::GetValueType(clonedDRE->getType()); | ||||||
|
||||||
return StmtDiff(clonedDRE, ConstantFolder::synthesizeLiteral( | ||||||
literalTy, m_Context, /*val=*/0)); | ||||||
} | ||||||
|
@@ -1208,6 +1215,7 @@ | |||||
} | ||||||
|
||||||
llvm::SmallVector<Expr*, 16> pushforwardFnArgs; | ||||||
// | ||||||
pushforwardFnArgs.insert(pushforwardFnArgs.end(), CallArgs.begin(), | ||||||
CallArgs.end()); | ||||||
pushforwardFnArgs.insert(pushforwardFnArgs.end(), diffArgs.begin(), | ||||||
|
@@ -1284,6 +1292,7 @@ | |||||
pushforwardFnRequest.BaseFunctionName = utils::ComputeEffectiveFnName(FD); | ||||||
// Silence diag outputs in nested derivation process. | ||||||
pushforwardFnRequest.VerboseDiags = false; | ||||||
pushforwardFnRequest.EnableUsefulAnalysis = m_DiffReq.EnableUsefulAnalysis; | ||||||
|
||||||
// Check if request already derived in DerivedFunctions. | ||||||
FunctionDecl* pushforwardFD = | ||||||
|
@@ -1446,7 +1455,8 @@ | |||||
derivedR = BuildParens(derivedR); | ||||||
opDiff = BuildOp(opCode, derivedL, derivedR); | ||||||
} else if (BinOp->isAssignmentOp()) { | ||||||
if (Ldiff.getExpr_dx()->isModifiableLvalue(m_Context) != Expr::MLV_Valid) { | ||||||
if (Ldiff.getExpr_dx() && | ||||||
Ldiff.getExpr_dx()->isModifiableLvalue(m_Context) != Expr::MLV_Valid) { | ||||||
diag(DiagnosticsEngine::Warning, BinOp->getEndLoc(), | ||||||
"derivative of an assignment attempts to assign to unassignable " | ||||||
"expr, assignment ignored"); | ||||||
|
@@ -1575,11 +1585,16 @@ | |||||
VarDecl* VDDerived = | ||||||
BuildVarDecl(VD->getType(), "_d_" + VD->getNameAsString(), initDx, | ||||||
VD->isDirectInit(), /*TSI=*/nullptr, VD->getInitStyle()); | ||||||
m_Variables.emplace(VDClone, BuildDeclRef(VDDerived)); | ||||||
|
||||||
if (!m_DiffReq.shouldHaveAdjointForw(VD)) | ||||||
VDDerived = nullptr; | ||||||
else | ||||||
m_Variables.emplace(VDClone, BuildDeclRef(VDDerived)); | ||||||
return DeclDiff<VarDecl>(VDClone, VDDerived); | ||||||
} | ||||||
|
||||||
StmtDiff BaseForwardModeVisitor::VisitDeclStmt(const DeclStmt* DS) { | ||||||
// llvm::errs() << "\nVisitDeclStmt"; | ||||||
llvm::SmallVector<Decl*, 4> decls; | ||||||
llvm::SmallVector<Decl*, 4> declsDiff; | ||||||
// If the type is marked as non_differentiable, skip generating its derivative | ||||||
|
@@ -1642,7 +1657,8 @@ | |||||
if (VDDiff.getDecl()->getDeclName() != VD->getDeclName()) | ||||||
m_DeclReplacements[VD] = VDDiff.getDecl(); | ||||||
decls.push_back(VDDiff.getDecl()); | ||||||
declsDiff.push_back(VDDiff.getDecl_dx()); | ||||||
if (m_DiffReq.shouldHaveAdjointForw(VD)) | ||||||
declsDiff.push_back(VDDiff.getDecl_dx()); | ||||||
} else if (auto* SAD = dyn_cast<StaticAssertDecl>(D)) { | ||||||
DeclDiff<StaticAssertDecl> SADDiff = DifferentiateStaticAssertDecl(SAD); | ||||||
if (SADDiff.getDecl()) | ||||||
|
@@ -1661,6 +1677,8 @@ | |||||
DSClone = BuildDeclStmt(decls); | ||||||
if (!declsDiff.empty()) | ||||||
DSDiff = BuildDeclStmt(declsDiff); | ||||||
// llvm::errs() << "\n====="; | ||||||
// DSDiff->dump(); | ||||||
return StmtDiff(DSClone, DSDiff); | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#include "UsefulAnalyzer.h" | ||
|
||
using namespace clang; | ||
|
||
namespace clad { | ||
|
||
void UsefulAnalyzer::Analyze(const FunctionDecl* FD) { | ||
// Build the CFG (control-flow graph) of FD. | ||
clang::CFG::BuildOptions Options; | ||
m_CFG = clang::CFG::buildCFG(FD, FD->getBody(), &m_Context, Options); | ||
|
||
m_BlockData.resize(m_CFG->size()); | ||
// Set current block ID to the ID of entry the block. | ||
CFGBlock* exit = &m_CFG->getExit(); | ||
m_CurBlockID = exit->getBlockID(); | ||
m_BlockData[m_CurBlockID] = createNewVarsData({}); | ||
for (const VarDecl* i : m_UsefulDecls) | ||
m_BlockData[m_CurBlockID]->insert(i); | ||
// Add the entry block to the queue. | ||
m_CFGQueue.insert(m_CurBlockID); | ||
|
||
// Visit CFG blocks in the queue until it's empty. | ||
while (!m_CFGQueue.empty()) { | ||
auto IDIter = m_CFGQueue.begin(); | ||
m_CurBlockID = *IDIter; | ||
m_CFGQueue.erase(IDIter); | ||
CFGBlock& nextBlock = *getCFGBlockByID(m_CurBlockID); | ||
AnalyzeCFGBlock(nextBlock); | ||
} | ||
} | ||
|
||
CFGBlock* UsefulAnalyzer::getCFGBlockByID(unsigned ID) { | ||
return *(m_CFG->begin() + ID); | ||
} | ||
|
||
bool UsefulAnalyzer::isUseful(const VarDecl* VD) const { | ||
const VarsData& curBranch = getCurBlockVarsData(); | ||
return curBranch.find(VD) != curBranch.end(); | ||
} | ||
|
||
void UsefulAnalyzer::copyVarToCurBlock(const clang::VarDecl* VD) { | ||
VarsData& curBranch = getCurBlockVarsData(); | ||
curBranch.insert(VD); | ||
} | ||
|
||
static void mergeVarsData(std::set<const clang::VarDecl*>* targetData, | ||
std::set<const clang::VarDecl*>* mergeData) { | ||
for (const clang::VarDecl* i : *mergeData) | ||
targetData->insert(i); | ||
*mergeData = *targetData; | ||
} | ||
|
||
void UsefulAnalyzer::AnalyzeCFGBlock(const CFGBlock& block) { | ||
|
||
for (auto ib = block.end(); ib != block.begin() - 1; ib--) { | ||
if (ib->getKind() == clang::CFGElement::Statement) { | ||
|
||
const clang::Stmt* S = ib->castAs<clang::CFGStmt>().getStmt(); | ||
// The const_cast is inevitable, since there is no | ||
// ConstRecusiveASTVisitor. | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) | ||
TraverseStmt(const_cast<clang::Stmt*>(S)); | ||
} | ||
} | ||
|
||
for (const clang::CFGBlock::AdjacentBlock pred : block.preds()) { | ||
if (!pred) | ||
continue; | ||
auto& predData = m_BlockData[pred->getBlockID()]; | ||
if (!predData) | ||
predData = createNewVarsData(*m_BlockData[block.getBlockID()]); | ||
|
||
bool shouldPushPred = true; | ||
if (pred->getBlockID() < block.getBlockID()) { | ||
if (m_LoopMem == *m_BlockData[block.getBlockID()]) | ||
shouldPushPred = false; | ||
|
||
for (const VarDecl* i : *m_BlockData[block.getBlockID()]) | ||
m_LoopMem.insert(i); | ||
} | ||
|
||
if (shouldPushPred) | ||
m_CFGQueue.insert(pred->getBlockID()); | ||
|
||
mergeVarsData(predData.get(), m_BlockData[block.getBlockID()].get()); | ||
} | ||
|
||
for (const VarDecl* i : *m_BlockData[block.getBlockID()]) | ||
m_UsefulDecls.insert(i); | ||
} | ||
|
||
bool UsefulAnalyzer::VisitBinaryOperator(BinaryOperator* BinOp) { | ||
Expr* L = BinOp->getLHS(); | ||
Expr* R = BinOp->getRHS(); | ||
const auto opCode = BinOp->getOpcode(); | ||
if (BinOp->isAssignmentOp()) { | ||
m_Useful = false; | ||
TraverseStmt(L); | ||
m_Marking = m_Useful; | ||
TraverseStmt(R); | ||
m_Marking = false; | ||
} else if (opCode == BO_Add || opCode == BO_Sub || opCode == BO_Mul || | ||
opCode == BO_Div) { | ||
for (auto* subexpr : BinOp->children()) | ||
if (!isa<BinaryOperator>(subexpr)) | ||
TraverseStmt(subexpr); | ||
} | ||
return true; | ||
} | ||
|
||
bool UsefulAnalyzer::VisitDeclStmt(DeclStmt* DS) { | ||
for (Decl* D : DS->decls()) { | ||
if (auto* VD = dyn_cast<VarDecl>(D)) { | ||
if (isUseful(VD)) { | ||
m_Useful = true; | ||
m_Marking = true; | ||
} | ||
if (Expr* init = cast<VarDecl>(D)->getInit()) | ||
TraverseStmt(init); | ||
m_Marking = false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool UsefulAnalyzer::VisitReturnStmt(ReturnStmt* RS) { | ||
m_Useful = true; | ||
m_Marking = true; | ||
auto* rv = RS->getRetValue(); | ||
TraverseStmt(rv); | ||
return true; | ||
} | ||
|
||
bool UsefulAnalyzer::VisitCallExpr(CallExpr* CE) { | ||
if (m_Useful) | ||
return true; | ||
FunctionDecl* FD = CE->getDirectCallee(); | ||
m_UsefulFuncs.insert(FD); | ||
return true; | ||
} | ||
|
||
bool UsefulAnalyzer::VisitDeclRefExpr(DeclRefExpr* DRE) { | ||
auto* VD = dyn_cast<VarDecl>(DRE->getDecl()); | ||
if (!VD) | ||
return true; | ||
|
||
if (isUseful(VD)) | ||
m_Useful = true; | ||
|
||
if (m_Useful && m_Marking) | ||
copyVarToCurBlock(VD); | ||
|
||
return true; | ||
} | ||
|
||
} // namespace clad |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: member variable 'ReqAdj' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes]