@@ -45,12 +45,26 @@ macro LineInfoNode(method)
4545end
4646
4747
48- function rewrite_inst (inst)
49- @show inst
48+
49+ function maybe_argextype (
50+ @nospecialize (x),
51+ src,
52+ )
53+ return try
54+ Core. Compiler. argextype (x, src)
55+ catch err
56+ ! (err isa Core. Compiler. InvalidIRError) && rethrow ()
57+ nothing
58+ end
59+ end
60+
61+ function rewrite_inst (inst, ir)
5062 if Meta. isexpr (inst, :call )
51- rep = Expr (:call , call_with_reactant, inst. args... )
52- @show rep
53- return rep
63+ ft = Core. Compiler. widenconst (maybe_argextype (inst. args[1 ], ir))
64+ if ! (ft <: Core.IntrinsicFunction ) && ! (ft <: Core.Builtin )
65+ rep = Expr (:call , call_with_reactant, inst. args... )
66+ return rep
67+ end
5468 end
5569 if Meta. isexpr (inst, :invoke )
5670 return Expr (:call , inst. args[2 : end ]. .. )
@@ -204,12 +218,11 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
204218 @nospecialize
205219
206220 args = redub_arguments
207- @show args
208221
209222 stub = Core. GeneratedFunctionStub (identity, Core. svec (:call_with_reactant , :redub_arguments ), Core. svec ())
210223
211224 # look up the method match
212- builtin_error = :(throw (AssertionError (" Unsupported call_with_reactant of builtin $args " )))
225+ builtin_error = :(throw (AssertionError (" Unsupported call_with_reactant of builtin $redub_arguments " )))
213226
214227 if args[1 ] <: Core.Builtin
215228 return stub (world, source, builtin_error)
@@ -218,7 +231,7 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
218231 method_error = :(throw (MethodError (args[1 ], args[2 : end ], $ world)))
219232
220233 interp = ReactantInterpreter (; world)
221-
234+
222235 sig = Tuple{args... }
223236 lookup_result = Core. Compiler. findall (sig, Core. Compiler. method_table (interp)). matches
224237
@@ -239,7 +252,6 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
239252 (Any, Any, Any), match. method, match. spec_types, match. sparams)
240253
241254 result = Core. Compiler. InferenceResult (mi, Core. Compiler. typeinf_lattice (interp))
242- @static if true
243255 frame = Core. Compiler. InferenceState (result, #= cache_mode=# :local , interp)
244256 @assert frame != = nothing
245257 Core. Compiler. typeinf (interp, frame)
@@ -260,19 +272,16 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
260272 end
261273
262274 for (i, inst) in enumerate (ir. stmts)
275+
263276 @static if VERSION < v " 1.11"
264- Core. Compiler. setindex! (ir. stmts[i], rewrite_inst (inst[:inst ]), :inst )
277+ Core. Compiler. setindex! (ir. stmts[i], rewrite_inst (inst[:inst ], ir ), :inst )
265278 else
266- Core. Compiler. setindex! (ir. stmts[i], rewrite_inst (inst[:stmt ]), :stmt )
279+ Core. Compiler. setindex! (ir. stmts[i], rewrite_inst (inst[:stmt ], ir ), :stmt )
267280 end
281+ Core. Compiler. setindex! (ir. stmts[i], Any, :type )
268282 end
269283 Core. Compiler. finish (interp, opt, ir, caller)
270-
271284 src = Core. Compiler. ir_to_codeinf! (opt)
272- # end
273- else
274- src = Core. Compiler. retrieve_code_info (mi, world)
275- end
276285
277286 # prepare a new code info
278287 code_info = copy (src)
@@ -347,17 +356,9 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
347356
348357 # substitute static parameters, offset slot numbers by number of added slots, and
349358 # offset statement indices by the number of additional statements
350- @show code_info. code
351359
352- @show n_prepended_slots
353- @static if false
354- Base. Meta. partially_inline! (code_info. code, fn_args, method. sig, Any[static_params... ],
355- n_prepended_slots, length (overdubbed_code), :propagate )
356- else
357360 arg_partially_inline! (code_info. code, fn_args, method. sig, Any[static_params... ],
358361 n_prepended_slots, n_prepended_slots, length (overdubbed_code), :propagate )
359- end
360- @show code_info. code
361362
362363 # callexpr = Expr(:call, Core.OpaqueClosure(ir), fn_args...)
363364 # push!(overdubbed_code, callexpr)
@@ -371,23 +372,6 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
371372 append! (overdubbed_code, code_info. code)
372373 append! (overdubbed_codelocs, code_info. codelocs)
373374
374- @show overdubbed_code
375-
376- @static if false
377- for i in eachindex (overdubbed_code)
378- prev = overdubbed_code[i]
379- if Base. Meta. isexpr (prev, :call )
380- @show prev
381- @show prev. args[1 ]
382- @show prev. args[1 ] isa Core. IntrinsicFunction
383- if ! (prev. args[1 ] isa Core. IntrinsicFunction)
384- overdubbed_code[i] = Expr (:call , GlobalRef (Reactant, :call_with_reactant ), prev. args... )
385- @show " post" , overdubbed_code[i]
386- end
387- end
388- end
389- end
390-
391375 #= == set `code_info`/`reflection` fields accordingly ===#
392376
393377 if code_info. method_for_inference_limit_heuristics === nothing
@@ -399,58 +383,7 @@ function call_with_reactant_generator(world::UInt, source::LineNumberNode, self,
399383 code_info. ssavaluetypes = length (overdubbed_code)
400384 code_info. ssaflags = [0x00 for _ in 1 : length (overdubbed_code)] # XXX we need to copy flags that are set for the original code
401385
402- @show code_info
403386 return code_info
404-
405- self_result = Core. Compiler. InferenceResult (self_mi, Core. Compiler. typeinf_lattice (interp))
406-
407-
408- @show self
409- self_meths = Base. _methods_by_ftype (Tuple{self, Vararg{Any}}, - 1 , world)
410- @show self_meths
411- self_method = (self_meths[1 ]:: Core.MethodMatch ). method
412- self_mi = Core. Compiler. specialize_method (self_method, Tuple{typeof (Reactant. call_with_reactant), sig. parameters... }, Core. svec ())
413- @show self_mi
414- self_result = Core. Compiler. InferenceResult (self_mi, Core. Compiler. typeinf_lattice (interp))
415- frame = Core. Compiler. InferenceState (self_result, code_info, #= cache_mode=# :global , interp)
416- @assert frame != = nothing
417- Core. Compiler. typeinf (interp, frame)
418- @assert Core. Compiler. is_inferred (frame)
419-
420- # if Core.Compiler.result_is_constabi(interp, frame.result)
421- # rt = frame.result.result::Core.Compiler.Const
422- # src = Core.Compiler.codeinfo_for_const(interp, frame.linfo, rt.val)
423- # else
424- opt = Core. Compiler. OptimizationState (frame, interp)
425-
426- ir = opt. src
427- @show ir
428- for (i, stmt) in enumerate (ir. stmts)
429- @show stmt
430-
431- end
432-
433- @show ir
434-
435- caller = frame. result
436- @static if VERSION < v " 1.11-"
437- ir = Core. Compiler. run_passes (ir, opt, caller)
438- else
439- ir = Core. Compiler. run_passes_ipo_safe (ir, opt, caller)
440- Core. Compiler. ipo_dataflow_analysis! (interp, opt, ir, caller)
441-
442- end
443- Core. Compiler. finish (interp, opt, ir, caller)
444-
445- src = Core. Compiler. ir_to_codeinf! (opt)
446- # end
447-
448- src = copy (src)
449- src. ssavaluetypes = length (src. code)
450-
451- @show src
452-
453- return src
454387end
455388
456389@eval function call_with_reactant (redub_arguments... )
0 commit comments