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
23 namespace spvtools {
24
25 struct Linter::Impl {
Implspvtools::Linter::Impl26 explicit Impl(spv_target_env env) : target_env(env) {
27 message_consumer = [](spv_message_level_t /*level*/, const char* /*source*/,
28 const spv_position_t& /*position*/,
29 const char* /*message*/) {};
30 }
31
32 spv_target_env target_env; // Target environment.
33 MessageConsumer message_consumer; // Message consumer.
34 };
35
Linter(spv_target_env env)36 Linter::Linter(spv_target_env env) : impl_(new Impl(env)) {}
37
~Linter()38 Linter::~Linter() {}
39
SetMessageConsumer(MessageConsumer consumer)40 void Linter::SetMessageConsumer(MessageConsumer consumer) {
41 impl_->message_consumer = std::move(consumer);
42 }
43
Consumer() const44 const MessageConsumer& Linter::Consumer() const {
45 return impl_->message_consumer;
46 }
47
Run(const uint32_t * binary,size_t binary_size)48 bool Linter::Run(const uint32_t* binary, size_t binary_size) {
49 std::unique_ptr<opt::IRContext> context =
50 BuildModule(SPV_ENV_VULKAN_1_2, Consumer(), binary, binary_size);
51 if (context == nullptr) return false;
52
53 bool result = true;
54 result &= lint::lints::CheckDivergentDerivatives(context.get());
55
56 return result;
57 }
58
59 } // namespace spvtools
60