1 // Copyright (c) 2016 Google Inc.
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 "source/opt/pass_manager.h"
16
17 #include <iostream>
18 #include <string>
19 #include <vector>
20
21 #include "source/opt/ir_context.h"
22 #include "source/util/timer.h"
23 #include "spirv-tools/libspirv.hpp"
24
25 namespace spvtools {
26
27 namespace opt {
28
Run(IRContext * context)29 Pass::Status PassManager::Run(IRContext* context) {
30 auto status = Pass::Status::SuccessWithoutChange;
31
32 // If print_all_stream_ is not null, prints the disassembly of the module
33 // to that stream, with the given preamble and optionally the pass name.
34 auto print_disassembly = [&context, this](const char* preamble, Pass* pass) {
35 if (print_all_stream_) {
36 std::vector<uint32_t> binary;
37 context->module()->ToBinary(&binary, false);
38 SpirvTools t(target_env_);
39 t.SetMessageConsumer(consumer());
40 std::string disassembly;
41 std::string pass_name = (pass ? pass->name() : "");
42 if (!t.Disassemble(binary, &disassembly)) {
43 std::string msg = "Disassembly failed before pass ";
44 msg += pass_name + "\n";
45 spv_position_t null_pos{0, 0, 0};
46 consumer()(SPV_MSG_WARNING, "", null_pos, msg.c_str());
47 return;
48 }
49 *print_all_stream_ << preamble << pass_name << "\n"
50 << disassembly << std::endl;
51 }
52 };
53
54 SPIRV_TIMER_DESCRIPTION(time_report_stream_, /* measure_mem_usage = */ true);
55 for (auto& pass : passes_) {
56 print_disassembly("; IR before pass ", pass.get());
57 SPIRV_TIMER_SCOPED(time_report_stream_, (pass ? pass->name() : ""), true);
58 const auto one_status = pass->Run(context);
59 if (one_status == Pass::Status::Failure) return one_status;
60 if (one_status == Pass::Status::SuccessWithChange) status = one_status;
61
62 if (validate_after_all_) {
63 spvtools::SpirvTools tools(target_env_);
64 tools.SetMessageConsumer(consumer());
65 std::vector<uint32_t> binary;
66 context->module()->ToBinary(&binary, true);
67 if (!tools.Validate(binary.data(), binary.size(), val_options_)) {
68 std::string msg = "Validation failed after pass ";
69 msg += pass->name();
70 spv_position_t null_pos{0, 0, 0};
71 consumer()(SPV_MSG_INTERNAL_ERROR, "", null_pos, msg.c_str());
72 return Pass::Status::Failure;
73 }
74 }
75
76 // Reset the pass to free any memory used by the pass.
77 pass.reset(nullptr);
78 }
79 print_disassembly("; IR after last pass", nullptr);
80
81 // Set the Id bound in the header in case a pass forgot to do so.
82 //
83 // TODO(dnovillo): This should be unnecessary and automatically maintained by
84 // the IRContext.
85 if (status == Pass::Status::SuccessWithChange) {
86 context->module()->SetIdBound(context->module()->ComputeIdBound());
87 }
88 passes_.clear();
89 return status;
90 }
91
92 } // namespace opt
93 } // namespace spvtools
94