@@ -143,9 +143,7 @@ function irgen(@nospecialize(job::CompilerJob))
143143 end
144144 end
145145
146- global current_job
147- current_job = job
148- can_throw (job) || lower_throw! (mod)
146+ can_throw (job) || lower_throw! (job, mod)
149147
150148 # resolve the `julia.gpu.debug_level` intrinsic (see `kernel_debug_level_value`) to
151149 # the job's configured level, so device code can branch on it as a compile-time
166164#
167165# once we have thorough inference (ie. discarding `@nospecialize` and thus supporting
168166# exception arguments) and proper debug info to unwind the stack, this pass can go.
169- function lower_throw! (mod:: LLVM.Module )
170- job = current_job:: CompilerJob
167+ function lower_throw! (@nospecialize (job:: CompilerJob ), mod:: LLVM.Module )
171168 changed = false
172169 @tracepoint " lower throw" begin
173170
@@ -200,7 +197,7 @@ function lower_throw!(mod::LLVM.Module)
200197 # replace the throw with a PTX-compatible exception
201198 @dispose builder= IRBuilder () begin
202199 position! (builder, call)
203- emit_exception! (builder, name, call)
200+ emit_exception! (job, builder, name, call)
204201 end
205202
206203 # remove the call
249246# while `!mayThrow() && willReturn()`) from deleting the `signal_exception` call below and
250247# folding away the guarding bounds-check branch. so the trap is the optimizer-correctness guard;
251248# do not move its removal earlier than post-`optimize!`.
252- function emit_exception! (builder, name, inst)
253- job = current_job:: CompilerJob
249+ function emit_exception! (@nospecialize (job:: CompilerJob ), builder, name, inst)
254250 bb = position (builder)
255251 fun = LLVM. parent (bb)
256252 mod = LLVM. parent (fun)
@@ -822,12 +818,13 @@ end
822818# so that the julia.gpu.state_getter` can be simplified to return an opaque pointer.
823819
824820# add a state argument to every function in the module, starting from the kernel entry point
825- function add_kernel_state! (mod:: LLVM.Module )
826- job = current_job:: CompilerJob
827-
821+ struct AddKernelState
822+ job:: CompilerJob
823+ end
824+ function (self:: AddKernelState )(mod:: LLVM.Module )
828825 # check if we even need a kernel state argument
829- job. config. kernel || return false
830- state = kernel_state_type (job)
826+ self . job. config. kernel || return false
827+ state = kernel_state_type (self . job)
831828 if state === Nothing
832829 return false
833830 end
@@ -1022,18 +1019,20 @@ function add_kernel_state!(mod::LLVM.Module)
10221019
10231020 return true
10241021end
1025- AddKernelStatePass () = NewPMModulePass (" AddKernelStatePass" , add_kernel_state! )
1022+ AddKernelStatePass (job ) = NewPMModulePass (" AddKernelStatePass" , AddKernelState (job) )
10261023
10271024# lower calls to the state getter intrinsic. this is a two-step process, so that the state
10281025# argument can be added before optimization, and that optimization can introduce new uses
10291026# before the intrinsic getting lowered late during optimization.
1030- function lower_kernel_state! (fun:: LLVM.Function )
1031- job = current_job:: CompilerJob
1027+ struct LowerKernelState
1028+ job:: CompilerJob
1029+ end
1030+ function (self:: LowerKernelState )(fun:: LLVM.Function )
10321031 mod = LLVM. parent (fun)
10331032 changed = false
10341033
10351034 # check if we even need a kernel state argument
1036- state = kernel_state_type (job)
1035+ state = kernel_state_type (self . job)
10371036 if state === Nothing
10381037 return false
10391038 end
@@ -1084,10 +1083,12 @@ function lower_kernel_state!(fun::LLVM.Function)
10841083
10851084 return changed
10861085end
1087- LowerKernelStatePass () = NewPMFunctionPass (" LowerKernelStatePass" , lower_kernel_state! )
1086+ LowerKernelStatePass (job ) = NewPMFunctionPass (" LowerKernelStatePass" , LowerKernelState (job) )
10881087
1089- function cleanup_kernel_state! (mod:: LLVM.Module )
1090- job = current_job:: CompilerJob
1088+ struct CleanupKernelState
1089+ job:: CompilerJob
1090+ end
1091+ function (self:: CleanupKernelState )(mod:: LLVM.Module )
10911092 changed = false
10921093
10931094 # remove the getter intrinsic
@@ -1102,7 +1103,7 @@ function cleanup_kernel_state!(mod::LLVM.Module)
11021103
11031104 return changed
11041105end
1105- CleanupKernelStatePass () = NewPMModulePass (" CleanupKernelStatePass" , cleanup_kernel_state! )
1106+ CleanupKernelStatePass (job ) = NewPMModulePass (" CleanupKernelStatePass" , CleanupKernelState (job) )
11061107
11071108function kernel_state_intr (mod:: LLVM.Module , T_state)
11081109 state_intr = if haskey (functions (mod), " julia.gpu.state_getter" )
@@ -1147,7 +1148,7 @@ end
11471148
11481149# device code can query the job's configured debug level as a compile-time constant via
11491150# `kernel_debug_level()`, which emits the `julia.gpu.debug_level` intrinsic; `lower_debug_level!`
1150- # (run from `irgen`, with `current_job ` in scope) replaces it with the constant. this keeps
1151+ # (run from `irgen`, with `job ` in scope) replaces it with the constant. this keeps
11511152# the level part of the cache key (it lives in `CompilerConfig`), unlike reading the `-g`
11521153# global at parse time (which would bake the wrong level under pkgimage reuse across `-g`).
11531154
0 commit comments