Debian Codegraph @

src/imagination/pco/pco_binary.c

raw
1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 /**
8 * \file pco_binary.c
9 *
10 * \brief PCO binary-specific functions.
11 */
12
13 #include "pco.h"
14 #include "pco_internal.h"
15 #include "pco_isa.h"
16 #include "pco_map.h"
17 #include "util/u_dynarray.h"
18
19 #include <assert.h>
20 #include <stdint.h>
21 #include <stdio.h>
22
23 /**
24 * \brief Encodes instruction group alignment.
25 *
26 * \param[in,out] buf Binary buffer.
27 * \param[in] igrp PCO instruction group.
28 */
29 static inline unsigned pco_encode_align(struct util_dynarray *buf,
30 pco_igrp *igrp)
31 {
32 unsigned bytes_encoded = 0;
33
34 if (igrp->enc.len.word_padding) {
35 util_dynarray_append_typed(buf, uint8_t, 0xff);
36 bytes_encoded += 1;
37 }
38
39 if (igrp->enc.len.align_padding) {
40 assert(!(igrp->enc.len.align_padding % 2));
41
42 unsigned align_words = igrp->enc.len.align_padding / 2;
43 util_dynarray_append_typed(buf, uint8_t, 0xf0 | align_words);
44 bytes_encoded += 1;
45
46 for (unsigned u = 0; u < igrp->enc.len.align_padding - 1; ++u) {
47 util_dynarray_append_typed(buf, uint8_t, 0xff);
48 bytes_encoded += 1;
49 }
50 }
51
52 return bytes_encoded;
53 }
54
55 /**
56 * \brief Encodes a PCO instruction group into binary.
57 *
58 * \param[in,out] buf Binary buffer.
59 * \param[in] igrp PCO instruction group.
60 * \return The number of bytes encoded.
61 */
62 static unsigned pco_encode_igrp(struct util_dynarray *buf, pco_igrp *igrp)
63 {
64 uint8_t *ptr;
65 unsigned bytes_encoded = 0;
66
67 /* Header. */
68 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.hdr);
69 bytes_encoded += pco_igrp_hdr_map_encode(ptr, igrp);
70
71 /* Instructions. */
72 pco_foreach_phase_in_igrp_rev (igrp, p) {
73 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.instrs[p]);
74 bytes_encoded += pco_instr_map_encode(ptr, igrp, p);
75 }
76
77 /* I/O. */
78 if (igrp->enc.len.lower_srcs) {
79 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.lower_srcs);
80 bytes_encoded += pco_srcs_map_encode(ptr, igrp, false);
81 }
82
83 if (igrp->enc.len.upper_srcs) {
84 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.upper_srcs);
85 bytes_encoded += pco_srcs_map_encode(ptr, igrp, true);
86 }
87
88 if (igrp->enc.len.iss) {
89 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.iss);
90 bytes_encoded += pco_iss_map_encode(ptr, igrp);
91 }
92
93 if (igrp->enc.len.dests) {
94 ptr = util_dynarray_grow(buf, uint8_t, igrp->enc.len.dests);
95 bytes_encoded += pco_dests_map_encode(ptr, igrp);
96 }
97
98 /* Word/alignment padding. */
99 bytes_encoded += pco_encode_align(buf, igrp);
100
101 assert(bytes_encoded == igrp->enc.len.total);
102
103 return bytes_encoded;
104 }
105
106 /**
107 * \brief Encodes a PCO shader into binary.
108 *
109 * \param[in] ctx PCO compiler context.
110 * \param[in,out] shader PCO shader.
111 */
112 void pco_encode_ir(pco_ctx *ctx, pco_shader *shader)
113 {
114 assert(shader->is_grouped);
115
116 util_dynarray_init(&shader->binary, shader);
117
118 unsigned bytes_encoded = 0;
119 pco_foreach_func_in_shader (func, shader) {
120 func->enc_offset = bytes_encoded;
121 pco_foreach_block_in_func (block, func) {
122 pco_foreach_igrp_in_block (igrp, block) {
123 bytes_encoded += pco_encode_igrp(&shader->binary, igrp);
124 }
125 }
126 }
127
128 if (pco_should_print_binary(shader))
129 pco_print_binary(shader, stdout, "after encoding");
130
131 if (pco_should_print_stats(shader))
132 pco_print_shader_stats(shader, stdout);
133 }
134
135 /**
136 * \brief Returns the size in bytes of a PCO shader binary.
137 *
138 * \param[in] shader PCO shader.
139 * \return The size in bytes of the PCO shader binary.
140 */
141 unsigned pco_shader_binary_size(pco_shader *shader)
142 {
143 if (!shader)
144 return 0;
145
146 return shader->binary.size;
147 }
148
149 /**
150 * \brief Returns the PCO shader binary data.
151 *
152 * \param[in] shader PCO shader.
153 * \return The PCO shader binary data.
154 */
155 const void *pco_shader_binary_data(pco_shader *shader)
156 {
157 if (!shader)
158 return NULL;
159 return shader->binary.data;
160 }