• 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 
GetEnv(const Blob & blob)29 spv_target_env GetEnv(const Blob &blob)
30 {
31     switch (blob[kHeaderIndexVersion])
32     {
33         case kVersion_1_4:
34             return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
35         default:
36             return SPV_ENV_VULKAN_1_1;
37     }
38 }
39 }  // anonymous namespace
40 
Validate(const Blob & blob)41 bool Validate(const Blob &blob)
42 {
43     spvtools::SpirvTools spirvTools(GetEnv(blob));
44 
45     spvtools::ValidatorOptions options;
46     options.SetFriendlyNames(false);
47 
48     spirvTools.SetMessageConsumer(ValidateSpirvMessage);
49     const bool result = spirvTools.Validate(blob.data(), blob.size(), options);
50 
51     if (!result)
52     {
53         std::string readableSpirv;
54         spirvTools.Disassemble(blob, &readableSpirv, 0);
55         WARN() << "Invalid SPIR-V:\n" << readableSpirv;
56     }
57 
58     return result;
59 }
60 
Print(const Blob & blob)61 void Print(const Blob &blob)
62 {
63     spvtools::SpirvTools spirvTools(GetEnv(blob));
64     std::string readableSpirv;
65     spirvTools.Disassemble(blob, &readableSpirv, 0);
66     INFO() << "Dissembly SPIRV: " << readableSpirv.c_str();
67 }
68 #else   // ANGLE_ENABLE_ASSERTS
69 bool Validate(const Blob &blob)
70 {
71     // Placeholder implementation since this is only used inside an ASSERT().
72     // Return false to indicate an error in case this is ever accidentally used somewhere else.
73     return false;
74 }
75 #endif  // ANGLE_ENABLE_ASSERTS
76 
77 }  // namespace spirv
78 }  // namespace angle
79