2 * Copyright © 2017 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "nir_builder.h"
28 * \file nir_opt_intrinsics.c
32 high_subgroup_mask(nir_builder
*b
,
36 /* group_mask could probably be calculated more efficiently but we want to
37 * be sure not to shift by 64 if the subgroup size is 64 because the GLSL
38 * shift operator is undefined in that case. In any case if we were worried
39 * about efficency this should probably be done further down because the
40 * subgroup size is likely to be known at compile time.
42 nir_ssa_def
*subgroup_size
= nir_load_subgroup_size(b
);
43 nir_ssa_def
*all_bits
= nir_imm_int64(b
, ~0ull);
44 nir_ssa_def
*shift
= nir_isub(b
, nir_imm_int(b
, 64), subgroup_size
);
45 nir_ssa_def
*group_mask
= nir_ushr(b
, all_bits
, shift
);
46 nir_ssa_def
*higher_bits
= nir_ishl(b
, nir_imm_int64(b
, base_mask
), count
);
48 return nir_iand(b
, higher_bits
, group_mask
);
52 opt_intrinsics_impl(nir_function_impl
*impl
)
55 nir_builder_init(&b
, impl
);
56 bool progress
= false;
58 nir_foreach_block(block
, impl
) {
59 nir_foreach_instr_safe(instr
, block
) {
60 if (instr
->type
!= nir_instr_type_intrinsic
)
63 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
64 nir_ssa_def
*replacement
= NULL
;
65 b
.cursor
= nir_before_instr(instr
);
67 switch (intrin
->intrinsic
) {
68 case nir_intrinsic_vote_any
:
69 case nir_intrinsic_vote_all
: {
70 nir_const_value
*val
= nir_src_as_const_value(intrin
->src
[0]);
71 if (!val
&& !b
.shader
->options
->lower_vote_trivial
)
74 replacement
= nir_ssa_for_src(&b
, intrin
->src
[0], 1);
77 case nir_intrinsic_vote_eq
: {
78 nir_const_value
*val
= nir_src_as_const_value(intrin
->src
[0]);
79 if (!val
&& !b
.shader
->options
->lower_vote_trivial
)
82 replacement
= nir_imm_int(&b
, NIR_TRUE
);
85 case nir_intrinsic_ballot
: {
86 assert(b
.shader
->options
->max_subgroup_size
!= 0);
87 if (b
.shader
->options
->max_subgroup_size
> 32 ||
88 intrin
->dest
.ssa
.bit_size
<= 32)
91 nir_intrinsic_instr
*ballot
=
92 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_ballot
);
93 nir_ssa_dest_init(&ballot
->instr
, &ballot
->dest
, 1, 32, NULL
);
94 nir_src_copy(&ballot
->src
[0], &intrin
->src
[0], ballot
);
96 nir_builder_instr_insert(&b
, &ballot
->instr
);
98 replacement
= nir_pack_64_2x32_split(&b
,
103 case nir_intrinsic_load_subgroup_eq_mask
:
104 case nir_intrinsic_load_subgroup_ge_mask
:
105 case nir_intrinsic_load_subgroup_gt_mask
:
106 case nir_intrinsic_load_subgroup_le_mask
:
107 case nir_intrinsic_load_subgroup_lt_mask
: {
108 if (!b
.shader
->options
->lower_subgroup_masks
)
111 nir_ssa_def
*count
= nir_load_subgroup_invocation(&b
);
113 switch (intrin
->intrinsic
) {
114 case nir_intrinsic_load_subgroup_eq_mask
:
115 replacement
= nir_ishl(&b
, nir_imm_int64(&b
, 1ull), count
);
117 case nir_intrinsic_load_subgroup_ge_mask
:
118 replacement
= high_subgroup_mask(&b
, count
, ~0ull);
120 case nir_intrinsic_load_subgroup_gt_mask
:
121 replacement
= high_subgroup_mask(&b
, count
, ~1ull);
123 case nir_intrinsic_load_subgroup_le_mask
:
124 replacement
= nir_inot(&b
, nir_ishl(&b
, nir_imm_int64(&b
, ~1ull), count
));
126 case nir_intrinsic_load_subgroup_lt_mask
:
127 replacement
= nir_inot(&b
, nir_ishl(&b
, nir_imm_int64(&b
, ~0ull), count
));
130 unreachable("you seriously can't tell this is unreachable?");
141 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
142 nir_src_for_ssa(replacement
));
143 nir_instr_remove(instr
);
152 nir_opt_intrinsics(nir_shader
*shader
)
154 bool progress
= false;
156 nir_foreach_function(function
, shader
) {
160 if (opt_intrinsics_impl(function
->impl
)) {
162 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
163 nir_metadata_dominance
);