Skip to content

Commit

Permalink
potential store (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses authored Nov 21, 2023
1 parent 227b443 commit 7f28ab0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
44 changes: 31 additions & 13 deletions enzyme/Enzyme/ActivityAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,9 +1613,11 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
}
}
}
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy") {
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy" ||
funcName == "jl_idtable_rehash" ||
funcName == "ijl_idtable_rehash") {
// This pointer is inactive if it is either not actively stored to
// and not actively loaded from.
// and not actively loaded from and the copied input is inactive.
if (directions & DOWN && directions & UP) {
if (UpHypothesis->isConstantValue(TR, op->getOperand(0))) {
auto DownHypothesis = std::shared_ptr<ActivityAnalyzer>(
Expand Down Expand Up @@ -1756,7 +1758,7 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
// an potentially active value is stored into the memory
// memory loaded from the value is used in an active way
Instruction *potentiallyActiveStore = nullptr;
bool potentialStore = false;
Instruction *potentialStore = nullptr;
Instruction *potentiallyActiveLoad = nullptr;

// Assume the value (not instruction) is itself active
Expand Down Expand Up @@ -2039,20 +2041,22 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
<< "\n";
if (auto SI = dyn_cast<StoreInst>(I)) {
bool cop = !Hypothesis->isConstantValue(TR, SI->getValueOperand());
bool cop2 = !Hypothesis->isConstantValue(TR, SI->getPointerOperand());
if (EnzymePrintActivity)
llvm::errs() << " -- store potential activity: " << (int)cop
llvm::errs() << " -- store potential activity: " << (int)cop << ","
<< (int)cop2 << ","
<< " - " << *SI << " of "
<< " Val=" << *Val << "\n";
potentialStore = true;
if (cop)
potentialStore = I;
if (cop && cop2)
potentiallyActiveStore = SI;
} else if (auto MTI = dyn_cast<MemTransferInst>(I)) {
bool cop = !Hypothesis->isConstantValue(TR, MTI->getArgOperand(1));
potentialStore = true;
potentialStore = I;
if (cop)
potentiallyActiveStore = MTI;
} else if (isa<MemSetInst>(I)) {
potentialStore = true;
potentialStore = I;
} else {
// Otherwise fallback and check if the instruction is active
// TODO: note that this can be optimized (especially for function
Expand All @@ -2062,7 +2066,7 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
llvm::errs() << " -- unknown store potential activity: " << (int)cop
<< " - " << *I << " of "
<< " Val=" << *Val << "\n";
potentialStore = true;
potentialStore = I;
if (cop)
potentiallyActiveStore = I;
}
Expand Down Expand Up @@ -2113,11 +2117,25 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
}

activeLoadAndStore:;
if (EnzymePrintActivity)
if (EnzymePrintActivity) {
llvm::errs() << " </MEMSEARCH" << (int)directions << ">" << *Val
<< " potentiallyActiveLoad=" << potentiallyActiveLoad
<< " potentiallyActiveStore=" << potentiallyActiveStore
<< " potentialStore=" << potentialStore << "\n";
<< " potentiallyActiveLoad=";
if (potentiallyActiveLoad)
llvm::errs() << *potentiallyActiveLoad;
else
llvm::errs() << potentiallyActiveLoad;
llvm::errs() << " potentiallyActiveStore=";
if (potentiallyActiveStore)
llvm::errs() << *potentiallyActiveStore;
else
llvm::errs() << potentiallyActiveStore;
llvm::errs() << " potentialStore=";
if (potentiallyActiveStore)
llvm::errs() << *potentialStore;
else
llvm::errs() << potentialStore;
llvm::errs() << "\n";
}
if (potentiallyActiveLoad && potentiallyActiveStore) {
ReEvaluateValueIfInactiveInst[potentiallyActiveLoad].insert(Val);
ReEvaluateValueIfInactiveInst[potentiallyActiveStore].insert(Val);
Expand Down
6 changes: 6 additions & 0 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,9 @@ bool writesToMemoryReadBy(llvm::AAResults &AA, llvm::TargetLibraryInfo &TLI,
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy")
return false;

if (funcName == "jl_idtable_rehash" || funcName == "ijl_idtable_rehash")
return false;

// Isend only writes to inaccessible mem only
if (funcName == "MPI_Send" || funcName == "PMPI_Send") {
return false;
Expand Down Expand Up @@ -2187,6 +2190,9 @@ bool writesToMemoryReadBy(llvm::AAResults &AA, llvm::TargetLibraryInfo &TLI,
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy")
return false;

if (funcName == "jl_idtable_rehash" || funcName == "ijl_idtable_rehash")
return false;

if (auto iasm = dyn_cast<InlineAsm>(call->getCalledOperand())) {
if (StringRef(iasm->getAsmString()).contains("exit"))
return false;
Expand Down
18 changes: 18 additions & 0 deletions enzyme/test/ActivityAnalysis/capturedalloc.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -print-activity-analysis -activity-analysis-func=f -activity-analysis-inactive-args -o /dev/null | FileCheck %s; fi
; RUN: %opt < %s %newLoadEnzyme -passes="print-activity-analysis" -activity-analysis-func=f -activity-analysis-inactive-args -S | FileCheck %s

define void @f(i8* %cv, i8** %cptr) {
entry:
%v2 = call i8* @mycopy(i8* %cv)
store i8* %v2, i8** %cptr
ret void
}

declare i8* @mycopy(i8*)

; CHECK: i8* %cv: icv:1
; CHECK: i8** %cptr: icv:1
; CHECK-NEXT: entry
; CHECK-NEXT: %v2 = call i8* @mycopy(i8* %cv): icv:1 ici:1
; CHECK-NEXT: store i8* %v2, i8** %cptr, align 8: icv:1 ici:1
; CHECK-NEXT: ret void: icv:1 ici:1

0 comments on commit 7f28ab0

Please sign in to comment.