• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Tint Authors.
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 "src/reader/spirv/parser.h"
16 
17 #include <utility>
18 
19 #include "src/reader/spirv/parser_impl.h"
20 #include "src/transform/decompose_strided_matrix.h"
21 #include "src/transform/manager.h"
22 #include "src/transform/remove_unreachable_statements.h"
23 #include "src/transform/simplify_pointers.h"
24 #include "src/transform/unshadow.h"
25 
26 namespace tint {
27 namespace reader {
28 namespace spirv {
29 
Parse(const std::vector<uint32_t> & input)30 Program Parse(const std::vector<uint32_t>& input) {
31   ParserImpl parser(input);
32   bool parsed = parser.Parse();
33 
34   ProgramBuilder& builder = parser.builder();
35   if (!parsed) {
36     // TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics.
37     builder.Diagnostics().add_error(diag::System::Reader, parser.error());
38     return Program(std::move(builder));
39   }
40 
41   // The SPIR-V parser can construct disjoint AST nodes, which is invalid for
42   // the Resolver. Clone the Program to clean these up.
43   builder.SetResolveOnBuild(false);
44   Program program_with_disjoint_ast(std::move(builder));
45 
46   ProgramBuilder output;
47   CloneContext(&output, &program_with_disjoint_ast, false).Clone();
48   auto program = Program(std::move(output));
49   if (!program.IsValid()) {
50     return program;
51   }
52 
53   // If the generated program contains matrices with a custom MatrixStride
54   // attribute then we need to decompose these into an array of vectors
55   if (transform::DecomposeStridedMatrix::ShouldRun(&program)) {
56     transform::Manager manager;
57     manager.Add<transform::Unshadow>();
58     manager.Add<transform::SimplifyPointers>();
59     manager.Add<transform::DecomposeStridedMatrix>();
60     manager.Add<transform::RemoveUnreachableStatements>();
61     return manager.Run(&program).program;
62   }
63 
64   return program;
65 }
66 
67 }  // namespace spirv
68 }  // namespace reader
69 }  // namespace tint
70