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

Do not do replaceFunction on a pointer return type #27

Merged
merged 2 commits into from
Nov 9, 2019
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/julia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ jobs:
ENZYME_PATH: "../Enzyme/build/Enzyme"
- name: "Check Enzyme"
run: |
cd build
make check-enzyme
#cd build
#make check-enzyme
2 changes: 1 addition & 1 deletion enzyme/Enzyme/Enzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void HandleAutoDiff(CallInst *CI, TargetLibraryInfo &TLI, AAResults &AA) {//, Lo
bool differentialReturn = cast<Function>(fn)->getReturnType()->isFPOrFPVectorTy();

std::set<unsigned> volatile_args;
auto newFunc = CreatePrimalAndGradient(cast<Function>(fn), constants, TLI, AA, /*should return*/false, differentialReturn, /*topLevel*/true, /*addedType*/nullptr, volatile_args);//, LI, DT);
auto newFunc = CreatePrimalAndGradient(cast<Function>(fn), constants, TLI, AA, /*should return*/false, differentialReturn, /*dretPtr*/false, /*topLevel*/true, /*addedType*/nullptr, volatile_args);//, LI, DT);

if (differentialReturn)
args.push_back(ConstantFP::get(cast<Function>(fn)->getReturnType(), 1.0));
Expand Down
116 changes: 89 additions & 27 deletions enzyme/Enzyme/EnzymeLogic.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion enzyme/Enzyme/EnzymeLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ extern llvm::cl::opt<bool> enzyme_print;
//! return structtype if recursive function
std::pair<llvm::Function*,llvm::StructType*> CreateAugmentedPrimal(llvm::Function* todiff, llvm::AAResults &AA, const std::set<unsigned>& constant_args, llvm::TargetLibraryInfo &TLI, bool differentialReturn);

llvm::Function* CreatePrimalAndGradient(llvm::Function* todiff, const std::set<unsigned>& constant_args, llvm::TargetLibraryInfo &TLI, llvm::AAResults &AA, bool returnValue, bool differentialReturn, bool topLevel, llvm::Type* additionalArg, std::set<unsigned> volatile_args);
llvm::Function* CreatePrimalAndGradient(llvm::Function* todiff, const std::set<unsigned>& constant_args, llvm::TargetLibraryInfo &TLI, llvm::AAResults &AA, bool returnValue, bool differentialReturn, bool dretPtr, bool topLevel, llvm::Type* additionalArg, std::set<unsigned> volatile_args);

#endif
15 changes: 10 additions & 5 deletions enzyme/Enzyme/FunctionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ PHINode* canonicalizeIVs(fake::SCEVExpander &e, Type *Ty, Loop *L, DominatorTree
Function* preprocessForClone(Function *F, AAResults &AA, TargetLibraryInfo &TLI) {
static std::map<Function*,Function*> cache;
static std::map<Function*, BasicAAResult*> cache_AA;
llvm::errs() << "Before cache lookup for " << F->getName() << "\n";
if (cache.find(F) != cache.end()) {
AA.addAAResult(*(cache_AA[F]));
return cache[F];
}
llvm::errs() << "Did not do cache lookup for " << F->getName() << "\n";
Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(), "preprocess_" + F->getName(), F->getParent());

ValueToValueMapTy VMap;
Expand Down Expand Up @@ -468,12 +466,17 @@ Function* preprocessForClone(Function *F, AAResults &AA, TargetLibraryInfo &TLI)
AssumptionCache* AC = new AssumptionCache(*NewF);
TargetLibraryInfo* TLI = new TargetLibraryInfo(AM.getResult<TargetLibraryAnalysis>(*NewF));
auto baa = new BasicAAResult(NewF->getParent()->getDataLayout(),
#if LLVM_VERSION_MAJOR > 6
*NewF,
#endif
*TLI,
*AC,
&AM.getResult<DominatorTreeAnalysis>(*NewF),
AM.getCachedResult<LoopAnalysis>(*NewF),
AM.getCachedResult<PhiValuesAnalysis>(*NewF));
AM.getCachedResult<LoopAnalysis>(*NewF)
#if LLVM_VERSION_MAJOR > 6
,AM.getCachedResult<PhiValuesAnalysis>(*NewF)
#endif
);
cache_AA[F] = baa;
AA.addAAResult(*baa);
//ScopedNoAliasAA sa;
Expand All @@ -498,7 +501,9 @@ Function *CloneFunctionWithReturns(Function *&F, AAResults &AA, TargetLibraryInf
F = preprocessForClone(F, AA, TLI);
diffeReturnArg &= differentialReturn;
std::vector<Type*> RetTypes;
if (returnValue == ReturnType::ArgsWithReturn)
if (returnValue == ReturnType::ArgsWithReturn || returnValue == ReturnType::ArgsWithTwoReturns)
RetTypes.push_back(F->getReturnType());
if (returnValue == ReturnType::ArgsWithTwoReturns)
RetTypes.push_back(F->getReturnType());
std::vector<Type*> ArgTypes;

Expand Down
4 changes: 3 additions & 1 deletion enzyme/Enzyme/GradientUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,14 @@ Value* GradientUtils::invertPointerM(Value* val, IRBuilder<>& BuilderM) {
if (auto arg = dyn_cast<GlobalVariable>(val)) {
if (!hasMetadata(arg, "enzyme_shadow")) {
llvm::errs() << *arg << "\n";
assert(0 && "cannot compute with global variable that doesn't have marked shadow global");
report_fatal_error("cannot compute with global variable that doesn't have marked shadow global");
}
auto md = arg->getMetadata("enzyme_shadow");
if (!isa<MDTuple>(md)) {
llvm::errs() << *arg << "\n";
llvm::errs() << *md << "\n";
assert(0 && "cannot compute with global variable that doesn't have marked shadow global");
report_fatal_error("cannot compute with global variable that doesn't have marked shadow global (metadata incorrect type)");
}
auto md2 = cast<MDTuple>(md);
Expand All @@ -352,7 +354,7 @@ Value* GradientUtils::invertPointerM(Value* val, IRBuilder<>& BuilderM) {
} else if (auto fn = dyn_cast<Function>(val)) {
//! Todo allow tape propagation
std::set<unsigned> uncacheable_args;
auto newf = CreatePrimalAndGradient(fn, /*constant_args*/{}, TLI, AA, /*returnValue*/false, /*differentialReturn*/fn->getReturnType()->isFPOrFPVectorTy(), /*topLevel*/false, /*additionalArg*/nullptr, uncacheable_args);
auto newf = CreatePrimalAndGradient(fn, /*constant_args*/{}, TLI, AA, /*returnValue*/false, /*differentialReturn*/fn->getReturnType()->isFPOrFPVectorTy(), /*dretPtr*/false, /*topLevel*/false, /*additionalArg*/nullptr, uncacheable_args);
return BuilderM.CreatePointerCast(newf, fn->getType());
} else if (auto arg = dyn_cast<CastInst>(val)) {
auto result = BuilderM.CreateCast(arg->getOpcode(), invertPointerM(arg->getOperand(0), BuilderM), arg->getDestTy(), arg->getName()+"'ipc");
Expand Down
8 changes: 1 addition & 7 deletions enzyme/Enzyme/GradientUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ class GradientUtils {

IRBuilder<> BuilderZ(getNextNonDebugInstruction(op));
BuilderZ.setFastMathFlags(getFast());
this->invertedPointers[op] = BuilderZ.CreatePHI(op->getType(), 1);
this->invertedPointers[op] = BuilderZ.CreatePHI(op->getType(), 1, op->getName() + "'ip_phi");

if ( called && (called->getName() == "malloc" || called->getName() == "_Znwm")) {
this->invertedPointers[op]->setName(op->getName()+"'mi");
Expand Down Expand Up @@ -985,7 +985,6 @@ class GradientUtils {
IRBuilder<> entryBuilder(inversionAllocs);
entryBuilder.setFastMathFlags(getFast());
AllocaInst* alloc = entryBuilder.CreateAlloca(types.back(), nullptr, name+"_cache");
llvm::errs() << "alloc: "<< *alloc << "\n";

Type *BPTy = Type::getInt8PtrTy(ctx->getContext());
auto realloc = newFunc->getParent()->getOrInsertFunction("realloc", BPTy, BPTy, Type::getInt64Ty(ctx->getContext()));
Expand Down Expand Up @@ -1027,8 +1026,6 @@ class GradientUtils {
//cast<Instruction>(firstallocation)->moveBefore(allocationBuilder.GetInsertBlock()->getTerminator());
//mallocs.push_back(firstallocation);
} else {
llvm::errs() << "storeInto: " << *storeInto << "\n";
llvm::errs() << "myType: " << *myType << "\n";
allocationBuilder.CreateStore(ConstantPointerNull::get(PointerType::getUnqual(myType)), storeInto);

IRBuilder <> build(containedloops.back().first.header->getFirstNonPHI());
Expand Down Expand Up @@ -1112,7 +1109,6 @@ class GradientUtils {
indices.push_back(idx.var);
available[idx.var] = idx.var;
}
llvm::errs() << "W sl idx=" << i << " " << *idx.var << " header=" << idx.header->getName() << "\n";

Value* lim = unwrapM(riter->second, BuilderM, available, /*lookupIfAble*/true);
assert(lim);
Expand All @@ -1124,10 +1120,8 @@ class GradientUtils {
}

if (indices.size() > 0) {
llvm::errs() << "sl idx=" << i << " " << *indices[0] << "\n";
Value* idx = indices[0];
for(unsigned ind=1; ind<indices.size(); ind++) {
llvm::errs() << "sl idx=" << i << " " << *indices[ind] << "\n";
idx = BuilderM.CreateNUWAdd(idx, BuilderM.CreateNUWMul(indices[ind], limits[ind-1]));
}
next = BuilderM.CreateGEP(next, {idx});
Expand Down
9 changes: 9 additions & 0 deletions enzyme/Enzyme/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@

#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/ValueMap.h"

static inline llvm::FastMathFlags getFast() {
llvm::FastMathFlags f;
f.set();
return f;
}

template<typename T, typename N>
static inline void dumpMap(const llvm::ValueMap<T, N> &o) {
llvm::errs() << "<begin dump>\n";
for(auto a : o) llvm::errs() << "key=" << *a.first << " val=" << *a.second << "\n";
llvm::errs() << "</end dump>\n";
}

template<typename T>
static inline void dumpSet(const llvm::SmallPtrSetImpl<T*> &o) {
llvm::errs() << "<begin dump>\n";
Expand All @@ -62,6 +70,7 @@ static inline bool hasMetadata(const llvm::GlobalObject* O, llvm::StringRef kind

enum class ReturnType {
ArgsWithReturn,
ArgsWithTwoReturns,
Args,
TapeAndReturns,
Tape,
Expand Down
18 changes: 9 additions & 9 deletions enzyme/test/Enzyme/hascast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ attributes #3 = { nounwind }

; CHECK: define internal {{(dso_local )?}}{ double, double } @diffefunction(double %y, double %z, double* %x, double* %"x'", { {}, double*, double* } %tapeArg)
; CHECK-NEXT: entry:
; CHECK-NEXT: %0 = extractvalue { {}, double*, double* } %tapeArg, 2
; CHECK-NEXT: %1 = load double, double* %0
; CHECK-NEXT: store double 0.000000e+00, double* %0
; CHECK-NEXT: %2 = call {} @diffecast(double* %x, double* %"x'", {} undef)
; CHECK-NEXT: %m0diffez = fmul fast double %1, %y
; CHECK-NEXT: %m1diffey = fmul fast double %1, %z
; CHECK-NEXT: %3 = insertvalue { double, double } undef, double %m1diffey, 0
; CHECK-NEXT: %4 = insertvalue { double, double } %3, double %m0diffez, 1
; CHECK-NEXT: ret { double, double } %4
; CHECK-NEXT: %[[callp:.+]] = extractvalue { {}, double*, double* } %tapeArg, 2
; CHECK-NEXT: %[[loadcallp:.+]] = load double, double* %[[callp]]
; CHECK-NEXT: store double 0.000000e+00, double* %[[callp]]
; CHECK-NEXT: %[[dcast:.+]] = call {} @diffecast(double* %x, double* %"x'", {} undef)
; CHECK-NEXT: %m0diffez = fmul fast double %[[loadcallp]], %y
; CHECK-NEXT: %m1diffey = fmul fast double %[[loadcallp]], %z
; CHECK-NEXT: %[[toret0:.+]] = insertvalue { double, double } undef, double %m1diffey, 0
; CHECK-NEXT: %[[toret:.+]] = insertvalue { double, double } %[[toret0]], double %m0diffez, 1
; CHECK-NEXT: ret { double, double } %[[toret]]
; CHECK-NEXT: }

; CHECK: define internal {{(dso_local )?}}{} @diffecast(double* readnone %x, double* %"x'", {} %tapeArg)
Expand Down
49 changes: 49 additions & 0 deletions enzyme/test/Enzyme/retptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; RUN: opt < %s %loadEnzyme -enzyme -enzyme_preopt=false -mem2reg -sroa -instsimplify -adce -correlated-propagation -simplifycfg -S | FileCheck %s

%mat = type { float* }

define float @f(float** %this) {
entry:
%call = tail call float* @sub(float** %this)
%res = load float, float* %call, align 4
ret float %res
}

define float* @sub(float** %this) {
entry:
%0 = load float*, float** %this, align 8
ret float* %0
}

define float @g(float** %this, float** %dthis) {
entry:
%0 = tail call float (float (float**)*, ...) @__enzyme_autodiff(float (float**)* @f, float** %this, float** %dthis)
ret float %0
}

declare float @__enzyme_autodiff(float (float**)*, ...)

; CHECK: define internal {} @diffef(float** %this, float** %"this'", float %differeturn) {
; CHECK-NEXT: entry:
; CHECK-NEXT: %0 = call { {}, float*, float* } @augmented_sub(float** %this, float** %"this'")
; CHECK-NEXT: %1 = extractvalue { {}, float*, float* } %0, 2
; CHECK-NEXT: %2 = load float, float* %1
; CHECK-NEXT: %3 = fadd fast float %2, %differeturn
; CHECK-NEXT: store float %3, float* %1
; CHECK-NEXT: %4 = call {} @diffesub(float** %this, float** %"this'", {} undef)
; CHECK-NEXT: ret {} undef
; CHECK-NEXT: }

; CHECK: define internal { {}, float*, float* } @augmented_sub(float** %this, float** %"this'") {
; CHECK-NEXT: entry:
; CHECK-NEXT: %"'ipl" = load float*, float** %"this'", align 8
; CHECK-NEXT: %[[real:.+]] = load float*, float** %this, align 8
; CHECK-NEXT: %[[ins1:.+]] = insertvalue { {}, float*, float* } undef, float* %0, 1
; CHECK-NEXT: %[[ins2:.+]] = insertvalue { {}, float*, float* } %[[ins1]], float* %"'ipl", 2
; CHECK-NEXT: ret { {}, float*, float* } %[[ins2]]
; CHECK-NEXT: }

; CHECK: define internal {} @diffesub(float** %this, float** %"this'", {} %tapeArg) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret {} undef
; CHECK-NEXT: }