2 * Copyright (C) 2018 Jonathan Marek <jonathan@marek.ca>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Jonathan Marek <jonathan@marek.ca>
27 /* some operations can only be scalar on a2xx:
28 * rsq, rcp, log2, exp2, cos, sin, sqrt
29 * mostly copy-pasted from nir_lower_alu_to_scalar.c
32 #include "ir2_private.h"
33 #include "compiler/nir/nir_builder.h"
36 nir_alu_ssa_dest_init(nir_alu_instr
* instr
, unsigned num_components
,
39 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
.dest
, num_components
,
41 instr
->dest
.write_mask
= (1 << num_components
) - 1;
45 lower_reduction(nir_alu_instr
* instr
, nir_op chan_op
, nir_op merge_op
,
46 nir_builder
* builder
)
48 unsigned num_components
= nir_op_infos
[instr
->op
].input_sizes
[0];
50 nir_ssa_def
*last
= NULL
;
51 for (unsigned i
= 0; i
< num_components
; i
++) {
53 nir_alu_instr_create(builder
->shader
, chan_op
);
54 nir_alu_ssa_dest_init(chan
, 1, instr
->dest
.dest
.ssa
.bit_size
);
55 nir_alu_src_copy(&chan
->src
[0], &instr
->src
[0], chan
);
56 chan
->src
[0].swizzle
[0] = chan
->src
[0].swizzle
[i
];
57 if (nir_op_infos
[chan_op
].num_inputs
> 1) {
58 assert(nir_op_infos
[chan_op
].num_inputs
== 2);
59 nir_alu_src_copy(&chan
->src
[1], &instr
->src
[1], chan
);
60 chan
->src
[1].swizzle
[0] = chan
->src
[1].swizzle
[i
];
62 chan
->exact
= instr
->exact
;
64 nir_builder_instr_insert(builder
, &chan
->instr
);
67 last
= &chan
->dest
.dest
.ssa
;
69 last
= nir_build_alu(builder
, merge_op
,
70 last
, &chan
->dest
.dest
.ssa
, NULL
, NULL
);
74 assert(instr
->dest
.write_mask
== 1);
75 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(last
));
76 nir_instr_remove(&instr
->instr
);
79 static bool lower_scalar(nir_alu_instr
* instr
, nir_builder
* b
)
81 assert(instr
->dest
.dest
.is_ssa
);
82 assert(instr
->dest
.write_mask
!= 0);
84 b
->cursor
= nir_before_instr(&instr
->instr
);
85 b
->exact
= instr
->exact
;
87 #define LOWER_REDUCTION(name, chan, merge) \
91 lower_reduction(instr, chan, merge, b); \
95 /* TODO: handle these instead of lowering */
96 LOWER_REDUCTION(nir_op_fall_equal
, nir_op_seq
, nir_op_fand
);
97 LOWER_REDUCTION(nir_op_fany_nequal
, nir_op_sne
, nir_op_for
);
111 assert(nir_op_infos
[instr
->op
].num_inputs
== 1);
113 unsigned num_components
= instr
->dest
.dest
.ssa
.num_components
;
114 nir_ssa_def
*comps
[NIR_MAX_VEC_COMPONENTS
] = { NULL
};
117 if (num_components
== 1)
120 for (chan
= 0; chan
< num_components
; chan
++) {
121 assert(instr
->dest
.write_mask
& (1 << chan
));
123 nir_alu_instr
*lower
= nir_alu_instr_create(b
->shader
, instr
->op
);
125 nir_alu_src_copy(&lower
->src
[0], &instr
->src
[0], lower
);
126 lower
->src
[0].swizzle
[0] = instr
->src
[0].swizzle
[chan
];
128 nir_alu_ssa_dest_init(lower
, 1, instr
->dest
.dest
.ssa
.bit_size
);
129 lower
->dest
.saturate
= instr
->dest
.saturate
;
130 comps
[chan
] = &lower
->dest
.dest
.ssa
;
131 lower
->exact
= instr
->exact
;
133 nir_builder_instr_insert(b
, &lower
->instr
);
136 nir_ssa_def
*vec
= nir_vec(b
, comps
, num_components
);
138 nir_ssa_def_rewrite_uses(&instr
->dest
.dest
.ssa
, nir_src_for_ssa(vec
));
140 nir_instr_remove(&instr
->instr
);
144 static bool lower_scalar_impl(nir_function_impl
* impl
)
147 nir_builder_init(&builder
, impl
);
148 bool progress
= false;
150 nir_foreach_block(block
, impl
) {
151 nir_foreach_instr_safe(instr
, block
) {
152 if (instr
->type
== nir_instr_type_alu
)
153 progress
= lower_scalar(nir_instr_as_alu(instr
), &builder
)
158 nir_metadata_preserve(impl
, nir_metadata_block_index
|
159 nir_metadata_dominance
);
164 bool ir2_nir_lower_scalar(nir_shader
* shader
)
166 bool progress
= false;
168 nir_foreach_function(function
, shader
) {
170 progress
= lower_scalar_impl(function
->impl
) || progress
;