File: ocloc_api.cpp

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (152 lines) | stat: -rw-r--r-- 6,199 bytes parent folder | download
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
148
149
150
151
152
/*
 * Copyright (C) 2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "ocloc_api.h"

#include "shared/offline_compiler/source/decoder/binary_decoder.h"
#include "shared/offline_compiler/source/decoder/binary_encoder.h"
#include "shared/offline_compiler/source/multi_command.h"
#include "shared/offline_compiler/source/ocloc_fatbinary.h"
#include "shared/offline_compiler/source/ocloc_validator.h"
#include "shared/offline_compiler/source/offline_compiler.h"

#include <iostream>

using namespace NEO;

const char *help = R"===(ocloc is a tool for managing Intel Compute GPU device binary format.
It can be used for generation (as part of 'compile' command) as well as
manipulation (decoding/modifying - as part of 'disasm'/'asm' commands) of such
binary files.
Intel Compute GPU device binary is a format used by Intel Compute GPU runtime
(aka NEO). Intel Compute GPU runtime will return this binary format when queried
using clGetProgramInfo(..., CL_PROGRAM_BINARIES, ...). It will also honor
this format as input to clCreateProgramWithBinary function call.
ocloc does not require Intel GPU device to be present in the system nor does it
depend on Intel Compute GPU runtime driver to be installed. It does however rely
on the same set of compilers (IGC, common_clang) as the runtime driver.

Usage: ocloc [--help] <command> [<command_args>]
Available commands are listed below.
Use 'ocloc <command> --help' to get help about specific command.

Commands:
  compile               Compiles input to Intel Compute GPU device binary.
  disasm                Disassembles Intel Compute GPU device binary.
  asm                   Assembles Intel Compute GPU device binary.
  multi                 Compiles multiple files using a config file.
  validate              Validates Intel Compute GPU device binary

Default command (when none provided) is 'compile'.

Examples:
  Compile file to Intel Compute GPU device binary (out = source_file_Gen9core.bin)
    ocloc -file source_file.cl -device skl

  Disassemble Intel Compute GPU device binary
    ocloc disasm -file source_file_Gen9core.bin

  Assemble to Intel Compute GPU device binary (after above disasm)
    ocloc asm -out reassembled.bin

  Validate Intel Compute GPU device binary
    ocloc validate -file source_file_Gen9core.bin
)===";

extern "C" {
void printOclocCmdLine(unsigned int numArgs, const char *argv[], std::unique_ptr<OclocArgHelper> &helper) {
    helper->printf("Command was:");
    for (auto i = 0u; i < numArgs; ++i)
        helper->printf(" %s", argv[i]);
    helper->printf("\n");
}

int oclocInvoke(unsigned int numArgs, const char *argv[],
                const uint32_t numSources, const uint8_t **dataSources, const uint64_t *lenSources, const char **nameSources,
                const uint32_t numInputHeaders, const uint8_t **dataInputHeaders, const uint64_t *lenInputHeaders, const char **nameInputHeaders,
                uint32_t *numOutputs, uint8_t ***dataOutputs, uint64_t **lenOutputs, char ***nameOutputs) {
    auto helper = std::make_unique<OclocArgHelper>(
        numSources, dataSources, lenSources, nameSources,
        numInputHeaders, dataInputHeaders, lenInputHeaders, nameInputHeaders,
        numOutputs, dataOutputs, lenOutputs, nameOutputs);
    std::vector<std::string> allArgs;
    if (numArgs > 1) {
        allArgs.assign(argv, argv + numArgs);
    }

    try {
        if (numArgs == 1 || (numArgs > 1 && (ConstStringRef("-h") == allArgs[1] || ConstStringRef("--help") == allArgs[1]))) {
            helper->printf("%s", help);
            return ErrorCode::SUCCESS;
        } else if (numArgs > 1 && ConstStringRef("disasm") == allArgs[1]) {
            BinaryDecoder disasm(helper.get());
            int retVal = disasm.validateInput(allArgs);
            if (retVal == 0) {
                return disasm.decode();
            } else {
                return retVal;
            }
        } else if (numArgs > 1 && ConstStringRef("asm") == allArgs[1]) {
            BinaryEncoder assembler(helper.get());
            int retVal = assembler.validateInput(allArgs);
            if (retVal == 0) {
                return assembler.encode();
            } else {
                return retVal;
            }
        } else if (numArgs > 1 && ConstStringRef("multi") == allArgs[1]) {
            int retValue = ErrorCode::SUCCESS;
            std::unique_ptr<MultiCommand> pMulti{(MultiCommand::create(allArgs, retValue, helper.get()))};
            return retValue;
        } else if (requestedFatBinary(allArgs)) {
            return buildFatBinary(allArgs, helper.get());
        } else if (numArgs > 1 && ConstStringRef("validate") == allArgs[1]) {
            return NEO::Ocloc::validate(allArgs, helper.get());
        } else {
            int retVal = ErrorCode::SUCCESS;

            std::unique_ptr<OfflineCompiler> pCompiler{OfflineCompiler::create(numArgs, allArgs, true, retVal, helper.get())};
            if (retVal == ErrorCode::SUCCESS) {
                retVal = buildWithSafetyGuard(pCompiler.get());

                std::string buildLog = pCompiler->getBuildLog();
                if (buildLog.empty() == false) {
                    helper->printf("%s\n", buildLog.c_str());
                }

                if (retVal == ErrorCode::SUCCESS) {
                    if (!pCompiler->isQuiet())
                        helper->printf("Build succeeded.\n");
                } else {
                    helper->printf("Build failed with error code: %d\n", retVal);
                }
            }

            if (retVal != ErrorCode::SUCCESS)
                printOclocCmdLine(numArgs, argv, helper);

            return retVal;
        }
    } catch (const std::exception &e) {
        helper->printf("%s\n", e.what());
        printOclocCmdLine(numArgs, argv, helper);
        return -1;
    }
    return -1;
}

int oclocFreeOutput(uint32_t *numOutputs, uint8_t ***dataOutputs, uint64_t **lenOutputs, char ***nameOutputs) {
    for (uint32_t i = 0; i < *numOutputs; i++) {
        delete[](*dataOutputs)[i];
        delete[](*nameOutputs)[i];
    }
    delete[](*dataOutputs);
    delete[](*lenOutputs);
    delete[](*nameOutputs);
    return 0;
}
}