Skip to content

Fix calling convention and other small bugs #16

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

Merged
merged 12 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixing vector bug
  • Loading branch information
wsmoses committed Oct 12, 2019
commit 1a613ad20d3b5d56a70d8cb0d315b27fb6ffc183
66 changes: 39 additions & 27 deletions enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
if (cachedfunctions.find(tup) != cachedfunctions.end()) {
return cachedfunctions[tup];
}
llvm::errs() << "creating augmented primal for " << todiff->getName() << "\n";

if (constant_args.size() == 0 && hasMetadata(todiff, "enzyme_augment")) {
auto md = todiff->getMetadata("enzyme_augment");
Expand Down Expand Up @@ -109,6 +110,7 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
gutils->forceAugmentedReturns();

for(BasicBlock* BB: gutils->originalBlocks) {
llvm::errs() << " augment block" << BB->getName() << "\n";
auto term = BB->getTerminator();
assert(term);
if(auto ri = dyn_cast<ReturnInst>(term)) {
Expand All @@ -133,11 +135,13 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
}

if (!isa<UnreachableInst>(term))
for (auto I = BB->rbegin(), E = BB->rend(); I != E;) {
for (BasicBlock::reverse_iterator I = BB->rbegin(), E = BB->rend(); I != E;) {
Instruction* inst = &*I;
assert(inst);
I++;
if (gutils->originalInstructions.find(inst) == gutils->originalInstructions.end()) continue;

llvm::errs() << " augment inst" << *inst << "\n";

if(auto op = dyn_cast_or_null<IntrinsicInst>(inst)) {
switch(op->getIntrinsicID()) {
Expand Down Expand Up @@ -195,7 +199,11 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
case Intrinsic::lifetime_end:
case Intrinsic::assume:
case Intrinsic::fabs:
case Intrinsic::x86_sse_max_ss:
case Intrinsic::x86_sse_max_ps:
case Intrinsic::maxnum:
case Intrinsic::x86_sse_min_ss:
case Intrinsic::x86_sse_min_ps:
case Intrinsic::minnum:
case Intrinsic::log:
case Intrinsic::log2:
Expand Down Expand Up @@ -321,7 +329,7 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
gutils->nonconstant_values.insert(rv);
}
assert(op->getType() == rv->getType());
llvm::errs() << "augmented considering differential ip of " << called->getName() << " " << *op->getType() << " " << gutils->isConstantValue(op) << "\n";
//llvm::errs() << "augmented considering differential ip of " << called->getName() << " " << *op->getType() << " " << gutils->isConstantValue(op) << "\n";

if ((op->getType()->isPointerTy() || op->getType()->isIntegerTy()) && !gutils->isConstantValue(op)) {
auto antiptr = cast<Instruction>(BuilderZ.CreateExtractValue(augmentcall, {2}));
Expand Down Expand Up @@ -422,7 +430,13 @@ std::pair<Function*,StructType*> CreateAugmentedPrimal(Function* todiff, AAResul
MallocTypes.push_back(a->getType());
}

StructType* tapeType = StructType::get(nf->getContext(), MallocTypes);
StructType* tapeType;

//if (MallocTypes.size() > 1) {
// tapeType = StructType::create(MallocTypes, (todiff->getName()+"_tapeType").str(), false);
//} else {
tapeType = StructType::get(nf->getContext(), MallocTypes);
//}

bool recursive = cachedfunctions[tup].first->getNumUses() > 0;

Expand Down Expand Up @@ -674,8 +688,16 @@ void createInvertedTerminator(DiffeGradientUtils* gutils, BasicBlock *BB, Alloca
setphi = true;
}
}
Instruction* dif = cast<Instruction>(Builder.CreateSelect(replacePHIs[pred], prediff, Constant::getNullValue(prediff->getType())));
SelectInst* dif = cast<SelectInst>(Builder.CreateSelect(replacePHIs[pred], prediff, Constant::getNullValue(prediff->getType())));
auto addedSelects = gutils->addToDiffe(PN->getIncomingValueForBlock(pred), dif, Builder);
if (dif->getNumUses() != 0) {
llvm::errs() << "oldFunc: " << *gutils->oldFunc << "\n";
llvm::errs() << "newFunc: " << *gutils->newFunc << "\n";
for (auto use : dif->users()) {
llvm::errs() << "user: " << *use << "\n";
}
llvm::errs() << "dif: " << *dif << "\n";
}
assert(dif->getNumUses() == 0);
dif->eraseFromParent();
for (auto select : addedSelects)
Expand Down Expand Up @@ -1403,6 +1425,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
}
return cachedfunctions[tup] = foundcalled;
}
llvm::errs() << "creating gradient for " << todiff->getName() << " topLevel:" << topLevel << "\n";

assert(!todiff->empty());
auto M = todiff->getParent();
Expand Down Expand Up @@ -1445,6 +1468,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
std::map<ReturnInst*,StoreInst*> replacedReturns;

for(BasicBlock* BB: gutils->originalBlocks) {
llvm::errs() << " gradient block" << BB->getName() << "\n";

LoopContext loopContext;
bool inLoop = gutils->getContext(BB, loopContext);
Expand Down Expand Up @@ -1479,22 +1503,10 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
gutils->setDiffe(val, toset, Builder2);
};

auto addToDiffeIndexed = [&](Value* val, Value* dif, ArrayRef<Value*> idxs) -> void{
gutils->addToDiffeIndexed(val, dif, idxs, Builder2);
};

auto invertPointer = [&](Value* val) -> Value* {
return gutils->invertPointerM(val, Builder2);
};

auto addToPtrDiffe = [&](Value* val, Value* dif) {
gutils->addToPtrDiffe(val, dif, Builder2);
};

auto setPtrDiffe = [&](Value* val, Value* dif) {
gutils->setPtrDiffe(val, dif, Builder2);
};

auto term = BB->getTerminator();
assert(term);
bool unreachableTerminator = false;
Expand Down Expand Up @@ -1539,6 +1551,8 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
assert(inst);
I++;
if (gutils->originalInstructions.find(inst) == gutils->originalInstructions.end()) continue;

llvm::errs() << " gradient inst" << *inst << "\n";

if (auto op = dyn_cast<BinaryOperator>(inst)) {
if (gutils->isConstantInstruction(inst)) continue;
Expand Down Expand Up @@ -1687,6 +1701,8 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
}
break;
}
case Intrinsic::x86_sse_max_ss:
case Intrinsic::x86_sse_max_ps:
case Intrinsic::maxnum: {
if (!gutils->isConstantInstruction(op) && !gutils->isConstantValue(op->getOperand(0))) {
auto cmp = Builder2.CreateFCmpOLT(lookup(op->getOperand(0)), lookup(op->getOperand(1)));
Expand All @@ -1698,6 +1714,8 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
}
break;
}
case Intrinsic::x86_sse_min_ss:
case Intrinsic::x86_sse_min_ps:
case Intrinsic::minnum: {
if (!gutils->isConstantInstruction(op) && !gutils->isConstantValue(op->getOperand(0))) {
auto cmp = Builder2.CreateFCmpOLT(lookup(op->getOperand(0)), lookup(op->getOperand(1)));
Expand Down Expand Up @@ -1828,7 +1846,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
if (!op->getType()->isPointerTy()) {
auto prediff = diffe(inst);
setDiffe(inst, Constant::getNullValue(inst->getType()));
addToPtrDiffe(op->getOperand(0), prediff);
gutils->addToPtrDiffe(op->getOperand(0), prediff, Builder2);
} else {
//Builder2.CreateStore(diffe(inst), invertPointer(op->getOperand(0)));//, op->getName()+"'psweird");
//addToNPtrDiffe(op->getOperand(0), diffe(inst));
Expand All @@ -1844,7 +1862,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
if (! ( op->getValueOperand()->getType()->isPointerTy() || (op->getValueOperand()->getType()->isIntegerTy() && !isIntASecretFloat(op->getValueOperand()) ) ) ) {
if (!gutils->isConstantValue(op->getValueOperand())) {
auto dif1 = Builder2.CreateLoad(invertPointer(op->getPointerOperand()));
setPtrDiffe(op->getPointerOperand(), Constant::getNullValue(op->getValueOperand()->getType()));
gutils->setPtrDiffe(op->getPointerOperand(), Constant::getNullValue(op->getValueOperand()->getType()), Builder2);
addToDiffe(op->getValueOperand(), dif1);
}
} else if (topLevel) {
Expand All @@ -1856,12 +1874,6 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
storeBuilder.CreateStore(valueop, pointerop);
//llvm::errs() << "ignoring store bc pointer of " << *op << "\n";
}
//?necessary if pointer is readwrite
/*
IRBuilder<> BuilderZ(inst);
Builder2.CreateStore(
lookup(BuilderZ.CreateLoad(op->getPointerOperand())), lookup(op->getPointerOperand()));
*/
} else if(auto op = dyn_cast<ExtractValueInst>(inst)) {
if (gutils->isConstantValue(inst)) continue;
if (op->getType()->isPointerTy()) continue;
Expand All @@ -1872,7 +1884,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
SmallVector<Value*,4> sv;
for(auto i : op->getIndices())
sv.push_back(ConstantInt::get(Type::getInt32Ty(Context), i));
addToDiffeIndexed(op->getOperand(0), prediff, sv);
gutils->addToDiffeIndexed(op->getOperand(0), prediff, sv, Builder2);
}
setDiffe(inst, Constant::getNullValue(inst->getType()));
} else if(auto op = dyn_cast<InsertValueInst>(inst)) {
Expand Down Expand Up @@ -1911,7 +1923,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
SmallVector<Value*,4> sv;
sv.push_back(ConstantInt::get(Type::getInt32Ty(Context), opidx));
if (!gutils->isConstantValue(op->getOperand(opnum)))
addToDiffeIndexed(op->getOperand(opnum), Builder2.CreateExtractElement(loaded, instidx), sv);
gutils->addToDiffeIndexed(op->getOperand(opnum), Builder2.CreateExtractElement(loaded, instidx), sv, Builder2);
instidx++;
}
setDiffe(inst, Constant::getNullValue(inst->getType()));
Expand All @@ -1921,7 +1933,7 @@ Function* CreatePrimalAndGradient(Function* todiff, const std::set<unsigned>& co
if (!gutils->isConstantValue(op->getVectorOperand())) {
SmallVector<Value*,4> sv;
sv.push_back(op->getIndexOperand());
addToDiffeIndexed(op->getVectorOperand(), diffe(inst), sv);
gutils->addToDiffeIndexed(op->getVectorOperand(), diffe(inst), sv, Builder2);
}
setDiffe(inst, Constant::getNullValue(inst->getType()));
} else if(auto op = dyn_cast<InsertElementInst>(inst)) {
Expand Down
13 changes: 8 additions & 5 deletions enzyme/Enzyme/GradientUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,14 @@ class GradientUtils {
erase(cast<Instruction>(malloc));
ret->setName(n);
}
llvm::errs() << " retrieved from malloc " << *ret << "\n";
return ret;
} else {
assert(malloc);
assert(!isa<PHINode>(malloc));

llvm::errs() << " adding to malloc " << *malloc << "\n";

if (isa<UndefValue>(malloc)) {
addedMallocs.push_back(malloc);
return malloc;
Expand Down Expand Up @@ -1274,16 +1277,16 @@ class DiffeGradientUtils : public GradientUtils {

//! optimize fadd of select to select of fadd
if (SelectInst* select = dyn_cast<SelectInst>(dif)) {
if (ConstantFP* ci = dyn_cast<ConstantFP>(select->getTrueValue())) {
if (ci->isZero()) {
if (Constant* ci = dyn_cast<Constant>(select->getTrueValue())) {
if (ci->isZeroValue()) {
cast<Instruction>(res)->eraseFromParent();
res = BuilderM.CreateSelect(select->getCondition(), old, BuilderM.CreateFAdd(old, select->getFalseValue()));
addedSelects.emplace_back(cast<SelectInst>(res));
goto endselect;
}
}
if (ConstantFP* ci = dyn_cast<ConstantFP>(select->getFalseValue())) {
if (ci->isZero()) {
if (Constant* ci = dyn_cast<Constant>(select->getFalseValue())) {
if (ci->isZeroValue()) {
cast<Instruction>(res)->eraseFromParent();
res = BuilderM.CreateSelect(select->getCondition(), BuilderM.CreateFAdd(old, select->getTrueValue()), old);
addedSelects.emplace_back(cast<SelectInst>(res));
Expand All @@ -1306,7 +1309,7 @@ class DiffeGradientUtils : public GradientUtils {
}
return addedSelects;
} else {
assert(0 && "lol");
llvm_unreachable("unknown type to add to diffe");
exit(1);
}
}
Expand Down
68 changes: 68 additions & 0 deletions enzyme/test/Enzyme/vecrelu.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; RUN: opt < %s %loadEnzyme -enzyme -enzyme_preopt=false -mem2reg -inline -early-cse -instcombine -simplifycfg -S | FileCheck %s

; __attribute__((noinline))
; double f(double x) {
; return x;
; }
;
; double relu(double x) {
; return (x > 0) ? f(x) : 0;
; }
;
; double drelu(double x) {
; return __builtin_autodiff(relu, x);
; }

define dso_local <4 x double> @f(<4 x double> %x) #1 {
entry:
ret <4 x double> %x
}

define dso_local i1 @cmp() #1 {
entry:
ret i1 true
}

define dso_local <4 x double> @relu(<4 x double> %x) {
entry:
%cmp = call i1 @cmp()
br i1 %cmp, label %cond.true, label %cond.end

cond.true: ; preds = %entry
%call = tail call fast <4 x double> @f(<4 x double> %x)
br label %cond.end

cond.end: ; preds = %entry, %cond.true
%cond = phi <4 x double> [ %call, %cond.true ], [ zeroinitializer, %entry ]
ret <4 x double> %cond
}

define dso_local <4 x double> @drelu(<4 x double> %x) {
entry:
%0 = tail call <4 x double> (<4 x double> (<4 x double>)*, ...) @__enzyme_autodiff(<4 x double> (<4 x double>)* nonnull @relu, <4 x double> %x)
ret <4 x double> %0
}

declare <4 x double> @__enzyme_autodiff(<4 x double> (<4 x double>)*, ...) #0

attributes #0 = { nounwind }
attributes #1 = { nounwind readnone noinline }

; CHECK: define dso_local <4 x double> @drelu(<4 x double> %x)
; CHECK-NEXT: entry:
; CHECK-NEXT: %cmp.i = call i1 @cmp()
; CHECK-NEXT: br i1 %cmp.i, label %invertcond.true.i, label %differelu.exit
; CHECK: invertcond.true.i: ; preds = %entry
; CHECK-NEXT: %[[diffef:.+]] = call { <4 x double> } @diffef(<4 x double> %x, <4 x double> <double 1.000000e+00, double 1.000000e+00, double 1.000000e+00, double 1.000000e+00>)
; CHECK-NEXT: %[[result:.+]] = extractvalue { <4 x double> } %[[diffef]], 0
; CHECK-NEXT: br label %differelu.exit
; CHECK: differelu.exit: ; preds = %entry, %invertcond.true.i
; CHECK-NEXT: %"x'de.0.i" = phi <4 x double> [ %[[result]], %invertcond.true.i ], [ zeroinitializer, %entry ]
; CHECK-NEXT: ret <4 x double> %"x'de.0.i"
; CHECK-NEXT: }

; CHECK: define internal {{(dso_local )?}}{ <4 x double> } @diffef(<4 x double> %x, <4 x double> %[[differet:.+]])
; CHECK-NEXT: entry:
; CHECK-NEXT: %[[result:.+]] = insertvalue { <4 x double> } undef, <4 x double> %[[differet]], 0
; CHECK-NEXT: ret { <4 x double> } %[[result]]
; CHECK-NEXT: }