Skip to content

Commit

Permalink
common: Improve/fix spir-v utils
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Shahbaz Youssefi <[email protected]>
Reviewed-by: Geoff Lang <[email protected]>
  • Loading branch information
annestrand authored and Angle LUCI CQ committed Oct 2, 2024
1 parent be8cc06 commit 55980db
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/common/spirv/angle_spirv_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
// 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
{

#if defined(ANGLE_ENABLE_ASSERTS)
namespace
{
void ValidateSpirvMessage(spv_message_level_t level,
Expand All @@ -40,22 +45,29 @@ spv_target_env GetEnv(const Blob &blob)

bool Validate(const Blob &blob)
{
spvtools::SpirvTools spirvTools(GetEnv(blob));
if (kAngleAssertEnabled)
{
spvtools::SpirvTools spirvTools(GetEnv(blob));

spvtools::ValidatorOptions options;
options.SetFriendlyNames(false);
spvtools::ValidatorOptions options;
options.SetFriendlyNames(false);

spirvTools.SetMessageConsumer(ValidateSpirvMessage);
const bool result = spirvTools.Validate(blob.data(), blob.size(), options);
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;
if (!result)
{
std::string readableSpirv;
spirvTools.Disassemble(blob, &readableSpirv, 0);
WARN() << "Invalid SPIR-V:\n" << readableSpirv;
}

return result;
}

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)
Expand All @@ -65,16 +77,8 @@ void Print(const Blob &blob)
spirvTools.Disassemble(blob, &readableSpirv,
SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT |
SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT);
INFO() << "Dissembly SPIRV: " << readableSpirv.c_str();
}
#else // ANGLE_ENABLE_ASSERTS
bool Validate(const Blob &blob)
{
// Placeholder implementation since this is only used inside an ASSERT().
// Return false to indicate an error in case this is ever accidentally used somewhere else.
return false;
INFO() << "Disassembled SPIR-V:\n" << readableSpirv.c_str();
}
#endif // ANGLE_ENABLE_ASSERTS

} // namespace spirv
} // namespace angle

0 comments on commit 55980db

Please sign in to comment.