blob: 93630769019962af19fef06073d509ffd251827d [file] [log] [blame]
Viet-Hoa Do3389f532023-07-05 17:36:40 +01001/*
2 * Copyright (c) 2023 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
25#include "src/cl/CLKernelWriter.h"
26#include "ckw/Error.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010027#include "ckw/Kernel.h"
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010028#include "ckw/TileOperand.h"
Gunes Bayirab0b7502023-07-11 14:57:36 +010029#include "src/cl/CLHelpers.h"
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010030#include "src/cl/CLTensorArgument.h"
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010031#include "src/cl/CLTile.h"
Gunes Bayirab0b7502023-07-11 14:57:36 +010032#include <cstdint>
Viet-Hoa Do3389f532023-07-05 17:36:40 +010033
34namespace ckw
35{
36
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010037CLKernelWriter::CLKernelWriter() = default;
Viet-Hoa Do3389f532023-07-05 17:36:40 +010038CLKernelWriter::~CLKernelWriter() = default;
39
40std::unique_ptr<Kernel> CLKernelWriter::emit_kernel(const std::string &name)
41{
42 CKW_UNUSED(name);
43 CKW_THROW_MSG("Not implemented!");
44}
45
46void CLKernelWriter::comment(const std::string &text)
47{
48#ifdef COMPUTE_KERNEL_WRITER_DEBUG_ENABLED
49
50 CKW_ASSERT(text.find("\n") == text.npos);
51 CKW_ASSERT(text.find("\r") == text.npos);
52
53 append_code("// ", text, "\n");
54
55#else // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED
56
57 CKW_UNUSED(text);
58
59#endif // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED
60}
61
62const std::string &CLKernelWriter::body_source_code() const
63{
64 return _body_source_code;
65}
66
Viet-Hoa Do0b23e0e2023-07-25 14:00:46 +010067TensorOperand CLKernelWriter::declare_tensor_argument(const std::string &name, const TensorInfo &info)
68{
69 const auto fullname = generate_full_name(name);
70
71 auto tensor = std::make_unique<CLTensorArgument>(fullname, info, false /* return_dims_by_value */);
72 const auto operand = create_tensor_operand(*tensor);
73
74 _tensors.insert(std::move(tensor));
75
76 return operand;
77}
78
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010079TileOperand CLKernelWriter::declare_tile(const std::string &name, const TileInfo &tile_info)
Gunes Bayirab0b7502023-07-11 14:57:36 +010080{
81 const std::string fullname = generate_full_name(name);
82
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010083 const int32_t height = tile_info.height();
84 const int32_t width = tile_info.width();
Gunes Bayirab0b7502023-07-11 14:57:36 +010085 const DataType data_type = tile_info.data_type();
86
87 for(int32_t row = 0; row < height; ++row)
88 {
89 const std::string cl_type = cl_get_variable_datatype_as_string(data_type, width);
90 append_code(cl_type, " ", fullname, std::to_string(row), ";\n");
91 }
92
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010093 auto tile = std::make_unique<CLTile>(name, tile_info);
94 const auto operand = create_tile_operand(*tile);
Gunes Bayirab0b7502023-07-11 14:57:36 +010095
Viet-Hoa Do25d26f42023-07-20 17:31:47 +010096 _tiles.insert(std::move(tile));
97
98 return operand;
Gunes Bayirab0b7502023-07-11 14:57:36 +010099}
100
Gunes Bayir366514d2023-07-27 22:52:32 +0100101void CLKernelWriter::op_write_raw_code(const std::string &raw_code)
102{
103 append_code(raw_code);
104}
105
Viet-Hoa Do3389f532023-07-05 17:36:40 +0100106} // namespace ckw