From: Alyssa Rosenzweig Date: Mon, 4 Nov 2019 22:27:18 +0000 (-0500) Subject: nir: Add SSBO->global lowering pass X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7ab4e4dd963028a4620ffc00c38988da4abc1860;p=mesa.git nir: Add SSBO->global lowering pass To facilitate lowering SSBOs to globals, we need a load_ssbo_address intrinsic. This intrinsic takes an SSBO index and loads the address in global memory of the SSBO (likely implemented via a uniform in the driver). In the future, we'll support bounds checking, but at the moment this is not supported (this pass should only be used for trusted contexts at the moment, i.e. contexts without robustness extensions). Signed-off-by: Alyssa Rosenzweig Reviewed-by: Karol Herbst Part-of: --- diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index a21d6d25ae3..6f9ae8f24b0 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -277,6 +277,7 @@ NIR_FILES = \ nir/nir_lower_returns.c \ nir/nir_lower_samplers.c \ nir/nir_lower_scratch.c \ + nir/nir_lower_ssbo.c \ nir/nir_lower_subgroups.c \ nir/nir_lower_system_values.c \ nir/nir_lower_tex.c \ diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build index 8101dc8c870..6ffb948f049 100644 --- a/src/compiler/nir/meson.build +++ b/src/compiler/nir/meson.build @@ -158,6 +158,7 @@ files_libnir = files( 'nir_lower_returns.c', 'nir_lower_samplers.c', 'nir_lower_scratch.c', + 'nir_lower_ssbo.c', 'nir_lower_subgroups.c', 'nir_lower_system_values.c', 'nir_lower_tex.c', diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 4939240653b..dd3c3cafe8f 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -4218,6 +4218,7 @@ bool nir_lower_ssa_defs_to_regs_block(nir_block *block); bool nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl); bool nir_lower_samplers(nir_shader *shader); +bool nir_lower_ssbo(nir_shader *shader); /* This is here for unit tests. */ bool nir_opt_comparison_pre_impl(nir_function_impl *impl); diff --git a/src/compiler/nir/nir_intrinsics.py b/src/compiler/nir/nir_intrinsics.py index 33012d4bb01..60f51339357 100644 --- a/src/compiler/nir/nir_intrinsics.py +++ b/src/compiler/nir/nir_intrinsics.py @@ -736,6 +736,8 @@ intrinsic("load_interpolated_input", src_comp=[2, 1], dest_comp=0, # src[] = { buffer_index, offset }. load("ssbo", 2, [ACCESS, ALIGN_MUL, ALIGN_OFFSET], [CAN_ELIMINATE]) +# src[] = { buffer_index } +load("ssbo_address", 1, [], [CAN_ELIMINATE, CAN_REORDER]) # src[] = { offset }. load("output", 1, [BASE, COMPONENT], flags=[CAN_ELIMINATE]) # src[] = { vertex, offset }. diff --git a/src/compiler/nir/nir_lower_ssbo.c b/src/compiler/nir/nir_lower_ssbo.c new file mode 100644 index 00000000000..ab22317caf3 --- /dev/null +++ b/src/compiler/nir/nir_lower_ssbo.c @@ -0,0 +1,162 @@ +/* + * Copyright © 2019 Collabora, Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors (Collabora): + * Alyssa Rosenzweig + */ + +#include "nir.h" +#include "nir_builder.h" + +/* + * Lowers SSBOs to globals, for hardware that lack native SSBO support. When + * lowering, *_ssbo_* instructions will become *_global_* instructions, + * augmented with load_ssbo_address. + * + * DOES NOT PERFORM BOUNDS CHECKING. DO NOT USE IN PRODUCTION ON UNTRUSTED + * CONTEXTS INCLUDING WEBGL 2. + */ + +static nir_intrinsic_op +lower_ssbo_op(nir_intrinsic_op op) +{ + switch (op) { + case nir_intrinsic_load_ssbo: + return nir_intrinsic_load_global; + + case nir_intrinsic_store_ssbo: + return nir_intrinsic_store_global; + + default: + unreachable("Invalid SSBO op"); + } +} + +/* Like SSBO property sysvals, though SSBO index may be indirect. C.f. + * nir_load_system_value */ + +static inline nir_ssa_def * +nir_load_ssbo_prop(nir_builder *b, nir_intrinsic_op op, + nir_src *idx, unsigned bitsize) +{ + nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op); + load->num_components = 1; + nir_src_copy(&load->src[0], idx, load); + nir_ssa_dest_init(&load->instr, &load->dest, 1, bitsize, NULL); + nir_builder_instr_insert(b, &load->instr); + return &load->dest.ssa; +} + +#define nir_ssbo_prop(b, prop, index, bitsize) \ + nir_load_ssbo_prop(b, nir_intrinsic_##prop, index, bitsize) + +static nir_ssa_def * +lower_ssbo_instr(nir_builder *b, nir_intrinsic_instr *intr) +{ + nir_intrinsic_op op = lower_ssbo_op(intr->intrinsic); + bool is_store = op == nir_intrinsic_store_global; + + /* We have to calculate the address: + * + * &(SSBO[offset]) = &SSBO + offset + */ + + nir_src index = intr->src[is_store ? 1 : 0]; + nir_src *offset_src = nir_get_io_offset_src(intr); + nir_ssa_def *offset = nir_ssa_for_src(b, *offset_src, 1); + + nir_ssa_def *address = + nir_iadd(b, + nir_ssbo_prop(b, load_ssbo_address, &index, 64), + nir_u2u64(b, offset)); + + /* Create the replacement intrinsic */ + + nir_intrinsic_instr *global = + nir_intrinsic_instr_create(b->shader, op); + + global->num_components = intr->num_components; + global->src[is_store ? 1 : 0] = nir_src_for_ssa(address); + + if (is_store) { + nir_src_copy(&global->src[0], &intr->src[0], global); + nir_intrinsic_set_write_mask(global, nir_intrinsic_write_mask(intr)); + } else { + nir_ssa_dest_init(&global->instr, &global->dest, + intr->dest.ssa.num_components, + intr->dest.ssa.bit_size, NULL); + } + + nir_builder_instr_insert(b, &global->instr); + return is_store ? NULL : &global->dest.ssa; +} + +static bool +should_lower_ssbo_instr(const nir_instr *instr) +{ + if (instr->type != nir_instr_type_intrinsic) + return false; + + const nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + + switch (intr->intrinsic) { + case nir_intrinsic_load_ssbo: + case nir_intrinsic_store_ssbo: + return true; + default: + return false; + } + + return false; +} + +bool +nir_lower_ssbo(nir_shader *shader) +{ + bool progress = false; + + nir_foreach_function(function, shader) { + nir_function_impl *impl = function->impl; + nir_builder b; + nir_builder_init(&b, impl); + + nir_foreach_block(block, impl) { + nir_foreach_instr_safe(instr, block) { + if (!should_lower_ssbo_instr(instr)) continue; + progress = true; + b.cursor = nir_before_instr(instr); + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + nir_ssa_def *replace = lower_ssbo_instr(&b, intr); + + if (replace) { + nir_ssa_def_rewrite_uses(&intr->dest.ssa, + nir_src_for_ssa(replace)); + } + + nir_instr_remove(instr); + } + } + } + + return progress; +}