Skip to content

Commit 60b04a8

Browse files
committed
add comments
1 parent 7eaa0cc commit 60b04a8

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

enzyme/Enzyme/FunctionUtils.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ Function* preprocessForClone(Function *F, AAResults &AA, TargetLibraryInfo &TLI)
398398

399399
}
400400
}
401-
401+
402+
//! Loop rotation now necessary to ensure that the condition of a loop is at the end
402403
{
403404
FunctionAnalysisManager AM;
404405
AM.registerPass([] { return AAManager(); });
@@ -410,29 +411,27 @@ Function* preprocessForClone(Function *F, AAResults &AA, TargetLibraryInfo &TLI)
410411
AM.registerPass([] { return MemorySSAAnalysis(); });
411412
AM.registerPass([] { return DominatorTreeAnalysis(); });
412413
AM.registerPass([] { return MemoryDependenceAnalysis(); });
413-
#if LLVM_VERSION_MAJOR > 6
414+
#if LLVM_VERSION_MAJOR > 6
414415
AM.registerPass([] { return PhiValuesAnalysis(); });
415-
#endif
416-
#if LLVM_VERSION_MAJOR >= 8
417-
AM.registerPass([] { return PassInstrumentationAnalysis(); });
418-
#endif
416+
#endif
417+
#if LLVM_VERSION_MAJOR >= 8
418+
AM.registerPass([] { return PassInstrumentationAnalysis(); });
419+
#endif
419420

420-
{
421+
{
421422

422423
LoopAnalysisManager LAM;
423424
AM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
424425
LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(AM); });
425426

426-
{
427-
//Loop rotation is necessary to ensure we are of the form body then conditional
428-
createFunctionToLoopPassAdaptor(LoopRotatePass()).run(*NewF, AM);
429-
}
427+
{
428+
//Loop rotation is necessary to ensure we are of the form body then conditional
429+
createFunctionToLoopPassAdaptor(LoopRotatePass()).run(*NewF, AM);
430+
}
430431
LAM.clear();
431-
}
432-
AM.clear();
433-
// Ensure there is only one exit block
434-
//LoopSimplifyPass().run(*NewF, AM);
435-
}
432+
}
433+
AM.clear();
434+
}
436435

437436
{
438437
FunctionAnalysisManager AM;

enzyme/Enzyme/GradientUtils.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <algorithm>
3030

31+
//! Is possibleChild a child loop or the same loop as possibleParent
3132
static bool isParentOrSameContext(LoopContext & possibleChild, LoopContext & possibleParent) {
3233
if (possibleChild.header == possibleParent.header) return true;
3334

@@ -37,9 +38,10 @@ static bool isParentOrSameContext(LoopContext & possibleChild, LoopContext & pos
3738
return false;
3839
}
3940

40-
//BB is a predecessor of branchingBlock
41+
//! Given an edge from BB to branchingBlock get the corresponding block to branch to in the reverse pass
4142
BasicBlock* GradientUtils::getReverseOrLatchMerge(BasicBlock* BB, BasicBlock* branchingBlock) {
4243
assert(BB);
44+
// BB should be a forward pass block, assert that
4345
if (reverseBlocks.find(BB) == reverseBlocks.end()) {
4446
llvm::errs() << *oldFunc << "\n";
4547
llvm::errs() << *newFunc << "\n";
@@ -812,6 +814,11 @@ bool GradientUtils::getContext(BasicBlock* BB, LoopContext& loopContext) {
812814
return getContextM(BB, loopContext, this->loopContexts, this->LI, this->SE, this->DT, *this);
813815
}
814816

817+
//! Given a map of edges we could have taken to desired target, compute a value that determines which target should be branched to
818+
// This function attempts to determine an equivalent condition from earlier in the code and use that if possible, falling back to creating a phi node of which edge was taken if necessary
819+
// This function can be used in two ways:
820+
// * If replacePHIs is null (usual case), this function does the branch
821+
// * If replacePHIs isn't null, do not perform the branch and instead replace the PHI's with the derived condition as to whether we should branch to a particular target
815822
void GradientUtils::branchToCorrespondingTarget(BasicBlock* ctx, IRBuilder <>& BuilderM, const std::map<BasicBlock*, std::vector<std::pair</*pred*/BasicBlock*,/*successor*/BasicBlock*>>> &targetToPreds, const std::map<BasicBlock*,PHINode*>* replacePHIs) {
816823
if (replacePHIs) {
817824
if (replacePHIs->size() == 0) return;
@@ -1068,4 +1075,4 @@ void GradientUtils::branchToCorrespondingTarget(BasicBlock* ctx, IRBuilder <>& B
10681075
}
10691076
return;
10701077

1071-
}
1078+
}

enzyme/Enzyme/GradientUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,8 @@ class GradientUtils {
808808
return nullptr;
809809
report_fatal_error("unable to unwrap");
810810
}
811-
811+
812+
//! Caching mechanism: creates a cache of type T in a scope given by ctx (where if ctx is in a loop there will be a corresponding number of slots)
812813
AllocaInst* createCacheForScope(BasicBlock* ctx, Type* T, StringRef name, CallInst** freeLocation, Instruction** lastScopeAllocLocation) {
813814
assert(ctx);
814815
assert(T);

0 commit comments

Comments
 (0)