5555
5656# compile to executable machine code
5757function compile (@nospecialize (job:: CompilerJob ))
58- # TODO : on 1.9, this actually creates a context. cache those.
59- ir, entry = JuliaContext () do ctx
60- mod, meta = GPUCompiler. compile (:llvm , job)
61- string (mod), LLVM. name (meta. entry)
62- end
63-
64- # generate AIR
65- air = let
66- input = Pipe ()
67- output = Pipe ()
58+ @signpost_event log= log_compiler () " Compile" " Job=$job "
6859
69- cmd = ` $(LLVMDowngrader_jll. llvm_as ()) --bitcode-version=5.0 -o -`
70- proc = run (pipeline (cmd, stdout = output, stderr = stderr , stdin = input); wait= false )
71- close (output. in)
72-
73- writer = @async begin
74- write (input, ir)
75- close (input)
60+ @signpost_interval log= log_compiler () " Generate LLVM IR" begin
61+ # TODO : on 1.9, this actually creates a context. cache those.
62+ ir, entry = JuliaContext () do ctx
63+ mod, meta = GPUCompiler. compile (:llvm , job)
64+ string (mod), LLVM. name (meta. entry)
7665 end
77- reader = @async read (output)
78-
79- wait (proc)
80- if ! success (proc)
81- file = tempname (cleanup= false ) * " .ll"
82- write (file, ir)
83- error (""" Compilation to AIR failed; see above for details.
84- If you think this is a bug, please file an issue and attach $(file) """ )
66+ end
67+
68+ @signpost_interval log= log_compiler () " Downgrade to AIR" begin
69+ # generate AIR
70+ air = let
71+ input = Pipe ()
72+ output = Pipe ()
73+
74+ cmd = ` $(LLVMDowngrader_jll. llvm_as ()) --bitcode-version=5.0 -o -`
75+ proc = run (pipeline (cmd, stdout = output, stderr = stderr , stdin = input); wait= false )
76+ close (output. in)
77+
78+ writer = @async begin
79+ write (input, ir)
80+ close (input)
81+ end
82+ reader = @async read (output)
83+
84+ wait (proc)
85+ if ! success (proc)
86+ file = tempname (cleanup= false ) * " .ll"
87+ write (file, ir)
88+ error (""" Compilation to AIR failed; see above for details.
89+ If you think this is a bug, please file an issue and attach $(file) """ )
90+ end
91+ fetch (reader)
8592 end
86- fetch (reader)
8793 end
8894
89- # create a Metal library
90- image = try
91- metallib_fun = MetalLibFunction (; name= entry, air_module= air,
92- air_version= job. config. target. air,
93- metal_version= job. config. target. metal)
94- metallib = MetalLib (; functions = [metallib_fun])
95-
96- image_stream = IOBuffer ()
97- write (image_stream, metallib)
98- take! (image_stream)
99- catch err
100- file = tempname (cleanup= false ) * " .air"
101- write (file, air)
102- error (""" Compilation to Metal library failed; see below for details.
103- If you think this is a bug, please file an issue and attach $(file) """ )
95+ @signpost_interval log= log_compiler () " Create Metal library" begin
96+ image = try
97+ metallib_fun = MetalLibFunction (; name= entry, air_module= air,
98+ air_version= job. config. target. air,
99+ metal_version= job. config. target. metal)
100+ metallib = MetalLib (; functions = [metallib_fun])
101+
102+ image_stream = IOBuffer ()
103+ write (image_stream, metallib)
104+ take! (image_stream)
105+ catch err
106+ file = tempname (cleanup= false ) * " .air"
107+ write (file, air)
108+ error (""" Compilation to Metal library failed; see below for details.
109+ If you think this is a bug, please file an issue and attach $(file) """ )
110+ end
104111 end
105112
106113 return (; image, entry)
@@ -109,20 +116,24 @@ end
109116# link into an executable kernel
110117@autoreleasepool function link (@nospecialize (job:: CompilerJob ), compiled;
111118 return_function= false )
112- dev = current_device ()
113- lib = MTLLibraryFromData (dev, compiled. image)
114- fun = MTLFunction (lib, compiled. entry)
115- pipeline_state = try
116- MTLComputePipelineState (dev, fun)
117- catch err
118- isa (err, NSError) || rethrow ()
119-
120- # the back-end compiler likely failed
121- # XXX : check more accurately? the error domain doesn't help much here
122- file = tempname (cleanup= false ) * " .metallib"
123- write (file, compiled. image)
124- error (""" Compilation to native code failed; see below for details.
125- If you think this is a bug, please file an issue and attach $(file) """ )
119+ @signpost_event log= log_compiler () " Link" " Job=$job "
120+
121+ @signpost_interval log= log_compiler () " Instantiate compute pipeline" begin
122+ dev = current_device ()
123+ lib = MTLLibraryFromData (dev, compiled. image)
124+ fun = MTLFunction (lib, compiled. entry)
125+ pipeline_state = try
126+ MTLComputePipelineState (dev, fun)
127+ catch err
128+ isa (err, NSError) || rethrow ()
129+
130+ # the back-end compiler likely failed
131+ # XXX : check more accurately? the error domain doesn't help much here
132+ file = tempname (cleanup= false ) * " .metallib"
133+ write (file, compiled. image)
134+ error (""" Compilation to native code failed; see below for details.
135+ If you think this is a bug, please file an issue and attach $(file) """ )
136+ end
126137 end
127138
128139 # most of the time, we don't need the function object,
0 commit comments