Debian Codegraph @

src/panfrost/compiler/bifrost/bi_opt_dual_tex.c

raw
1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "compiler.h"
8
9 #define XXH_INLINE_ALL
10 #include "util/xxhash.h"
11
12 /* Fuse pairs of TEXS_2D instructions into a single dual texture TEXC, if both
13 * sample at the same coordinate with the default LOD mode for the shader stage
14 * (computed LOD in fragment shaders, else zero LOD) and immediate
15 * texture/sampler indices 0...3.
16 *
17 * Fusing across basic block boundaries is not expected to be useful, as it
18 * increases register pressure and causes redundant memory traffic. As such, we
19 * use a local optimization pass.
20 *
21 * To pair ops efficiently, we maintain a set (backed by a hash table) using
22 * only the coordinate sources for hashing and equality. Hence the pass runs in
23 * O(n) worst case expected time for n insturctions in a block. We reject
24 * invalid texture instructions quickly to reduce the constant factor.
25 *
26 * Dual texture instructions have skip flags, like normal texture instructions.
27 * Adding a skip flag to an instruction that doesn't have it is illegal, but
28 * removing a skip flag from one that has it is legal. Accordingly, set the
29 * fused TEXC's skip to the logical AND of the unfused TEXS flags. We run the
30 * optimization pass to run after bi_analyze_helper_requirements.
31 */
32
33 static inline bool
34 bi_can_fuse_dual_tex(bi_instr *I, bool fuse_zero_lod)
35 {
36 return (I->op == BI_OPCODE_TEXS_2D_F32 || I->op == BI_OPCODE_TEXS_2D_F16) &&
37 (I->texture_index < 4 && I->sampler_index < 4) &&
38 (I->lod_mode == fuse_zero_lod);
39 }
40
41 static enum bifrost_texture_format
42 bi_format_for_texs_2d(enum bi_opcode op)
43 {
44 switch (op) {
45 case BI_OPCODE_TEXS_2D_F32:
46 return BIFROST_TEXTURE_FORMAT_F32;
47 case BI_OPCODE_TEXS_2D_F16:
48 return BIFROST_TEXTURE_FORMAT_F16;
49 default:
50 UNREACHABLE("Invalid TEXS_2D instruction");
51 }
52 }
53
54 static void
55 bi_fuse_dual(bi_context *ctx, bi_instr *I1, bi_instr *I2)
56 {
57 /* Construct a texture operation descriptor for the dual texture */
58 struct bifrost_dual_texture_operation desc = {
59 .mode = BIFROST_TEXTURE_OPERATION_DUAL,
60
61 .primary_texture_index = I1->texture_index,
62 .primary_sampler_index = I1->sampler_index,
63 .primary_format = bi_format_for_texs_2d(I1->op),
64 .primary_mask = 0xF,
65
66 .secondary_texture_index = I2->texture_index,
67 .secondary_sampler_index = I2->sampler_index,
68 .secondary_format = bi_format_for_texs_2d(I2->op),
69 .secondary_mask = 0xF,
70 };
71
72 /* LOD mode is implied in a shader stage */
73 assert(I1->lod_mode == I2->lod_mode);
74
75 /* Insert before the earlier instruction in case its result is consumed
76 * before the later instruction
77 */
78 bi_builder b = bi_init_builder(ctx, bi_before_instr(I1));
79
80 bi_instr *I = bi_texc_dual_to(
81 &b, I1->dest[0], I2->dest[0], bi_null(), /* staging */
82 I1->src[0], I1->src[1], /* coordinates */
83 bi_imm_u32(bi_dual_tex_as_u32(desc)), I1->lod_mode,
84 bi_count_write_registers(I1, 0), bi_count_write_registers(I2, 0));
85
86 I->skip = I1->skip && I2->skip;
87
88 bi_remove_instruction(I1);
89 bi_remove_instruction(I2);
90 }
91
92 #define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
93
94 static uint32_t
95 coord_hash(const void *key)
96 {
97 const bi_instr *I = key;
98
99 return XXH32(&I->src[0], sizeof(I->src[0]) + sizeof(I->src[1]), 0);
100 }
101
102 static bool
103 coord_equal(const void *key1, const void *key2)
104 {
105 const bi_instr *I = key1;
106 const bi_instr *J = key2;
107
108 return memcmp(&I->src[0], &J->src[0],
109 sizeof(I->src[0]) + sizeof(I->src[1])) == 0;
110 }
111
112 static void
113 bi_opt_fuse_dual_texture_block(bi_context *ctx, bi_block *block)
114 {
115 struct set *set = _mesa_set_create(ctx, coord_hash, coord_equal);
116 bool fuse_zero_lod = (ctx->stage != MESA_SHADER_FRAGMENT);
117 bool found = false;
118
119 bi_foreach_instr_in_block_safe(block, I) {
120 if (!bi_can_fuse_dual_tex(I, fuse_zero_lod))
121 continue;
122
123 struct set_entry *ent = _mesa_set_search_or_add(set, I, &found);
124
125 if (found) {
126 bi_fuse_dual(ctx, (bi_instr *)ent->key, I);
127 _mesa_set_remove(set, ent);
128 }
129 }
130 }
131
132 void
133 bi_opt_fuse_dual_texture(bi_context *ctx)
134 {
135 bi_foreach_block(ctx, block) {
136 bi_opt_fuse_dual_texture_block(ctx, block);
137 }
138 }