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
|
/*
* Copyright (C) 2017-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/device_binary_format/patchtokens_decoder.h"
#include "shared/source/execution_environment/execution_environment.h"
#include "shared/test/unit_test/device_binary_format/patchtokens_tests.h"
#include "opencl/source/program/create.inl"
#include "opencl/source/program/program.h"
#include "gtest/gtest.h"
using namespace NEO;
extern GFXCORE_FAMILY renderCoreFamily;
template <typename ContainerT, typename TokenT>
inline void PushBackToken(ContainerT &container, const TokenT &token) {
container.insert(container.end(), reinterpret_cast<const typename ContainerT::value_type *>(&token),
reinterpret_cast<const typename ContainerT::value_type *>(&token) + sizeof(token));
}
struct MockProgramRecordUnhandledTokens : public Program {
bool allowUnhandledTokens;
mutable int lastUnhandledTokenFound;
MockProgramRecordUnhandledTokens(ExecutionEnvironment &executionEnvironment) : Program(executionEnvironment) {}
MockProgramRecordUnhandledTokens(ExecutionEnvironment &executionEnvironment, Context *context, bool isBuiltinKernel) : Program(executionEnvironment, context, isBuiltinKernel) {}
bool isSafeToSkipUnhandledToken(unsigned int token) const override {
lastUnhandledTokenFound = static_cast<int>(token);
return allowUnhandledTokens;
}
bool getDefaultIsSafeToSkipUnhandledToken() const {
return Program::isSafeToSkipUnhandledToken(iOpenCL::NUM_PATCH_TOKENS);
}
};
inline cl_int GetDecodeErrorCode(const std::vector<char> &binary, bool allowUnhandledTokens,
int defaultUnhandledTokenId, int &foundUnhandledTokenId) {
NEO::ExecutionEnvironment executionEnvironment;
using PT = MockProgramRecordUnhandledTokens;
std::unique_ptr<PT> prog;
cl_int errorCode = CL_INVALID_BINARY;
prog.reset(NEO::Program::createFromGenBinary<PT>(executionEnvironment,
nullptr,
binary.data(),
binary.size(),
false, &errorCode));
prog->allowUnhandledTokens = allowUnhandledTokens;
prog->lastUnhandledTokenFound = defaultUnhandledTokenId;
auto ret = prog->processGenBinary();
foundUnhandledTokenId = prog->lastUnhandledTokenFound;
return ret;
};
inline std::vector<char> CreateBinary(bool addUnhandledProgramScopePatchToken, bool addUnhandledKernelScopePatchToken,
int32_t unhandledTokenId = static_cast<int32_t>(iOpenCL::NUM_PATCH_TOKENS)) {
std::vector<char> ret;
if (addUnhandledProgramScopePatchToken && addUnhandledKernelScopePatchToken) {
return {};
}
if (addUnhandledProgramScopePatchToken) {
PatchTokensTestData::ValidProgramWithConstantSurface programWithUnhandledToken;
iOpenCL::SPatchItemHeader &unhandledToken = *programWithUnhandledToken.constSurfMutable;
unhandledToken.Size += programWithUnhandledToken.constSurfMutable->InlineDataSize;
unhandledToken.Token = static_cast<uint32_t>(unhandledTokenId);
ret.assign(reinterpret_cast<char *>(programWithUnhandledToken.storage.data()),
reinterpret_cast<char *>(programWithUnhandledToken.storage.data() + programWithUnhandledToken.storage.size()));
} else if (addUnhandledKernelScopePatchToken) {
PatchTokensTestData::ValidProgramWithKernelAndArg programWithKernelWithUnhandledToken;
iOpenCL::SPatchItemHeader &unhandledToken = *programWithKernelWithUnhandledToken.arg0InfoMutable;
unhandledToken.Token = static_cast<uint32_t>(unhandledTokenId);
programWithKernelWithUnhandledToken.recalcTokPtr();
ret.assign(reinterpret_cast<char *>(programWithKernelWithUnhandledToken.storage.data()),
reinterpret_cast<char *>(programWithKernelWithUnhandledToken.storage.data() + programWithKernelWithUnhandledToken.storage.size()));
} else {
PatchTokensTestData::ValidProgramWithKernel regularProgramTokens;
ret.assign(reinterpret_cast<char *>(regularProgramTokens.storage.data()), reinterpret_cast<char *>(regularProgramTokens.storage.data() + regularProgramTokens.storage.size()));
}
return ret;
}
constexpr int32_t unhandledTokenId = iOpenCL::NUM_PATCH_TOKENS;
TEST(EvaluateUnhandledToken, GivenDefaultWhenSkippingUnhandledTokenThenUltAreNotAffected) {
ExecutionEnvironment executionEnvironment;
MockProgramRecordUnhandledTokens program(executionEnvironment);
EXPECT_TRUE(program.getDefaultIsSafeToSkipUnhandledToken());
}
TEST(EvaluateUnhandledToken, GivenAllTokensAreSupportedWhenDecodingProgramBinaryThenDecodingSucceeds) {
int lastUnhandledTokenFound = -1;
auto retVal = GetDecodeErrorCode(CreateBinary(false, false), false, -7, lastUnhandledTokenFound);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(-7, lastUnhandledTokenFound);
}
TEST(EvaluateUnhandledToken, GivenUnhandledTokenIsFoundAndIsSafeToSkipWhenDecodingProgramBinaryThenDecodingSucceeds) {
int lastUnhandledTokenFound = -1;
auto retVal = GetDecodeErrorCode(CreateBinary(true, false, unhandledTokenId), true, -7, lastUnhandledTokenFound);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(unhandledTokenId, lastUnhandledTokenFound);
}
TEST(EvaluateUnhandledToken, GivenUnhandledTokenIsFoundAndIsUnsafeToSkipWhenDecodingProgramBinaryThenDecodingFails) {
int lastUnhandledTokenFound = -1;
auto retVal = GetDecodeErrorCode(CreateBinary(true, false, unhandledTokenId), false, -7, lastUnhandledTokenFound);
EXPECT_EQ(CL_INVALID_BINARY, retVal);
EXPECT_EQ(unhandledTokenId, lastUnhandledTokenFound);
}
TEST(EvaluateUnhandledToken, GivenUnhandledTokenIsFoundAndIsSafeToSkipWhenDecodingKernelBinaryThenDecodingSucceeds) {
int lastUnhandledTokenFound = -1;
auto retVal = GetDecodeErrorCode(CreateBinary(false, true, unhandledTokenId), true, -7, lastUnhandledTokenFound);
EXPECT_EQ(CL_SUCCESS, retVal);
EXPECT_EQ(unhandledTokenId, lastUnhandledTokenFound);
}
TEST(EvaluateUnhandledToken, GivenUnhandledTokenIsFoundAndIsUnsafeToSkipWhenDecodingKernelBinaryThenDecodingFails) {
int lastUnhandledTokenFound = -1;
auto retVal = GetDecodeErrorCode(CreateBinary(false, true, unhandledTokenId), false, -7, lastUnhandledTokenFound);
EXPECT_EQ(CL_INVALID_BINARY, retVal);
EXPECT_EQ(unhandledTokenId, lastUnhandledTokenFound);
}
|