2
* Copyright (C) 2021 Collabora Ltd.
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
#include "va_compiler.h"
26
#include "bi_builder.h"
28
/* Only some special immediates are available, as specified in the Table of
29
* Immediates in the specification. Other immediates must be lowered, either to
30
* uniforms or to moves.
34
va_mov_imm(bi_builder *b, uint32_t imm)
36
bi_index zero = bi_fau(BIR_FAU_IMMEDIATE | 0, false);
37
return bi_iadd_imm_i32(b, zero, imm);
41
va_lut_index_32(uint32_t imm)
43
for (unsigned i = 0; i < ARRAY_SIZE(valhall_immediates); ++i) {
44
if (valhall_immediates[i] == imm)
52
va_lut_index_16(uint16_t imm)
54
uint16_t *arr16 = (uint16_t *) valhall_immediates;
56
for (unsigned i = 0; i < (2 * ARRAY_SIZE(valhall_immediates)); ++i) {
58
return bi_half(va_lut(i >> 1), i & 1);
64
UNUSED static bi_index
65
va_lut_index_8(uint8_t imm)
67
uint8_t *arr8 = (uint8_t *) valhall_immediates;
69
for (unsigned i = 0; i < (4 * ARRAY_SIZE(valhall_immediates)); ++i) {
71
return bi_byte(va_lut(i >> 2), i & 3);
78
va_demote_constant_fp16(uint32_t value)
80
uint16_t fp16 = _mesa_float_to_half(uif(value));
82
/* Only convert if it is exact */
83
if (fui(_mesa_half_to_float(fp16)) == value)
84
return va_lut_index_16(fp16);
90
va_resolve_constant(bi_builder *b, uint32_t value, struct va_src_info info, bool staging)
92
/* Try the constant as-is */
94
bi_index lut = va_lut_index_32(value);
95
if (!bi_is_null(lut)) return lut;
98
/* Try using a single half of a FP16 constant */
99
bool replicated_halves = (value & 0xFFFF) == (value >> 16);
100
if (!staging && info.swizzle && info.size == VA_SIZE_16 && replicated_halves) {
101
bi_index lut = va_lut_index_16(value & 0xFFFF);
102
if (!bi_is_null(lut)) return lut;
105
/* TODO: Distinguish sign extend from zero extend */
107
/* Try zero-extending a single byte */
108
if (!staging && info.widen && value <= UINT8_MAX) {
109
bi_index lut = va_lut_index_8(value);
110
if (!bi_is_null(lut)) return lut;
113
/* Try zero-extending a single halfword */
114
if (!staging && info.widen && value <= UINT16_MAX) {
115
bi_index lut = va_lut_index_16(value);
116
if (!bi_is_null(lut)) return lut;
120
/* Try demoting the constant to FP16 */
121
if (!staging && info.swizzle && info.size == VA_SIZE_32) {
122
bi_index lut = va_demote_constant_fp16(value);
123
if (!bi_is_null(lut)) return lut;
126
/* TODO: Optimize to uniform */
127
return va_mov_imm(b, value);
131
va_lower_constants(bi_context *ctx, bi_instr *I)
133
bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
135
bi_foreach_src(I, s) {
136
if (I->src[s].type == BI_INDEX_CONSTANT) {
137
/* abs(#c) is pointless, but -#c occurs in transcendental sequences */
138
assert(!I->src[s].abs && "redundant .abs modifier");
140
bool staging = (s < valhall_opcodes[I->op].nr_staging_srcs);
141
struct va_src_info info = va_src_info(I->op, s);
142
uint32_t value = I->src[s].value;
143
enum bi_swizzle swz = I->src[s].swizzle;
145
/* Resolve any swizzle, keeping in mind the different interpretations
146
* swizzles in different contexts.
148
if (info.size == VA_SIZE_32) {
149
/* Extracting a half from the 32-bit value */
150
if (swz == BI_SWIZZLE_H00)
151
value = (value & 0xFFFF);
152
else if (swz == BI_SWIZZLE_H11)
153
value = (value >> 16);
155
assert(swz == BI_SWIZZLE_H01);
158
if (info.swizzle && swz != BI_SWIZZLE_H01)
159
value = fui(_mesa_half_to_float(value));
160
} else if (info.size == VA_SIZE_16) {
161
assert(swz >= BI_SWIZZLE_H00 && swz <= BI_SWIZZLE_H11);
162
value = bi_apply_swizzle(value, swz);
163
} else if (info.size == VA_SIZE_8 && info.lanes) {
165
unsigned chan = (swz - BI_SWIZZLE_B0000);
168
value = (value >> (8 * chan)) & 0xFF;
170
/* TODO: Any other special handling? */
171
value = bi_apply_swizzle(value, swz);
174
bi_index cons = va_resolve_constant(&b, value, info, staging);
175
cons.neg ^= I->src[s].neg;