@@ -213,6 +213,48 @@ Core.Compiler.code_cache(interp::GPUInterpreter) = WorldView(interp.global_cache
213213Core. Compiler. lock_mi_inference (interp:: GPUInterpreter , mi:: MethodInstance ) = nothing
214214Core. Compiler. unlock_mi_inference (interp:: GPUInterpreter , mi:: MethodInstance ) = nothing
215215
216+ import Core. Compiler: retrieve_code_info, validate_code_in_debug_mode, InferenceState
217+ # Replace usage sites of `retrieve_code_info`, OptimizationState is one such use, but in all
218+ # interesting use-cases it is derived from an InferenceState. There is a third one in
219+ # `typeinf_ext` in case the module forbids inference.
220+ function InferenceState (result:: InferenceResult , cached:: Symbol , interp:: GPUInterpreter )
221+ src = retrieve_code_info (result. linfo)
222+ src === nothing && return nothing
223+ validate_code_in_debug_mode (result. linfo, src, " lowered" )
224+ validate_globalrefs (result. linfo, src)
225+ return InferenceState (result, src, cached, interp)
226+ end
227+
228+ function validate_globalrefs (mi, src)
229+ function validate (x)
230+ if x isa Expr
231+ return Expr (x. head, validate .(x. args))
232+ elseif x isa GlobalRef
233+ Base. isbindingresolved (x. mod, x. name) || return
234+ # XXX : when does this happen? do we miss any cases by bailing out early?
235+ # why doesn't calling `Base.resolve(x, force=true)` work?
236+ if ! Base. isdefined (x. mod, x. name)
237+ error (" using undefined global: $(x. mod) .$(x. name) " )
238+ end
239+ if ! Base. isconst (x. mod, x. name)
240+ error (" using mutable global: $(x. mod) .$(x. name) " )
241+ end
242+ # XXX : can we use KernelError? and make the validation conditional? both are
243+ # complicated by the fact that we don't have the CompilerJob here,
244+ # and that inference results can be cached across jobs.
245+
246+ # TODO : perform more validation? e.g. disallow Arrays and other CPU values?
247+ # probably requires an interface, so again access to the CompilerJob
248+ # (as a CPU-back-end would still support such values).
249+ end
250+ end
251+
252+ validate .(src. code)
253+
254+ return
255+ end
256+
257+
216258function Core. Compiler. add_remark! (interp:: GPUInterpreter , sv:: InferenceState , msg)
217259 @safe_debug " Inference remark during GPU compilation of $(sv. linfo) : $msg "
218260end
0 commit comments