~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/panfrost/bifrost/valhall/va_lower_constants.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2021 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 "va_compiler.h"
25
 
#include "valhall.h"
26
 
#include "bi_builder.h"
27
 
 
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.
31
 
 */
32
 
 
33
 
static bi_index
34
 
va_mov_imm(bi_builder *b, uint32_t imm)
35
 
{
36
 
   bi_index zero = bi_fau(BIR_FAU_IMMEDIATE | 0, false);
37
 
   return bi_iadd_imm_i32(b, zero, imm);
38
 
}
39
 
 
40
 
static bi_index
41
 
va_lut_index_32(uint32_t imm)
42
 
{
43
 
   for (unsigned i = 0; i < ARRAY_SIZE(valhall_immediates); ++i) {
44
 
      if (valhall_immediates[i] == imm)
45
 
         return va_lut(i);
46
 
   }
47
 
 
48
 
   return bi_null();
49
 
}
50
 
 
51
 
static bi_index
52
 
va_lut_index_16(uint16_t imm)
53
 
{
54
 
   uint16_t *arr16 = (uint16_t *) valhall_immediates;
55
 
 
56
 
   for (unsigned i = 0; i < (2 * ARRAY_SIZE(valhall_immediates)); ++i) {
57
 
      if (arr16[i] == imm)
58
 
         return bi_half(va_lut(i >> 1), i & 1);
59
 
   }
60
 
 
61
 
   return bi_null();
62
 
}
63
 
 
64
 
UNUSED static bi_index
65
 
va_lut_index_8(uint8_t imm)
66
 
{
67
 
   uint8_t *arr8 = (uint8_t *) valhall_immediates;
68
 
 
69
 
   for (unsigned i = 0; i < (4 * ARRAY_SIZE(valhall_immediates)); ++i) {
70
 
      if (arr8[i] == imm)
71
 
         return bi_byte(va_lut(i >> 2), i & 3);
72
 
   }
73
 
 
74
 
   return bi_null();
75
 
}
76
 
 
77
 
static bi_index
78
 
va_demote_constant_fp16(uint32_t value)
79
 
{
80
 
   uint16_t fp16 = _mesa_float_to_half(uif(value));
81
 
 
82
 
   /* Only convert if it is exact */
83
 
   if (fui(_mesa_half_to_float(fp16)) == value)
84
 
      return va_lut_index_16(fp16);
85
 
   else
86
 
      return bi_null();
87
 
}
88
 
 
89
 
static bi_index
90
 
va_resolve_constant(bi_builder *b, uint32_t value, struct va_src_info info, bool staging)
91
 
{
92
 
   /* Try the constant as-is */
93
 
   if (!staging) {
94
 
      bi_index lut = va_lut_index_32(value);
95
 
      if (!bi_is_null(lut)) return lut;
96
 
   }
97
 
 
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;
103
 
   }
104
 
 
105
 
   /* TODO: Distinguish sign extend from zero extend */
106
 
#if 0
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;
111
 
   }
112
 
 
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;
117
 
   }
118
 
#endif
119
 
 
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;
124
 
   }
125
 
 
126
 
   /* TODO: Optimize to uniform */
127
 
   return va_mov_imm(b, value);
128
 
}
129
 
 
130
 
void
131
 
va_lower_constants(bi_context *ctx, bi_instr *I)
132
 
{
133
 
   bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
134
 
 
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");
139
 
 
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;
144
 
 
145
 
         /* Resolve any swizzle, keeping in mind the different interpretations
146
 
          * swizzles in different contexts.
147
 
          */
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);
154
 
            else
155
 
               assert(swz == BI_SWIZZLE_H01);
156
 
 
157
 
            /* FP16 -> FP32 */
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) {
164
 
            /* 8-bit extract */
165
 
            unsigned chan = (swz - BI_SWIZZLE_B0000);
166
 
            assert(chan < 4);
167
 
 
168
 
            value = (value >> (8 * chan)) & 0xFF;
169
 
         } else {
170
 
            /* TODO: Any other special handling? */
171
 
            value = bi_apply_swizzle(value, swz);
172
 
         }
173
 
 
174
 
         bi_index cons = va_resolve_constant(&b, value, info, staging);
175
 
         cons.neg ^= I->src[s].neg;
176
 
         I->src[s] = cons;
177
 
      }
178
 
   }
179
 
}