14#include "mlir/IR/IRMapping.h"
15#include "mlir/Interfaces/FunctionInterfaces.h"
67 const SmallPtrSetImpl<mlir::Value> &constantvalues_,
68 const SmallPtrSetImpl<mlir::Value> &activevals_,
69 ArrayRef<DIFFE_TYPE> ReturnActivities,
70 ArrayRef<DIFFE_TYPE> ArgDiffeTypes_,
71 IRMapping &originalToNewFn_,
72 std::map<Operation *, Operation *> &originalToNewFnOps_,
76 void erase(Operation *op) { op->erase(); }
78 for (
auto &&[res, rep] : llvm::zip(op->getResults(), vals)) {
82 newOp->replaceAllUsesWith(vals);
113 void setDiffe(mlir::Value origv, mlir::Value newv, mlir::OpBuilder &builder);
117 auto iface = cast<AutoDiffTypeInterface>(T);
118 return iface.getShadowType(
width);
121 static llvm::SmallVector<mlir::Value, 1>
123 mlir::OperandRange range) {
124 llvm::SmallVector<mlir::Value, 1> results;
125 for (
size_t i = 0; i < range.size(); i++) {
126 results.push_back(vals[range.getBeginOperandIndex() + i]);
146 void setDiffe(mlir::Value origv, mlir::Value newv, mlir::OpBuilder &builder);
148 void zeroDiffe(mlir::Value origv, mlir::OpBuilder &builder);
150 mlir::Value
diffe(mlir::Value origv, mlir::OpBuilder &builder);
157 const SmallPtrSetImpl<mlir::Value> &constantvalues_,
158 const SmallPtrSetImpl<mlir::Value> &activevals_,
159 ArrayRef<DIFFE_TYPE> RetActivity,
160 ArrayRef<DIFFE_TYPE> ArgActivity, IRMapping &origToNew_,
161 std::map<Operation *, Operation *> &origToNewOps_,
167 activevals_, RetActivity, ArgActivity, origToNew_,
178 ArrayRef<DIFFE_TYPE> RetActivity, ArrayRef<DIFFE_TYPE> ArgActivity,
194 llvm_unreachable(
"invalid DerivativeMode: ReverseModePrimal\n");
198 prefix += std::to_string(
width);
200 IRMapping originalToNew;
201 std::map<Operation *, Operation *> originalToNewOps;
203 SmallPtrSet<mlir::Value, 1> returnvals;
204 SmallPtrSet<mlir::Value, 1> constant_values;
205 SmallPtrSet<mlir::Value, 1> nonconstant_values;
210 RetActivity, prefix + todiff.getName(), originalToNew, originalToNewOps,
215 returnShadows, constant_values, nonconstant_values, RetActivity,
216 ArgActivity, originalToNew, originalToNewOps,
mode,
width,
omp,
FunctionOpInterface CloneFunctionWithReturns(DerivativeMode mode, unsigned width, FunctionOpInterface F, IRMapping &ptrInputs, ArrayRef< DIFFE_TYPE > ArgActivity, SmallPtrSetImpl< mlir::Value > &constants, SmallPtrSetImpl< mlir::Value > &nonconstants, SmallPtrSetImpl< mlir::Value > &returnvals, const std::vector< bool > &returnPrimals, const std::vector< bool > &returnShadows, ArrayRef< DIFFE_TYPE > RetActivity, Twine name, IRMapping &VMap, std::map< Operation *, Operation * > &OpMap, mlir::Type additionalArg)
DIFFE_TYPE
Potential differentiable argument classifications.
SmallVector< std::function< Value(Location, Type)> > gradientCreatorHook
void registerGradientCreatorHook(std::function< Value(Location, Type)> hook)
mlir::Value getDifferential(mlir::Value origv)
Block * initializationBlock
void setDiffe(mlir::Value origv, mlir::Value newv, mlir::OpBuilder &builder)
void deregisterGradientCreatorHook(std::function< Value(Location, Type)> hook)
static MDiffeGradientUtils * CreateFromClone(MEnzymeLogic &Logic, DerivativeMode mode, unsigned width, FunctionOpInterface todiff, MTypeAnalysis &TA, MFnTypeInfo &oldTypeInfo, const llvm::ArrayRef< bool > returnPrimals, const llvm::ArrayRef< bool > returnShadows, ArrayRef< DIFFE_TYPE > RetActivity, ArrayRef< DIFFE_TYPE > ArgActivity, mlir::Type additionalArg, bool omp, llvm::StringRef postpasses, bool verifyPostPasses, bool strongZero)
Value getNewGradient(Location loc, Type t)
mlir::Value diffe(mlir::Value origv, mlir::OpBuilder &builder)
void zeroDiffe(mlir::Value origv, mlir::OpBuilder &builder)
MDiffeGradientUtils(MEnzymeLogic &Logic, FunctionOpInterface newFunc_, FunctionOpInterface oldFunc_, MTypeAnalysis &TA, MTypeResults TR, IRMapping &invertedPointers_, const llvm::ArrayRef< bool > returnPrimals, const llvm::ArrayRef< bool > returnShadows, const SmallPtrSetImpl< mlir::Value > &constantvalues_, const SmallPtrSetImpl< mlir::Value > &activevals_, ArrayRef< DIFFE_TYPE > RetActivity, ArrayRef< DIFFE_TYPE > ArgActivity, IRMapping &origToNew_, std::map< Operation *, Operation * > &origToNewOps_, DerivativeMode mode, unsigned width, bool omp, llvm::StringRef postpasses, bool verifyPostPasses, bool strongZero)
bool primalStoreElidable(mlir::Value ptr)
True when a primal store through this pointer may be omitted: declaring the base enzyme_dupnoneed is ...
Operation * cloneWithNewOperands(OpBuilder &B, Operation *op)
IRMapping originalToNewFn
ArrayRef< DIFFE_TYPE > RetDiffeTypes
static llvm::SmallVector< mlir::Value, 1 > reindex_arguments(llvm::ArrayRef< mlir::Value > vals, mlir::OperandRange range)
llvm::StringRef postpasses
std::unique_ptr< DataFlowSolver > dataflowSolver
const llvm::ArrayRef< bool > returnShadows
mlir::Type getShadowType(mlir::Type T)
const llvm::ArrayRef< bool > returnPrimals
FunctionOpInterface oldFunc
IRMapping invertedPointers
void replaceOrigOpWith(Operation *op, ValueRange vals)
std::map< Operation *, Operation * > originalToNewFnOps
DenseMap< Operation *, bool > readOnlyCache
ArrayRef< DIFFE_TYPE > ArgDiffeTypes
LogicalResult visitChild(Operation *op)
void erase(Operation *op)
DIFFE_TYPE getDiffeTypeOfBase(mlir::Value ptr)
The activity the caller declared for the memory this pointer views: the base's entry in ArgDiffeTypes...
void setDiffe(mlir::Value origv, mlir::Value newv, mlir::OpBuilder &builder)
mlir::Value invertPointerM(mlir::Value v, OpBuilder &Builder2)
void eraseIfUnused(Operation *op, bool erase=true, bool check=true)
SmallVector< mlir::Value, 1 > getNewFromOriginal(ValueRange originst) const
void forceAugmentedReturns()
std::unique_ptr< enzyme::DataFlowActivityAnalyzer > dataflowActivityAnalyzer
MGradientUtils(MEnzymeLogic &Logic, FunctionOpInterface newFunc_, FunctionOpInterface oldFunc_, MTypeAnalysis &TA_, MTypeResults TR_, IRMapping &invertedPointers_, const llvm::ArrayRef< bool > returnPrimals, const llvm::ArrayRef< bool > returnShadows, const SmallPtrSetImpl< mlir::Value > &constantvalues_, const SmallPtrSetImpl< mlir::Value > &activevals_, ArrayRef< DIFFE_TYPE > ReturnActivities, ArrayRef< DIFFE_TYPE > ArgDiffeTypes_, IRMapping &originalToNewFn_, std::map< Operation *, Operation * > &originalToNewFnOps_, DerivativeMode mode, unsigned width, bool omp, llvm::StringRef postpasses, bool verifyPostPasses, bool strongZero)
std::unique_ptr< enzyme::ActivityAnalyzer > activityAnalyzer
FunctionOpInterface newFunc
bool isConstantInstruction(mlir::Operation *v) const
void setInvertedPointer(mlir::Value origv, mlir::Value newv)
bool isConstantValue(mlir::Value v) const
SmallPtrSet< Block *, 4 > blocksNotForAnalysis
static mlir::Value getBaseObject(mlir::Value v)
The base allocation a pointer-like value views: view-like ops and LLVM geps and casts peeled away,...