blob: a51fa327c88ed9050616c942975078b3b9ab8e6a [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2020 The Dawn & Tint Authors
Ryan Harrisondbc13af2022-02-21 15:19:07 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
Ryan Harrisondbc13af2022-02-21 15:19:07 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
Ryan Harrisondbc13af2022-02-21 15:19:07 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12//
13// 3. Neither the name of the copyright holder nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ryan Harrisondbc13af2022-02-21 15:19:07 +000027
dan sinclair61190452023-07-26 20:47:54 +000028#include "src/tint/lang/wgsl/reader/reader.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000029
Ben Claytoncc636962024-01-30 16:55:59 +000030#include <limits>
Ryan Harrisondbc13af2022-02-21 15:19:07 +000031#include <utility>
32
Ben Claytondfc815c2023-09-25 15:38:43 +000033#include "src/tint/lang/wgsl/reader/lower/lower.h"
dan sinclair61190452023-07-26 20:47:54 +000034#include "src/tint/lang/wgsl/reader/parser/parser.h"
Ben Claytondfc815c2023-09-25 15:38:43 +000035#include "src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.h"
Ben Clayton915ceca2023-07-29 13:12:58 +000036#include "src/tint/lang/wgsl/resolver/resolve.h"
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037
dan sinclair61190452023-07-26 20:47:54 +000038namespace tint::wgsl::reader {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039
James Price78da6642023-11-06 18:42:21 +000040Program Parse(const Source::File* file, const Options& options) {
Ben Claytoncc636962024-01-30 16:55:59 +000041 if (TINT_UNLIKELY(file->content.data.size() >
42 static_cast<size_t>(std::numeric_limits<uint32_t>::max()))) {
43 ProgramBuilder b;
Ben Clayton415bd732024-05-02 14:36:02 +000044 b.Diagnostics().AddError(tint::Source{}) << "WGSL source must be 0xffffffff bytes or fewer";
Ben Claytoncc636962024-01-30 16:55:59 +000045 return Program(std::move(b));
46 }
dan sinclair61190452023-07-26 20:47:54 +000047 Parser parser(file);
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 parser.Parse();
James Price51f19e32024-06-12 14:38:34 +000049 return resolver::Resolve(parser.builder(), options.allowed_features, options.mode);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050}
51
James Price78da6642023-11-06 18:42:21 +000052Result<core::ir::Module> WgslToIR(const Source::File* file, const Options& options) {
53 Program program = Parse(file, options);
Ben Claytondfc815c2023-09-25 15:38:43 +000054 auto module = ProgramToIR(program);
Ben Clayton89274f72024-01-03 10:53:42 +000055 if (module != Success) {
Ben Claytondfc815c2023-09-25 15:38:43 +000056 return module.Failure();
57 }
58 // WGSL-dialect -> core-dialect
Ben Clayton89274f72024-01-03 10:53:42 +000059 if (auto res = Lower(module.Get()); res != Success) {
Ben Claytondfc815c2023-09-25 15:38:43 +000060 return res.Failure();
61 }
62 return module;
63}
64
dan sinclair24b81092023-11-21 16:02:47 +000065tint::Result<core::ir::Module> ProgramToLoweredIR(const Program& program) {
66 auto ir = tint::wgsl::reader::ProgramToIR(program);
Ben Clayton89274f72024-01-03 10:53:42 +000067 if (ir != Success) {
dan sinclair24b81092023-11-21 16:02:47 +000068 return ir.Failure();
69 }
70
71 // Lower from WGSL-dialect to core-dialect
72 auto res = tint::wgsl::reader::Lower(ir.Get());
Ben Clayton89274f72024-01-03 10:53:42 +000073 if (res != Success) {
dan sinclair24b81092023-11-21 16:02:47 +000074 return res.Failure();
75 }
76
77 return ir;
78}
79
Ryan Harrisonc7d42052024-05-22 04:38:49 +000080bool IsUnsupportedByIR(const ast::Enable* enable) {
81 for (auto ext : enable->extensions) {
82 switch (ext->name) {
83 case tint::wgsl::Extension::kChromiumExperimentalFramebufferFetch:
84 case tint::wgsl::Extension::kChromiumExperimentalPixelLocal:
85 case tint::wgsl::Extension::kChromiumExperimentalPushConstant:
Ryan Harrisonc7d42052024-05-22 04:38:49 +000086 case tint::wgsl::Extension::kChromiumInternalRelaxedUniformLayout:
87 return true;
88 default:
89 break;
90 }
91 }
92 return false;
93}
94
dan sinclair61190452023-07-26 20:47:54 +000095} // namespace tint::wgsl::reader