• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "spirv-tools/linter.hpp"
16 
17 #include "source/lint/lints.h"
18 #include "source/opt/build_module.h"
19 #include "source/opt/ir_context.h"
20 #include "spirv-tools/libspirv.h"
21 #include "spirv-tools/libspirv.hpp"
22 #include "spirv/unified1/spirv.h"
23 
24 namespace spvtools {
25 
26 struct Linter::Impl {
Implspvtools::Linter::Impl27   explicit Impl(spv_target_env env) : target_env(env) {
28     message_consumer = [](spv_message_level_t /*level*/, const char* /*source*/,
29                           const spv_position_t& /*position*/,
30                           const char* /*message*/) {};
31   }
32 
33   spv_target_env target_env;         // Target environment.
34   MessageConsumer message_consumer;  // Message consumer.
35 };
36 
Linter(spv_target_env env)37 Linter::Linter(spv_target_env env) : impl_(new Impl(env)) {}
38 
~Linter()39 Linter::~Linter() {}
40 
SetMessageConsumer(MessageConsumer consumer)41 void Linter::SetMessageConsumer(MessageConsumer consumer) {
42   impl_->message_consumer = std::move(consumer);
43 }
44 
Consumer() const45 const MessageConsumer& Linter::Consumer() const {
46   return impl_->message_consumer;
47 }
48 
Run(const uint32_t * binary,size_t binary_size)49 bool Linter::Run(const uint32_t* binary, size_t binary_size) {
50   std::unique_ptr<opt::IRContext> context =
51       BuildModule(SPV_ENV_VULKAN_1_2, Consumer(), binary, binary_size);
52   if (context == nullptr) return false;
53 
54   bool result = true;
55   result &= lint::lints::CheckDivergentDerivatives(context.get());
56 
57   return result;
58 }
59 
60 }  // namespace spvtools
61