1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation
3bf215546Sopenharmony_ci * Copyright © 2018 Broadcom
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22bf215546Sopenharmony_ci * IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "v3d_compiler.h"
26bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
27bf215546Sopenharmony_ci#include "compiler/nir/nir_format_convert.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/** @file v3d_nir_lower_scratch.c
30bf215546Sopenharmony_ci *
31bf215546Sopenharmony_ci * Swizzles around the addresses of
32bf215546Sopenharmony_ci * nir_intrinsic_load_scratch/nir_intrinsic_store_scratch so that a QPU stores
33bf215546Sopenharmony_ci * a cacheline at a time per dword of scratch access, scalarizing and removing
34bf215546Sopenharmony_ci * writemasks in the process.
35bf215546Sopenharmony_ci */
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic nir_ssa_def *
38bf215546Sopenharmony_civ3d_nir_scratch_offset(nir_builder *b, nir_intrinsic_instr *instr)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci        bool is_store = instr->intrinsic == nir_intrinsic_store_scratch;
41bf215546Sopenharmony_ci        nir_ssa_def *offset = nir_ssa_for_src(b, instr->src[is_store ? 1 : 0], 1);
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci        assert(nir_intrinsic_align_mul(instr) >= 4);
44bf215546Sopenharmony_ci        assert(nir_intrinsic_align_offset(instr) == 0);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci        /* The spill_offset register will already have the subgroup ID (EIDX)
47bf215546Sopenharmony_ci         * shifted and ORed in at bit 2, so all we need to do is to move the
48bf215546Sopenharmony_ci         * dword index up above V3D_CHANNELS.
49bf215546Sopenharmony_ci         */
50bf215546Sopenharmony_ci        return nir_imul_imm(b, offset, V3D_CHANNELS);
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_civ3d_nir_lower_load_scratch(nir_builder *b, nir_intrinsic_instr *instr)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci        b->cursor = nir_before_instr(&instr->instr);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci        nir_ssa_def *offset = v3d_nir_scratch_offset(b,instr);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci        nir_ssa_def *chans[NIR_MAX_VEC_COMPONENTS];
61bf215546Sopenharmony_ci        for (int i = 0; i < instr->num_components; i++) {
62bf215546Sopenharmony_ci                nir_ssa_def *chan_offset =
63bf215546Sopenharmony_ci                        nir_iadd_imm(b, offset, V3D_CHANNELS * i * 4);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci                nir_intrinsic_instr *chan_instr =
66bf215546Sopenharmony_ci                        nir_intrinsic_instr_create(b->shader, instr->intrinsic);
67bf215546Sopenharmony_ci                chan_instr->num_components = 1;
68bf215546Sopenharmony_ci                nir_ssa_dest_init(&chan_instr->instr, &chan_instr->dest, 1,
69bf215546Sopenharmony_ci                                  instr->dest.ssa.bit_size, NULL);
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci                chan_instr->src[0] = nir_src_for_ssa(chan_offset);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci                nir_intrinsic_set_align(chan_instr, 4, 0);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci                nir_builder_instr_insert(b, &chan_instr->instr);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci                chans[i] = &chan_instr->dest.ssa;
78bf215546Sopenharmony_ci        }
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci        nir_ssa_def *result = nir_vec(b, chans, instr->num_components);
81bf215546Sopenharmony_ci        nir_ssa_def_rewrite_uses(&instr->dest.ssa, result);
82bf215546Sopenharmony_ci        nir_instr_remove(&instr->instr);
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistatic void
86bf215546Sopenharmony_civ3d_nir_lower_store_scratch(nir_builder *b, nir_intrinsic_instr *instr)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci        b->cursor = nir_before_instr(&instr->instr);
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci        nir_ssa_def *offset = v3d_nir_scratch_offset(b, instr);
91bf215546Sopenharmony_ci        nir_ssa_def *value = nir_ssa_for_src(b, instr->src[0],
92bf215546Sopenharmony_ci                                             instr->num_components);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci        for (int i = 0; i < instr->num_components; i++) {
95bf215546Sopenharmony_ci                if (!(nir_intrinsic_write_mask(instr) & (1 << i)))
96bf215546Sopenharmony_ci                        continue;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci                nir_ssa_def *chan_offset =
99bf215546Sopenharmony_ci                        nir_iadd_imm(b, offset, V3D_CHANNELS * i * 4);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci                nir_intrinsic_instr *chan_instr =
102bf215546Sopenharmony_ci                        nir_intrinsic_instr_create(b->shader, instr->intrinsic);
103bf215546Sopenharmony_ci                chan_instr->num_components = 1;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci                chan_instr->src[0] = nir_src_for_ssa(nir_channel(b,
106bf215546Sopenharmony_ci                                                                 value,
107bf215546Sopenharmony_ci                                                                 i));
108bf215546Sopenharmony_ci                chan_instr->src[1] = nir_src_for_ssa(chan_offset);
109bf215546Sopenharmony_ci                nir_intrinsic_set_write_mask(chan_instr, 0x1);
110bf215546Sopenharmony_ci                nir_intrinsic_set_align(chan_instr, 4, 0);
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci                nir_builder_instr_insert(b, &chan_instr->instr);
113bf215546Sopenharmony_ci        }
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci        nir_instr_remove(&instr->instr);
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_cistatic bool
119bf215546Sopenharmony_civ3d_nir_lower_scratch_cb(nir_builder *b,
120bf215546Sopenharmony_ci                         nir_instr *instr,
121bf215546Sopenharmony_ci                         void *_state)
122bf215546Sopenharmony_ci{
123bf215546Sopenharmony_ci        if (instr->type != nir_instr_type_intrinsic)
124bf215546Sopenharmony_ci                return false;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci        nir_intrinsic_instr *intr =
127bf215546Sopenharmony_ci                nir_instr_as_intrinsic(instr);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci        switch (intr->intrinsic) {
130bf215546Sopenharmony_ci        case nir_intrinsic_load_scratch:
131bf215546Sopenharmony_ci                v3d_nir_lower_load_scratch(b, intr);
132bf215546Sopenharmony_ci                return true;
133bf215546Sopenharmony_ci        case nir_intrinsic_store_scratch:
134bf215546Sopenharmony_ci                v3d_nir_lower_store_scratch(b, intr);
135bf215546Sopenharmony_ci                return true;
136bf215546Sopenharmony_ci        default:
137bf215546Sopenharmony_ci                return false;
138bf215546Sopenharmony_ci        }
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci        return false;
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_cibool
144bf215546Sopenharmony_civ3d_nir_lower_scratch(nir_shader *s)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci        return nir_shader_instructions_pass(s, v3d_nir_lower_scratch_cb,
147bf215546Sopenharmony_ci                                            nir_metadata_block_index |
148bf215546Sopenharmony_ci                                            nir_metadata_dominance, NULL);
149bf215546Sopenharmony_ci}
150