aboutsummaryrefslogtreecommitdiffstats
path: root/src/glsl/nir/nir_opt_peephole_ffma.c
blob: 2c9b8e570d4e4e7e16c2bc0dff09133074d2b392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Jason Ekstrand (jason@jlekstrand.net)
 *
 */

#include "nir.h"

/*
 * Implements a small peephole optimization that looks for a multiply that
 * is only ever used in an add and replaces both with an fma.
 */

struct peephole_ffma_state {
   void *mem_ctx;
   nir_function_impl *impl;
   bool progress;
};

static inline nir_alu_instr *
get_mul_for_src(nir_alu_instr *add, unsigned idx)
{
   if (!add->src[idx].src.is_ssa)
      return NULL;

   /* We can't handle these in between the operations */
   if (add->src[idx].negate || add->src[idx].abs)
      return NULL;

   nir_instr *instr = add->src[idx].src.ssa->parent_instr;
   if (instr->type != nir_instr_type_alu)
      return NULL;

   nir_alu_instr *mul = nir_instr_as_alu(instr);
   if (mul->op != nir_op_fmul)
      return NULL;

   /* Can't handle a saturate in between */
   if (mul->dest.saturate)
      return NULL;

   /* We already know that the same source is not used twice in the add and
    * we will assume valid use-def information, so this check is sufficient
    */
   if (mul->dest.dest.ssa.uses->entries > 1)
      return NULL; /* Not the only use */

   return mul;
}

/* Copies (and maybe swizzles) the given ALU source */
static inline void
copy_alu_src(void *mem_ctx, nir_alu_src *new_src, nir_alu_src old_src,
             uint8_t *swizzle)
{
   new_src->src = nir_src_copy(old_src.src, mem_ctx);
   new_src->abs = old_src.abs;
   new_src->negate = old_src.negate;

   if (swizzle == NULL) {
      memcpy(new_src->swizzle, old_src.swizzle, sizeof old_src.swizzle);
   } else {
      for (int i = 0; i < 4; ++i) {
         if (swizzle[i] < 4)
            new_src->swizzle[i] = old_src.swizzle[swizzle[i]];
      }
   }
}

static bool
nir_opt_peephole_ffma_block(nir_block *block, void *void_state)
{
   struct peephole_ffma_state *state = void_state;

   nir_foreach_instr_safe(block, instr) {
      if (instr->type != nir_instr_type_alu)
         continue;

      nir_alu_instr *add = nir_instr_as_alu(instr);
      if (add->op != nir_op_fadd)
         continue;

      /* TODO: Maybe bail if this expression is considered "precise"? */

      /* This, is the case a + a.  We would rather handle this with an
       * algebraic reduction than fuse it.  Also, we want to only fuse
       * things where the multiply is used only once and, in this case,
       * it would be used twice by the same instruction.
       */
      if (add->src[0].src.is_ssa && add->src[1].src.is_ssa &&
          add->src[0].src.ssa == add->src[1].src.ssa)
         continue;

      nir_alu_instr *mul = get_mul_for_src(add, 0);
      unsigned mul_src = 0;

      if (mul == NULL) {
         mul = get_mul_for_src(add, 1);
         mul_src = 1;
      }

      if (mul == NULL)
         continue;

      nir_alu_instr *ffma = nir_alu_instr_create(state->mem_ctx, nir_op_ffma);
      ffma->dest.saturate = add->dest.saturate;
      ffma->dest.write_mask = add->dest.write_mask;

      copy_alu_src(state->mem_ctx, &ffma->src[0], mul->src[0],
                   add->src[mul_src].swizzle);
      copy_alu_src(state->mem_ctx, &ffma->src[1], mul->src[1],
                   add->src[mul_src].swizzle);
      copy_alu_src(state->mem_ctx, &ffma->src[2], add->src[1 - mul_src], NULL);

      if (add->dest.dest.is_ssa) {
         ffma->dest.dest.is_ssa = true;
         nir_ssa_def_init(state->impl, &ffma->instr, &ffma->dest.dest.ssa,
                          add->dest.dest.ssa.num_components,
                          add->dest.dest.ssa.name);

         nir_src ffma_dest_src = {
            .is_ssa = true,
            .ssa = &ffma->dest.dest.ssa,
         };
         nir_ssa_def_rewrite_uses(&add->dest.dest.ssa, ffma_dest_src,
                                  state->mem_ctx);
      } else {
         ffma->dest.dest = nir_dest_copy(add->dest.dest, state->mem_ctx);
      }

      nir_instr_insert_before(&add->instr, &ffma->instr);
      nir_instr_remove(&add->instr);
      nir_instr_remove(&mul->instr);

      state->progress = true;
   }

   return true;
}

static bool
nir_opt_peephole_ffma_impl(nir_function_impl *impl)
{
   struct peephole_ffma_state state;

   state.mem_ctx = ralloc_parent(impl);
   state.impl = impl;
   state.progress = false;

   nir_foreach_block(impl, nir_opt_peephole_ffma_block, &state);

   if (state.progress)
      nir_metadata_dirty(impl, nir_metadata_block_index |
                               nir_metadata_dominance);

   return state.progress;
}

bool
nir_opt_peephole_ffma(nir_shader *shader)
{
   bool progress = false;

   nir_foreach_overload(shader, overload) {
      if (overload->impl)
         progress |= nir_opt_peephole_ffma_impl(overload->impl);
   }

   return progress;
}