Blender V5.3
gpu/shader_tool/attribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "intermediate.hh"
10#include "metadata.hh"
11#include "processor.hh"
12
13#include <unordered_set>
14
15namespace blender::gpu::shader {
16using namespace std;
17using namespace shader::parser;
18using namespace shader::parser::ast;
19using namespace metadata;
20
21void SourceProcessor::lower_noop_attributes(Parser &parser)
22{
23 parser().foreach_token(SquareOpen, [&](Token par_open) {
24 if (par_open.next() != '[') {
25 return;
26 }
27 Scope attributes = par_open.next().scope();
28 attributes.foreach_attribute([&](Token attr, Scope) {
29 if (attr.str() == "maybe_unused" || attr.str() == "fallthrough") {
30 if (attr.next() == ',') {
31 parser.erase(attr, attr.next());
32 }
33 else if (attr.prev() == ',') {
34 parser.erase(attr.prev(), attr);
35 }
36 else if (attr.next() == ']') {
37 parser.erase(attributes.scope());
38 }
39 }
40 });
41 });
42
43 parser.apply_mutations();
44}
45
46void SourceProcessor::lint_attributes(Parser &parser)
47{
48 parser().foreach_token(SquareOpen, [&](Token par_open) {
49 if (par_open.next() != '[') {
50 return;
51 }
52 Scope attributes = par_open.next().scope();
53 bool invalid = false;
54 attributes.foreach_attribute([&](Token attr, Scope attr_scope) {
55 string attr_str = string(attr.str());
56 if (attr_str == "base_instance" || attr_str == "clip_distance" ||
57 attr_str == "force_inline" || attr_str == "compilation_constant" ||
58 attr_str == "compute" || attr_str == "shared" || attr_str == "early_fragment_tests" ||
59 attr_str == "flat" || attr_str == "frag_coord" || attr_str == "frag_stencil_ref" ||
60 attr_str == "fragment" || attr_str == "front_facing" || attr_str == "bary_coord" ||
61 attr_str == "global_invocation_id" || attr_str == "in" || attr_str == "instance_id" ||
62 attr_str == "instance_index" || attr_str == "layer" ||
63 attr_str == "local_invocation_id" || attr_str == "local_invocation_index" ||
64 attr_str == "no_perspective" || attr_str == "num_work_groups" || attr_str == "out" ||
65 attr_str == "subpass_in" || attr_str == "point_coord" || attr_str == "point_size" ||
66 attr_str == "position" || attr_str == "push_constant" || attr_str == "resource_table" ||
67 attr_str == "smooth" || attr_str == "vertex_id" || attr_str == "legacy_info" ||
68 attr_str == "legacy_iface" || attr_str == "vertex" || attr_str == "viewport_index" ||
69 attr_str == "work_group_id" || attr_str == "maybe_unused" || attr_str == "fallthrough" ||
70 attr_str == "nodiscard" || attr_str == "node" || attr_str == "clip_control" ||
71 attr_str == "texture_atomic")
72 {
73 if (attr_scope.is_valid()) {
74 report_error(attr, "This attribute requires no argument");
75 invalid = true;
76 }
77 }
78 else if (attr_str == "acceleration_structure" || attr_str == "attribute" ||
79 attr_str == "index" || attr_str == "frag_color" || attr_str == "frag_depth" ||
80 attr_str == "uniform" || attr_str == "condition" ||
81 attr_str == "raster_order_group" || attr_str == "frequency" ||
82 attr_str == "sampler" || attr_str == "specialization_constant")
83 {
84 if (attr_scope.is_invalid()) {
85 report_error(attr, "This attribute requires 1 argument");
86 invalid = true;
87 }
88 }
89 else if (attr_str == "storage" || attr_str == "subpass_input") {
90 if (attr_scope.is_invalid()) {
91 report_error(attr, "This attribute requires 2 arguments");
92 invalid = true;
93 }
94 }
95 else if (attr_str == "image") {
96 if (attr_scope.is_invalid()) {
97 report_error(attr, "This attribute requires 3 arguments");
98 invalid = true;
99 }
100 }
101 else if (attr_str == "local_size") {
102 if (attr_scope.is_invalid()) {
103 report_error(attr, "This attribute requires at least 1 argument");
104 invalid = true;
105 }
106 }
107 else if (attr_str == "metal_max_total_threads_per_threadgroup") {
108 if (attr_scope.is_invalid()) {
109 report_error(attr, "This attribute requires at least 1 argument");
110 invalid = true;
111 }
112 }
113 else if (attr_str == "host_shared") {
114 if (attributes.front().prev().prev() != Struct && attributes.front().prev().prev() != Enum)
115 {
116 report_error(
117 attr, "host_shared attributes must be placed after a struct or an enum definition");
118 invalid = true;
119 }
120 /* Placement already checked. */
121 return;
122 }
123 else if (attr_str == "unroll" || attr_str == "unroll_n") {
124 if (attributes.front().prev().prev().scope().front().prev() != For) {
125 report_error(attr, "[[unroll]] attribute must be declared after a 'for' statement");
126 invalid = true;
127 }
128 /* Placement already checked. */
129 return;
130 }
131 else if (attr_str == "static_branch") {
132 if (attributes.front().prev().prev().scope().front().prev() != If) {
133 report_error(attr,
134 "[[static_branch]] attribute must be declared after a 'if' condition");
135 invalid = true;
136 }
137 /* Placement already checked. */
138 return;
139 }
140 else {
141 report_error(attr, "Unrecognized attribute");
142 invalid = true;
143 /* Attribute already invalid, don't check placement. */
144 return;
145 }
146
147 if (attr_str == "fallthrough") {
148 /* Placement is too complicated to check. C++ compilation should already have checked. */
149 return;
150 }
151
152 Token prev_tok = attributes.front().prev().prev();
153 if (prev_tok == '(' || prev_tok == '{' || prev_tok == ';' || prev_tok == ',' ||
154 prev_tok == '}' || prev_tok == ')' || prev_tok == '\n' || prev_tok == ' ' ||
155 prev_tok == '>' || prev_tok.is_invalid() ||
156 prev_tok.scope().type() == ScopeType::Preprocessor)
157 {
158 /* Placement is maybe correct. Could refine a bit more. */
159 }
160 else {
161 report_error(attr, "attribute must be declared at a start of a declaration");
162 invalid = true;
163 }
164 });
165 if (invalid) {
166 /* Erase invalid attributes to avoid spawning more errors. */
167 parser.erase(attributes.scope());
168 }
169 });
170 parser.apply_mutations();
171}
172
173void SourceProcessor::lint_attributes_ast(Parser &parser)
174{
175 struct ArgCount {
176 int start, size;
177
178 int last() const
179 {
180 return start + size - 1;
181 }
182 };
183
184 unordered_map<string, ArgCount> attr_map = {
185 /* No argument. */
186 {"base_instance", ArgCount(0, 0)},
187 {"clip_control", ArgCount(0, 0)},
188 {"clip_distance", ArgCount(0, 0)},
189 {"compilation_constant", ArgCount(0, 0)},
190 {"compute", ArgCount(0, 0)},
191 {"early_fragment_tests", ArgCount(0, 0)},
192 {"fallthrough", ArgCount(0, 0)},
193 {"flat", ArgCount(0, 0)},
194 {"frag_coord", ArgCount(0, 0)},
195 {"frag_stencil_ref", ArgCount(0, 0)},
196 {"fragment", ArgCount(0, 0)},
197 {"front_facing", ArgCount(0, 0)},
198 {"bary_coord", ArgCount(0, 0)},
199 {"global_invocation_id", ArgCount(0, 0)},
200 {"host_shared", ArgCount(0, 0)},
201 {"in", ArgCount(0, 0)},
202 {"instance_id", ArgCount(0, 0)},
203 {"instance_index", ArgCount(0, 0)},
204 {"layer", ArgCount(0, 0)},
205 {"legacy_info", ArgCount(0, 0)},
206 {"legacy_iface", ArgCount(0, 0)},
207 {"local_invocation_id", ArgCount(0, 0)},
208 {"local_invocation_index", ArgCount(0, 0)},
209 {"maybe_unused", ArgCount(0, 0)},
210 {"no_perspective", ArgCount(0, 0)},
211 {"node", ArgCount(0, 0)},
212 {"nodiscard", ArgCount(0, 0)},
213 {"num_work_groups", ArgCount(0, 0)},
214 {"out", ArgCount(0, 0)},
215 {"point_coord", ArgCount(0, 0)},
216 {"point_size", ArgCount(0, 0)},
217 {"position", ArgCount(0, 0)},
218 {"push_constant", ArgCount(0, 0)},
219 {"resource_table", ArgCount(0, 0)},
220 {"shared", ArgCount(0, 0)},
221 {"smooth", ArgCount(0, 0)},
222 {"static_branch", ArgCount(0, 0)},
223 {"subpass_in", ArgCount(0, 0)},
224 {"texture_atomic", ArgCount(0, 0)},
225 {"unroll", ArgCount(0, 0)},
226 {"vertex", ArgCount(0, 0)},
227 {"vertex_id", ArgCount(0, 0)},
228 {"viewport_index", ArgCount(0, 0)},
229 {"work_group_id", ArgCount(0, 0)},
230 /* 1 argument. */
231 {"attribute", ArgCount(1, 1)},
232 {"condition", ArgCount(1, 1)},
233 {"frag_color", ArgCount(1, 1)},
234 {"frag_depth", ArgCount(1, 1)},
235 {"frequency", ArgCount(1, 1)},
236 {"unroll_n", ArgCount(1, 1)},
237 {"index", ArgCount(1, 1)},
238 {"metal_max_total_threads_per_threadgroup", ArgCount(1, 1)},
239 {"raster_order_group", ArgCount(1, 1)},
240 {"sampler", ArgCount(1, 1)},
241 {"specialization_constant", ArgCount(1, 1)},
242 {"uniform", ArgCount(1, 1)},
243 {"acceleration_structure", ArgCount(1, 1)},
244 /* 2 arguments. */
245 {"storage", ArgCount(2, 1)},
246 {"subpass_input", ArgCount(2, 1)},
247 /* 3 arguments. */
248 {"image", ArgCount(3, 1)},
249 /* 1-3 arguments. */
250 {"local_size", ArgCount(1, 3)},
251 };
252
253 for (Attr attr : parser.root().children_of_type<Attr>()) {
254 FuncParamList param = attr.parameters();
255 string attr_str(attr.identifier().str());
256 if (!attr_map.contains(attr_str)) {
257 report_error(attr, "Unrecognized attribute");
258 return;
259 }
260
261 const auto &param_range = attr_map[string(attr.identifier().str())];
262 if (param_range.size == 0 && param.is_valid()) {
263 report_error(param, "This attribute requires no argument");
264 return;
265 }
266
267 int arg_count = param.child_count();
268 if (param_range.size == 1 && arg_count != param_range.start) {
269 report_error(param,
270 "This attribute requires " + to_string(param_range.start) + " argument(s)");
271 return;
272 }
273 if (param_range.size != 1 && arg_count < param_range.start && arg_count > param_range.last()) {
274 report_error(param,
275 "This attribute requires between " + to_string(param_range.start) + " and " +
276 to_string(param_range.last()) + " argument(s)");
277 return;
278 }
279
280 NodeType parent_type = attr.parent().type();
281
282 if (attr_str == "host_shared" && parent_type != NodeType::ClassDecl) {
283 report_error(attr, "host_shared attribute must be inside a struct or an enum definition");
284 return;
285 }
286
287 NodeType parent_parent_type = attr.parent().parent().type();
288
289 if ((attr_str == "unroll" || attr_str == "unroll_n") &&
290 ((parent_type != NodeType::Condition) || (parent_parent_type != NodeType::ForLoop)))
291 {
292 report_error(attr, "unroll attribute must be declared after a for loop statement");
293 return;
294 }
295
296 if (attr_str == "static_branch" &&
297 (parent_type != NodeType::Condition || parent_parent_type != NodeType::IfStmt ||
298 parent_parent_type != NodeType::ElseIfStmt))
299 {
300 report_error(attr, "static_branch attribute must be declared after a condition");
301 return;
302 }
303 }
304}
305
306/* Merge attribute scopes. They are equivalent in the C++ standard.
307 * This allow to simplify parsing later on.
308 * `[[a]] [[b]]` > `[[a, b]]` */
309void SourceProcessor::lower_attribute_sequences(Parser &parser)
310{
311 do {
312 parser().foreach_match("[[..]][[..]]", [&](vector<Token> toks) {
313 parser.insert_before(toks[4], ",");
314 parser.erase(toks[4], toks[7]);
315 });
316 } while (parser.apply_mutations());
317}
318
319void SourceProcessor::lower_attribute_sequences_ast(Parser &parser)
320{
321 for (AttrList list : parser.root().descendants_of_type<AttrList>()) {
322 if (list.parent().type() == NodeType::AttrList) {
323 Token front = list.front().prev(2);
324 parser.replace(front, ",");
325 parser.erase(front.next(1), front.next(3));
326 }
327 }
328}
329
330} // namespace blender::gpu::shader
int size(const AssetLibraryReference *library_reference)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
@ For
Definition types.hh:91
@ Enum
Definition types.hh:106
@ Struct
Definition types.hh:117
@ SquareOpen
Definition types.hh:57
@ If
Definition types.hh:97
void foreach_attribute(CallbackFn callback) const
Definition scope.hh:367
Token prev(int i=1) const
Definition lexit.hh:376