33function optimize! (@nospecialize (job:: CompilerJob ), mod:: LLVM.Module ; opt_level= 1 )
44 tm = llvm_machine (job. config. target)
55
6- global current_job
6+ global current_job # ScopedValue?
77 current_job = job
88
99 @dispose pb= NewPMPassBuilder () begin
@@ -24,6 +24,15 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module; opt_level=
2424 return
2525end
2626
27+ # TODO : Priority heap to provide order between different plugins
28+ const PIPELINE_CALLBACKS = Dict {String, Any} ()
29+ function register_plugin! (name:: String , plugin)
30+ if haskey (PIPELINE_CALLBACKS, name)
31+ error (" GPUCompiler plugin with name $name is already registered" )
32+ end
33+ PIPELINE_CALLBACKS[name] = plugin
34+ end
35+
2736function buildNewPMPipeline! (mpm, @nospecialize (job:: CompilerJob ), opt_level)
2837 buildEarlySimplificationPipeline (mpm, job, opt_level)
2938 add! (mpm, AlwaysInlinerPass ())
@@ -41,6 +50,9 @@ function buildNewPMPipeline!(mpm, @nospecialize(job::CompilerJob), opt_level)
4150 add! (fpm, WarnMissedTransformationsPass ())
4251 end
4352 end
53+ for (name, callback) in PIPELINE_CALLBACKS
54+ add! (mpm, CallbackPass (name, callback))
55+ end
4456 buildIntrinsicLoweringPipeline (mpm, job, opt_level)
4557 buildCleanupPipeline (mpm, job, opt_level)
4658end
@@ -423,3 +435,17 @@ function lower_ptls!(mod::LLVM.Module)
423435 return changed
424436end
425437LowerPTLSPass () = NewPMModulePass (" GPULowerPTLS" , lower_ptls!)
438+
439+
440+ function callback_pass! (name, callback:: F , mod:: LLVM.Module ) where F
441+ job = current_job:: CompilerJob
442+ changed = false
443+
444+ if haskey (functions (mod), name)
445+ marker = functions (mod)[name]
446+ changed = callback (job, marker, mod)
447+ end
448+ return changed
449+ end
450+
451+ CallbackPass (name, callback) = NewPMModulePass (" CallbackPass<$name >" , (mod)-> callback_pass! (name, callback, mod))
0 commit comments