File: generate_cpp_array.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (147 lines) | stat: -rw-r--r-- 5,074 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2020-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>

constexpr int minArgCount = 7;

static void showUsage(std::string name) {
    std::cerr << "Usage " << name << "<option(s)> - ALL BUT -p, --platform MUST BE SPECIFIED\n"
              << "Options :\n"
              << "\t -f, --file\t\tA file which content will be parsed into a uint32_t array in a .cpp file\n"
              << "\t -o, --output\t\t.Cpp output file name\n"
              << "\t -d, --device\t\tOPTIONAL - device ip version in format <major>_<minor>_<revision>\n"
              << "\t -a, --array\t\tName of an uin32_t type array containing parsed input file" << std::endl;
}
std::string parseToCharArray(std::unique_ptr<uint8_t[]> &binary, size_t size, std::string &builtinName, std::string &deviceIp, bool isSpirV) {
    std::ostringstream out;

    bool deviceIpPresent = !deviceIp.empty();
    out << "#include <cstddef>\n";
    out << "#include <cstdint>\n\n";
    out << "size_t " << builtinName;
    if (deviceIpPresent) {
        out << "BinarySize_" << deviceIp;
    } else {
        out << "BinarySize";
    }
    out << " = " << size << ";\n";
    out << "uint32_t " << builtinName;
    if (deviceIpPresent) {
        out << "Binary_" << deviceIp;
    } else {
        out << "Binary";
    }
    out << "[" << (size + 3) / 4 << "] = {"
        << std::endl
        << "    ";
    uint32_t *binaryUint = reinterpret_cast<uint32_t *>(binary.get());
    for (size_t i = 0; i < (size + 3) / 4; i++) {
        if (i != 0) {
            out << ", ";
            if (i % 8 == 0) {
                out << std::endl
                    << "    ";
            }
        }
        if (i < size / 4) {
            out << "0x" << std::hex << std::setw(8) << std::setfill('0') << binaryUint[i];
        } else {
            uint32_t lastBytes = size & 0x3;
            uint32_t lastUint = 0;
            uint8_t *pLastUint = (uint8_t *)&lastUint;
            for (uint32_t j = 0; j < lastBytes; j++) {
                pLastUint[sizeof(uint32_t) - 1 - j] = binary.get()[i * 4 + j];
            }
            out << "0x" << std::hex << std::setw(8) << std::setfill('0') << lastUint;
        }
    }
    out << "};" << std::endl;

    out << std::endl
        << "#include \"shared/source/built_ins/registry/built_ins_registry.h\"\n"
        << std::endl;
    out << "namespace NEO {" << std::endl;
    out << "static RegisterEmbeddedResource register" << builtinName;
    isSpirV ? out << "Ir(" : out << "Bin(";
    out << std::endl;
    out << "    \"";
    deviceIp != "" ? out << deviceIp << "_" << builtinName : out << builtinName;
    isSpirV ? out << ".builtin_kernel.spv\"," : out << ".builtin_kernel.bin\",";
    out << std::endl;
    out << "    (const char *)" << builtinName;
    if (deviceIpPresent) {
        out << "Binary_" << deviceIp;
    } else {
        out << "Binary";
    }
    out << "," << std::endl;
    out << "    " << builtinName;
    if (deviceIpPresent) {
        out << "BinarySize_" << deviceIp;
    } else {
        out << "BinarySize";
    }
    out << ");" << std::endl;
    out << "}" << std::endl;

    return out.str();
}

int main(int argc, char *argv[]) {
    if (argc < minArgCount) {
        showUsage(argv[0]);
        return 1;
    }
    std::string fileName;
    std::string cppOutputName;
    std::string arrayName;
    std::string deviceIp = "";
    size_t size = 0;
    std::fstream inputFile;
    bool isSpirV;
    for (int i = 1; i < argc; i++) {
        std::string arg = argv[i];
        if ((arg == "-f") || (arg == "--file")) {
            fileName = argv[++i];
        } else if ((arg == "-o") || (arg == "--output")) {
            cppOutputName = argv[++i];
        } else if ((arg == "-a") || (arg == "--array")) {
            arrayName = argv[++i];
        } else if ((arg == "-d") || (arg == "--device")) {
            deviceIp = argv[++i];
        } else {
            return 1;
        }
    }
    if (fileName.empty() || cppOutputName.empty() || arrayName.empty()) {
        std::cerr << "All three: fileName, cppOutputName and arrayName must be specified!" << std::endl;
        return 1;
    }
    inputFile.open(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);

    if (inputFile.is_open()) {
        size = static_cast<size_t>(inputFile.tellg());
        std::unique_ptr<uint8_t[]> memblock = std::make_unique<uint8_t[]>(size);
        inputFile.clear();
        inputFile.seekg(0, std::ios::beg);
        inputFile.read(reinterpret_cast<char *>(memblock.get()), size);
        inputFile.close();
        isSpirV = fileName.find(".spv") != std::string::npos;
        std::string cpp = parseToCharArray(memblock, size, arrayName, deviceIp, isSpirV);
        std::fstream(cppOutputName.c_str(), std::ios::out | std::ios::binary).write(cpp.c_str(), cpp.size());
    } else {
        std::cerr << "File cannot be opened!" << std::endl;
        return 1;
    }
    return 0;
}