Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | /// Copyright 2020 The Dawn & Tint Authors |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/hlsl/writer/ast_printer/ast_printer.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 30 | #include <cmath> |
| 31 | #include <functional> |
| 32 | #include <iomanip> |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
Ben Clayton | 8752366 | 2024-02-28 19:29:04 +0000 | [diff] [blame] | 36 | #include "src/tint/api/common/binding_point.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 37 | #include "src/tint/lang/core/constant/splat.h" |
| 38 | #include "src/tint/lang/core/constant/value.h" |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 39 | #include "src/tint/lang/core/fluent_types.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 40 | #include "src/tint/lang/core/type/array.h" |
| 41 | #include "src/tint/lang/core/type/atomic.h" |
| 42 | #include "src/tint/lang/core/type/depth_multisampled_texture.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 43 | #include "src/tint/lang/core/type/multisampled_texture.h" |
| 44 | #include "src/tint/lang/core/type/sampled_texture.h" |
| 45 | #include "src/tint/lang/core/type/storage_texture.h" |
| 46 | #include "src/tint/lang/core/type/texture_dimension.h" |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 47 | #include "src/tint/lang/hlsl/writer/ast_raise/calculate_array_length.h" |
| 48 | #include "src/tint/lang/hlsl/writer/ast_raise/decompose_memory_access.h" |
| 49 | #include "src/tint/lang/hlsl/writer/ast_raise/localize_struct_array_assignment.h" |
| 50 | #include "src/tint/lang/hlsl/writer/ast_raise/num_workgroups_from_uniform.h" |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 51 | #include "src/tint/lang/hlsl/writer/ast_raise/pixel_local.h" |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 52 | #include "src/tint/lang/hlsl/writer/ast_raise/truncate_interstage_variables.h" |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 53 | #include "src/tint/lang/hlsl/writer/common/option_helpers.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 54 | #include "src/tint/lang/wgsl/ast/call_statement.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 55 | #include "src/tint/lang/wgsl/ast/internal_attribute.h" |
| 56 | #include "src/tint/lang/wgsl/ast/interpolate_attribute.h" |
| 57 | #include "src/tint/lang/wgsl/ast/transform/add_empty_entry_point.h" |
| 58 | #include "src/tint/lang/wgsl/ast/transform/array_length_from_uniform.h" |
| 59 | #include "src/tint/lang/wgsl/ast/transform/binding_remapper.h" |
| 60 | #include "src/tint/lang/wgsl/ast/transform/builtin_polyfill.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 61 | #include "src/tint/lang/wgsl/ast/transform/canonicalize_entry_point_io.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 62 | #include "src/tint/lang/wgsl/ast/transform/demote_to_helper.h" |
| 63 | #include "src/tint/lang/wgsl/ast/transform/direct_variable_access.h" |
| 64 | #include "src/tint/lang/wgsl/ast/transform/disable_uniformity_analysis.h" |
| 65 | #include "src/tint/lang/wgsl/ast/transform/expand_compound_assignment.h" |
dan sinclair | f3b93c1 | 2024-03-20 13:38:35 +0000 | [diff] [blame] | 66 | #include "src/tint/lang/wgsl/ast/transform/fold_constants.h" |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 67 | #include "src/tint/lang/wgsl/ast/transform/manager.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 68 | #include "src/tint/lang/wgsl/ast/transform/multiplanar_external_texture.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 69 | #include "src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.h" |
| 70 | #include "src/tint/lang/wgsl/ast/transform/promote_side_effects_to_decl.h" |
dan sinclair | 997bc01 | 2024-02-22 02:08:41 +0000 | [diff] [blame] | 71 | #include "src/tint/lang/wgsl/ast/transform/remove_continue_in_switch.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 72 | #include "src/tint/lang/wgsl/ast/transform/remove_phonies.h" |
| 73 | #include "src/tint/lang/wgsl/ast/transform/robustness.h" |
| 74 | #include "src/tint/lang/wgsl/ast/transform/simplify_pointers.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 75 | #include "src/tint/lang/wgsl/ast/transform/unshadow.h" |
| 76 | #include "src/tint/lang/wgsl/ast/transform/vectorize_scalar_matrix_initializers.h" |
| 77 | #include "src/tint/lang/wgsl/ast/transform/zero_init_workgroup_memory.h" |
| 78 | #include "src/tint/lang/wgsl/ast/variable_decl_statement.h" |
dan sinclair | a4c1735 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 79 | #include "src/tint/lang/wgsl/helpers/append_vector.h" |
| 80 | #include "src/tint/lang/wgsl/helpers/check_supported_extensions.h" |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 81 | #include "src/tint/lang/wgsl/sem/block_statement.h" |
| 82 | #include "src/tint/lang/wgsl/sem/call.h" |
| 83 | #include "src/tint/lang/wgsl/sem/function.h" |
| 84 | #include "src/tint/lang/wgsl/sem/member_accessor_expression.h" |
| 85 | #include "src/tint/lang/wgsl/sem/module.h" |
| 86 | #include "src/tint/lang/wgsl/sem/statement.h" |
| 87 | #include "src/tint/lang/wgsl/sem/struct.h" |
| 88 | #include "src/tint/lang/wgsl/sem/switch_statement.h" |
| 89 | #include "src/tint/lang/wgsl/sem/value_constructor.h" |
| 90 | #include "src/tint/lang/wgsl/sem/value_conversion.h" |
| 91 | #include "src/tint/lang/wgsl/sem/variable.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 92 | #include "src/tint/utils/containers/map.h" |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 93 | #include "src/tint/utils/ice/ice.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 94 | #include "src/tint/utils/macros/compiler.h" |
| 95 | #include "src/tint/utils/macros/defer.h" |
| 96 | #include "src/tint/utils/macros/scoped_assignment.h" |
| 97 | #include "src/tint/utils/rtti/switch.h" |
Ben Clayton | 16be11d | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 98 | #include "src/tint/utils/strconv/float_to_string.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 99 | #include "src/tint/utils/text/string.h" |
| 100 | #include "src/tint/utils/text/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 101 | |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 102 | using namespace tint::core::number_suffixes; // NOLINT |
| 103 | using namespace tint::core::fluent_types; // NOLINT |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 104 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 105 | namespace tint::hlsl::writer { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | |
| 108 | const char kTempNamePrefix[] = "tint_tmp"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 109 | |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 110 | const char* ImageFormatToRWtextureType(core::TexelFormat image_format) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 111 | switch (image_format) { |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 112 | case core::TexelFormat::kR8Unorm: |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 113 | case core::TexelFormat::kBgra8Unorm: |
| 114 | case core::TexelFormat::kRgba8Unorm: |
| 115 | case core::TexelFormat::kRgba8Snorm: |
| 116 | case core::TexelFormat::kRgba16Float: |
| 117 | case core::TexelFormat::kR32Float: |
| 118 | case core::TexelFormat::kRg32Float: |
| 119 | case core::TexelFormat::kRgba32Float: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 120 | return "float4"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 121 | case core::TexelFormat::kRgba8Uint: |
| 122 | case core::TexelFormat::kRgba16Uint: |
| 123 | case core::TexelFormat::kR32Uint: |
| 124 | case core::TexelFormat::kRg32Uint: |
| 125 | case core::TexelFormat::kRgba32Uint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 126 | return "uint4"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 127 | case core::TexelFormat::kRgba8Sint: |
| 128 | case core::TexelFormat::kRgba16Sint: |
| 129 | case core::TexelFormat::kR32Sint: |
| 130 | case core::TexelFormat::kRg32Sint: |
| 131 | case core::TexelFormat::kRgba32Sint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 132 | return "int4"; |
| 133 | default: |
| 134 | return nullptr; |
| 135 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 136 | } |
| 137 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 138 | void PrintF32(StringStream& out, float value) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 139 | if (std::isinf(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 140 | out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 141 | } else if (std::isnan(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 142 | out << "0.0f /* nan */"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 143 | } else { |
dan sinclair | 37e9d11 | 2023-11-20 13:18:28 +0000 | [diff] [blame] | 144 | out << tint::strconv::FloatToString(value) << "f"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 148 | void PrintF16(StringStream& out, float value) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 149 | if (std::isinf(value)) { |
| 150 | out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
| 151 | } else if (std::isnan(value)) { |
| 152 | out << "0.0h /* nan */"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 153 | } else { |
dan sinclair | 37e9d11 | 2023-11-20 13:18:28 +0000 | [diff] [blame] | 154 | out << tint::strconv::FloatToString(value) << "h"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 158 | // Helper for writing " : register(RX, spaceY)", where R is the register, X is |
| 159 | // the binding point binding value, and Y is the binding point group value. |
| 160 | struct RegisterAndSpace { |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 161 | RegisterAndSpace(char r, BindingPoint bp) : reg(r), binding_point(bp) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 162 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 163 | const char reg; |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 164 | BindingPoint const binding_point; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 167 | StringStream& operator<<(StringStream& s, const RegisterAndSpace& rs) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 168 | s << " : register(" << rs.reg << rs.binding_point.binding; |
| 169 | // Omit the space if it's 0, as it's the default. |
| 170 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better compatibility. |
| 171 | if (rs.binding_point.group == 0) { |
| 172 | s << ")"; |
| 173 | } else { |
| 174 | s << ", space" << rs.binding_point.group << ")"; |
| 175 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 176 | return s; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 179 | } // namespace |
| 180 | |
| 181 | SanitizedResult::SanitizedResult() = default; |
| 182 | SanitizedResult::~SanitizedResult() = default; |
| 183 | SanitizedResult::SanitizedResult(SanitizedResult&&) = default; |
| 184 | |
Ben Clayton | 5ed5cc4 | 2023-09-22 10:31:04 +0000 | [diff] [blame] | 185 | SanitizedResult Sanitize(const Program& in, const Options& options) { |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 186 | ast::transform::Manager manager; |
| 187 | ast::transform::DataMap data; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 188 | |
dan sinclair | f3b93c1 | 2024-03-20 13:38:35 +0000 | [diff] [blame] | 189 | manager.Add<ast::transform::FoldConstants>(); |
| 190 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 191 | manager.Add<ast::transform::DisableUniformityAnalysis>(); |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 192 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 193 | // ExpandCompoundAssignment must come before BuiltinPolyfill |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 194 | manager.Add<ast::transform::ExpandCompoundAssignment>(); |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 195 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 196 | manager.Add<ast::transform::Unshadow>(); // Must come before DirectVariableAccess |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 197 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 198 | // LocalizeStructArrayAssignment must come after: |
| 199 | // * SimplifyPointers, because it assumes assignment to arrays in structs are |
| 200 | // done directly, not indirectly. |
| 201 | // TODO(crbug.com/tint/1340): See if we can get rid of the duplicate |
| 202 | // SimplifyPointers transform. Can't do it right now because |
| 203 | // LocalizeStructArrayAssignment introduces pointers. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 204 | manager.Add<ast::transform::SimplifyPointers>(); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 205 | manager.Add<LocalizeStructArrayAssignment>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 206 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 207 | manager.Add<ast::transform::PromoteSideEffectsToDecl>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 208 | |
| 209 | if (!options.disable_robustness) { |
Ben Clayton | 8525ff2 | 2023-03-06 21:05:01 +0000 | [diff] [blame] | 210 | // Robustness must come after PromoteSideEffectsToDecl |
| 211 | // Robustness must come before BuiltinPolyfill and CanonicalizeEntryPointIO |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 212 | manager.Add<ast::transform::Robustness>(); |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 213 | |
| 214 | ast::transform::Robustness::Config config = {}; |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 215 | config.bindings_ignored = std::unordered_set<BindingPoint>( |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 216 | options.bindings.ignored_by_robustness_transform.cbegin(), |
| 217 | options.bindings.ignored_by_robustness_transform.cend()); |
Jiawei Shao | 2e11963 | 2023-06-21 01:43:25 +0000 | [diff] [blame] | 218 | |
| 219 | // Direct3D guarantees to return zero for any resource that is accessed out of bounds, and |
| 220 | // according to the description of the assembly store_uav_typed, out of bounds addressing |
| 221 | // means nothing gets written to memory. |
| 222 | config.texture_action = ast::transform::Robustness::Action::kIgnore; |
| 223 | |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 224 | data.Add<ast::transform::Robustness::Config>(config); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 225 | } |
| 226 | |
dan sinclair | cd5a5e9 | 2024-06-06 02:09:13 +0000 | [diff] [blame] | 227 | tint::transform::multiplanar::BindingsMap multiplanar_map{}; |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 228 | RemapperData remapper_data{}; |
| 229 | ArrayLengthFromUniformOptions array_length_from_uniform_options{}; |
dan sinclair | cd5a5e9 | 2024-06-06 02:09:13 +0000 | [diff] [blame] | 230 | PopulateBindingRelatedOptions(options, remapper_data, multiplanar_map, |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 231 | array_length_from_uniform_options); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 232 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 233 | manager.Add<ast::transform::BindingRemapper>(); |
dan sinclair | 1521550 | 2023-09-26 11:31:47 +0000 | [diff] [blame] | 234 | // D3D11 and 12 registers like `t3` and `c3` have the same bindingOffset number in |
| 235 | // the remapping but should not be considered a collision because they have |
| 236 | // different types. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 237 | data.Add<ast::transform::BindingRemapper::Remappings>( |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 238 | remapper_data, options.bindings.access_controls, /* allow_collisions */ true); |
| 239 | |
| 240 | // Note: it is more efficient for MultiplanarExternalTexture to come after Robustness |
| 241 | // MultiplanarExternalTexture must come after BindingRemapper |
dan sinclair | cd5a5e9 | 2024-06-06 02:09:13 +0000 | [diff] [blame] | 242 | data.Add<ast::transform::MultiplanarExternalTexture::NewBindingPoints>(multiplanar_map, |
| 243 | /* may_collide */ true); |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 244 | manager.Add<ast::transform::MultiplanarExternalTexture>(); |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 245 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 246 | { // Builtin polyfills |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 247 | ast::transform::BuiltinPolyfill::Builtins polyfills; |
| 248 | polyfills.acosh = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | d23f296 | 2022-06-28 15:27:44 +0000 | [diff] [blame] | 249 | polyfills.asinh = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 250 | polyfills.atanh = ast::transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 02f04d9 | 2022-11-03 19:15:17 +0000 | [diff] [blame] | 251 | polyfills.bitshift_modulo = true; |
Ben Clayton | 6dbb463 | 2022-10-31 17:54:49 +0000 | [diff] [blame] | 252 | polyfills.clamp_int = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 253 | // TODO(crbug.com/tint/1449): Some of these can map to HLSL's `firstbitlow` |
| 254 | // and `firstbithigh`. |
Ben Clayton | cc3f851 | 2023-03-09 21:26:12 +0000 | [diff] [blame] | 255 | polyfills.conv_f32_to_iu32 = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 256 | polyfills.count_leading_zeros = true; |
| 257 | polyfills.count_trailing_zeros = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 258 | polyfills.extract_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 259 | polyfills.first_leading_bit = true; |
| 260 | polyfills.first_trailing_bit = true; |
James Price | 6fd43a0 | 2024-04-18 11:42:27 +0000 | [diff] [blame] | 261 | polyfills.fwidth_fine = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 262 | polyfills.insert_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
Jiawei Shao | 825f626 | 2024-01-08 10:05:27 +0000 | [diff] [blame] | 263 | polyfills.int_div_mod = !options.disable_polyfill_integer_div_mod; |
Antonio Maiorano | ee665a4 | 2023-02-14 16:12:59 +0000 | [diff] [blame] | 264 | polyfills.precise_float_mod = true; |
Zhaoming Jiang | 04529be | 2023-02-27 02:59:50 +0000 | [diff] [blame] | 265 | polyfills.reflect_vec2_f32 = options.polyfill_reflect_vec2_f32; |
Ben Clayton | c4ebf2c | 2022-09-22 22:59:16 +0000 | [diff] [blame] | 266 | polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true; |
James Price | 128980f | 2023-01-06 02:25:06 +0000 | [diff] [blame] | 267 | polyfills.workgroup_uniform_load = true; |
Jiawei Shao | 7c0a801 | 2023-11-29 00:25:56 +0000 | [diff] [blame] | 268 | polyfills.dot_4x8_packed = options.polyfill_dot_4x8_packed; |
Jiawei Shao | dd81723 | 2024-01-12 08:10:46 +0000 | [diff] [blame] | 269 | polyfills.pack_unpack_4x8 = options.polyfill_pack_unpack_4x8; |
| 270 | // Currently Pack4xU8Clamp() must be polyfilled because on latest DXC pack_clamp_u8() |
| 271 | // receives an int32_t4 as its input. |
| 272 | // See https://github.com/microsoft/DirectXShaderCompiler/issues/5091 for more details. |
| 273 | polyfills.pack_4xu8_clamp = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 274 | data.Add<ast::transform::BuiltinPolyfill::Config>(polyfills); |
| 275 | manager.Add<ast::transform::BuiltinPolyfill>(); // Must come before DirectVariableAccess |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 276 | } |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 277 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 278 | manager.Add<ast::transform::DirectVariableAccess>(); |
Antonio Maiorano | fa00fe9 | 2023-05-03 15:30:54 +0000 | [diff] [blame] | 279 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 280 | if (!options.disable_workgroup_init) { |
| 281 | // ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as |
| 282 | // ZeroInitWorkgroupMemory may inject new builtin parameters. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 283 | manager.Add<ast::transform::ZeroInitWorkgroupMemory>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 284 | } |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 285 | |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 286 | { |
| 287 | PixelLocal::Config cfg; |
dan sinclair | 260c1f0 | 2024-06-06 15:31:12 +0000 | [diff] [blame] | 288 | for (auto it : options.pixel_local.attachments) { |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 289 | cfg.pls_member_to_rov_reg.Add(it.first, it.second); |
| 290 | } |
dan sinclair | 260c1f0 | 2024-06-06 15:31:12 +0000 | [diff] [blame] | 291 | for (auto it : options.pixel_local.attachment_formats) { |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 292 | core::TexelFormat format = core::TexelFormat::kUndefined; |
| 293 | switch (it.second) { |
| 294 | case PixelLocalOptions::TexelFormat::kR32Sint: |
| 295 | format = core::TexelFormat::kR32Sint; |
| 296 | break; |
| 297 | case PixelLocalOptions::TexelFormat::kR32Uint: |
| 298 | format = core::TexelFormat::kR32Uint; |
| 299 | break; |
| 300 | case PixelLocalOptions::TexelFormat::kR32Float: |
| 301 | format = core::TexelFormat::kR32Float; |
| 302 | break; |
| 303 | default: |
| 304 | TINT_ICE() << "missing texel format for pixel local storage attachment"; |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 305 | } |
| 306 | cfg.pls_member_to_rov_format.Add(it.first, format); |
| 307 | } |
dan sinclair | 260c1f0 | 2024-06-06 15:31:12 +0000 | [diff] [blame] | 308 | cfg.rov_group_index = options.pixel_local.group_index; |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 309 | data.Add<PixelLocal::Config>(cfg); |
| 310 | manager.Add<PixelLocal>(); |
| 311 | } |
| 312 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 313 | // CanonicalizeEntryPointIO must come after Robustness |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 314 | manager.Add<ast::transform::CanonicalizeEntryPointIO>(); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 315 | |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame] | 316 | if (options.truncate_interstage_variables) { |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 317 | // When interstage_locations is empty, it means there's no user-defined interstage variables |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame] | 318 | // being used in the next stage. Still, HLSL compiler register mismatch could happen, if |
| 319 | // there's builtin inputs used in the next stage. So we still run |
| 320 | // TruncateInterstageVariables transform. |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 321 | |
| 322 | // TruncateInterstageVariables itself will skip when interstage_locations matches exactly |
| 323 | // with the current stage output. |
| 324 | |
| 325 | // Build the config for internal TruncateInterstageVariables transform. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 326 | TruncateInterstageVariables::Config truncate_interstage_variables_cfg; |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 327 | truncate_interstage_variables_cfg.interstage_locations = |
| 328 | std::move(options.interstage_locations); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 329 | manager.Add<TruncateInterstageVariables>(); |
| 330 | data.Add<TruncateInterstageVariables::Config>(std::move(truncate_interstage_variables_cfg)); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 331 | } |
| 332 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 333 | // NumWorkgroupsFromUniform must come after CanonicalizeEntryPointIO, as it |
| 334 | // assumes that num_workgroups builtins only appear as struct members and are |
| 335 | // only accessed directly via member accessors. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 336 | manager.Add<NumWorkgroupsFromUniform>(); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 337 | manager.Add<ast::transform::VectorizeScalarMatrixInitializers>(); |
| 338 | manager.Add<ast::transform::SimplifyPointers>(); |
| 339 | manager.Add<ast::transform::RemovePhonies>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 340 | |
| 341 | // DemoteToHelper must come after CanonicalizeEntryPointIO, PromoteSideEffectsToDecl, and |
| 342 | // ExpandCompoundAssignment. |
| 343 | // TODO(crbug.com/tint/1752): This is only necessary when FXC is being used. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 344 | manager.Add<ast::transform::DemoteToHelper>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 345 | |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 346 | // ArrayLengthFromUniform must come after SimplifyPointers as it assumes that the form of the |
| 347 | // array length argument is &var.array. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 348 | manager.Add<ast::transform::ArrayLengthFromUniform>(); |
Ben Clayton | 8752366 | 2024-02-28 19:29:04 +0000 | [diff] [blame] | 349 | // Build the config for the internal ArrayLengthFromUniform transform. |
| 350 | ast::transform::ArrayLengthFromUniform::Config array_length_from_uniform_cfg( |
| 351 | BindingPoint{array_length_from_uniform_options.ubo_binding.group, |
| 352 | array_length_from_uniform_options.ubo_binding.binding}); |
| 353 | array_length_from_uniform_cfg.bindpoint_to_size_index = |
| 354 | std::move(array_length_from_uniform_options.bindpoint_to_size_index); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 355 | data.Add<ast::transform::ArrayLengthFromUniform::Config>( |
| 356 | std::move(array_length_from_uniform_cfg)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 357 | // DecomposeMemoryAccess must come after: |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 358 | // * SimplifyPointers, as we cannot take the address of calls to |
| 359 | // DecomposeMemoryAccess::Intrinsic and we need to fold away the address-of and dereferences |
| 360 | // of `*(&(intrinsic_load()))` expressions. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 361 | // * RemovePhonies, as phonies can be assigned a pointer to a |
| 362 | // non-constructible buffer, or dynamic array, which DMA cannot cope with. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 363 | manager.Add<DecomposeMemoryAccess>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 364 | // CalculateArrayLength must come after DecomposeMemoryAccess, as |
| 365 | // DecomposeMemoryAccess special-cases the arrayLength() intrinsic, which |
| 366 | // will be transformed by CalculateArrayLength |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 367 | manager.Add<CalculateArrayLength>(); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 368 | manager.Add<ast::transform::PromoteInitializersToLet>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 369 | |
dan sinclair | 997bc01 | 2024-02-22 02:08:41 +0000 | [diff] [blame] | 370 | manager.Add<ast::transform::RemoveContinueInSwitch>(); |
Antonio Maiorano | b349710 | 2022-03-31 15:02:25 +0000 | [diff] [blame] | 371 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 372 | manager.Add<ast::transform::AddEmptyEntryPoint>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 373 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 374 | data.Add<ast::transform::CanonicalizeEntryPointIO::Config>( |
| 375 | ast::transform::CanonicalizeEntryPointIO::ShaderStyle::kHlsl); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 376 | data.Add<NumWorkgroupsFromUniform::Config>(options.root_constant_binding_point); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 377 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 378 | SanitizedResult result; |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 379 | ast::transform::DataMap outputs; |
James Price | 0e6534e | 2023-05-17 01:21:45 +0000 | [diff] [blame] | 380 | result.program = manager.Run(in, data, outputs); |
| 381 | if (auto* res = outputs.Get<ast::transform::ArrayLengthFromUniform::Result>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 382 | result.used_array_length_from_uniform_indices = std::move(res->used_size_indices); |
| 383 | } |
| 384 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Ben Clayton | 5ed5cc4 | 2023-09-22 10:31:04 +0000 | [diff] [blame] | 387 | ASTPrinter::ASTPrinter(const Program& program) : builder_(ProgramBuilder::Wrap(program)) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 388 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 389 | ASTPrinter::~ASTPrinter() = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 390 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 391 | bool ASTPrinter::Generate() { |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 392 | if (!tint::wgsl::CheckSupportedExtensions( |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 393 | "HLSL", builder_.AST(), diagnostics_, |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 394 | Vector{ |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 395 | wgsl::Extension::kChromiumDisableUniformityAnalysis, |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 396 | wgsl::Extension::kChromiumExperimentalPixelLocal, |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 397 | wgsl::Extension::kChromiumExperimentalPushConstant, |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 398 | wgsl::Extension::kChromiumExperimentalSubgroups, |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 399 | wgsl::Extension::kChromiumInternalGraphite, |
| 400 | wgsl::Extension::kF16, |
Jiawei Shao | cc43f15 | 2024-05-29 00:44:43 +0000 | [diff] [blame] | 401 | wgsl::Extension::kDualSourceBlending, |
James Price | aa88fa0 | 2024-06-26 11:25:37 +0000 | [diff] [blame] | 402 | wgsl::Extension::kSubgroups, |
| 403 | wgsl::Extension::kSubgroupsF16, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 404 | })) { |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 408 | const tint::TypeInfo* last_kind = nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 409 | size_t last_padding_line = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 410 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 411 | auto* mod = builder_.Sem().Module(); |
| 412 | for (auto* decl : mod->DependencyOrderedDeclarations()) { |
James Price | 631aaa3 | 2023-11-02 15:03:02 +0000 | [diff] [blame] | 413 | if (decl->IsAnyOf<ast::Alias, ast::DiagnosticDirective, ast::Enable, ast::Requires, |
| 414 | ast::ConstAssert>()) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 415 | continue; // These are not emitted. |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 416 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 417 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 418 | // Emit a new line between declarations if the type of declaration has |
| 419 | // changed, or we're about to emit a function |
| 420 | auto* kind = &decl->TypeInfo(); |
| 421 | if (current_buffer_->lines.size() != last_padding_line) { |
| 422 | if (last_kind && (last_kind != kind || decl->Is<ast::Function>())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 423 | Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 424 | last_padding_line = current_buffer_->lines.size(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 425 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 426 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 427 | last_kind = kind; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 428 | |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 429 | global_insertion_point_ = current_buffer_->lines.size(); |
| 430 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 431 | bool ok = Switch( |
| 432 | decl, |
| 433 | [&](const ast::Variable* global) { // |
| 434 | return EmitGlobalVariable(global); |
| 435 | }, |
| 436 | [&](const ast::Struct* str) { |
| 437 | auto* ty = builder_.Sem().Get(str); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 438 | auto address_space_uses = ty->AddressSpaceUsage(); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 439 | if (address_space_uses.Count() != |
| 440 | ((address_space_uses.Contains(core::AddressSpace::kStorage) ? 1u : 0u) + |
| 441 | (address_space_uses.Contains(core::AddressSpace::kUniform) ? 1u : 0u))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 442 | // The structure is used as something other than a storage buffer or |
| 443 | // uniform buffer, so it needs to be emitted. |
| 444 | // Storage buffer are read and written to via a ByteAddressBuffer |
| 445 | // instead of true structure. |
| 446 | // Structures used as uniform buffer are read from an array of |
| 447 | // vectors instead of true structure. |
| 448 | return EmitStructType(current_buffer_, ty); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 449 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 450 | return true; |
| 451 | }, |
| 452 | [&](const ast::Function* func) { |
| 453 | if (func->IsEntryPoint()) { |
| 454 | return EmitEntryPointFunction(func); |
| 455 | } |
| 456 | return EmitFunction(func); |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 457 | }, // |
| 458 | TINT_ICE_ON_NO_MATCH); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 459 | |
| 460 | if (!ok) { |
| 461 | return false; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | if (!helpers_.lines.empty()) { |
| 466 | current_buffer_->Insert(helpers_, 0, 0); |
| 467 | } |
| 468 | |
| 469 | return true; |
| 470 | } |
| 471 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 472 | bool ASTPrinter::EmitDynamicVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 473 | const core::type::Vector* vec) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 474 | auto name = tint::GetOrAdd(dynamic_vector_write_, vec, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 475 | std::string fn = UniqueIdentifier("set_vector_element"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 476 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 477 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 478 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 479 | if (!EmitTypeAndName(out, vec, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 480 | "vec")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 481 | return ""; |
| 482 | } |
| 483 | out << ", int idx, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 484 | if (!EmitTypeAndName(out, vec->type(), core::AddressSpace::kUndefined, |
| 485 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 486 | return ""; |
| 487 | } |
| 488 | out << ") {"; |
| 489 | } |
| 490 | { |
| 491 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 492 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 493 | switch (vec->Width()) { |
| 494 | case 2: |
| 495 | out << "vec = (idx.xx == int2(0, 1)) ? val.xx : vec;"; |
| 496 | break; |
| 497 | case 3: |
| 498 | out << "vec = (idx.xxx == int3(0, 1, 2)) ? val.xxx : vec;"; |
| 499 | break; |
| 500 | case 4: |
| 501 | out << "vec = (idx.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : vec;"; |
| 502 | break; |
| 503 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 504 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 505 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 506 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 507 | Line(&helpers_) << "}"; |
| 508 | Line(&helpers_); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 509 | return fn; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 510 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 511 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 512 | if (name.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 513 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 514 | } |
| 515 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 516 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 517 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 518 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 519 | out << name << "("; |
| 520 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 521 | return false; |
| 522 | } |
| 523 | out << ", "; |
| 524 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 525 | return false; |
| 526 | } |
| 527 | out << ", "; |
| 528 | if (!EmitExpression(out, stmt->rhs)) { |
| 529 | return false; |
| 530 | } |
| 531 | out << ");"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 532 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 533 | return true; |
| 534 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 535 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 536 | bool ASTPrinter::EmitDynamicMatrixVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 537 | const core::type::Matrix* mat) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 538 | auto name = tint::GetOrAdd(dynamic_matrix_vector_write_, mat, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 539 | std::string fn = UniqueIdentifier("set_matrix_column"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 540 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 541 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 542 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 543 | if (!EmitTypeAndName(out, mat, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 544 | "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 545 | return ""; |
| 546 | } |
| 547 | out << ", int col, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 548 | if (!EmitTypeAndName(out, mat->ColumnType(), core::AddressSpace::kUndefined, |
| 549 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 550 | return ""; |
| 551 | } |
| 552 | out << ") {"; |
| 553 | } |
| 554 | { |
| 555 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 556 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 557 | { |
| 558 | ScopedIndent si2(&helpers_); |
| 559 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 560 | Line(&helpers_) << "case " << i << ": mat[" << i << "] = val; break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 563 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 564 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 565 | Line(&helpers_) << "}"; |
| 566 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 567 | return fn; |
| 568 | }); |
| 569 | |
| 570 | if (name.empty()) { |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 575 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 576 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 577 | out << name << "("; |
| 578 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 579 | return false; |
| 580 | } |
| 581 | out << ", "; |
| 582 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 583 | return false; |
| 584 | } |
| 585 | out << ", "; |
| 586 | if (!EmitExpression(out, stmt->rhs)) { |
| 587 | return false; |
| 588 | } |
| 589 | out << ");"; |
| 590 | |
| 591 | return true; |
| 592 | } |
| 593 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 594 | bool ASTPrinter::EmitDynamicMatrixScalarAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 595 | const core::type::Matrix* mat) { |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 596 | auto* lhs_row_access = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 597 | auto* lhs_col_access = lhs_row_access->object->As<ast::IndexAccessorExpression>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 598 | |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 599 | auto name = tint::GetOrAdd(dynamic_matrix_scalar_write_, mat, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 600 | std::string fn = UniqueIdentifier("set_matrix_scalar"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 601 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 602 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 603 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 604 | if (!EmitTypeAndName(out, mat, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 605 | "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 606 | return ""; |
| 607 | } |
| 608 | out << ", int col, int row, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 609 | if (!EmitTypeAndName(out, mat->type(), core::AddressSpace::kUndefined, |
| 610 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 611 | return ""; |
| 612 | } |
| 613 | out << ") {"; |
| 614 | } |
| 615 | { |
| 616 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 617 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 618 | { |
| 619 | ScopedIndent si2(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 620 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 621 | Line(&helpers_) << "case " << i << ":"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 622 | { |
| 623 | auto vec_name = "mat[" + std::to_string(i) + "]"; |
| 624 | ScopedIndent si3(&helpers_); |
| 625 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 626 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 627 | switch (mat->rows()) { |
| 628 | case 2: |
| 629 | out << vec_name |
| 630 | << " = (row.xx == int2(0, 1)) ? val.xx : " << vec_name |
| 631 | << ";"; |
| 632 | break; |
| 633 | case 3: |
| 634 | out << vec_name |
| 635 | << " = (row.xxx == int3(0, 1, 2)) ? val.xxx : " << vec_name |
| 636 | << ";"; |
| 637 | break; |
| 638 | case 4: |
| 639 | out << vec_name |
| 640 | << " = (row.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : " |
| 641 | << vec_name << ";"; |
| 642 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 643 | default: { |
| 644 | auto* vec = TypeOf(lhs_row_access->object) |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 645 | ->UnwrapPtrOrRef() |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 646 | ->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 647 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 648 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 651 | Line(&helpers_) << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 655 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 656 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 657 | Line(&helpers_) << "}"; |
| 658 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 659 | return fn; |
| 660 | }); |
| 661 | |
| 662 | if (name.empty()) { |
| 663 | return false; |
| 664 | } |
| 665 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 666 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 667 | out << name << "("; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 668 | if (!EmitExpression(out, lhs_col_access->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | out << ", "; |
| 672 | if (!EmitExpression(out, lhs_col_access->index)) { |
| 673 | return false; |
| 674 | } |
| 675 | out << ", "; |
| 676 | if (!EmitExpression(out, lhs_row_access->index)) { |
| 677 | return false; |
| 678 | } |
| 679 | out << ", "; |
| 680 | if (!EmitExpression(out, stmt->rhs)) { |
| 681 | return false; |
| 682 | } |
| 683 | out << ");"; |
| 684 | |
| 685 | return true; |
| 686 | } |
| 687 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 688 | bool ASTPrinter::EmitIndexAccessor(StringStream& out, const ast::IndexAccessorExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 689 | if (!EmitExpression(out, expr->object)) { |
| 690 | return false; |
| 691 | } |
| 692 | out << "["; |
| 693 | |
| 694 | if (!EmitExpression(out, expr->index)) { |
| 695 | return false; |
| 696 | } |
| 697 | out << "]"; |
| 698 | |
| 699 | return true; |
| 700 | } |
| 701 | |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 702 | bool ASTPrinter::EmitBitcastCall(StringStream& out, const ast::CallExpression* call) { |
| 703 | auto* arg = call->args[0]; |
| 704 | auto* src_type = TypeOf(arg)->UnwrapRef(); |
| 705 | auto* dst_type = TypeOf(call); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 706 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 707 | auto* src_el_type = src_type->DeepestElement(); |
| 708 | auto* dst_el_type = dst_type->DeepestElement(); |
| 709 | |
| 710 | if (!dst_el_type->is_integer_scalar() && !dst_el_type->is_float_scalar()) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 711 | diagnostics_.AddError(Source{}) |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 712 | << "Unable to do bitcast to type " << dst_el_type->FriendlyName(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 713 | return false; |
| 714 | } |
| 715 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 716 | // Handle identity bitcast. |
| 717 | if (src_type == dst_type) { |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 718 | return EmitExpression(out, arg); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // Handle the f16 types using polyfill functions |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 722 | if (src_el_type->Is<core::type::F16>() || dst_el_type->Is<core::type::F16>()) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 723 | auto f16_bitcast_polyfill = [&]() { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 724 | if (src_el_type->Is<core::type::F16>()) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 725 | // Source type must be vec2<f16> or vec4<f16>, since type f16 and vec3<f16> can only |
| 726 | // have identity bitcast. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 727 | auto* src_vec = src_type->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 728 | TINT_ASSERT(src_vec); |
| 729 | TINT_ASSERT(((src_vec->Width() == 2u) || (src_vec->Width() == 4u))); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 730 | |
| 731 | // Bitcast f16 types to others by converting the given f16 value to f32 and call |
| 732 | // f32tof16 to get the bits. This should be safe, because the convertion is precise |
| 733 | // for finite and infinite f16 value as they are exactly representable by f32, and |
| 734 | // WGSL spec allow any result if f16 value is NaN. |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 735 | return tint::GetOrAdd( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 736 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 737 | TextBuffer b; |
| 738 | TINT_DEFER(helpers_.Append(b)); |
| 739 | |
| 740 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_from_f16")); |
| 741 | { |
| 742 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 743 | if (!EmitTypeAndName(decl, dst_type, core::AddressSpace::kUndefined, |
| 744 | core::Access::kUndefined, fn_name)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 745 | return ""; |
| 746 | } |
| 747 | { |
| 748 | ScopedParen sp(decl); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 749 | if (!EmitTypeAndName(decl, src_type, core::AddressSpace::kUndefined, |
| 750 | core::Access::kUndefined, "src")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 751 | return ""; |
| 752 | } |
| 753 | } |
| 754 | decl << " {"; |
| 755 | } |
| 756 | { |
| 757 | ScopedIndent si(&b); |
| 758 | { |
| 759 | Line(&b) << "uint" << src_vec->Width() << " r = f32tof16(float" |
| 760 | << src_vec->Width() << "(src));"; |
| 761 | |
| 762 | { |
| 763 | auto s = Line(&b); |
| 764 | s << "return as"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 765 | if (!EmitType(s, dst_el_type, core::AddressSpace::kUndefined, |
| 766 | core::Access::kReadWrite, "")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 767 | return ""; |
| 768 | } |
| 769 | s << "("; |
| 770 | switch (src_vec->Width()) { |
| 771 | case 2: { |
| 772 | s << "uint((r.x & 0xffff) | ((r.y & 0xffff) << 16))"; |
| 773 | break; |
| 774 | } |
| 775 | case 4: { |
| 776 | s << "uint2((r.x & 0xffff) | ((r.y & 0xffff) << 16), " |
| 777 | "(r.z & 0xffff) | ((r.w & 0xffff) << 16))"; |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | s << ");"; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | Line(&b) << "}"; |
| 786 | Line(&b); |
| 787 | return fn_name; |
| 788 | }); |
| 789 | } else { |
| 790 | // Destination type must be vec2<f16> or vec4<f16>. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 791 | auto* dst_vec = dst_type->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 792 | TINT_ASSERT((dst_vec && ((dst_vec->Width() == 2u) || (dst_vec->Width() == 4u)) && |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 793 | dst_el_type->Is<core::type::F16>())); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 794 | // Source type must be f32/i32/u32 or vec2<f32/i32/u32>. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 795 | auto* src_vec = src_type->As<core::type::Vector>(); |
| 796 | TINT_ASSERT( |
| 797 | (src_type->IsAnyOf<core::type::I32, core::type::U32, core::type::F32>() || |
| 798 | (src_vec && src_vec->Width() == 2u && |
| 799 | src_el_type->IsAnyOf<core::type::I32, core::type::U32, core::type::F32>()))); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 800 | std::string src_type_suffix = (src_vec ? "2" : ""); |
| 801 | |
| 802 | // Bitcast other types to f16 types by reinterpreting their bits as f16 using |
| 803 | // f16tof32, and convert the result f32 to f16. This should be safe, because the |
| 804 | // convertion is precise for finite and infinite f16 result value as they are |
| 805 | // exactly representable by f32, and WGSL spec allow any result if f16 result value |
| 806 | // would be NaN. |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 807 | return tint::GetOrAdd( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 808 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 809 | TextBuffer b; |
| 810 | TINT_DEFER(helpers_.Append(b)); |
| 811 | |
| 812 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_to_f16")); |
| 813 | { |
| 814 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 815 | if (!EmitTypeAndName(decl, dst_type, core::AddressSpace::kUndefined, |
| 816 | core::Access::kUndefined, fn_name)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 817 | return ""; |
| 818 | } |
| 819 | { |
| 820 | ScopedParen sp(decl); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 821 | if (!EmitTypeAndName(decl, src_type, core::AddressSpace::kUndefined, |
| 822 | core::Access::kUndefined, "src")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 823 | return ""; |
| 824 | } |
| 825 | } |
| 826 | decl << " {"; |
| 827 | } |
| 828 | { |
| 829 | ScopedIndent si(&b); |
| 830 | { |
| 831 | // Convert the source to uint for f16tof32. |
| 832 | Line(&b) << "uint" << src_type_suffix << " v = asuint(src);"; |
| 833 | // Reinterprete the low 16 bits and high 16 bits |
| 834 | Line(&b) << "float" << src_type_suffix |
| 835 | << " t_low = f16tof32(v & 0xffff);"; |
| 836 | Line(&b) << "float" << src_type_suffix |
| 837 | << " t_high = f16tof32((v >> 16) & 0xffff);"; |
| 838 | // Construct the result f16 vector |
| 839 | { |
| 840 | auto s = Line(&b); |
| 841 | s << "return "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 842 | if (!EmitType(s, dst_type, core::AddressSpace::kUndefined, |
| 843 | core::Access::kReadWrite, "")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 844 | return ""; |
| 845 | } |
| 846 | s << "("; |
| 847 | switch (dst_vec->Width()) { |
| 848 | case 2: { |
| 849 | s << "t_low.x, t_high.x"; |
| 850 | break; |
| 851 | } |
| 852 | case 4: { |
| 853 | s << "t_low.x, t_high.x, t_low.y, t_high.y"; |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | s << ");"; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | Line(&b) << "}"; |
| 862 | Line(&b); |
| 863 | return fn_name; |
| 864 | }); |
| 865 | } |
| 866 | }; |
| 867 | |
| 868 | // Get or create the polyfill |
| 869 | auto fn = f16_bitcast_polyfill(); |
| 870 | if (fn.empty()) { |
| 871 | return false; |
| 872 | } |
| 873 | // Call the polyfill |
| 874 | out << fn; |
| 875 | { |
| 876 | ScopedParen sp(out); |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 877 | if (!EmitExpression(out, arg)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 878 | return false; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return true; |
| 883 | } |
| 884 | |
| 885 | // Otherwise, bitcasting between non-f16 types. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 886 | TINT_ASSERT((!src_el_type->Is<core::type::F16>() && !dst_el_type->Is<core::type::F16>())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 887 | out << "as"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 888 | if (!EmitType(out, dst_el_type, core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 889 | return false; |
| 890 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 891 | out << "("; |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 892 | if (!EmitExpression(out, arg)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 893 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 894 | } |
| 895 | out << ")"; |
| 896 | return true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 897 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 898 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 899 | bool ASTPrinter::EmitAssign(const ast::AssignmentStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 900 | if (auto* lhs_access = stmt->lhs->As<ast::IndexAccessorExpression>()) { |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 901 | auto validate_obj_not_pointer = [&](const core::type::Type* object_ty) { |
| 902 | if (TINT_UNLIKELY(object_ty->Is<core::type::Pointer>())) { |
| 903 | TINT_ICE() << "lhs of index accessor should not be a pointer. These should have " |
| 904 | "been removed by transforms such as SimplifyPointers, " |
| 905 | "DecomposeMemoryAccess, and DirectVariableAccess"; |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 906 | } |
| 907 | return true; |
| 908 | }; |
| 909 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 910 | // BUG(crbug.com/tint/1333): work around assignment of scalar to matrices |
| 911 | // with at least one dynamic index |
| 912 | if (auto* lhs_sub_access = lhs_access->object->As<ast::IndexAccessorExpression>()) { |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 913 | const auto* lhs_sub_access_type = TypeOf(lhs_sub_access->object); |
| 914 | if (!validate_obj_not_pointer(lhs_sub_access_type)) { |
| 915 | return false; |
| 916 | } |
| 917 | if (auto* mat = lhs_sub_access_type->UnwrapRef()->As<core::type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 918 | auto* rhs_row_idx_sem = builder_.Sem().GetVal(lhs_access->index); |
| 919 | auto* rhs_col_idx_sem = builder_.Sem().GetVal(lhs_sub_access->index); |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 920 | if (!rhs_row_idx_sem->ConstantValue() || !rhs_col_idx_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 921 | return EmitDynamicMatrixScalarAssignment(stmt, mat); |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | // BUG(crbug.com/tint/1333): work around assignment of vector to matrices |
| 926 | // with dynamic indices |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 927 | const auto* lhs_access_type = TypeOf(lhs_access->object); |
| 928 | if (!validate_obj_not_pointer(lhs_access_type)) { |
| 929 | return false; |
| 930 | } |
| 931 | if (auto* mat = lhs_access_type->UnwrapRef()->As<core::type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 932 | auto* lhs_index_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 933 | if (!lhs_index_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 934 | return EmitDynamicMatrixVectorAssignment(stmt, mat); |
| 935 | } |
| 936 | } |
| 937 | // BUG(crbug.com/tint/534): work around assignment to vectors with dynamic |
| 938 | // indices |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 939 | if (auto* vec = lhs_access_type->UnwrapRef()->As<core::type::Vector>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 940 | auto* rhs_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 941 | if (!rhs_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 942 | return EmitDynamicVectorAssignment(stmt, vec); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 947 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 948 | if (!EmitExpression(out, stmt->lhs)) { |
| 949 | return false; |
| 950 | } |
| 951 | out << " = "; |
| 952 | if (!EmitExpression(out, stmt->rhs)) { |
| 953 | return false; |
| 954 | } |
| 955 | out << ";"; |
| 956 | return true; |
| 957 | } |
| 958 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 959 | bool ASTPrinter::EmitBinary(StringStream& out, const ast::BinaryExpression* expr) { |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 960 | if (expr->op == core::BinaryOp::kLogicalAnd || expr->op == core::BinaryOp::kLogicalOr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 961 | auto name = UniqueIdentifier(kTempNamePrefix); |
| 962 | |
| 963 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 964 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 965 | pre << "bool " << name << " = "; |
| 966 | if (!EmitExpression(pre, expr->lhs)) { |
| 967 | return false; |
| 968 | } |
| 969 | pre << ";"; |
| 970 | } |
| 971 | |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 972 | if (expr->op == core::BinaryOp::kLogicalOr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 973 | Line() << "if (!" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 974 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 975 | Line() << "if (" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | { |
| 979 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 980 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 981 | pre << name << " = "; |
| 982 | if (!EmitExpression(pre, expr->rhs)) { |
| 983 | return false; |
| 984 | } |
| 985 | pre << ";"; |
| 986 | } |
| 987 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 988 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 989 | |
| 990 | out << "(" << name << ")"; |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); |
| 995 | auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); |
| 996 | // Multiplying by a matrix requires the use of `mul` in order to get the |
| 997 | // type of multiply we desire. |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 998 | if (expr->op == core::BinaryOp::kMultiply && |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 999 | ((lhs_type->Is<core::type::Vector>() && rhs_type->Is<core::type::Matrix>()) || |
| 1000 | (lhs_type->Is<core::type::Matrix>() && rhs_type->Is<core::type::Vector>()) || |
| 1001 | (lhs_type->Is<core::type::Matrix>() && rhs_type->Is<core::type::Matrix>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1002 | // Matrices are transposed, so swap LHS and RHS. |
| 1003 | out << "mul("; |
| 1004 | if (!EmitExpression(out, expr->rhs)) { |
| 1005 | return false; |
| 1006 | } |
| 1007 | out << ", "; |
| 1008 | if (!EmitExpression(out, expr->lhs)) { |
| 1009 | return false; |
| 1010 | } |
| 1011 | out << ")"; |
| 1012 | |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1016 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (!EmitExpression(out, expr->lhs)) { |
| 1019 | return false; |
| 1020 | } |
| 1021 | out << " "; |
| 1022 | |
| 1023 | switch (expr->op) { |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1024 | case core::BinaryOp::kAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1025 | out << "&"; |
| 1026 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1027 | case core::BinaryOp::kOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1028 | out << "|"; |
| 1029 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1030 | case core::BinaryOp::kXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1031 | out << "^"; |
| 1032 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1033 | case core::BinaryOp::kLogicalAnd: |
| 1034 | case core::BinaryOp::kLogicalOr: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1035 | // These are both handled above. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1036 | TINT_UNREACHABLE(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1037 | } |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1038 | case core::BinaryOp::kEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1039 | out << "=="; |
| 1040 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1041 | case core::BinaryOp::kNotEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1042 | out << "!="; |
| 1043 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1044 | case core::BinaryOp::kLessThan: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1045 | out << "<"; |
| 1046 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1047 | case core::BinaryOp::kGreaterThan: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1048 | out << ">"; |
| 1049 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1050 | case core::BinaryOp::kLessThanEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1051 | out << "<="; |
| 1052 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1053 | case core::BinaryOp::kGreaterThanEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1054 | out << ">="; |
| 1055 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1056 | case core::BinaryOp::kShiftLeft: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1057 | out << "<<"; |
| 1058 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1059 | case core::BinaryOp::kShiftRight: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1060 | // TODO(dsinclair): MSL is based on C++14, and >> in C++14 has |
| 1061 | // implementation-defined behaviour for negative LHS. We may have to |
| 1062 | // generate extra code to implement WGSL-specified behaviour for negative |
| 1063 | // LHS. |
| 1064 | out << R"(>>)"; |
| 1065 | break; |
| 1066 | |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1067 | case core::BinaryOp::kAdd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1068 | out << "+"; |
| 1069 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1070 | case core::BinaryOp::kSubtract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1071 | out << "-"; |
| 1072 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1073 | case core::BinaryOp::kMultiply: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1074 | out << "*"; |
| 1075 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1076 | case core::BinaryOp::kDivide: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1077 | out << "/"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1078 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1079 | case core::BinaryOp::kModulo: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1080 | out << "%"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1081 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1082 | } |
| 1083 | out << " "; |
| 1084 | |
| 1085 | if (!EmitExpression(out, expr->rhs)) { |
| 1086 | return false; |
| 1087 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1088 | |
| 1089 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1092 | bool ASTPrinter::EmitStatements(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1093 | for (auto* s : stmts) { |
| 1094 | if (!EmitStatement(s)) { |
| 1095 | return false; |
| 1096 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1097 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1098 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1101 | bool ASTPrinter::EmitStatementsWithIndent(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1102 | ScopedIndent si(this); |
| 1103 | return EmitStatements(stmts); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1106 | bool ASTPrinter::EmitBlock(const ast::BlockStatement* stmt) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1107 | Line() << "{"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1108 | if (!EmitStatementsWithIndent(stmt->statements)) { |
| 1109 | return false; |
| 1110 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1111 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1112 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1115 | bool ASTPrinter::EmitBreak(const ast::BreakStatement*) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1116 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1117 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1120 | bool ASTPrinter::EmitBreakIf(const ast::BreakIfStatement* b) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1121 | auto out = Line(); |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 1122 | out << "if ("; |
| 1123 | if (!EmitExpression(out, b->condition)) { |
| 1124 | return false; |
| 1125 | } |
| 1126 | out << ") { break; }"; |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1130 | bool ASTPrinter::EmitCall(StringStream& out, const ast::CallExpression* expr) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 1131 | auto* call = builder_.Sem().Get<sem::Call>(expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1132 | auto* target = call->Target(); |
| 1133 | return Switch( |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1134 | target, // |
| 1135 | [&](const sem::Function* func) { return EmitFunctionCall(out, call, func); }, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 1136 | [&](const sem::BuiltinFn* builtin) { return EmitBuiltinCall(out, call, builtin); }, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1137 | [&](const sem::ValueConversion* conv) { return EmitValueConversion(out, call, conv); }, |
| 1138 | [&](const sem::ValueConstructor* ctor) { return EmitValueConstructor(out, call, ctor); }, |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 1139 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1142 | bool ASTPrinter::EmitFunctionCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1143 | const sem::Call* call, |
| 1144 | const sem::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1145 | auto* expr = call->Declaration(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1146 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1147 | if (ast::HasAttribute<CalculateArrayLength::BufferSizeIntrinsic>( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1148 | func->Declaration()->attributes)) { |
| 1149 | // Special function generated by the CalculateArrayLength transform for |
| 1150 | // calling X.GetDimensions(Y) |
| 1151 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1152 | return false; |
| 1153 | } |
| 1154 | out << ".GetDimensions("; |
| 1155 | if (!EmitExpression(out, call->Arguments()[1]->Declaration())) { |
| 1156 | return false; |
| 1157 | } |
| 1158 | out << ")"; |
| 1159 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1160 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1161 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1162 | if (auto* intrinsic = |
| 1163 | ast::GetAttribute<DecomposeMemoryAccess::Intrinsic>(func->Declaration()->attributes)) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 1164 | switch (intrinsic->address_space) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1165 | case core::AddressSpace::kUniform: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1166 | return EmitUniformBufferAccess(out, expr, intrinsic); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1167 | case core::AddressSpace::kStorage: |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1168 | if (!intrinsic->IsAtomic()) { |
| 1169 | return EmitStorageBufferAccess(out, expr, intrinsic); |
| 1170 | } |
| 1171 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1172 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1173 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic address space:" |
| 1174 | << intrinsic->address_space; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1175 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1176 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1177 | |
James Price | fe24b4a | 2023-08-08 01:40:43 +0000 | [diff] [blame] | 1178 | if (auto* wave_intrinsic = |
| 1179 | ast::GetAttribute<ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic>( |
| 1180 | func->Declaration()->attributes)) { |
| 1181 | switch (wave_intrinsic->op) { |
| 1182 | case ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic::Op::kWaveGetLaneCount: |
| 1183 | out << "WaveGetLaneCount()"; |
| 1184 | return true; |
| 1185 | case ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic::Op::kWaveGetLaneIndex: |
| 1186 | out << "WaveGetLaneIndex()"; |
| 1187 | return true; |
| 1188 | } |
| 1189 | } |
| 1190 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1191 | out << func->Declaration()->name->symbol.Name() << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1192 | |
| 1193 | bool first = true; |
| 1194 | for (auto* arg : call->Arguments()) { |
| 1195 | if (!first) { |
| 1196 | out << ", "; |
| 1197 | } |
| 1198 | first = false; |
| 1199 | |
| 1200 | if (!EmitExpression(out, arg->Declaration())) { |
| 1201 | return false; |
| 1202 | } |
| 1203 | } |
| 1204 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1205 | out << ")"; |
| 1206 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1209 | bool ASTPrinter::EmitBuiltinCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1210 | const sem::Call* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 1211 | const sem::BuiltinFn* builtin) { |
| 1212 | const auto type = builtin->Fn(); |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1213 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1214 | auto* expr = call->Declaration(); |
| 1215 | if (builtin->IsTexture()) { |
| 1216 | return EmitTextureCall(out, call, builtin); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1217 | } |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 1218 | if (type == wgsl::BuiltinFn::kBitcast) { |
| 1219 | return EmitBitcastCall(out, expr); |
| 1220 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1221 | if (type == wgsl::BuiltinFn::kSelect) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1222 | return EmitSelectCall(out, expr); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1223 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1224 | if (type == wgsl::BuiltinFn::kModf) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1225 | return EmitModfCall(out, expr, builtin); |
| 1226 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1227 | if (type == wgsl::BuiltinFn::kFrexp) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1228 | return EmitFrexpCall(out, expr, builtin); |
| 1229 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1230 | if (type == wgsl::BuiltinFn::kDegrees) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1231 | return EmitDegreesCall(out, expr, builtin); |
| 1232 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1233 | if (type == wgsl::BuiltinFn::kRadians) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1234 | return EmitRadiansCall(out, expr, builtin); |
| 1235 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1236 | if (type == wgsl::BuiltinFn::kSign) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 1237 | return EmitSignCall(out, call, builtin); |
| 1238 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1239 | if (type == wgsl::BuiltinFn::kQuantizeToF16) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 1240 | return EmitQuantizeToF16Call(out, expr, builtin); |
| 1241 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1242 | if (type == wgsl::BuiltinFn::kTrunc) { |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 1243 | return EmitTruncCall(out, expr, builtin); |
| 1244 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1245 | if (builtin->IsDataPacking()) { |
| 1246 | return EmitDataPackingCall(out, expr, builtin); |
| 1247 | } |
| 1248 | if (builtin->IsDataUnpacking()) { |
| 1249 | return EmitDataUnpackingCall(out, expr, builtin); |
| 1250 | } |
| 1251 | if (builtin->IsBarrier()) { |
| 1252 | return EmitBarrierCall(out, builtin); |
| 1253 | } |
| 1254 | if (builtin->IsAtomic()) { |
| 1255 | return EmitWorkgroupAtomicCall(out, expr, builtin); |
| 1256 | } |
Jiawei Shao | 53c0fa9 | 2023-12-08 01:24:00 +0000 | [diff] [blame] | 1257 | if (builtin->IsPacked4x8IntegerDotProductBuiltin()) { |
| 1258 | return EmitPacked4x8IntegerDotProductBuiltinCall(out, expr, builtin); |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 1259 | } |
Natalie Chouinard | dbefda9 | 2024-08-13 19:10:21 +0000 | [diff] [blame] | 1260 | if (type == wgsl::BuiltinFn::kSubgroupShuffleXor || |
| 1261 | type == wgsl::BuiltinFn::kSubgroupShuffleUp || |
| 1262 | type == wgsl::BuiltinFn::kSubgroupShuffleDown) { |
| 1263 | return EmitSubgroupShuffleBuiltinCall(out, expr, builtin); |
| 1264 | } |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1265 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1266 | auto name = generate_builtin_name(builtin); |
| 1267 | if (name.empty()) { |
| 1268 | return false; |
| 1269 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1270 | |
Natalie Chouinard | e9e450e | 2024-08-08 23:16:40 +0000 | [diff] [blame] | 1271 | // Handle single argument builtins that only accept and return uint (not int overload). |
| 1272 | // For count bits and reverse bits, we need to explicitly cast the return value (we also cast |
| 1273 | // the arg for good measure). See crbug.com/tint/1550. |
| 1274 | // For the following subgroup builtins, the lack of support may be a bug in DXC. See |
| 1275 | // github.com/microsoft/DirectXShaderCompiler/issues/6850. |
| 1276 | if (type == wgsl::BuiltinFn::kCountOneBits || type == wgsl::BuiltinFn::kReverseBits || |
| 1277 | type == wgsl::BuiltinFn::kSubgroupAnd || type == wgsl::BuiltinFn::kSubgroupOr || |
| 1278 | type == wgsl::BuiltinFn::kSubgroupXor) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1279 | auto* arg = call->Arguments()[0]; |
Natalie Chouinard | e9e450e | 2024-08-08 23:16:40 +0000 | [diff] [blame] | 1280 | auto* argType = arg->Type()->UnwrapRef(); |
| 1281 | if (argType->is_signed_integer_scalar_or_vector()) { |
| 1282 | // Bitcast of literal int vectors fails in DXC so extract arg to a var. See |
| 1283 | // github.com/microsoft/DirectXShaderCompiler/issues/6851. |
| 1284 | if (argType->is_signed_integer_vector() && |
| 1285 | arg->Stage() == core::EvaluationStage::kConstant) { |
| 1286 | auto varName = UniqueIdentifier(kTempNamePrefix); |
| 1287 | auto pre = Line(); |
| 1288 | if (!EmitTypeAndName(pre, argType, core::AddressSpace::kUndefined, |
| 1289 | core::Access::kUndefined, varName)) { |
| 1290 | return false; |
| 1291 | } |
| 1292 | pre << " = "; |
| 1293 | if (!EmitExpression(pre, arg->Declaration())) { |
| 1294 | return false; |
| 1295 | } |
| 1296 | pre << ";"; |
| 1297 | out << "asint(" << name << "(asuint(" << varName << ")))"; |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1301 | out << "asint(" << name << "(asuint("; |
| 1302 | if (!EmitExpression(out, arg->Declaration())) { |
| 1303 | return false; |
| 1304 | } |
| 1305 | out << ")))"; |
| 1306 | return true; |
| 1307 | } |
| 1308 | } |
| 1309 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1310 | out << name << "("; |
| 1311 | |
| 1312 | bool first = true; |
| 1313 | for (auto* arg : call->Arguments()) { |
| 1314 | if (!first) { |
| 1315 | out << ", "; |
| 1316 | } |
| 1317 | first = false; |
| 1318 | |
| 1319 | if (!EmitExpression(out, arg->Declaration())) { |
| 1320 | return false; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | out << ")"; |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1326 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1329 | bool ASTPrinter::EmitValueConversion(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1330 | const sem::Call* call, |
| 1331 | const sem::ValueConversion* conv) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1332 | if (!EmitType(out, conv->Target(), core::AddressSpace::kUndefined, core::Access::kReadWrite, |
| 1333 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1334 | return false; |
| 1335 | } |
| 1336 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1337 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1338 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1339 | return false; |
| 1340 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1341 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1342 | out << ")"; |
| 1343 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1346 | bool ASTPrinter::EmitValueConstructor(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1347 | const sem::Call* call, |
| 1348 | const sem::ValueConstructor* ctor) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1349 | auto* type = call->Type(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1350 | |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1351 | // If the value constructor arguments are empty then we need to construct with the zero value |
| 1352 | // for all components. |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1353 | if (call->Arguments().IsEmpty()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1354 | return EmitZeroValue(out, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1355 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1356 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1357 | // Single parameter matrix initializers must be identity initializer. |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1358 | // It could also be conversions between f16 and f32 matrix when f16 is properly supported. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1359 | if (type->Is<core::type::Matrix>() && call->Arguments().Length() == 1) { |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1360 | if (!ctor->Parameters()[0]->Type()->UnwrapRef()->is_float_matrix()) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1361 | TINT_UNREACHABLE() |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1362 | << "found a single-parameter matrix initializer that is not identity initializer"; |
Ben Clayton | 3b5edf1 | 2022-05-16 21:14:11 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1366 | bool brackets = type->IsAnyOf<core::type::Array, core::type::Struct>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1367 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1368 | // For single-value vector initializers, swizzle the scalar to the right |
| 1369 | // vector dimension using .x |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1370 | const bool is_single_value_vector_init = |
| 1371 | type->is_scalar_vector() && call->Arguments().Length() == 1 && |
| 1372 | ctor->Parameters()[0]->Type()->Is<core::type::Scalar>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1373 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 1374 | if (brackets) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1375 | out << "{"; |
| 1376 | } else { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1377 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1381 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1382 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1383 | if (is_single_value_vector_init) { |
| 1384 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1385 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1386 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1387 | bool first = true; |
| 1388 | for (auto* e : call->Arguments()) { |
| 1389 | if (!first) { |
| 1390 | out << ", "; |
| 1391 | } |
| 1392 | first = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1393 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1394 | if (!EmitExpression(out, e->Declaration())) { |
| 1395 | return false; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (is_single_value_vector_init) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1400 | out << ")." << std::string(type->As<core::type::Vector>()->Width(), 'x'); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | out << (brackets ? "}" : ")"); |
| 1404 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1407 | bool ASTPrinter::EmitUniformBufferAccess(StringStream& out, |
| 1408 | const ast::CallExpression* expr, |
| 1409 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1410 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1411 | auto* const offset = expr->args[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1412 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1413 | // offset in bytes |
| 1414 | uint32_t scalar_offset_bytes = 0; |
| 1415 | // offset in uint (4 bytes) |
| 1416 | uint32_t scalar_offset_index = 0; |
| 1417 | // expression to calculate offset in bytes |
| 1418 | std::string scalar_offset_bytes_expr; |
| 1419 | // expression to calculate offset in uint, by dividing scalar_offset_bytes_expr by 4 |
| 1420 | std::string scalar_offset_index_expr; |
| 1421 | // expression to calculate offset in uint, independently |
| 1422 | std::string scalar_offset_index_unified_expr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1423 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1424 | // If true, use scalar_offset_index, otherwise use scalar_offset_index_expr |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1425 | bool scalar_offset_constant = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1426 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1427 | if (auto* val = builder_.Sem().GetVal(offset)->ConstantValue()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1428 | TINT_ASSERT(val->Type()->Is<core::type::U32>()); |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 1429 | scalar_offset_bytes = static_cast<uint32_t>(val->ValueAs<AInt>()); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1430 | scalar_offset_index = scalar_offset_bytes / 4; // bytes -> scalar index |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1431 | scalar_offset_constant = true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1432 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1433 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1434 | // If true, scalar_offset_bytes or scalar_offset_bytes_expr should be used, otherwise only use |
| 1435 | // scalar_offset_index or scalar_offset_index_unified_expr. Currently only loading f16 scalar |
| 1436 | // require using offset in bytes. |
| 1437 | const bool need_offset_in_bytes = |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1438 | intrinsic->type == DecomposeMemoryAccess::Intrinsic::DataType::kF16; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1439 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1440 | if (!scalar_offset_constant) { |
| 1441 | // UBO offset not compile-time known. |
| 1442 | // Calculate the scalar offset into a temporary. |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1443 | if (need_offset_in_bytes) { |
| 1444 | scalar_offset_bytes_expr = UniqueIdentifier("scalar_offset_bytes"); |
| 1445 | scalar_offset_index_expr = UniqueIdentifier("scalar_offset_index"); |
| 1446 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1447 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1448 | pre << "const uint " << scalar_offset_bytes_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1449 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1450 | return false; |
| 1451 | } |
| 1452 | pre << ");"; |
| 1453 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1454 | Line() << "const uint " << scalar_offset_index_expr << " = " << scalar_offset_bytes_expr |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1455 | << " / 4;"; |
| 1456 | } else { |
| 1457 | scalar_offset_index_unified_expr = UniqueIdentifier("scalar_offset"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1458 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1459 | pre << "const uint " << scalar_offset_index_unified_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1460 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1461 | return false; |
| 1462 | } |
| 1463 | pre << ") / 4;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1464 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1465 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1466 | |
Austin Eng | a0e96b5 | 2023-02-18 00:39:01 +0000 | [diff] [blame] | 1467 | const char swizzle[] = {'x', 'y', 'z', 'w'}; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1468 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1469 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
| 1470 | using DataType = DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1471 | switch (intrinsic->op) { |
| 1472 | case Op::kLoad: { |
| 1473 | auto cast = [&](const char* to, auto&& load) { |
| 1474 | out << to << "("; |
| 1475 | auto result = load(); |
| 1476 | out << ")"; |
| 1477 | return result; |
| 1478 | }; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1479 | auto load_u32_to = [&](StringStream& target) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1480 | target << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1481 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1482 | target << "[" << (scalar_offset_index / 4) << "]." |
| 1483 | << swizzle[scalar_offset_index & 3]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1484 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1485 | target << "[" << scalar_offset_index_unified_expr << " / 4][" |
| 1486 | << scalar_offset_index_unified_expr << " % 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1487 | } |
| 1488 | return true; |
| 1489 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1490 | auto load_u32 = [&] { return load_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1491 | // Has a minimum alignment of 8 bytes, so is either .xy or .zw |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1492 | auto load_vec2_u32_to = [&](StringStream& target) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1493 | if (scalar_offset_constant) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1494 | target << buffer << "[" << (scalar_offset_index / 4) << "]" |
| 1495 | << ((scalar_offset_index & 2) == 0 ? ".xy" : ".zw"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1496 | } else { |
| 1497 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1498 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1499 | auto pre = Line(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1500 | pre << "uint4 " << ubo_load << " = " << buffer << "[" |
| 1501 | << scalar_offset_index_unified_expr << " / 4];"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1502 | } |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1503 | target << "((" << scalar_offset_index_unified_expr << " & 2) ? " << ubo_load |
| 1504 | << ".zw : " << ubo_load << ".xy)"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1505 | } |
| 1506 | return true; |
| 1507 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1508 | auto load_vec2_u32 = [&] { return load_vec2_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1509 | // vec4 has a minimum alignment of 16 bytes, easiest case |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1510 | auto load_vec4_u32 = [&] { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1511 | out << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1512 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1513 | out << "[" << (scalar_offset_index / 4) << "]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1514 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1515 | out << "[" << scalar_offset_index_unified_expr << " / 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1516 | } |
| 1517 | return true; |
| 1518 | }; |
| 1519 | // vec3 has a minimum alignment of 16 bytes, so is just a .xyz swizzle |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1520 | auto load_vec3_u32 = [&] { |
| 1521 | if (!load_vec4_u32()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1522 | return false; |
| 1523 | } |
| 1524 | out << ".xyz"; |
| 1525 | return true; |
| 1526 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1527 | auto load_scalar_f16 = [&] { |
| 1528 | // offset bytes = 4k, ((buffer[index].x) & 0xFFFF) |
| 1529 | // offset bytes = 4k+2, ((buffer[index].x >> 16) & 0xFFFF) |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1530 | out << "float16_t(f16tof32(((" << buffer; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1531 | if (scalar_offset_constant) { |
| 1532 | out << "[" << (scalar_offset_index / 4) << "]." |
| 1533 | << swizzle[scalar_offset_index & 3]; |
| 1534 | // WGSL spec ensure little endian memory layout. |
| 1535 | if (scalar_offset_bytes % 4 == 0) { |
| 1536 | out << ") & 0xFFFF)"; |
| 1537 | } else { |
| 1538 | out << " >> 16) & 0xFFFF)"; |
| 1539 | } |
| 1540 | } else { |
| 1541 | out << "[" << scalar_offset_index_expr << " / 4][" << scalar_offset_index_expr |
| 1542 | << " % 4] >> (" << scalar_offset_bytes_expr |
| 1543 | << " % 4 == 0 ? 0 : 16)) & 0xFFFF)"; |
| 1544 | } |
| 1545 | out << "))"; |
| 1546 | return true; |
| 1547 | }; |
| 1548 | auto load_vec2_f16 = [&] { |
| 1549 | // vec2<f16> is aligned to 4 bytes |
| 1550 | // Preclude code load the vec2<f16> data as a uint: |
| 1551 | // uint ubo_load = buffer[id0][id1]; |
| 1552 | // Loading code convert it to vec2<f16>: |
| 1553 | // vector<float16_t, 2>(float16_t(f16tof32(ubo_load & 0xFFFF)), |
| 1554 | // float16_t(f16tof32(ubo_load >> 16))) |
| 1555 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1556 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1557 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1558 | // Load the 4 bytes f16 vector as an uint |
| 1559 | pre << "uint " << ubo_load << " = "; |
| 1560 | if (!load_u32_to(pre)) { |
| 1561 | return false; |
| 1562 | } |
| 1563 | pre << ";"; |
| 1564 | } |
| 1565 | out << "vector<float16_t, 2>(float16_t(f16tof32(" << ubo_load |
| 1566 | << " & 0xFFFF)), float16_t(f16tof32(" << ubo_load << " >> 16)))"; |
| 1567 | return true; |
| 1568 | }; |
| 1569 | auto load_vec3_f16 = [&] { |
| 1570 | // vec3<f16> is aligned to 8 bytes |
| 1571 | // Preclude code load the vec3<f16> data as uint2 and convert its elements to |
| 1572 | // float16_t: |
| 1573 | // uint2 ubo_load = buffer[id0].xy; |
| 1574 | // /* The low 8 bits of two uint are the x and z elements of vec3<f16> */ |
| 1575 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1576 | // 0xFFFF)); |
| 1577 | // /* The high 8 bits of first uint is the y element of vec3<f16> */ |
| 1578 | // float16_t ubo_load_y = f16tof32(ubo_load[0] >> 16); |
| 1579 | // Loading code convert it to vec3<f16>: |
| 1580 | // vector<float16_t, 3>(ubo_load_xz[0], ubo_load_y, ubo_load_xz[1]) |
| 1581 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1582 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1583 | std::string ubo_load_y = UniqueIdentifier(ubo_load + "_y"); |
| 1584 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1585 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1586 | // Load the 8 bytes uint2 with the f16 vector at lower 6 bytes |
| 1587 | pre << "uint2 " << ubo_load << " = "; |
| 1588 | if (!load_vec2_u32_to(pre)) { |
| 1589 | return false; |
| 1590 | } |
| 1591 | pre << ";"; |
| 1592 | } |
| 1593 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1594 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1595 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1596 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1597 | } |
| 1598 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1599 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1600 | pre << "float16_t " << ubo_load_y << " = f16tof32(" << ubo_load |
| 1601 | << "[0] >> 16);"; |
| 1602 | } |
| 1603 | out << "vector<float16_t, 3>(" << ubo_load_xz << "[0], " << ubo_load_y << ", " |
| 1604 | << ubo_load_xz << "[1])"; |
| 1605 | return true; |
| 1606 | }; |
| 1607 | auto load_vec4_f16 = [&] { |
| 1608 | // vec4<f16> is aligned to 8 bytes |
| 1609 | // Preclude code load the vec4<f16> data as uint2 and convert its elements to |
| 1610 | // float16_t: |
| 1611 | // uint2 ubo_load = buffer[id0].xy; |
| 1612 | // /* The low 8 bits of two uint are the x and z elements of vec4<f16> */ |
| 1613 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1614 | // 0xFFFF)); |
| 1615 | // /* The high 8 bits of two uint are the y and w elements of vec4<f16> */ |
| 1616 | // vector<float16_t, 2> ubo_load_yw = vector<float16_t, 2>(f16tof32(ubo_load >> |
| 1617 | // 16)); |
| 1618 | // Loading code convert it to vec4<f16>: |
| 1619 | // vector<float16_t, 4>(ubo_load_xz[0], ubo_load_yw[0], ubo_load_xz[1], |
| 1620 | // ubo_load_yw[1]) |
| 1621 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1622 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1623 | std::string ubo_load_yw = UniqueIdentifier(ubo_load + "_yw"); |
| 1624 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1625 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1626 | // Load the 8 bytes f16 vector as an uint2 |
| 1627 | pre << "uint2 " << ubo_load << " = "; |
| 1628 | if (!load_vec2_u32_to(pre)) { |
| 1629 | return false; |
| 1630 | } |
| 1631 | pre << ";"; |
| 1632 | } |
| 1633 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1634 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1635 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1636 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1637 | } |
| 1638 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1639 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1640 | pre << "vector<float16_t, 2> " << ubo_load_yw |
| 1641 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " >> 16));"; |
| 1642 | } |
| 1643 | out << "vector<float16_t, 4>(" << ubo_load_xz << "[0], " << ubo_load_yw << "[0], " |
| 1644 | << ubo_load_xz << "[1], " << ubo_load_yw << "[1])"; |
| 1645 | return true; |
| 1646 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1647 | switch (intrinsic->type) { |
| 1648 | case DataType::kU32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1649 | return load_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1650 | case DataType::kF32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1651 | return cast("asfloat", load_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1652 | case DataType::kI32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1653 | return cast("asint", load_u32); |
| 1654 | case DataType::kF16: |
| 1655 | return load_scalar_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1656 | case DataType::kVec2U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1657 | return load_vec2_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1658 | case DataType::kVec2F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1659 | return cast("asfloat", load_vec2_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1660 | case DataType::kVec2I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1661 | return cast("asint", load_vec2_u32); |
| 1662 | case DataType::kVec2F16: |
| 1663 | return load_vec2_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1664 | case DataType::kVec3U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1665 | return load_vec3_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1666 | case DataType::kVec3F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1667 | return cast("asfloat", load_vec3_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1668 | case DataType::kVec3I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1669 | return cast("asint", load_vec3_u32); |
| 1670 | case DataType::kVec3F16: |
| 1671 | return load_vec3_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1672 | case DataType::kVec4U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1673 | return load_vec4_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1674 | case DataType::kVec4F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1675 | return cast("asfloat", load_vec4_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1676 | case DataType::kVec4I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1677 | return cast("asint", load_vec4_u32); |
| 1678 | case DataType::kVec4F16: |
| 1679 | return load_vec4_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1680 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1681 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1682 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1683 | } |
| 1684 | default: |
| 1685 | break; |
| 1686 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1687 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1688 | << static_cast<int>(intrinsic->op); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1691 | bool ASTPrinter::EmitStorageBufferAccess(StringStream& out, |
| 1692 | const ast::CallExpression* expr, |
| 1693 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1694 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1695 | auto* const offset = expr->args[0]; |
Ben Clayton | 407137a | 2023-06-28 21:02:32 +0000 | [diff] [blame] | 1696 | auto* const value = expr->args.Length() > 1 ? expr->args[1] : nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1697 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1698 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
| 1699 | using DataType = DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1700 | switch (intrinsic->op) { |
| 1701 | case Op::kLoad: { |
| 1702 | auto load = [&](const char* cast, int n) { |
| 1703 | if (cast) { |
| 1704 | out << cast << "("; |
| 1705 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1706 | out << buffer << ".Load"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1707 | if (n > 1) { |
| 1708 | out << n; |
| 1709 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1710 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1711 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1712 | return false; |
| 1713 | } |
| 1714 | if (cast) { |
| 1715 | out << ")"; |
| 1716 | } |
| 1717 | return true; |
| 1718 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1719 | // Templated load used for f16 types, requires SM6.2 or higher and DXC |
| 1720 | // Used by loading f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1721 | // to emit `buffer.Load<float16_t>(offset)`. |
| 1722 | auto templated_load = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1723 | out << buffer << ".Load<" << type << ">"; // templated load |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1724 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1725 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1726 | return false; |
| 1727 | } |
| 1728 | return true; |
| 1729 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1730 | switch (intrinsic->type) { |
| 1731 | case DataType::kU32: |
| 1732 | return load(nullptr, 1); |
| 1733 | case DataType::kF32: |
| 1734 | return load("asfloat", 1); |
| 1735 | case DataType::kI32: |
| 1736 | return load("asint", 1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1737 | case DataType::kF16: |
| 1738 | return templated_load("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1739 | case DataType::kVec2U32: |
| 1740 | return load(nullptr, 2); |
| 1741 | case DataType::kVec2F32: |
| 1742 | return load("asfloat", 2); |
| 1743 | case DataType::kVec2I32: |
| 1744 | return load("asint", 2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1745 | case DataType::kVec2F16: |
| 1746 | return templated_load("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1747 | case DataType::kVec3U32: |
| 1748 | return load(nullptr, 3); |
| 1749 | case DataType::kVec3F32: |
| 1750 | return load("asfloat", 3); |
| 1751 | case DataType::kVec3I32: |
| 1752 | return load("asint", 3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1753 | case DataType::kVec3F16: |
| 1754 | return templated_load("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1755 | case DataType::kVec4U32: |
| 1756 | return load(nullptr, 4); |
| 1757 | case DataType::kVec4F32: |
| 1758 | return load("asfloat", 4); |
| 1759 | case DataType::kVec4I32: |
| 1760 | return load("asint", 4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1761 | case DataType::kVec4F16: |
| 1762 | return templated_load("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1763 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1764 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1765 | << static_cast<int>(intrinsic->type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1766 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1767 | |
| 1768 | case Op::kStore: { |
| 1769 | auto store = [&](int n) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1770 | out << buffer << ".Store"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1771 | if (n > 1) { |
| 1772 | out << n; |
| 1773 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1774 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1775 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
| 1778 | out << ", asuint"; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1779 | ScopedParen sp2(out); |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 1780 | if (!EmitTextureOrStorageBufferCallArgExpression(out, value)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1781 | return false; |
| 1782 | } |
| 1783 | return true; |
| 1784 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1785 | // Templated stored used for f16 types, requires SM6.2 or higher and DXC |
| 1786 | // Used by storing f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1787 | // to emit `buffer.Store<float16_t>(offset)`. |
| 1788 | auto templated_store = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1789 | out << buffer << ".Store<" << type << ">"; // templated store |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1790 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1791 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1792 | return false; |
| 1793 | } |
| 1794 | out << ", "; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1795 | if (!EmitExpression(out, value)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1796 | return false; |
| 1797 | } |
| 1798 | return true; |
| 1799 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1800 | switch (intrinsic->type) { |
| 1801 | case DataType::kU32: |
| 1802 | return store(1); |
| 1803 | case DataType::kF32: |
| 1804 | return store(1); |
| 1805 | case DataType::kI32: |
| 1806 | return store(1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1807 | case DataType::kF16: |
| 1808 | return templated_store("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1809 | case DataType::kVec2U32: |
| 1810 | return store(2); |
| 1811 | case DataType::kVec2F32: |
| 1812 | return store(2); |
| 1813 | case DataType::kVec2I32: |
| 1814 | return store(2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1815 | case DataType::kVec2F16: |
| 1816 | return templated_store("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1817 | case DataType::kVec3U32: |
| 1818 | return store(3); |
| 1819 | case DataType::kVec3F32: |
| 1820 | return store(3); |
| 1821 | case DataType::kVec3I32: |
| 1822 | return store(3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1823 | case DataType::kVec3F16: |
| 1824 | return templated_store("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1825 | case DataType::kVec4U32: |
| 1826 | return store(4); |
| 1827 | case DataType::kVec4F32: |
| 1828 | return store(4); |
| 1829 | case DataType::kVec4I32: |
| 1830 | return store(4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1831 | case DataType::kVec4F16: |
| 1832 | return templated_store("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1833 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1834 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1835 | << static_cast<int>(intrinsic->type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1836 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1837 | default: |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1838 | // Break out to error case below |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1839 | // Note that atomic intrinsics are generated as functions. |
| 1840 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1843 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1844 | << static_cast<int>(intrinsic->op); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1847 | bool ASTPrinter::EmitStorageAtomicIntrinsic(const ast::Function* func, |
| 1848 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
| 1849 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1850 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1851 | const sem::Function* sem_func = builder_.Sem().Get(func); |
| 1852 | auto* result_ty = sem_func->ReturnType(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1853 | const auto name = func->name->symbol.Name(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1854 | auto& buf = *current_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1855 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1856 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1857 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1858 | auto rmw = [&](const char* hlsl) -> bool { |
| 1859 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1860 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1861 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1862 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1865 | fn << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1866 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1867 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1868 | return false; |
| 1869 | } |
| 1870 | fn << ") {"; |
| 1871 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1872 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1873 | buf.IncrementIndent(); |
| 1874 | TINT_DEFER({ |
| 1875 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1876 | Line(&buf) << "}"; |
| 1877 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1878 | }); |
| 1879 | |
| 1880 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1881 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1882 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 1883 | core::Access::kUndefined, "original_value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1884 | return false; |
| 1885 | } |
| 1886 | l << " = 0;"; |
| 1887 | } |
| 1888 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1889 | auto l = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1890 | l << buffer << "." << hlsl << "(offset, "; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1891 | if (intrinsic->op == Op::kAtomicSub) { |
| 1892 | l << "-"; |
| 1893 | } |
| 1894 | l << "value, original_value);"; |
| 1895 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1896 | Line(&buf) << "return original_value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1897 | return true; |
| 1898 | }; |
| 1899 | |
| 1900 | switch (intrinsic->op) { |
| 1901 | case Op::kAtomicAdd: |
| 1902 | return rmw("InterlockedAdd"); |
| 1903 | |
| 1904 | case Op::kAtomicSub: |
| 1905 | // Use add with the operand negated. |
| 1906 | return rmw("InterlockedAdd"); |
| 1907 | |
| 1908 | case Op::kAtomicMax: |
| 1909 | return rmw("InterlockedMax"); |
| 1910 | |
| 1911 | case Op::kAtomicMin: |
| 1912 | return rmw("InterlockedMin"); |
| 1913 | |
| 1914 | case Op::kAtomicAnd: |
| 1915 | return rmw("InterlockedAnd"); |
| 1916 | |
| 1917 | case Op::kAtomicOr: |
| 1918 | return rmw("InterlockedOr"); |
| 1919 | |
| 1920 | case Op::kAtomicXor: |
| 1921 | return rmw("InterlockedXor"); |
| 1922 | |
| 1923 | case Op::kAtomicExchange: |
| 1924 | return rmw("InterlockedExchange"); |
| 1925 | |
| 1926 | case Op::kAtomicLoad: { |
| 1927 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 1928 | // InterlockedOr using 0 as the OR value |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1929 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1930 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1931 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1932 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1933 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1934 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1935 | fn << "(uint offset) {"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | buf.IncrementIndent(); |
| 1939 | TINT_DEFER({ |
| 1940 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1941 | Line(&buf) << "}"; |
| 1942 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1943 | }); |
| 1944 | |
| 1945 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1946 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1947 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 1948 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1949 | return false; |
| 1950 | } |
| 1951 | l << " = 0;"; |
| 1952 | } |
| 1953 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1954 | Line(&buf) << buffer << ".InterlockedOr(offset, 0, value);"; |
| 1955 | Line(&buf) << "return value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1956 | return true; |
| 1957 | } |
| 1958 | case Op::kAtomicStore: { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1959 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1960 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 1961 | // InterlockedExchange and discard the returned value |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1962 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1963 | auto fn = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1964 | fn << "void " << name << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1965 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 1966 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1967 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1968 | } |
| 1969 | fn << ") {"; |
| 1970 | } |
| 1971 | |
| 1972 | buf.IncrementIndent(); |
| 1973 | TINT_DEFER({ |
| 1974 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1975 | Line(&buf) << "}"; |
| 1976 | Line(&buf); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1977 | }); |
| 1978 | |
| 1979 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1980 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1981 | if (!EmitTypeAndName(l, value_ty, core::AddressSpace::kUndefined, |
| 1982 | core::Access::kUndefined, "ignored")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1983 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1984 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1985 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1986 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1987 | Line(&buf) << buffer << ".InterlockedExchange(offset, value, ignored);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1988 | return true; |
| 1989 | } |
| 1990 | case Op::kAtomicCompareExchangeWeak: { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1991 | if (!EmitStructType(&helpers_, result_ty->As<core::type::Struct>())) { |
Ben Clayton | 4b8a3d3 | 2023-06-13 16:55:57 +0000 | [diff] [blame] | 1992 | return false; |
| 1993 | } |
| 1994 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1995 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1996 | // NOTE: We don't need to emit the return type struct here as DecomposeMemoryAccess |
| 1997 | // already added it to the AST, and it should have already been emitted by now. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1998 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1999 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2000 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 2001 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2002 | return false; |
| 2003 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 2004 | fn << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2005 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 2006 | core::Access::kUndefined, "compare")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2007 | return false; |
| 2008 | } |
| 2009 | fn << ", "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2010 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 2011 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2012 | return false; |
| 2013 | } |
| 2014 | fn << ") {"; |
| 2015 | } |
| 2016 | |
| 2017 | buf.IncrementIndent(); |
| 2018 | TINT_DEFER({ |
| 2019 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2020 | Line(&buf) << "}"; |
| 2021 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2022 | }); |
| 2023 | |
| 2024 | { // T result = {0}; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2025 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2026 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 2027 | core::Access::kUndefined, "result")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2028 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2029 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2030 | l << "="; |
| 2031 | if (!EmitZeroValue(l, result_ty)) { |
| 2032 | return false; |
| 2033 | } |
| 2034 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2035 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2036 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2037 | Line(&buf) << buffer |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 2038 | << ".InterlockedCompareExchange(offset, compare, value, result.old_value);"; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2039 | Line(&buf) << "result.exchanged = result.old_value == compare;"; |
| 2040 | Line(&buf) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2041 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2042 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2043 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2044 | default: |
| 2045 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2046 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2047 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2048 | TINT_UNREACHABLE() << "unsupported atomic DecomposeMemoryAccess::Intrinsic::Op: " |
| 2049 | << static_cast<int>(intrinsic->op); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2052 | bool ASTPrinter::EmitWorkgroupAtomicCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2053 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2054 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2055 | std::string result = UniqueIdentifier("atomic_result"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2056 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2057 | if (!builtin->ReturnType()->Is<core::type::Void>()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2058 | auto pre = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2059 | if (!EmitTypeAndName(pre, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2060 | core::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2061 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2062 | } |
| 2063 | pre << " = "; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2064 | if (!EmitZeroValue(pre, builtin->ReturnType())) { |
| 2065 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2066 | } |
| 2067 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2068 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2069 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2070 | auto call = [&](const char* name) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2071 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2072 | pre << name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2073 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2074 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2075 | ScopedParen sp(pre); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 2076 | for (size_t i = 0; i < expr->args.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2077 | auto* arg = expr->args[i]; |
| 2078 | if (i > 0) { |
| 2079 | pre << ", "; |
| 2080 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2081 | if (!EmitExpression(pre, arg)) { |
| 2082 | return false; |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | pre << ", " << result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2087 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2088 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2089 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2090 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2091 | out << result; |
| 2092 | return true; |
| 2093 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2094 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2095 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2096 | case wgsl::BuiltinFn::kAtomicLoad: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2097 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 2098 | // InterlockedOr using 0 as the OR value |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2099 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2100 | pre << "InterlockedOr"; |
| 2101 | { |
| 2102 | ScopedParen sp(pre); |
| 2103 | if (!EmitExpression(pre, expr->args[0])) { |
| 2104 | return false; |
| 2105 | } |
| 2106 | pre << ", 0, " << result; |
| 2107 | } |
| 2108 | pre << ";"; |
| 2109 | |
| 2110 | out << result; |
| 2111 | return true; |
| 2112 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2113 | case wgsl::BuiltinFn::kAtomicStore: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2114 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 2115 | // InterlockedExchange and discard the returned value |
| 2116 | { // T result = 0; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2117 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2118 | auto* value_ty = builtin->Parameters()[1]->Type()->UnwrapRef(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2119 | if (!EmitTypeAndName(pre, value_ty, core::AddressSpace::kUndefined, |
| 2120 | core::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2121 | return false; |
| 2122 | } |
| 2123 | pre << " = "; |
| 2124 | if (!EmitZeroValue(pre, value_ty)) { |
| 2125 | return false; |
| 2126 | } |
| 2127 | pre << ";"; |
| 2128 | } |
| 2129 | |
| 2130 | out << "InterlockedExchange"; |
| 2131 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2132 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2133 | if (!EmitExpression(out, expr->args[0])) { |
| 2134 | return false; |
| 2135 | } |
| 2136 | out << ", "; |
| 2137 | if (!EmitExpression(out, expr->args[1])) { |
| 2138 | return false; |
| 2139 | } |
| 2140 | out << ", " << result; |
| 2141 | } |
| 2142 | return true; |
| 2143 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2144 | case wgsl::BuiltinFn::kAtomicCompareExchangeWeak: { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2145 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2146 | return false; |
| 2147 | } |
| 2148 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2149 | auto* dest = expr->args[0]; |
| 2150 | auto* compare_value = expr->args[1]; |
| 2151 | auto* value = expr->args[2]; |
| 2152 | |
| 2153 | std::string compare = UniqueIdentifier("atomic_compare_value"); |
| 2154 | |
| 2155 | { // T compare_value = <compare_value>; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2156 | auto pre = Line(); |
Antonio Maiorano | f99671b | 2022-06-23 13:14:54 +0000 | [diff] [blame] | 2157 | if (!EmitTypeAndName(pre, TypeOf(compare_value)->UnwrapRef(), |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2158 | core::AddressSpace::kUndefined, core::Access::kUndefined, |
dan sinclair | 61c16eb | 2023-01-21 23:44:38 +0000 | [diff] [blame] | 2159 | compare)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2160 | return false; |
| 2161 | } |
| 2162 | pre << " = "; |
| 2163 | if (!EmitExpression(pre, compare_value)) { |
| 2164 | return false; |
| 2165 | } |
| 2166 | pre << ";"; |
| 2167 | } |
| 2168 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2169 | { // InterlockedCompareExchange(dst, compare, value, result.old_value); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2170 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2171 | pre << "InterlockedCompareExchange"; |
| 2172 | { |
| 2173 | ScopedParen sp(pre); |
| 2174 | if (!EmitExpression(pre, dest)) { |
| 2175 | return false; |
| 2176 | } |
| 2177 | pre << ", " << compare << ", "; |
| 2178 | if (!EmitExpression(pre, value)) { |
| 2179 | return false; |
| 2180 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2181 | pre << ", " << result << ".old_value"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2182 | } |
| 2183 | pre << ";"; |
| 2184 | } |
| 2185 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2186 | // result.exchanged = result.old_value == compare; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2187 | Line() << result << ".exchanged = " << result << ".old_value == " << compare << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2188 | |
| 2189 | out << result; |
| 2190 | return true; |
| 2191 | } |
| 2192 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2193 | case wgsl::BuiltinFn::kAtomicAdd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2194 | return call("InterlockedAdd"); |
| 2195 | |
Stephen White | 4249124 | 2023-10-18 18:02:20 +0000 | [diff] [blame] | 2196 | case wgsl::BuiltinFn::kAtomicSub: { |
| 2197 | auto pre = Line(); |
| 2198 | // Sub uses InterlockedAdd with the operand negated. |
| 2199 | pre << "InterlockedAdd"; |
| 2200 | { |
| 2201 | ScopedParen sp(pre); |
| 2202 | TINT_ASSERT(expr->args.Length() == 2); |
| 2203 | |
| 2204 | if (!EmitExpression(pre, expr->args[0])) { |
| 2205 | return false; |
| 2206 | } |
| 2207 | pre << ", -"; |
| 2208 | { |
| 2209 | ScopedParen argSP(pre); |
| 2210 | if (!EmitExpression(pre, expr->args[1])) { |
| 2211 | return false; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | pre << ", " << result; |
| 2216 | } |
| 2217 | |
| 2218 | pre << ";"; |
| 2219 | |
| 2220 | out << result; |
| 2221 | } |
| 2222 | return true; |
| 2223 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2224 | case wgsl::BuiltinFn::kAtomicMax: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2225 | return call("InterlockedMax"); |
| 2226 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2227 | case wgsl::BuiltinFn::kAtomicMin: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2228 | return call("InterlockedMin"); |
| 2229 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2230 | case wgsl::BuiltinFn::kAtomicAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2231 | return call("InterlockedAnd"); |
| 2232 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2233 | case wgsl::BuiltinFn::kAtomicOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2234 | return call("InterlockedOr"); |
| 2235 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2236 | case wgsl::BuiltinFn::kAtomicXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2237 | return call("InterlockedXor"); |
| 2238 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2239 | case wgsl::BuiltinFn::kAtomicExchange: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2240 | return call("InterlockedExchange"); |
| 2241 | |
| 2242 | default: |
| 2243 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2246 | TINT_UNREACHABLE() << "unsupported atomic builtin: " << builtin->Fn(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2249 | bool ASTPrinter::EmitSelectCall(StringStream& out, const ast::CallExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2250 | auto* expr_false = expr->args[0]; |
| 2251 | auto* expr_true = expr->args[1]; |
| 2252 | auto* expr_cond = expr->args[2]; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2253 | ScopedParen paren(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2254 | if (!EmitExpression(out, expr_cond)) { |
| 2255 | return false; |
| 2256 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2257 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2258 | out << " ? "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2259 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2260 | if (!EmitExpression(out, expr_true)) { |
| 2261 | return false; |
| 2262 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2263 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2264 | out << " : "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2265 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2266 | if (!EmitExpression(out, expr_false)) { |
| 2267 | return false; |
| 2268 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2269 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2270 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2273 | bool ASTPrinter::EmitModfCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2274 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2275 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2276 | return CallBuiltinHelper( |
| 2277 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2278 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2279 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2280 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2281 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2282 | if (auto* vec = ty->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2283 | width = std::to_string(vec->Width()); |
| 2284 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2285 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2286 | // Emit the builtin return type unique to this overload. This does not |
| 2287 | // exist in the AST, so it will not be generated in Generate(). |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2288 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2289 | return false; |
| 2290 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2291 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2292 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2293 | auto l = Line(b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2294 | if (!EmitType(l, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2295 | core::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2296 | return false; |
| 2297 | } |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2298 | l << " result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2299 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2300 | Line(b) << "result.fract = modf(" << params[0] << ", result.whole);"; |
| 2301 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2302 | return true; |
| 2303 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2306 | bool ASTPrinter::EmitFrexpCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2307 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2308 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2309 | return CallBuiltinHelper( |
| 2310 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2311 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2312 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2313 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2314 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2315 | if (auto* vec = ty->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2316 | width = std::to_string(vec->Width()); |
| 2317 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2318 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2319 | // Emit the builtin return type unique to this overload. This does not |
| 2320 | // exist in the AST, so it will not be generated in Generate(). |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2321 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2322 | return false; |
| 2323 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2324 | |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2325 | std::string member_type; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2326 | if (Is<core::type::F16>(ty->DeepestElement())) { |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2327 | member_type = width.empty() ? "float16_t" : ("vector<float16_t, " + width + ">"); |
| 2328 | } else { |
| 2329 | member_type = "float" + width; |
| 2330 | } |
| 2331 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2332 | Line(b) << member_type << " exp;"; |
| 2333 | Line(b) << member_type << " fract = sign(" << in << ") * frexp(" << in << ", exp);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2334 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2335 | auto l = Line(b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2336 | if (!EmitType(l, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2337 | core::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2338 | return false; |
| 2339 | } |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 2340 | l << " result = {fract, int" << width << "(exp)};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2341 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2342 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2343 | return true; |
| 2344 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2347 | bool ASTPrinter::EmitDegreesCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2348 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2349 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2350 | return CallBuiltinHelper(out, expr, builtin, |
| 2351 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2352 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2353 | << sem::kRadToDeg << ";"; |
| 2354 | return true; |
| 2355 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2358 | bool ASTPrinter::EmitRadiansCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2359 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2360 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2361 | return CallBuiltinHelper(out, expr, builtin, |
| 2362 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2363 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2364 | << sem::kDegToRad << ";"; |
| 2365 | return true; |
| 2366 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2369 | // The HLSL `sign` method always returns an `int` result (scalar or vector). In WGSL the result is |
| 2370 | // expected to be the same type as the argument. This injects a cast to the expected WGSL result |
| 2371 | // type after the call to `sign`. |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2372 | bool ASTPrinter::EmitSignCall(StringStream& out, const sem::Call* call, const sem::BuiltinFn*) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2373 | auto* arg = call->Arguments()[0]; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2374 | if (!EmitType(out, arg->Type(), core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2375 | return false; |
| 2376 | } |
| 2377 | out << "(sign("; |
| 2378 | if (!EmitExpression(out, arg->Declaration())) { |
| 2379 | return false; |
| 2380 | } |
| 2381 | out << "))"; |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2385 | bool ASTPrinter::EmitQuantizeToF16Call(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2386 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2387 | const sem::BuiltinFn* builtin) { |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2388 | // Cast to f16 and back |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2389 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2390 | if (auto* vec = builtin->ReturnType()->As<core::type::Vector>()) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2391 | width = std::to_string(vec->Width()); |
| 2392 | } |
dan sinclair | 260c1f0 | 2024-06-06 15:31:12 +0000 | [diff] [blame] | 2393 | out << "f16tof32(f32tof16("; |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2394 | if (!EmitExpression(out, expr->args[0])) { |
| 2395 | return false; |
| 2396 | } |
| 2397 | out << "))"; |
| 2398 | return true; |
| 2399 | } |
| 2400 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2401 | bool ASTPrinter::EmitTruncCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2402 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2403 | const sem::BuiltinFn* builtin) { |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2404 | // HLSL's trunc is broken for very large/small float values. |
| 2405 | // See crbug.com/tint/1883 |
| 2406 | return CallBuiltinHelper( // |
| 2407 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2408 | // value < 0 ? ceil(value) : floor(value) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2409 | Line(b) << "return " << params[0] << " < 0 ? ceil(" << params[0] << ") : floor(" |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2410 | << params[0] << ");"; |
| 2411 | return true; |
| 2412 | }); |
| 2413 | } |
| 2414 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2415 | bool ASTPrinter::EmitDataPackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2416 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2417 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2418 | return CallBuiltinHelper( |
| 2419 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2420 | uint32_t dims = 2; |
| 2421 | bool is_signed = false; |
| 2422 | uint32_t scale = 65535; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2423 | if (builtin->Fn() == wgsl::BuiltinFn::kPack4X8Snorm || |
| 2424 | builtin->Fn() == wgsl::BuiltinFn::kPack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2425 | dims = 4; |
| 2426 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2427 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2428 | if (builtin->Fn() == wgsl::BuiltinFn::kPack4X8Snorm || |
| 2429 | builtin->Fn() == wgsl::BuiltinFn::kPack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2430 | is_signed = true; |
| 2431 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2432 | } |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2433 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2434 | case wgsl::BuiltinFn::kPack4X8Snorm: |
| 2435 | case wgsl::BuiltinFn::kPack4X8Unorm: |
| 2436 | case wgsl::BuiltinFn::kPack2X16Snorm: |
| 2437 | case wgsl::BuiltinFn::kPack2X16Unorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2438 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2439 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2440 | l << (is_signed ? "" : "u") << "int" << dims |
| 2441 | << " i = " << (is_signed ? "" : "u") << "int" << dims << "(round(clamp(" |
| 2442 | << params[0] << ", " << (is_signed ? "-1.0" : "0.0") << ", 1.0) * " |
| 2443 | << scale << ".0))"; |
| 2444 | if (is_signed) { |
| 2445 | l << " & " << (dims == 4 ? "0xff" : "0xffff"); |
| 2446 | } |
| 2447 | l << ";"; |
| 2448 | } |
| 2449 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2450 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2451 | l << "return "; |
| 2452 | if (is_signed) { |
| 2453 | l << "asuint"; |
| 2454 | } |
| 2455 | l << "(i.x | i.y << " << (32 / dims); |
| 2456 | if (dims == 4) { |
| 2457 | l << " | i.z << 16 | i.w << 24"; |
| 2458 | } |
| 2459 | l << ");"; |
| 2460 | } |
| 2461 | break; |
| 2462 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2463 | case wgsl::BuiltinFn::kPack2X16Float: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2464 | Line(b) << "uint2 i = f32tof16(" << params[0] << ");"; |
| 2465 | Line(b) << "return i.x | (i.y << 16);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | } |
| 2468 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2469 | TINT_ICE() << " unhandled data packing builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2470 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2471 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2472 | return true; |
| 2473 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2476 | bool ASTPrinter::EmitDataUnpackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2477 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2478 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2479 | return CallBuiltinHelper( |
| 2480 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2481 | uint32_t dims = 2; |
| 2482 | bool is_signed = false; |
| 2483 | uint32_t scale = 65535; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2484 | if (builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Snorm || |
| 2485 | builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2486 | dims = 4; |
| 2487 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2488 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2489 | if (builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Snorm || |
| 2490 | builtin->Fn() == wgsl::BuiltinFn::kUnpack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2491 | is_signed = true; |
| 2492 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2493 | } |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2494 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2495 | case wgsl::BuiltinFn::kUnpack4X8Snorm: |
| 2496 | case wgsl::BuiltinFn::kUnpack2X16Snorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2497 | Line(b) << "int j = int(" << params[0] << ");"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2498 | { // Perform sign extension on the converted values. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2499 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2500 | l << "int" << dims << " i = int" << dims << "("; |
| 2501 | if (dims == 2) { |
| 2502 | l << "j << 16, j) >> 16"; |
| 2503 | } else { |
| 2504 | l << "j << 24, j << 16, j << 8, j) >> 24"; |
| 2505 | } |
| 2506 | l << ";"; |
| 2507 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2508 | Line(b) << "return clamp(float" << dims << "(i) / " << scale << ".0, " |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2509 | << (is_signed ? "-1.0" : "0.0") << ", 1.0);"; |
| 2510 | break; |
| 2511 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2512 | case wgsl::BuiltinFn::kUnpack4X8Unorm: |
| 2513 | case wgsl::BuiltinFn::kUnpack2X16Unorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2514 | Line(b) << "uint j = " << params[0] << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2515 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2516 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2517 | l << "uint" << dims << " i = uint" << dims << "("; |
| 2518 | l << "j & " << (dims == 2 ? "0xffff" : "0xff") << ", "; |
| 2519 | if (dims == 4) { |
| 2520 | l << "(j >> " << (32 / dims) << ") & 0xff, (j >> 16) & 0xff, j >> 24"; |
| 2521 | } else { |
| 2522 | l << "j >> " << (32 / dims); |
| 2523 | } |
| 2524 | l << ");"; |
| 2525 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2526 | Line(b) << "return float" << dims << "(i) / " << scale << ".0;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2527 | break; |
| 2528 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2529 | case wgsl::BuiltinFn::kUnpack2X16Float: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2530 | Line(b) << "uint i = " << params[0] << ";"; |
| 2531 | Line(b) << "return f16tof32(uint2(i & 0xffff, i >> 16));"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2532 | break; |
| 2533 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2534 | TINT_ICE() << "unhandled data packing builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2535 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2536 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2537 | return true; |
| 2538 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Jiawei Shao | 53c0fa9 | 2023-12-08 01:24:00 +0000 | [diff] [blame] | 2541 | bool ASTPrinter::EmitPacked4x8IntegerDotProductBuiltinCall(StringStream& out, |
| 2542 | const ast::CallExpression* expr, |
| 2543 | const sem::BuiltinFn* builtin) { |
Jiawei Shao | dd81723 | 2024-01-12 08:10:46 +0000 | [diff] [blame] | 2544 | switch (builtin->Fn()) { |
| 2545 | case wgsl::BuiltinFn::kDot4I8Packed: |
| 2546 | case wgsl::BuiltinFn::kDot4U8Packed: |
| 2547 | break; |
| 2548 | case wgsl::BuiltinFn::kPack4XI8: { |
| 2549 | out << "uint(pack_s8("; |
| 2550 | if (!EmitExpression(out, expr->args[0])) { |
| 2551 | return false; |
| 2552 | } |
| 2553 | out << "))"; |
| 2554 | return true; |
| 2555 | } |
| 2556 | case wgsl::BuiltinFn::kPack4XU8: { |
| 2557 | out << "uint(pack_u8("; |
| 2558 | if (!EmitExpression(out, expr->args[0])) { |
| 2559 | return false; |
| 2560 | } |
| 2561 | out << "))"; |
| 2562 | return true; |
| 2563 | } |
| 2564 | case wgsl::BuiltinFn::kPack4XI8Clamp: { |
| 2565 | out << "uint(pack_clamp_s8("; |
| 2566 | if (!EmitExpression(out, expr->args[0])) { |
| 2567 | return false; |
| 2568 | } |
| 2569 | out << "))"; |
| 2570 | return true; |
| 2571 | } |
| 2572 | case wgsl::BuiltinFn::kUnpack4XI8: { |
| 2573 | out << "unpack_s8s32(int8_t4_packed("; |
| 2574 | if (!EmitExpression(out, expr->args[0])) { |
| 2575 | return false; |
| 2576 | } |
| 2577 | out << "))"; |
| 2578 | return true; |
| 2579 | } |
| 2580 | case wgsl::BuiltinFn::kUnpack4XU8: { |
| 2581 | out << "unpack_u8u32(uint8_t4_packed("; |
| 2582 | if (!EmitExpression(out, expr->args[0])) { |
| 2583 | return false; |
| 2584 | } |
| 2585 | out << "))"; |
| 2586 | return true; |
| 2587 | } |
| 2588 | case wgsl::BuiltinFn::kPack4XU8Clamp: |
| 2589 | default: |
| 2590 | TINT_UNIMPLEMENTED() << builtin->Fn(); |
Jiawei Shao | dd81723 | 2024-01-12 08:10:46 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2593 | return CallBuiltinHelper(out, expr, builtin, |
| 2594 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2595 | std::string functionName; |
| 2596 | switch (builtin->Fn()) { |
| 2597 | case wgsl::BuiltinFn::kDot4I8Packed: |
| 2598 | Line(b) << "int accumulator = 0;"; |
| 2599 | functionName = "dot4add_i8packed"; |
| 2600 | break; |
| 2601 | case wgsl::BuiltinFn::kDot4U8Packed: |
| 2602 | Line(b) << "uint accumulator = 0u;"; |
| 2603 | functionName = "dot4add_u8packed"; |
| 2604 | break; |
| 2605 | default: |
| 2606 | TINT_ICE() << "Internal error: unhandled DP4a builtin"; |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2607 | } |
| 2608 | Line(b) << "return " << functionName << "(" << params[0] << ", " |
| 2609 | << params[1] << ", accumulator);"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2610 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2611 | return true; |
| 2612 | }); |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2615 | bool ASTPrinter::EmitBarrierCall(StringStream& out, const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2616 | // TODO(crbug.com/tint/661): Combine sequential barriers to a single |
| 2617 | // instruction. |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2618 | if (builtin->Fn() == wgsl::BuiltinFn::kWorkgroupBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2619 | out << "GroupMemoryBarrierWithGroupSync()"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2620 | } else if (builtin->Fn() == wgsl::BuiltinFn::kStorageBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2621 | out << "DeviceMemoryBarrierWithGroupSync()"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2622 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureBarrier) { |
James Price | a6287df | 2023-08-11 00:45:54 +0000 | [diff] [blame] | 2623 | out << "DeviceMemoryBarrierWithGroupSync()"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2624 | } else { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2625 | TINT_UNREACHABLE() << "unexpected barrier builtin type " << builtin->Fn(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2626 | } |
| 2627 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 2630 | bool ASTPrinter::EmitTextureOrStorageBufferCallArgExpression(StringStream& out, |
| 2631 | const ast::Expression* expr) { |
| 2632 | // TODO(crbug.com/tint/1976): Workaround DXC bug that fails to compile texture/storage function |
| 2633 | // calls with signed integer splatted constants. DXC fails to convert the coord arg, for e.g. |
| 2634 | // `0.xxx`, from a vector of 64-bit ints to a vector of 32-bit ints to match the texture load |
| 2635 | // parameter type. We work around this for now by explicitly casting the splatted constant to |
| 2636 | // the right type, for e.g. `int3(0.xxx)`. |
| 2637 | bool emitted_cast = false; |
| 2638 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
| 2639 | if (auto* constant = sem->ConstantValue()) { |
| 2640 | if (auto* splat = constant->As<core::constant::Splat>()) { |
| 2641 | if (splat->Type()->is_signed_integer_vector()) { |
| 2642 | if (!EmitType(out, constant->Type(), core::AddressSpace::kUndefined, |
| 2643 | core::Access::kUndefined, "")) { |
| 2644 | return false; |
| 2645 | } |
| 2646 | out << "("; |
| 2647 | emitted_cast = true; |
| 2648 | } |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | if (!EmitExpression(out, expr)) { |
| 2653 | return false; |
| 2654 | } |
| 2655 | if (emitted_cast) { |
| 2656 | out << ")"; |
| 2657 | } |
| 2658 | return true; |
| 2659 | } |
| 2660 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2661 | bool ASTPrinter::EmitTextureCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2662 | const sem::Call* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2663 | const sem::BuiltinFn* builtin) { |
Ben Clayton | 015fbe7 | 2023-08-09 07:46:44 +0000 | [diff] [blame] | 2664 | using Usage = core::ParameterUsage; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2665 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2666 | auto& signature = builtin->Signature(); |
| 2667 | auto* expr = call->Declaration(); |
| 2668 | auto arguments = expr->args; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2669 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2670 | // Returns the argument with the given usage |
| 2671 | auto arg = [&](Usage usage) { |
| 2672 | int idx = signature.IndexOf(usage); |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 2673 | return (idx >= 0) ? arguments[static_cast<size_t>(idx)] : nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2674 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2675 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2676 | auto* texture = arg(Usage::kTexture); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2677 | if (TINT_UNLIKELY(!texture)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2678 | TINT_ICE() << "missing texture argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2679 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2680 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2681 | auto* texture_type = TypeOf(texture)->UnwrapRef()->As<core::type::Texture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2682 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2683 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2684 | case wgsl::BuiltinFn::kTextureDimensions: |
| 2685 | case wgsl::BuiltinFn::kTextureNumLayers: |
| 2686 | case wgsl::BuiltinFn::kTextureNumLevels: |
| 2687 | case wgsl::BuiltinFn::kTextureNumSamples: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2688 | // All of these builtins use the GetDimensions() method on the texture |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2689 | bool is_ms = texture_type->IsAnyOf<core::type::MultisampledTexture, |
| 2690 | core::type::DepthMultisampledTexture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2691 | int num_dimensions = 0; |
| 2692 | std::string swizzle; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2693 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2694 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2695 | case wgsl::BuiltinFn::kTextureDimensions: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2696 | switch (texture_type->dim()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2697 | case core::type::TextureDimension::kNone: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2698 | TINT_ICE() << "texture dimension is kNone"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2699 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2700 | num_dimensions = 1; |
| 2701 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2702 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2703 | num_dimensions = is_ms ? 3 : 2; |
| 2704 | swizzle = is_ms ? ".xy" : ""; |
| 2705 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2706 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2707 | num_dimensions = is_ms ? 4 : 3; |
| 2708 | swizzle = ".xy"; |
| 2709 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2710 | case core::type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2711 | num_dimensions = 3; |
| 2712 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2713 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2714 | num_dimensions = 2; |
| 2715 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2716 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2717 | num_dimensions = 3; |
| 2718 | swizzle = ".xy"; |
| 2719 | break; |
| 2720 | } |
| 2721 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2722 | case wgsl::BuiltinFn::kTextureNumLayers: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2723 | switch (texture_type->dim()) { |
| 2724 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2725 | TINT_ICE() << "texture dimension is not arrayed"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2726 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2727 | num_dimensions = is_ms ? 4 : 3; |
| 2728 | swizzle = ".z"; |
| 2729 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2730 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2731 | num_dimensions = 3; |
| 2732 | swizzle = ".z"; |
| 2733 | break; |
| 2734 | } |
| 2735 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2736 | case wgsl::BuiltinFn::kTextureNumLevels: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2737 | switch (texture_type->dim()) { |
| 2738 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2739 | TINT_ICE() << "texture dimension does not support mips"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2740 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2741 | num_dimensions = 2; |
| 2742 | swizzle = ".y"; |
| 2743 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2744 | case core::type::TextureDimension::k2d: |
| 2745 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2746 | num_dimensions = 3; |
| 2747 | swizzle = ".z"; |
| 2748 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2749 | case core::type::TextureDimension::k2dArray: |
| 2750 | case core::type::TextureDimension::k3d: |
| 2751 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2752 | num_dimensions = 4; |
| 2753 | swizzle = ".w"; |
| 2754 | break; |
| 2755 | } |
| 2756 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2757 | case wgsl::BuiltinFn::kTextureNumSamples: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2758 | switch (texture_type->dim()) { |
| 2759 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2760 | TINT_ICE() << "texture dimension does not support multisampling"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2761 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2762 | num_dimensions = 3; |
| 2763 | swizzle = ".z"; |
| 2764 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2765 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2766 | num_dimensions = 4; |
| 2767 | swizzle = ".w"; |
| 2768 | break; |
| 2769 | } |
| 2770 | break; |
| 2771 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2772 | TINT_ICE() << "unexpected builtin"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2773 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2774 | |
| 2775 | auto* level_arg = arg(Usage::kLevel); |
| 2776 | |
| 2777 | if (level_arg) { |
| 2778 | // `NumberOfLevels` is a non-optional argument if `MipLevel` was passed. |
| 2779 | // Increment the number of dimensions for the temporary vector to |
| 2780 | // accommodate this. |
| 2781 | num_dimensions++; |
| 2782 | |
| 2783 | // If the swizzle was empty, the expression will evaluate to the whole |
| 2784 | // vector. As we've grown the vector by one element, we now need to |
| 2785 | // swizzle to keep the result expression equivalent. |
| 2786 | if (swizzle.empty()) { |
| 2787 | static constexpr const char* swizzles[] = {"", ".x", ".xy", ".xyz"}; |
| 2788 | swizzle = swizzles[num_dimensions - 1]; |
| 2789 | } |
| 2790 | } |
| 2791 | |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2792 | if (TINT_UNLIKELY(num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2793 | TINT_ICE() << "Texture query builtin temporary vector has " << num_dimensions |
| 2794 | << " dimensions"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | // Declare a variable to hold the queried texture info |
| 2798 | auto dims = UniqueIdentifier(kTempNamePrefix); |
| 2799 | if (num_dimensions == 1) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2800 | Line() << "uint " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2801 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2802 | Line() << "uint" << num_dimensions << " " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | { // texture.GetDimensions(...) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2806 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2807 | if (!EmitExpression(pre, texture)) { |
| 2808 | return false; |
| 2809 | } |
| 2810 | pre << ".GetDimensions("; |
| 2811 | |
| 2812 | if (level_arg) { |
| 2813 | if (!EmitExpression(pre, level_arg)) { |
| 2814 | return false; |
| 2815 | } |
| 2816 | pre << ", "; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2817 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureNumLevels) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2818 | pre << "0, "; |
| 2819 | } |
| 2820 | |
| 2821 | if (num_dimensions == 1) { |
| 2822 | pre << dims; |
| 2823 | } else { |
| 2824 | static constexpr char xyzw[] = {'x', 'y', 'z', 'w'}; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2825 | if (TINT_UNLIKELY(num_dimensions < 0 || num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2826 | TINT_ICE() << "vector dimensions are " << num_dimensions; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2827 | } |
| 2828 | for (int i = 0; i < num_dimensions; i++) { |
| 2829 | if (i > 0) { |
| 2830 | pre << ", "; |
| 2831 | } |
| 2832 | pre << dims << "." << xyzw[i]; |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | pre << ");"; |
| 2837 | } |
| 2838 | |
| 2839 | // The out parameters of the GetDimensions() call is now in temporary |
| 2840 | // `dims` variable. This may be packed with other data, so the final |
| 2841 | // expression may require a swizzle. |
| 2842 | out << dims << swizzle; |
| 2843 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2844 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2845 | default: |
| 2846 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2847 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2848 | |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2849 | if (!EmitExpression(out, texture)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2850 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2851 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2852 | |
| 2853 | // If pack_level_in_coords is true, then the mip level will be appended as the |
| 2854 | // last value of the coordinates argument. If the WGSL builtin overload does |
| 2855 | // not have a level parameter and pack_level_in_coords is true, then a zero |
| 2856 | // mip level will be inserted. |
| 2857 | bool pack_level_in_coords = false; |
| 2858 | |
| 2859 | uint32_t hlsl_ret_width = 4u; |
| 2860 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2861 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2862 | case wgsl::BuiltinFn::kTextureSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2863 | out << ".Sample("; |
| 2864 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2865 | case wgsl::BuiltinFn::kTextureSampleBias: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2866 | out << ".SampleBias("; |
| 2867 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2868 | case wgsl::BuiltinFn::kTextureSampleLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2869 | out << ".SampleLevel("; |
| 2870 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2871 | case wgsl::BuiltinFn::kTextureSampleGrad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2872 | out << ".SampleGrad("; |
| 2873 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2874 | case wgsl::BuiltinFn::kTextureSampleCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2875 | out << ".SampleCmp("; |
| 2876 | hlsl_ret_width = 1; |
| 2877 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2878 | case wgsl::BuiltinFn::kTextureSampleCompareLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2879 | out << ".SampleCmpLevelZero("; |
| 2880 | hlsl_ret_width = 1; |
| 2881 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2882 | case wgsl::BuiltinFn::kTextureLoad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2883 | out << ".Load("; |
Jiawei Shao | a54554a | 2023-11-13 08:24:12 +0000 | [diff] [blame] | 2884 | // Multisampled textures and read-write storage textures do not support mip-levels. |
| 2885 | if (texture_type->Is<core::type::MultisampledTexture>()) { |
| 2886 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2887 | } |
Jiawei Shao | a54554a | 2023-11-13 08:24:12 +0000 | [diff] [blame] | 2888 | if (auto* storage_texture_type = texture_type->As<core::type::StorageTexture>()) { |
| 2889 | if (storage_texture_type->access() == core::Access::kReadWrite) { |
| 2890 | break; |
| 2891 | } |
| 2892 | } |
| 2893 | pack_level_in_coords = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2894 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2895 | case wgsl::BuiltinFn::kTextureGather: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2896 | out << ".Gather"; |
Ben Clayton | 015fbe7 | 2023-08-09 07:46:44 +0000 | [diff] [blame] | 2897 | if (builtin->Parameters()[0]->Usage() == core::ParameterUsage::kComponent) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 2898 | switch (call->Arguments()[0]->ConstantValue()->ValueAs<AInt>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2899 | case 0: |
| 2900 | out << "Red"; |
| 2901 | break; |
| 2902 | case 1: |
| 2903 | out << "Green"; |
| 2904 | break; |
| 2905 | case 2: |
| 2906 | out << "Blue"; |
| 2907 | break; |
| 2908 | case 3: |
| 2909 | out << "Alpha"; |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | out << "("; |
| 2914 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2915 | case wgsl::BuiltinFn::kTextureGatherCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2916 | out << ".GatherCmp("; |
| 2917 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2918 | case wgsl::BuiltinFn::kTextureStore: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2919 | out << "["; |
| 2920 | break; |
| 2921 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2922 | TINT_ICE() << "Unhandled texture builtin '" << builtin << "'"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | if (auto* sampler = arg(Usage::kSampler)) { |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2926 | if (!EmitExpression(out, sampler)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2927 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2928 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2929 | out << ", "; |
| 2930 | } |
| 2931 | |
| 2932 | auto* param_coords = arg(Usage::kCoords); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2933 | if (TINT_UNLIKELY(!param_coords)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2934 | TINT_ICE() << "missing coords argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2938 | auto* i32 = builder_.create<core::type::I32>(); |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 2939 | auto* zero = builder_.Expr(0_i); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2940 | auto* stmt = builder_.Sem().Get(vector)->Stmt(); |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2941 | builder_.Sem().Add(zero, builder_.create<sem::ValueExpression>( |
Ben Clayton | 36c6155 | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 2942 | zero, i32, core::EvaluationStage::kRuntime, stmt, |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2943 | /* constant_value */ nullptr, |
| 2944 | /* has_side_effects */ false)); |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2945 | auto* packed = tint::wgsl::AppendVector(&builder_, vector, zero); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2946 | return EmitExpression(out, packed->Declaration()); |
| 2947 | }; |
| 2948 | |
| 2949 | auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { |
| 2950 | if (auto* level = arg(Usage::kLevel)) { |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2951 | auto* packed = tint::wgsl::AppendVector(&builder_, vector, level); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2952 | return EmitExpression(out, packed->Declaration()); |
| 2953 | } |
| 2954 | return emit_vector_appended_with_i32_zero(vector); |
| 2955 | }; |
| 2956 | |
| 2957 | if (auto* array_index = arg(Usage::kArrayIndex)) { |
| 2958 | // Array index needs to be appended to the coordinates. |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2959 | auto* packed = tint::wgsl::AppendVector(&builder_, param_coords, array_index); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2960 | if (pack_level_in_coords) { |
| 2961 | // Then mip level needs to be appended to the coordinates. |
| 2962 | if (!emit_vector_appended_with_level(packed->Declaration())) { |
| 2963 | return false; |
| 2964 | } |
| 2965 | } else { |
| 2966 | if (!EmitExpression(out, packed->Declaration())) { |
| 2967 | return false; |
| 2968 | } |
| 2969 | } |
| 2970 | } else if (pack_level_in_coords) { |
| 2971 | // Mip level needs to be appended to the coordinates. |
| 2972 | if (!emit_vector_appended_with_level(param_coords)) { |
| 2973 | return false; |
| 2974 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2975 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureStore) { |
Antonio Maiorano | b0dfccd | 2023-08-30 14:49:42 +0000 | [diff] [blame] | 2976 | // param_coords is an index expression, not a function arg |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2977 | if (!EmitExpression(out, param_coords)) { |
| 2978 | return false; |
| 2979 | } |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 2980 | } else if (!EmitTextureOrStorageBufferCallArgExpression(out, param_coords)) { |
Antonio Maiorano | b0dfccd | 2023-08-30 14:49:42 +0000 | [diff] [blame] | 2981 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2982 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2983 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2984 | for (auto usage : {Usage::kDepthRef, Usage::kBias, Usage::kLevel, Usage::kDdx, Usage::kDdy, |
| 2985 | Usage::kSampleIndex, Usage::kOffset}) { |
| 2986 | if (usage == Usage::kLevel && pack_level_in_coords) { |
| 2987 | continue; // mip level already packed in coordinates. |
| 2988 | } |
| 2989 | if (auto* e = arg(usage)) { |
| 2990 | out << ", "; |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 2991 | if (!EmitTextureOrStorageBufferCallArgExpression(out, e)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2992 | return false; |
| 2993 | } |
| 2994 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2995 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2996 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2997 | if (builtin->Fn() == wgsl::BuiltinFn::kTextureStore) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2998 | out << "] = "; |
| 2999 | if (!EmitExpression(out, arg(Usage::kValue))) { |
| 3000 | return false; |
| 3001 | } |
| 3002 | } else { |
| 3003 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3004 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3005 | // If the builtin return type does not match the number of elements of the |
| 3006 | // HLSL builtin, we need to swizzle the expression to generate the correct |
| 3007 | // number of components. |
| 3008 | uint32_t wgsl_ret_width = 1; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3009 | if (auto* vec = builtin->ReturnType()->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3010 | wgsl_ret_width = vec->Width(); |
| 3011 | } |
| 3012 | if (wgsl_ret_width < hlsl_ret_width) { |
| 3013 | out << "."; |
| 3014 | for (uint32_t i = 0; i < wgsl_ret_width; i++) { |
| 3015 | out << "xyz"[i]; |
| 3016 | } |
| 3017 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 3018 | if (TINT_UNLIKELY(wgsl_ret_width > hlsl_ret_width)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3019 | TINT_ICE() << "WGSL return width (" << wgsl_ret_width |
| 3020 | << ") is wider than HLSL return width (" << hlsl_ret_width << ") for " |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 3021 | << builtin->Fn(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3022 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3023 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3024 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3025 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Natalie Chouinard | dbefda9 | 2024-08-13 19:10:21 +0000 | [diff] [blame] | 3028 | // The following subgroup builtin functions are translated to HLSL as follows: |
| 3029 | // +---------------------+----------------------------------------------------------------+ |
| 3030 | // | WGSL | HLSL | |
| 3031 | // +---------------------+----------------------------------------------------------------+ |
| 3032 | // | subgroupShuffleXor | WaveReadLaneAt with index equal subgroup_invocation_id ^ mask | |
| 3033 | // | subgroupShuffleUp | WaveReadLaneAt with index equal subgroup_invocation_id - delta | |
| 3034 | // | subgroupShuffleDown | WaveReadLaneAt with index equal subgroup_invocation_id + delta | |
| 3035 | // +---------------------+----------------------------------------------------------------+ |
| 3036 | bool ASTPrinter::EmitSubgroupShuffleBuiltinCall(StringStream& out, |
| 3037 | const ast::CallExpression* expr, |
| 3038 | const sem::BuiltinFn* builtin) { |
| 3039 | out << "WaveReadLaneAt("; |
| 3040 | |
| 3041 | if (!EmitExpression(out, expr->args[0])) { |
| 3042 | return false; |
| 3043 | } |
| 3044 | |
| 3045 | out << ", (WaveGetLaneIndex() "; |
| 3046 | |
| 3047 | switch (builtin->Fn()) { |
| 3048 | case wgsl::BuiltinFn::kSubgroupShuffleXor: |
| 3049 | out << "^ "; |
| 3050 | break; |
| 3051 | case wgsl::BuiltinFn::kSubgroupShuffleUp: |
| 3052 | out << "- "; |
| 3053 | break; |
| 3054 | case wgsl::BuiltinFn::kSubgroupShuffleDown: |
| 3055 | out << "+ "; |
| 3056 | break; |
| 3057 | default: |
| 3058 | TINT_UNREACHABLE(); |
| 3059 | } |
| 3060 | |
| 3061 | if (!EmitExpression(out, expr->args[1])) { |
| 3062 | return false; |
| 3063 | } |
| 3064 | out << "))"; |
| 3065 | |
| 3066 | return true; |
| 3067 | } |
| 3068 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 3069 | std::string ASTPrinter::generate_builtin_name(const sem::BuiltinFn* builtin) { |
| 3070 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3071 | case wgsl::BuiltinFn::kAbs: |
| 3072 | case wgsl::BuiltinFn::kAcos: |
| 3073 | case wgsl::BuiltinFn::kAll: |
| 3074 | case wgsl::BuiltinFn::kAny: |
| 3075 | case wgsl::BuiltinFn::kAsin: |
| 3076 | case wgsl::BuiltinFn::kAtan: |
| 3077 | case wgsl::BuiltinFn::kAtan2: |
| 3078 | case wgsl::BuiltinFn::kCeil: |
| 3079 | case wgsl::BuiltinFn::kClamp: |
| 3080 | case wgsl::BuiltinFn::kCos: |
| 3081 | case wgsl::BuiltinFn::kCosh: |
| 3082 | case wgsl::BuiltinFn::kCross: |
| 3083 | case wgsl::BuiltinFn::kDeterminant: |
| 3084 | case wgsl::BuiltinFn::kDistance: |
| 3085 | case wgsl::BuiltinFn::kDot: |
| 3086 | case wgsl::BuiltinFn::kExp: |
| 3087 | case wgsl::BuiltinFn::kExp2: |
| 3088 | case wgsl::BuiltinFn::kFloor: |
| 3089 | case wgsl::BuiltinFn::kFrexp: |
| 3090 | case wgsl::BuiltinFn::kLdexp: |
| 3091 | case wgsl::BuiltinFn::kLength: |
| 3092 | case wgsl::BuiltinFn::kLog: |
| 3093 | case wgsl::BuiltinFn::kLog2: |
| 3094 | case wgsl::BuiltinFn::kMax: |
| 3095 | case wgsl::BuiltinFn::kMin: |
| 3096 | case wgsl::BuiltinFn::kModf: |
| 3097 | case wgsl::BuiltinFn::kNormalize: |
| 3098 | case wgsl::BuiltinFn::kPow: |
| 3099 | case wgsl::BuiltinFn::kReflect: |
| 3100 | case wgsl::BuiltinFn::kRefract: |
| 3101 | case wgsl::BuiltinFn::kRound: |
| 3102 | case wgsl::BuiltinFn::kSaturate: |
| 3103 | case wgsl::BuiltinFn::kSin: |
| 3104 | case wgsl::BuiltinFn::kSinh: |
| 3105 | case wgsl::BuiltinFn::kSqrt: |
| 3106 | case wgsl::BuiltinFn::kStep: |
| 3107 | case wgsl::BuiltinFn::kTan: |
| 3108 | case wgsl::BuiltinFn::kTanh: |
| 3109 | case wgsl::BuiltinFn::kTranspose: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3110 | return builtin->str(); |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3111 | case wgsl::BuiltinFn::kCountOneBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3112 | return "countbits"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3113 | case wgsl::BuiltinFn::kDpdx: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3114 | return "ddx"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3115 | case wgsl::BuiltinFn::kDpdxCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3116 | return "ddx_coarse"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3117 | case wgsl::BuiltinFn::kDpdxFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3118 | return "ddx_fine"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3119 | case wgsl::BuiltinFn::kDpdy: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3120 | return "ddy"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3121 | case wgsl::BuiltinFn::kDpdyCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3122 | return "ddy_coarse"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3123 | case wgsl::BuiltinFn::kDpdyFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3124 | return "ddy_fine"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3125 | case wgsl::BuiltinFn::kFaceForward: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3126 | return "faceforward"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3127 | case wgsl::BuiltinFn::kFract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3128 | return "frac"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3129 | case wgsl::BuiltinFn::kFma: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3130 | return "mad"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3131 | case wgsl::BuiltinFn::kFwidth: |
| 3132 | case wgsl::BuiltinFn::kFwidthCoarse: |
| 3133 | case wgsl::BuiltinFn::kFwidthFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3134 | return "fwidth"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3135 | case wgsl::BuiltinFn::kInverseSqrt: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3136 | return "rsqrt"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3137 | case wgsl::BuiltinFn::kMix: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3138 | return "lerp"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3139 | case wgsl::BuiltinFn::kReverseBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3140 | return "reversebits"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3141 | case wgsl::BuiltinFn::kSmoothstep: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3142 | return "smoothstep"; |
James Price | b2b6ade | 2024-07-18 18:06:30 +0000 | [diff] [blame] | 3143 | case wgsl::BuiltinFn::kSubgroupBallot: |
| 3144 | return "WaveActiveBallot"; |
Natalie Chouinard | f15cc07 | 2024-08-09 20:50:15 +0000 | [diff] [blame] | 3145 | case wgsl::BuiltinFn::kSubgroupElect: |
| 3146 | return "WaveIsFirstLane"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3147 | case wgsl::BuiltinFn::kSubgroupBroadcast: |
David Neto | eea786f | 2023-09-21 05:32:53 +0000 | [diff] [blame] | 3148 | return "WaveReadLaneAt"; |
Natalie Chouinard | f15cc07 | 2024-08-09 20:50:15 +0000 | [diff] [blame] | 3149 | case wgsl::BuiltinFn::kSubgroupBroadcastFirst: |
| 3150 | return "WaveReadLaneFirst"; |
Natalie Chouinard | b862299 | 2024-08-12 15:44:48 +0000 | [diff] [blame] | 3151 | case wgsl::BuiltinFn::kSubgroupShuffle: |
| 3152 | return "WaveReadLaneAt"; |
Natalie Chouinard | 06e23ab | 2024-08-07 18:07:27 +0000 | [diff] [blame] | 3153 | case wgsl::BuiltinFn::kSubgroupAdd: |
| 3154 | return "WaveActiveSum"; |
| 3155 | case wgsl::BuiltinFn::kSubgroupExclusiveAdd: |
| 3156 | return "WavePrefixSum"; |
| 3157 | case wgsl::BuiltinFn::kSubgroupMul: |
| 3158 | return "WaveActiveProduct"; |
| 3159 | case wgsl::BuiltinFn::kSubgroupExclusiveMul: |
| 3160 | return "WavePrefixProduct"; |
Natalie Chouinard | e9e450e | 2024-08-08 23:16:40 +0000 | [diff] [blame] | 3161 | case wgsl::BuiltinFn::kSubgroupAnd: |
| 3162 | return "WaveActiveBitAnd"; |
| 3163 | case wgsl::BuiltinFn::kSubgroupOr: |
| 3164 | return "WaveActiveBitOr"; |
| 3165 | case wgsl::BuiltinFn::kSubgroupXor: |
| 3166 | return "WaveActiveBitXor"; |
Natalie Chouinard | 7be306a | 2024-08-09 00:48:25 +0000 | [diff] [blame] | 3167 | case wgsl::BuiltinFn::kSubgroupMin: |
| 3168 | return "WaveActiveMin"; |
| 3169 | case wgsl::BuiltinFn::kSubgroupMax: |
| 3170 | return "WaveActiveMax"; |
Natalie Chouinard | 9562469 | 2024-08-09 19:03:38 +0000 | [diff] [blame] | 3171 | case wgsl::BuiltinFn::kSubgroupAll: |
| 3172 | return "WaveActiveAllTrue"; |
| 3173 | case wgsl::BuiltinFn::kSubgroupAny: |
| 3174 | return "WaveActiveAnyTrue"; |
Natalie Chouinard | b7114a6 | 2024-08-14 16:10:59 +0000 | [diff] [blame] | 3175 | case wgsl::BuiltinFn::kQuadBroadcast: |
| 3176 | return "QuadReadLaneAt"; |
Natalie Chouinard | e31fa9d | 2024-08-14 20:52:07 +0000 | [diff] [blame] | 3177 | case wgsl::BuiltinFn::kQuadSwapX: |
| 3178 | return "QuadReadAcrossX"; |
| 3179 | case wgsl::BuiltinFn::kQuadSwapY: |
| 3180 | return "QuadReadAcrossY"; |
| 3181 | case wgsl::BuiltinFn::kQuadSwapDiagonal: |
| 3182 | return "QuadReadAcrossDiagonal"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3183 | default: |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3184 | diagnostics_.AddError(Source{}) << "Unknown builtin method: " << builtin->str(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3185 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3186 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3187 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3190 | bool ASTPrinter::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3191 | auto* stmt = s->body[case_idx]; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3192 | auto* sem = builder_.Sem().Get<sem::CaseStatement>(stmt); |
| 3193 | for (auto* selector : sem->Selectors()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3194 | auto out = Line(); |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3195 | if (selector->IsDefault()) { |
| 3196 | out << "default"; |
| 3197 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3198 | out << "case "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3199 | if (!EmitConstant(out, selector->Value(), /* is_variable_initializer */ false)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3200 | return false; |
| 3201 | } |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3202 | } |
| 3203 | out << ":"; |
| 3204 | if (selector == sem->Selectors().back()) { |
| 3205 | out << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3209 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3210 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3211 | DecrementIndent(); |
| 3212 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3213 | }); |
| 3214 | |
| 3215 | // Emit the case statement |
| 3216 | if (!EmitStatements(stmt->body->statements)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3217 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3218 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3219 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3220 | if (!tint::IsAnyOf<ast::BreakStatement>(stmt->body->Last())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3221 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3222 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3223 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3224 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3227 | bool ASTPrinter::EmitContinue(const ast::ContinueStatement*) { |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 3228 | if (!emit_continuing_ || !emit_continuing_()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3229 | return false; |
| 3230 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3231 | Line() << "continue;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3232 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3235 | bool ASTPrinter::EmitDiscard(const ast::DiscardStatement*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3236 | // TODO(dsinclair): Verify this is correct when the discard semantics are |
| 3237 | // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3238 | Line() << "discard;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3239 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3242 | bool ASTPrinter::EmitExpression(StringStream& out, const ast::Expression* expr) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 3243 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3244 | if (auto* constant = sem->ConstantValue()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3245 | bool is_variable_initializer = false; |
| 3246 | if (auto* stmt = sem->Stmt()) { |
| 3247 | if (auto* decl = As<ast::VariableDeclStatement>(stmt->Declaration())) { |
| 3248 | is_variable_initializer = decl->variable->initializer == expr; |
| 3249 | } |
| 3250 | } |
| 3251 | return EmitConstant(out, constant, is_variable_initializer); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3254 | return Switch( |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 3255 | expr, // |
| 3256 | [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, |
| 3257 | [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 3258 | [&](const ast::CallExpression* c) { return EmitCall(out, c); }, |
| 3259 | [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, |
| 3260 | [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, |
| 3261 | [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3262 | [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, // |
| 3263 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3266 | bool ASTPrinter::EmitIdentifier(StringStream& out, const ast::IdentifierExpression* expr) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3267 | out << expr->identifier->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3268 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3271 | bool ASTPrinter::EmitIf(const ast::IfStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3272 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3273 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3274 | out << "if ("; |
| 3275 | if (!EmitExpression(out, stmt->condition)) { |
| 3276 | return false; |
| 3277 | } |
| 3278 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3279 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3280 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3281 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
James Price | 26ebe5e | 2022-04-29 00:14:53 +0000 | [diff] [blame] | 3282 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3283 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3284 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3285 | if (stmt->else_statement) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3286 | Line() << "} else {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3287 | if (auto* block = stmt->else_statement->As<ast::BlockStatement>()) { |
| 3288 | if (!EmitStatementsWithIndent(block->statements)) { |
| 3289 | return false; |
| 3290 | } |
| 3291 | } else { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3292 | if (!EmitStatementsWithIndent(Vector{stmt->else_statement})) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3293 | return false; |
| 3294 | } |
| 3295 | } |
| 3296 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3297 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3298 | |
| 3299 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3302 | bool ASTPrinter::EmitFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3303 | auto* sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3304 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3305 | // Emit storage atomic helpers |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 3306 | if (auto* intrinsic = ast::GetAttribute<DecomposeMemoryAccess::Intrinsic>(func->attributes)) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3307 | if (intrinsic->address_space == core::AddressSpace::kStorage && intrinsic->IsAtomic()) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3308 | if (!EmitStorageAtomicIntrinsic(func, intrinsic)) { |
| 3309 | return false; |
| 3310 | } |
| 3311 | } |
| 3312 | return true; |
| 3313 | } |
| 3314 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3315 | if (ast::HasAttribute<ast::InternalAttribute>(func->attributes)) { |
| 3316 | // An internal function. Do not emit. |
| 3317 | return true; |
| 3318 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3319 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3320 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3321 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3322 | auto name = func->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3323 | // If the function returns an array, then we need to declare a typedef for |
| 3324 | // this. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3325 | if (sem->ReturnType()->Is<core::type::Array>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3326 | auto typedef_name = UniqueIdentifier(name + "_ret"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3327 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3328 | pre << "typedef "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3329 | if (!EmitTypeAndName(pre, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3330 | core::Access::kReadWrite, typedef_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3331 | return false; |
| 3332 | } |
| 3333 | pre << ";"; |
| 3334 | out << typedef_name; |
| 3335 | } else { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3336 | if (!EmitType(out, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3337 | core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3338 | return false; |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | out << " " << name << "("; |
| 3343 | |
| 3344 | bool first = true; |
| 3345 | |
| 3346 | for (auto* v : sem->Parameters()) { |
| 3347 | if (!first) { |
| 3348 | out << ", "; |
| 3349 | } |
| 3350 | first = false; |
| 3351 | |
| 3352 | auto const* type = v->Type(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3353 | auto address_space = core::AddressSpace::kUndefined; |
| 3354 | auto access = core::Access::kUndefined; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3355 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3356 | if (auto* ptr = type->As<core::type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3357 | type = ptr->StoreType(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3358 | switch (ptr->AddressSpace()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3359 | case core::AddressSpace::kStorage: |
| 3360 | case core::AddressSpace::kUniform: |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3361 | // Not allowed by WGSL, but is used by certain transforms (e.g. DMA) to pass |
| 3362 | // storage buffers and uniform buffers down into transform-generated |
| 3363 | // functions. In this situation we want to generate the parameter without an |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3364 | // 'inout', using the address space and access from the pointer. |
| 3365 | address_space = ptr->AddressSpace(); |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3366 | access = ptr->Access(); |
| 3367 | break; |
| 3368 | default: |
| 3369 | // Transform regular WGSL pointer parameters in to `inout` parameters. |
| 3370 | out << "inout "; |
| 3371 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3374 | // Note: WGSL only allows for AddressSpace::kUndefined on parameters, however |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3375 | // the sanitizer transforms generates load / store functions for storage |
| 3376 | // or uniform buffers. These functions have a buffer parameter with |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3377 | // AddressSpace::kStorage or AddressSpace::kUniform. This is required to |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3378 | // correctly translate the parameter to a [RW]ByteAddressBuffer for |
| 3379 | // storage buffers and a uint4[N] for uniform buffers. |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3380 | if (!EmitTypeAndName(out, type, address_space, access, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3381 | v->Declaration()->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3382 | return false; |
| 3383 | } |
| 3384 | } |
| 3385 | out << ") {"; |
| 3386 | } |
| 3387 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3388 | if (sem->DiscardStatement() && !sem->ReturnType()->Is<core::type::Void>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3389 | // BUG(crbug.com/tint/1081): work around non-void functions with discard |
| 3390 | // failing compilation sometimes |
| 3391 | if (!EmitFunctionBodyWithDiscard(func)) { |
| 3392 | return false; |
| 3393 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3394 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3395 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3396 | return false; |
| 3397 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3400 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3401 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3402 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3405 | bool ASTPrinter::EmitFunctionBodyWithDiscard(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3406 | // FXC sometimes fails to compile functions that discard with 'Not all control |
| 3407 | // paths return a value'. We work around this by wrapping the function body |
| 3408 | // within an "if (true) { <body> } return <default return type obj>;" so that |
| 3409 | // there is always an (unused) return statement. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3410 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3411 | auto* sem = builder_.Sem().Get(func); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3412 | TINT_ASSERT(sem->DiscardStatement() && !sem->ReturnType()->Is<core::type::Void>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3413 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3414 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3415 | Line() << "if (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3416 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3417 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3418 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3419 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3420 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3421 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3422 | |
| 3423 | // Return an unused result that matches the type of the return value |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3424 | auto name = builder_.Symbols().New("unused").Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3425 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3426 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3427 | if (!EmitTypeAndName(out, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3428 | core::Access::kReadWrite, name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3429 | return false; |
| 3430 | } |
| 3431 | out << ";"; |
| 3432 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3433 | Line() << "return " << name << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3434 | |
| 3435 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3438 | bool ASTPrinter::EmitGlobalVariable(const ast::Variable* global) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3439 | return Switch( |
| 3440 | global, // |
| 3441 | [&](const ast::Var* var) { |
| 3442 | auto* sem = builder_.Sem().Get(global); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3443 | switch (sem->AddressSpace()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3444 | case core::AddressSpace::kUniform: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3445 | return EmitUniformVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3446 | case core::AddressSpace::kStorage: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3447 | return EmitStorageVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3448 | case core::AddressSpace::kHandle: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3449 | return EmitHandleVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3450 | case core::AddressSpace::kPrivate: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3451 | return EmitPrivateVariable(sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3452 | case core::AddressSpace::kWorkgroup: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3453 | return EmitWorkgroupVariable(sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3454 | case core::AddressSpace::kPushConstant: |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3455 | diagnostics_.AddError(Source{}) |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 3456 | << "unhandled address space " << sem->AddressSpace(); |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3457 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3458 | default: { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3459 | TINT_ICE() << "unhandled address space " << sem->AddressSpace(); |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3460 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3461 | } |
| 3462 | }, |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3463 | [&](const ast::Override*) { |
| 3464 | // Override is removed with SubstituteOverride |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3465 | diagnostics_.AddError(Source{}) |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 3466 | << "override-expressions should have been removed with the SubstituteOverride " |
| 3467 | "transform"; |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3468 | return false; |
| 3469 | }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3470 | [&](const ast::Const*) { |
| 3471 | return true; // Constants are embedded at their use |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3472 | }, // |
| 3473 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3476 | bool ASTPrinter::EmitUniformVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3477 | auto binding_point = *sem->As<sem::GlobalVariable>()->Attributes().binding_point; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3478 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3479 | auto name = var->name->symbol.Name(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3480 | Line() << "cbuffer cbuffer_" << name << RegisterAndSpace('b', binding_point) << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3481 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3482 | { |
| 3483 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3484 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3485 | if (!EmitTypeAndName(out, type, core::AddressSpace::kUniform, sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3486 | return false; |
| 3487 | } |
| 3488 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3489 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3490 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3491 | Line() << "};"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3492 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3493 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3496 | bool ASTPrinter::EmitStorageVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3497 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3498 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3499 | if (!EmitTypeAndName(out, type, core::AddressSpace::kStorage, sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3500 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3501 | return false; |
| 3502 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3503 | |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3504 | auto* global_sem = sem->As<sem::GlobalVariable>(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3505 | out << RegisterAndSpace(sem->Access() == core::Access::kRead ? 't' : 'u', |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3506 | *global_sem->Attributes().binding_point) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3507 | << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3508 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3509 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3512 | bool ASTPrinter::EmitHandleVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3513 | auto* unwrapped_type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3514 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3515 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3516 | auto name = var->name->symbol.Name(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3517 | auto* type = sem->Type()->UnwrapRef(); |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 3518 | if (ast::HasAttribute<PixelLocal::RasterizerOrderedView>(var->attributes)) { |
| 3519 | TINT_ASSERT(!type->Is<core::type::MultisampledTexture>()); |
| 3520 | auto* storage = type->As<core::type::StorageTexture>(); |
| 3521 | if (!storage) { |
| 3522 | TINT_ICE() << "Rasterizer Ordered View type isn't storage texture"; |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 3523 | } |
| 3524 | out << "RasterizerOrderedTexture2D"; |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 3525 | auto* component = ImageFormatToRWtextureType(storage->texel_format()); |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 3526 | if (TINT_UNLIKELY(!component)) { |
| 3527 | TINT_ICE() << "Unsupported StorageTexture TexelFormat: " |
| 3528 | << static_cast<int>(storage->texel_format()); |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 3529 | } |
| 3530 | out << "<" << component << "> " << name; |
| 3531 | } else if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3532 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3533 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3534 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3535 | const char* register_space = nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3536 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3537 | if (unwrapped_type->Is<core::type::Texture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3538 | register_space = "t"; |
James Price | 88c231b | 2023-08-15 12:47:28 +0000 | [diff] [blame] | 3539 | if (auto* st = unwrapped_type->As<core::type::StorageTexture>(); |
| 3540 | st && st->access() != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3541 | register_space = "u"; |
| 3542 | } |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3543 | } else if (unwrapped_type->Is<core::type::Sampler>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3544 | register_space = "s"; |
| 3545 | } |
| 3546 | |
| 3547 | if (register_space) { |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3548 | auto bp = sem->As<sem::GlobalVariable>()->Attributes().binding_point; |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3549 | out << " : register(" << register_space << bp->binding; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3550 | // Omit the space if it's 0, as it's the default. |
| 3551 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better |
| 3552 | // compatibility. |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3553 | if (bp->group == 0) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3554 | out << ")"; |
| 3555 | } else { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3556 | out << ", space" << bp->group << ")"; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3557 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | out << ";"; |
| 3561 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3564 | bool ASTPrinter::EmitPrivateVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3565 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3566 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3567 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3568 | out << "static "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3569 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3570 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3571 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3572 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3573 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3574 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3575 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3576 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3577 | if (auto* initializer = decl->initializer) { |
| 3578 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3579 | return false; |
| 3580 | } |
| 3581 | } else { |
| 3582 | if (!EmitZeroValue(out, var->Type()->UnwrapRef())) { |
| 3583 | return false; |
| 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | out << ";"; |
| 3588 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3591 | bool ASTPrinter::EmitWorkgroupVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3592 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3593 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3594 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3595 | out << "groupshared "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3596 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3597 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3598 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3599 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3600 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3601 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3602 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3603 | if (auto* initializer = decl->initializer) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3604 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3605 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3606 | return false; |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | out << ";"; |
| 3611 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3614 | std::string ASTPrinter::builtin_to_attribute(core::BuiltinValue builtin) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3615 | switch (builtin) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3616 | case core::BuiltinValue::kPosition: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3617 | return "SV_Position"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3618 | case core::BuiltinValue::kVertexIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3619 | return "SV_VertexID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3620 | case core::BuiltinValue::kInstanceIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3621 | return "SV_InstanceID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3622 | case core::BuiltinValue::kFrontFacing: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3623 | return "SV_IsFrontFace"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3624 | case core::BuiltinValue::kFragDepth: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3625 | return "SV_Depth"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3626 | case core::BuiltinValue::kLocalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3627 | return "SV_GroupThreadID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3628 | case core::BuiltinValue::kLocalInvocationIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3629 | return "SV_GroupIndex"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3630 | case core::BuiltinValue::kGlobalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3631 | return "SV_DispatchThreadID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3632 | case core::BuiltinValue::kWorkgroupId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3633 | return "SV_GroupID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3634 | case core::BuiltinValue::kSampleIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3635 | return "SV_SampleIndex"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3636 | case core::BuiltinValue::kSampleMask: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3637 | return "SV_Coverage"; |
| 3638 | default: |
| 3639 | break; |
| 3640 | } |
| 3641 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3644 | std::string ASTPrinter::interpolation_to_modifiers(core::InterpolationType type, |
| 3645 | core::InterpolationSampling sampling) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3646 | std::string modifiers; |
| 3647 | switch (type) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3648 | case core::InterpolationType::kPerspective: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3649 | modifiers += "linear "; |
| 3650 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3651 | case core::InterpolationType::kLinear: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3652 | modifiers += "noperspective "; |
| 3653 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3654 | case core::InterpolationType::kFlat: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3655 | modifiers += "nointerpolation "; |
| 3656 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3657 | case core::InterpolationType::kUndefined: |
Ben Clayton | f9ed9d3 | 2022-10-11 19:49:17 +0000 | [diff] [blame] | 3658 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3659 | } |
| 3660 | switch (sampling) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3661 | case core::InterpolationSampling::kCentroid: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3662 | modifiers += "centroid "; |
| 3663 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3664 | case core::InterpolationSampling::kSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3665 | modifiers += "sample "; |
| 3666 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3667 | case core::InterpolationSampling::kCenter: |
Gregg Tavares | d015151 | 2024-07-11 23:12:44 +0000 | [diff] [blame] | 3668 | case core::InterpolationSampling::kFirst: |
| 3669 | case core::InterpolationSampling::kEither: |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3670 | case core::InterpolationSampling::kUndefined: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3671 | break; |
| 3672 | } |
| 3673 | return modifiers; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3676 | bool ASTPrinter::EmitEntryPointFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3677 | auto* func_sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3678 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3679 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3680 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3681 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 3682 | // Emit the workgroup_size attribute. |
| 3683 | auto wgsize = func_sem->WorkgroupSize(); |
| 3684 | out << "[numthreads("; |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 3685 | for (size_t i = 0; i < 3; i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3686 | if (i > 0) { |
| 3687 | out << ", "; |
| 3688 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3689 | if (!wgsize[i].has_value()) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3690 | diagnostics_.AddError(Source{}) |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 3691 | << "override-expressions should have been removed with the " |
| 3692 | "SubstituteOverride transform"; |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3693 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3694 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3695 | out << std::to_string(wgsize[i].value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3696 | } |
dan sinclair | 8700be5 | 2024-05-31 18:07:26 +0000 | [diff] [blame] | 3697 | out << ")]\n"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3700 | if (!EmitTypeAndName(out, func_sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3701 | core::Access::kUndefined, func->name->symbol.Name())) { |
Ben Clayton | 1906857 | 2023-02-07 21:28:09 +0000 | [diff] [blame] | 3702 | return false; |
| 3703 | } |
| 3704 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3705 | |
| 3706 | bool first = true; |
| 3707 | |
| 3708 | // Emit entry point parameters. |
| 3709 | for (auto* var : func->params) { |
| 3710 | auto* sem = builder_.Sem().Get(var); |
| 3711 | auto* type = sem->Type(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3712 | if (TINT_UNLIKELY(!type->Is<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3713 | // ICE likely indicates that the CanonicalizeEntryPointIO transform was |
| 3714 | // not run, or a builtin parameter was added after it was run. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3715 | TINT_ICE() << "Unsupported non-struct entry point parameter"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | if (!first) { |
| 3719 | out << ", "; |
| 3720 | } |
| 3721 | first = false; |
| 3722 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3723 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3724 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3725 | return false; |
| 3726 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3727 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3728 | |
| 3729 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3732 | { |
| 3733 | ScopedIndent si(this); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3734 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3735 | if (!EmitStatements(func->body->statements)) { |
| 3736 | return false; |
| 3737 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3738 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3739 | if (!Is<ast::ReturnStatement>(func->body->Last())) { |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 3740 | ast::ReturnStatement ret(GenerationID(), ast::NodeID{}, Source{}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3741 | if (!EmitStatement(&ret)) { |
| 3742 | return false; |
| 3743 | } |
| 3744 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3747 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3748 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3749 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3752 | bool ASTPrinter::EmitConstant(StringStream& out, |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 3753 | const core::constant::Value* constant, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3754 | bool is_variable_initializer) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3755 | return Switch( |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3756 | constant->Type(), // |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3757 | [&](const core::type::Bool*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3758 | out << (constant->ValueAs<AInt>() ? "true" : "false"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3759 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3760 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3761 | [&](const core::type::F32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3762 | PrintF32(out, constant->ValueAs<f32>()); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3763 | return true; |
| 3764 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3765 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3766 | // emit a f16 scalar with explicit float16_t type declaration. |
| 3767 | out << "float16_t("; |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3768 | PrintF16(out, constant->ValueAs<f16>()); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3769 | out << ")"; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3770 | return true; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3771 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3772 | [&](const core::type::I32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3773 | out << constant->ValueAs<AInt>(); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3774 | return true; |
| 3775 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3776 | [&](const core::type::U32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3777 | out << constant->ValueAs<AInt>() << "u"; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3778 | return true; |
| 3779 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3780 | [&](const core::type::Vector* v) { |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 3781 | if (auto* splat = constant->As<core::constant::Splat>()) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3782 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3783 | ScopedParen sp(out); |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3784 | if (!EmitConstant(out, splat->el, is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3785 | return false; |
| 3786 | } |
| 3787 | } |
| 3788 | out << "."; |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3789 | for (size_t i = 0; i < v->Width(); i++) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3790 | out << "x"; |
| 3791 | } |
| 3792 | return true; |
| 3793 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3794 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3795 | if (!EmitType(out, v, core::AddressSpace::kUndefined, core::Access::kUndefined, "")) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3796 | return false; |
| 3797 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3798 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3799 | ScopedParen sp(out); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3800 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3801 | for (size_t i = 0; i < v->Width(); i++) { |
| 3802 | if (i > 0) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3803 | out << ", "; |
| 3804 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3805 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3806 | return false; |
| 3807 | } |
| 3808 | } |
| 3809 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3810 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3811 | [&](const core::type::Matrix* m) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3812 | if (!EmitType(out, m, core::AddressSpace::kUndefined, core::Access::kUndefined, "")) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3813 | return false; |
| 3814 | } |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3815 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3816 | ScopedParen sp(out); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3817 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3818 | for (size_t i = 0; i < m->columns(); i++) { |
| 3819 | if (i > 0) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3820 | out << ", "; |
| 3821 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3822 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3823 | return false; |
| 3824 | } |
| 3825 | } |
| 3826 | return true; |
| 3827 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3828 | [&](const core::type::Array* a) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3829 | if (constant->AllZero()) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3830 | out << "("; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3831 | if (!EmitType(out, a, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 3832 | "")) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3833 | return false; |
| 3834 | } |
| 3835 | out << ")0"; |
| 3836 | return true; |
| 3837 | } |
| 3838 | |
| 3839 | out << "{"; |
| 3840 | TINT_DEFER(out << "}"); |
| 3841 | |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3842 | auto count = a->ConstantCount(); |
| 3843 | if (!count) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3844 | diagnostics_.AddError(Source{}) << core::type::Array::kErrExpectedConstantCount; |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3845 | return false; |
| 3846 | } |
| 3847 | |
| 3848 | for (size_t i = 0; i < count; i++) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3849 | if (i > 0) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3850 | out << ", "; |
| 3851 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3852 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3853 | return false; |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | return true; |
| 3858 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3859 | [&](const core::type::Struct* s) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3860 | if (!EmitStructType(&helpers_, s)) { |
| 3861 | return false; |
| 3862 | } |
| 3863 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3864 | if (constant->AllZero()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3865 | out << "(" << StructName(s) << ")0"; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3866 | return true; |
| 3867 | } |
| 3868 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3869 | auto emit_member_values = [&](StringStream& o) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3870 | o << "{"; |
dan sinclair | ad9cd0a | 2022-12-06 20:01:54 +0000 | [diff] [blame] | 3871 | for (size_t i = 0; i < s->Members().Length(); i++) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3872 | if (i > 0) { |
| 3873 | o << ", "; |
| 3874 | } |
| 3875 | if (!EmitConstant(o, constant->Index(i), is_variable_initializer)) { |
| 3876 | return false; |
| 3877 | } |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3878 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3879 | o << "}"; |
| 3880 | return true; |
| 3881 | }; |
| 3882 | |
| 3883 | if (is_variable_initializer) { |
| 3884 | if (!emit_member_values(out)) { |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3885 | return false; |
| 3886 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3887 | } else { |
| 3888 | // HLSL requires structure initializers to be assigned directly to a variable. |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3889 | // For these constants use 'static const' at global-scope. 'const' at global scope |
| 3890 | // creates a variable who's initializer is ignored, and the value is expected to be |
| 3891 | // provided in a cbuffer. 'static const' is a true value-embedded-in-the-shader-code |
| 3892 | // constant. We also emit these for function-local constant expressions for |
| 3893 | // consistency and to ensure that these are not computed at execution time. |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3894 | auto name = UniqueIdentifier("c"); |
| 3895 | { |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3896 | StringStream decl; |
| 3897 | decl << "static const " << StructName(s) << " " << name << " = "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3898 | if (!emit_member_values(decl)) { |
| 3899 | return false; |
| 3900 | } |
| 3901 | decl << ";"; |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3902 | current_buffer_->Insert(decl.str(), global_insertion_point_++, 0); |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3903 | } |
| 3904 | out << name; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3908 | }, // |
| 3909 | TINT_ICE_ON_NO_MATCH); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3912 | bool ASTPrinter::EmitLiteral(StringStream& out, const ast::LiteralExpression* lit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3913 | return Switch( |
| 3914 | lit, |
| 3915 | [&](const ast::BoolLiteralExpression* l) { |
| 3916 | out << (l->value ? "true" : "false"); |
| 3917 | return true; |
| 3918 | }, |
Ben Clayton | 3ad927c | 2022-05-25 23:12:14 +0000 | [diff] [blame] | 3919 | [&](const ast::FloatLiteralExpression* l) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3920 | if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) { |
| 3921 | // Emit f16 literal with explicit float16_t type declaration. |
| 3922 | out << "float16_t("; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3923 | PrintF16(out, static_cast<float>(l->value)); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3924 | out << ")"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3925 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3926 | PrintF32(out, static_cast<float>(l->value)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3927 | return true; |
| 3928 | }, |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3929 | [&](const ast::IntLiteralExpression* i) { |
| 3930 | out << i->value; |
| 3931 | switch (i->suffix) { |
| 3932 | case ast::IntLiteralExpression::Suffix::kNone: |
| 3933 | case ast::IntLiteralExpression::Suffix::kI: |
| 3934 | return true; |
| 3935 | case ast::IntLiteralExpression::Suffix::kU: |
| 3936 | out << "u"; |
| 3937 | return true; |
| 3938 | } |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 3939 | diagnostics_.AddError(Source{}) << "unknown integer literal suffix type"; |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3940 | return false; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3941 | }, // |
| 3942 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3945 | bool ASTPrinter::EmitValue(StringStream& out, const core::type::Type* type, int value) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3946 | return Switch( |
| 3947 | type, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3948 | [&](const core::type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3949 | out << (value == 0 ? "false" : "true"); |
| 3950 | return true; |
| 3951 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3952 | [&](const core::type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3953 | out << value << ".0f"; |
| 3954 | return true; |
| 3955 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3956 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3957 | out << "float16_t(" << value << ".0h)"; |
| 3958 | return true; |
| 3959 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3960 | [&](const core::type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3961 | out << value; |
| 3962 | return true; |
| 3963 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3964 | [&](const core::type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3965 | out << value << "u"; |
| 3966 | return true; |
| 3967 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3968 | [&](const core::type::Vector* vec) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3969 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3970 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3971 | return false; |
| 3972 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3973 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3974 | for (uint32_t i = 0; i < vec->Width(); i++) { |
| 3975 | if (i != 0) { |
| 3976 | out << ", "; |
| 3977 | } |
| 3978 | if (!EmitValue(out, vec->type(), value)) { |
| 3979 | return false; |
| 3980 | } |
| 3981 | } |
| 3982 | return true; |
| 3983 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3984 | [&](const core::type::Matrix* mat) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3985 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3986 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3987 | return false; |
| 3988 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3989 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3990 | for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { |
| 3991 | if (i != 0) { |
| 3992 | out << ", "; |
| 3993 | } |
| 3994 | if (!EmitValue(out, mat->type(), value)) { |
| 3995 | return false; |
| 3996 | } |
| 3997 | } |
| 3998 | return true; |
| 3999 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4000 | [&](const core::type::Struct*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4001 | out << "("; |
| 4002 | TINT_DEFER(out << ")" << value); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4003 | return EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 4004 | ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4005 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4006 | [&](const core::type::Array*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4007 | out << "("; |
| 4008 | TINT_DEFER(out << ")" << value); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4009 | return EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 4010 | ""); |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4011 | }, // |
| 4012 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4015 | bool ASTPrinter::EmitZeroValue(StringStream& out, const core::type::Type* type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4016 | return EmitValue(out, type, 0); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4019 | bool ASTPrinter::EmitLoop(const ast::LoopStatement* stmt) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 4020 | auto emit_continuing = [this, stmt] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4021 | if (stmt->continuing && !stmt->continuing->Empty()) { |
| 4022 | if (!EmitBlock(stmt->continuing)) { |
| 4023 | return false; |
| 4024 | } |
| 4025 | } |
| 4026 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4027 | }; |
| 4028 | |
| 4029 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4030 | Line() << "while (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4031 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4032 | ScopedIndent si(this); |
| 4033 | if (!EmitStatements(stmt->body->statements)) { |
| 4034 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4035 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4036 | if (!emit_continuing_()) { |
| 4037 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4038 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4039 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4040 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4041 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4042 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4045 | bool ASTPrinter::EmitForLoop(const ast::ForLoopStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4046 | // Nest a for loop with a new block. In HLSL the initializer scope is not |
| 4047 | // nested by the for-loop, so we may get variable redefinitions. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4048 | Line() << "{"; |
| 4049 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4050 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4051 | DecrementIndent(); |
| 4052 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4053 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4054 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4055 | TextBuffer init_buf; |
| 4056 | if (auto* init = stmt->initializer) { |
| 4057 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); |
| 4058 | if (!EmitStatement(init)) { |
| 4059 | return false; |
| 4060 | } |
| 4061 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4062 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4063 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4064 | StringStream cond_buf; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4065 | if (auto* cond = stmt->condition) { |
| 4066 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 4067 | if (!EmitExpression(cond_buf, cond)) { |
| 4068 | return false; |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | TextBuffer cont_buf; |
| 4073 | if (auto* cont = stmt->continuing) { |
| 4074 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cont_buf); |
| 4075 | if (!EmitStatement(cont)) { |
| 4076 | return false; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | // If the for-loop has a multi-statement conditional and / or continuing, then |
| 4081 | // we cannot emit this as a regular for-loop in HLSL. Instead we need to |
| 4082 | // generate a `while(true)` loop. |
| 4083 | bool emit_as_loop = cond_pre.lines.size() > 0 || cont_buf.lines.size() > 1; |
| 4084 | |
| 4085 | // If the for-loop has multi-statement initializer, or is going to be emitted |
| 4086 | // as a `while(true)` loop, then declare the initializer statement(s) before |
| 4087 | // the loop. |
| 4088 | if (init_buf.lines.size() > 1 || (stmt->initializer && emit_as_loop)) { |
| 4089 | current_buffer_->Append(init_buf); |
| 4090 | init_buf.lines.clear(); // Don't emit the initializer again in the 'for' |
| 4091 | } |
| 4092 | |
| 4093 | if (emit_as_loop) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 4094 | auto emit_continuing = [&] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4095 | current_buffer_->Append(cont_buf); |
| 4096 | return true; |
| 4097 | }; |
| 4098 | |
| 4099 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4100 | Line() << "while (true) {"; |
| 4101 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4102 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4103 | DecrementIndent(); |
| 4104 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4105 | }); |
| 4106 | |
| 4107 | if (stmt->condition) { |
| 4108 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4109 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | if (!EmitStatements(stmt->body->statements)) { |
| 4113 | return false; |
| 4114 | } |
| 4115 | |
| 4116 | if (!emit_continuing_()) { |
| 4117 | return false; |
| 4118 | } |
| 4119 | } else { |
| 4120 | // For-loop can be generated. |
| 4121 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4122 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 4123 | out << "for"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4124 | { |
| 4125 | ScopedParen sp(out); |
| 4126 | |
| 4127 | if (!init_buf.lines.empty()) { |
| 4128 | out << init_buf.lines[0].content << " "; |
| 4129 | } else { |
| 4130 | out << "; "; |
| 4131 | } |
| 4132 | |
| 4133 | out << cond_buf.str() << "; "; |
| 4134 | |
| 4135 | if (!cont_buf.lines.empty()) { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4136 | out << tint::TrimSuffix(cont_buf.lines[0].content, ";"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4137 | } |
| 4138 | } |
| 4139 | out << " {"; |
| 4140 | } |
| 4141 | { |
| 4142 | auto emit_continuing = [] { return true; }; |
| 4143 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 4144 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 4145 | return false; |
| 4146 | } |
| 4147 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4148 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | return true; |
| 4152 | } |
| 4153 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4154 | bool ASTPrinter::EmitWhile(const ast::WhileStatement* stmt) { |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4155 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4156 | StringStream cond_buf; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4157 | { |
| 4158 | auto* cond = stmt->condition; |
| 4159 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 4160 | if (!EmitExpression(cond_buf, cond)) { |
| 4161 | return false; |
| 4162 | } |
| 4163 | } |
| 4164 | |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 4165 | auto emit_continuing = [&] { return true; }; |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 4166 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 4167 | |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4168 | // If the while has a multi-statement conditional, then we cannot emit this |
| 4169 | // as a regular while in HLSL. Instead we need to generate a `while(true)` loop. |
| 4170 | bool emit_as_loop = cond_pre.lines.size() > 0; |
| 4171 | if (emit_as_loop) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4172 | Line() << "while (true) {"; |
| 4173 | IncrementIndent(); |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4174 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4175 | DecrementIndent(); |
| 4176 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4177 | }); |
| 4178 | |
| 4179 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4180 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4181 | if (!EmitStatements(stmt->body->statements)) { |
| 4182 | return false; |
| 4183 | } |
| 4184 | } else { |
| 4185 | // While can be generated. |
| 4186 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4187 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 4188 | out << "while"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4189 | { |
| 4190 | ScopedParen sp(out); |
| 4191 | out << cond_buf.str(); |
| 4192 | } |
| 4193 | out << " {"; |
| 4194 | } |
| 4195 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 4196 | return false; |
| 4197 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4198 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | return true; |
| 4202 | } |
| 4203 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4204 | bool ASTPrinter::EmitMemberAccessor(StringStream& out, const ast::MemberAccessorExpression* expr) { |
Ben Clayton | ad31565 | 2023-02-05 12:36:50 +0000 | [diff] [blame] | 4205 | if (!EmitExpression(out, expr->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4206 | return false; |
| 4207 | } |
| 4208 | out << "."; |
| 4209 | |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 4210 | auto* sem = builder_.Sem().Get(expr)->UnwrapLoad(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4211 | |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4212 | return Switch( |
| 4213 | sem, |
| 4214 | [&](const sem::Swizzle*) { |
| 4215 | // Swizzles output the name directly |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4216 | out << expr->member->symbol.Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4217 | return true; |
| 4218 | }, |
| 4219 | [&](const sem::StructMemberAccess* member_access) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4220 | out << member_access->Member()->Name().Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4221 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4222 | }, // |
| 4223 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4226 | bool ASTPrinter::EmitReturn(const ast::ReturnStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4227 | if (stmt->value) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4228 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4229 | out << "return "; |
| 4230 | if (!EmitExpression(out, stmt->value)) { |
| 4231 | return false; |
| 4232 | } |
| 4233 | out << ";"; |
| 4234 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4235 | Line() << "return;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4236 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4237 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4240 | bool ASTPrinter::EmitStatement(const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4241 | return Switch( |
| 4242 | stmt, |
| 4243 | [&](const ast::AssignmentStatement* a) { // |
| 4244 | return EmitAssign(a); |
| 4245 | }, |
| 4246 | [&](const ast::BlockStatement* b) { // |
| 4247 | return EmitBlock(b); |
| 4248 | }, |
| 4249 | [&](const ast::BreakStatement* b) { // |
| 4250 | return EmitBreak(b); |
| 4251 | }, |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 4252 | [&](const ast::BreakIfStatement* b) { // |
| 4253 | return EmitBreakIf(b); |
| 4254 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4255 | [&](const ast::CallStatement* c) { // |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4256 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4257 | if (!EmitCall(out, c->expr)) { |
| 4258 | return false; |
| 4259 | } |
| 4260 | out << ";"; |
| 4261 | return true; |
| 4262 | }, |
| 4263 | [&](const ast::ContinueStatement* c) { // |
| 4264 | return EmitContinue(c); |
| 4265 | }, |
| 4266 | [&](const ast::DiscardStatement* d) { // |
| 4267 | return EmitDiscard(d); |
| 4268 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4269 | [&](const ast::IfStatement* i) { // |
| 4270 | return EmitIf(i); |
| 4271 | }, |
| 4272 | [&](const ast::LoopStatement* l) { // |
| 4273 | return EmitLoop(l); |
| 4274 | }, |
| 4275 | [&](const ast::ForLoopStatement* l) { // |
| 4276 | return EmitForLoop(l); |
| 4277 | }, |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4278 | [&](const ast::WhileStatement* l) { // |
| 4279 | return EmitWhile(l); |
| 4280 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4281 | [&](const ast::ReturnStatement* r) { // |
| 4282 | return EmitReturn(r); |
| 4283 | }, |
| 4284 | [&](const ast::SwitchStatement* s) { // |
| 4285 | return EmitSwitch(s); |
| 4286 | }, |
| 4287 | [&](const ast::VariableDeclStatement* v) { // |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4288 | return Switch( |
| 4289 | v->variable, // |
| 4290 | [&](const ast::Var* var) { return EmitVar(var); }, |
| 4291 | [&](const ast::Let* let) { return EmitLet(let); }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 4292 | [&](const ast::Const*) { |
| 4293 | return true; // Constants are embedded at their use |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4294 | }, // |
| 4295 | TINT_ICE_ON_NO_MATCH); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4296 | }, |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 4297 | [&](const ast::ConstAssert*) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 4298 | return true; // Not emitted |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4299 | }, // |
| 4300 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4303 | bool ASTPrinter::EmitDefaultOnlySwitch(const ast::SwitchStatement* stmt) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4304 | TINT_ASSERT(stmt->body.Length() == 1 && stmt->body[0]->ContainsDefault()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4305 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4306 | // FXC fails to compile a switch with just a default case, ignoring the |
| 4307 | // default case body. We work around this here by emitting the default case |
| 4308 | // without the switch. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4309 | |
Antonio Maiorano | eab1f62 | 2023-02-01 15:46:34 +0000 | [diff] [blame] | 4310 | // Emit the switch condition as-is if it has side-effects (e.g. |
| 4311 | // function call). Note that we can ignore the result of the expression (if any). |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 4312 | if (auto* sem_cond = builder_.Sem().GetVal(stmt->condition); sem_cond->HasSideEffects()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4313 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4314 | if (!EmitExpression(out, stmt->condition)) { |
| 4315 | return false; |
| 4316 | } |
| 4317 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4318 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4319 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4320 | // Emit "do { <default case body> } while(false);". We use a 'do' loop so |
| 4321 | // that break statements work as expected, and make it 'while (false)' in |
| 4322 | // case there isn't a break statement. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4323 | Line() << "do {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4324 | { |
| 4325 | ScopedIndent si(this); |
| 4326 | if (!EmitStatements(stmt->body[0]->body->statements)) { |
| 4327 | return false; |
| 4328 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4329 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4330 | Line() << "} while (false);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4331 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4334 | bool ASTPrinter::EmitSwitch(const ast::SwitchStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4335 | // BUG(crbug.com/tint/1188): work around default-only switches |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 4336 | if (stmt->body.Length() == 1 && stmt->body[0]->selectors.Length() == 1 && |
| 4337 | stmt->body[0]->ContainsDefault()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4338 | return EmitDefaultOnlySwitch(stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4339 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4340 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4341 | { // switch(expr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4342 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4343 | out << "switch("; |
| 4344 | if (!EmitExpression(out, stmt->condition)) { |
| 4345 | return false; |
| 4346 | } |
| 4347 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4348 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4349 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4350 | { |
| 4351 | ScopedIndent si(this); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 4352 | for (size_t i = 0; i < stmt->body.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4353 | if (!EmitCase(stmt, i)) { |
| 4354 | return false; |
| 4355 | } |
| 4356 | } |
| 4357 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4358 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4359 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4360 | |
| 4361 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4364 | bool ASTPrinter::EmitType(StringStream& out, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4365 | const core::type::Type* type, |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4366 | core::AddressSpace address_space, |
| 4367 | core::Access access, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4368 | const std::string& name, |
| 4369 | bool* name_printed /* = nullptr */) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4370 | if (name_printed) { |
| 4371 | *name_printed = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4372 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4373 | switch (address_space) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4374 | case core::AddressSpace::kStorage: |
| 4375 | if (access != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4376 | out << "RW"; |
| 4377 | } |
| 4378 | out << "ByteAddressBuffer"; |
| 4379 | return true; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4380 | case core::AddressSpace::kUniform: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4381 | auto array_length = (type->Size() + 15) / 16; |
| 4382 | out << "uint4 " << name << "[" << array_length << "]"; |
| 4383 | if (name_printed) { |
| 4384 | *name_printed = true; |
| 4385 | } |
| 4386 | return true; |
| 4387 | } |
| 4388 | default: |
| 4389 | break; |
| 4390 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4391 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4392 | return Switch( |
| 4393 | type, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4394 | [&](const core::type::Array* ary) { |
| 4395 | const core::type::Type* base_type = ary; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4396 | std::vector<uint32_t> sizes; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4397 | while (auto* arr = base_type->As<core::type::Array>()) { |
| 4398 | if (TINT_UNLIKELY(arr->Count()->Is<core::type::RuntimeArrayCount>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4399 | TINT_ICE() |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4400 | << "runtime arrays may only exist in storage buffers, which should have " |
Ben Clayton | 3a68ab4 | 2022-06-24 08:30:28 +0000 | [diff] [blame] | 4401 | "been transformed into a ByteAddressBuffer"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4402 | } |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4403 | const auto count = arr->ConstantCount(); |
| 4404 | if (!count) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 4405 | diagnostics_.AddError(Source{}) << core::type::Array::kErrExpectedConstantCount; |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4406 | return false; |
| 4407 | } |
| 4408 | |
| 4409 | sizes.push_back(count.value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4410 | base_type = arr->ElemType(); |
| 4411 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4412 | if (!EmitType(out, base_type, address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4413 | return false; |
| 4414 | } |
| 4415 | if (!name.empty()) { |
| 4416 | out << " " << name; |
| 4417 | if (name_printed) { |
| 4418 | *name_printed = true; |
| 4419 | } |
| 4420 | } |
| 4421 | for (uint32_t size : sizes) { |
| 4422 | out << "[" << size << "]"; |
| 4423 | } |
| 4424 | return true; |
| 4425 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4426 | [&](const core::type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4427 | out << "bool"; |
| 4428 | return true; |
| 4429 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4430 | [&](const core::type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4431 | out << "float"; |
| 4432 | return true; |
| 4433 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4434 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4435 | out << "float16_t"; |
| 4436 | return true; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 4437 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4438 | [&](const core::type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4439 | out << "int"; |
| 4440 | return true; |
| 4441 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4442 | [&](const core::type::Matrix* mat) { |
| 4443 | if (mat->type()->Is<core::type::F16>()) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4444 | // Use matrix<type, N, M> for f16 matrix |
| 4445 | out << "matrix<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4446 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4447 | return false; |
| 4448 | } |
| 4449 | out << ", " << mat->columns() << ", " << mat->rows() << ">"; |
| 4450 | return true; |
| 4451 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4452 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4453 | return false; |
| 4454 | } |
| 4455 | // Note: HLSL's matrices are declared as <type>NxM, where N is the |
| 4456 | // number of rows and M is the number of columns. Despite HLSL's |
| 4457 | // matrices being column-major by default, the index operator and |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4458 | // initializers actually operate on row-vectors, where as WGSL operates |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4459 | // on column vectors. To simplify everything we use the transpose of the |
| 4460 | // matrices. See: |
| 4461 | // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math#matrix-ordering |
| 4462 | out << mat->columns() << "x" << mat->rows(); |
| 4463 | return true; |
| 4464 | }, |
Ben Clayton | 7cdaffe | 2024-05-08 16:24:07 +0000 | [diff] [blame] | 4465 | [&](const core::type::Pointer*) -> bool { |
| 4466 | TINT_ICE() << "Attempting to emit pointer type. These should have been removed with " |
| 4467 | "the SimplifyPointers transform"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4468 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4469 | [&](const core::type::Sampler* sampler) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4470 | out << "Sampler"; |
| 4471 | if (sampler->IsComparison()) { |
| 4472 | out << "Comparison"; |
| 4473 | } |
| 4474 | out << "State"; |
| 4475 | return true; |
| 4476 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4477 | [&](const core::type::Struct* str) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4478 | out << StructName(str); |
| 4479 | return true; |
| 4480 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4481 | [&](const core::type::Texture* tex) { |
| 4482 | if (TINT_UNLIKELY(tex->Is<core::type::ExternalTexture>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4483 | TINT_ICE() << "Multiplanar external texture transform was not run."; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4484 | } |
Brandon Jones | 6661b28 | 2022-02-25 20:14:52 +0000 | [diff] [blame] | 4485 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4486 | auto* storage = tex->As<core::type::StorageTexture>(); |
| 4487 | auto* ms = tex->As<core::type::MultisampledTexture>(); |
| 4488 | auto* depth_ms = tex->As<core::type::DepthMultisampledTexture>(); |
| 4489 | auto* sampled = tex->As<core::type::SampledTexture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4490 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4491 | if (storage && storage->access() != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4492 | out << "RW"; |
| 4493 | } |
| 4494 | out << "Texture"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4495 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4496 | switch (tex->dim()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4497 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4498 | out << "1D"; |
| 4499 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4500 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4501 | out << ((ms || depth_ms) ? "2DMS" : "2D"); |
| 4502 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4503 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4504 | out << ((ms || depth_ms) ? "2DMSArray" : "2DArray"); |
| 4505 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4506 | case core::type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4507 | out << "3D"; |
| 4508 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4509 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4510 | out << "Cube"; |
| 4511 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4512 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4513 | out << "CubeArray"; |
| 4514 | break; |
| 4515 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4516 | TINT_UNREACHABLE() << "unexpected TextureDimension " << tex->dim(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4517 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4518 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4519 | if (storage) { |
Ben Clayton | 448c01b | 2024-02-28 00:23:17 +0000 | [diff] [blame] | 4520 | auto* component = ImageFormatToRWtextureType(storage->texel_format()); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4521 | if (TINT_UNLIKELY(!component)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4522 | TINT_ICE() << "Unsupported StorageTexture TexelFormat: " |
| 4523 | << static_cast<int>(storage->texel_format()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4524 | } |
| 4525 | out << "<" << component << ">"; |
| 4526 | } else if (depth_ms) { |
| 4527 | out << "<float4>"; |
| 4528 | } else if (sampled || ms) { |
| 4529 | auto* subtype = sampled ? sampled->type() : ms->type(); |
| 4530 | out << "<"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4531 | if (subtype->Is<core::type::F32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4532 | out << "float4"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4533 | } else if (subtype->Is<core::type::I32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4534 | out << "int4"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4535 | } else if (TINT_LIKELY(subtype->Is<core::type::U32>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4536 | out << "uint4"; |
| 4537 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4538 | TINT_ICE() << "Unsupported multisampled texture type"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4539 | } |
| 4540 | out << ">"; |
| 4541 | } |
| 4542 | return true; |
| 4543 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4544 | [&](const core::type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4545 | out << "uint"; |
| 4546 | return true; |
| 4547 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4548 | [&](const core::type::Vector* vec) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4549 | auto width = vec->Width(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4550 | if (vec->type()->Is<core::type::F32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4551 | out << "float" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4552 | } else if (vec->type()->Is<core::type::I32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4553 | out << "int" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4554 | } else if (vec->type()->Is<core::type::U32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4555 | out << "uint" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4556 | } else if (vec->type()->Is<core::type::Bool>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4557 | out << "bool" << width; |
| 4558 | } else { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4559 | // For example, use "vector<float16_t, N>" for f16 vector. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4560 | out << "vector<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4561 | if (!EmitType(out, vec->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4562 | return false; |
| 4563 | } |
| 4564 | out << ", " << width << ">"; |
| 4565 | } |
| 4566 | return true; |
| 4567 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4568 | [&](const core::type::Atomic* atomic) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4569 | return EmitType(out, atomic->Type(), address_space, access, name); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4570 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4571 | [&](const core::type::Void*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4572 | out << "void"; |
| 4573 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4574 | }, // |
| 4575 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4578 | bool ASTPrinter::EmitTypeAndName(StringStream& out, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4579 | const core::type::Type* type, |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4580 | core::AddressSpace address_space, |
| 4581 | core::Access access, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4582 | const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4583 | bool name_printed = false; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4584 | if (!EmitType(out, type, address_space, access, name, &name_printed)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4585 | return false; |
| 4586 | } |
| 4587 | if (!name.empty() && !name_printed) { |
| 4588 | out << " " << name; |
| 4589 | } |
| 4590 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4593 | bool ASTPrinter::EmitStructType(TextBuffer* b, const core::type::Struct* str) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 4594 | auto it = emitted_structs_.emplace(str); |
| 4595 | if (!it.second) { |
| 4596 | return true; |
| 4597 | } |
| 4598 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4599 | Line(b) << "struct " << StructName(str) << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4600 | { |
| 4601 | ScopedIndent si(b); |
| 4602 | for (auto* mem : str->Members()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4603 | auto mem_name = mem->Name().Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4604 | auto* ty = mem->Type(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4605 | auto out = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4606 | std::string pre, post; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4607 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4608 | auto& attributes = mem->Attributes(); |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 4609 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4610 | if (auto location = attributes.location) { |
| 4611 | auto& pipeline_stage_uses = str->PipelineStageUses(); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4612 | if (TINT_UNLIKELY(pipeline_stage_uses.Count() != 1)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4613 | TINT_ICE() << "invalid entry point IO struct uses"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4614 | } |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4615 | if (pipeline_stage_uses.Contains(core::type::PipelineStageUsage::kVertexInput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4616 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4617 | } else if (pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4618 | core::type::PipelineStageUsage::kVertexOutput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4619 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4620 | } else if (pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4621 | core::type::PipelineStageUsage::kFragmentInput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4622 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4623 | } else if (TINT_LIKELY(pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4624 | core::type::PipelineStageUsage::kFragmentOutput))) { |
Corentin Wallez | 0880e06 | 2024-01-30 16:16:45 +0000 | [diff] [blame] | 4625 | if (auto blend_src = attributes.blend_src) { |
| 4626 | post += |
| 4627 | " : SV_Target" + std::to_string(location.value() + blend_src.value()); |
Brandon Jones | 07500d3 | 2023-07-21 22:07:03 +0000 | [diff] [blame] | 4628 | } else { |
| 4629 | post += " : SV_Target" + std::to_string(location.value()); |
| 4630 | } |
| 4631 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4632 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4633 | TINT_ICE() << "invalid use of location attribute"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | if (auto builtin = attributes.builtin) { |
| 4637 | auto name = builtin_to_attribute(builtin.value()); |
| 4638 | if (name.empty()) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 4639 | diagnostics_.AddError(Source{}) << "unsupported builtin"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4640 | return false; |
| 4641 | } |
| 4642 | post += " : " + name; |
| 4643 | } |
| 4644 | if (auto interpolation = attributes.interpolation) { |
| 4645 | auto mod = interpolation_to_modifiers(interpolation->type, interpolation->sampling); |
| 4646 | if (mod.empty()) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 4647 | diagnostics_.AddError(Source{}) << "unsupported interpolation"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4648 | return false; |
| 4649 | } |
| 4650 | pre += mod; |
| 4651 | } |
| 4652 | if (attributes.invariant) { |
| 4653 | // Note: `precise` is not exactly the same as `invariant`, but is |
| 4654 | // stricter and therefore provides the necessary guarantees. |
| 4655 | // See discussion here: https://github.com/gpuweb/gpuweb/issues/893 |
| 4656 | pre += "precise "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4659 | out << pre; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4660 | if (!EmitTypeAndName(out, ty, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
| 4661 | mem_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4662 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4663 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4664 | out << post << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4665 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4666 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4667 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4668 | Line(b) << "};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4669 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4672 | bool ASTPrinter::EmitUnaryOp(StringStream& out, const ast::UnaryOpExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4673 | switch (expr->op) { |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4674 | case core::UnaryOp::kIndirection: |
| 4675 | case core::UnaryOp::kAddressOf: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4676 | return EmitExpression(out, expr->expr); |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4677 | case core::UnaryOp::kComplement: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4678 | out << "~"; |
| 4679 | break; |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4680 | case core::UnaryOp::kNot: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4681 | out << "!"; |
| 4682 | break; |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4683 | case core::UnaryOp::kNegation: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4684 | out << "-"; |
| 4685 | break; |
| 4686 | } |
| 4687 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4688 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4689 | if (!EmitExpression(out, expr->expr)) { |
| 4690 | return false; |
| 4691 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4692 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4693 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4694 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4695 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4698 | bool ASTPrinter::EmitVar(const ast::Var* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4699 | auto* sem = builder_.Sem().Get(var); |
| 4700 | auto* type = sem->Type()->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4701 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4702 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4703 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4704 | return false; |
| 4705 | } |
| 4706 | |
| 4707 | out << " = "; |
| 4708 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4709 | if (var->initializer) { |
| 4710 | if (!EmitExpression(out, var->initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4711 | return false; |
| 4712 | } |
| 4713 | } else { |
| 4714 | if (!EmitZeroValue(out, type)) { |
| 4715 | return false; |
| 4716 | } |
| 4717 | } |
| 4718 | out << ";"; |
| 4719 | |
| 4720 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4723 | bool ASTPrinter::EmitLet(const ast::Let* let) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4724 | auto* sem = builder_.Sem().Get(let); |
| 4725 | auto* type = sem->Type()->UnwrapRef(); |
| 4726 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4727 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4728 | if (!EmitTypeAndName(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4729 | let->name->symbol.Name())) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4730 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4731 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4732 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4733 | if (!EmitExpression(out, let->initializer)) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4734 | return false; |
| 4735 | } |
| 4736 | out << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4737 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4738 | return true; |
| 4739 | } |
| 4740 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4741 | template <typename F> |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4742 | bool ASTPrinter::CallBuiltinHelper(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4743 | const ast::CallExpression* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 4744 | const sem::BuiltinFn* builtin, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4745 | F&& build) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4746 | // Generate the helper function if it hasn't been created already |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 4747 | auto fn = tint::GetOrAdd(builtins_, builtin, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4748 | TextBuffer b; |
| 4749 | TINT_DEFER(helpers_.Append(b)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4750 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 4751 | auto fn_name = UniqueIdentifier(std::string("tint_") + wgsl::str(builtin->Fn())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4752 | std::vector<std::string> parameter_names; |
| 4753 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4754 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4755 | if (!EmitTypeAndName(decl, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 4756 | core::Access::kUndefined, fn_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4757 | return ""; |
| 4758 | } |
| 4759 | { |
| 4760 | ScopedParen sp(decl); |
| 4761 | for (auto* param : builtin->Parameters()) { |
| 4762 | if (!parameter_names.empty()) { |
| 4763 | decl << ", "; |
| 4764 | } |
| 4765 | auto param_name = "param_" + std::to_string(parameter_names.size()); |
| 4766 | const auto* ty = param->Type(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4767 | if (auto* ptr = ty->As<core::type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4768 | decl << "inout "; |
| 4769 | ty = ptr->StoreType(); |
| 4770 | } |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4771 | if (!EmitTypeAndName(decl, ty, core::AddressSpace::kUndefined, |
| 4772 | core::Access::kUndefined, param_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4773 | return ""; |
| 4774 | } |
| 4775 | parameter_names.emplace_back(std::move(param_name)); |
| 4776 | } |
| 4777 | } |
| 4778 | decl << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4779 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4780 | { |
| 4781 | ScopedIndent si(&b); |
| 4782 | if (!build(&b, parameter_names)) { |
| 4783 | return ""; |
| 4784 | } |
| 4785 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4786 | Line(&b) << "}"; |
| 4787 | Line(&b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4788 | return fn_name; |
| 4789 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4790 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4791 | if (fn.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4792 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4793 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4794 | |
| 4795 | // Call the helper |
| 4796 | out << fn; |
| 4797 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 4798 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4799 | bool first = true; |
| 4800 | for (auto* arg : call->args) { |
| 4801 | if (!first) { |
| 4802 | out << ", "; |
| 4803 | } |
| 4804 | first = false; |
| 4805 | if (!EmitExpression(out, arg)) { |
| 4806 | return false; |
| 4807 | } |
| 4808 | } |
| 4809 | } |
| 4810 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4813 | std::string ASTPrinter::StructName(const core::type::Struct* s) { |
Ben Clayton | 34c8e57 | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 4814 | auto name = s->Name().Name(); |
| 4815 | if (HasPrefix(name, "__")) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 4816 | name = tint::GetOrAdd(builtin_struct_names_, s, |
| 4817 | [&] { return UniqueIdentifier(name.substr(2)); }); |
Ben Clayton | 34c8e57 | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 4818 | } |
| 4819 | return name; |
| 4820 | } |
| 4821 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4822 | std::string ASTPrinter::UniqueIdentifier(const std::string& prefix /* = "" */) { |
Ben Clayton | 5ed099a | 2023-07-25 18:52:03 +0000 | [diff] [blame] | 4823 | return builder_.Symbols().New(prefix).Name(); |
| 4824 | } |
| 4825 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4826 | } // namespace tint::hlsl::writer |