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
@@ -14,6 +14,10 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module; opt_level=
1414 register! (pb, LowerKernelStatePass ())
1515 register! (pb, CleanupKernelStatePass ())
1616
17+ for (name, callback) in PIPELINE_CALLBACKS
18+ register! (pb, CallbackPass (name, callback))
19+ end
20+
1721 add! (pb, NewPMModulePassManager ()) do mpm
1822 buildNewPMPipeline! (mpm, job, opt_level)
1923 end
@@ -24,6 +28,15 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module; opt_level=
2428 return
2529end
2630
31+ # TODO : Priority heap to provide order between different plugins
32+ const PIPELINE_CALLBACKS = Dict {String, Any} ()
33+ function register_plugin! (name:: String , plugin)
34+ if haskey (PIPELINE_CALLBACKS, name)
35+ error (" GPUCompiler plugin with name $name is already registered" )
36+ end
37+ PIPELINE_CALLBACKS[name] = plugin
38+ end
39+
2740function buildNewPMPipeline! (mpm, @nospecialize (job:: CompilerJob ), opt_level)
2841 buildEarlySimplificationPipeline (mpm, job, opt_level)
2942 add! (mpm, AlwaysInlinerPass ())
@@ -41,6 +54,9 @@ function buildNewPMPipeline!(mpm, @nospecialize(job::CompilerJob), opt_level)
4154 add! (fpm, WarnMissedTransformationsPass ())
4255 end
4356 end
57+ for (name, callback) in PIPELINE_CALLBACKS
58+ add! (mpm, CallbackPass (name, callback))
59+ end
4460 buildIntrinsicLoweringPipeline (mpm, job, opt_level)
4561 buildCleanupPipeline (mpm, job, opt_level)
4662end
@@ -423,3 +439,17 @@ function lower_ptls!(mod::LLVM.Module)
423439 return changed
424440end
425441LowerPTLSPass () = NewPMModulePass (" GPULowerPTLS" , lower_ptls!)
442+
443+
444+ function callback_pass! (name, callback:: F , mod:: LLVM.Module ) where F
445+ job = current_job:: CompilerJob
446+ changed = false
447+
448+ if haskey (functions (mod), name)
449+ marker = functions (mod)[name]
450+ changed = callback (job, marker, mod)
451+ end
452+ return changed
453+ end
454+
455+ CallbackPass (name, callback) = NewPMModulePass (" CallbackPass<$name >" , (mod)-> callback_pass! (name, callback, mod))
0 commit comments