1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2020 Icecream95 <ixn@disroot.org>
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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "pan_ir.h"
25bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* OpenCL uses 64-bit types for some intrinsic functions, including
28bf215546Sopenharmony_ci * global_invocation_id(). This could be worked around during conversion to
29bf215546Sopenharmony_ci * MIR, except that global_invocation_id is a vec3, and the 128-bit registers
30bf215546Sopenharmony_ci * on Midgard can only hold a 64-bit vec2.
31bf215546Sopenharmony_ci * Rather than attempting to add hacky 64-bit vec3 support, convert these
32bf215546Sopenharmony_ci * intrinsics to 32-bit and add a cast back to 64-bit, and rely on NIR not
33bf215546Sopenharmony_ci * vectorizing back to vec3.
34bf215546Sopenharmony_ci */
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic bool
37bf215546Sopenharmony_cinir_lower_64bit_intrin_instr(nir_builder *b, nir_instr *instr, void *data)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci        if (instr->type != nir_instr_type_intrinsic)
40bf215546Sopenharmony_ci                return false;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci        nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci        switch (intr->intrinsic) {
45bf215546Sopenharmony_ci        case nir_intrinsic_load_global_invocation_id:
46bf215546Sopenharmony_ci        case nir_intrinsic_load_global_invocation_id_zero_base:
47bf215546Sopenharmony_ci        case nir_intrinsic_load_workgroup_id:
48bf215546Sopenharmony_ci        case nir_intrinsic_load_num_workgroups:
49bf215546Sopenharmony_ci                break;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci        default:
52bf215546Sopenharmony_ci                return false;
53bf215546Sopenharmony_ci        }
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci        if (nir_dest_bit_size(intr->dest) != 64)
56bf215546Sopenharmony_ci                return false;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci        b->cursor = nir_after_instr(instr);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci        assert(intr->dest.is_ssa);
61bf215546Sopenharmony_ci        intr->dest.ssa.bit_size = 32;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci        nir_ssa_def *conv = nir_u2u64(b, &intr->dest.ssa);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci        nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, conv,
66bf215546Sopenharmony_ci                                       conv->parent_instr);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci        return true;
69bf215546Sopenharmony_ci}
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cibool
72bf215546Sopenharmony_cipan_nir_lower_64bit_intrin(nir_shader *shader)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci        return nir_shader_instructions_pass(shader,
75bf215546Sopenharmony_ci                                            nir_lower_64bit_intrin_instr,
76bf215546Sopenharmony_ci                                            nir_metadata_block_index | nir_metadata_dominance,
77bf215546Sopenharmony_ci                                            NULL);
78bf215546Sopenharmony_ci}
79