Skip to content

Commit

Permalink
add globalptr bad test
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses committed Nov 7, 2019
1 parent 48eb605 commit d7ad407
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
47 changes: 14 additions & 33 deletions enzyme/Enzyme/GradientUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,6 @@ class GradientUtils {
if (!getContext(blk, idx)) {
break;
}
//llvm::errs() << " adding to contexts: " << idx.header->getName() << " starting ctx=" << ctx->getName() << "\n";
contexts.emplace_back(idx);
blk = idx.preheader;
}
Expand All @@ -925,35 +924,19 @@ class GradientUtils {
if (contexts[i].dynamic) {
limits[i] = ConstantInt::get(Type::getInt64Ty(ctx->getContext()), 1);
} else {
//while (limits[i] == nullptr) {
ValueToValueMapTy emptyMap;
IRBuilder <> allocationBuilder(&allocationPreheaders[i]->back());
Value* limitMinus1 = unwrapM(contexts[i].limit, allocationBuilder, emptyMap, /*lookupIfAble*/false);

if (limitMinus1 == nullptr) {
allocationPreheaders[i] = contexts[i].preheader;
allocationBuilder.SetInsertPoint(&allocationPreheaders[i]->back());
limitMinus1 = unwrapM(contexts[i].limit, allocationBuilder, emptyMap, /*lookupIfAble*/false);
/*
assert(allocationPreheaders[i]);
llvm::errs() << *oldFunc << "\n";
llvm::errs() << *newFunc << "\n";
llvm::errs() << "needed value " << *contexts[i].limit << " at " << allocationPreheaders[i]->getName() << "\n";
*/
}
assert(limitMinus1 != nullptr);
limits[i] = allocationBuilder.CreateNUWAdd(limitMinus1, ConstantInt::get(limitMinus1->getType(), 1));
//TODO allow triangular arrays per above
/*
if (limits[i] == nullptr) {
int firstDifferent = j+1;
while (allocationPreheaders[firstDifferent] == allocationPreheaders[i]) {
firstDifferent++;
assert(firstDifferent < contexts.size());
}
allocationPreheaders[i] = allocationPreheaders[firstDifferent];
}
}*/
ValueToValueMapTy emptyMap;
IRBuilder <> allocationBuilder(&allocationPreheaders[i]->back());
Value* limitMinus1 = unwrapM(contexts[i].limit, allocationBuilder, emptyMap, /*lookupIfAble*/false);

// We have a loop with static bounds, but whose limit is not available to be computed at the current loop preheader (such as the innermost loop of triangular iteration domain)
// Handle this case like a dynamic loop
if (limitMinus1 == nullptr) {
allocationPreheaders[i] = contexts[i].preheader;
allocationBuilder.SetInsertPoint(&allocationPreheaders[i]->back());
limitMinus1 = unwrapM(contexts[i].limit, allocationBuilder, emptyMap, /*lookupIfAble*/false);
}
assert(limitMinus1 != nullptr);
limits[i] = allocationBuilder.CreateNUWAdd(limitMinus1, ConstantInt::get(limitMinus1->getType(), 1));
}
}

Expand All @@ -970,17 +953,15 @@ class GradientUtils {
size = allocationBuilder.CreateNUWMul(size, limits[i]);
}

//llvm::errs() << "considering ctx " << ctx->getName() << " alph=" << allocationPreheaders[i]->getName() << " ctxheader=" << contexts[i].header->getName() << "\n";
// We are now starting a new allocation context
if ( (i+1 < contexts.size()) && (allocationPreheaders[i] != allocationPreheaders[i+1]) ) {
//llvm::errs() << "starting outermost ph at " << allocationPreheaders[i]->getName() << "|ctx=" << ctx->getName() <<"\n";
sublimits.push_back(std::make_pair(size, lims));
size = nullptr;
lims.clear();
}
}

if (size != nullptr) {
//llvm::errs() << "starting final outermost ph at " << allocationPreheaders[contexts.size()-1]->getName()<<"|ctx=" << ctx->getName() << "\n";
sublimits.push_back(std::make_pair(size, lims));
lims.clear();
}
Expand Down
44 changes: 44 additions & 0 deletions enzyme/test/Enzyme/globalptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; RUN: opt < %s %loadEnzyme -enzyme -enzyme_preopt=false -mem2reg -instsimplify -adce -correlated-propagation -simplifycfg -S | FileCheck %s

; XFAIL: *
; a function returning a pointer/float with no arguments is mistakenly marked as constant in spite of accessing a global

@global = external dso_local local_unnamed_addr global double*, align 8

; Function Attrs: noinline norecurse nounwind readonly uwtable
define dso_local double* @myglobal() local_unnamed_addr #0 {
entry:
%0 = load double*, double** @global, align 8
ret double* %0
}

; Function Attrs: noinline norecurse nounwind readonly uwtable
define dso_local double @mulglobal(double %x) #0 {
entry:
%call = tail call double* @myglobal()
%arrayidx = getelementptr inbounds double, double* %call, i64 2
%0 = load double, double* %arrayidx, align 8
%mul = fmul fast double %0, %x
ret double %mul
}

; Function Attrs: noinline nounwind uwtable
define dso_local double @derivative(double %x) local_unnamed_addr #1 {
entry:
%0 = tail call double (...) @__enzyme_autodiff.f64(double (double)* nonnull @mulglobal, double %x) #2
ret double %0
}

declare double @__enzyme_autodiff.f64(...) local_unnamed_addr

attributes #0 = { noinline norecurse nounwind readonly uwtable }
attributes #1 = { noinline nounwind uwtable }
attributes #2 = { nounwind }

; CHECK: define internal { double } @diffemulglobal(double %x, double %differeturn)
; CHECK-NEXT: entry:
; CHECK-NEXT: %call = tail call double* @myglobal()
; CHECK-NEXT: %arrayidx = getelementptr inbounds double, double* %call, i64 2
; CHECK-NEXT: %0 = load double, double* %arrayidx, align 8
; CHECK-NEXT: %[[tmul:.+]] = fmul fast double %0, %x
; CHECK-NEXT: %[[tcall.+]] = call {} @diffemyglobal(double %x)
1 change: 0 additions & 1 deletion enzyme/test/Enzyme/triangular.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ define dso_local double @get(double* nocapture %x, i64 %i, i64 %j) local_unnamed
entry:
%arrayidx = getelementptr inbounds double, double* %x, i64 %i
%0 = load double, double* %arrayidx, align 8
store double 0.000000e+00, double* %arrayidx, align 8
ret double %0
}

Expand Down

0 comments on commit d7ad407

Please sign in to comment.