Skip to content

Commit

Permalink
Support more Julia 1.11 functions (#2121)
Browse files Browse the repository at this point in the history
* Support more Julia 1.11 functions

* Consider allocating functions
  • Loading branch information
wsmoses authored Oct 17, 2024
1 parent 8416a68 commit f085ac8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 30 deletions.
4 changes: 3 additions & 1 deletion enzyme/Enzyme/ActivityAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,9 @@ bool ActivityAnalyzer::isConstantValue(TypeResults const &TR, Value *Val) {
}
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy" ||
funcName == "jl_idtable_rehash" ||
funcName == "ijl_idtable_rehash") {
funcName == "ijl_idtable_rehash" ||
funcName == "jl_genericmemory_copy_slice" ||
funcName == "ijl_genericmemory_copy_slice") {
// This pointer is inactive if it is either not actively stored to
// and not actively loaded from and the copied input is inactive.
if (directions & DOWN && directions & UP) {
Expand Down
19 changes: 4 additions & 15 deletions enzyme/Enzyme/CallDerivatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3386,15 +3386,8 @@ bool AdjointGenerator::handleKnownCallDerivatives(
}

// No need to free GC.
if (funcName == "ijl_alloc_array_1d" ||
funcName == "ijl_alloc_array_2d" ||
funcName == "ijl_alloc_array_3d" ||
funcName == "ijl_array_copy" || funcName == "jl_alloc_array_1d" ||
funcName == "jl_alloc_array_2d" ||
funcName == "jl_alloc_array_3d" || funcName == "jl_array_copy" ||
funcName == "julia.gc_alloc_obj" ||
funcName == "jl_gc_alloc_typed" ||
funcName == "ijl_gc_alloc_typed") {
if (EnzymeJuliaAddrLoad && isa<PointerType>(call.getType()) &&
cast<PointerType>(call.getType())->getAddressSpace() == 10) {
if (Mode == DerivativeMode::ReverseModeGradient && AllocationLoop)
gutils->rematerializedPrimalOrShadowAllocations.push_back(
newCall);
Expand Down Expand Up @@ -3459,12 +3452,8 @@ bool AdjointGenerator::handleKnownCallDerivatives(

// If an object is managed by the GC do not preserve it for later free,
// Thus it only needs caching if there is a need for it in the reverse.
if (funcName == "jl_alloc_array_1d" || funcName == "jl_alloc_array_2d" ||
funcName == "jl_alloc_array_3d" || funcName == "jl_array_copy" ||
funcName == "ijl_alloc_array_1d" || funcName == "ijl_alloc_array_2d" ||
funcName == "ijl_alloc_array_3d" || funcName == "ijl_array_copy" ||
funcName == "julia.gc_alloc_obj" || funcName == "jl_gc_alloc_typed" ||
funcName == "ijl_gc_alloc_typed") {
if (EnzymeJuliaAddrLoad && isa<PointerType>(call.getType()) &&
cast<PointerType>(call.getType())->getAddressSpace() == 10) {
if (!subretused) {
eraseIfUnused(call, /*erase*/ true, /*check*/ false);
return true;
Expand Down
13 changes: 4 additions & 9 deletions enzyme/Enzyme/GradientUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9267,15 +9267,10 @@ void GradientUtils::computeGuaranteedFrees() {
if (hasMetadata(CI, "enzyme_fromstack")) {
allocationsWithGuaranteedFree[CI].insert(CI);
}
if (funcName == "jl_alloc_array_1d" ||
funcName == "jl_alloc_array_2d" ||
funcName == "jl_alloc_array_3d" || funcName == "jl_array_copy" ||
funcName == "ijl_alloc_array_1d" ||
funcName == "ijl_alloc_array_2d" ||
funcName == "ijl_alloc_array_3d" || funcName == "ijl_array_copy" ||
funcName == "julia.gc_alloc_obj" ||
funcName == "jl_gc_alloc_typed" ||
funcName == "ijl_gc_alloc_typed") {
// TODO: special case object managed by the GC as it is automatically
// freed.
if (EnzymeJuliaAddrLoad && isa<PointerType>(CI->getType()) &&
cast<PointerType>(CI->getType())->getAddressSpace() == 10) {
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion enzyme/Enzyme/MLIR/Analysis/ActivityAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,11 @@ bool mlir::enzyme::ActivityAnalyzer::isConstantValue(MTypeResults const &TR,
// }
// }
// }
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" ||
funcName == "jl_genericmemory_copy_slice" ||
funcName == "ijl_genericmemory_copy_slice") {
// This pointer is inactive if it is either not actively stored to
// and not actively loaded from.
if (directions & DOWN && directions & UP) {
Expand Down
17 changes: 13 additions & 4 deletions enzyme/Enzyme/TypeAnalysis/TypeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,10 +1346,19 @@ void TypeAnalyzer::considerTBAA() {
updateAnalysis(call->getOperand(0), TT.Only(-1, call), call);
}
if (F) {
StringSet<> JuliaKnownTypes = {
"julia.gc_alloc_obj", "jl_alloc_array_1d", "jl_alloc_array_2d",
"jl_alloc_array_3d", "ijl_alloc_array_1d", "ijl_alloc_array_2d",
"ijl_alloc_array_3d", "jl_gc_alloc_typed", "ijl_gc_alloc_typed"};
StringSet<> JuliaKnownTypes = {"julia.gc_alloc_obj",
"jl_alloc_array_1d",
"jl_alloc_array_2d",
"jl_alloc_array_3d",
"ijl_alloc_array_1d",
"ijl_alloc_array_2d",
"ijl_alloc_array_3d",
"jl_gc_alloc_typed",
"ijl_gc_alloc_typed",
"jl_alloc_genericmemory",
"ijl_alloc_genericmemory",
"jl_new_array",
"ijl_new_array"};
if (JuliaKnownTypes.count(F->getName())) {
visitCallBase(*call);
continue;
Expand Down
8 changes: 8 additions & 0 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,10 @@ bool writesToMemoryReadBy(const TypeResults *TR, llvm::AAResults &AA,
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy")
return false;

if (funcName == "jl_genericmemory_copy_slice" ||
funcName == "ijl_genericmemory_copy_slice")
return false;

if (funcName == "jl_new_array" || funcName == "ijl_new_array")
return false;

Expand Down Expand Up @@ -2494,6 +2498,10 @@ bool writesToMemoryReadBy(const TypeResults *TR, llvm::AAResults &AA,
if (funcName == "jl_array_copy" || funcName == "ijl_array_copy")
return false;

if (funcName == "jl_genericmemory_copy_slice" ||
funcName == "ijl_genericmemory_copy_slice")
return false;

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

Expand Down

0 comments on commit f085ac8

Please sign in to comment.