-
Notifications
You must be signed in to change notification settings - Fork 79
Add world to EnzymeContext #2643
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
base: main
Are you sure you want to change the base?
Conversation
vchuravy
commented
Oct 2, 2025
- access enzyme_context through gutils
- assert check the world we encode in enzyme_extrace_world
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/api.jl b/src/api.jl
index 51a4fd31..b43bb9ad 100644
--- a/src/api.jl
+++ b/src/api.jl
@@ -580,7 +580,7 @@ EnzymeGetShadowType(width, T) =
ccall((:EnzymeGetShadowType, libEnzyme), LLVMTypeRef, (UInt64, LLVMTypeRef), width, T)
function EnzymeGradientUtilsGetExternalContext(gutils)
- ccall(
+ return ccall(
(:EnzymeGradientUtilsGetExternalContext, libEnzyme),
Ptr{Cvoid},
(EnzymeGradientUtilsRef,),
diff --git a/src/compiler.jl b/src/compiler.jl
index 58d2ff4d..a570428f 100644
--- a/src/compiler.jl
+++ b/src/compiler.jl
@@ -224,11 +224,11 @@ include("compiler/utils.jl")
include("compiler/orcv2.jl")
import .Enzyme: GradientUtils, call_samefunc_with_inverted_bundles!,
- get_width, get_mode, get_runtime_activity,
- get_strong_zero, get_shadow_type, get_uncacheable,
- erase_with_placeholder, is_constant_value, is_constant_inst,
- new_from_original, lookup_value, invert_pointer, debug_from_orig!,
- add_reverse_block!, set_reverse_block!, enzyme_context, enzyme_gutils_context
+ get_width, get_mode, get_runtime_activity,
+ get_strong_zero, get_shadow_type, get_uncacheable,
+ erase_with_placeholder, is_constant_value, is_constant_inst,
+ new_from_original, lookup_value, invert_pointer, debug_from_orig!,
+ add_reverse_block!, set_reverse_block!, enzyme_context, enzyme_gutils_context
# Julia function to LLVM stem and arity
const cmplx_known_ops =
@@ -488,7 +488,7 @@ include("typeutils/make_zero.jl")
function nested_codegen!(ctx::EnzymeContext, mode::API.CDerivativeMode, mod::LLVM.Module, @nospecialize(f), @nospecialize(tt::Type))
funcspec = my_methodinstance(mode == API.DEM_ForwardMode ? Forward : Reverse, typeof(f), tt, ctx.world)
- nested_codegen!(ctx, mode, mod, funcspec)
+ return nested_codegen!(ctx, mode, mod, funcspec)
end
function prepare_llvm(interp, mod::LLVM.Module, job, meta)
@@ -1235,10 +1235,10 @@ const DumpPreNestedOpt = Ref(false)
const DumpPostNestedOpt = Ref(false)
function nested_codegen!(
- ctx::EnzymeContext,
+ ctx::EnzymeContext,
mode::API.CDerivativeMode,
mod::LLVM.Module,
- funcspec::Core.MethodInstance,
+ funcspec::Core.MethodInstance,
)
world = ctx.world
# TODO: Put a cache here index on `mod` and f->tt
@@ -2401,7 +2401,7 @@ const DumpPostEnzyme = Ref(false)
const DumpPostWrap = Ref(false)
function enzyme!(
- enzyme_context::EnzymeContext,
+ enzyme_context::EnzymeContext,
job::CompilerJob,
interp,
mod::LLVM.Module,
@@ -2586,7 +2586,7 @@ function enzyme!(
if wrap
augmented_primalf = create_abi_wrapper(
- enzyme_context,
+ enzyme_context,
augmented_primalf,
TT,
rt,
@@ -2595,7 +2595,7 @@ function enzyme!(
augmented,
width,
returnPrimal,
- shadow_init,
+ shadow_init,
interp,
runtimeActivity,
)
@@ -2628,7 +2628,7 @@ function enzyme!(
) #=atomicAdd=#
if wrap
adjointf = create_abi_wrapper(
- enzyme_context,
+ enzyme_context,
adjointf,
TT,
rt,
@@ -2637,7 +2637,7 @@ function enzyme!(
augmented,
width,
false,
- shadow_init,
+ shadow_init,
interp,
runtimeActivity
) #=returnPrimal=#
@@ -2669,7 +2669,7 @@ function enzyme!(
augmented_primalf = nothing
if wrap
adjointf = create_abi_wrapper(
- enzyme_context,
+ enzyme_context,
adjointf,
TT,
rt,
@@ -2678,7 +2678,7 @@ function enzyme!(
nothing,
width,
returnPrimal,
- shadow_init,
+ shadow_init,
interp,
runtimeActivity
)
@@ -2714,7 +2714,7 @@ function enzyme!(
if wrap
pf = adjointf
adjointf = create_abi_wrapper(
- enzyme_context,
+ enzyme_context,
adjointf,
TT,
rt,
@@ -2723,7 +2723,7 @@ function enzyme!(
nothing,
width,
returnPrimal,
- shadow_init,
+ shadow_init,
interp,
runtimeActivity
)
@@ -2795,7 +2795,7 @@ function set_subprogram!(f::LLVM.Function, sp)
end
function create_abi_wrapper(
- ctx::EnzymeContext,
+ ctx::EnzymeContext,
enzymefn::LLVM.Function,
@nospecialize(TT::Type),
@nospecialize(rettype::Type),
@@ -2804,7 +2804,7 @@ function create_abi_wrapper(
augmented,
width::Int,
returnPrimal::Bool,
- shadow_init::Bool,
+ shadow_init::Bool,
interp,
runtime_activity::Bool
)
@@ -5117,7 +5117,7 @@ end
adjointf, augmented_primalf, TapeType = enzyme!(
ctx,
job,
- interp,
+ interp,
mod,
primalf,
TT, |
| function guess_activity end | ||
|
|
||
| mutable struct EnzymeContext | ||
| world::UInt64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should already be accessible everywhere gutils is available per enzyme_extract_world(fn) or something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is meant as a proof of concept to demonstrate that yes we can get the world we are compiling from everywhere.
Using a string attribute is fine, but is also a blocker for caching the IR for later use, so I wanted to see if we can use this to avoid adding ephemeral data to potentially cacheable inputs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2643 +/- ##
=======================================
Coverage 52.84% 52.84%
=======================================
Files 12 12
Lines 1194 1194
=======================================
Hits 631 631
Misses 563 563 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| # TODO: get world from TypeAnalyzer | ||
| # @assert world == enzyme_gutils_context(gutils).world | ||
| throw(IllegalTypeAnalysisException(msg, mi, world, sval, ir, bt)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wsmoses would you be okay with a change where CustomErrorHandler always receives gutils? We can go from gutils to TypeAnalyzer (if I saw that right).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on the particular error thrown there may not be a gutils created.
But we can for sure audit all the current error types and see what we can do
|
ok new jll is out |
1bfee3a to
f49e5b0
Compare
Benchmark Results
Benchmark PlotsA plot of the benchmark results has been uploaded as an artifact at https://github.com/EnzymeAD/Enzyme.jl/actions/runs/18928507383/artifacts/4413159292. |
f49e5b0 to
b623edf
Compare