[Mesa-dev] [PATCH 1/2] nir: add nir_opt_move_load_ubo() optimization pass
Samuel Pitoiset
samuel.pitoiset at gmail.com
Mon Mar 12 10:18:28 UTC 2018
On 03/11/2018 04:41 PM, Marek Olšák wrote:
> On Thu, Mar 8, 2018 at 5:48 PM, Ian Romanick <idr at freedesktop.org> wrote:
>> On 03/08/2018 06:50 AM, Samuel Pitoiset wrote:
>>> This pass moves load UBO operations just before their first use,
>>> loosely based on nir_opt_move_comparisons.
>>
>> If I'm reading this correctly, it moves UBO loads closer to the first
>> use in the same block. My assumption is the benefit in the next patch
>> occurs because live ranges are smaller. It seems like this could also
>> hurt performance since it may be harder for the schedule to hide the
>> latency of the load when register pressure is not an issue. Have you
>> measured performance of running apps to see if this is an issue?
>>
>> I'm mostly asking because Jason had a series for global code motion that
>> does, in some cases, the opposite of this patch by moving UBO loads up
>> to earlier blocks.
>
> The pass is OK for LLVM, because LLVM does CSE across basic blocks,
> and it also does instruction scheduling within a block.
>
> radeonsi/tgsi does the same thing: it load uniforms from memory at
> every use. It sounds inefficient, but we found out that it's the best
> thing to do with LLVM. LLVM can move loads away from the use, but it
> doesn't move loads close to the use.
Exactly, RadeonSI does something similar. Though the shader-db result
posted by Timothy doesn't look very good for NIR, what do you think?
>
> Marek
>
>>
>>> Signed-off-by: Samuel Pitoiset <samuel.pitoiset at gmail.com>
>>> ---
>>> src/compiler/Makefile.sources | 1 +
>>> src/compiler/nir/meson.build | 1 +
>>> src/compiler/nir/nir.h | 2 +
>>> src/compiler/nir/nir_opt_move_load_ubo.c | 116 +++++++++++++++++++++++++++++++
>>> 4 files changed, 120 insertions(+)
>>> create mode 100644 src/compiler/nir/nir_opt_move_load_ubo.c
>>>
>>> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
>>> index 37340ba809..55143dbc66 100644
>>> --- a/src/compiler/Makefile.sources
>>> +++ b/src/compiler/Makefile.sources
>>> @@ -266,6 +266,7 @@ NIR_FILES = \
>>> nir/nir_opt_intrinsics.c \
>>> nir/nir_opt_loop_unroll.c \
>>> nir/nir_opt_move_comparisons.c \
>>> + nir/nir_opt_move_load_ubo.c \
>>> nir/nir_opt_peephole_select.c \
>>> nir/nir_opt_remove_phis.c \
>>> nir/nir_opt_shrink_load.c \
>>> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build
>>> index a70c236b95..289bb9ea78 100644
>>> --- a/src/compiler/nir/meson.build
>>> +++ b/src/compiler/nir/meson.build
>>> @@ -160,6 +160,7 @@ files_libnir = files(
>>> 'nir_opt_intrinsics.c',
>>> 'nir_opt_loop_unroll.c',
>>> 'nir_opt_move_comparisons.c',
>>> + 'nir_opt_move_load_ubo.c',
>>> 'nir_opt_peephole_select.c',
>>> 'nir_opt_remove_phis.c',
>>> 'nir_opt_shrink_load.c',
>>> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
>>> index 5b28c727c8..4224da5f82 100644
>>> --- a/src/compiler/nir/nir.h
>>> +++ b/src/compiler/nir/nir.h
>>> @@ -2786,6 +2786,8 @@ bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask);
>>>
>>> bool nir_opt_move_comparisons(nir_shader *shader);
>>>
>>> +bool nir_opt_move_load_ubo(nir_shader *shader);
>>> +
>>> bool nir_opt_peephole_select(nir_shader *shader, unsigned limit);
>>>
>>> bool nir_opt_remove_phis(nir_shader *shader);
>>> diff --git a/src/compiler/nir/nir_opt_move_load_ubo.c b/src/compiler/nir/nir_opt_move_load_ubo.c
>>> new file mode 100644
>>> index 0000000000..642651152b
>>> --- /dev/null
>>> +++ b/src/compiler/nir/nir_opt_move_load_ubo.c
>>> @@ -0,0 +1,116 @@
>>> +/*
>>> + * Copyright © 2016 Intel Corporation
>>> + * Copyright © 2018 Valve Corporation
>>> + *
>>> + * 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.
>>> + */
>>> +
>>> +#include "nir.h"
>>> +
>>> +/**
>>> + * \file nir_opt_move_load_ubo.c
>>> + *
>>> + * This pass moves load UBO operations just before their first use.
>>> + */
>>> +static bool
>>> +move_load_ubo_source(nir_src *src, nir_block *block, nir_instr *before)
>>> +{
>>> + if (!src->is_ssa)
>>> + return false;
>>> +
>>> + nir_instr *src_instr = src->ssa->parent_instr;
>>> +
>>> + if (src_instr->block == block &&
>>> + src_instr->type == nir_instr_type_intrinsic &&
>>> + nir_instr_as_intrinsic(src_instr)->intrinsic == nir_intrinsic_load_ubo) {
>>> +
>>> + exec_node_remove(&src_instr->node);
>>> +
>>> + if (before)
>>> + exec_node_insert_node_before(&before->node, &src_instr->node);
>>> + else
>>> + exec_list_push_tail(&block->instr_list, &src_instr->node);
>>> +
>>> + return true;
>>> + }
>>> + return false;
>>> +}
>>> +
>>> +static bool
>>> +move_load_ubo_source_cb(nir_src *src, void *data)
>>> +{
>>> + bool *progress = data;
>>> +
>>> + nir_instr *instr = src->parent_instr;
>>> + if (move_load_ubo_source(src, instr->block, instr))
>>> + *progress = true;
>>> +
>>> + return true; /* nir_foreach_src should keep going */
>>> +}
>>> +
>>> +static bool
>>> +move_load_ubo(nir_block *block)
>>> +{
>>> + bool progress = false;
>>> +
>>> + nir_if *iff = nir_block_get_following_if(block);
>>> + if (iff) {
>>> + progress |= move_load_ubo_source(&iff->condition, block, NULL);
>>> + }
>>> +
>>> + nir_foreach_instr_reverse(instr, block) {
>>> +
>>> + if (instr->type == nir_instr_type_phi) {
>>> + /* We're going backwards so everything else is a phi too */
>>> + } else if (instr->type == nir_instr_type_alu) {
>>> + nir_alu_instr *alu = nir_instr_as_alu(instr);
>>> +
>>> + for (int i = nir_op_infos[alu->op].num_inputs - 1; i >= 0; i--) {
>>> + progress |= move_load_ubo_source(&alu->src[i].src, block, instr);
>>> + }
>>> + } else {
>>> + nir_foreach_src(instr, move_load_ubo_source_cb, &progress);
>>> + }
>>> + }
>>> +
>>> + return false;
>>> +}
>>> +
>>> +bool
>>> +nir_opt_move_load_ubo(nir_shader *shader)
>>> +{
>>> + bool progress = false;
>>> +
>>> + nir_foreach_function(func, shader) {
>>> + if (!func->impl)
>>> + continue;
>>> +
>>> + nir_foreach_block(block, func->impl) {
>>> + if (move_load_ubo(block)) {
>>> + nir_metadata_preserve(func->impl, nir_metadata_block_index |
>>> + nir_metadata_dominance |
>>> + nir_metadata_live_ssa_defs);
>>> + progress = true;
>>> + }
>>> + }
>>> + }
>>> +
>>> + return progress;
>>> +}
>>>
>>
>> _______________________________________________
>> mesa-dev mailing list
>> mesa-dev at lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
More information about the mesa-dev
mailing list