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 opt_intrinsics_impl(nir_function_impl
*impl
)
35 nir_builder_init(&b
, impl
);
36 bool progress
= false;
38 nir_foreach_block(block
, impl
) {
39 nir_foreach_instr_safe(instr
, block
) {
40 if (instr
->type
!= nir_instr_type_intrinsic
)
43 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
44 nir_ssa_def
*replacement
= NULL
;
45 b
.cursor
= nir_before_instr(instr
);
47 switch (intrin
->intrinsic
) {
48 case nir_intrinsic_vote_any
:
49 case nir_intrinsic_vote_all
:
50 if (nir_src_as_const_value(intrin
->src
[0]))
51 replacement
= nir_ssa_for_src(&b
, intrin
->src
[0], 1);
53 case nir_intrinsic_vote_eq
:
54 if (nir_src_as_const_value(intrin
->src
[0]))
55 replacement
= nir_imm_int(&b
, NIR_TRUE
);
57 case nir_intrinsic_ballot
: {
58 assert(b
.shader
->options
->max_subgroup_size
!= 0);
59 if (b
.shader
->options
->max_subgroup_size
> 32 ||
60 intrin
->dest
.ssa
.bit_size
<= 32)
63 nir_intrinsic_instr
*ballot
=
64 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_ballot
);
65 nir_ssa_dest_init(&ballot
->instr
, &ballot
->dest
, 1, 32, NULL
);
66 nir_src_copy(&ballot
->src
[0], &intrin
->src
[0], ballot
);
68 nir_builder_instr_insert(&b
, &ballot
->instr
);
70 replacement
= nir_pack_64_2x32_split(&b
,
82 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
83 nir_src_for_ssa(replacement
));
84 nir_instr_remove(instr
);
93 nir_opt_intrinsics(nir_shader
*shader
)
95 bool progress
= false;
97 nir_foreach_function(function
, shader
) {
101 if (opt_intrinsics_impl(function
->impl
)) {
103 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
104 nir_metadata_dominance
);