1 /*
2 * Copyright (C) 2020 Collabora Ltd.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25 #include "bi_builder.h"
26
27 /* Not all 8-bit and 16-bit instructions support all swizzles on all sources.
28 * These passes, intended to run after NIR->BIR but before scheduling/RA, lower
29 * away swizzles that cannot be represented. In the future, we should try to
30 * recombine swizzles where we can as an optimization.
31 */
32
33 static void
bi_lower_swizzle_16(bi_context * ctx,bi_instr * ins,unsigned src)34 bi_lower_swizzle_16(bi_context *ctx, bi_instr *ins, unsigned src)
35 {
36 /* Identity is ok */
37 if (ins->src[src].swizzle == BI_SWIZZLE_H01)
38 return;
39
40 /* TODO: Use the opcode table and be a lot more methodical about this... */
41 switch (ins->op) {
42 /* Some instructions used with 16-bit data never have swizzles */
43 case BI_OPCODE_CSEL_V2F16:
44 case BI_OPCODE_CSEL_V2I16:
45 case BI_OPCODE_CSEL_V2S16:
46 case BI_OPCODE_CSEL_V2U16:
47
48 /* Despite ostensibly being 32-bit instructions, CLPER does not
49 * inherently interpret the data, so it can be used for v2f16
50 * derivatives, which might require swizzle lowering */
51 case BI_OPCODE_CLPER_I32:
52 case BI_OPCODE_CLPER_V6_I32:
53 break;
54
55 case BI_OPCODE_IADD_V2S16:
56 case BI_OPCODE_IADD_V2U16:
57 case BI_OPCODE_ISUB_V2S16:
58 case BI_OPCODE_ISUB_V2U16:
59 if (src == 0 && ins->src[src].swizzle != BI_SWIZZLE_H10)
60 break;
61 else
62 return;
63 case BI_OPCODE_LSHIFT_AND_V2I16:
64 case BI_OPCODE_LSHIFT_OR_V2I16:
65 case BI_OPCODE_LSHIFT_XOR_V2I16:
66 case BI_OPCODE_RSHIFT_AND_V2I16:
67 case BI_OPCODE_RSHIFT_OR_V2I16:
68 case BI_OPCODE_RSHIFT_XOR_V2I16:
69 if (src == 2)
70 return;
71 else
72 break;
73
74 /* We don't want to deal with reswizzling logic in modifier prop. Move
75 * the swizzle outside, it's easier for clamp propagation. */
76 case BI_OPCODE_FCLAMP_V2F16:
77 {
78 bi_builder b = bi_init_builder(ctx, bi_after_instr(ins));
79 bi_index dest = ins->dest[0];
80 bi_index tmp = bi_temp(ctx);
81
82 ins->dest[0] = tmp;
83 bi_swz_v2i16_to(&b, dest, bi_replace_index(ins->src[0], tmp));
84 return;
85 }
86
87 default:
88 return;
89 }
90
91 /* If the instruction is scalar we can ignore the other component */
92 if (ins->dest[0].swizzle == BI_SWIZZLE_H00 &&
93 ins->src[src].swizzle == BI_SWIZZLE_H00)
94 {
95 ins->src[src].swizzle = BI_SWIZZLE_H01;
96 return;
97 }
98
99 /* Lower it away */
100 bi_builder b = bi_init_builder(ctx, bi_before_instr(ins));
101 ins->src[src] = bi_replace_index(ins->src[src],
102 bi_swz_v2i16(&b, ins->src[src]));
103 ins->src[src].swizzle = BI_SWIZZLE_H01;
104 }
105
106 void
bi_lower_swizzle(bi_context * ctx)107 bi_lower_swizzle(bi_context *ctx)
108 {
109 bi_foreach_instr_global_safe(ctx, ins) {
110 bi_foreach_src(ins, s) {
111 if (!bi_is_null(ins->src[s]))
112 bi_lower_swizzle_16(ctx, ins, s);
113 }
114 }
115 }
116