1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015-2016 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "brw_compiler.h" 25bf215546Sopenharmony_ci#include "brw_shader.h" 26bf215546Sopenharmony_ci#include "brw_eu.h" 27bf215546Sopenharmony_ci#include "dev/intel_debug.h" 28bf215546Sopenharmony_ci#include "compiler/nir/nir.h" 29bf215546Sopenharmony_ci#include "main/errors.h" 30bf215546Sopenharmony_ci#include "util/debug.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#define COMMON_OPTIONS \ 33bf215546Sopenharmony_ci .lower_fdiv = true, \ 34bf215546Sopenharmony_ci .lower_scmp = true, \ 35bf215546Sopenharmony_ci .lower_flrp16 = true, \ 36bf215546Sopenharmony_ci .lower_fmod = true, \ 37bf215546Sopenharmony_ci .lower_bitfield_extract = true, \ 38bf215546Sopenharmony_ci .lower_bitfield_insert = true, \ 39bf215546Sopenharmony_ci .lower_uadd_carry = true, \ 40bf215546Sopenharmony_ci .lower_usub_borrow = true, \ 41bf215546Sopenharmony_ci .lower_flrp64 = true, \ 42bf215546Sopenharmony_ci .lower_fisnormal = true, \ 43bf215546Sopenharmony_ci .lower_isign = true, \ 44bf215546Sopenharmony_ci .lower_ldexp = true, \ 45bf215546Sopenharmony_ci .lower_device_index_to_zero = true, \ 46bf215546Sopenharmony_ci .vectorize_io = true, \ 47bf215546Sopenharmony_ci .use_interpolated_input_intrinsics = true, \ 48bf215546Sopenharmony_ci .lower_insert_byte = true, \ 49bf215546Sopenharmony_ci .lower_insert_word = true, \ 50bf215546Sopenharmony_ci .vertex_id_zero_based = true, \ 51bf215546Sopenharmony_ci .lower_base_vertex = true, \ 52bf215546Sopenharmony_ci .use_scoped_barrier = true, \ 53bf215546Sopenharmony_ci .support_16bit_alu = true, \ 54bf215546Sopenharmony_ci .lower_uniforms_to_ubo = true, \ 55bf215546Sopenharmony_ci .has_txs = true 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci#define COMMON_SCALAR_OPTIONS \ 58bf215546Sopenharmony_ci .lower_to_scalar = true, \ 59bf215546Sopenharmony_ci .lower_pack_half_2x16 = true, \ 60bf215546Sopenharmony_ci .lower_pack_snorm_2x16 = true, \ 61bf215546Sopenharmony_ci .lower_pack_snorm_4x8 = true, \ 62bf215546Sopenharmony_ci .lower_pack_unorm_2x16 = true, \ 63bf215546Sopenharmony_ci .lower_pack_unorm_4x8 = true, \ 64bf215546Sopenharmony_ci .lower_unpack_half_2x16 = true, \ 65bf215546Sopenharmony_ci .lower_unpack_snorm_2x16 = true, \ 66bf215546Sopenharmony_ci .lower_unpack_snorm_4x8 = true, \ 67bf215546Sopenharmony_ci .lower_unpack_unorm_2x16 = true, \ 68bf215546Sopenharmony_ci .lower_unpack_unorm_4x8 = true, \ 69bf215546Sopenharmony_ci .lower_hadd64 = true, \ 70bf215546Sopenharmony_ci .avoid_ternary_with_two_constants = true, \ 71bf215546Sopenharmony_ci .has_pack_32_4x8 = true, \ 72bf215546Sopenharmony_ci .max_unroll_iterations = 32, \ 73bf215546Sopenharmony_ci .force_indirect_unrolling = nir_var_function_temp, \ 74bf215546Sopenharmony_ci .divergence_analysis_options = \ 75bf215546Sopenharmony_ci (nir_divergence_single_prim_per_subgroup | \ 76bf215546Sopenharmony_ci nir_divergence_single_patch_per_tcs_subgroup | \ 77bf215546Sopenharmony_ci nir_divergence_single_patch_per_tes_subgroup) 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistatic const struct nir_shader_compiler_options scalar_nir_options = { 80bf215546Sopenharmony_ci COMMON_OPTIONS, 81bf215546Sopenharmony_ci COMMON_SCALAR_OPTIONS, 82bf215546Sopenharmony_ci}; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_cistatic const struct nir_shader_compiler_options vector_nir_options = { 85bf215546Sopenharmony_ci COMMON_OPTIONS, 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci /* In the vec4 backend, our dpN instruction replicates its result to all the 88bf215546Sopenharmony_ci * components of a vec4. We would like NIR to give us replicated fdot 89bf215546Sopenharmony_ci * instructions because it can optimize better for us. 90bf215546Sopenharmony_ci */ 91bf215546Sopenharmony_ci .fdot_replicates = true, 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci .lower_usub_sat = true, 94bf215546Sopenharmony_ci .lower_pack_snorm_2x16 = true, 95bf215546Sopenharmony_ci .lower_pack_unorm_2x16 = true, 96bf215546Sopenharmony_ci .lower_unpack_snorm_2x16 = true, 97bf215546Sopenharmony_ci .lower_unpack_unorm_2x16 = true, 98bf215546Sopenharmony_ci .lower_extract_byte = true, 99bf215546Sopenharmony_ci .lower_extract_word = true, 100bf215546Sopenharmony_ci .intel_vec4 = true, 101bf215546Sopenharmony_ci .max_unroll_iterations = 32, 102bf215546Sopenharmony_ci}; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_cistruct brw_compiler * 105bf215546Sopenharmony_cibrw_compiler_create(void *mem_ctx, const struct intel_device_info *devinfo) 106bf215546Sopenharmony_ci{ 107bf215546Sopenharmony_ci struct brw_compiler *compiler = rzalloc(mem_ctx, struct brw_compiler); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci compiler->devinfo = devinfo; 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci brw_init_isa_info(&compiler->isa, devinfo); 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci brw_fs_alloc_reg_sets(compiler); 114bf215546Sopenharmony_ci if (devinfo->ver < 8) 115bf215546Sopenharmony_ci brw_vec4_alloc_reg_set(compiler); 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci compiler->precise_trig = env_var_as_boolean("INTEL_PRECISE_TRIG", false); 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci compiler->use_tcs_8_patch = 120bf215546Sopenharmony_ci devinfo->ver >= 12 || 121bf215546Sopenharmony_ci (devinfo->ver >= 9 && INTEL_DEBUG(DEBUG_TCS_EIGHT_PATCH)); 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci /* Default to the sampler since that's what we've done since forever */ 124bf215546Sopenharmony_ci compiler->indirect_ubos_use_sampler = true; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci /* There is no vec4 mode on Gfx10+, and we don't use it at all on Gfx8+. */ 127bf215546Sopenharmony_ci for (int i = MESA_SHADER_VERTEX; i < MESA_ALL_SHADER_STAGES; i++) { 128bf215546Sopenharmony_ci compiler->scalar_stage[i] = devinfo->ver >= 8 || 129bf215546Sopenharmony_ci i == MESA_SHADER_FRAGMENT || i == MESA_SHADER_COMPUTE; 130bf215546Sopenharmony_ci } 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci for (int i = MESA_SHADER_TASK; i < MESA_VULKAN_SHADER_STAGES; i++) 133bf215546Sopenharmony_ci compiler->scalar_stage[i] = true; 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci nir_lower_int64_options int64_options = 136bf215546Sopenharmony_ci nir_lower_imul64 | 137bf215546Sopenharmony_ci nir_lower_isign64 | 138bf215546Sopenharmony_ci nir_lower_divmod64 | 139bf215546Sopenharmony_ci nir_lower_imul_high64; 140bf215546Sopenharmony_ci nir_lower_doubles_options fp64_options = 141bf215546Sopenharmony_ci nir_lower_drcp | 142bf215546Sopenharmony_ci nir_lower_dsqrt | 143bf215546Sopenharmony_ci nir_lower_drsq | 144bf215546Sopenharmony_ci nir_lower_dtrunc | 145bf215546Sopenharmony_ci nir_lower_dfloor | 146bf215546Sopenharmony_ci nir_lower_dceil | 147bf215546Sopenharmony_ci nir_lower_dfract | 148bf215546Sopenharmony_ci nir_lower_dround_even | 149bf215546Sopenharmony_ci nir_lower_dmod | 150bf215546Sopenharmony_ci nir_lower_dsub | 151bf215546Sopenharmony_ci nir_lower_ddiv; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci if (!devinfo->has_64bit_float || INTEL_DEBUG(DEBUG_SOFT64)) 154bf215546Sopenharmony_ci fp64_options |= nir_lower_fp64_full_software; 155bf215546Sopenharmony_ci if (!devinfo->has_64bit_int) 156bf215546Sopenharmony_ci int64_options |= (nir_lower_int64_options)~0; 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ci /* The Bspec's section titled "Instruction_multiply[DevBDW+]" claims that 159bf215546Sopenharmony_ci * destination type can be Quadword and source type Doubleword for Gfx8 and 160bf215546Sopenharmony_ci * Gfx9. So, lower 64 bit multiply instruction on rest of the platforms. 161bf215546Sopenharmony_ci */ 162bf215546Sopenharmony_ci if (devinfo->ver < 8 || devinfo->ver > 9) 163bf215546Sopenharmony_ci int64_options |= nir_lower_imul_2x32_64; 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci /* We want the GLSL compiler to emit code that uses condition codes */ 166bf215546Sopenharmony_ci for (int i = 0; i < MESA_ALL_SHADER_STAGES; i++) { 167bf215546Sopenharmony_ci struct nir_shader_compiler_options *nir_options = 168bf215546Sopenharmony_ci rzalloc(compiler, struct nir_shader_compiler_options); 169bf215546Sopenharmony_ci bool is_scalar = compiler->scalar_stage[i]; 170bf215546Sopenharmony_ci if (is_scalar) { 171bf215546Sopenharmony_ci *nir_options = scalar_nir_options; 172bf215546Sopenharmony_ci int64_options |= nir_lower_usub_sat64; 173bf215546Sopenharmony_ci } else { 174bf215546Sopenharmony_ci *nir_options = vector_nir_options; 175bf215546Sopenharmony_ci } 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci /* Prior to Gfx6, there are no three source operations, and Gfx11 loses 178bf215546Sopenharmony_ci * LRP. 179bf215546Sopenharmony_ci */ 180bf215546Sopenharmony_ci nir_options->lower_ffma16 = devinfo->ver < 6; 181bf215546Sopenharmony_ci nir_options->lower_ffma32 = devinfo->ver < 6; 182bf215546Sopenharmony_ci nir_options->lower_ffma64 = devinfo->ver < 6; 183bf215546Sopenharmony_ci nir_options->lower_flrp32 = devinfo->ver < 6 || devinfo->ver >= 11; 184bf215546Sopenharmony_ci nir_options->lower_fpow = devinfo->ver >= 12; 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci nir_options->lower_rotate = devinfo->ver < 11; 187bf215546Sopenharmony_ci nir_options->lower_bitfield_reverse = devinfo->ver < 7; 188bf215546Sopenharmony_ci nir_options->has_iadd3 = devinfo->verx10 >= 125; 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci nir_options->has_sdot_4x8 = devinfo->ver >= 12; 191bf215546Sopenharmony_ci nir_options->has_udot_4x8 = devinfo->ver >= 12; 192bf215546Sopenharmony_ci nir_options->has_sudot_4x8 = devinfo->ver >= 12; 193bf215546Sopenharmony_ci 194bf215546Sopenharmony_ci nir_options->lower_int64_options = int64_options; 195bf215546Sopenharmony_ci nir_options->lower_doubles_options = fp64_options; 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci nir_options->unify_interfaces = i < MESA_SHADER_FRAGMENT; 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci nir_options->force_indirect_unrolling |= 200bf215546Sopenharmony_ci brw_nir_no_indirect_mask(compiler, i); 201bf215546Sopenharmony_ci nir_options->force_indirect_unrolling_sampler = devinfo->ver < 7; 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci if (compiler->use_tcs_8_patch) { 204bf215546Sopenharmony_ci /* TCS 8_PATCH mode has multiple patches per subgroup */ 205bf215546Sopenharmony_ci nir_options->divergence_analysis_options &= 206bf215546Sopenharmony_ci ~nir_divergence_single_patch_per_tcs_subgroup; 207bf215546Sopenharmony_ci } 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci compiler->nir_options[i] = nir_options; 210bf215546Sopenharmony_ci } 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci return compiler; 213bf215546Sopenharmony_ci} 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_cistatic void 216bf215546Sopenharmony_ciinsert_u64_bit(uint64_t *val, bool add) 217bf215546Sopenharmony_ci{ 218bf215546Sopenharmony_ci *val = (*val << 1) | !!add; 219bf215546Sopenharmony_ci} 220bf215546Sopenharmony_ci 221bf215546Sopenharmony_ciuint64_t 222bf215546Sopenharmony_cibrw_get_compiler_config_value(const struct brw_compiler *compiler) 223bf215546Sopenharmony_ci{ 224bf215546Sopenharmony_ci uint64_t config = 0; 225bf215546Sopenharmony_ci insert_u64_bit(&config, compiler->precise_trig); 226bf215546Sopenharmony_ci 227bf215546Sopenharmony_ci uint64_t mask = DEBUG_DISK_CACHE_MASK; 228bf215546Sopenharmony_ci while (mask != 0) { 229bf215546Sopenharmony_ci const uint64_t bit = 1ULL << (ffsll(mask) - 1); 230bf215546Sopenharmony_ci insert_u64_bit(&config, INTEL_DEBUG(bit)); 231bf215546Sopenharmony_ci mask &= ~bit; 232bf215546Sopenharmony_ci } 233bf215546Sopenharmony_ci return config; 234bf215546Sopenharmony_ci} 235bf215546Sopenharmony_ci 236bf215546Sopenharmony_ciunsigned 237bf215546Sopenharmony_cibrw_prog_data_size(gl_shader_stage stage) 238bf215546Sopenharmony_ci{ 239bf215546Sopenharmony_ci static const size_t stage_sizes[] = { 240bf215546Sopenharmony_ci [MESA_SHADER_VERTEX] = sizeof(struct brw_vs_prog_data), 241bf215546Sopenharmony_ci [MESA_SHADER_TESS_CTRL] = sizeof(struct brw_tcs_prog_data), 242bf215546Sopenharmony_ci [MESA_SHADER_TESS_EVAL] = sizeof(struct brw_tes_prog_data), 243bf215546Sopenharmony_ci [MESA_SHADER_GEOMETRY] = sizeof(struct brw_gs_prog_data), 244bf215546Sopenharmony_ci [MESA_SHADER_FRAGMENT] = sizeof(struct brw_wm_prog_data), 245bf215546Sopenharmony_ci [MESA_SHADER_COMPUTE] = sizeof(struct brw_cs_prog_data), 246bf215546Sopenharmony_ci [MESA_SHADER_TASK] = sizeof(struct brw_task_prog_data), 247bf215546Sopenharmony_ci [MESA_SHADER_MESH] = sizeof(struct brw_mesh_prog_data), 248bf215546Sopenharmony_ci [MESA_SHADER_RAYGEN] = sizeof(struct brw_bs_prog_data), 249bf215546Sopenharmony_ci [MESA_SHADER_ANY_HIT] = sizeof(struct brw_bs_prog_data), 250bf215546Sopenharmony_ci [MESA_SHADER_CLOSEST_HIT] = sizeof(struct brw_bs_prog_data), 251bf215546Sopenharmony_ci [MESA_SHADER_MISS] = sizeof(struct brw_bs_prog_data), 252bf215546Sopenharmony_ci [MESA_SHADER_INTERSECTION] = sizeof(struct brw_bs_prog_data), 253bf215546Sopenharmony_ci [MESA_SHADER_CALLABLE] = sizeof(struct brw_bs_prog_data), 254bf215546Sopenharmony_ci [MESA_SHADER_KERNEL] = sizeof(struct brw_cs_prog_data), 255bf215546Sopenharmony_ci }; 256bf215546Sopenharmony_ci assert((int)stage >= 0 && stage < ARRAY_SIZE(stage_sizes)); 257bf215546Sopenharmony_ci return stage_sizes[stage]; 258bf215546Sopenharmony_ci} 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_ciunsigned 261bf215546Sopenharmony_cibrw_prog_key_size(gl_shader_stage stage) 262bf215546Sopenharmony_ci{ 263bf215546Sopenharmony_ci static const size_t stage_sizes[] = { 264bf215546Sopenharmony_ci [MESA_SHADER_VERTEX] = sizeof(struct brw_vs_prog_key), 265bf215546Sopenharmony_ci [MESA_SHADER_TESS_CTRL] = sizeof(struct brw_tcs_prog_key), 266bf215546Sopenharmony_ci [MESA_SHADER_TESS_EVAL] = sizeof(struct brw_tes_prog_key), 267bf215546Sopenharmony_ci [MESA_SHADER_GEOMETRY] = sizeof(struct brw_gs_prog_key), 268bf215546Sopenharmony_ci [MESA_SHADER_FRAGMENT] = sizeof(struct brw_wm_prog_key), 269bf215546Sopenharmony_ci [MESA_SHADER_COMPUTE] = sizeof(struct brw_cs_prog_key), 270bf215546Sopenharmony_ci [MESA_SHADER_TASK] = sizeof(struct brw_task_prog_key), 271bf215546Sopenharmony_ci [MESA_SHADER_MESH] = sizeof(struct brw_mesh_prog_key), 272bf215546Sopenharmony_ci [MESA_SHADER_RAYGEN] = sizeof(struct brw_bs_prog_key), 273bf215546Sopenharmony_ci [MESA_SHADER_ANY_HIT] = sizeof(struct brw_bs_prog_key), 274bf215546Sopenharmony_ci [MESA_SHADER_CLOSEST_HIT] = sizeof(struct brw_bs_prog_key), 275bf215546Sopenharmony_ci [MESA_SHADER_MISS] = sizeof(struct brw_bs_prog_key), 276bf215546Sopenharmony_ci [MESA_SHADER_INTERSECTION] = sizeof(struct brw_bs_prog_key), 277bf215546Sopenharmony_ci [MESA_SHADER_CALLABLE] = sizeof(struct brw_bs_prog_key), 278bf215546Sopenharmony_ci [MESA_SHADER_KERNEL] = sizeof(struct brw_cs_prog_key), 279bf215546Sopenharmony_ci }; 280bf215546Sopenharmony_ci assert((int)stage >= 0 && stage < ARRAY_SIZE(stage_sizes)); 281bf215546Sopenharmony_ci return stage_sizes[stage]; 282bf215546Sopenharmony_ci} 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_civoid 285bf215546Sopenharmony_cibrw_write_shader_relocs(const struct brw_isa_info *isa, 286bf215546Sopenharmony_ci void *program, 287bf215546Sopenharmony_ci const struct brw_stage_prog_data *prog_data, 288bf215546Sopenharmony_ci struct brw_shader_reloc_value *values, 289bf215546Sopenharmony_ci unsigned num_values) 290bf215546Sopenharmony_ci{ 291bf215546Sopenharmony_ci for (unsigned i = 0; i < prog_data->num_relocs; i++) { 292bf215546Sopenharmony_ci assert(prog_data->relocs[i].offset % 8 == 0); 293bf215546Sopenharmony_ci void *dst = program + prog_data->relocs[i].offset; 294bf215546Sopenharmony_ci for (unsigned j = 0; j < num_values; j++) { 295bf215546Sopenharmony_ci if (prog_data->relocs[i].id == values[j].id) { 296bf215546Sopenharmony_ci uint32_t value = values[j].value + prog_data->relocs[i].delta; 297bf215546Sopenharmony_ci switch (prog_data->relocs[i].type) { 298bf215546Sopenharmony_ci case BRW_SHADER_RELOC_TYPE_U32: 299bf215546Sopenharmony_ci *(uint32_t *)dst = value; 300bf215546Sopenharmony_ci break; 301bf215546Sopenharmony_ci case BRW_SHADER_RELOC_TYPE_MOV_IMM: 302bf215546Sopenharmony_ci brw_update_reloc_imm(isa, dst, value); 303bf215546Sopenharmony_ci break; 304bf215546Sopenharmony_ci default: 305bf215546Sopenharmony_ci unreachable("Invalid relocation type"); 306bf215546Sopenharmony_ci } 307bf215546Sopenharmony_ci break; 308bf215546Sopenharmony_ci } 309bf215546Sopenharmony_ci } 310bf215546Sopenharmony_ci } 311bf215546Sopenharmony_ci} 312