1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include "nir/nir.h"
29 #include "radv_debug.h"
30 #include "radv_llvm_helper.h"
31 #include "radv_private.h"
32 #include "radv_shader.h"
33 #include "radv_shader_args.h"
34
35 #include "ac_binary.h"
36 #include "ac_llvm_build.h"
37 #include "ac_nir.h"
38 #include "ac_nir_to_llvm.h"
39 #include "ac_shader_abi.h"
40 #include "ac_shader_util.h"
41 #include "sid.h"
42
43 struct radv_shader_context {
44 struct ac_llvm_context ac;
45 const struct nir_shader *shader;
46 struct ac_shader_abi abi;
47 const struct radv_nir_compiler_options *options;
48 const struct radv_shader_info *shader_info;
49 const struct radv_shader_args *args;
50
51 gl_shader_stage stage;
52
53 unsigned max_workgroup_size;
54 LLVMContextRef context;
55 struct ac_llvm_pointer main_function;
56
57 LLVMValueRef descriptor_sets[MAX_SETS];
58
59 LLVMValueRef gs_wave_id;
60
61 uint64_t output_mask;
62 };
63
64 static inline struct radv_shader_context *
radv_shader_context_from_abi(struct ac_shader_abi * abi)65 radv_shader_context_from_abi(struct ac_shader_abi *abi)
66 {
67 return container_of(abi, struct radv_shader_context, abi);
68 }
69
70 static struct ac_llvm_pointer
create_llvm_function(struct ac_llvm_context * ctx,LLVMModuleRef module,LLVMBuilderRef builder,const struct ac_shader_args * args,enum ac_llvm_calling_convention convention,unsigned max_workgroup_size,const struct radv_nir_compiler_options * options)71 create_llvm_function(struct ac_llvm_context *ctx, LLVMModuleRef module, LLVMBuilderRef builder,
72 const struct ac_shader_args *args, enum ac_llvm_calling_convention convention,
73 unsigned max_workgroup_size, const struct radv_nir_compiler_options *options)
74 {
75 struct ac_llvm_pointer main_function = ac_build_main(args, ctx, convention, "main", ctx->voidt, module);
76
77 if (options->info->address32_hi) {
78 ac_llvm_add_target_dep_function_attr(main_function.value, "amdgpu-32bit-address-high-bits",
79 options->info->address32_hi);
80 }
81
82 ac_llvm_set_workgroup_size(main_function.value, max_workgroup_size);
83 ac_llvm_set_target_features(main_function.value, ctx, true);
84
85 return main_function;
86 }
87
88 static void
load_descriptor_sets(struct radv_shader_context * ctx)89 load_descriptor_sets(struct radv_shader_context *ctx)
90 {
91 const struct radv_userdata_locations *user_sgprs_locs = &ctx->shader_info->user_sgprs_locs;
92 uint32_t mask = ctx->shader_info->desc_set_used_mask;
93
94 if (user_sgprs_locs->shader_data[AC_UD_INDIRECT_DESCRIPTOR_SETS].sgpr_idx != -1) {
95 struct ac_llvm_pointer desc_sets = ac_get_ptr_arg(&ctx->ac, &ctx->args->ac, ctx->args->descriptor_sets[0]);
96 while (mask) {
97 int i = u_bit_scan(&mask);
98
99 ctx->descriptor_sets[i] = ac_build_load_to_sgpr(&ctx->ac, desc_sets, LLVMConstInt(ctx->ac.i32, i, false));
100 LLVMSetAlignment(ctx->descriptor_sets[i], 4);
101 }
102 } else {
103 while (mask) {
104 int i = u_bit_scan(&mask);
105
106 ctx->descriptor_sets[i] = ac_get_arg(&ctx->ac, ctx->args->descriptor_sets[i]);
107 }
108 }
109 }
110
111 static enum ac_llvm_calling_convention
get_llvm_calling_convention(LLVMValueRef func,gl_shader_stage stage)112 get_llvm_calling_convention(LLVMValueRef func, gl_shader_stage stage)
113 {
114 switch (stage) {
115 case MESA_SHADER_VERTEX:
116 case MESA_SHADER_TESS_EVAL:
117 return AC_LLVM_AMDGPU_VS;
118 break;
119 case MESA_SHADER_GEOMETRY:
120 return AC_LLVM_AMDGPU_GS;
121 break;
122 case MESA_SHADER_TESS_CTRL:
123 return AC_LLVM_AMDGPU_HS;
124 break;
125 case MESA_SHADER_FRAGMENT:
126 return AC_LLVM_AMDGPU_PS;
127 break;
128 case MESA_SHADER_COMPUTE:
129 return AC_LLVM_AMDGPU_CS;
130 break;
131 default:
132 unreachable("Unhandle shader type");
133 }
134 }
135
136 /* Returns whether the stage is a stage that can be directly before the GS */
137 static bool
is_pre_gs_stage(gl_shader_stage stage)138 is_pre_gs_stage(gl_shader_stage stage)
139 {
140 return stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_TESS_EVAL;
141 }
142
143 static void
create_function(struct radv_shader_context * ctx,gl_shader_stage stage,bool has_previous_stage)144 create_function(struct radv_shader_context *ctx, gl_shader_stage stage, bool has_previous_stage)
145 {
146 if (ctx->ac.gfx_level >= GFX10) {
147 if (is_pre_gs_stage(stage) && ctx->shader_info->is_ngg) {
148 /* On GFX10+, VS and TES are merged into GS for NGG. */
149 stage = MESA_SHADER_GEOMETRY;
150 has_previous_stage = true;
151 }
152 }
153
154 ctx->main_function = create_llvm_function(&ctx->ac, ctx->ac.module, ctx->ac.builder, &ctx->args->ac,
155 get_llvm_calling_convention(ctx->main_function.value, stage),
156 ctx->max_workgroup_size, ctx->options);
157
158 load_descriptor_sets(ctx);
159
160 if (stage == MESA_SHADER_TESS_CTRL || (stage == MESA_SHADER_VERTEX && ctx->shader_info->vs.as_ls) ||
161 ctx->shader_info->is_ngg ||
162 /* GFX9 has the ESGS ring buffer in LDS. */
163 (stage == MESA_SHADER_GEOMETRY && has_previous_stage)) {
164 ac_declare_lds_as_pointer(&ctx->ac);
165 }
166 }
167
168 static LLVMValueRef
radv_load_base_vertex(struct ac_shader_abi * abi,bool non_indexed_is_zero)169 radv_load_base_vertex(struct ac_shader_abi *abi, bool non_indexed_is_zero)
170 {
171 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
172 return ac_get_arg(&ctx->ac, ctx->args->ac.base_vertex);
173 }
174
175 static LLVMValueRef
radv_load_rsrc(struct radv_shader_context * ctx,LLVMValueRef ptr,LLVMTypeRef type)176 radv_load_rsrc(struct radv_shader_context *ctx, LLVMValueRef ptr, LLVMTypeRef type)
177 {
178 if (ptr && LLVMTypeOf(ptr) == ctx->ac.i32) {
179 LLVMValueRef result;
180
181 LLVMTypeRef ptr_type = LLVMPointerType(type, AC_ADDR_SPACE_CONST_32BIT);
182 ptr = LLVMBuildIntToPtr(ctx->ac.builder, ptr, ptr_type, "");
183 LLVMSetMetadata(ptr, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
184
185 result = LLVMBuildLoad2(ctx->ac.builder, type, ptr, "");
186 LLVMSetMetadata(result, ctx->ac.invariant_load_md_kind, ctx->ac.empty_md);
187
188 return result;
189 }
190
191 return ptr;
192 }
193
194 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi * abi,LLVMValueRef buffer_ptr)195 radv_load_ubo(struct ac_shader_abi *abi, LLVMValueRef buffer_ptr)
196 {
197 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
198 return radv_load_rsrc(ctx, buffer_ptr, ctx->ac.v4i32);
199 }
200
201 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi * abi,LLVMValueRef buffer_ptr,bool write,bool non_uniform)202 radv_load_ssbo(struct ac_shader_abi *abi, LLVMValueRef buffer_ptr, bool write, bool non_uniform)
203 {
204 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
205 return radv_load_rsrc(ctx, buffer_ptr, ctx->ac.v4i32);
206 }
207
208 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi * abi,LLVMValueRef index,enum ac_descriptor_type desc_type)209 radv_get_sampler_desc(struct ac_shader_abi *abi, LLVMValueRef index, enum ac_descriptor_type desc_type)
210 {
211 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
212
213 /* 3 plane formats always have same size and format for plane 1 & 2, so
214 * use the tail from plane 1 so that we can store only the first 16 bytes
215 * of the last plane. */
216 if (desc_type == AC_DESC_PLANE_2 && index && LLVMTypeOf(index) == ctx->ac.i32) {
217 LLVMValueRef plane1_addr = LLVMBuildSub(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i32, 32, false), "");
218 LLVMValueRef descriptor1 = radv_load_rsrc(ctx, plane1_addr, ctx->ac.v8i32);
219 LLVMValueRef descriptor2 = radv_load_rsrc(ctx, index, ctx->ac.v4i32);
220
221 LLVMValueRef components[8];
222 for (unsigned i = 0; i < 4; ++i)
223 components[i] = ac_llvm_extract_elem(&ctx->ac, descriptor2, i);
224
225 for (unsigned i = 4; i < 8; ++i)
226 components[i] = ac_llvm_extract_elem(&ctx->ac, descriptor1, i);
227 return ac_build_gather_values(&ctx->ac, components, 8);
228 }
229
230 bool v4 = desc_type == AC_DESC_BUFFER || desc_type == AC_DESC_SAMPLER;
231 return radv_load_rsrc(ctx, index, v4 ? ctx->ac.v4i32 : ctx->ac.v8i32);
232 }
233
234 static void
scan_shader_output_decl(struct radv_shader_context * ctx,struct nir_variable * variable,struct nir_shader * shader,gl_shader_stage stage)235 scan_shader_output_decl(struct radv_shader_context *ctx, struct nir_variable *variable, struct nir_shader *shader,
236 gl_shader_stage stage)
237 {
238 int idx = variable->data.driver_location;
239 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
240 uint64_t mask_attribs;
241
242 if (variable->data.compact) {
243 unsigned component_count = variable->data.location_frac + glsl_get_length(variable->type);
244 attrib_count = (component_count + 3) / 4;
245 }
246
247 mask_attribs = ((1ull << attrib_count) - 1) << idx;
248
249 ctx->output_mask |= mask_attribs;
250 }
251
252 static LLVMValueRef
radv_load_output(struct radv_shader_context * ctx,unsigned index,unsigned chan)253 radv_load_output(struct radv_shader_context *ctx, unsigned index, unsigned chan)
254 {
255 int idx = ac_llvm_reg_index_soa(index, chan);
256 LLVMValueRef output = ctx->abi.outputs[idx];
257 LLVMTypeRef type = ctx->abi.is_16bit[idx] ? ctx->ac.f16 : ctx->ac.f32;
258 return LLVMBuildLoad2(ctx->ac.builder, type, output, "");
259 }
260
261 static void
ac_llvm_finalize_module(struct radv_shader_context * ctx,LLVMPassManagerRef passmgr)262 ac_llvm_finalize_module(struct radv_shader_context *ctx, LLVMPassManagerRef passmgr)
263 {
264 LLVMRunPassManager(passmgr, ctx->ac.module);
265 LLVMDisposeBuilder(ctx->ac.builder);
266
267 ac_llvm_context_dispose(&ctx->ac);
268 }
269
270 static void
prepare_gs_input_vgprs(struct radv_shader_context * ctx,bool merged)271 prepare_gs_input_vgprs(struct radv_shader_context *ctx, bool merged)
272 {
273 if (merged) {
274 ctx->gs_wave_id = ac_unpack_param(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args->ac.merged_wave_info), 16, 8);
275 } else {
276 ctx->gs_wave_id = ac_get_arg(&ctx->ac, ctx->args->ac.gs_wave_id);
277 }
278 }
279
280 /* Ensure that the esgs ring is declared.
281 *
282 * We declare it with 64KB alignment as a hint that the
283 * pointer value will always be 0.
284 */
285 static void
declare_esgs_ring(struct radv_shader_context * ctx)286 declare_esgs_ring(struct radv_shader_context *ctx)
287 {
288 assert(!LLVMGetNamedGlobal(ctx->ac.module, "esgs_ring"));
289
290 LLVMValueRef esgs_ring =
291 LLVMAddGlobalInAddressSpace(ctx->ac.module, LLVMArrayType(ctx->ac.i32, 0), "esgs_ring", AC_ADDR_SPACE_LDS);
292 LLVMSetLinkage(esgs_ring, LLVMExternalLinkage);
293 LLVMSetAlignment(esgs_ring, 64 * 1024);
294 }
295
296 static LLVMValueRef
radv_intrinsic_load(struct ac_shader_abi * abi,nir_intrinsic_instr * intrin)297 radv_intrinsic_load(struct ac_shader_abi *abi, nir_intrinsic_instr *intrin)
298 {
299 switch (intrin->intrinsic) {
300 case nir_intrinsic_load_base_vertex:
301 case nir_intrinsic_load_first_vertex:
302 return radv_load_base_vertex(abi, intrin->intrinsic == nir_intrinsic_load_base_vertex);
303 default:
304 return NULL;
305 }
306 }
307
308 static LLVMModuleRef
ac_translate_nir_to_llvm(struct ac_llvm_compiler * ac_llvm,const struct radv_nir_compiler_options * options,const struct radv_shader_info * info,struct nir_shader * const * shaders,int shader_count,const struct radv_shader_args * args)309 ac_translate_nir_to_llvm(struct ac_llvm_compiler *ac_llvm, const struct radv_nir_compiler_options *options,
310 const struct radv_shader_info *info, struct nir_shader *const *shaders, int shader_count,
311 const struct radv_shader_args *args)
312 {
313 struct radv_shader_context ctx = {0};
314 ctx.args = args;
315 ctx.options = options;
316 ctx.shader_info = info;
317
318 enum ac_float_mode float_mode = AC_FLOAT_MODE_DEFAULT;
319
320 if (shaders[0]->info.float_controls_execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32) {
321 float_mode = AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO;
322 }
323
324 bool exports_mrtz = false;
325 bool exports_color_null = false;
326 if (shaders[0]->info.stage == MESA_SHADER_FRAGMENT) {
327 exports_mrtz = info->ps.writes_z || info->ps.writes_stencil || info->ps.writes_sample_mask;
328 exports_color_null = !exports_mrtz || (shaders[0]->info.outputs_written & (0xffu << FRAG_RESULT_DATA0));
329 }
330
331 ac_llvm_context_init(&ctx.ac, ac_llvm, options->info, float_mode, info->wave_size, info->ballot_bit_size,
332 exports_color_null, exports_mrtz);
333
334 uint32_t length = 1;
335 for (uint32_t i = 0; i < shader_count; i++)
336 if (shaders[i]->info.name)
337 length += strlen(shaders[i]->info.name) + 1;
338
339 char *name = malloc(length);
340 if (name) {
341 uint32_t offset = 0;
342 for (uint32_t i = 0; i < shader_count; i++) {
343 if (!shaders[i]->info.name)
344 continue;
345
346 strcpy(name + offset, shaders[i]->info.name);
347 offset += strlen(shaders[i]->info.name);
348 if (i != shader_count - 1)
349 name[offset++] = ',';
350 }
351
352 LLVMSetSourceFileName(ctx.ac.module, name, offset);
353 }
354
355 ctx.context = ctx.ac.context;
356
357 ctx.max_workgroup_size = info->workgroup_size;
358
359 create_function(&ctx, shaders[shader_count - 1]->info.stage, shader_count >= 2);
360
361 ctx.abi.intrinsic_load = radv_intrinsic_load;
362 ctx.abi.load_ubo = radv_load_ubo;
363 ctx.abi.load_ssbo = radv_load_ssbo;
364 ctx.abi.load_sampler_desc = radv_get_sampler_desc;
365 ctx.abi.clamp_shadow_reference = false;
366 ctx.abi.robust_buffer_access = options->robust_buffer_access_llvm;
367 ctx.abi.load_grid_size_from_user_sgpr = args->load_grid_size_from_user_sgpr;
368
369 bool is_ngg = is_pre_gs_stage(shaders[0]->info.stage) && info->is_ngg;
370 if (shader_count >= 2 || is_ngg)
371 ac_init_exec_full_mask(&ctx.ac);
372
373 if (args->ac.vertex_id.used)
374 ctx.abi.vertex_id = ac_get_arg(&ctx.ac, args->ac.vertex_id);
375 if (args->ac.vs_rel_patch_id.used)
376 ctx.abi.vs_rel_patch_id = ac_get_arg(&ctx.ac, args->ac.vs_rel_patch_id);
377 if (args->ac.instance_id.used)
378 ctx.abi.instance_id = ac_get_arg(&ctx.ac, args->ac.instance_id);
379
380 if (options->info->has_ls_vgpr_init_bug && shaders[shader_count - 1]->info.stage == MESA_SHADER_TESS_CTRL)
381 ac_fixup_ls_hs_input_vgprs(&ctx.ac, &ctx.abi, &args->ac);
382
383 if (is_ngg) {
384 if (!info->is_ngg_passthrough)
385 declare_esgs_ring(&ctx);
386
387 if (ctx.stage == MESA_SHADER_GEOMETRY) {
388 /* Scratch space used by NGG GS for repacking vertices at the end. */
389 LLVMTypeRef ai32 = LLVMArrayType(ctx.ac.i32, 8);
390 LLVMValueRef gs_ngg_scratch =
391 LLVMAddGlobalInAddressSpace(ctx.ac.module, ai32, "ngg_scratch", AC_ADDR_SPACE_LDS);
392 LLVMSetInitializer(gs_ngg_scratch, LLVMGetUndef(ai32));
393 LLVMSetLinkage(gs_ngg_scratch, LLVMExternalLinkage);
394 LLVMSetAlignment(gs_ngg_scratch, 4);
395
396 /* Vertex emit space used by NGG GS for storing all vertex attributes. */
397 LLVMValueRef gs_ngg_emit =
398 LLVMAddGlobalInAddressSpace(ctx.ac.module, LLVMArrayType(ctx.ac.i32, 0), "ngg_emit", AC_ADDR_SPACE_LDS);
399 LLVMSetInitializer(gs_ngg_emit, LLVMGetUndef(ai32));
400 LLVMSetLinkage(gs_ngg_emit, LLVMExternalLinkage);
401 LLVMSetAlignment(gs_ngg_emit, 4);
402 }
403
404 /* GFX10 hang workaround - there needs to be an s_barrier before gs_alloc_req always */
405 if (ctx.ac.gfx_level == GFX10 && shader_count == 1)
406 ac_build_s_barrier(&ctx.ac, shaders[0]->info.stage);
407 }
408
409 for (int shader_idx = 0; shader_idx < shader_count; ++shader_idx) {
410 ctx.stage = shaders[shader_idx]->info.stage;
411 ctx.shader = shaders[shader_idx];
412 ctx.output_mask = 0;
413
414 if (shader_idx && !(shaders[shader_idx]->info.stage == MESA_SHADER_GEOMETRY && info->is_ngg)) {
415 /* Execute a barrier before the second shader in
416 * a merged shader.
417 *
418 * Execute the barrier inside the conditional block,
419 * so that empty waves can jump directly to s_endpgm,
420 * which will also signal the barrier.
421 *
422 * This is possible in gfx9, because an empty wave
423 * for the second shader does not participate in
424 * the epilogue. With NGG, empty waves may still
425 * be required to export data (e.g. GS output vertices),
426 * so we cannot let them exit early.
427 *
428 * If the shader is TCS and the TCS epilog is present
429 * and contains a barrier, it will wait there and then
430 * reach s_endpgm.
431 */
432 ac_build_waitcnt(&ctx.ac, AC_WAIT_LGKM);
433 ac_build_s_barrier(&ctx.ac, shaders[shader_idx]->info.stage);
434 }
435
436 nir_foreach_shader_out_variable (variable, shaders[shader_idx])
437 scan_shader_output_decl(&ctx, variable, shaders[shader_idx], shaders[shader_idx]->info.stage);
438
439 bool check_merged_wave_info = shader_count >= 2 && !(is_ngg && shader_idx == 1);
440 LLVMBasicBlockRef merge_block = NULL;
441
442 if (check_merged_wave_info) {
443 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
444 LLVMBasicBlockRef then_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
445 merge_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
446
447 LLVMValueRef count =
448 ac_unpack_param(&ctx.ac, ac_get_arg(&ctx.ac, args->ac.merged_wave_info), 8 * shader_idx, 8);
449 LLVMValueRef thread_id = ac_get_thread_id(&ctx.ac);
450 LLVMValueRef cond = LLVMBuildICmp(ctx.ac.builder, LLVMIntULT, thread_id, count, "");
451 LLVMBuildCondBr(ctx.ac.builder, cond, then_block, merge_block);
452
453 LLVMPositionBuilderAtEnd(ctx.ac.builder, then_block);
454 }
455
456 if (shaders[shader_idx]->info.stage == MESA_SHADER_GEOMETRY && !info->is_ngg)
457 prepare_gs_input_vgprs(&ctx, shader_count >= 2);
458
459 if (!ac_nir_translate(&ctx.ac, &ctx.abi, &args->ac, shaders[shader_idx])) {
460 abort();
461 }
462
463 if (check_merged_wave_info) {
464 LLVMBuildBr(ctx.ac.builder, merge_block);
465 LLVMPositionBuilderAtEnd(ctx.ac.builder, merge_block);
466 }
467 }
468
469 LLVMBuildRetVoid(ctx.ac.builder);
470
471 if (options->dump_preoptir) {
472 fprintf(stderr, "%s LLVM IR:\n\n", radv_get_shader_name(info, shaders[shader_count - 1]->info.stage));
473 ac_dump_module(ctx.ac.module);
474 fprintf(stderr, "\n");
475 }
476
477 ac_llvm_finalize_module(&ctx, ac_llvm->passmgr);
478
479 free(name);
480
481 return ctx.ac.module;
482 }
483
484 static void
ac_diagnostic_handler(LLVMDiagnosticInfoRef di,void * context)485 ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
486 {
487 unsigned *retval = (unsigned *)context;
488 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
489 char *description = LLVMGetDiagInfoDescription(di);
490
491 if (severity == LLVMDSError) {
492 *retval = 1;
493 fprintf(stderr, "LLVM triggered Diagnostic Handler: %s\n", description);
494 }
495
496 LLVMDisposeMessage(description);
497 }
498
499 static unsigned
radv_llvm_compile(LLVMModuleRef M,char ** pelf_buffer,size_t * pelf_size,struct ac_llvm_compiler * ac_llvm)500 radv_llvm_compile(LLVMModuleRef M, char **pelf_buffer, size_t *pelf_size, struct ac_llvm_compiler *ac_llvm)
501 {
502 unsigned retval = 0;
503 LLVMContextRef llvm_ctx;
504
505 /* Setup Diagnostic Handler*/
506 llvm_ctx = LLVMGetModuleContext(M);
507
508 LLVMContextSetDiagnosticHandler(llvm_ctx, ac_diagnostic_handler, &retval);
509
510 /* Compile IR*/
511 if (!radv_compile_to_elf(ac_llvm, M, pelf_buffer, pelf_size))
512 retval = 1;
513 return retval;
514 }
515
516 static void
ac_compile_llvm_module(struct ac_llvm_compiler * ac_llvm,LLVMModuleRef llvm_module,struct radv_shader_binary ** rbinary,const char * name,const struct radv_nir_compiler_options * options)517 ac_compile_llvm_module(struct ac_llvm_compiler *ac_llvm, LLVMModuleRef llvm_module, struct radv_shader_binary **rbinary,
518 const char *name, const struct radv_nir_compiler_options *options)
519 {
520 char *elf_buffer = NULL;
521 size_t elf_size = 0;
522 char *llvm_ir_string = NULL;
523
524 if (options->dump_shader) {
525 fprintf(stderr, "%s LLVM IR:\n\n", name);
526 ac_dump_module(llvm_module);
527 fprintf(stderr, "\n");
528 }
529
530 if (options->record_ir) {
531 char *llvm_ir = LLVMPrintModuleToString(llvm_module);
532 llvm_ir_string = strdup(llvm_ir);
533 LLVMDisposeMessage(llvm_ir);
534 }
535
536 int v = radv_llvm_compile(llvm_module, &elf_buffer, &elf_size, ac_llvm);
537 if (v) {
538 fprintf(stderr, "compile failed\n");
539 }
540
541 LLVMContextRef ctx = LLVMGetModuleContext(llvm_module);
542 LLVMDisposeModule(llvm_module);
543 LLVMContextDispose(ctx);
544
545 size_t llvm_ir_size = llvm_ir_string ? strlen(llvm_ir_string) : 0;
546 size_t alloc_size = sizeof(struct radv_shader_binary_rtld) + elf_size + llvm_ir_size + 1;
547 struct radv_shader_binary_rtld *rbin = calloc(1, alloc_size);
548 memcpy(rbin->data, elf_buffer, elf_size);
549 if (llvm_ir_string)
550 memcpy(rbin->data + elf_size, llvm_ir_string, llvm_ir_size + 1);
551
552 rbin->base.type = RADV_BINARY_TYPE_RTLD;
553 rbin->base.total_size = alloc_size;
554 rbin->elf_size = elf_size;
555 rbin->llvm_ir_size = llvm_ir_size;
556 *rbinary = &rbin->base;
557
558 free(llvm_ir_string);
559 free(elf_buffer);
560 }
561
562 static void
radv_compile_nir_shader(struct ac_llvm_compiler * ac_llvm,const struct radv_nir_compiler_options * options,const struct radv_shader_info * info,struct radv_shader_binary ** rbinary,const struct radv_shader_args * args,struct nir_shader * const * nir,int nir_count)563 radv_compile_nir_shader(struct ac_llvm_compiler *ac_llvm, const struct radv_nir_compiler_options *options,
564 const struct radv_shader_info *info, struct radv_shader_binary **rbinary,
565 const struct radv_shader_args *args, struct nir_shader *const *nir, int nir_count)
566 {
567
568 LLVMModuleRef llvm_module;
569
570 llvm_module = ac_translate_nir_to_llvm(ac_llvm, options, info, nir, nir_count, args);
571
572 ac_compile_llvm_module(ac_llvm, llvm_module, rbinary, radv_get_shader_name(info, nir[nir_count - 1]->info.stage),
573 options);
574 }
575
576 void
llvm_compile_shader(const struct radv_nir_compiler_options * options,const struct radv_shader_info * info,unsigned shader_count,struct nir_shader * const * shaders,struct radv_shader_binary ** binary,const struct radv_shader_args * args)577 llvm_compile_shader(const struct radv_nir_compiler_options *options, const struct radv_shader_info *info,
578 unsigned shader_count, struct nir_shader *const *shaders, struct radv_shader_binary **binary,
579 const struct radv_shader_args *args)
580 {
581 enum ac_target_machine_options tm_options = 0;
582 struct ac_llvm_compiler ac_llvm;
583
584 tm_options |= AC_TM_SUPPORTS_SPILL;
585 if (options->check_ir)
586 tm_options |= AC_TM_CHECK_IR;
587
588 radv_init_llvm_compiler(&ac_llvm, options->info->family, tm_options, info->wave_size);
589
590 radv_compile_nir_shader(&ac_llvm, options, info, binary, args, shaders, shader_count);
591 }
592