Initial commit

fixes
Dan Sinclair 4 years ago
commit 6e581895a5
  1. 2
      .clang-format
  2. 8
      .gitignore
  3. 140
      CMakeLists.txt
  4. 93
      CODE_OF_CONDUCT.md
  5. 28
      CONTRIBUTING.md
  6. 2
      CPPLINT.cfg
  7. 25
      DEPS
  8. 2485
      Doxyfile
  9. 202
      LICENSE
  10. 28
      README.md
  11. 29
      fuzz/CMakeLists.txt
  12. 27
      fuzz/tint_fuzz.cc
  13. 23
      samples/CMakeLists.txt
  14. 278
      samples/main.cc
  15. 310
      src/CMakeLists.txt
  16. 49
      src/ast/array_accessor_expression.cc
  17. 83
      src/ast/array_accessor_expression.h
  18. 41
      src/ast/as_expression.cc
  19. 80
      src/ast/as_expression.h
  20. 47
      src/ast/assignment_statement.cc
  21. 80
      src/ast/assignment_statement.h
  22. 29
      src/ast/binding_decoration.cc
  23. 50
      src/ast/binding_decoration.h
  24. 38
      src/ast/binding_decoration_test.cc
  25. 29
      src/ast/bool_literal.cc
  26. 51
      src/ast/bool_literal.h
  27. 55
      src/ast/bool_literal_test.cc
  28. 58
      src/ast/break_statement.cc
  29. 84
      src/ast/break_statement.h
  30. 75
      src/ast/builtin.cc
  31. 44
      src/ast/builtin.h
  32. 29
      src/ast/builtin_decoration.cc
  33. 49
      src/ast/builtin_decoration.h
  34. 38
      src/ast/builtin_decoration_test.cc
  35. 47
      src/ast/call_expression.cc
  36. 84
      src/ast/call_expression.h
  37. 56
      src/ast/case_statement.cc
  38. 90
      src/ast/case_statement.h
  39. 42
      src/ast/cast_expression.cc
  40. 80
      src/ast/cast_expression.h
  41. 42
      src/ast/const_initializer_expression.cc
  42. 70
      src/ast/const_initializer_expression.h
  43. 57
      src/ast/continue_statement.cc
  44. 84
      src/ast/continue_statement.h
  45. 51
      src/ast/decorated_variable.cc
  46. 67
      src/ast/decorated_variable.h
  47. 39
      src/ast/derivative_modifier.cc
  48. 31
      src/ast/derivative_modifier.h
  49. 63
      src/ast/else_statement.cc
  50. 96
      src/ast/else_statement.h
  51. 50
      src/ast/entry_point.cc
  52. 89
      src/ast/entry_point.h
  53. 91
      src/ast/entry_point_test.cc
  54. 96
      src/ast/expression.cc
  55. 102
      src/ast/expression.h
  56. 37
      src/ast/fallthrough_statement.cc
  57. 58
      src/ast/fallthrough_statement.h
  58. 29
      src/ast/float_literal.cc
  59. 49
      src/ast/float_literal.h
  60. 45
      src/ast/float_literal_test.cc
  61. 71
      src/ast/function.cc
  62. 109
      src/ast/function.h
  63. 55
      src/ast/identifier_expression.cc
  64. 75
      src/ast/identifier_expression.h
  65. 77
      src/ast/if_statement.cc
  66. 109
      src/ast/if_statement.h
  67. 54
      src/ast/import.cc
  68. 76
      src/ast/import.h
  69. 91
      src/ast/import_test.cc
  70. 43
      src/ast/initializer_expression.cc
  71. 60
      src/ast/initializer_expression.h
  72. 29
      src/ast/int_literal.cc
  73. 49
      src/ast/int_literal.h
  74. 45
      src/ast/int_literal_test.cc
  75. 36
      src/ast/kill_statement.cc
  76. 53
      src/ast/kill_statement.h
  77. 52
      src/ast/literal.cc
  78. 62
      src/ast/literal.h
  79. 29
      src/ast/location_decoration.cc
  80. 50
      src/ast/location_decoration.h
  81. 38
      src/ast/location_decoration_test.cc
  82. 58
      src/ast/loop_statement.cc
  83. 85
      src/ast/loop_statement.h
  84. 49
      src/ast/member_accessor_expression.cc
  85. 85
      src/ast/member_accessor_expression.h
  86. 61
      src/ast/module.cc
  87. 110
      src/ast/module.h
  88. 78
      src/ast/module_test.cc
  89. 40
      src/ast/node.cc
  90. 77
      src/ast/node.h
  91. 36
      src/ast/nop_statement.cc
  92. 53
      src/ast/nop_statement.h
  93. 43
      src/ast/pipeline_stage.cc
  94. 31
      src/ast/pipeline_stage.h
  95. 58
      src/ast/regardless_statement.cc
  96. 84
      src/ast/regardless_statement.h
  97. 55
      src/ast/relational_expression.cc
  98. 220
      src/ast/relational_expression.h
  99. 48
      src/ast/return_statement.cc
  100. 71
      src/ast/return_statement.h
  101. Some files were not shown because too many files have changed in this diff Show More

@ -0,0 +1,2 @@
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: Chromium

8
.gitignore vendored

@ -0,0 +1,8 @@
.vscode
.DS_Store
build
out
third_party/cpplint
third_party/googletest
third_party/spirv-headers
third_party/spirv-tools

@ -0,0 +1,140 @@
# Copyright 2020 The Tint Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.10.2)
project(tint)
enable_testing()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_DEBUG_POSTFIX "")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()
option(TINT_BUILD_DOCS "Build documentation" ON)
option(TINT_BUILD_SPV_PARSER "Build the SPIR-V input parser" OFF)
option(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
message(STATUS "Tint build SPIR-V parser: ${TINT_BUILD_SPV_PARSER}")
message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}")
message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}")
message(STATUS "Tint build with UBSAN: ${TINT_ENABLE_UBSAN}")
message(STATUS "Using python3")
find_package(PythonInterp 3 REQUIRED)
if (${TINT_BUILD_SPV_PARSER})
include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
endif()
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") OR
("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") OR
(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND
(NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")))
set(COMPILER_IS_LIKE_GNU TRUE)
endif()
find_package(Doxygen)
if(DOXYGEN_FOUND)
add_custom_target(tint_docs ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Generating API documentation"
VERBATIM)
else()
message("Doxygen not found. Skipping documentation")
endif()
function(tint_default_compile_options TARGET)
include_directories("${PROJECT_SOURCE_DIR}")
if (${COMPILER_IS_LIKE_GNU})
target_compile_options(${TARGET} PRIVATE
-std=c++14
-fno-exceptions
-fno-rtti
-fvisibility=hidden
-Wall
-Werror
-Wextra
-Wno-documentation-unknown-command
-Wno-padded
-Wno-switch-enum
-Wno-unknown-pragmas
-pedantic-errors
)
if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
target_compile_options(${TARGET} PRIVATE
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-format-pedantic
-Wno-return-std-move-in-c++11
-Wno-unknown-warning-option
-Weverything
)
endif()
if (${TINT_ENABLE_MSAN})
target_compile_options(${TARGET} PRIVATE -fsanitize=memory)
target_link_options(${TARGET} PRIVATE -fsanitize=memory)
elseif (${TINT_ENABLE_ASAN})
target_compile_options(${TARGET} PRIVATE -fsanitize=address)
target_link_options(${TARGET} PRIVATE -fsanitize=address)
elseif (${TINT_ENABLE_UBSAN})
target_compile_options(${TARGET} PRIVATE -fsanitize=undefined)
target_link_options(${TARGET} PRIVATE -fsanitize=undefined)
endif()
endif()
if (MSVC)
# Specify /EHs for exception handling.
target_compile_options(${TARGET} PRIVATE
/bigobj
/EHsc
/W3
/WX
/wd4068
/wd4514
/wd4571
/wd4625
/wd4626
/wd4710
/wd4774
/wd4820
/wd5026
/wd5027
)
endif()
endfunction()
add_subdirectory(third_party)
add_subdirectory(src)
add_subdirectory(samples)
if (${TINT_BUILD_FUZZERS})
add_subdirectory(fuzz)
endif()

@ -0,0 +1,93 @@
# Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of
experience, education, socio-economic status, nationality, personal appearance,
race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, or to ban temporarily or permanently any
contributor for other behaviors that they deem inappropriate, threatening,
offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
This Code of Conduct also applies outside the project spaces when the Project
Steward has a reasonable belief that an individual's behavior may have a
negative impact on the project or its community.
## Conflict Resolution
We do not believe that all conflict is bad; healthy debate and disagreement
often yield positive results. However, it is never okay to be disrespectful or
to engage in behavior that violates the projectโ€™s code of conduct.
If you see someone violating the code of conduct, you are encouraged to address
the behavior directly with those involved. Many issues can be resolved quickly
and easily, and this gives people more control over the outcome of their
dispute. If you are unable to resolve the matter for any reason, or if the
behavior is threatening or harassing, report it. We are dedicated to providing
an environment where participants feel welcome and safe.
Reports should be directed to David Neto <dneto@google.com>, the
Project Steward(s) for Tint. It is the Project Stewardโ€™s duty to
receive and address reported violations of the code of conduct. They will then
work with a committee consisting of representatives from the Open Source
Programs Office and the Google Open Source Strategy team. If for any reason you
are uncomfortable reaching out the Project Steward, please email
opensource@google.com.
We will investigate every complaint, but you may not receive a direct response.
We will use our discretion in determining when and how to follow up on reported
incidents, which may range from not taking action to permanent expulsion from
the project and project-sponsored spaces. We will notify the accused of the
report and provide them an opportunity to discuss it before any action is taken.
The identity of the reporter will be omitted from the details of the report
supplied to the accused. In potentially harmful situations, such as ongoing
harassment or threats to anyone's safety, we may take action without notice.
## Attribution
This Code of Conduct is adapted from the Contributor Covenant, version 1.4,
available at
https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

@ -0,0 +1,28 @@
# How to Contribute
We'd love to accept your patches and contributions to this project. There are
just a few small guidelines you need to follow.
## Contributor License Agreement
Contributions to this project must be accompanied by a Contributor License
Agreement. You (or your employer) retain the copyright to your contribution;
this simply gives us permission to use and redistribute your contributions as
part of the project. Head over to <https://cla.developers.google.com/> to see
your current agreements on file or to sign a new one.
You generally only need to submit a CLA once, so if you've already submitted one
(even if it was for a different project), you probably don't need to do it
again.
## Code reviews
All submissions, including submissions by project members, require review. We
use GitHub pull requests for this purpose. Consult
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
information on using pull requests.
## Community Guidelines
This project follows
[Google's Open Source Community Guidelines](https://opensource.google.com/conduct/).

@ -0,0 +1,2 @@
set noparent
headers=h,hpp

25
DEPS

@ -0,0 +1,25 @@
use_relative_paths = True
vars = {
'google_git': 'https://github.com/google',
'khronos_git': 'https://github.com/KhronosGroup',
'cpplint_revision': '26470f9ccb354ff2f6d098f831271a1833701b28',
'googletest_revision': '41b5f149ab306e96b5b2faf523505d75acffd98a',
'spirv_headers_revision': '5dbc1c32182e17b8ab8e8158a802ecabaf35aad3',
'spirv_tools_revision': 'fe10239f92f4539e9050da375dab095328fec196',
}
deps = {
'third_party/cpplint': Var('google_git') + '/styleguide.git@' +
Var('cpplint_revision'),
'third_party/googletest': Var('google_git') + '/googletest.git@' +
Var('googletest_revision'),
'third_party/spirv-headers': Var('khronos_git') + '/SPIRV-Headers.git@' +
Var('spirv_headers_revision'),
'third_party/spirv-tools': Var('khronos_git') + '/SPIRV-Tools.git@' +
Var('spirv_tools_revision'),
}

2485
Doxyfile

File diff suppressed because it is too large Load Diff

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

@ -0,0 +1,28 @@
# Tint
Tint is a compiler for the WebGPU Shader Language (WGSL).
This is not an officially supported Google product.
## Requirements
* Git
* CMake (3.10.2 or later)
* Ninja (or other build tool)
* Python, for fetching dependencies
## Build options
* `TINT_BUILD_SPV_PARSER` : enable the SPIR-V input parser
## Building
```
./tools/git-sync-deps
mkdir -p out/Debug
cd out/Debug
cmake -GNinja ../..
ninja
```
## Contributing
Please see the CONTRIBUTING and CODE_OF_CONDUCT files on how to contribute to
Tint.

@ -0,0 +1,29 @@
# Copyright 2020 The Tint Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
add_executable(tint_fuzz tint_fuzz.cc)
target_link_libraries(tint_fuzz libtint)
tint_default_compile_options(tint_fuzz)
target_link_options(tint_fuzz PRIVATE
-fno-omit-frame-pointer
-fsanitize=fuzzer,address,undefined
-fsanitize-address-use-after-scope
-O1
-g
)
target_compile_options(tint_fuzz PRIVATE
-fsanitize=fuzzer,address,undefined
-Wno-missing-prototypes
)

@ -0,0 +1,27 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "src/reader/wgsl/parser.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
std::string str(reinterpret_cast<const char*>(data), size);
tint::reader::wgsl::Parser parser(str);
parser.Parse();
return 0;
}

@ -0,0 +1,23 @@
# Copyright 2020 The Tint Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(TINT_SRCS
main.cc
)
## Tint executable
add_executable(tint ${TINT_SRCS})
target_link_libraries(tint libtint SPIRV-Tools)
tint_default_compile_options(tint)

@ -0,0 +1,278 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fstream>
#include <iostream>
#include <memory>
#include <vector>
#include "spirv-tools/libspirv.hpp"
#include "src/reader/reader.h"
#include "src/reader/wgsl/parser.h"
#include "src/type_determiner.h"
#include "src/validator.h"
#include "src/writer/spv/generator.h"
#include "src/writer/wgsl/generator.h"
#include "src/writer/writer.h"
namespace {
enum class Format {
kNone = -1,
kSpirv,
kSpvAsm,
kWgsl,
};
struct Options {
bool show_help = false;
std::string input_filename;
std::string output_name = "";
std::string output_ext = "spv";
bool parse_only = false;
bool dump_ast = false;
Format format = Format::kSpirv;
};
const char kUsage[] = R"(Usage: tint [options] SCRIPT [SCRIPTS...]
options:
--format <spirv|spvasm|wgsl> -- Output format
--output-name <name> -- Name for the output file, without extension
--parse-only -- Stop after parsing the input
--dump-ast -- Dump the generated AST to stdout
-h -- This help text)";
Format parse_format(const std::string& fmt) {
if (fmt == "spirv")
return Format::kSpirv;
if (fmt == "spvasm")
return Format::kSpvAsm;
if (fmt == "wgsl")
return Format::kWgsl;
return Format::kNone;
}
bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
for (size_t i = 1; i < args.size(); ++i) {
const std::string& arg = args[i];
if (arg == "--format") {
++i;
if (i >= args.size()) {
std::cerr << "Missing value for --format argument." << std::endl;
return false;
}
opts->format = parse_format(args[i]);
if (opts->format == Format::kNone) {
std::cerr << "Unknown output format: " << args[i] << std::endl;
return false;
}
if (opts->format == Format::kSpvAsm)
opts->output_ext = "spvasm";
else if (opts->format == Format::kWgsl)
opts->output_ext = "wgsl";
}
if (arg == "--output-name") {
++i;
if (i >= args.size()) {
std::cerr << "Missing value for --output_name argument." << std::endl;
return false;
}
opts->output_name = args[i];
} else if (arg == "-h" || arg == "--help") {
opts->show_help = true;
} else if (arg == "--parse-only") {
opts->parse_only = true;
} else if (arg == "--dump-ast") {
opts->dump_ast = true;
} else if (!arg.empty()) {
opts->input_filename = arg;
}
}
return true;
}
std::vector<uint8_t> ReadFile(const std::string& input_file) {
FILE* file = nullptr;
#if defined(_MSC_VER)
fopen_s(&file, input_file.c_str(), "rb");
#else
file = fopen(input_file.c_str(), "rb");
#endif
if (!file) {
std::cerr << "Failed to open " << input_file << std::endl;
return {};
}
fseek(file, 0, SEEK_END);
uint64_t tell_file_size = static_cast<uint64_t>(ftell(file));
if (tell_file_size <= 0) {
std::cerr << "Input file of incorrect size: " << input_file << std::endl;
fclose(file);
return {};
}
fseek(file, 0, SEEK_SET);
size_t file_size = static_cast<size_t>(tell_file_size);
std::vector<uint8_t> data;
data.resize(file_size);
size_t bytes_read = fread(data.data(), sizeof(uint8_t), file_size, file);
fclose(file);
if (bytes_read != file_size) {
std::cerr << "Failed to read " << input_file << std::endl;
return {};
}
return data;
}
std::string Disassemble(const std::vector<uint32_t>& data) {
std::string spv_errors;
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0;
auto msg_consumer = [&spv_errors](spv_message_level_t level, const char*,
const spv_position_t& position,
const char* message) {
switch (level) {
case SPV_MSG_FATAL:
case SPV_MSG_INTERNAL_ERROR:
case SPV_MSG_ERROR:
spv_errors += "error: line " + std::to_string(position.index) + ": " +
message + "\n";
break;
case SPV_MSG_WARNING:
spv_errors += "warning: line " + std::to_string(position.index) + ": " +
message + "\n";
break;
case SPV_MSG_INFO:
spv_errors += "info: line " + std::to_string(position.index) + ": " +
message + "\n";
break;
case SPV_MSG_DEBUG:
break;
}
};
spvtools::SpirvTools tools(target_env);
tools.SetMessageConsumer(msg_consumer);
std::string result;
tools.Disassemble(data, &result,
SPV_BINARY_TO_TEXT_OPTION_INDENT |
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
return result;
}
} // namespace
int main(int argc, const char** argv) {
std::vector<std::string> args(argv, argv + argc);
Options options;
if (!ParseArgs(args, &options)) {
std::cerr << "Failed to parse arguments." << std::endl;
return 1;
}
if (options.show_help) {
std::cout << kUsage << std::endl;
return 0;
}
if (options.input_filename == "") {
std::cerr << "Input file missing" << std::endl;
std::cout << kUsage << std::endl;
return 1;
}
auto data = ReadFile(options.input_filename);
if (data.size() == 0)
return 1;
std::unique_ptr<tint::reader::Reader> reader;
std::string ext = "wgsl";
if (options.input_filename.size() > 4 &&
options.input_filename.substr(options.input_filename.size() - 4) ==
"wgsl") {
reader = std::make_unique<tint::reader::wgsl::Parser>(
std::string(data.begin(), data.end()));
}
if (!reader) {
std::cerr << "Failed to create reader for input file: "
<< options.input_filename << std::endl;
return 1;
}
if (!reader->Parse()) {
std::cerr << reader->error() << std::endl;
return 1;
}
auto module = reader->module();
if (options.dump_ast) {
std::cout << std::endl << module.to_str() << std::endl;
}
if (options.parse_only) {
return 1;
}
tint::TypeDeterminer td;
if (!td.Determine(&module)) {
std::cerr << td.error() << std::endl;
return 1;
}
tint::Validator v;
if (!v.Validate(module)) {
std::cerr << v.error() << std::endl;
return 1;
}
std::unique_ptr<tint::writer::Writer> writer;
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
writer = std::make_unique<tint::writer::spv::Generator>(std::move(module));
} else if (options.format == Format::kWgsl) {
writer = std::make_unique<tint::writer::wgsl::Generator>(std::move(module));
} else {
std::cerr << "Unknown output format specified" << std::endl;
return 1;
}
if (!writer->Generate()) {
std::cerr << "Failed to generate SPIR-V: " << writer->error() << std::endl;
return 1;
}
if (options.format == Format::kSpvAsm) {
auto w = static_cast<tint::writer::spv::Generator*>(writer.get());
auto str = Disassemble(w->result());
// TODO(dsinclair): Write to file if output_file given
std::cout << str << std::endl;
} else if (options.format == Format::kSpirv) {
// auto w = static_cast<tint::writer::spv::Generator*>(writer.get());
// TODO(dsincliair): Write to to file
} else if (options.format == Format::kWgsl) {
auto w = static_cast<tint::writer::wgsl::Generator*>(writer.get());
std::cout << w->result() << std::endl;
}
return 0;
}

@ -0,0 +1,310 @@
# Copyright 2020 The Tint Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set(TINT_LIB_SRCS
ast/array_accessor_expression.cc
ast/array_accessor_expression.h
ast/as_expression.cc
ast/as_expression.h
ast/assignment_statement.cc
ast/assignment_statement.h
ast/binding_decoration.cc
ast/binding_decoration.h
ast/bool_literal.h
ast/bool_literal.cc
ast/break_statement.cc
ast/break_statement.h
ast/builtin.cc
ast/builtin.h
ast/builtin_decoration.cc
ast/builtin_decoration.h
ast/call_expression.cc
ast/call_expression.h
ast/case_statement.cc
ast/case_statement.h
ast/cast_expression.cc
ast/cast_expression.h
ast/const_initializer_expression.cc
ast/const_initializer_expression.h
ast/continue_statement.cc
ast/continue_statement.h
ast/decorated_variable.cc
ast/decorated_variable.h
ast/derivative_modifier.cc
ast/derivative_modifier.h
ast/else_statement.cc
ast/else_statement.h
ast/entry_point.cc
ast/entry_point.h
ast/expression.cc
ast/expression.h
ast/fallthrough_statement.cc
ast/fallthrough_statement.h
ast/float_literal.cc
ast/float_literal.h
ast/function.cc
ast/function.h
ast/identifier_expression.cc
ast/identifier_expression.h
ast/if_statement.cc
ast/if_statement.h
ast/import.cc
ast/import.h
ast/initializer_expression.cc
ast/initializer_expression.h
ast/int_literal.cc
ast/int_literal.h
ast/kill_statement.cc
ast/kill_statement.h
ast/literal.h
ast/literal.cc
ast/location_decoration.cc
ast/location_decoration.h
ast/loop_statement.cc
ast/loop_statement.h
ast/member_accessor_expression.cc
ast/member_accessor_expression.h
ast/module.cc
ast/module.h
ast/node.cc
ast/node.h
ast/nop_statement.cc
ast/nop_statement.h
ast/pipeline_stage.cc
ast/pipeline_stage.h
ast/regardless_statement.cc
ast/regardless_statement.h
ast/relational_expression.cc
ast/relational_expression.h
ast/return_statement.cc
ast/return_statement.h
ast/set_decoration.cc
ast/set_decoration.h
ast/statement.cc
ast/statement.h
ast/statement_condition.cc
ast/statement_condition.h
ast/storage_class.cc
ast/storage_class.h
ast/struct_decoration.cc
ast/struct_decoration.h
ast/struct.cc
ast/struct.h
ast/struct_member.cc
ast/struct_member.h
ast/struct_member_decoration.cc
ast/struct_member_decoration.h
ast/struct_member_offset_decoration.cc
ast/struct_member_offset_decoration.h
ast/switch_statement.cc
ast/switch_statement.h
ast/type_initializer_expression.h
ast/type_initializer_expression.cc
ast/type/alias_type.cc
ast/type/alias_type.h
ast/type/array_type.cc
ast/type/array_type.h
ast/type/bool_type.cc
ast/type/bool_type.h
ast/type/f32_type.cc
ast/type/f32_type.h
ast/type/i32_type.cc
ast/type/i32_type.h
ast/type/matrix_type.cc
ast/type/matrix_type.h
ast/type/pointer_type.cc
ast/type/pointer_type.h
ast/type/struct_type.cc
ast/type/struct_type.h
ast/type/type.cc
ast/type/type.h
ast/type/u32_type.cc
ast/type/u32_type.h
ast/type/vector_type.cc
ast/type/vector_type.h
ast/type/void_type.cc
ast/type/void_type.h
ast/uint_literal.cc
ast/uint_literal.h
ast/unary_derivative.cc
ast/unary_derivative.h
ast/unary_derivative_expression.cc
ast/unary_derivative_expression.h
ast/unary_method.cc
ast/unary_method.h
ast/unary_method_expression.cc
ast/unary_method_expression.h
ast/unary_op.cc
ast/unary_op.h
ast/unary_op_expression.cc
ast/unary_op_expression.h
ast/unless_statement.cc
ast/unless_statement.h
ast/variable.cc
ast/variable.h
ast/variable_decoration.cc
ast/variable_decoration.h
ast/variable_statement.cc
ast/variable_statement.h
reader/reader.cc
reader/reader.h
reader/wgsl/lexer.cc
reader/wgsl/lexer.h
reader/wgsl/parser.cc
reader/wgsl/parser.h
reader/wgsl/parser_impl.cc
reader/wgsl/parser_impl.h
reader/wgsl/token.cc
reader/wgsl/token.h
source.h
type_determiner.cc
type_determiner.h
type_manager.cc
type_manager.h
validator.cc
validator.h
# TODO(dsinclair): The writers should all be optional
writer/spv/generator.cc
writer/spv/generator.h
writer/wgsl/generator.cc
writer/wgsl/generator.h
writer/writer.cc
writer/writer.h
)
if(TINT_BUILD_SPV_PARSER)
list(APPEND TINT_LIB_SRCS
reader/spv/parser.cc
reader/spv/parser.h
)
endif()
set(TINT_TEST_SRCS
ast/binding_decoration_test.cc
ast/bool_literal_test.cc
ast/builtin_decoration_test.cc
ast/entry_point_test.cc
ast/import_test.cc
ast/int_literal_test.cc
ast/location_decoration_test.cc
ast/module_test.cc
ast/set_decoration_test.cc
ast/struct_member_test.cc
ast/struct_member_offset_decoration_test.cc
ast/struct_test.cc
ast/type/alias_type_test.cc
ast/type/array_type_test.cc
ast/type/bool_type_test.cc
ast/type/f32_type_test.cc
ast/type/i32_type_test.cc
ast/type/matrix_type_test.cc
ast/type/pointer_type_test.cc
ast/type/struct_type_test.cc
ast/type/u32_type_test.cc
ast/type/vector_type_test.cc
ast/uint_literal_test.cc
ast/variable_test.cc
reader/wgsl/lexer_test.cc
reader/wgsl/parser_test.cc
reader/wgsl/parser_impl_additive_expression_test.cc
reader/wgsl/parser_impl_and_expression_test.cc
reader/wgsl/parser_impl_argument_expression_list_test.cc
reader/wgsl/parser_impl_assignment_stmt_test.cc
reader/wgsl/parser_impl_body_stmt_test.cc
reader/wgsl/parser_impl_break_stmt_test.cc
reader/wgsl/parser_impl_builtin_decoration_test.cc
reader/wgsl/parser_impl_case_body_test.cc
reader/wgsl/parser_impl_const_expr_test.cc
reader/wgsl/parser_impl_const_literal_test.cc
reader/wgsl/parser_impl_continue_stmt_test.cc
reader/wgsl/parser_impl_continuing_stmt_test.cc
reader/wgsl/parser_impl_derivative_modifier_test.cc
reader/wgsl/parser_impl_else_stmt_test.cc
reader/wgsl/parser_impl_elseif_stmt_test.cc
reader/wgsl/parser_impl_entry_point_decl_test.cc
reader/wgsl/parser_impl_equality_expression_test.cc
reader/wgsl/parser_impl_exclusive_or_expression_test.cc
reader/wgsl/parser_impl_function_decl_test.cc
reader/wgsl/parser_impl_function_header_test.cc
reader/wgsl/parser_impl_function_type_decl_test.cc
reader/wgsl/parser_impl_global_constant_decl_test.cc
reader/wgsl/parser_impl_global_decl_test.cc
reader/wgsl/parser_impl_global_variable_decl_test.cc
reader/wgsl/parser_impl_if_stmt_test.cc
reader/wgsl/parser_impl_import_decl_test.cc
reader/wgsl/parser_impl_inclusive