Hash :
55980dbd
Author :
Date :
2024-06-07T13:45:23
common: Improve/fix spir-v utils This change improves the handling of macroed-sections of the code. Also, fixed some typos. Bug: angleproject:370557215 Change-Id: I437b8c4d835dada4554569c72ef7100568c6be48 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5898609 Commit-Queue: Austin Annestrand <a.annestrand@samsung.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
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
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// spirv_types.cpp:
// Helper SPIR-V functions.
#include "spirv_types.h"
// SPIR-V tools include for AST validation.
#include <spirv-tools/libspirv.hpp>
#if defined(ANGLE_ENABLE_ASSERTS)
constexpr bool kAngleAssertEnabled = true;
#else
constexpr bool kAngleAssertEnabled = false;
#endif
namespace angle
{
namespace spirv
{
namespace
{
void ValidateSpirvMessage(spv_message_level_t level,
const char *source,
const spv_position_t &position,
const char *message)
{
WARN() << "Level" << level << ": " << message;
}
spv_target_env GetEnv(const Blob &blob)
{
switch (blob[kHeaderIndexVersion])
{
case kVersion_1_4:
return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
default:
return SPV_ENV_VULKAN_1_1;
}
}
} // anonymous namespace
bool Validate(const Blob &blob)
{
if (kAngleAssertEnabled)
{
spvtools::SpirvTools spirvTools(GetEnv(blob));
spvtools::ValidatorOptions options;
options.SetFriendlyNames(false);
spirvTools.SetMessageConsumer(ValidateSpirvMessage);
const bool result = spirvTools.Validate(blob.data(), blob.size(), options);
if (!result)
{
std::string readableSpirv;
spirvTools.Disassemble(blob, &readableSpirv, 0);
WARN() << "Invalid SPIR-V:\n" << readableSpirv;
}
return result;
}
// "Validate()" is only used inside an ASSERT().
// Return false to indicate an error in case this is ever accidentally used somewhere else.
return false;
}
void Print(const Blob &blob)
{
spvtools::SpirvTools spirvTools(GetEnv(blob));
std::string readableSpirv;
spirvTools.Disassemble(blob, &readableSpirv,
SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT |
SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT);
INFO() << "Disassembled SPIR-V:\n" << readableSpirv.c_str();
}
} // namespace spirv
} // namespace angle