• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // spirv_types.cpp:
6 //   Helper SPIR-V functions.
7 
8 #include "spirv_types.h"
9 
10 // SPIR-V tools include for AST validation.
11 #include <spirv-tools/libspirv.hpp>
12 
13 namespace angle
14 {
15 namespace spirv
16 {
17 
18 #if defined(ANGLE_ENABLE_ASSERTS)
19 namespace
20 {
ValidateSpirvMessage(spv_message_level_t level,const char * source,const spv_position_t & position,const char * message)21 void ValidateSpirvMessage(spv_message_level_t level,
22                           const char *source,
23                           const spv_position_t &position,
24                           const char *message)
25 {
26     WARN() << "Level" << level << ": " << message;
27 }
28 }  // anonymous namespace
29 
Validate(const Blob & blob)30 bool Validate(const Blob &blob)
31 {
32     spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1);
33 
34     spirvTools.SetMessageConsumer(ValidateSpirvMessage);
35     bool result = spirvTools.Validate(blob);
36 
37     if (!result)
38     {
39         std::string readableSpirv;
40         spirvTools.Disassemble(blob, &readableSpirv, 0);
41         WARN() << "Invalid SPIR-V:\n" << readableSpirv;
42     }
43 
44     return result;
45 }
46 #else   // ANGLE_ENABLE_ASSERTS
47 bool Validate(const Blob &blob)
48 {
49     // Placeholder implementation since this is only used inside an ASSERT().
50     // Return false to indicate an error in case this is ever accidentally used somewhere else.
51     return false;
52 }
53 #endif  // ANGLE_ENABLE_ASSERTS
54 
55 }  // namespace spirv
56 }  // namespace angle
57