blob: 4c720ea1aa5af887cbf136f27737a7c1437a5dd9 [file] [log] [blame]
Giorgio Arena232c4522022-03-03 10:09:01 +00001/*
2 * Copyright (c) 2022 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)
25
26#ifndef ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H
27#define ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H
28
29#include "arm_compute/core/CL/CLCompileContext.h"
Giorgio Arena892b70a2022-03-30 12:23:10 +010030#include "arm_compute/core/CL/CLKernelLibrary.h"
Giorgio Arena232c4522022-03-03 10:09:01 +000031#include "arm_compute/core/Error.h"
32#include "arm_compute/core/GPUTarget.h"
Gunes Bayir8a879832022-03-10 21:21:01 +000033#include "src/core/common/Macros.h"
Giorgio Arenabd44caa2022-03-15 13:45:15 +000034#include "support/StringSupport.h"
Giorgio Arena232c4522022-03-03 10:09:01 +000035
36#include "src/core/experimental/dynamic_fusion/ClKernelBuildingAPI.h"
37
38#include <queue>
39#include <stack>
40#include <string>
41#include <unordered_set>
42
43namespace arm_compute
44{
45namespace experimental
46{
47namespace dynamic_fusion
48{
49/** We introduce the concept of *Shared Variables* in the context of kernel building.
50 * They are variables that can be accessed / shared among all the kernel components within a single kernel.
51 * For now we consider 2 groups of shared variables:
52 * Argument: The argument variables (parameters) of a kernel
53 * Automatic: The automatic variables declared inside a kernel
54 * All Shared Variables have the same kernel scope, and are thus visible to all kernel components
55*/
56
57enum class SharedVarIO
58{
59 Input,
60 Output
61};
62
63enum class SharedVarGroup
64{
65 Argument, // Parameters to a kernel function
66 Automatic // Automatic variables declared within the kernel body
67};
68
Gunes Bayir8a879832022-03-10 21:21:01 +000069/** Specifies a shared variable link for a component.
70 * It describes all the information that's available when a component is constructed / added:
Giorgio Arena232c4522022-03-03 10:09:01 +000071 * e.g. its linkage (via ArgumentID and io) and its group
72 * This is not shared variable on its own, but is used for instantiating a SharedVar when building the code
73 */
74struct SharedVarLink
75{
76 ArgumentID arg_id{ g_arg_placeholder };
77 SharedVarIO io{ SharedVarIO::Input };
78 SharedVarGroup group{ SharedVarGroup::Argument };
79 bool is_empty() const
80 {
81 return arg_id == g_arg_placeholder;
82 }
83};
84
85/** A table of all the variables used in the kernel / blueprint
86 * NOTE: the order they appear in the table is the order of their "declaration" in the component code, and is also their ID
87 * NOTE: the variables all have the scope of the full kernel function
88 */
89class SharedVarTable
90{
91public:
92 struct SharedVar
93 {
94 SharedVarGroup group;
95 std::string uniq_name; // Unique name, also the final variable name used in the built code
96 ClKernelArgRuntimeDescriptor desc; // Automatic variables can and should still be described using this struct
97 };
98
99 using Arguments = std::vector<SharedVar>;
100
101 /** @note: The order of insertion is important. There is one precondition:
102 * PRECOND: The components have been sorted topologically / is being traversed in topological order
103 * This ensures that all the consumer var links (Output, Automatic Links) can consume (return) the producer var links when they're referred
104 */
105 SharedVar add(SharedVarLink var_link, ClKernelArgRuntimeDescriptor runtime_desc, const std::string &name = "unnamed")
106 {
107 ARM_COMPUTE_ERROR_ON_MSG(var_link.is_empty(), "Non-empty SharedVarLink expected");
108 auto var_id = _num_var;
109 std::stringstream ss;
110 ss << name << "_" << var_id;
111 const auto uniq_name = ss.str();
112 SharedVar var{ var_link.group, uniq_name, runtime_desc };
113
114 if(var_link.group == SharedVarGroup::Argument)
115 {
116 _arguments.emplace(var_id, var);
117 _num_var++;
118 _var_id_lut[var_link.arg_id] = var_id;
119 }
120 else if(var_link.group == SharedVarGroup::Automatic)
121 {
122 if(var_link.io == SharedVarIO::Output)
123 {
124 _global_vars.emplace(var_id, var);
125 _num_var++;
126 _var_id_lut[var_link.arg_id] = var_id;
127 }
128 else
129 {
130 // For the input link, the var (and thus its arg_id) will always have been added by the time we get here if we traverse components in topological order
131 var = get_var(var_link.arg_id);
132 }
133 }
134 else
135 {
136 ARM_COMPUTE_ERROR("Unrecognised SharedVarGroup");
137 }
138 return var;
139 }
140
141 SharedVar get_var(ArgumentID arg_id) const
142 {
143 const auto var_id = _var_id_lut.at(arg_id); // arg_id has to exist in lut to begin with
144 auto it = _global_vars.find(var_id);
145 if(it != _global_vars.end())
146 {
147 return it->second;
148 }
149 it = _arguments.find(var_id);
150 if(it != _arguments.end())
151 {
152 return it->second;
153 }
154 ARM_COMPUTE_ERROR("Cannot find component variable");
155 }
156
157 /** @note The arguments are returned in the order they are added
158 */
159 Arguments get_kernel_arguments() const
160 {
161 Arguments args{};
162 for(const auto &a : _arguments)
163 {
164 args.push_back(a.second);
165 }
166 return args;
167 }
168
169private:
170 using VarID = int32_t;
171
172private:
173 std::map<VarID, SharedVar> _global_vars{};
174 std::map<VarID, SharedVar> _arguments{};
175 std::unordered_map<ArgumentID, VarID> _var_id_lut{};
176 VarID _num_var{ 0 };
177};
178
179enum class ComponentType
180{
181 Simple,
182 Complex,
183 Store
184};
185
186using ComponentID = int32_t;
187using ComponentList = std::vector<ComponentID>;
188class IClKernelComponent
189{
190public:
191 using Link = SharedVarLink;
192 using Tag = std::string;
193 struct TagVal
194 {
195 TagVal() = default;
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000196 TagVal(const SharedVarTable::SharedVar &var)
Giorgio Arena232c4522022-03-03 10:09:01 +0000197 : value{ var.uniq_name }
198 {
199 }
200
201 TagVal(ComponentID id)
202 : value{ std::to_string(id) }
203 {
204 }
205
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000206 TagVal(const std::string &val)
207 : value{ val }
208 {
209 }
210
Giorgio Arena232c4522022-03-03 10:09:01 +0000211 std::string value{};
212 };
213 using TagLUT = std::unordered_map<Tag, TagVal>; // Used to instantiating a code template / replacing tags
214public:
Gunes Bayir8a879832022-03-10 21:21:01 +0000215 IClKernelComponent(const ClKernelBlueprint *blueprint)
216 : _blueprint(blueprint)
217 {
218 }
219
220 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(IClKernelComponent);
221
Giorgio Arena232c4522022-03-03 10:09:01 +0000222 virtual ~IClKernelComponent() = default;
223 virtual ComponentType get_component_type() const = 0;
224 virtual std::vector<Link> get_links() const = 0;
225 virtual std::string name() const = 0;
226
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000227 // @note: some tags can be unused since they could be used only for the macros, or only for the component code
Giorgio Arena232c4522022-03-03 10:09:01 +0000228 static std::string replace_tags(const std::string &code_template, const TagLUT &tags)
229 {
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000230 std::string replaced_code = "";
231 bool scanning_pattern = false;
232 std::string pattern_found = "";
Giorgio Arena232c4522022-03-03 10:09:01 +0000233 for(size_t i = 0; i < code_template.size() - 1; ++i)
234 {
235 if(!scanning_pattern)
236 {
237 if(code_template[i] == '{' && code_template[i + 1] == '{')
238 {
239 i += 1;
240 scanning_pattern = true;
241 pattern_found = "";
242 }
243 else
244 {
245 replaced_code += code_template[i];
246 }
247 }
248 else
249 {
250 if(code_template[i] == '}' && code_template[i + 1] == '}')
251 {
252 i += 1;
253 scanning_pattern = false;
254 std::string err = "Pattern " + pattern_found + " not found in tags";
255 ARM_COMPUTE_ERROR_ON_MSG(tags.find(pattern_found) == tags.end(), err.c_str());
256 replaced_code += tags.find(pattern_found)->second.value;
Giorgio Arena232c4522022-03-03 10:09:01 +0000257 }
258 else
259 {
260 pattern_found += code_template[i];
261 }
262 }
263 }
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000264
Giorgio Arena232c4522022-03-03 10:09:01 +0000265 return replaced_code;
266 }
267 ComponentID id() const
268 {
269 return _id;
270 }
271 void set_id(ComponentID id)
272 {
273 _id = id;
274 }
275
276 virtual std::set<std::string> get_headers_list() const
277 {
278 return std::set<std::string> {};
279 }
280
281 virtual std::string get_additional_macros() const
282 {
283 return "";
284 }
285
286 virtual std::string get_component_code() const
287 {
288 return "";
289 }
Gunes Bayir8a879832022-03-10 21:21:01 +0000290
291 virtual Window get_window() const
292 {
293 return Window{};
294 }
Giorgio Arena232c4522022-03-03 10:09:01 +0000295 /** "Allocate" all shared variables used in a component to the @p vtable, and generate a TagLUT used to instantiate the component code
296 *
297 * @param vtable
298 * @return TagLUT
299 */
300 virtual TagLUT allocate_vars(SharedVarTable &vtable) const = 0;
301
302 virtual std::string get_dst_addr_calculation() const
303 {
304 return "";
305 }
306
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000307 virtual CLBuildOptions generate_build_options() const
308 {
309 return CLBuildOptions{};
310 }
311
Gunes Bayir8a879832022-03-10 21:21:01 +0000312protected:
313 const ClKernelBlueprint *_blueprint;
314
Giorgio Arena232c4522022-03-03 10:09:01 +0000315private:
316 ComponentID _id{};
317};
318
319using ComponentUniquePtr = std::unique_ptr<IClKernelComponent>;
320
321/** Intermediate representation of the final, complete kernel source.
322 */
323struct ClKernelBlueprint::Implementation
324{
325public:
326 Implementation() = default;
327 ~Implementation() = default;
328
329public:
330 ArgumentID add_kernel_argument(const ClTensorDescriptor &tensor_desc)
331 {
332 _kernel_arguments.insert(std::make_pair(_num_args, tensor_desc));
333 _shared_var_group_lut[_num_args] = SharedVarGroup::Argument;
334 return _num_args++;
335 }
336
337 ArgumentID add_intermediate_tensor()
338 {
339 _intermediate_tensors.insert(_num_args);
340 _shared_var_group_lut[_num_args] = SharedVarGroup::Automatic;
341 return _num_args++;
342 }
343
344 void set_tile_info(const TileDescriptor &tile_info)
345 {
346 _tile_info = tile_info;
347 }
348
349 SharedVarGroup group(ArgumentID arg_id) const
350 {
351 if(arg_id == g_arg_placeholder)
352 {
353 // In case of placeholder, don't care what we return;
354 return SharedVarGroup::Argument;
355 }
356 return _shared_var_group_lut.at(arg_id);
357 }
358
359 void validate_arg_ids(std::initializer_list<ArgumentID> args) const
360 {
361 for(const auto arg_id : args)
362 {
363 ARM_COMPUTE_UNUSED(arg_id);
364 ARM_COMPUTE_ERROR_ON_MSG(_kernel_arguments.find(arg_id) == _kernel_arguments.end() && _intermediate_tensors.find(arg_id) == _intermediate_tensors.end() && arg_id != g_arg_placeholder,
365 "Trying to use an argument that hasn't been added to the blueprint");
366 }
367 }
368
369 void add_component(ComponentUniquePtr component)
370 {
371 if(component->get_component_type() == ComponentType::Complex)
372 {
373 ++_num_complex_components;
374 ARM_COMPUTE_ERROR_ON_MSG(_num_complex_components > 1, "Only one complex component per blueprint is supported.");
375 }
376
377 // This flag specifies if the current component is the root of the component graph
378 // If the root is set to -1, it means that a root hasn't been added yet
379 bool is_graph_root = true;
380
381 // Get an unique ID for the component that's being added
382 const ComponentID component_id = _num_components++;
383 component->set_id(component_id);
384
385 // Add this component to the component graph. Don't connect it to anything yet
386 _component_graph.emplace(component_id, ComponentList{});
387
388 int32_t positional_arg = 0;
389
390 // For every { arg_id, arg_io } passed along with this component...
391 for(const auto &link : component->get_links())
392 {
393 const ArgumentID &arg_id = link.arg_id;
394 const SharedVarIO &arg_io = link.io;
395
396 // A component is considered root only if all its input arguments are kernel arguments (or placeholders, which means nullptr)
397 // This performs a check on every argument, and if one of them doesn't respect the condition, the component is not considered root
398 is_graph_root &= (_kernel_arguments.find(arg_id) != _kernel_arguments.end()) || (arg_io == SharedVarIO::Output) || (arg_id == g_arg_placeholder);
399
400 // Add the arg_id to the map describing the input/output relationship between an argument and the components that use it, if it doesn't yet exist there
401 if(_outgoing_components.find(arg_id) == _outgoing_components.end())
402 {
403 _outgoing_components.emplace(arg_id, ComponentList{});
404 _incoming_components.emplace(arg_id, ComponentList{});
405 }
406
407 // If it's an input argument, connect any other component that has it as output with this component
408 // Additionally, set this component as one that treats this argument as "Input" (append to index 0)
409 // This is used so that we keep track of whether two components use the same argument, one as input and one as output
410 if(arg_io == SharedVarIO::Input)
411 {
412 for(const auto &prev_component : _incoming_components[arg_id])
413 {
414 _component_graph[prev_component].push_back(component_id);
415 }
416
417 _outgoing_components[arg_id].push_back(component_id);
418 }
419 // If it's an output argument, connect this component with any other component that has it as input
420 // Additionally, set this component as one that treats this argument as "Output" (append to index 1)
421 else
422 {
Gunes Bayir8a879832022-03-10 21:21:01 +0000423 if(component->get_component_type() == ComponentType::Store)
424 {
425 ARM_COMPUTE_ERROR_ON_MSG(_dst_id >= 0, "Trying to add more than one dst argument to the graph");
426 _dst_id = arg_id;
427 }
428
Giorgio Arena232c4522022-03-03 10:09:01 +0000429 for(const auto &subseq_component : _outgoing_components[arg_id])
430 {
431 _component_graph[component_id].push_back(subseq_component);
432 }
433
434 _incoming_components[arg_id].push_back(component_id);
435 }
436
437 ++positional_arg;
438 }
439
440 if(is_graph_root)
441 {
442 ARM_COMPUTE_ERROR_ON_MSG(_graph_root >= 0, "Trying to add more than one root to the graph");
443 _graph_root = component_id;
444 }
445
446 // Finally, add this component to the dictionary of components
447 _components.insert(std::make_pair(component_id, std::move(component)));
448 }
449
450 std::string build_kernel_name() const
451 {
452 std::string name = "";
453
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000454 traverse([&](std::stack<ComponentID> stack)
Giorgio Arena232c4522022-03-03 10:09:01 +0000455 {
456 name += _components.find(stack.top())->second->name() + (stack.size() > 2 ? "___" : "");
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000457 });
Giorgio Arena232c4522022-03-03 10:09:01 +0000458
Giorgio Arena232c4522022-03-03 10:09:01 +0000459 return name;
460 }
461
462 std::string build_code()
463 {
464 ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph");
465
466 // These data structures will hold the data from all the components in the blueprint
467 std::set<std::string> headers_list{};
468 std::set<std::string> additional_macros{};
469 std::vector<std::string> component_codes{}; // vector because order matters
470
471 // Go through the components graph (topological sort) and fill the data structures above
472 auto stack = topological_sort();
473 while(!stack.empty())
474 {
475 auto curr_component_id = stack.top();
476 auto &curr_component = _components.find(curr_component_id)->second;
477
478 auto curr_headers_list = curr_component->get_headers_list();
479 auto curr_additional_macros = curr_component->get_additional_macros();
480 auto curr_component_code = curr_component->get_component_code();
481 const auto var_lut = curr_component->allocate_vars(_vtable); // Ideally can be merged with get_component_code once we have finer-grained code generation technique
482 component_codes.push_back(IClKernelComponent::replace_tags(curr_component_code, var_lut));
483
484 headers_list.insert(curr_headers_list.begin(), curr_headers_list.end());
485 if(!curr_additional_macros.empty()) // Some components might not have any
486 {
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000487 additional_macros.insert(IClKernelComponent::replace_tags(curr_additional_macros, var_lut));
Giorgio Arena232c4522022-03-03 10:09:01 +0000488 }
489
490 stack.pop();
491 }
492
493 // This section assembles the data gathered by traversing the graph into the string "code"
494 std::string code = "";
495
496 for(auto &header : headers_list)
497 {
Giorgio Arena892b70a2022-03-30 12:23:10 +0100498#if defined(EMBEDDED_KERNELS)
499 code += CLKernelLibrary::get().get_program(header).first;
500#else // defined(EMBEDDED_KERNELS)
Giorgio Arena232c4522022-03-03 10:09:01 +0000501 code += "#include \"" + header + "\"\n";
Giorgio Arena892b70a2022-03-30 12:23:10 +0100502#endif // defined(EMBEDDED_KERNELS)
Giorgio Arena232c4522022-03-03 10:09:01 +0000503 }
504
505 for(auto &macros : additional_macros)
506 {
507 code += macros;
508 }
509
510 code += generate_kernel_signature(_vtable.get_kernel_arguments());
511
512 code += "\n{\n\n";
513
514 code += " //------------------ START KERNEL_BUILDER_COORDINATE ---------------------\n\n";
515 code += generate_global_section();
516 code += " //------------------ END KERNEL_BUILDER_COORDINATE ---------------------\n";
517
518 for(auto &component_code : component_codes)
519 {
520 code += component_code;
521 }
522
523 code += "}\n";
524
525 return code;
526 }
527
528 std::string build_config_id() const
529 {
530 return "";
531 }
532
533 CLBuildOptions build_options() const
534 {
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000535 CLBuildOptions build_opts{};
536
537 traverse([&](std::stack<ComponentID> stack)
538 {
539 build_opts.add_options(_components.find(stack.top())->second->generate_build_options().options());
540 });
541
542 return build_opts;
543 }
544
545 TileDescriptor get_tile_info() const
546 {
547 return _tile_info;
Giorgio Arena232c4522022-03-03 10:09:01 +0000548 }
549
550 Window get_execution_window() const
551 {
Gunes Bayir8a879832022-03-10 21:21:01 +0000552 ARM_COMPUTE_ERROR_ON_MSG(_graph_root < 0, "No root found in the component graph");
553 ARM_COMPUTE_ERROR_ON_MSG(_dst_id == -1, "Destination Tensor Id should be ready before calling get_execution_window()");
554
555 return _components.find(_graph_root)->second->get_window();
556 }
557
558 ArgumentID get_dst_id() const
559 {
560 return _dst_id;
Giorgio Arena232c4522022-03-03 10:09:01 +0000561 }
562
563 ClKernelArgList get_arguments() const
564 {
565 ClKernelArgList arg_list{};
566 for(const auto &arg_var : _vtable.get_kernel_arguments())
567 {
568 arg_list.push_back(arg_var.desc);
569 }
570 return arg_list;
571 }
572
Gunes Bayir8a879832022-03-10 21:21:01 +0000573 const ClTensorDescriptor *get_kernel_argument(const ArgumentID id) const
574 {
575 auto it = _kernel_arguments.find(id);
576 if(it != _kernel_arguments.end())
577 {
578 return &_kernel_arguments.find(id)->second;
579 }
580 return nullptr;
581 }
582
583 ITensorInfo *get_kernel_argument_info(const ArgumentID id) const
584 {
585 const ClTensorDescriptor *arg_desc = get_kernel_argument(id);
586 if(arg_desc != nullptr)
587 {
588 return arg_desc->tensor_info;
589 }
590 return nullptr;
591 }
592
Giorgio Arena232c4522022-03-03 10:09:01 +0000593private:
594 void topological_sort_utility(ComponentID component_id, std::unordered_set<ComponentID> &visited, std::stack<ComponentID> &stack) const
595 {
596 visited.insert(component_id);
597
598 for(auto connected_component : _component_graph.find(component_id)->second)
599 {
600 if(visited.find(connected_component) == visited.end())
601 {
602 topological_sort_utility(connected_component, visited, stack);
603 }
604 }
605
606 stack.push(component_id);
607 }
608
609 std::stack<ComponentID> topological_sort() const
610 {
611 std::stack<ComponentID> stack{};
612 std::unordered_set<ComponentID> visited{};
613
614 topological_sort_utility(_graph_root, visited, stack);
615
616 return stack;
617 }
618
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000619 void traverse(const std::function<void(std::stack<ComponentID>)> &func) const
620 {
621 std::stack<ComponentID> stack = topological_sort();
622
623 while(!stack.empty())
624 {
625 func(stack);
626 stack.pop();
627 }
628 }
629
Giorgio Arena232c4522022-03-03 10:09:01 +0000630 std::string generate_argument_declaration(const SharedVarTable::SharedVar &var) const
631 {
632 ARM_COMPUTE_ERROR_ON_MSG(var.group != SharedVarGroup::Argument, "An argument declaration can only be generated from a kernel argument");
633 std::string code;
634 switch(var.desc.tensor_arg_type)
635 {
636 case TensorArgType::Image:
637 {
638 code += "IMAGE_DECLARATION(" + var.uniq_name + ")";
639 break;
640 }
641 case TensorArgType::Image_3D:
642 {
643 code += "IMAGE_DECLARATION(" + var.uniq_name + "),\n";
644 code += "uint " + var.uniq_name + "_stride_z";
645 break;
646 }
647 case TensorArgType::Image_3D_Export_To_ClImage2D:
648 {
649 code += "__read_only image2d_t " + var.uniq_name + "_img,\n";
650 code += "uint " + var.uniq_name + "_stride_z,\n";
651 break;
652 }
653 default:
654 {
655 ARM_COMPUTE_ERROR("Unsupported declaration generation for TensorArgType");
656 }
657 }
658 return code;
659 }
660
661 std::string generate_kernel_signature(const SharedVarTable::Arguments &argument_list) const
662 {
663 std::string code = "\n__kernel void " + build_kernel_name() + "(";
664
665 for(const auto &arg : argument_list)
666 {
667 code += "\n " + generate_argument_declaration(arg) + ",";
668 }
669
670 code[code.length() - 1] = ')';
671
672 return code;
673 }
674
675 std::string generate_global_section() const
676 {
677 std::string code = " uint g_x = get_global_id(0);\n";
678 code += " uint g_y = get_global_id(1);\n";
679 code += " uint g_z = get_global_id(2);\n\n";
680
681 size_t tile_dim_x = _tile_info.empty() ? 1 : _tile_info.tile_dims.x();
682 size_t tile_dim_y = _tile_info.empty() ? 1 : _tile_info.tile_dims.y();
683
684 switch(_tile_info.clipping)
685 {
686 case ClippingStrategy::TOP_LEFT:
687 code += " const bool g_cond_x = (g_x == 0);\n";
688 code += " const bool g_cond_y = (g_y == 0);\n";
689 break;
690 case ClippingStrategy::TOP_RIGHT:
691 code += " const bool g_cond_x = ((g_x + 1) * " + std::to_string(tile_dim_x) + " >= " + std::to_string(_tile_info.boundaries.x()) + ");\n";
692 code += " const bool g_cond_y = (g_y == 0);\n";
693 break;
694 case ClippingStrategy::BOTTOM_LEFT:
695 code += " const bool g_cond_x = (g_x == 0);\n";
696 code += " const bool g_cond_y = ((g_y + 1) * " + std::to_string(tile_dim_y) + " >= " + std::to_string(_tile_info.boundaries.y()) + ");\n";
697 break;
698 case ClippingStrategy::BOTTOM_RIGHT:
699 code += " const bool g_cond_x = ((g_x + 1) * " + std::to_string(tile_dim_x) + " >= " + std::to_string(_tile_info.boundaries.x()) + ");\n";
700 code += " const bool g_cond_y = ((g_y + 1) * " + std::to_string(tile_dim_y) + " >= " + std::to_string(_tile_info.boundaries.y()) + ");\n";
701 break;
702 default:
703 ARM_COMPUTE_ERROR("Unsupported clipping strategy");
704 }
705
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000706 code += "\n REPEAT_VAR_INIT_TO_CONST(" + std::to_string(tile_dim_y) + ", uint, g_zout, 0);\n";
Giorgio Arena232c4522022-03-03 10:09:01 +0000707 code += " REPEAT_VAR_INIT_TO_CONST(16, uint, g_zero, 0);\n\n";
708
709 return code;
710 }
711
712 TileDescriptor _tile_info{};
713
714 int32_t _num_args{};
715 int32_t _num_components{};
716 int32_t _num_complex_components{};
717
Giorgio Arenabd44caa2022-03-15 13:45:15 +0000718 ArgumentID _dst_id{ -1 }; // Initially set to -1, which means the graph has no dst yet, since node IDs are positive numbers
Gunes Bayir8a879832022-03-10 21:21:01 +0000719
Giorgio Arena232c4522022-03-03 10:09:01 +0000720 // Argument, components and intermediate tensors IDs with corresponding ptrs (except intermediate)
721 std::unordered_map<ComponentID, ComponentUniquePtr> _components{};
722 std::unordered_map<ArgumentID, ClTensorDescriptor> _kernel_arguments{};
723 std::unordered_set<ArgumentID> _intermediate_tensors{};
724 // Argument group lookup. Can be replaced by extending the ArgumentID type to include group info
725 std::unordered_map<ArgumentID, SharedVarGroup> _shared_var_group_lut{};
726
727 // Tracks all variables (e.g.: kernel arguments, kernel "global variables")
728 SharedVarTable _vtable{};
729
730 // Component directed graph (represented by an adjecency list of Component IDs)
731 // This is used to understand the ordering and bindings between components when generating the kernel
732 // It's initially set to -1 which means the graph has no root yet, since node IDs are positive numbers
733 ComponentID _graph_root{ -1 };
734 std::unordered_map<ComponentID, ComponentList> _component_graph{};
735
736 // Additional data structures used to define the relationships between components and arguments
737 // For each argument, it contains the list of components that consider it as an incoming or an outgoing argument
738 // E.g. tensor0 -> component0 -> tensor1
739 // _outgoing_components[tensor0] == {component0} (component0 is the outgoing component of tensor0. Component0 treats tensor0 as an input tensor)
740 // _incoming_components[tensor1] == {component0} (component0 is the incoming component of tensor1. Component1 treats tensor1 as an output tensor)
741 std::unordered_map<ArgumentID, ComponentList> _outgoing_components{};
742 std::unordered_map<ArgumentID, ComponentList> _incoming_components{};
743};
744
745} // namespace dynamic_fusion
746} // namespace experimental
747} // namespace arm_compute
748#endif //ARM_COMPUTE_EXPERIMENTAL_DYNAMICFUSION_IMPL_COMMON_H
749
750#endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)