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 | |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 36 | #include "src/tint/lang/core/constant/splat.h" |
| 37 | #include "src/tint/lang/core/constant/value.h" |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 38 | #include "src/tint/lang/core/fluent_types.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 39 | #include "src/tint/lang/core/type/array.h" |
| 40 | #include "src/tint/lang/core/type/atomic.h" |
| 41 | #include "src/tint/lang/core/type/depth_multisampled_texture.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 42 | #include "src/tint/lang/core/type/multisampled_texture.h" |
| 43 | #include "src/tint/lang/core/type/sampled_texture.h" |
| 44 | #include "src/tint/lang/core/type/storage_texture.h" |
| 45 | #include "src/tint/lang/core/type/texture_dimension.h" |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 46 | #include "src/tint/lang/hlsl/writer/ast_raise/calculate_array_length.h" |
| 47 | #include "src/tint/lang/hlsl/writer/ast_raise/decompose_memory_access.h" |
| 48 | #include "src/tint/lang/hlsl/writer/ast_raise/localize_struct_array_assignment.h" |
| 49 | #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] | 50 | #include "src/tint/lang/hlsl/writer/ast_raise/pixel_local.h" |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 51 | #include "src/tint/lang/hlsl/writer/ast_raise/truncate_interstage_variables.h" |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 52 | #include "src/tint/lang/hlsl/writer/common/option_helpers.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 53 | #include "src/tint/lang/wgsl/ast/call_statement.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 54 | #include "src/tint/lang/wgsl/ast/internal_attribute.h" |
| 55 | #include "src/tint/lang/wgsl/ast/interpolate_attribute.h" |
| 56 | #include "src/tint/lang/wgsl/ast/transform/add_empty_entry_point.h" |
| 57 | #include "src/tint/lang/wgsl/ast/transform/array_length_from_uniform.h" |
| 58 | #include "src/tint/lang/wgsl/ast/transform/binding_remapper.h" |
| 59 | #include "src/tint/lang/wgsl/ast/transform/builtin_polyfill.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 60 | #include "src/tint/lang/wgsl/ast/transform/canonicalize_entry_point_io.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 61 | #include "src/tint/lang/wgsl/ast/transform/demote_to_helper.h" |
| 62 | #include "src/tint/lang/wgsl/ast/transform/direct_variable_access.h" |
| 63 | #include "src/tint/lang/wgsl/ast/transform/disable_uniformity_analysis.h" |
| 64 | #include "src/tint/lang/wgsl/ast/transform/expand_compound_assignment.h" |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 65 | #include "src/tint/lang/wgsl/ast/transform/manager.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 66 | #include "src/tint/lang/wgsl/ast/transform/multiplanar_external_texture.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 67 | #include "src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.h" |
| 68 | #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] | 69 | #include "src/tint/lang/wgsl/ast/transform/remove_continue_in_switch.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 70 | #include "src/tint/lang/wgsl/ast/transform/remove_phonies.h" |
| 71 | #include "src/tint/lang/wgsl/ast/transform/robustness.h" |
| 72 | #include "src/tint/lang/wgsl/ast/transform/simplify_pointers.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 73 | #include "src/tint/lang/wgsl/ast/transform/unshadow.h" |
| 74 | #include "src/tint/lang/wgsl/ast/transform/vectorize_scalar_matrix_initializers.h" |
| 75 | #include "src/tint/lang/wgsl/ast/transform/zero_init_workgroup_memory.h" |
| 76 | #include "src/tint/lang/wgsl/ast/variable_decl_statement.h" |
dan sinclair | a4c1735 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 77 | #include "src/tint/lang/wgsl/helpers/append_vector.h" |
| 78 | #include "src/tint/lang/wgsl/helpers/check_supported_extensions.h" |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 79 | #include "src/tint/lang/wgsl/sem/block_statement.h" |
| 80 | #include "src/tint/lang/wgsl/sem/call.h" |
| 81 | #include "src/tint/lang/wgsl/sem/function.h" |
| 82 | #include "src/tint/lang/wgsl/sem/member_accessor_expression.h" |
| 83 | #include "src/tint/lang/wgsl/sem/module.h" |
| 84 | #include "src/tint/lang/wgsl/sem/statement.h" |
| 85 | #include "src/tint/lang/wgsl/sem/struct.h" |
| 86 | #include "src/tint/lang/wgsl/sem/switch_statement.h" |
| 87 | #include "src/tint/lang/wgsl/sem/value_constructor.h" |
| 88 | #include "src/tint/lang/wgsl/sem/value_conversion.h" |
| 89 | #include "src/tint/lang/wgsl/sem/variable.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 90 | #include "src/tint/utils/containers/map.h" |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 91 | #include "src/tint/utils/ice/ice.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 92 | #include "src/tint/utils/macros/compiler.h" |
| 93 | #include "src/tint/utils/macros/defer.h" |
| 94 | #include "src/tint/utils/macros/scoped_assignment.h" |
| 95 | #include "src/tint/utils/rtti/switch.h" |
Ben Clayton | 16be11d | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 96 | #include "src/tint/utils/strconv/float_to_string.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 97 | #include "src/tint/utils/text/string.h" |
| 98 | #include "src/tint/utils/text/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 99 | |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 100 | using namespace tint::core::number_suffixes; // NOLINT |
| 101 | using namespace tint::core::fluent_types; // NOLINT |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 102 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 103 | namespace tint::hlsl::writer { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 104 | namespace { |
| 105 | |
| 106 | const char kTempNamePrefix[] = "tint_tmp"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 107 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 108 | const char* image_format_to_rwtexture_type(core::TexelFormat image_format) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 109 | switch (image_format) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 110 | case core::TexelFormat::kBgra8Unorm: |
| 111 | case core::TexelFormat::kRgba8Unorm: |
| 112 | case core::TexelFormat::kRgba8Snorm: |
| 113 | case core::TexelFormat::kRgba16Float: |
| 114 | case core::TexelFormat::kR32Float: |
| 115 | case core::TexelFormat::kRg32Float: |
| 116 | case core::TexelFormat::kRgba32Float: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 117 | return "float4"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 118 | case core::TexelFormat::kRgba8Uint: |
| 119 | case core::TexelFormat::kRgba16Uint: |
| 120 | case core::TexelFormat::kR32Uint: |
| 121 | case core::TexelFormat::kRg32Uint: |
| 122 | case core::TexelFormat::kRgba32Uint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 123 | return "uint4"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 124 | case core::TexelFormat::kRgba8Sint: |
| 125 | case core::TexelFormat::kRgba16Sint: |
| 126 | case core::TexelFormat::kR32Sint: |
| 127 | case core::TexelFormat::kRg32Sint: |
| 128 | case core::TexelFormat::kRgba32Sint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 129 | return "int4"; |
| 130 | default: |
| 131 | return nullptr; |
| 132 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 135 | void PrintF32(StringStream& out, float value) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 136 | if (std::isinf(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 137 | out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 138 | } else if (std::isnan(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 139 | out << "0.0f /* nan */"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 140 | } else { |
dan sinclair | 37e9d11 | 2023-11-20 13:18:28 +0000 | [diff] [blame] | 141 | out << tint::strconv::FloatToString(value) << "f"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 145 | void PrintF16(StringStream& out, float value) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 146 | if (std::isinf(value)) { |
| 147 | out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
| 148 | } else if (std::isnan(value)) { |
| 149 | out << "0.0h /* nan */"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 150 | } else { |
dan sinclair | 37e9d11 | 2023-11-20 13:18:28 +0000 | [diff] [blame] | 151 | out << tint::strconv::FloatToString(value) << "h"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 155 | // Helper for writing " : register(RX, spaceY)", where R is the register, X is |
| 156 | // the binding point binding value, and Y is the binding point group value. |
| 157 | struct RegisterAndSpace { |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 158 | RegisterAndSpace(char r, BindingPoint bp) : reg(r), binding_point(bp) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 159 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 160 | const char reg; |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 161 | BindingPoint const binding_point; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 164 | StringStream& operator<<(StringStream& s, const RegisterAndSpace& rs) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 165 | s << " : register(" << rs.reg << rs.binding_point.binding; |
| 166 | // Omit the space if it's 0, as it's the default. |
| 167 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better compatibility. |
| 168 | if (rs.binding_point.group == 0) { |
| 169 | s << ")"; |
| 170 | } else { |
| 171 | s << ", space" << rs.binding_point.group << ")"; |
| 172 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 173 | return s; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 176 | } // namespace |
| 177 | |
| 178 | SanitizedResult::SanitizedResult() = default; |
| 179 | SanitizedResult::~SanitizedResult() = default; |
| 180 | SanitizedResult::SanitizedResult(SanitizedResult&&) = default; |
| 181 | |
Ben Clayton | 5ed5cc4 | 2023-09-22 10:31:04 +0000 | [diff] [blame] | 182 | SanitizedResult Sanitize(const Program& in, const Options& options) { |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 183 | ast::transform::Manager manager; |
| 184 | ast::transform::DataMap data; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 185 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 186 | manager.Add<ast::transform::DisableUniformityAnalysis>(); |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 187 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 188 | // ExpandCompoundAssignment must come before BuiltinPolyfill |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 189 | manager.Add<ast::transform::ExpandCompoundAssignment>(); |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 190 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 191 | manager.Add<ast::transform::Unshadow>(); // Must come before DirectVariableAccess |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 192 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 193 | // LocalizeStructArrayAssignment must come after: |
| 194 | // * SimplifyPointers, because it assumes assignment to arrays in structs are |
| 195 | // done directly, not indirectly. |
| 196 | // TODO(crbug.com/tint/1340): See if we can get rid of the duplicate |
| 197 | // SimplifyPointers transform. Can't do it right now because |
| 198 | // LocalizeStructArrayAssignment introduces pointers. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 199 | manager.Add<ast::transform::SimplifyPointers>(); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 200 | manager.Add<LocalizeStructArrayAssignment>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 201 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 202 | manager.Add<ast::transform::PromoteSideEffectsToDecl>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 203 | |
| 204 | if (!options.disable_robustness) { |
Ben Clayton | 8525ff2 | 2023-03-06 21:05:01 +0000 | [diff] [blame] | 205 | // Robustness must come after PromoteSideEffectsToDecl |
| 206 | // Robustness must come before BuiltinPolyfill and CanonicalizeEntryPointIO |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 207 | manager.Add<ast::transform::Robustness>(); |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 208 | |
| 209 | ast::transform::Robustness::Config config = {}; |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 210 | config.bindings_ignored = std::unordered_set<BindingPoint>( |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 211 | options.bindings.ignored_by_robustness_transform.cbegin(), |
| 212 | options.bindings.ignored_by_robustness_transform.cend()); |
Jiawei Shao | 2e11963 | 2023-06-21 01:43:25 +0000 | [diff] [blame] | 213 | |
| 214 | // Direct3D guarantees to return zero for any resource that is accessed out of bounds, and |
| 215 | // according to the description of the assembly store_uav_typed, out of bounds addressing |
| 216 | // means nothing gets written to memory. |
| 217 | config.texture_action = ast::transform::Robustness::Action::kIgnore; |
| 218 | |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 219 | data.Add<ast::transform::Robustness::Config>(config); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 222 | ExternalTextureOptions external_texture_options{}; |
| 223 | RemapperData remapper_data{}; |
| 224 | ArrayLengthFromUniformOptions array_length_from_uniform_options{}; |
| 225 | PopulateBindingRelatedOptions(options, remapper_data, external_texture_options, |
| 226 | array_length_from_uniform_options); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 227 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 228 | manager.Add<ast::transform::BindingRemapper>(); |
dan sinclair | 1521550 | 2023-09-26 11:31:47 +0000 | [diff] [blame] | 229 | // D3D11 and 12 registers like `t3` and `c3` have the same bindingOffset number in |
| 230 | // the remapping but should not be considered a collision because they have |
| 231 | // different types. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 232 | data.Add<ast::transform::BindingRemapper::Remappings>( |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 233 | remapper_data, options.bindings.access_controls, /* allow_collisions */ true); |
| 234 | |
| 235 | // Note: it is more efficient for MultiplanarExternalTexture to come after Robustness |
| 236 | // MultiplanarExternalTexture must come after BindingRemapper |
| 237 | data.Add<ast::transform::MultiplanarExternalTexture::NewBindingPoints>( |
| 238 | external_texture_options.bindings_map, /* may_collide */ true); |
| 239 | manager.Add<ast::transform::MultiplanarExternalTexture>(); |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 240 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 241 | { // Builtin polyfills |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 242 | ast::transform::BuiltinPolyfill::Builtins polyfills; |
| 243 | polyfills.acosh = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | d23f296 | 2022-06-28 15:27:44 +0000 | [diff] [blame] | 244 | polyfills.asinh = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 245 | polyfills.atanh = ast::transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 02f04d9 | 2022-11-03 19:15:17 +0000 | [diff] [blame] | 246 | polyfills.bitshift_modulo = true; |
Ben Clayton | 6dbb463 | 2022-10-31 17:54:49 +0000 | [diff] [blame] | 247 | polyfills.clamp_int = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 248 | // TODO(crbug.com/tint/1449): Some of these can map to HLSL's `firstbitlow` |
| 249 | // and `firstbithigh`. |
Ben Clayton | cc3f851 | 2023-03-09 21:26:12 +0000 | [diff] [blame] | 250 | polyfills.conv_f32_to_iu32 = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 251 | polyfills.count_leading_zeros = true; |
| 252 | polyfills.count_trailing_zeros = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 253 | polyfills.extract_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 254 | polyfills.first_leading_bit = true; |
| 255 | polyfills.first_trailing_bit = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 256 | polyfills.insert_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
Jiawei Shao | 825f626 | 2024-01-08 10:05:27 +0000 | [diff] [blame] | 257 | polyfills.int_div_mod = !options.disable_polyfill_integer_div_mod; |
Antonio Maiorano | ee665a4 | 2023-02-14 16:12:59 +0000 | [diff] [blame] | 258 | polyfills.precise_float_mod = true; |
Zhaoming Jiang | 04529be | 2023-02-27 02:59:50 +0000 | [diff] [blame] | 259 | polyfills.reflect_vec2_f32 = options.polyfill_reflect_vec2_f32; |
Ben Clayton | c4ebf2c | 2022-09-22 22:59:16 +0000 | [diff] [blame] | 260 | polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true; |
James Price | 128980f | 2023-01-06 02:25:06 +0000 | [diff] [blame] | 261 | polyfills.workgroup_uniform_load = true; |
Jiawei Shao | 7c0a801 | 2023-11-29 00:25:56 +0000 | [diff] [blame] | 262 | polyfills.dot_4x8_packed = options.polyfill_dot_4x8_packed; |
Jiawei Shao | dd81723 | 2024-01-12 08:10:46 +0000 | [diff] [blame] | 263 | polyfills.pack_unpack_4x8 = options.polyfill_pack_unpack_4x8; |
| 264 | // Currently Pack4xU8Clamp() must be polyfilled because on latest DXC pack_clamp_u8() |
| 265 | // receives an int32_t4 as its input. |
| 266 | // See https://github.com/microsoft/DirectXShaderCompiler/issues/5091 for more details. |
| 267 | polyfills.pack_4xu8_clamp = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 268 | data.Add<ast::transform::BuiltinPolyfill::Config>(polyfills); |
| 269 | manager.Add<ast::transform::BuiltinPolyfill>(); // Must come before DirectVariableAccess |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 270 | } |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 271 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 272 | manager.Add<ast::transform::DirectVariableAccess>(); |
Antonio Maiorano | fa00fe9 | 2023-05-03 15:30:54 +0000 | [diff] [blame] | 273 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 274 | if (!options.disable_workgroup_init) { |
| 275 | // ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as |
| 276 | // ZeroInitWorkgroupMemory may inject new builtin parameters. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 277 | manager.Add<ast::transform::ZeroInitWorkgroupMemory>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 278 | } |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 279 | |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 280 | { |
| 281 | PixelLocal::Config cfg; |
| 282 | for (auto it : options.pixel_local_options.attachments) { |
| 283 | cfg.pls_member_to_rov_reg.Add(it.first, it.second); |
| 284 | } |
| 285 | for (auto it : options.pixel_local_options.attachment_formats) { |
| 286 | core::TexelFormat format = core::TexelFormat::kUndefined; |
| 287 | switch (it.second) { |
| 288 | case PixelLocalOptions::TexelFormat::kR32Sint: |
| 289 | format = core::TexelFormat::kR32Sint; |
| 290 | break; |
| 291 | case PixelLocalOptions::TexelFormat::kR32Uint: |
| 292 | format = core::TexelFormat::kR32Uint; |
| 293 | break; |
| 294 | case PixelLocalOptions::TexelFormat::kR32Float: |
| 295 | format = core::TexelFormat::kR32Float; |
| 296 | break; |
| 297 | default: |
| 298 | TINT_ICE() << "missing texel format for pixel local storage attachment"; |
| 299 | return SanitizedResult(); |
| 300 | } |
| 301 | cfg.pls_member_to_rov_format.Add(it.first, format); |
| 302 | } |
| 303 | cfg.rov_group_index = options.pixel_local_options.pixel_local_group_index; |
| 304 | data.Add<PixelLocal::Config>(cfg); |
| 305 | manager.Add<PixelLocal>(); |
| 306 | } |
| 307 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 308 | // CanonicalizeEntryPointIO must come after Robustness |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 309 | manager.Add<ast::transform::CanonicalizeEntryPointIO>(); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 310 | |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame] | 311 | if (options.truncate_interstage_variables) { |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 312 | // 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] | 313 | // being used in the next stage. Still, HLSL compiler register mismatch could happen, if |
| 314 | // there's builtin inputs used in the next stage. So we still run |
| 315 | // TruncateInterstageVariables transform. |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 316 | |
| 317 | // TruncateInterstageVariables itself will skip when interstage_locations matches exactly |
| 318 | // with the current stage output. |
| 319 | |
| 320 | // Build the config for internal TruncateInterstageVariables transform. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 321 | TruncateInterstageVariables::Config truncate_interstage_variables_cfg; |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 322 | truncate_interstage_variables_cfg.interstage_locations = |
| 323 | std::move(options.interstage_locations); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 324 | manager.Add<TruncateInterstageVariables>(); |
| 325 | data.Add<TruncateInterstageVariables::Config>(std::move(truncate_interstage_variables_cfg)); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 326 | } |
| 327 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 328 | // NumWorkgroupsFromUniform must come after CanonicalizeEntryPointIO, as it |
| 329 | // assumes that num_workgroups builtins only appear as struct members and are |
| 330 | // only accessed directly via member accessors. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 331 | manager.Add<NumWorkgroupsFromUniform>(); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 332 | manager.Add<ast::transform::VectorizeScalarMatrixInitializers>(); |
| 333 | manager.Add<ast::transform::SimplifyPointers>(); |
| 334 | manager.Add<ast::transform::RemovePhonies>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 335 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 336 | // Build the config for the internal ArrayLengthFromUniform transform. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 337 | ast::transform::ArrayLengthFromUniform::Config array_length_from_uniform_cfg( |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 338 | array_length_from_uniform_options.ubo_binding); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 339 | array_length_from_uniform_cfg.bindpoint_to_size_index = |
Antonio Maiorano | 26a41b9 | 2024-02-05 21:36:47 +0000 | [diff] [blame] | 340 | std::move(array_length_from_uniform_options.bindpoint_to_size_index); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 341 | |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 342 | // DemoteToHelper must come after CanonicalizeEntryPointIO, PromoteSideEffectsToDecl, and |
| 343 | // ExpandCompoundAssignment. |
| 344 | // 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] | 345 | manager.Add<ast::transform::DemoteToHelper>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 346 | |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 347 | // ArrayLengthFromUniform must come after SimplifyPointers as it assumes that the form of the |
| 348 | // array length argument is &var.array. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 349 | manager.Add<ast::transform::ArrayLengthFromUniform>(); |
| 350 | data.Add<ast::transform::ArrayLengthFromUniform::Config>( |
| 351 | std::move(array_length_from_uniform_cfg)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 352 | // DecomposeMemoryAccess must come after: |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 353 | // * SimplifyPointers, as we cannot take the address of calls to |
| 354 | // DecomposeMemoryAccess::Intrinsic and we need to fold away the address-of and dereferences |
| 355 | // of `*(&(intrinsic_load()))` expressions. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 356 | // * RemovePhonies, as phonies can be assigned a pointer to a |
| 357 | // non-constructible buffer, or dynamic array, which DMA cannot cope with. |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 358 | manager.Add<DecomposeMemoryAccess>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 359 | // CalculateArrayLength must come after DecomposeMemoryAccess, as |
| 360 | // DecomposeMemoryAccess special-cases the arrayLength() intrinsic, which |
| 361 | // will be transformed by CalculateArrayLength |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 362 | manager.Add<CalculateArrayLength>(); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 363 | manager.Add<ast::transform::PromoteInitializersToLet>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 364 | |
dan sinclair | 997bc01 | 2024-02-22 02:08:41 +0000 | [diff] [blame] | 365 | manager.Add<ast::transform::RemoveContinueInSwitch>(); |
Antonio Maiorano | b349710 | 2022-03-31 15:02:25 +0000 | [diff] [blame] | 366 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 367 | manager.Add<ast::transform::AddEmptyEntryPoint>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 368 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 369 | data.Add<ast::transform::CanonicalizeEntryPointIO::Config>( |
| 370 | ast::transform::CanonicalizeEntryPointIO::ShaderStyle::kHlsl); |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 371 | data.Add<NumWorkgroupsFromUniform::Config>(options.root_constant_binding_point); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 372 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 373 | SanitizedResult result; |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 374 | ast::transform::DataMap outputs; |
James Price | 0e6534e | 2023-05-17 01:21:45 +0000 | [diff] [blame] | 375 | result.program = manager.Run(in, data, outputs); |
| 376 | if (auto* res = outputs.Get<ast::transform::ArrayLengthFromUniform::Result>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 377 | result.used_array_length_from_uniform_indices = std::move(res->used_size_indices); |
| 378 | } |
| 379 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Ben Clayton | 5ed5cc4 | 2023-09-22 10:31:04 +0000 | [diff] [blame] | 382 | ASTPrinter::ASTPrinter(const Program& program) : builder_(ProgramBuilder::Wrap(program)) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 383 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 384 | ASTPrinter::~ASTPrinter() = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 385 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 386 | bool ASTPrinter::Generate() { |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 387 | if (!tint::wgsl::CheckSupportedExtensions( |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 388 | "HLSL", builder_.AST(), diagnostics_, |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 389 | Vector{ |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 390 | wgsl::Extension::kChromiumDisableUniformityAnalysis, |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 391 | wgsl::Extension::kChromiumExperimentalPushConstant, |
Ben Clayton | 1165389 | 2023-09-19 19:15:59 +0000 | [diff] [blame] | 392 | wgsl::Extension::kChromiumExperimentalSubgroups, |
| 393 | wgsl::Extension::kF16, |
| 394 | wgsl::Extension::kChromiumInternalDualSourceBlending, |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 395 | wgsl::Extension::kChromiumExperimentalPixelLocal, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 396 | })) { |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 400 | const tint::TypeInfo* last_kind = nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 401 | size_t last_padding_line = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 402 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 403 | auto* mod = builder_.Sem().Module(); |
| 404 | for (auto* decl : mod->DependencyOrderedDeclarations()) { |
James Price | 631aaa3 | 2023-11-02 15:03:02 +0000 | [diff] [blame] | 405 | if (decl->IsAnyOf<ast::Alias, ast::DiagnosticDirective, ast::Enable, ast::Requires, |
| 406 | ast::ConstAssert>()) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 407 | continue; // These are not emitted. |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 408 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 409 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 410 | // Emit a new line between declarations if the type of declaration has |
| 411 | // changed, or we're about to emit a function |
| 412 | auto* kind = &decl->TypeInfo(); |
| 413 | if (current_buffer_->lines.size() != last_padding_line) { |
| 414 | if (last_kind && (last_kind != kind || decl->Is<ast::Function>())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 415 | Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 416 | last_padding_line = current_buffer_->lines.size(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 417 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 418 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 419 | last_kind = kind; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 420 | |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 421 | global_insertion_point_ = current_buffer_->lines.size(); |
| 422 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 423 | bool ok = Switch( |
| 424 | decl, |
| 425 | [&](const ast::Variable* global) { // |
| 426 | return EmitGlobalVariable(global); |
| 427 | }, |
| 428 | [&](const ast::Struct* str) { |
| 429 | auto* ty = builder_.Sem().Get(str); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 430 | auto address_space_uses = ty->AddressSpaceUsage(); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 431 | if (address_space_uses.Count() != |
| 432 | ((address_space_uses.Contains(core::AddressSpace::kStorage) ? 1u : 0u) + |
| 433 | (address_space_uses.Contains(core::AddressSpace::kUniform) ? 1u : 0u))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 434 | // The structure is used as something other than a storage buffer or |
| 435 | // uniform buffer, so it needs to be emitted. |
| 436 | // Storage buffer are read and written to via a ByteAddressBuffer |
| 437 | // instead of true structure. |
| 438 | // Structures used as uniform buffer are read from an array of |
| 439 | // vectors instead of true structure. |
| 440 | return EmitStructType(current_buffer_, ty); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 441 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 442 | return true; |
| 443 | }, |
| 444 | [&](const ast::Function* func) { |
| 445 | if (func->IsEntryPoint()) { |
| 446 | return EmitEntryPointFunction(func); |
| 447 | } |
| 448 | return EmitFunction(func); |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 449 | }, // |
| 450 | TINT_ICE_ON_NO_MATCH); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 451 | |
| 452 | if (!ok) { |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (!helpers_.lines.empty()) { |
| 458 | current_buffer_->Insert(helpers_, 0, 0); |
| 459 | } |
| 460 | |
| 461 | return true; |
| 462 | } |
| 463 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 464 | bool ASTPrinter::EmitDynamicVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 465 | const core::type::Vector* vec) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 466 | auto name = tint::GetOrAdd(dynamic_vector_write_, vec, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 467 | std::string fn = UniqueIdentifier("set_vector_element"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 468 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 469 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 470 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 471 | if (!EmitTypeAndName(out, vec, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 472 | "vec")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 473 | return ""; |
| 474 | } |
| 475 | out << ", int idx, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 476 | if (!EmitTypeAndName(out, vec->type(), core::AddressSpace::kUndefined, |
| 477 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 478 | return ""; |
| 479 | } |
| 480 | out << ") {"; |
| 481 | } |
| 482 | { |
| 483 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 484 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 485 | switch (vec->Width()) { |
| 486 | case 2: |
| 487 | out << "vec = (idx.xx == int2(0, 1)) ? val.xx : vec;"; |
| 488 | break; |
| 489 | case 3: |
| 490 | out << "vec = (idx.xxx == int3(0, 1, 2)) ? val.xxx : vec;"; |
| 491 | break; |
| 492 | case 4: |
| 493 | out << "vec = (idx.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : vec;"; |
| 494 | break; |
| 495 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 496 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 497 | break; |
| 498 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 499 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 500 | Line(&helpers_) << "}"; |
| 501 | Line(&helpers_); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 502 | return fn; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 503 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 504 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 505 | if (name.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 506 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 507 | } |
| 508 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 509 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 510 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 511 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 512 | out << name << "("; |
| 513 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 514 | return false; |
| 515 | } |
| 516 | out << ", "; |
| 517 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 518 | return false; |
| 519 | } |
| 520 | out << ", "; |
| 521 | if (!EmitExpression(out, stmt->rhs)) { |
| 522 | return false; |
| 523 | } |
| 524 | out << ");"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 525 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 526 | return true; |
| 527 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 528 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 529 | bool ASTPrinter::EmitDynamicMatrixVectorAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 530 | const core::type::Matrix* mat) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 531 | auto name = tint::GetOrAdd(dynamic_matrix_vector_write_, mat, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 532 | std::string fn = UniqueIdentifier("set_matrix_column"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 533 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 534 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 535 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 536 | if (!EmitTypeAndName(out, mat, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 537 | "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 538 | return ""; |
| 539 | } |
| 540 | out << ", int col, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 541 | if (!EmitTypeAndName(out, mat->ColumnType(), core::AddressSpace::kUndefined, |
| 542 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 543 | return ""; |
| 544 | } |
| 545 | out << ") {"; |
| 546 | } |
| 547 | { |
| 548 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 549 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 550 | { |
| 551 | ScopedIndent si2(&helpers_); |
| 552 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 553 | Line(&helpers_) << "case " << i << ": mat[" << i << "] = val; break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 556 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 557 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 558 | Line(&helpers_) << "}"; |
| 559 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 560 | return fn; |
| 561 | }); |
| 562 | |
| 563 | if (name.empty()) { |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 568 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 569 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 570 | out << name << "("; |
| 571 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 572 | return false; |
| 573 | } |
| 574 | out << ", "; |
| 575 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 576 | return false; |
| 577 | } |
| 578 | out << ", "; |
| 579 | if (!EmitExpression(out, stmt->rhs)) { |
| 580 | return false; |
| 581 | } |
| 582 | out << ");"; |
| 583 | |
| 584 | return true; |
| 585 | } |
| 586 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 587 | bool ASTPrinter::EmitDynamicMatrixScalarAssignment(const ast::AssignmentStatement* stmt, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 588 | const core::type::Matrix* mat) { |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 589 | auto* lhs_row_access = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 590 | auto* lhs_col_access = lhs_row_access->object->As<ast::IndexAccessorExpression>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 591 | |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 592 | auto name = tint::GetOrAdd(dynamic_matrix_scalar_write_, mat, [&]() -> std::string { |
Ben Clayton | 80144d2 | 2024-01-26 23:45:49 +0000 | [diff] [blame] | 593 | std::string fn = UniqueIdentifier("set_matrix_scalar"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 594 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 595 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 596 | out << "void " << fn << "(inout "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 597 | if (!EmitTypeAndName(out, mat, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 598 | "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 599 | return ""; |
| 600 | } |
| 601 | out << ", int col, int row, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 602 | if (!EmitTypeAndName(out, mat->type(), core::AddressSpace::kUndefined, |
| 603 | core::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 604 | return ""; |
| 605 | } |
| 606 | out << ") {"; |
| 607 | } |
| 608 | { |
| 609 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 610 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 611 | { |
| 612 | ScopedIndent si2(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 613 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 614 | Line(&helpers_) << "case " << i << ":"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 615 | { |
| 616 | auto vec_name = "mat[" + std::to_string(i) + "]"; |
| 617 | ScopedIndent si3(&helpers_); |
| 618 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 619 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 620 | switch (mat->rows()) { |
| 621 | case 2: |
| 622 | out << vec_name |
| 623 | << " = (row.xx == int2(0, 1)) ? val.xx : " << vec_name |
| 624 | << ";"; |
| 625 | break; |
| 626 | case 3: |
| 627 | out << vec_name |
| 628 | << " = (row.xxx == int3(0, 1, 2)) ? val.xxx : " << vec_name |
| 629 | << ";"; |
| 630 | break; |
| 631 | case 4: |
| 632 | out << vec_name |
| 633 | << " = (row.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : " |
| 634 | << vec_name << ";"; |
| 635 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 636 | default: { |
| 637 | auto* vec = TypeOf(lhs_row_access->object) |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 638 | ->UnwrapPtrOrRef() |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 639 | ->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 640 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 641 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 642 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 645 | Line(&helpers_) << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 649 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 650 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 651 | Line(&helpers_) << "}"; |
| 652 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 653 | return fn; |
| 654 | }); |
| 655 | |
| 656 | if (name.empty()) { |
| 657 | return false; |
| 658 | } |
| 659 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 660 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 661 | out << name << "("; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 662 | if (!EmitExpression(out, lhs_col_access->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 663 | return false; |
| 664 | } |
| 665 | out << ", "; |
| 666 | if (!EmitExpression(out, lhs_col_access->index)) { |
| 667 | return false; |
| 668 | } |
| 669 | out << ", "; |
| 670 | if (!EmitExpression(out, lhs_row_access->index)) { |
| 671 | return false; |
| 672 | } |
| 673 | out << ", "; |
| 674 | if (!EmitExpression(out, stmt->rhs)) { |
| 675 | return false; |
| 676 | } |
| 677 | out << ");"; |
| 678 | |
| 679 | return true; |
| 680 | } |
| 681 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 682 | bool ASTPrinter::EmitIndexAccessor(StringStream& out, const ast::IndexAccessorExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 683 | if (!EmitExpression(out, expr->object)) { |
| 684 | return false; |
| 685 | } |
| 686 | out << "["; |
| 687 | |
| 688 | if (!EmitExpression(out, expr->index)) { |
| 689 | return false; |
| 690 | } |
| 691 | out << "]"; |
| 692 | |
| 693 | return true; |
| 694 | } |
| 695 | |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 696 | bool ASTPrinter::EmitBitcastCall(StringStream& out, const ast::CallExpression* call) { |
| 697 | auto* arg = call->args[0]; |
| 698 | auto* src_type = TypeOf(arg)->UnwrapRef(); |
| 699 | auto* dst_type = TypeOf(call); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 700 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 701 | auto* src_el_type = src_type->DeepestElement(); |
| 702 | auto* dst_el_type = dst_type->DeepestElement(); |
| 703 | |
| 704 | if (!dst_el_type->is_integer_scalar() && !dst_el_type->is_float_scalar()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 705 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 706 | << "Unable to do bitcast to type " << dst_el_type->FriendlyName(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 710 | // Handle identity bitcast. |
| 711 | if (src_type == dst_type) { |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 712 | return EmitExpression(out, arg); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Handle the f16 types using polyfill functions |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 716 | 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] | 717 | auto f16_bitcast_polyfill = [&]() { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 718 | if (src_el_type->Is<core::type::F16>()) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 719 | // Source type must be vec2<f16> or vec4<f16>, since type f16 and vec3<f16> can only |
| 720 | // have identity bitcast. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 721 | auto* src_vec = src_type->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 722 | TINT_ASSERT(src_vec); |
| 723 | TINT_ASSERT(((src_vec->Width() == 2u) || (src_vec->Width() == 4u))); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 724 | |
| 725 | // Bitcast f16 types to others by converting the given f16 value to f32 and call |
| 726 | // f32tof16 to get the bits. This should be safe, because the convertion is precise |
| 727 | // for finite and infinite f16 value as they are exactly representable by f32, and |
| 728 | // WGSL spec allow any result if f16 value is NaN. |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 729 | return tint::GetOrAdd( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 730 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 731 | TextBuffer b; |
| 732 | TINT_DEFER(helpers_.Append(b)); |
| 733 | |
| 734 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_from_f16")); |
| 735 | { |
| 736 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 737 | if (!EmitTypeAndName(decl, dst_type, core::AddressSpace::kUndefined, |
| 738 | core::Access::kUndefined, fn_name)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 739 | return ""; |
| 740 | } |
| 741 | { |
| 742 | ScopedParen sp(decl); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 743 | if (!EmitTypeAndName(decl, src_type, core::AddressSpace::kUndefined, |
| 744 | core::Access::kUndefined, "src")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 745 | return ""; |
| 746 | } |
| 747 | } |
| 748 | decl << " {"; |
| 749 | } |
| 750 | { |
| 751 | ScopedIndent si(&b); |
| 752 | { |
| 753 | Line(&b) << "uint" << src_vec->Width() << " r = f32tof16(float" |
| 754 | << src_vec->Width() << "(src));"; |
| 755 | |
| 756 | { |
| 757 | auto s = Line(&b); |
| 758 | s << "return as"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 759 | if (!EmitType(s, dst_el_type, core::AddressSpace::kUndefined, |
| 760 | core::Access::kReadWrite, "")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 761 | return ""; |
| 762 | } |
| 763 | s << "("; |
| 764 | switch (src_vec->Width()) { |
| 765 | case 2: { |
| 766 | s << "uint((r.x & 0xffff) | ((r.y & 0xffff) << 16))"; |
| 767 | break; |
| 768 | } |
| 769 | case 4: { |
| 770 | s << "uint2((r.x & 0xffff) | ((r.y & 0xffff) << 16), " |
| 771 | "(r.z & 0xffff) | ((r.w & 0xffff) << 16))"; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | s << ");"; |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | Line(&b) << "}"; |
| 780 | Line(&b); |
| 781 | return fn_name; |
| 782 | }); |
| 783 | } else { |
| 784 | // Destination type must be vec2<f16> or vec4<f16>. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 785 | auto* dst_vec = dst_type->As<core::type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 786 | TINT_ASSERT((dst_vec && ((dst_vec->Width() == 2u) || (dst_vec->Width() == 4u)) && |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 787 | dst_el_type->Is<core::type::F16>())); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 788 | // Source type must be f32/i32/u32 or vec2<f32/i32/u32>. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 789 | auto* src_vec = src_type->As<core::type::Vector>(); |
| 790 | TINT_ASSERT( |
| 791 | (src_type->IsAnyOf<core::type::I32, core::type::U32, core::type::F32>() || |
| 792 | (src_vec && src_vec->Width() == 2u && |
| 793 | 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] | 794 | std::string src_type_suffix = (src_vec ? "2" : ""); |
| 795 | |
| 796 | // Bitcast other types to f16 types by reinterpreting their bits as f16 using |
| 797 | // f16tof32, and convert the result f32 to f16. This should be safe, because the |
| 798 | // convertion is precise for finite and infinite f16 result value as they are |
| 799 | // exactly representable by f32, and WGSL spec allow any result if f16 result value |
| 800 | // would be NaN. |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 801 | return tint::GetOrAdd( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 802 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 803 | TextBuffer b; |
| 804 | TINT_DEFER(helpers_.Append(b)); |
| 805 | |
| 806 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_to_f16")); |
| 807 | { |
| 808 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 809 | if (!EmitTypeAndName(decl, dst_type, core::AddressSpace::kUndefined, |
| 810 | core::Access::kUndefined, fn_name)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 811 | return ""; |
| 812 | } |
| 813 | { |
| 814 | ScopedParen sp(decl); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 815 | if (!EmitTypeAndName(decl, src_type, core::AddressSpace::kUndefined, |
| 816 | core::Access::kUndefined, "src")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 817 | return ""; |
| 818 | } |
| 819 | } |
| 820 | decl << " {"; |
| 821 | } |
| 822 | { |
| 823 | ScopedIndent si(&b); |
| 824 | { |
| 825 | // Convert the source to uint for f16tof32. |
| 826 | Line(&b) << "uint" << src_type_suffix << " v = asuint(src);"; |
| 827 | // Reinterprete the low 16 bits and high 16 bits |
| 828 | Line(&b) << "float" << src_type_suffix |
| 829 | << " t_low = f16tof32(v & 0xffff);"; |
| 830 | Line(&b) << "float" << src_type_suffix |
| 831 | << " t_high = f16tof32((v >> 16) & 0xffff);"; |
| 832 | // Construct the result f16 vector |
| 833 | { |
| 834 | auto s = Line(&b); |
| 835 | s << "return "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 836 | if (!EmitType(s, dst_type, core::AddressSpace::kUndefined, |
| 837 | core::Access::kReadWrite, "")) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 838 | return ""; |
| 839 | } |
| 840 | s << "("; |
| 841 | switch (dst_vec->Width()) { |
| 842 | case 2: { |
| 843 | s << "t_low.x, t_high.x"; |
| 844 | break; |
| 845 | } |
| 846 | case 4: { |
| 847 | s << "t_low.x, t_high.x, t_low.y, t_high.y"; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | s << ");"; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | Line(&b) << "}"; |
| 856 | Line(&b); |
| 857 | return fn_name; |
| 858 | }); |
| 859 | } |
| 860 | }; |
| 861 | |
| 862 | // Get or create the polyfill |
| 863 | auto fn = f16_bitcast_polyfill(); |
| 864 | if (fn.empty()) { |
| 865 | return false; |
| 866 | } |
| 867 | // Call the polyfill |
| 868 | out << fn; |
| 869 | { |
| 870 | ScopedParen sp(out); |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 871 | if (!EmitExpression(out, arg)) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | return true; |
| 877 | } |
| 878 | |
| 879 | // Otherwise, bitcasting between non-f16 types. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 880 | 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] | 881 | out << "as"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 882 | if (!EmitType(out, dst_el_type, core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 883 | return false; |
| 884 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 885 | out << "("; |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 886 | if (!EmitExpression(out, arg)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 887 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 888 | } |
| 889 | out << ")"; |
| 890 | return true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 891 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 892 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 893 | bool ASTPrinter::EmitAssign(const ast::AssignmentStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 894 | if (auto* lhs_access = stmt->lhs->As<ast::IndexAccessorExpression>()) { |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 895 | auto validate_obj_not_pointer = [&](const core::type::Type* object_ty) { |
| 896 | if (TINT_UNLIKELY(object_ty->Is<core::type::Pointer>())) { |
| 897 | TINT_ICE() << "lhs of index accessor should not be a pointer. These should have " |
| 898 | "been removed by transforms such as SimplifyPointers, " |
| 899 | "DecomposeMemoryAccess, and DirectVariableAccess"; |
| 900 | return false; |
| 901 | } |
| 902 | return true; |
| 903 | }; |
| 904 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 905 | // BUG(crbug.com/tint/1333): work around assignment of scalar to matrices |
| 906 | // with at least one dynamic index |
| 907 | if (auto* lhs_sub_access = lhs_access->object->As<ast::IndexAccessorExpression>()) { |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 908 | const auto* lhs_sub_access_type = TypeOf(lhs_sub_access->object); |
| 909 | if (!validate_obj_not_pointer(lhs_sub_access_type)) { |
| 910 | return false; |
| 911 | } |
| 912 | if (auto* mat = lhs_sub_access_type->UnwrapRef()->As<core::type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 913 | auto* rhs_row_idx_sem = builder_.Sem().GetVal(lhs_access->index); |
| 914 | auto* rhs_col_idx_sem = builder_.Sem().GetVal(lhs_sub_access->index); |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 915 | if (!rhs_row_idx_sem->ConstantValue() || !rhs_col_idx_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 916 | return EmitDynamicMatrixScalarAssignment(stmt, mat); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | // BUG(crbug.com/tint/1333): work around assignment of vector to matrices |
| 921 | // with dynamic indices |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 922 | const auto* lhs_access_type = TypeOf(lhs_access->object); |
| 923 | if (!validate_obj_not_pointer(lhs_access_type)) { |
| 924 | return false; |
| 925 | } |
| 926 | if (auto* mat = lhs_access_type->UnwrapRef()->As<core::type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 927 | auto* lhs_index_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 928 | if (!lhs_index_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 929 | return EmitDynamicMatrixVectorAssignment(stmt, mat); |
| 930 | } |
| 931 | } |
| 932 | // BUG(crbug.com/tint/534): work around assignment to vectors with dynamic |
| 933 | // indices |
Antonio Maiorano | 08d9279 | 2024-01-11 20:51:50 +0000 | [diff] [blame] | 934 | if (auto* vec = lhs_access_type->UnwrapRef()->As<core::type::Vector>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 935 | auto* rhs_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 936 | if (!rhs_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 937 | return EmitDynamicVectorAssignment(stmt, vec); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 942 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 943 | if (!EmitExpression(out, stmt->lhs)) { |
| 944 | return false; |
| 945 | } |
| 946 | out << " = "; |
| 947 | if (!EmitExpression(out, stmt->rhs)) { |
| 948 | return false; |
| 949 | } |
| 950 | out << ";"; |
| 951 | return true; |
| 952 | } |
| 953 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 954 | bool ASTPrinter::EmitBinary(StringStream& out, const ast::BinaryExpression* expr) { |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 955 | if (expr->op == core::BinaryOp::kLogicalAnd || expr->op == core::BinaryOp::kLogicalOr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 956 | auto name = UniqueIdentifier(kTempNamePrefix); |
| 957 | |
| 958 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 959 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 960 | pre << "bool " << name << " = "; |
| 961 | if (!EmitExpression(pre, expr->lhs)) { |
| 962 | return false; |
| 963 | } |
| 964 | pre << ";"; |
| 965 | } |
| 966 | |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 967 | if (expr->op == core::BinaryOp::kLogicalOr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 968 | Line() << "if (!" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 969 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 970 | Line() << "if (" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | { |
| 974 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 975 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 976 | pre << name << " = "; |
| 977 | if (!EmitExpression(pre, expr->rhs)) { |
| 978 | return false; |
| 979 | } |
| 980 | pre << ";"; |
| 981 | } |
| 982 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 983 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 984 | |
| 985 | out << "(" << name << ")"; |
| 986 | return true; |
| 987 | } |
| 988 | |
| 989 | auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); |
| 990 | auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); |
| 991 | // Multiplying by a matrix requires the use of `mul` in order to get the |
| 992 | // type of multiply we desire. |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 993 | if (expr->op == core::BinaryOp::kMultiply && |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 994 | ((lhs_type->Is<core::type::Vector>() && rhs_type->Is<core::type::Matrix>()) || |
| 995 | (lhs_type->Is<core::type::Matrix>() && rhs_type->Is<core::type::Vector>()) || |
| 996 | (lhs_type->Is<core::type::Matrix>() && rhs_type->Is<core::type::Matrix>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 997 | // Matrices are transposed, so swap LHS and RHS. |
| 998 | out << "mul("; |
| 999 | if (!EmitExpression(out, expr->rhs)) { |
| 1000 | return false; |
| 1001 | } |
| 1002 | out << ", "; |
| 1003 | if (!EmitExpression(out, expr->lhs)) { |
| 1004 | return false; |
| 1005 | } |
| 1006 | out << ")"; |
| 1007 | |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1011 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1012 | |
| 1013 | if (!EmitExpression(out, expr->lhs)) { |
| 1014 | return false; |
| 1015 | } |
| 1016 | out << " "; |
| 1017 | |
| 1018 | switch (expr->op) { |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1019 | case core::BinaryOp::kAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1020 | out << "&"; |
| 1021 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1022 | case core::BinaryOp::kOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1023 | out << "|"; |
| 1024 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1025 | case core::BinaryOp::kXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1026 | out << "^"; |
| 1027 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1028 | case core::BinaryOp::kLogicalAnd: |
| 1029 | case core::BinaryOp::kLogicalOr: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1030 | // These are both handled above. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1031 | TINT_UNREACHABLE(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1032 | return false; |
| 1033 | } |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1034 | case core::BinaryOp::kEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1035 | out << "=="; |
| 1036 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1037 | case core::BinaryOp::kNotEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1038 | out << "!="; |
| 1039 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1040 | case core::BinaryOp::kLessThan: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1041 | out << "<"; |
| 1042 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1043 | case core::BinaryOp::kGreaterThan: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1044 | out << ">"; |
| 1045 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1046 | case core::BinaryOp::kLessThanEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1047 | out << "<="; |
| 1048 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1049 | case core::BinaryOp::kGreaterThanEqual: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1050 | out << ">="; |
| 1051 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1052 | case core::BinaryOp::kShiftLeft: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1053 | out << "<<"; |
| 1054 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1055 | case core::BinaryOp::kShiftRight: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1056 | // TODO(dsinclair): MSL is based on C++14, and >> in C++14 has |
| 1057 | // implementation-defined behaviour for negative LHS. We may have to |
| 1058 | // generate extra code to implement WGSL-specified behaviour for negative |
| 1059 | // LHS. |
| 1060 | out << R"(>>)"; |
| 1061 | break; |
| 1062 | |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1063 | case core::BinaryOp::kAdd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1064 | out << "+"; |
| 1065 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1066 | case core::BinaryOp::kSubtract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1067 | out << "-"; |
| 1068 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1069 | case core::BinaryOp::kMultiply: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1070 | out << "*"; |
| 1071 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1072 | case core::BinaryOp::kDivide: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1073 | out << "/"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1074 | break; |
Ben Clayton | edc51ab | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 1075 | case core::BinaryOp::kModulo: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1076 | out << "%"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1077 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1078 | } |
| 1079 | out << " "; |
| 1080 | |
| 1081 | if (!EmitExpression(out, expr->rhs)) { |
| 1082 | return false; |
| 1083 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1084 | |
| 1085 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1088 | bool ASTPrinter::EmitStatements(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1089 | for (auto* s : stmts) { |
| 1090 | if (!EmitStatement(s)) { |
| 1091 | return false; |
| 1092 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1093 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1094 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1097 | bool ASTPrinter::EmitStatementsWithIndent(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1098 | ScopedIndent si(this); |
| 1099 | return EmitStatements(stmts); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1102 | bool ASTPrinter::EmitBlock(const ast::BlockStatement* stmt) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1103 | Line() << "{"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1104 | if (!EmitStatementsWithIndent(stmt->statements)) { |
| 1105 | return false; |
| 1106 | } |
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 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1111 | bool ASTPrinter::EmitBreak(const ast::BreakStatement*) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1112 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1113 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1116 | bool ASTPrinter::EmitBreakIf(const ast::BreakIfStatement* b) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1117 | auto out = Line(); |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 1118 | out << "if ("; |
| 1119 | if (!EmitExpression(out, b->condition)) { |
| 1120 | return false; |
| 1121 | } |
| 1122 | out << ") { break; }"; |
| 1123 | return true; |
| 1124 | } |
| 1125 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1126 | bool ASTPrinter::EmitCall(StringStream& out, const ast::CallExpression* expr) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 1127 | auto* call = builder_.Sem().Get<sem::Call>(expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1128 | auto* target = call->Target(); |
| 1129 | return Switch( |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1130 | target, // |
| 1131 | [&](const sem::Function* func) { return EmitFunctionCall(out, call, func); }, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 1132 | [&](const sem::BuiltinFn* builtin) { return EmitBuiltinCall(out, call, builtin); }, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1133 | [&](const sem::ValueConversion* conv) { return EmitValueConversion(out, call, conv); }, |
| 1134 | [&](const sem::ValueConstructor* ctor) { return EmitValueConstructor(out, call, ctor); }, |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 1135 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1138 | bool ASTPrinter::EmitFunctionCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1139 | const sem::Call* call, |
| 1140 | const sem::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1141 | auto* expr = call->Declaration(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1142 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1143 | if (ast::HasAttribute<CalculateArrayLength::BufferSizeIntrinsic>( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1144 | func->Declaration()->attributes)) { |
| 1145 | // Special function generated by the CalculateArrayLength transform for |
| 1146 | // calling X.GetDimensions(Y) |
| 1147 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1148 | return false; |
| 1149 | } |
| 1150 | out << ".GetDimensions("; |
| 1151 | if (!EmitExpression(out, call->Arguments()[1]->Declaration())) { |
| 1152 | return false; |
| 1153 | } |
| 1154 | out << ")"; |
| 1155 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1156 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1157 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1158 | if (auto* intrinsic = |
| 1159 | ast::GetAttribute<DecomposeMemoryAccess::Intrinsic>(func->Declaration()->attributes)) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 1160 | switch (intrinsic->address_space) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1161 | case core::AddressSpace::kUniform: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1162 | return EmitUniformBufferAccess(out, expr, intrinsic); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1163 | case core::AddressSpace::kStorage: |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1164 | if (!intrinsic->IsAtomic()) { |
| 1165 | return EmitStorageBufferAccess(out, expr, intrinsic); |
| 1166 | } |
| 1167 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1168 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1169 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic address space:" |
| 1170 | << intrinsic->address_space; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1173 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1174 | |
James Price | fe24b4a | 2023-08-08 01:40:43 +0000 | [diff] [blame] | 1175 | if (auto* wave_intrinsic = |
| 1176 | ast::GetAttribute<ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic>( |
| 1177 | func->Declaration()->attributes)) { |
| 1178 | switch (wave_intrinsic->op) { |
| 1179 | case ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic::Op::kWaveGetLaneCount: |
| 1180 | out << "WaveGetLaneCount()"; |
| 1181 | return true; |
| 1182 | case ast::transform::CanonicalizeEntryPointIO::HLSLWaveIntrinsic::Op::kWaveGetLaneIndex: |
| 1183 | out << "WaveGetLaneIndex()"; |
| 1184 | return true; |
| 1185 | } |
| 1186 | } |
| 1187 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1188 | out << func->Declaration()->name->symbol.Name() << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1189 | |
| 1190 | bool first = true; |
| 1191 | for (auto* arg : call->Arguments()) { |
| 1192 | if (!first) { |
| 1193 | out << ", "; |
| 1194 | } |
| 1195 | first = false; |
| 1196 | |
| 1197 | if (!EmitExpression(out, arg->Declaration())) { |
| 1198 | return false; |
| 1199 | } |
| 1200 | } |
| 1201 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1202 | out << ")"; |
| 1203 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1206 | bool ASTPrinter::EmitBuiltinCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1207 | const sem::Call* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 1208 | const sem::BuiltinFn* builtin) { |
| 1209 | const auto type = builtin->Fn(); |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1210 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1211 | auto* expr = call->Declaration(); |
| 1212 | if (builtin->IsTexture()) { |
| 1213 | return EmitTextureCall(out, call, builtin); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1214 | } |
Ben Clayton | 52e6a0f | 2024-02-21 08:11:33 +0000 | [diff] [blame] | 1215 | if (type == wgsl::BuiltinFn::kBitcast) { |
| 1216 | return EmitBitcastCall(out, expr); |
| 1217 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1218 | if (type == wgsl::BuiltinFn::kSelect) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1219 | return EmitSelectCall(out, expr); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1220 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1221 | if (type == wgsl::BuiltinFn::kModf) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1222 | return EmitModfCall(out, expr, builtin); |
| 1223 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1224 | if (type == wgsl::BuiltinFn::kFrexp) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1225 | return EmitFrexpCall(out, expr, builtin); |
| 1226 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1227 | if (type == wgsl::BuiltinFn::kDegrees) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1228 | return EmitDegreesCall(out, expr, builtin); |
| 1229 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1230 | if (type == wgsl::BuiltinFn::kRadians) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1231 | return EmitRadiansCall(out, expr, builtin); |
| 1232 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1233 | if (type == wgsl::BuiltinFn::kSign) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 1234 | return EmitSignCall(out, call, builtin); |
| 1235 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1236 | if (type == wgsl::BuiltinFn::kQuantizeToF16) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 1237 | return EmitQuantizeToF16Call(out, expr, builtin); |
| 1238 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1239 | if (type == wgsl::BuiltinFn::kTrunc) { |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 1240 | return EmitTruncCall(out, expr, builtin); |
| 1241 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1242 | if (builtin->IsDataPacking()) { |
| 1243 | return EmitDataPackingCall(out, expr, builtin); |
| 1244 | } |
| 1245 | if (builtin->IsDataUnpacking()) { |
| 1246 | return EmitDataUnpackingCall(out, expr, builtin); |
| 1247 | } |
| 1248 | if (builtin->IsBarrier()) { |
| 1249 | return EmitBarrierCall(out, builtin); |
| 1250 | } |
| 1251 | if (builtin->IsAtomic()) { |
| 1252 | return EmitWorkgroupAtomicCall(out, expr, builtin); |
| 1253 | } |
Jiawei Shao | 53c0fa9 | 2023-12-08 01:24:00 +0000 | [diff] [blame] | 1254 | if (builtin->IsPacked4x8IntegerDotProductBuiltin()) { |
| 1255 | return EmitPacked4x8IntegerDotProductBuiltinCall(out, expr, builtin); |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 1256 | } |
James Price | 501983f | 2023-08-08 01:37:22 +0000 | [diff] [blame] | 1257 | if (builtin->IsSubgroup()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1258 | if (builtin->Fn() == wgsl::BuiltinFn::kSubgroupBroadcast) { |
David Neto | eea786f | 2023-09-21 05:32:53 +0000 | [diff] [blame] | 1259 | // Fall through the regular path. |
| 1260 | } else { |
| 1261 | return EmitSubgroupCall(out, expr, builtin); |
| 1262 | } |
James Price | 501983f | 2023-08-08 01:37:22 +0000 | [diff] [blame] | 1263 | } |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1264 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1265 | auto name = generate_builtin_name(builtin); |
| 1266 | if (name.empty()) { |
| 1267 | return false; |
| 1268 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1269 | |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1270 | // Handle single argument builtins that only accept and return uint (not int overload). We need |
| 1271 | // to explicitly cast the return value (we also cast the arg for good measure). See |
| 1272 | // crbug.com/tint/1550 |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1273 | if (type == wgsl::BuiltinFn::kCountOneBits || type == wgsl::BuiltinFn::kReverseBits) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1274 | auto* arg = call->Arguments()[0]; |
Ben Clayton | 6c337aa | 2022-12-07 19:25:17 +0000 | [diff] [blame] | 1275 | if (arg->Type()->UnwrapRef()->is_signed_integer_scalar_or_vector()) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1276 | out << "asint(" << name << "(asuint("; |
| 1277 | if (!EmitExpression(out, arg->Declaration())) { |
| 1278 | return false; |
| 1279 | } |
| 1280 | out << ")))"; |
| 1281 | return true; |
| 1282 | } |
| 1283 | } |
| 1284 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1285 | out << name << "("; |
| 1286 | |
| 1287 | bool first = true; |
| 1288 | for (auto* arg : call->Arguments()) { |
| 1289 | if (!first) { |
| 1290 | out << ", "; |
| 1291 | } |
| 1292 | first = false; |
| 1293 | |
| 1294 | if (!EmitExpression(out, arg->Declaration())) { |
| 1295 | return false; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | out << ")"; |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1300 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1301 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1304 | bool ASTPrinter::EmitValueConversion(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1305 | const sem::Call* call, |
| 1306 | const sem::ValueConversion* conv) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1307 | if (!EmitType(out, conv->Target(), core::AddressSpace::kUndefined, core::Access::kReadWrite, |
| 1308 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | } |
| 1311 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1312 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1313 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1314 | return false; |
| 1315 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1316 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1317 | out << ")"; |
| 1318 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1321 | bool ASTPrinter::EmitValueConstructor(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1322 | const sem::Call* call, |
| 1323 | const sem::ValueConstructor* ctor) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1324 | auto* type = call->Type(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1325 | |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1326 | // If the value constructor arguments are empty then we need to construct with the zero value |
| 1327 | // for all components. |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1328 | if (call->Arguments().IsEmpty()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1329 | return EmitZeroValue(out, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1330 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1331 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1332 | // Single parameter matrix initializers must be identity initializer. |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1333 | // 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] | 1334 | if (type->Is<core::type::Matrix>() && call->Arguments().Length() == 1) { |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1335 | if (!ctor->Parameters()[0]->Type()->UnwrapRef()->is_float_matrix()) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1336 | TINT_UNREACHABLE() |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1337 | << "found a single-parameter matrix initializer that is not identity initializer"; |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1338 | return false; |
Ben Clayton | 3b5edf1 | 2022-05-16 21:14:11 +0000 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1342 | bool brackets = type->IsAnyOf<core::type::Array, core::type::Struct>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1343 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1344 | // For single-value vector initializers, swizzle the scalar to the right |
| 1345 | // vector dimension using .x |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1346 | const bool is_single_value_vector_init = |
| 1347 | type->is_scalar_vector() && call->Arguments().Length() == 1 && |
| 1348 | ctor->Parameters()[0]->Type()->Is<core::type::Scalar>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1349 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 1350 | if (brackets) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1351 | out << "{"; |
| 1352 | } else { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1353 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1354 | return false; |
| 1355 | } |
| 1356 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1357 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1358 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1359 | if (is_single_value_vector_init) { |
| 1360 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1361 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1362 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1363 | bool first = true; |
| 1364 | for (auto* e : call->Arguments()) { |
| 1365 | if (!first) { |
| 1366 | out << ", "; |
| 1367 | } |
| 1368 | first = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1369 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1370 | if (!EmitExpression(out, e->Declaration())) { |
| 1371 | return false; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (is_single_value_vector_init) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1376 | out << ")." << std::string(type->As<core::type::Vector>()->Width(), 'x'); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | out << (brackets ? "}" : ")"); |
| 1380 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1383 | bool ASTPrinter::EmitUniformBufferAccess(StringStream& out, |
| 1384 | const ast::CallExpression* expr, |
| 1385 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1386 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1387 | auto* const offset = expr->args[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1388 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1389 | // offset in bytes |
| 1390 | uint32_t scalar_offset_bytes = 0; |
| 1391 | // offset in uint (4 bytes) |
| 1392 | uint32_t scalar_offset_index = 0; |
| 1393 | // expression to calculate offset in bytes |
| 1394 | std::string scalar_offset_bytes_expr; |
| 1395 | // expression to calculate offset in uint, by dividing scalar_offset_bytes_expr by 4 |
| 1396 | std::string scalar_offset_index_expr; |
| 1397 | // expression to calculate offset in uint, independently |
| 1398 | std::string scalar_offset_index_unified_expr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1399 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1400 | // If true, use scalar_offset_index, otherwise use scalar_offset_index_expr |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1401 | bool scalar_offset_constant = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1402 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1403 | if (auto* val = builder_.Sem().GetVal(offset)->ConstantValue()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1404 | TINT_ASSERT(val->Type()->Is<core::type::U32>()); |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 1405 | scalar_offset_bytes = static_cast<uint32_t>(val->ValueAs<AInt>()); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1406 | scalar_offset_index = scalar_offset_bytes / 4; // bytes -> scalar index |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1407 | scalar_offset_constant = true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1408 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1409 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1410 | // If true, scalar_offset_bytes or scalar_offset_bytes_expr should be used, otherwise only use |
| 1411 | // scalar_offset_index or scalar_offset_index_unified_expr. Currently only loading f16 scalar |
| 1412 | // require using offset in bytes. |
| 1413 | const bool need_offset_in_bytes = |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1414 | intrinsic->type == DecomposeMemoryAccess::Intrinsic::DataType::kF16; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1415 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1416 | if (!scalar_offset_constant) { |
| 1417 | // UBO offset not compile-time known. |
| 1418 | // Calculate the scalar offset into a temporary. |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1419 | if (need_offset_in_bytes) { |
| 1420 | scalar_offset_bytes_expr = UniqueIdentifier("scalar_offset_bytes"); |
| 1421 | scalar_offset_index_expr = UniqueIdentifier("scalar_offset_index"); |
| 1422 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1423 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1424 | pre << "const uint " << scalar_offset_bytes_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1425 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
| 1428 | pre << ");"; |
| 1429 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1430 | Line() << "const uint " << scalar_offset_index_expr << " = " << scalar_offset_bytes_expr |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1431 | << " / 4;"; |
| 1432 | } else { |
| 1433 | scalar_offset_index_unified_expr = UniqueIdentifier("scalar_offset"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1434 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1435 | pre << "const uint " << scalar_offset_index_unified_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1436 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | pre << ") / 4;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1440 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1441 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1442 | |
Austin Eng | a0e96b5 | 2023-02-18 00:39:01 +0000 | [diff] [blame] | 1443 | const char swizzle[] = {'x', 'y', 'z', 'w'}; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1444 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1445 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
| 1446 | using DataType = DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1447 | switch (intrinsic->op) { |
| 1448 | case Op::kLoad: { |
| 1449 | auto cast = [&](const char* to, auto&& load) { |
| 1450 | out << to << "("; |
| 1451 | auto result = load(); |
| 1452 | out << ")"; |
| 1453 | return result; |
| 1454 | }; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1455 | auto load_u32_to = [&](StringStream& target) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1456 | target << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1457 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1458 | target << "[" << (scalar_offset_index / 4) << "]." |
| 1459 | << swizzle[scalar_offset_index & 3]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1460 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1461 | target << "[" << scalar_offset_index_unified_expr << " / 4][" |
| 1462 | << scalar_offset_index_unified_expr << " % 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1463 | } |
| 1464 | return true; |
| 1465 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1466 | auto load_u32 = [&] { return load_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1467 | // 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] | 1468 | auto load_vec2_u32_to = [&](StringStream& target) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1469 | if (scalar_offset_constant) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1470 | target << buffer << "[" << (scalar_offset_index / 4) << "]" |
| 1471 | << ((scalar_offset_index & 2) == 0 ? ".xy" : ".zw"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1474 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1475 | auto pre = Line(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1476 | pre << "uint4 " << ubo_load << " = " << buffer << "[" |
| 1477 | << scalar_offset_index_unified_expr << " / 4];"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1478 | } |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1479 | target << "((" << scalar_offset_index_unified_expr << " & 2) ? " << ubo_load |
| 1480 | << ".zw : " << ubo_load << ".xy)"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1481 | } |
| 1482 | return true; |
| 1483 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1484 | auto load_vec2_u32 = [&] { return load_vec2_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1485 | // vec4 has a minimum alignment of 16 bytes, easiest case |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1486 | auto load_vec4_u32 = [&] { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1487 | out << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1488 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1489 | out << "[" << (scalar_offset_index / 4) << "]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1490 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1491 | out << "[" << scalar_offset_index_unified_expr << " / 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1492 | } |
| 1493 | return true; |
| 1494 | }; |
| 1495 | // 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] | 1496 | auto load_vec3_u32 = [&] { |
| 1497 | if (!load_vec4_u32()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1498 | return false; |
| 1499 | } |
| 1500 | out << ".xyz"; |
| 1501 | return true; |
| 1502 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1503 | auto load_scalar_f16 = [&] { |
| 1504 | // offset bytes = 4k, ((buffer[index].x) & 0xFFFF) |
| 1505 | // offset bytes = 4k+2, ((buffer[index].x >> 16) & 0xFFFF) |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1506 | out << "float16_t(f16tof32(((" << buffer; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1507 | if (scalar_offset_constant) { |
| 1508 | out << "[" << (scalar_offset_index / 4) << "]." |
| 1509 | << swizzle[scalar_offset_index & 3]; |
| 1510 | // WGSL spec ensure little endian memory layout. |
| 1511 | if (scalar_offset_bytes % 4 == 0) { |
| 1512 | out << ") & 0xFFFF)"; |
| 1513 | } else { |
| 1514 | out << " >> 16) & 0xFFFF)"; |
| 1515 | } |
| 1516 | } else { |
| 1517 | out << "[" << scalar_offset_index_expr << " / 4][" << scalar_offset_index_expr |
| 1518 | << " % 4] >> (" << scalar_offset_bytes_expr |
| 1519 | << " % 4 == 0 ? 0 : 16)) & 0xFFFF)"; |
| 1520 | } |
| 1521 | out << "))"; |
| 1522 | return true; |
| 1523 | }; |
| 1524 | auto load_vec2_f16 = [&] { |
| 1525 | // vec2<f16> is aligned to 4 bytes |
| 1526 | // Preclude code load the vec2<f16> data as a uint: |
| 1527 | // uint ubo_load = buffer[id0][id1]; |
| 1528 | // Loading code convert it to vec2<f16>: |
| 1529 | // vector<float16_t, 2>(float16_t(f16tof32(ubo_load & 0xFFFF)), |
| 1530 | // float16_t(f16tof32(ubo_load >> 16))) |
| 1531 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1532 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1533 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1534 | // Load the 4 bytes f16 vector as an uint |
| 1535 | pre << "uint " << ubo_load << " = "; |
| 1536 | if (!load_u32_to(pre)) { |
| 1537 | return false; |
| 1538 | } |
| 1539 | pre << ";"; |
| 1540 | } |
| 1541 | out << "vector<float16_t, 2>(float16_t(f16tof32(" << ubo_load |
| 1542 | << " & 0xFFFF)), float16_t(f16tof32(" << ubo_load << " >> 16)))"; |
| 1543 | return true; |
| 1544 | }; |
| 1545 | auto load_vec3_f16 = [&] { |
| 1546 | // vec3<f16> is aligned to 8 bytes |
| 1547 | // Preclude code load the vec3<f16> data as uint2 and convert its elements to |
| 1548 | // float16_t: |
| 1549 | // uint2 ubo_load = buffer[id0].xy; |
| 1550 | // /* The low 8 bits of two uint are the x and z elements of vec3<f16> */ |
| 1551 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1552 | // 0xFFFF)); |
| 1553 | // /* The high 8 bits of first uint is the y element of vec3<f16> */ |
| 1554 | // float16_t ubo_load_y = f16tof32(ubo_load[0] >> 16); |
| 1555 | // Loading code convert it to vec3<f16>: |
| 1556 | // vector<float16_t, 3>(ubo_load_xz[0], ubo_load_y, ubo_load_xz[1]) |
| 1557 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1558 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1559 | std::string ubo_load_y = UniqueIdentifier(ubo_load + "_y"); |
| 1560 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1561 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1562 | // Load the 8 bytes uint2 with the f16 vector at lower 6 bytes |
| 1563 | pre << "uint2 " << ubo_load << " = "; |
| 1564 | if (!load_vec2_u32_to(pre)) { |
| 1565 | return false; |
| 1566 | } |
| 1567 | pre << ";"; |
| 1568 | } |
| 1569 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1570 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1571 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1572 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1573 | } |
| 1574 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1575 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1576 | pre << "float16_t " << ubo_load_y << " = f16tof32(" << ubo_load |
| 1577 | << "[0] >> 16);"; |
| 1578 | } |
| 1579 | out << "vector<float16_t, 3>(" << ubo_load_xz << "[0], " << ubo_load_y << ", " |
| 1580 | << ubo_load_xz << "[1])"; |
| 1581 | return true; |
| 1582 | }; |
| 1583 | auto load_vec4_f16 = [&] { |
| 1584 | // vec4<f16> is aligned to 8 bytes |
| 1585 | // Preclude code load the vec4<f16> data as uint2 and convert its elements to |
| 1586 | // float16_t: |
| 1587 | // uint2 ubo_load = buffer[id0].xy; |
| 1588 | // /* The low 8 bits of two uint are the x and z elements of vec4<f16> */ |
| 1589 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1590 | // 0xFFFF)); |
| 1591 | // /* The high 8 bits of two uint are the y and w elements of vec4<f16> */ |
| 1592 | // vector<float16_t, 2> ubo_load_yw = vector<float16_t, 2>(f16tof32(ubo_load >> |
| 1593 | // 16)); |
| 1594 | // Loading code convert it to vec4<f16>: |
| 1595 | // vector<float16_t, 4>(ubo_load_xz[0], ubo_load_yw[0], ubo_load_xz[1], |
| 1596 | // ubo_load_yw[1]) |
| 1597 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1598 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1599 | std::string ubo_load_yw = UniqueIdentifier(ubo_load + "_yw"); |
| 1600 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1601 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1602 | // Load the 8 bytes f16 vector as an uint2 |
| 1603 | pre << "uint2 " << ubo_load << " = "; |
| 1604 | if (!load_vec2_u32_to(pre)) { |
| 1605 | return false; |
| 1606 | } |
| 1607 | pre << ";"; |
| 1608 | } |
| 1609 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1610 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1611 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1612 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1613 | } |
| 1614 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1615 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1616 | pre << "vector<float16_t, 2> " << ubo_load_yw |
| 1617 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " >> 16));"; |
| 1618 | } |
| 1619 | out << "vector<float16_t, 4>(" << ubo_load_xz << "[0], " << ubo_load_yw << "[0], " |
| 1620 | << ubo_load_xz << "[1], " << ubo_load_yw << "[1])"; |
| 1621 | return true; |
| 1622 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1623 | switch (intrinsic->type) { |
| 1624 | case DataType::kU32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1625 | return load_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1626 | case DataType::kF32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1627 | return cast("asfloat", load_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1628 | case DataType::kI32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1629 | return cast("asint", load_u32); |
| 1630 | case DataType::kF16: |
| 1631 | return load_scalar_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1632 | case DataType::kVec2U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1633 | return load_vec2_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1634 | case DataType::kVec2F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1635 | return cast("asfloat", load_vec2_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1636 | case DataType::kVec2I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1637 | return cast("asint", load_vec2_u32); |
| 1638 | case DataType::kVec2F16: |
| 1639 | return load_vec2_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1640 | case DataType::kVec3U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1641 | return load_vec3_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1642 | case DataType::kVec3F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1643 | return cast("asfloat", load_vec3_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1644 | case DataType::kVec3I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1645 | return cast("asint", load_vec3_u32); |
| 1646 | case DataType::kVec3F16: |
| 1647 | return load_vec3_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1648 | case DataType::kVec4U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1649 | return load_vec4_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1650 | case DataType::kVec4F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1651 | return cast("asfloat", load_vec4_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1652 | case DataType::kVec4I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1653 | return cast("asint", load_vec4_u32); |
| 1654 | case DataType::kVec4F16: |
| 1655 | return load_vec4_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1656 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1657 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1658 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
| 1661 | default: |
| 1662 | break; |
| 1663 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1664 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1665 | << static_cast<int>(intrinsic->op); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1666 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1669 | bool ASTPrinter::EmitStorageBufferAccess(StringStream& out, |
| 1670 | const ast::CallExpression* expr, |
| 1671 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1672 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1673 | auto* const offset = expr->args[0]; |
Ben Clayton | 407137a | 2023-06-28 21:02:32 +0000 | [diff] [blame] | 1674 | auto* const value = expr->args.Length() > 1 ? expr->args[1] : nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1675 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1676 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
| 1677 | using DataType = DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1678 | switch (intrinsic->op) { |
| 1679 | case Op::kLoad: { |
| 1680 | auto load = [&](const char* cast, int n) { |
| 1681 | if (cast) { |
| 1682 | out << cast << "("; |
| 1683 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1684 | out << buffer << ".Load"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1685 | if (n > 1) { |
| 1686 | out << n; |
| 1687 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1688 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1689 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1690 | return false; |
| 1691 | } |
| 1692 | if (cast) { |
| 1693 | out << ")"; |
| 1694 | } |
| 1695 | return true; |
| 1696 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1697 | // Templated load used for f16 types, requires SM6.2 or higher and DXC |
| 1698 | // Used by loading f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1699 | // to emit `buffer.Load<float16_t>(offset)`. |
| 1700 | auto templated_load = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1701 | out << buffer << ".Load<" << type << ">"; // templated load |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1702 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1703 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1704 | return false; |
| 1705 | } |
| 1706 | return true; |
| 1707 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1708 | switch (intrinsic->type) { |
| 1709 | case DataType::kU32: |
| 1710 | return load(nullptr, 1); |
| 1711 | case DataType::kF32: |
| 1712 | return load("asfloat", 1); |
| 1713 | case DataType::kI32: |
| 1714 | return load("asint", 1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1715 | case DataType::kF16: |
| 1716 | return templated_load("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1717 | case DataType::kVec2U32: |
| 1718 | return load(nullptr, 2); |
| 1719 | case DataType::kVec2F32: |
| 1720 | return load("asfloat", 2); |
| 1721 | case DataType::kVec2I32: |
| 1722 | return load("asint", 2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1723 | case DataType::kVec2F16: |
| 1724 | return templated_load("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1725 | case DataType::kVec3U32: |
| 1726 | return load(nullptr, 3); |
| 1727 | case DataType::kVec3F32: |
| 1728 | return load("asfloat", 3); |
| 1729 | case DataType::kVec3I32: |
| 1730 | return load("asint", 3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1731 | case DataType::kVec3F16: |
| 1732 | return templated_load("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1733 | case DataType::kVec4U32: |
| 1734 | return load(nullptr, 4); |
| 1735 | case DataType::kVec4F32: |
| 1736 | return load("asfloat", 4); |
| 1737 | case DataType::kVec4I32: |
| 1738 | return load("asint", 4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1739 | case DataType::kVec4F16: |
| 1740 | return templated_load("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1741 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1742 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1743 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1744 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1745 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1746 | |
| 1747 | case Op::kStore: { |
| 1748 | auto store = [&](int n) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1749 | out << buffer << ".Store"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1750 | if (n > 1) { |
| 1751 | out << n; |
| 1752 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1753 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1754 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1755 | return false; |
| 1756 | } |
| 1757 | out << ", asuint"; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1758 | ScopedParen sp2(out); |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 1759 | if (!EmitTextureOrStorageBufferCallArgExpression(out, value)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1760 | return false; |
| 1761 | } |
| 1762 | return true; |
| 1763 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1764 | // Templated stored used for f16 types, requires SM6.2 or higher and DXC |
| 1765 | // Used by storing f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1766 | // to emit `buffer.Store<float16_t>(offset)`. |
| 1767 | auto templated_store = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1768 | out << buffer << ".Store<" << type << ">"; // templated store |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1769 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1770 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1771 | return false; |
| 1772 | } |
| 1773 | out << ", "; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1774 | if (!EmitExpression(out, value)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1775 | return false; |
| 1776 | } |
| 1777 | return true; |
| 1778 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1779 | switch (intrinsic->type) { |
| 1780 | case DataType::kU32: |
| 1781 | return store(1); |
| 1782 | case DataType::kF32: |
| 1783 | return store(1); |
| 1784 | case DataType::kI32: |
| 1785 | return store(1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1786 | case DataType::kF16: |
| 1787 | return templated_store("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1788 | case DataType::kVec2U32: |
| 1789 | return store(2); |
| 1790 | case DataType::kVec2F32: |
| 1791 | return store(2); |
| 1792 | case DataType::kVec2I32: |
| 1793 | return store(2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1794 | case DataType::kVec2F16: |
| 1795 | return templated_store("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1796 | case DataType::kVec3U32: |
| 1797 | return store(3); |
| 1798 | case DataType::kVec3F32: |
| 1799 | return store(3); |
| 1800 | case DataType::kVec3I32: |
| 1801 | return store(3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1802 | case DataType::kVec3F16: |
| 1803 | return templated_store("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1804 | case DataType::kVec4U32: |
| 1805 | return store(4); |
| 1806 | case DataType::kVec4F32: |
| 1807 | return store(4); |
| 1808 | case DataType::kVec4I32: |
| 1809 | return store(4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1810 | case DataType::kVec4F16: |
| 1811 | return templated_store("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1812 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1813 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1814 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1815 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1816 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1817 | default: |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1818 | // Break out to error case below |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1819 | // Note that atomic intrinsics are generated as functions. |
| 1820 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1823 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1824 | << static_cast<int>(intrinsic->op); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1825 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 1828 | bool ASTPrinter::EmitStorageAtomicIntrinsic(const ast::Function* func, |
| 1829 | const DecomposeMemoryAccess::Intrinsic* intrinsic) { |
| 1830 | using Op = DecomposeMemoryAccess::Intrinsic::Op; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1831 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1832 | const sem::Function* sem_func = builder_.Sem().Get(func); |
| 1833 | auto* result_ty = sem_func->ReturnType(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1834 | const auto name = func->name->symbol.Name(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1835 | auto& buf = *current_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1836 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1837 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1838 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1839 | auto rmw = [&](const char* hlsl) -> bool { |
| 1840 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1841 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1842 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1843 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1846 | fn << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1847 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1848 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1849 | return false; |
| 1850 | } |
| 1851 | fn << ") {"; |
| 1852 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1853 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1854 | buf.IncrementIndent(); |
| 1855 | TINT_DEFER({ |
| 1856 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1857 | Line(&buf) << "}"; |
| 1858 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1859 | }); |
| 1860 | |
| 1861 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1862 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1863 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 1864 | core::Access::kUndefined, "original_value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1865 | return false; |
| 1866 | } |
| 1867 | l << " = 0;"; |
| 1868 | } |
| 1869 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1870 | auto l = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1871 | l << buffer << "." << hlsl << "(offset, "; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1872 | if (intrinsic->op == Op::kAtomicSub) { |
| 1873 | l << "-"; |
| 1874 | } |
| 1875 | l << "value, original_value);"; |
| 1876 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1877 | Line(&buf) << "return original_value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1878 | return true; |
| 1879 | }; |
| 1880 | |
| 1881 | switch (intrinsic->op) { |
| 1882 | case Op::kAtomicAdd: |
| 1883 | return rmw("InterlockedAdd"); |
| 1884 | |
| 1885 | case Op::kAtomicSub: |
| 1886 | // Use add with the operand negated. |
| 1887 | return rmw("InterlockedAdd"); |
| 1888 | |
| 1889 | case Op::kAtomicMax: |
| 1890 | return rmw("InterlockedMax"); |
| 1891 | |
| 1892 | case Op::kAtomicMin: |
| 1893 | return rmw("InterlockedMin"); |
| 1894 | |
| 1895 | case Op::kAtomicAnd: |
| 1896 | return rmw("InterlockedAnd"); |
| 1897 | |
| 1898 | case Op::kAtomicOr: |
| 1899 | return rmw("InterlockedOr"); |
| 1900 | |
| 1901 | case Op::kAtomicXor: |
| 1902 | return rmw("InterlockedXor"); |
| 1903 | |
| 1904 | case Op::kAtomicExchange: |
| 1905 | return rmw("InterlockedExchange"); |
| 1906 | |
| 1907 | case Op::kAtomicLoad: { |
| 1908 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 1909 | // InterlockedOr using 0 as the OR value |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1910 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1911 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1912 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1913 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1914 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1915 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1916 | fn << "(uint offset) {"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | buf.IncrementIndent(); |
| 1920 | TINT_DEFER({ |
| 1921 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1922 | Line(&buf) << "}"; |
| 1923 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1924 | }); |
| 1925 | |
| 1926 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1927 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1928 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 1929 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1930 | return false; |
| 1931 | } |
| 1932 | l << " = 0;"; |
| 1933 | } |
| 1934 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1935 | Line(&buf) << buffer << ".InterlockedOr(offset, 0, value);"; |
| 1936 | Line(&buf) << "return value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1937 | return true; |
| 1938 | } |
| 1939 | case Op::kAtomicStore: { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1940 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1941 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 1942 | // InterlockedExchange and discard the returned value |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1943 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1944 | auto fn = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1945 | fn << "void " << name << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1946 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 1947 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1948 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1949 | } |
| 1950 | fn << ") {"; |
| 1951 | } |
| 1952 | |
| 1953 | buf.IncrementIndent(); |
| 1954 | TINT_DEFER({ |
| 1955 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1956 | Line(&buf) << "}"; |
| 1957 | Line(&buf); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1958 | }); |
| 1959 | |
| 1960 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1961 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1962 | if (!EmitTypeAndName(l, value_ty, core::AddressSpace::kUndefined, |
| 1963 | core::Access::kUndefined, "ignored")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1964 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1965 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1966 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1967 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1968 | Line(&buf) << buffer << ".InterlockedExchange(offset, value, ignored);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1969 | return true; |
| 1970 | } |
| 1971 | case Op::kAtomicCompareExchangeWeak: { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 1972 | if (!EmitStructType(&helpers_, result_ty->As<core::type::Struct>())) { |
Ben Clayton | 4b8a3d3 | 2023-06-13 16:55:57 +0000 | [diff] [blame] | 1973 | return false; |
| 1974 | } |
| 1975 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1976 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1977 | // NOTE: We don't need to emit the return type struct here as DecomposeMemoryAccess |
| 1978 | // 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] | 1979 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1980 | auto fn = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1981 | if (!EmitTypeAndName(fn, result_ty, core::AddressSpace::kUndefined, |
| 1982 | core::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1983 | return false; |
| 1984 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1985 | fn << "(uint offset, "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1986 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 1987 | core::Access::kUndefined, "compare")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1988 | return false; |
| 1989 | } |
| 1990 | fn << ", "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 1991 | if (!EmitTypeAndName(fn, value_ty, core::AddressSpace::kUndefined, |
| 1992 | core::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1993 | return false; |
| 1994 | } |
| 1995 | fn << ") {"; |
| 1996 | } |
| 1997 | |
| 1998 | buf.IncrementIndent(); |
| 1999 | TINT_DEFER({ |
| 2000 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2001 | Line(&buf) << "}"; |
| 2002 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2003 | }); |
| 2004 | |
| 2005 | { // T result = {0}; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2006 | auto l = Line(&buf); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2007 | if (!EmitTypeAndName(l, result_ty, core::AddressSpace::kUndefined, |
| 2008 | core::Access::kUndefined, "result")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2009 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2010 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2011 | l << "="; |
| 2012 | if (!EmitZeroValue(l, result_ty)) { |
| 2013 | return false; |
| 2014 | } |
| 2015 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2016 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2017 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2018 | Line(&buf) << buffer |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 2019 | << ".InterlockedCompareExchange(offset, compare, value, result.old_value);"; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2020 | Line(&buf) << "result.exchanged = result.old_value == compare;"; |
| 2021 | Line(&buf) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2022 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2023 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2024 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2025 | default: |
| 2026 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2027 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2028 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2029 | TINT_UNREACHABLE() << "unsupported atomic DecomposeMemoryAccess::Intrinsic::Op: " |
| 2030 | << static_cast<int>(intrinsic->op); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2031 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2034 | bool ASTPrinter::EmitWorkgroupAtomicCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2035 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2036 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2037 | std::string result = UniqueIdentifier("atomic_result"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2038 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2039 | if (!builtin->ReturnType()->Is<core::type::Void>()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2040 | auto pre = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2041 | if (!EmitTypeAndName(pre, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2042 | core::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2043 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2044 | } |
| 2045 | pre << " = "; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2046 | if (!EmitZeroValue(pre, builtin->ReturnType())) { |
| 2047 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2048 | } |
| 2049 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2050 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2051 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2052 | auto call = [&](const char* name) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2053 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2054 | pre << name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2055 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2056 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2057 | ScopedParen sp(pre); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 2058 | for (size_t i = 0; i < expr->args.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2059 | auto* arg = expr->args[i]; |
| 2060 | if (i > 0) { |
| 2061 | pre << ", "; |
| 2062 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2063 | if (!EmitExpression(pre, arg)) { |
| 2064 | return false; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | pre << ", " << result; |
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 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2071 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2072 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2073 | out << result; |
| 2074 | return true; |
| 2075 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2076 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2077 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2078 | case wgsl::BuiltinFn::kAtomicLoad: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2079 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 2080 | // InterlockedOr using 0 as the OR value |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2081 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2082 | pre << "InterlockedOr"; |
| 2083 | { |
| 2084 | ScopedParen sp(pre); |
| 2085 | if (!EmitExpression(pre, expr->args[0])) { |
| 2086 | return false; |
| 2087 | } |
| 2088 | pre << ", 0, " << result; |
| 2089 | } |
| 2090 | pre << ";"; |
| 2091 | |
| 2092 | out << result; |
| 2093 | return true; |
| 2094 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2095 | case wgsl::BuiltinFn::kAtomicStore: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2096 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 2097 | // InterlockedExchange and discard the returned value |
| 2098 | { // T result = 0; |
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 | auto* value_ty = builtin->Parameters()[1]->Type()->UnwrapRef(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2101 | if (!EmitTypeAndName(pre, value_ty, core::AddressSpace::kUndefined, |
| 2102 | core::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2103 | return false; |
| 2104 | } |
| 2105 | pre << " = "; |
| 2106 | if (!EmitZeroValue(pre, value_ty)) { |
| 2107 | return false; |
| 2108 | } |
| 2109 | pre << ";"; |
| 2110 | } |
| 2111 | |
| 2112 | out << "InterlockedExchange"; |
| 2113 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2114 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2115 | if (!EmitExpression(out, expr->args[0])) { |
| 2116 | return false; |
| 2117 | } |
| 2118 | out << ", "; |
| 2119 | if (!EmitExpression(out, expr->args[1])) { |
| 2120 | return false; |
| 2121 | } |
| 2122 | out << ", " << result; |
| 2123 | } |
| 2124 | return true; |
| 2125 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2126 | case wgsl::BuiltinFn::kAtomicCompareExchangeWeak: { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2127 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2128 | return false; |
| 2129 | } |
| 2130 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2131 | auto* dest = expr->args[0]; |
| 2132 | auto* compare_value = expr->args[1]; |
| 2133 | auto* value = expr->args[2]; |
| 2134 | |
| 2135 | std::string compare = UniqueIdentifier("atomic_compare_value"); |
| 2136 | |
| 2137 | { // T compare_value = <compare_value>; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2138 | auto pre = Line(); |
Antonio Maiorano | f99671b | 2022-06-23 13:14:54 +0000 | [diff] [blame] | 2139 | if (!EmitTypeAndName(pre, TypeOf(compare_value)->UnwrapRef(), |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2140 | core::AddressSpace::kUndefined, core::Access::kUndefined, |
dan sinclair | 61c16eb | 2023-01-21 23:44:38 +0000 | [diff] [blame] | 2141 | compare)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
| 2144 | pre << " = "; |
| 2145 | if (!EmitExpression(pre, compare_value)) { |
| 2146 | return false; |
| 2147 | } |
| 2148 | pre << ";"; |
| 2149 | } |
| 2150 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2151 | { // InterlockedCompareExchange(dst, compare, value, result.old_value); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2152 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2153 | pre << "InterlockedCompareExchange"; |
| 2154 | { |
| 2155 | ScopedParen sp(pre); |
| 2156 | if (!EmitExpression(pre, dest)) { |
| 2157 | return false; |
| 2158 | } |
| 2159 | pre << ", " << compare << ", "; |
| 2160 | if (!EmitExpression(pre, value)) { |
| 2161 | return false; |
| 2162 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2163 | pre << ", " << result << ".old_value"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2164 | } |
| 2165 | pre << ";"; |
| 2166 | } |
| 2167 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2168 | // result.exchanged = result.old_value == compare; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2169 | Line() << result << ".exchanged = " << result << ".old_value == " << compare << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2170 | |
| 2171 | out << result; |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2175 | case wgsl::BuiltinFn::kAtomicAdd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2176 | return call("InterlockedAdd"); |
| 2177 | |
Stephen White | 4249124 | 2023-10-18 18:02:20 +0000 | [diff] [blame] | 2178 | case wgsl::BuiltinFn::kAtomicSub: { |
| 2179 | auto pre = Line(); |
| 2180 | // Sub uses InterlockedAdd with the operand negated. |
| 2181 | pre << "InterlockedAdd"; |
| 2182 | { |
| 2183 | ScopedParen sp(pre); |
| 2184 | TINT_ASSERT(expr->args.Length() == 2); |
| 2185 | |
| 2186 | if (!EmitExpression(pre, expr->args[0])) { |
| 2187 | return false; |
| 2188 | } |
| 2189 | pre << ", -"; |
| 2190 | { |
| 2191 | ScopedParen argSP(pre); |
| 2192 | if (!EmitExpression(pre, expr->args[1])) { |
| 2193 | return false; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | pre << ", " << result; |
| 2198 | } |
| 2199 | |
| 2200 | pre << ";"; |
| 2201 | |
| 2202 | out << result; |
| 2203 | } |
| 2204 | return true; |
| 2205 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2206 | case wgsl::BuiltinFn::kAtomicMax: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2207 | return call("InterlockedMax"); |
| 2208 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2209 | case wgsl::BuiltinFn::kAtomicMin: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2210 | return call("InterlockedMin"); |
| 2211 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2212 | case wgsl::BuiltinFn::kAtomicAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2213 | return call("InterlockedAnd"); |
| 2214 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2215 | case wgsl::BuiltinFn::kAtomicOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2216 | return call("InterlockedOr"); |
| 2217 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2218 | case wgsl::BuiltinFn::kAtomicXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2219 | return call("InterlockedXor"); |
| 2220 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2221 | case wgsl::BuiltinFn::kAtomicExchange: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2222 | return call("InterlockedExchange"); |
| 2223 | |
| 2224 | default: |
| 2225 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2228 | TINT_UNREACHABLE() << "unsupported atomic builtin: " << builtin->Fn(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2229 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2232 | bool ASTPrinter::EmitSelectCall(StringStream& out, const ast::CallExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2233 | auto* expr_false = expr->args[0]; |
| 2234 | auto* expr_true = expr->args[1]; |
| 2235 | auto* expr_cond = expr->args[2]; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2236 | ScopedParen paren(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2237 | if (!EmitExpression(out, expr_cond)) { |
| 2238 | return false; |
| 2239 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2240 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2241 | out << " ? "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2242 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2243 | if (!EmitExpression(out, expr_true)) { |
| 2244 | return false; |
| 2245 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2246 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2247 | out << " : "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2248 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2249 | if (!EmitExpression(out, expr_false)) { |
| 2250 | return false; |
| 2251 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2252 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2253 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2256 | bool ASTPrinter::EmitModfCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2257 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2258 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2259 | return CallBuiltinHelper( |
| 2260 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2261 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2262 | auto in = params[0]; |
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 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2265 | if (auto* vec = ty->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2266 | width = std::to_string(vec->Width()); |
| 2267 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2268 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2269 | // Emit the builtin return type unique to this overload. This does not |
| 2270 | // exist in the AST, so it will not be generated in Generate(). |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2271 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2272 | return false; |
| 2273 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2274 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2275 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2276 | auto l = Line(b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2277 | if (!EmitType(l, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2278 | core::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2279 | return false; |
| 2280 | } |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2281 | l << " result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2282 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2283 | Line(b) << "result.fract = modf(" << params[0] << ", result.whole);"; |
| 2284 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2285 | return true; |
| 2286 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2289 | bool ASTPrinter::EmitFrexpCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2290 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2291 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2292 | return CallBuiltinHelper( |
| 2293 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2294 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2295 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2296 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2297 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2298 | if (auto* vec = ty->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2299 | width = std::to_string(vec->Width()); |
| 2300 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2301 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2302 | // Emit the builtin return type unique to this overload. This does not |
| 2303 | // exist in the AST, so it will not be generated in Generate(). |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2304 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2305 | return false; |
| 2306 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2307 | |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2308 | std::string member_type; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2309 | if (Is<core::type::F16>(ty->DeepestElement())) { |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2310 | member_type = width.empty() ? "float16_t" : ("vector<float16_t, " + width + ">"); |
| 2311 | } else { |
| 2312 | member_type = "float" + width; |
| 2313 | } |
| 2314 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2315 | Line(b) << member_type << " exp;"; |
| 2316 | Line(b) << member_type << " fract = sign(" << in << ") * frexp(" << in << ", exp);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2317 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2318 | auto l = Line(b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2319 | if (!EmitType(l, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 2320 | core::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2321 | return false; |
| 2322 | } |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 2323 | l << " result = {fract, int" << width << "(exp)};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2324 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2325 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2326 | return true; |
| 2327 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2330 | bool ASTPrinter::EmitDegreesCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2331 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2332 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2333 | return CallBuiltinHelper(out, expr, builtin, |
| 2334 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2335 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2336 | << sem::kRadToDeg << ";"; |
| 2337 | return true; |
| 2338 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2341 | bool ASTPrinter::EmitRadiansCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2342 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2343 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2344 | return CallBuiltinHelper(out, expr, builtin, |
| 2345 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2346 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2347 | << sem::kDegToRad << ";"; |
| 2348 | return true; |
| 2349 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2352 | // The HLSL `sign` method always returns an `int` result (scalar or vector). In WGSL the result is |
| 2353 | // expected to be the same type as the argument. This injects a cast to the expected WGSL result |
| 2354 | // type after the call to `sign`. |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2355 | bool ASTPrinter::EmitSignCall(StringStream& out, const sem::Call* call, const sem::BuiltinFn*) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2356 | auto* arg = call->Arguments()[0]; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 2357 | if (!EmitType(out, arg->Type(), core::AddressSpace::kUndefined, core::Access::kReadWrite, "")) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2358 | return false; |
| 2359 | } |
| 2360 | out << "(sign("; |
| 2361 | if (!EmitExpression(out, arg->Declaration())) { |
| 2362 | return false; |
| 2363 | } |
| 2364 | out << "))"; |
| 2365 | return true; |
| 2366 | } |
| 2367 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2368 | bool ASTPrinter::EmitQuantizeToF16Call(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2369 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2370 | const sem::BuiltinFn* builtin) { |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2371 | // Cast to f16 and back |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2372 | std::string width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2373 | if (auto* vec = builtin->ReturnType()->As<core::type::Vector>()) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2374 | width = std::to_string(vec->Width()); |
| 2375 | } |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2376 | out << "f16tof32(f32tof16" |
| 2377 | << "("; |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2378 | if (!EmitExpression(out, expr->args[0])) { |
| 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::EmitTruncCall(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 | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2388 | // HLSL's trunc is broken for very large/small float values. |
| 2389 | // See crbug.com/tint/1883 |
| 2390 | return CallBuiltinHelper( // |
| 2391 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2392 | // value < 0 ? ceil(value) : floor(value) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2393 | Line(b) << "return " << params[0] << " < 0 ? ceil(" << params[0] << ") : floor(" |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2394 | << params[0] << ");"; |
| 2395 | return true; |
| 2396 | }); |
| 2397 | } |
| 2398 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2399 | bool ASTPrinter::EmitDataPackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2400 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2401 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2402 | return CallBuiltinHelper( |
| 2403 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2404 | uint32_t dims = 2; |
| 2405 | bool is_signed = false; |
| 2406 | uint32_t scale = 65535; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2407 | if (builtin->Fn() == wgsl::BuiltinFn::kPack4X8Snorm || |
| 2408 | builtin->Fn() == wgsl::BuiltinFn::kPack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2409 | dims = 4; |
| 2410 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2411 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2412 | if (builtin->Fn() == wgsl::BuiltinFn::kPack4X8Snorm || |
| 2413 | builtin->Fn() == wgsl::BuiltinFn::kPack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2414 | is_signed = true; |
| 2415 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2416 | } |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2417 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2418 | case wgsl::BuiltinFn::kPack4X8Snorm: |
| 2419 | case wgsl::BuiltinFn::kPack4X8Unorm: |
| 2420 | case wgsl::BuiltinFn::kPack2X16Snorm: |
| 2421 | case wgsl::BuiltinFn::kPack2X16Unorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2422 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2423 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2424 | l << (is_signed ? "" : "u") << "int" << dims |
| 2425 | << " i = " << (is_signed ? "" : "u") << "int" << dims << "(round(clamp(" |
| 2426 | << params[0] << ", " << (is_signed ? "-1.0" : "0.0") << ", 1.0) * " |
| 2427 | << scale << ".0))"; |
| 2428 | if (is_signed) { |
| 2429 | l << " & " << (dims == 4 ? "0xff" : "0xffff"); |
| 2430 | } |
| 2431 | l << ";"; |
| 2432 | } |
| 2433 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2434 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2435 | l << "return "; |
| 2436 | if (is_signed) { |
| 2437 | l << "asuint"; |
| 2438 | } |
| 2439 | l << "(i.x | i.y << " << (32 / dims); |
| 2440 | if (dims == 4) { |
| 2441 | l << " | i.z << 16 | i.w << 24"; |
| 2442 | } |
| 2443 | l << ");"; |
| 2444 | } |
| 2445 | break; |
| 2446 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2447 | case wgsl::BuiltinFn::kPack2X16Float: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2448 | Line(b) << "uint2 i = f32tof16(" << params[0] << ");"; |
| 2449 | Line(b) << "return i.x | (i.y << 16);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2450 | break; |
| 2451 | } |
| 2452 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 2453 | TINT_ICE() << " unhandled data packing builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2454 | return false; |
| 2455 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2456 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2457 | return true; |
| 2458 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2461 | bool ASTPrinter::EmitDataUnpackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2462 | const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2463 | const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2464 | return CallBuiltinHelper( |
| 2465 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2466 | uint32_t dims = 2; |
| 2467 | bool is_signed = false; |
| 2468 | uint32_t scale = 65535; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2469 | if (builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Snorm || |
| 2470 | builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2471 | dims = 4; |
| 2472 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2473 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2474 | if (builtin->Fn() == wgsl::BuiltinFn::kUnpack4X8Snorm || |
| 2475 | builtin->Fn() == wgsl::BuiltinFn::kUnpack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2476 | is_signed = true; |
| 2477 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2478 | } |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2479 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2480 | case wgsl::BuiltinFn::kUnpack4X8Snorm: |
| 2481 | case wgsl::BuiltinFn::kUnpack2X16Snorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2482 | Line(b) << "int j = int(" << params[0] << ");"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2483 | { // Perform sign extension on the converted values. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2484 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2485 | l << "int" << dims << " i = int" << dims << "("; |
| 2486 | if (dims == 2) { |
| 2487 | l << "j << 16, j) >> 16"; |
| 2488 | } else { |
| 2489 | l << "j << 24, j << 16, j << 8, j) >> 24"; |
| 2490 | } |
| 2491 | l << ";"; |
| 2492 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2493 | Line(b) << "return clamp(float" << dims << "(i) / " << scale << ".0, " |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2494 | << (is_signed ? "-1.0" : "0.0") << ", 1.0);"; |
| 2495 | break; |
| 2496 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2497 | case wgsl::BuiltinFn::kUnpack4X8Unorm: |
| 2498 | case wgsl::BuiltinFn::kUnpack2X16Unorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2499 | Line(b) << "uint j = " << params[0] << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2500 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2501 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2502 | l << "uint" << dims << " i = uint" << dims << "("; |
| 2503 | l << "j & " << (dims == 2 ? "0xffff" : "0xff") << ", "; |
| 2504 | if (dims == 4) { |
| 2505 | l << "(j >> " << (32 / dims) << ") & 0xff, (j >> 16) & 0xff, j >> 24"; |
| 2506 | } else { |
| 2507 | l << "j >> " << (32 / dims); |
| 2508 | } |
| 2509 | l << ");"; |
| 2510 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2511 | Line(b) << "return float" << dims << "(i) / " << scale << ".0;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2512 | break; |
| 2513 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2514 | case wgsl::BuiltinFn::kUnpack2X16Float: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2515 | Line(b) << "uint i = " << params[0] << ";"; |
| 2516 | Line(b) << "return f16tof32(uint2(i & 0xffff, i >> 16));"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 2519 | TINT_ICE() << "unhandled data packing builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2520 | return false; |
| 2521 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2522 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2523 | return true; |
| 2524 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Jiawei Shao | 53c0fa9 | 2023-12-08 01:24:00 +0000 | [diff] [blame] | 2527 | bool ASTPrinter::EmitPacked4x8IntegerDotProductBuiltinCall(StringStream& out, |
| 2528 | const ast::CallExpression* expr, |
| 2529 | const sem::BuiltinFn* builtin) { |
Jiawei Shao | dd81723 | 2024-01-12 08:10:46 +0000 | [diff] [blame] | 2530 | switch (builtin->Fn()) { |
| 2531 | case wgsl::BuiltinFn::kDot4I8Packed: |
| 2532 | case wgsl::BuiltinFn::kDot4U8Packed: |
| 2533 | break; |
| 2534 | case wgsl::BuiltinFn::kPack4XI8: { |
| 2535 | out << "uint(pack_s8("; |
| 2536 | if (!EmitExpression(out, expr->args[0])) { |
| 2537 | return false; |
| 2538 | } |
| 2539 | out << "))"; |
| 2540 | return true; |
| 2541 | } |
| 2542 | case wgsl::BuiltinFn::kPack4XU8: { |
| 2543 | out << "uint(pack_u8("; |
| 2544 | if (!EmitExpression(out, expr->args[0])) { |
| 2545 | return false; |
| 2546 | } |
| 2547 | out << "))"; |
| 2548 | return true; |
| 2549 | } |
| 2550 | case wgsl::BuiltinFn::kPack4XI8Clamp: { |
| 2551 | out << "uint(pack_clamp_s8("; |
| 2552 | if (!EmitExpression(out, expr->args[0])) { |
| 2553 | return false; |
| 2554 | } |
| 2555 | out << "))"; |
| 2556 | return true; |
| 2557 | } |
| 2558 | case wgsl::BuiltinFn::kUnpack4XI8: { |
| 2559 | out << "unpack_s8s32(int8_t4_packed("; |
| 2560 | if (!EmitExpression(out, expr->args[0])) { |
| 2561 | return false; |
| 2562 | } |
| 2563 | out << "))"; |
| 2564 | return true; |
| 2565 | } |
| 2566 | case wgsl::BuiltinFn::kUnpack4XU8: { |
| 2567 | out << "unpack_u8u32(uint8_t4_packed("; |
| 2568 | if (!EmitExpression(out, expr->args[0])) { |
| 2569 | return false; |
| 2570 | } |
| 2571 | out << "))"; |
| 2572 | return true; |
| 2573 | } |
| 2574 | case wgsl::BuiltinFn::kPack4XU8Clamp: |
| 2575 | default: |
| 2576 | TINT_UNIMPLEMENTED() << builtin->Fn(); |
| 2577 | return false; |
| 2578 | } |
| 2579 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 2580 | return CallBuiltinHelper(out, expr, builtin, |
| 2581 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2582 | std::string functionName; |
| 2583 | switch (builtin->Fn()) { |
| 2584 | case wgsl::BuiltinFn::kDot4I8Packed: |
| 2585 | Line(b) << "int accumulator = 0;"; |
| 2586 | functionName = "dot4add_i8packed"; |
| 2587 | break; |
| 2588 | case wgsl::BuiltinFn::kDot4U8Packed: |
| 2589 | Line(b) << "uint accumulator = 0u;"; |
| 2590 | functionName = "dot4add_u8packed"; |
| 2591 | break; |
| 2592 | default: |
| 2593 | TINT_ICE() << "Internal error: unhandled DP4a builtin"; |
| 2594 | return false; |
| 2595 | } |
| 2596 | Line(b) << "return " << functionName << "(" << params[0] << ", " |
| 2597 | << params[1] << ", accumulator);"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2598 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 2599 | return true; |
| 2600 | }); |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2603 | bool ASTPrinter::EmitBarrierCall(StringStream& out, const sem::BuiltinFn* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2604 | // TODO(crbug.com/tint/661): Combine sequential barriers to a single |
| 2605 | // instruction. |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2606 | if (builtin->Fn() == wgsl::BuiltinFn::kWorkgroupBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2607 | out << "GroupMemoryBarrierWithGroupSync()"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2608 | } else if (builtin->Fn() == wgsl::BuiltinFn::kStorageBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2609 | out << "DeviceMemoryBarrierWithGroupSync()"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2610 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureBarrier) { |
James Price | a6287df | 2023-08-11 00:45:54 +0000 | [diff] [blame] | 2611 | out << "DeviceMemoryBarrierWithGroupSync()"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2612 | } else { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2613 | TINT_UNREACHABLE() << "unexpected barrier builtin type " << builtin->Fn(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2614 | return false; |
| 2615 | } |
| 2616 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
James Price | 501983f | 2023-08-08 01:37:22 +0000 | [diff] [blame] | 2619 | bool ASTPrinter::EmitSubgroupCall(StringStream& out, |
| 2620 | [[maybe_unused]] const ast::CallExpression* expr, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2621 | const sem::BuiltinFn* builtin) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2622 | if (builtin->Fn() == wgsl::BuiltinFn::kSubgroupBallot) { |
James Price | 501983f | 2023-08-08 01:37:22 +0000 | [diff] [blame] | 2623 | out << "WaveActiveBallot(true)"; |
| 2624 | } else { |
David Neto | eea786f | 2023-09-21 05:32:53 +0000 | [diff] [blame] | 2625 | // subgroupBroadcast is already handled in the regular builtin flow. |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2626 | TINT_UNREACHABLE() << "unexpected subgroup builtin type " << builtin->Fn(); |
James Price | 501983f | 2023-08-08 01:37:22 +0000 | [diff] [blame] | 2627 | return false; |
| 2628 | } |
| 2629 | return true; |
| 2630 | } |
| 2631 | |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 2632 | bool ASTPrinter::EmitTextureOrStorageBufferCallArgExpression(StringStream& out, |
| 2633 | const ast::Expression* expr) { |
| 2634 | // TODO(crbug.com/tint/1976): Workaround DXC bug that fails to compile texture/storage function |
| 2635 | // calls with signed integer splatted constants. DXC fails to convert the coord arg, for e.g. |
| 2636 | // `0.xxx`, from a vector of 64-bit ints to a vector of 32-bit ints to match the texture load |
| 2637 | // parameter type. We work around this for now by explicitly casting the splatted constant to |
| 2638 | // the right type, for e.g. `int3(0.xxx)`. |
| 2639 | bool emitted_cast = false; |
| 2640 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
| 2641 | if (auto* constant = sem->ConstantValue()) { |
| 2642 | if (auto* splat = constant->As<core::constant::Splat>()) { |
| 2643 | if (splat->Type()->is_signed_integer_vector()) { |
| 2644 | if (!EmitType(out, constant->Type(), core::AddressSpace::kUndefined, |
| 2645 | core::Access::kUndefined, "")) { |
| 2646 | return false; |
| 2647 | } |
| 2648 | out << "("; |
| 2649 | emitted_cast = true; |
| 2650 | } |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | if (!EmitExpression(out, expr)) { |
| 2655 | return false; |
| 2656 | } |
| 2657 | if (emitted_cast) { |
| 2658 | out << ")"; |
| 2659 | } |
| 2660 | return true; |
| 2661 | } |
| 2662 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2663 | bool ASTPrinter::EmitTextureCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2664 | const sem::Call* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2665 | const sem::BuiltinFn* builtin) { |
Ben Clayton | 015fbe7 | 2023-08-09 07:46:44 +0000 | [diff] [blame] | 2666 | using Usage = core::ParameterUsage; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2667 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2668 | auto& signature = builtin->Signature(); |
| 2669 | auto* expr = call->Declaration(); |
| 2670 | auto arguments = expr->args; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2671 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2672 | // Returns the argument with the given usage |
| 2673 | auto arg = [&](Usage usage) { |
| 2674 | int idx = signature.IndexOf(usage); |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 2675 | return (idx >= 0) ? arguments[static_cast<size_t>(idx)] : nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2676 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2677 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2678 | auto* texture = arg(Usage::kTexture); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2679 | if (TINT_UNLIKELY(!texture)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2680 | TINT_ICE() << "missing texture argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2681 | return false; |
| 2682 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2683 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2684 | auto* texture_type = TypeOf(texture)->UnwrapRef()->As<core::type::Texture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2685 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2686 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2687 | case wgsl::BuiltinFn::kTextureDimensions: |
| 2688 | case wgsl::BuiltinFn::kTextureNumLayers: |
| 2689 | case wgsl::BuiltinFn::kTextureNumLevels: |
| 2690 | case wgsl::BuiltinFn::kTextureNumSamples: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2691 | // All of these builtins use the GetDimensions() method on the texture |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2692 | bool is_ms = texture_type->IsAnyOf<core::type::MultisampledTexture, |
| 2693 | core::type::DepthMultisampledTexture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2694 | int num_dimensions = 0; |
| 2695 | std::string swizzle; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2696 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2697 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2698 | case wgsl::BuiltinFn::kTextureDimensions: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2699 | switch (texture_type->dim()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2700 | case core::type::TextureDimension::kNone: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2701 | TINT_ICE() << "texture dimension is kNone"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2702 | return false; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2703 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2704 | num_dimensions = 1; |
| 2705 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2706 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2707 | num_dimensions = is_ms ? 3 : 2; |
| 2708 | swizzle = is_ms ? ".xy" : ""; |
| 2709 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2710 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2711 | num_dimensions = is_ms ? 4 : 3; |
| 2712 | swizzle = ".xy"; |
| 2713 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2714 | case core::type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2715 | num_dimensions = 3; |
| 2716 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2717 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2718 | num_dimensions = 2; |
| 2719 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2720 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2721 | num_dimensions = 3; |
| 2722 | swizzle = ".xy"; |
| 2723 | break; |
| 2724 | } |
| 2725 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2726 | case wgsl::BuiltinFn::kTextureNumLayers: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2727 | switch (texture_type->dim()) { |
| 2728 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2729 | TINT_ICE() << "texture dimension is not arrayed"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2730 | return false; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2731 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2732 | num_dimensions = is_ms ? 4 : 3; |
| 2733 | swizzle = ".z"; |
| 2734 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2735 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2736 | num_dimensions = 3; |
| 2737 | swizzle = ".z"; |
| 2738 | break; |
| 2739 | } |
| 2740 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2741 | case wgsl::BuiltinFn::kTextureNumLevels: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2742 | switch (texture_type->dim()) { |
| 2743 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2744 | TINT_ICE() << "texture dimension does not support mips"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2745 | return false; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2746 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2747 | num_dimensions = 2; |
| 2748 | swizzle = ".y"; |
| 2749 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2750 | case core::type::TextureDimension::k2d: |
| 2751 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2752 | num_dimensions = 3; |
| 2753 | swizzle = ".z"; |
| 2754 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2755 | case core::type::TextureDimension::k2dArray: |
| 2756 | case core::type::TextureDimension::k3d: |
| 2757 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2758 | num_dimensions = 4; |
| 2759 | swizzle = ".w"; |
| 2760 | break; |
| 2761 | } |
| 2762 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2763 | case wgsl::BuiltinFn::kTextureNumSamples: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2764 | switch (texture_type->dim()) { |
| 2765 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2766 | TINT_ICE() << "texture dimension does not support multisampling"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2767 | return false; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2768 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2769 | num_dimensions = 3; |
| 2770 | swizzle = ".z"; |
| 2771 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2772 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2773 | num_dimensions = 4; |
| 2774 | swizzle = ".w"; |
| 2775 | break; |
| 2776 | } |
| 2777 | break; |
| 2778 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2779 | TINT_ICE() << "unexpected builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2780 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2781 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2782 | |
| 2783 | auto* level_arg = arg(Usage::kLevel); |
| 2784 | |
| 2785 | if (level_arg) { |
| 2786 | // `NumberOfLevels` is a non-optional argument if `MipLevel` was passed. |
| 2787 | // Increment the number of dimensions for the temporary vector to |
| 2788 | // accommodate this. |
| 2789 | num_dimensions++; |
| 2790 | |
| 2791 | // If the swizzle was empty, the expression will evaluate to the whole |
| 2792 | // vector. As we've grown the vector by one element, we now need to |
| 2793 | // swizzle to keep the result expression equivalent. |
| 2794 | if (swizzle.empty()) { |
| 2795 | static constexpr const char* swizzles[] = {"", ".x", ".xy", ".xyz"}; |
| 2796 | swizzle = swizzles[num_dimensions - 1]; |
| 2797 | } |
| 2798 | } |
| 2799 | |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2800 | if (TINT_UNLIKELY(num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2801 | TINT_ICE() << "Texture query builtin temporary vector has " << num_dimensions |
| 2802 | << " dimensions"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2803 | return false; |
| 2804 | } |
| 2805 | |
| 2806 | // Declare a variable to hold the queried texture info |
| 2807 | auto dims = UniqueIdentifier(kTempNamePrefix); |
| 2808 | if (num_dimensions == 1) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2809 | Line() << "uint " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2810 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2811 | Line() << "uint" << num_dimensions << " " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | { // texture.GetDimensions(...) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2815 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2816 | if (!EmitExpression(pre, texture)) { |
| 2817 | return false; |
| 2818 | } |
| 2819 | pre << ".GetDimensions("; |
| 2820 | |
| 2821 | if (level_arg) { |
| 2822 | if (!EmitExpression(pre, level_arg)) { |
| 2823 | return false; |
| 2824 | } |
| 2825 | pre << ", "; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2826 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureNumLevels) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2827 | pre << "0, "; |
| 2828 | } |
| 2829 | |
| 2830 | if (num_dimensions == 1) { |
| 2831 | pre << dims; |
| 2832 | } else { |
| 2833 | static constexpr char xyzw[] = {'x', 'y', 'z', 'w'}; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2834 | if (TINT_UNLIKELY(num_dimensions < 0 || num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2835 | TINT_ICE() << "vector dimensions are " << num_dimensions; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2836 | return false; |
| 2837 | } |
| 2838 | for (int i = 0; i < num_dimensions; i++) { |
| 2839 | if (i > 0) { |
| 2840 | pre << ", "; |
| 2841 | } |
| 2842 | pre << dims << "." << xyzw[i]; |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | pre << ");"; |
| 2847 | } |
| 2848 | |
| 2849 | // The out parameters of the GetDimensions() call is now in temporary |
| 2850 | // `dims` variable. This may be packed with other data, so the final |
| 2851 | // expression may require a swizzle. |
| 2852 | out << dims << swizzle; |
| 2853 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2854 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2855 | default: |
| 2856 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2857 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2858 | |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2859 | if (!EmitExpression(out, texture)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2860 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2861 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2862 | |
| 2863 | // If pack_level_in_coords is true, then the mip level will be appended as the |
| 2864 | // last value of the coordinates argument. If the WGSL builtin overload does |
| 2865 | // not have a level parameter and pack_level_in_coords is true, then a zero |
| 2866 | // mip level will be inserted. |
| 2867 | bool pack_level_in_coords = false; |
| 2868 | |
| 2869 | uint32_t hlsl_ret_width = 4u; |
| 2870 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 2871 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2872 | case wgsl::BuiltinFn::kTextureSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2873 | out << ".Sample("; |
| 2874 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2875 | case wgsl::BuiltinFn::kTextureSampleBias: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2876 | out << ".SampleBias("; |
| 2877 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2878 | case wgsl::BuiltinFn::kTextureSampleLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2879 | out << ".SampleLevel("; |
| 2880 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2881 | case wgsl::BuiltinFn::kTextureSampleGrad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2882 | out << ".SampleGrad("; |
| 2883 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2884 | case wgsl::BuiltinFn::kTextureSampleCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2885 | out << ".SampleCmp("; |
| 2886 | hlsl_ret_width = 1; |
| 2887 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2888 | case wgsl::BuiltinFn::kTextureSampleCompareLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2889 | out << ".SampleCmpLevelZero("; |
| 2890 | hlsl_ret_width = 1; |
| 2891 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2892 | case wgsl::BuiltinFn::kTextureLoad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2893 | out << ".Load("; |
Jiawei Shao | a54554a | 2023-11-13 08:24:12 +0000 | [diff] [blame] | 2894 | // Multisampled textures and read-write storage textures do not support mip-levels. |
| 2895 | if (texture_type->Is<core::type::MultisampledTexture>()) { |
| 2896 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2897 | } |
Jiawei Shao | a54554a | 2023-11-13 08:24:12 +0000 | [diff] [blame] | 2898 | if (auto* storage_texture_type = texture_type->As<core::type::StorageTexture>()) { |
| 2899 | if (storage_texture_type->access() == core::Access::kReadWrite) { |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | pack_level_in_coords = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2904 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2905 | case wgsl::BuiltinFn::kTextureGather: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2906 | out << ".Gather"; |
Ben Clayton | 015fbe7 | 2023-08-09 07:46:44 +0000 | [diff] [blame] | 2907 | if (builtin->Parameters()[0]->Usage() == core::ParameterUsage::kComponent) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 2908 | switch (call->Arguments()[0]->ConstantValue()->ValueAs<AInt>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2909 | case 0: |
| 2910 | out << "Red"; |
| 2911 | break; |
| 2912 | case 1: |
| 2913 | out << "Green"; |
| 2914 | break; |
| 2915 | case 2: |
| 2916 | out << "Blue"; |
| 2917 | break; |
| 2918 | case 3: |
| 2919 | out << "Alpha"; |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | out << "("; |
| 2924 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2925 | case wgsl::BuiltinFn::kTextureGatherCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2926 | out << ".GatherCmp("; |
| 2927 | break; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2928 | case wgsl::BuiltinFn::kTextureStore: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2929 | out << "["; |
| 2930 | break; |
| 2931 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 2932 | TINT_ICE() << "Unhandled texture builtin '" << builtin << "'"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2933 | return false; |
| 2934 | } |
| 2935 | |
| 2936 | if (auto* sampler = arg(Usage::kSampler)) { |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2937 | if (!EmitExpression(out, sampler)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2938 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2939 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2940 | out << ", "; |
| 2941 | } |
| 2942 | |
| 2943 | auto* param_coords = arg(Usage::kCoords); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2944 | if (TINT_UNLIKELY(!param_coords)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2945 | TINT_ICE() << "missing coords argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2946 | return false; |
| 2947 | } |
| 2948 | |
| 2949 | auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2950 | auto* i32 = builder_.create<core::type::I32>(); |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 2951 | auto* zero = builder_.Expr(0_i); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2952 | auto* stmt = builder_.Sem().Get(vector)->Stmt(); |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2953 | builder_.Sem().Add(zero, builder_.create<sem::ValueExpression>( |
Ben Clayton | 36c6155 | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 2954 | zero, i32, core::EvaluationStage::kRuntime, stmt, |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2955 | /* constant_value */ nullptr, |
| 2956 | /* has_side_effects */ false)); |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2957 | auto* packed = tint::wgsl::AppendVector(&builder_, vector, zero); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2958 | return EmitExpression(out, packed->Declaration()); |
| 2959 | }; |
| 2960 | |
| 2961 | auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { |
| 2962 | if (auto* level = arg(Usage::kLevel)) { |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2963 | auto* packed = tint::wgsl::AppendVector(&builder_, vector, level); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2964 | return EmitExpression(out, packed->Declaration()); |
| 2965 | } |
| 2966 | return emit_vector_appended_with_i32_zero(vector); |
| 2967 | }; |
| 2968 | |
| 2969 | if (auto* array_index = arg(Usage::kArrayIndex)) { |
| 2970 | // Array index needs to be appended to the coordinates. |
Ben Clayton | 2550b49 | 2023-10-11 10:41:12 +0000 | [diff] [blame] | 2971 | auto* packed = tint::wgsl::AppendVector(&builder_, param_coords, array_index); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2972 | if (pack_level_in_coords) { |
| 2973 | // Then mip level needs to be appended to the coordinates. |
| 2974 | if (!emit_vector_appended_with_level(packed->Declaration())) { |
| 2975 | return false; |
| 2976 | } |
| 2977 | } else { |
| 2978 | if (!EmitExpression(out, packed->Declaration())) { |
| 2979 | return false; |
| 2980 | } |
| 2981 | } |
| 2982 | } else if (pack_level_in_coords) { |
| 2983 | // Mip level needs to be appended to the coordinates. |
| 2984 | if (!emit_vector_appended_with_level(param_coords)) { |
| 2985 | return false; |
| 2986 | } |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2987 | } else if (builtin->Fn() == wgsl::BuiltinFn::kTextureStore) { |
Antonio Maiorano | b0dfccd | 2023-08-30 14:49:42 +0000 | [diff] [blame] | 2988 | // param_coords is an index expression, not a function arg |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2989 | if (!EmitExpression(out, param_coords)) { |
| 2990 | return false; |
| 2991 | } |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 2992 | } else if (!EmitTextureOrStorageBufferCallArgExpression(out, param_coords)) { |
Antonio Maiorano | b0dfccd | 2023-08-30 14:49:42 +0000 | [diff] [blame] | 2993 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2994 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2995 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2996 | for (auto usage : {Usage::kDepthRef, Usage::kBias, Usage::kLevel, Usage::kDdx, Usage::kDdy, |
| 2997 | Usage::kSampleIndex, Usage::kOffset}) { |
| 2998 | if (usage == Usage::kLevel && pack_level_in_coords) { |
| 2999 | continue; // mip level already packed in coordinates. |
| 3000 | } |
| 3001 | if (auto* e = arg(usage)) { |
| 3002 | out << ", "; |
Antonio Maiorano | d1368d7 | 2023-09-05 20:29:56 +0000 | [diff] [blame] | 3003 | if (!EmitTextureOrStorageBufferCallArgExpression(out, e)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3004 | return false; |
| 3005 | } |
| 3006 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3007 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3008 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3009 | if (builtin->Fn() == wgsl::BuiltinFn::kTextureStore) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3010 | out << "] = "; |
| 3011 | if (!EmitExpression(out, arg(Usage::kValue))) { |
| 3012 | return false; |
| 3013 | } |
| 3014 | } else { |
| 3015 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3016 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3017 | // If the builtin return type does not match the number of elements of the |
| 3018 | // HLSL builtin, we need to swizzle the expression to generate the correct |
| 3019 | // number of components. |
| 3020 | uint32_t wgsl_ret_width = 1; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3021 | if (auto* vec = builtin->ReturnType()->As<core::type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3022 | wgsl_ret_width = vec->Width(); |
| 3023 | } |
| 3024 | if (wgsl_ret_width < hlsl_ret_width) { |
| 3025 | out << "."; |
| 3026 | for (uint32_t i = 0; i < wgsl_ret_width; i++) { |
| 3027 | out << "xyz"[i]; |
| 3028 | } |
| 3029 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 3030 | if (TINT_UNLIKELY(wgsl_ret_width > hlsl_ret_width)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3031 | TINT_ICE() << "WGSL return width (" << wgsl_ret_width |
| 3032 | << ") is wider than HLSL return width (" << hlsl_ret_width << ") for " |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 3033 | << builtin->Fn(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3034 | return false; |
| 3035 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3036 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3037 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3038 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 3041 | std::string ASTPrinter::generate_builtin_name(const sem::BuiltinFn* builtin) { |
| 3042 | switch (builtin->Fn()) { |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3043 | case wgsl::BuiltinFn::kAbs: |
| 3044 | case wgsl::BuiltinFn::kAcos: |
| 3045 | case wgsl::BuiltinFn::kAll: |
| 3046 | case wgsl::BuiltinFn::kAny: |
| 3047 | case wgsl::BuiltinFn::kAsin: |
| 3048 | case wgsl::BuiltinFn::kAtan: |
| 3049 | case wgsl::BuiltinFn::kAtan2: |
| 3050 | case wgsl::BuiltinFn::kCeil: |
| 3051 | case wgsl::BuiltinFn::kClamp: |
| 3052 | case wgsl::BuiltinFn::kCos: |
| 3053 | case wgsl::BuiltinFn::kCosh: |
| 3054 | case wgsl::BuiltinFn::kCross: |
| 3055 | case wgsl::BuiltinFn::kDeterminant: |
| 3056 | case wgsl::BuiltinFn::kDistance: |
| 3057 | case wgsl::BuiltinFn::kDot: |
| 3058 | case wgsl::BuiltinFn::kExp: |
| 3059 | case wgsl::BuiltinFn::kExp2: |
| 3060 | case wgsl::BuiltinFn::kFloor: |
| 3061 | case wgsl::BuiltinFn::kFrexp: |
| 3062 | case wgsl::BuiltinFn::kLdexp: |
| 3063 | case wgsl::BuiltinFn::kLength: |
| 3064 | case wgsl::BuiltinFn::kLog: |
| 3065 | case wgsl::BuiltinFn::kLog2: |
| 3066 | case wgsl::BuiltinFn::kMax: |
| 3067 | case wgsl::BuiltinFn::kMin: |
| 3068 | case wgsl::BuiltinFn::kModf: |
| 3069 | case wgsl::BuiltinFn::kNormalize: |
| 3070 | case wgsl::BuiltinFn::kPow: |
| 3071 | case wgsl::BuiltinFn::kReflect: |
| 3072 | case wgsl::BuiltinFn::kRefract: |
| 3073 | case wgsl::BuiltinFn::kRound: |
| 3074 | case wgsl::BuiltinFn::kSaturate: |
| 3075 | case wgsl::BuiltinFn::kSin: |
| 3076 | case wgsl::BuiltinFn::kSinh: |
| 3077 | case wgsl::BuiltinFn::kSqrt: |
| 3078 | case wgsl::BuiltinFn::kStep: |
| 3079 | case wgsl::BuiltinFn::kTan: |
| 3080 | case wgsl::BuiltinFn::kTanh: |
| 3081 | case wgsl::BuiltinFn::kTranspose: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3082 | return builtin->str(); |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3083 | case wgsl::BuiltinFn::kCountOneBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3084 | return "countbits"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3085 | case wgsl::BuiltinFn::kDpdx: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3086 | return "ddx"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3087 | case wgsl::BuiltinFn::kDpdxCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3088 | return "ddx_coarse"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3089 | case wgsl::BuiltinFn::kDpdxFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3090 | return "ddx_fine"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3091 | case wgsl::BuiltinFn::kDpdy: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3092 | return "ddy"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3093 | case wgsl::BuiltinFn::kDpdyCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3094 | return "ddy_coarse"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3095 | case wgsl::BuiltinFn::kDpdyFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3096 | return "ddy_fine"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3097 | case wgsl::BuiltinFn::kFaceForward: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3098 | return "faceforward"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3099 | case wgsl::BuiltinFn::kFract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3100 | return "frac"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3101 | case wgsl::BuiltinFn::kFma: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3102 | return "mad"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3103 | case wgsl::BuiltinFn::kFwidth: |
| 3104 | case wgsl::BuiltinFn::kFwidthCoarse: |
| 3105 | case wgsl::BuiltinFn::kFwidthFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3106 | return "fwidth"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3107 | case wgsl::BuiltinFn::kInverseSqrt: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3108 | return "rsqrt"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3109 | case wgsl::BuiltinFn::kMix: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3110 | return "lerp"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3111 | case wgsl::BuiltinFn::kReverseBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3112 | return "reversebits"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3113 | case wgsl::BuiltinFn::kSmoothstep: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3114 | return "smoothstep"; |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 3115 | case wgsl::BuiltinFn::kSubgroupBroadcast: |
David Neto | eea786f | 2023-09-21 05:32:53 +0000 | [diff] [blame] | 3116 | return "WaveReadLaneAt"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3117 | default: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3118 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3119 | << "Unknown builtin method: " << builtin->str(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3120 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3121 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3122 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3125 | bool ASTPrinter::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3126 | auto* stmt = s->body[case_idx]; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3127 | auto* sem = builder_.Sem().Get<sem::CaseStatement>(stmt); |
| 3128 | for (auto* selector : sem->Selectors()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3129 | auto out = Line(); |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3130 | if (selector->IsDefault()) { |
| 3131 | out << "default"; |
| 3132 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3133 | out << "case "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3134 | if (!EmitConstant(out, selector->Value(), /* is_variable_initializer */ false)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3135 | return false; |
| 3136 | } |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 3137 | } |
| 3138 | out << ":"; |
| 3139 | if (selector == sem->Selectors().back()) { |
| 3140 | out << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3144 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3145 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3146 | DecrementIndent(); |
| 3147 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3148 | }); |
| 3149 | |
| 3150 | // Emit the case statement |
| 3151 | if (!EmitStatements(stmt->body->statements)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3152 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3153 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3154 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3155 | if (!tint::IsAnyOf<ast::BreakStatement>(stmt->body->Last())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3156 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3157 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3158 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3159 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3162 | bool ASTPrinter::EmitContinue(const ast::ContinueStatement*) { |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 3163 | if (!emit_continuing_ || !emit_continuing_()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3164 | return false; |
| 3165 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3166 | Line() << "continue;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3167 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3170 | bool ASTPrinter::EmitDiscard(const ast::DiscardStatement*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3171 | // TODO(dsinclair): Verify this is correct when the discard semantics are |
| 3172 | // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3173 | Line() << "discard;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3174 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3177 | bool ASTPrinter::EmitExpression(StringStream& out, const ast::Expression* expr) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 3178 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3179 | if (auto* constant = sem->ConstantValue()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3180 | bool is_variable_initializer = false; |
| 3181 | if (auto* stmt = sem->Stmt()) { |
| 3182 | if (auto* decl = As<ast::VariableDeclStatement>(stmt->Declaration())) { |
| 3183 | is_variable_initializer = decl->variable->initializer == expr; |
| 3184 | } |
| 3185 | } |
| 3186 | return EmitConstant(out, constant, is_variable_initializer); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3187 | } |
| 3188 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3189 | return Switch( |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 3190 | expr, // |
| 3191 | [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, |
| 3192 | [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 3193 | [&](const ast::CallExpression* c) { return EmitCall(out, c); }, |
| 3194 | [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, |
| 3195 | [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, |
| 3196 | [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3197 | [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, // |
| 3198 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3201 | bool ASTPrinter::EmitIdentifier(StringStream& out, const ast::IdentifierExpression* expr) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3202 | out << expr->identifier->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3203 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3206 | bool ASTPrinter::EmitIf(const ast::IfStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3207 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3208 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3209 | out << "if ("; |
| 3210 | if (!EmitExpression(out, stmt->condition)) { |
| 3211 | return false; |
| 3212 | } |
| 3213 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3214 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3215 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3216 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
James Price | 26ebe5e | 2022-04-29 00:14:53 +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 | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3220 | if (stmt->else_statement) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3221 | Line() << "} else {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3222 | if (auto* block = stmt->else_statement->As<ast::BlockStatement>()) { |
| 3223 | if (!EmitStatementsWithIndent(block->statements)) { |
| 3224 | return false; |
| 3225 | } |
| 3226 | } else { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3227 | if (!EmitStatementsWithIndent(Vector{stmt->else_statement})) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3228 | return false; |
| 3229 | } |
| 3230 | } |
| 3231 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3232 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3233 | |
| 3234 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3237 | bool ASTPrinter::EmitFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3238 | auto* sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3239 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3240 | // Emit storage atomic helpers |
Ben Clayton | 7a5f54ea | 2023-09-06 16:58:22 +0000 | [diff] [blame] | 3241 | if (auto* intrinsic = ast::GetAttribute<DecomposeMemoryAccess::Intrinsic>(func->attributes)) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3242 | if (intrinsic->address_space == core::AddressSpace::kStorage && intrinsic->IsAtomic()) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3243 | if (!EmitStorageAtomicIntrinsic(func, intrinsic)) { |
| 3244 | return false; |
| 3245 | } |
| 3246 | } |
| 3247 | return true; |
| 3248 | } |
| 3249 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3250 | if (ast::HasAttribute<ast::InternalAttribute>(func->attributes)) { |
| 3251 | // An internal function. Do not emit. |
| 3252 | return true; |
| 3253 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3254 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3255 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3256 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3257 | auto name = func->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3258 | // If the function returns an array, then we need to declare a typedef for |
| 3259 | // this. |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3260 | if (sem->ReturnType()->Is<core::type::Array>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3261 | auto typedef_name = UniqueIdentifier(name + "_ret"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3262 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3263 | pre << "typedef "; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3264 | if (!EmitTypeAndName(pre, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3265 | core::Access::kReadWrite, typedef_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3266 | return false; |
| 3267 | } |
| 3268 | pre << ";"; |
| 3269 | out << typedef_name; |
| 3270 | } else { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3271 | if (!EmitType(out, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3272 | core::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3273 | return false; |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | out << " " << name << "("; |
| 3278 | |
| 3279 | bool first = true; |
| 3280 | |
| 3281 | for (auto* v : sem->Parameters()) { |
| 3282 | if (!first) { |
| 3283 | out << ", "; |
| 3284 | } |
| 3285 | first = false; |
| 3286 | |
| 3287 | auto const* type = v->Type(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3288 | auto address_space = core::AddressSpace::kUndefined; |
| 3289 | auto access = core::Access::kUndefined; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3290 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3291 | if (auto* ptr = type->As<core::type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3292 | type = ptr->StoreType(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3293 | switch (ptr->AddressSpace()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3294 | case core::AddressSpace::kStorage: |
| 3295 | case core::AddressSpace::kUniform: |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3296 | // Not allowed by WGSL, but is used by certain transforms (e.g. DMA) to pass |
| 3297 | // storage buffers and uniform buffers down into transform-generated |
| 3298 | // functions. In this situation we want to generate the parameter without an |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3299 | // 'inout', using the address space and access from the pointer. |
| 3300 | address_space = ptr->AddressSpace(); |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3301 | access = ptr->Access(); |
| 3302 | break; |
| 3303 | default: |
| 3304 | // Transform regular WGSL pointer parameters in to `inout` parameters. |
| 3305 | out << "inout "; |
| 3306 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3309 | // Note: WGSL only allows for AddressSpace::kUndefined on parameters, however |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3310 | // the sanitizer transforms generates load / store functions for storage |
| 3311 | // or uniform buffers. These functions have a buffer parameter with |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3312 | // AddressSpace::kStorage or AddressSpace::kUniform. This is required to |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3313 | // correctly translate the parameter to a [RW]ByteAddressBuffer for |
| 3314 | // storage buffers and a uint4[N] for uniform buffers. |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3315 | if (!EmitTypeAndName(out, type, address_space, access, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3316 | v->Declaration()->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3317 | return false; |
| 3318 | } |
| 3319 | } |
| 3320 | out << ") {"; |
| 3321 | } |
| 3322 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3323 | if (sem->DiscardStatement() && !sem->ReturnType()->Is<core::type::Void>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3324 | // BUG(crbug.com/tint/1081): work around non-void functions with discard |
| 3325 | // failing compilation sometimes |
| 3326 | if (!EmitFunctionBodyWithDiscard(func)) { |
| 3327 | return false; |
| 3328 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3329 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3330 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3331 | return false; |
| 3332 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3335 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3336 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3337 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3340 | bool ASTPrinter::EmitFunctionBodyWithDiscard(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3341 | // FXC sometimes fails to compile functions that discard with 'Not all control |
| 3342 | // paths return a value'. We work around this by wrapping the function body |
| 3343 | // within an "if (true) { <body> } return <default return type obj>;" so that |
| 3344 | // there is always an (unused) return statement. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3345 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3346 | auto* sem = builder_.Sem().Get(func); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3347 | TINT_ASSERT(sem->DiscardStatement() && !sem->ReturnType()->Is<core::type::Void>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3348 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3349 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3350 | Line() << "if (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3351 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3352 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3353 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3354 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3355 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3356 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3357 | |
| 3358 | // Return an unused result that matches the type of the return value |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3359 | auto name = builder_.Symbols().New("unused").Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3360 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3361 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3362 | if (!EmitTypeAndName(out, sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3363 | core::Access::kReadWrite, name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3364 | return false; |
| 3365 | } |
| 3366 | out << ";"; |
| 3367 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3368 | Line() << "return " << name << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3369 | |
| 3370 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3373 | bool ASTPrinter::EmitGlobalVariable(const ast::Variable* global) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3374 | return Switch( |
| 3375 | global, // |
| 3376 | [&](const ast::Var* var) { |
| 3377 | auto* sem = builder_.Sem().Get(global); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3378 | switch (sem->AddressSpace()) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3379 | case core::AddressSpace::kUniform: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3380 | return EmitUniformVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3381 | case core::AddressSpace::kStorage: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3382 | return EmitStorageVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3383 | case core::AddressSpace::kHandle: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3384 | return EmitHandleVariable(var, sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3385 | case core::AddressSpace::kPrivate: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3386 | return EmitPrivateVariable(sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3387 | case core::AddressSpace::kWorkgroup: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3388 | return EmitWorkgroupVariable(sem); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3389 | case core::AddressSpace::kPushConstant: |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3390 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3391 | << "unhandled address space " << sem->AddressSpace(); |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3392 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3393 | default: { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3394 | TINT_ICE() << "unhandled address space " << sem->AddressSpace(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3395 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3396 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3397 | } |
| 3398 | }, |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3399 | [&](const ast::Override*) { |
| 3400 | // Override is removed with SubstituteOverride |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3401 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3402 | << "override-expressions should have been removed with the SubstituteOverride " |
| 3403 | "transform"; |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3404 | return false; |
| 3405 | }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3406 | [&](const ast::Const*) { |
| 3407 | return true; // Constants are embedded at their use |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3408 | }, // |
| 3409 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3412 | bool ASTPrinter::EmitUniformVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3413 | auto binding_point = *sem->As<sem::GlobalVariable>()->Attributes().binding_point; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3414 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3415 | auto name = var->name->symbol.Name(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3416 | Line() << "cbuffer cbuffer_" << name << RegisterAndSpace('b', binding_point) << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3417 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3418 | { |
| 3419 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3420 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3421 | if (!EmitTypeAndName(out, type, core::AddressSpace::kUniform, sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3422 | return false; |
| 3423 | } |
| 3424 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3425 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3426 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3427 | Line() << "};"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3428 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3429 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3432 | bool ASTPrinter::EmitStorageVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3433 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3434 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3435 | if (!EmitTypeAndName(out, type, core::AddressSpace::kStorage, sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3436 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3437 | return false; |
| 3438 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3439 | |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3440 | auto* global_sem = sem->As<sem::GlobalVariable>(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3441 | out << RegisterAndSpace(sem->Access() == core::Access::kRead ? 't' : 'u', |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3442 | *global_sem->Attributes().binding_point) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3443 | << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3444 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3445 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3448 | bool ASTPrinter::EmitHandleVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3449 | auto* unwrapped_type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3450 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3451 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3452 | auto name = var->name->symbol.Name(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3453 | auto* type = sem->Type()->UnwrapRef(); |
Jiawei Shao | a87d063 | 2023-11-16 00:21:08 +0000 | [diff] [blame] | 3454 | if (ast::HasAttribute<PixelLocal::RasterizerOrderedView>(var->attributes)) { |
| 3455 | TINT_ASSERT(!type->Is<core::type::MultisampledTexture>()); |
| 3456 | auto* storage = type->As<core::type::StorageTexture>(); |
| 3457 | if (!storage) { |
| 3458 | TINT_ICE() << "Rasterizer Ordered View type isn't storage texture"; |
| 3459 | return false; |
| 3460 | } |
| 3461 | out << "RasterizerOrderedTexture2D"; |
| 3462 | auto* component = image_format_to_rwtexture_type(storage->texel_format()); |
| 3463 | if (TINT_UNLIKELY(!component)) { |
| 3464 | TINT_ICE() << "Unsupported StorageTexture TexelFormat: " |
| 3465 | << static_cast<int>(storage->texel_format()); |
| 3466 | return false; |
| 3467 | } |
| 3468 | out << "<" << component << "> " << name; |
| 3469 | } else if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3470 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3471 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3472 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3473 | const char* register_space = nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3474 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3475 | if (unwrapped_type->Is<core::type::Texture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3476 | register_space = "t"; |
James Price | 88c231b | 2023-08-15 12:47:28 +0000 | [diff] [blame] | 3477 | if (auto* st = unwrapped_type->As<core::type::StorageTexture>(); |
| 3478 | st && st->access() != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3479 | register_space = "u"; |
| 3480 | } |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3481 | } else if (unwrapped_type->Is<core::type::Sampler>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3482 | register_space = "s"; |
| 3483 | } |
| 3484 | |
| 3485 | if (register_space) { |
Ben Clayton | 65e3824 | 2023-11-09 19:37:44 +0000 | [diff] [blame] | 3486 | auto bp = sem->As<sem::GlobalVariable>()->Attributes().binding_point; |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3487 | out << " : register(" << register_space << bp->binding; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3488 | // Omit the space if it's 0, as it's the default. |
| 3489 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better |
| 3490 | // compatibility. |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3491 | if (bp->group == 0) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3492 | out << ")"; |
| 3493 | } else { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3494 | out << ", space" << bp->group << ")"; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3495 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | out << ";"; |
| 3499 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3502 | bool ASTPrinter::EmitPrivateVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3503 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3504 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3505 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3506 | out << "static "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3507 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3508 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3509 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3510 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3511 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3512 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3513 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3514 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3515 | if (auto* initializer = decl->initializer) { |
| 3516 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3517 | return false; |
| 3518 | } |
| 3519 | } else { |
| 3520 | if (!EmitZeroValue(out, var->Type()->UnwrapRef())) { |
| 3521 | return false; |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | out << ";"; |
| 3526 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3529 | bool ASTPrinter::EmitWorkgroupVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3530 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3531 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3532 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3533 | out << "groupshared "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3534 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3535 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3536 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3537 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3538 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3539 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3540 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3541 | if (auto* initializer = decl->initializer) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3542 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3543 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3544 | return false; |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | out << ";"; |
| 3549 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3552 | std::string ASTPrinter::builtin_to_attribute(core::BuiltinValue builtin) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3553 | switch (builtin) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3554 | case core::BuiltinValue::kPosition: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3555 | return "SV_Position"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3556 | case core::BuiltinValue::kVertexIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3557 | return "SV_VertexID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3558 | case core::BuiltinValue::kInstanceIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3559 | return "SV_InstanceID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3560 | case core::BuiltinValue::kFrontFacing: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3561 | return "SV_IsFrontFace"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3562 | case core::BuiltinValue::kFragDepth: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3563 | return "SV_Depth"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3564 | case core::BuiltinValue::kLocalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3565 | return "SV_GroupThreadID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3566 | case core::BuiltinValue::kLocalInvocationIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3567 | return "SV_GroupIndex"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3568 | case core::BuiltinValue::kGlobalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3569 | return "SV_DispatchThreadID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3570 | case core::BuiltinValue::kWorkgroupId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3571 | return "SV_GroupID"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3572 | case core::BuiltinValue::kSampleIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3573 | return "SV_SampleIndex"; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3574 | case core::BuiltinValue::kSampleMask: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3575 | return "SV_Coverage"; |
| 3576 | default: |
| 3577 | break; |
| 3578 | } |
| 3579 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3582 | std::string ASTPrinter::interpolation_to_modifiers(core::InterpolationType type, |
| 3583 | core::InterpolationSampling sampling) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3584 | std::string modifiers; |
| 3585 | switch (type) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3586 | case core::InterpolationType::kPerspective: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3587 | modifiers += "linear "; |
| 3588 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3589 | case core::InterpolationType::kLinear: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3590 | modifiers += "noperspective "; |
| 3591 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3592 | case core::InterpolationType::kFlat: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3593 | modifiers += "nointerpolation "; |
| 3594 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3595 | case core::InterpolationType::kUndefined: |
Ben Clayton | f9ed9d3 | 2022-10-11 19:49:17 +0000 | [diff] [blame] | 3596 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3597 | } |
| 3598 | switch (sampling) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3599 | case core::InterpolationSampling::kCentroid: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3600 | modifiers += "centroid "; |
| 3601 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3602 | case core::InterpolationSampling::kSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3603 | modifiers += "sample "; |
| 3604 | break; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3605 | case core::InterpolationSampling::kCenter: |
| 3606 | case core::InterpolationSampling::kUndefined: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3607 | break; |
| 3608 | } |
| 3609 | return modifiers; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3612 | bool ASTPrinter::EmitEntryPointFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3613 | auto* func_sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3614 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3615 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3616 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3617 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 3618 | // Emit the workgroup_size attribute. |
| 3619 | auto wgsize = func_sem->WorkgroupSize(); |
| 3620 | out << "[numthreads("; |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 3621 | for (size_t i = 0; i < 3; i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3622 | if (i > 0) { |
| 3623 | out << ", "; |
| 3624 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3625 | if (!wgsize[i].has_value()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3626 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3627 | << "override-expressions should have been removed with the " |
| 3628 | "SubstituteOverride transform"; |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3629 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3630 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3631 | out << std::to_string(wgsize[i].value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3632 | } |
| 3633 | out << ")]" << std::endl; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3636 | if (!EmitTypeAndName(out, func_sem->ReturnType(), core::AddressSpace::kUndefined, |
| 3637 | core::Access::kUndefined, func->name->symbol.Name())) { |
Ben Clayton | 1906857 | 2023-02-07 21:28:09 +0000 | [diff] [blame] | 3638 | return false; |
| 3639 | } |
| 3640 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3641 | |
| 3642 | bool first = true; |
| 3643 | |
| 3644 | // Emit entry point parameters. |
| 3645 | for (auto* var : func->params) { |
| 3646 | auto* sem = builder_.Sem().Get(var); |
| 3647 | auto* type = sem->Type(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3648 | if (TINT_UNLIKELY(!type->Is<core::type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3649 | // ICE likely indicates that the CanonicalizeEntryPointIO transform was |
| 3650 | // not run, or a builtin parameter was added after it was run. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3651 | TINT_ICE() << "Unsupported non-struct entry point parameter"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | if (!first) { |
| 3655 | out << ", "; |
| 3656 | } |
| 3657 | first = false; |
| 3658 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3659 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3660 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3661 | return false; |
| 3662 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3663 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3664 | |
| 3665 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3668 | { |
| 3669 | ScopedIndent si(this); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3670 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3671 | if (!EmitStatements(func->body->statements)) { |
| 3672 | return false; |
| 3673 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3674 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3675 | if (!Is<ast::ReturnStatement>(func->body->Last())) { |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 3676 | ast::ReturnStatement ret(GenerationID(), ast::NodeID{}, Source{}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3677 | if (!EmitStatement(&ret)) { |
| 3678 | return false; |
| 3679 | } |
| 3680 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3683 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3684 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3685 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3688 | bool ASTPrinter::EmitConstant(StringStream& out, |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 3689 | const core::constant::Value* constant, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3690 | bool is_variable_initializer) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3691 | return Switch( |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3692 | constant->Type(), // |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3693 | [&](const core::type::Bool*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3694 | out << (constant->ValueAs<AInt>() ? "true" : "false"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3695 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3696 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3697 | [&](const core::type::F32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3698 | PrintF32(out, constant->ValueAs<f32>()); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3699 | return true; |
| 3700 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3701 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3702 | // emit a f16 scalar with explicit float16_t type declaration. |
| 3703 | out << "float16_t("; |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3704 | PrintF16(out, constant->ValueAs<f16>()); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3705 | out << ")"; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3706 | return true; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3707 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3708 | [&](const core::type::I32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3709 | out << constant->ValueAs<AInt>(); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3710 | return true; |
| 3711 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3712 | [&](const core::type::U32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3713 | out << constant->ValueAs<AInt>() << "u"; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3714 | return true; |
| 3715 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3716 | [&](const core::type::Vector* v) { |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 3717 | if (auto* splat = constant->As<core::constant::Splat>()) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3718 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3719 | ScopedParen sp(out); |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3720 | if (!EmitConstant(out, splat->el, is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3721 | return false; |
| 3722 | } |
| 3723 | } |
| 3724 | out << "."; |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3725 | for (size_t i = 0; i < v->Width(); i++) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3726 | out << "x"; |
| 3727 | } |
| 3728 | return true; |
| 3729 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3730 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3731 | if (!EmitType(out, v, core::AddressSpace::kUndefined, core::Access::kUndefined, "")) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3732 | return false; |
| 3733 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3734 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3735 | ScopedParen sp(out); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3736 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3737 | for (size_t i = 0; i < v->Width(); i++) { |
| 3738 | if (i > 0) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3739 | out << ", "; |
| 3740 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3741 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3742 | return false; |
| 3743 | } |
| 3744 | } |
| 3745 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3746 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3747 | [&](const core::type::Matrix* m) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3748 | if (!EmitType(out, m, core::AddressSpace::kUndefined, core::Access::kUndefined, "")) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3749 | return false; |
| 3750 | } |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3751 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3752 | ScopedParen sp(out); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3753 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3754 | for (size_t i = 0; i < m->columns(); i++) { |
| 3755 | if (i > 0) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3756 | out << ", "; |
| 3757 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3758 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3759 | return false; |
| 3760 | } |
| 3761 | } |
| 3762 | return true; |
| 3763 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3764 | [&](const core::type::Array* a) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3765 | if (constant->AllZero()) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3766 | out << "("; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3767 | if (!EmitType(out, a, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 3768 | "")) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3769 | return false; |
| 3770 | } |
| 3771 | out << ")0"; |
| 3772 | return true; |
| 3773 | } |
| 3774 | |
| 3775 | out << "{"; |
| 3776 | TINT_DEFER(out << "}"); |
| 3777 | |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3778 | auto count = a->ConstantCount(); |
| 3779 | if (!count) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3780 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3781 | << core::type::Array::kErrExpectedConstantCount; |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3782 | return false; |
| 3783 | } |
| 3784 | |
| 3785 | for (size_t i = 0; i < count; i++) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3786 | if (i > 0) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3787 | out << ", "; |
| 3788 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3789 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3790 | return false; |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | return true; |
| 3795 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3796 | [&](const core::type::Struct* s) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3797 | if (!EmitStructType(&helpers_, s)) { |
| 3798 | return false; |
| 3799 | } |
| 3800 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3801 | if (constant->AllZero()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3802 | out << "(" << StructName(s) << ")0"; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3803 | return true; |
| 3804 | } |
| 3805 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3806 | auto emit_member_values = [&](StringStream& o) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3807 | o << "{"; |
dan sinclair | ad9cd0a | 2022-12-06 20:01:54 +0000 | [diff] [blame] | 3808 | for (size_t i = 0; i < s->Members().Length(); i++) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3809 | if (i > 0) { |
| 3810 | o << ", "; |
| 3811 | } |
| 3812 | if (!EmitConstant(o, constant->Index(i), is_variable_initializer)) { |
| 3813 | return false; |
| 3814 | } |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3815 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3816 | o << "}"; |
| 3817 | return true; |
| 3818 | }; |
| 3819 | |
| 3820 | if (is_variable_initializer) { |
| 3821 | if (!emit_member_values(out)) { |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3822 | return false; |
| 3823 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3824 | } else { |
| 3825 | // HLSL requires structure initializers to be assigned directly to a variable. |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3826 | // For these constants use 'static const' at global-scope. 'const' at global scope |
| 3827 | // creates a variable who's initializer is ignored, and the value is expected to be |
| 3828 | // provided in a cbuffer. 'static const' is a true value-embedded-in-the-shader-code |
| 3829 | // constant. We also emit these for function-local constant expressions for |
| 3830 | // consistency and to ensure that these are not computed at execution time. |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3831 | auto name = UniqueIdentifier("c"); |
| 3832 | { |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3833 | StringStream decl; |
| 3834 | decl << "static const " << StructName(s) << " " << name << " = "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3835 | if (!emit_member_values(decl)) { |
| 3836 | return false; |
| 3837 | } |
| 3838 | decl << ";"; |
Ben Clayton | 2d501c5 | 2024-01-17 01:27:37 +0000 | [diff] [blame] | 3839 | current_buffer_->Insert(decl.str(), global_insertion_point_++, 0); |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3840 | } |
| 3841 | out << name; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3845 | }, // |
| 3846 | TINT_ICE_ON_NO_MATCH); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3849 | bool ASTPrinter::EmitLiteral(StringStream& out, const ast::LiteralExpression* lit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3850 | return Switch( |
| 3851 | lit, |
| 3852 | [&](const ast::BoolLiteralExpression* l) { |
| 3853 | out << (l->value ? "true" : "false"); |
| 3854 | return true; |
| 3855 | }, |
Ben Clayton | 3ad927c | 2022-05-25 23:12:14 +0000 | [diff] [blame] | 3856 | [&](const ast::FloatLiteralExpression* l) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3857 | if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) { |
| 3858 | // Emit f16 literal with explicit float16_t type declaration. |
| 3859 | out << "float16_t("; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3860 | PrintF16(out, static_cast<float>(l->value)); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3861 | out << ")"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3862 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3863 | PrintF32(out, static_cast<float>(l->value)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3864 | return true; |
| 3865 | }, |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3866 | [&](const ast::IntLiteralExpression* i) { |
| 3867 | out << i->value; |
| 3868 | switch (i->suffix) { |
| 3869 | case ast::IntLiteralExpression::Suffix::kNone: |
| 3870 | case ast::IntLiteralExpression::Suffix::kI: |
| 3871 | return true; |
| 3872 | case ast::IntLiteralExpression::Suffix::kU: |
| 3873 | out << "u"; |
| 3874 | return true; |
| 3875 | } |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 3876 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 3877 | << "unknown integer literal suffix type"; |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3878 | return false; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3879 | }, // |
| 3880 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3883 | bool ASTPrinter::EmitValue(StringStream& out, const core::type::Type* type, int value) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3884 | return Switch( |
| 3885 | type, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3886 | [&](const core::type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3887 | out << (value == 0 ? "false" : "true"); |
| 3888 | return true; |
| 3889 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3890 | [&](const core::type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3891 | out << value << ".0f"; |
| 3892 | return true; |
| 3893 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3894 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3895 | out << "float16_t(" << value << ".0h)"; |
| 3896 | return true; |
| 3897 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3898 | [&](const core::type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3899 | out << value; |
| 3900 | return true; |
| 3901 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3902 | [&](const core::type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3903 | out << value << "u"; |
| 3904 | return true; |
| 3905 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3906 | [&](const core::type::Vector* vec) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3907 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3908 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3909 | return false; |
| 3910 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3911 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3912 | for (uint32_t i = 0; i < vec->Width(); i++) { |
| 3913 | if (i != 0) { |
| 3914 | out << ", "; |
| 3915 | } |
| 3916 | if (!EmitValue(out, vec->type(), value)) { |
| 3917 | return false; |
| 3918 | } |
| 3919 | } |
| 3920 | return true; |
| 3921 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3922 | [&](const core::type::Matrix* mat) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3923 | if (!EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3924 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3925 | return false; |
| 3926 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3927 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3928 | for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { |
| 3929 | if (i != 0) { |
| 3930 | out << ", "; |
| 3931 | } |
| 3932 | if (!EmitValue(out, mat->type(), value)) { |
| 3933 | return false; |
| 3934 | } |
| 3935 | } |
| 3936 | return true; |
| 3937 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3938 | [&](const core::type::Struct*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3939 | out << "("; |
| 3940 | TINT_DEFER(out << ")" << value); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3941 | return EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 3942 | ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3943 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3944 | [&](const core::type::Array*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3945 | out << "("; |
| 3946 | TINT_DEFER(out << ")" << value); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 3947 | return EmitType(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
| 3948 | ""); |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 3949 | }, // |
| 3950 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 3953 | bool ASTPrinter::EmitZeroValue(StringStream& out, const core::type::Type* type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3954 | return EmitValue(out, type, 0); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3957 | bool ASTPrinter::EmitLoop(const ast::LoopStatement* stmt) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 3958 | auto emit_continuing = [this, stmt] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3959 | if (stmt->continuing && !stmt->continuing->Empty()) { |
| 3960 | if (!EmitBlock(stmt->continuing)) { |
| 3961 | return false; |
| 3962 | } |
| 3963 | } |
| 3964 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3965 | }; |
| 3966 | |
| 3967 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3968 | Line() << "while (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3969 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3970 | ScopedIndent si(this); |
| 3971 | if (!EmitStatements(stmt->body->statements)) { |
| 3972 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3973 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3974 | if (!emit_continuing_()) { |
| 3975 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3976 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3977 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3978 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3979 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3980 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3983 | bool ASTPrinter::EmitForLoop(const ast::ForLoopStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3984 | // Nest a for loop with a new block. In HLSL the initializer scope is not |
| 3985 | // nested by the for-loop, so we may get variable redefinitions. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3986 | Line() << "{"; |
| 3987 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3988 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3989 | DecrementIndent(); |
| 3990 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3991 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3992 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3993 | TextBuffer init_buf; |
| 3994 | if (auto* init = stmt->initializer) { |
| 3995 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); |
| 3996 | if (!EmitStatement(init)) { |
| 3997 | return false; |
| 3998 | } |
| 3999 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4000 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4001 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4002 | StringStream cond_buf; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4003 | if (auto* cond = stmt->condition) { |
| 4004 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 4005 | if (!EmitExpression(cond_buf, cond)) { |
| 4006 | return false; |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | TextBuffer cont_buf; |
| 4011 | if (auto* cont = stmt->continuing) { |
| 4012 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cont_buf); |
| 4013 | if (!EmitStatement(cont)) { |
| 4014 | return false; |
| 4015 | } |
| 4016 | } |
| 4017 | |
| 4018 | // If the for-loop has a multi-statement conditional and / or continuing, then |
| 4019 | // we cannot emit this as a regular for-loop in HLSL. Instead we need to |
| 4020 | // generate a `while(true)` loop. |
| 4021 | bool emit_as_loop = cond_pre.lines.size() > 0 || cont_buf.lines.size() > 1; |
| 4022 | |
| 4023 | // If the for-loop has multi-statement initializer, or is going to be emitted |
| 4024 | // as a `while(true)` loop, then declare the initializer statement(s) before |
| 4025 | // the loop. |
| 4026 | if (init_buf.lines.size() > 1 || (stmt->initializer && emit_as_loop)) { |
| 4027 | current_buffer_->Append(init_buf); |
| 4028 | init_buf.lines.clear(); // Don't emit the initializer again in the 'for' |
| 4029 | } |
| 4030 | |
| 4031 | if (emit_as_loop) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 4032 | auto emit_continuing = [&] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4033 | current_buffer_->Append(cont_buf); |
| 4034 | return true; |
| 4035 | }; |
| 4036 | |
| 4037 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4038 | Line() << "while (true) {"; |
| 4039 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4040 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4041 | DecrementIndent(); |
| 4042 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4043 | }); |
| 4044 | |
| 4045 | if (stmt->condition) { |
| 4046 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4047 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | if (!EmitStatements(stmt->body->statements)) { |
| 4051 | return false; |
| 4052 | } |
| 4053 | |
| 4054 | if (!emit_continuing_()) { |
| 4055 | return false; |
| 4056 | } |
| 4057 | } else { |
| 4058 | // For-loop can be generated. |
| 4059 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4060 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 4061 | out << "for"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4062 | { |
| 4063 | ScopedParen sp(out); |
| 4064 | |
| 4065 | if (!init_buf.lines.empty()) { |
| 4066 | out << init_buf.lines[0].content << " "; |
| 4067 | } else { |
| 4068 | out << "; "; |
| 4069 | } |
| 4070 | |
| 4071 | out << cond_buf.str() << "; "; |
| 4072 | |
| 4073 | if (!cont_buf.lines.empty()) { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4074 | out << tint::TrimSuffix(cont_buf.lines[0].content, ";"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4075 | } |
| 4076 | } |
| 4077 | out << " {"; |
| 4078 | } |
| 4079 | { |
| 4080 | auto emit_continuing = [] { return true; }; |
| 4081 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 4082 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 4083 | return false; |
| 4084 | } |
| 4085 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4086 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | return true; |
| 4090 | } |
| 4091 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4092 | bool ASTPrinter::EmitWhile(const ast::WhileStatement* stmt) { |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4093 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4094 | StringStream cond_buf; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4095 | { |
| 4096 | auto* cond = stmt->condition; |
| 4097 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 4098 | if (!EmitExpression(cond_buf, cond)) { |
| 4099 | return false; |
| 4100 | } |
| 4101 | } |
| 4102 | |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 4103 | auto emit_continuing = [&] { return true; }; |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 4104 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 4105 | |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4106 | // If the while has a multi-statement conditional, then we cannot emit this |
| 4107 | // as a regular while in HLSL. Instead we need to generate a `while(true)` loop. |
| 4108 | bool emit_as_loop = cond_pre.lines.size() > 0; |
| 4109 | if (emit_as_loop) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4110 | Line() << "while (true) {"; |
| 4111 | IncrementIndent(); |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4112 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4113 | DecrementIndent(); |
| 4114 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4115 | }); |
| 4116 | |
| 4117 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4118 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4119 | if (!EmitStatements(stmt->body->statements)) { |
| 4120 | return false; |
| 4121 | } |
| 4122 | } else { |
| 4123 | // While can be generated. |
| 4124 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4125 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 4126 | out << "while"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4127 | { |
| 4128 | ScopedParen sp(out); |
| 4129 | out << cond_buf.str(); |
| 4130 | } |
| 4131 | out << " {"; |
| 4132 | } |
| 4133 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 4134 | return false; |
| 4135 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4136 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | return true; |
| 4140 | } |
| 4141 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4142 | bool ASTPrinter::EmitMemberAccessor(StringStream& out, const ast::MemberAccessorExpression* expr) { |
Ben Clayton | ad31565 | 2023-02-05 12:36:50 +0000 | [diff] [blame] | 4143 | if (!EmitExpression(out, expr->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4144 | return false; |
| 4145 | } |
| 4146 | out << "."; |
| 4147 | |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 4148 | auto* sem = builder_.Sem().Get(expr)->UnwrapLoad(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4149 | |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4150 | return Switch( |
| 4151 | sem, |
| 4152 | [&](const sem::Swizzle*) { |
| 4153 | // Swizzles output the name directly |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4154 | out << expr->member->symbol.Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4155 | return true; |
| 4156 | }, |
| 4157 | [&](const sem::StructMemberAccess* member_access) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4158 | out << member_access->Member()->Name().Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 4159 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4160 | }, // |
| 4161 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4164 | bool ASTPrinter::EmitReturn(const ast::ReturnStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4165 | if (stmt->value) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4166 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4167 | out << "return "; |
| 4168 | if (!EmitExpression(out, stmt->value)) { |
| 4169 | return false; |
| 4170 | } |
| 4171 | out << ";"; |
| 4172 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4173 | Line() << "return;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4174 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4175 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4178 | bool ASTPrinter::EmitStatement(const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4179 | return Switch( |
| 4180 | stmt, |
| 4181 | [&](const ast::AssignmentStatement* a) { // |
| 4182 | return EmitAssign(a); |
| 4183 | }, |
| 4184 | [&](const ast::BlockStatement* b) { // |
| 4185 | return EmitBlock(b); |
| 4186 | }, |
| 4187 | [&](const ast::BreakStatement* b) { // |
| 4188 | return EmitBreak(b); |
| 4189 | }, |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 4190 | [&](const ast::BreakIfStatement* b) { // |
| 4191 | return EmitBreakIf(b); |
| 4192 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4193 | [&](const ast::CallStatement* c) { // |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4194 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4195 | if (!EmitCall(out, c->expr)) { |
| 4196 | return false; |
| 4197 | } |
| 4198 | out << ";"; |
| 4199 | return true; |
| 4200 | }, |
| 4201 | [&](const ast::ContinueStatement* c) { // |
| 4202 | return EmitContinue(c); |
| 4203 | }, |
| 4204 | [&](const ast::DiscardStatement* d) { // |
| 4205 | return EmitDiscard(d); |
| 4206 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4207 | [&](const ast::IfStatement* i) { // |
| 4208 | return EmitIf(i); |
| 4209 | }, |
| 4210 | [&](const ast::LoopStatement* l) { // |
| 4211 | return EmitLoop(l); |
| 4212 | }, |
| 4213 | [&](const ast::ForLoopStatement* l) { // |
| 4214 | return EmitForLoop(l); |
| 4215 | }, |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4216 | [&](const ast::WhileStatement* l) { // |
| 4217 | return EmitWhile(l); |
| 4218 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4219 | [&](const ast::ReturnStatement* r) { // |
| 4220 | return EmitReturn(r); |
| 4221 | }, |
| 4222 | [&](const ast::SwitchStatement* s) { // |
| 4223 | return EmitSwitch(s); |
| 4224 | }, |
| 4225 | [&](const ast::VariableDeclStatement* v) { // |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4226 | return Switch( |
| 4227 | v->variable, // |
| 4228 | [&](const ast::Var* var) { return EmitVar(var); }, |
| 4229 | [&](const ast::Let* let) { return EmitLet(let); }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 4230 | [&](const ast::Const*) { |
| 4231 | return true; // Constants are embedded at their use |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4232 | }, // |
| 4233 | TINT_ICE_ON_NO_MATCH); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4234 | }, |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 4235 | [&](const ast::ConstAssert*) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 4236 | return true; // Not emitted |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4237 | }, // |
| 4238 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4241 | bool ASTPrinter::EmitDefaultOnlySwitch(const ast::SwitchStatement* stmt) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4242 | TINT_ASSERT(stmt->body.Length() == 1 && stmt->body[0]->ContainsDefault()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4243 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4244 | // FXC fails to compile a switch with just a default case, ignoring the |
| 4245 | // default case body. We work around this here by emitting the default case |
| 4246 | // without the switch. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4247 | |
Antonio Maiorano | eab1f62 | 2023-02-01 15:46:34 +0000 | [diff] [blame] | 4248 | // Emit the switch condition as-is if it has side-effects (e.g. |
| 4249 | // 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] | 4250 | if (auto* sem_cond = builder_.Sem().GetVal(stmt->condition); sem_cond->HasSideEffects()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4251 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4252 | if (!EmitExpression(out, stmt->condition)) { |
| 4253 | return false; |
| 4254 | } |
| 4255 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4256 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4257 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4258 | // Emit "do { <default case body> } while(false);". We use a 'do' loop so |
| 4259 | // that break statements work as expected, and make it 'while (false)' in |
| 4260 | // case there isn't a break statement. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4261 | Line() << "do {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4262 | { |
| 4263 | ScopedIndent si(this); |
| 4264 | if (!EmitStatements(stmt->body[0]->body->statements)) { |
| 4265 | return false; |
| 4266 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4267 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4268 | Line() << "} while (false);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4269 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4272 | bool ASTPrinter::EmitSwitch(const ast::SwitchStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4273 | // BUG(crbug.com/tint/1188): work around default-only switches |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 4274 | if (stmt->body.Length() == 1 && stmt->body[0]->selectors.Length() == 1 && |
| 4275 | stmt->body[0]->ContainsDefault()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4276 | return EmitDefaultOnlySwitch(stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4277 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4278 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4279 | { // switch(expr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4280 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4281 | out << "switch("; |
| 4282 | if (!EmitExpression(out, stmt->condition)) { |
| 4283 | return false; |
| 4284 | } |
| 4285 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4286 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4287 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4288 | { |
| 4289 | ScopedIndent si(this); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 4290 | for (size_t i = 0; i < stmt->body.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4291 | if (!EmitCase(stmt, i)) { |
| 4292 | return false; |
| 4293 | } |
| 4294 | } |
| 4295 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4296 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4297 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4298 | |
| 4299 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4302 | bool ASTPrinter::EmitType(StringStream& out, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4303 | const core::type::Type* type, |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4304 | core::AddressSpace address_space, |
| 4305 | core::Access access, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4306 | const std::string& name, |
| 4307 | bool* name_printed /* = nullptr */) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4308 | if (name_printed) { |
| 4309 | *name_printed = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4310 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4311 | switch (address_space) { |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4312 | case core::AddressSpace::kStorage: |
| 4313 | if (access != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4314 | out << "RW"; |
| 4315 | } |
| 4316 | out << "ByteAddressBuffer"; |
| 4317 | return true; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4318 | case core::AddressSpace::kUniform: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4319 | auto array_length = (type->Size() + 15) / 16; |
| 4320 | out << "uint4 " << name << "[" << array_length << "]"; |
| 4321 | if (name_printed) { |
| 4322 | *name_printed = true; |
| 4323 | } |
| 4324 | return true; |
| 4325 | } |
| 4326 | default: |
| 4327 | break; |
| 4328 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4329 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4330 | return Switch( |
| 4331 | type, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4332 | [&](const core::type::Array* ary) { |
| 4333 | const core::type::Type* base_type = ary; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4334 | std::vector<uint32_t> sizes; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4335 | while (auto* arr = base_type->As<core::type::Array>()) { |
| 4336 | if (TINT_UNLIKELY(arr->Count()->Is<core::type::RuntimeArrayCount>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4337 | TINT_ICE() |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4338 | << "runtime arrays may only exist in storage buffers, which should have " |
Ben Clayton | 3a68ab4 | 2022-06-24 08:30:28 +0000 | [diff] [blame] | 4339 | "been transformed into a ByteAddressBuffer"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4340 | return false; |
| 4341 | } |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4342 | const auto count = arr->ConstantCount(); |
| 4343 | if (!count) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 4344 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 4345 | << core::type::Array::kErrExpectedConstantCount; |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4346 | return false; |
| 4347 | } |
| 4348 | |
| 4349 | sizes.push_back(count.value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4350 | base_type = arr->ElemType(); |
| 4351 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4352 | if (!EmitType(out, base_type, address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4353 | return false; |
| 4354 | } |
| 4355 | if (!name.empty()) { |
| 4356 | out << " " << name; |
| 4357 | if (name_printed) { |
| 4358 | *name_printed = true; |
| 4359 | } |
| 4360 | } |
| 4361 | for (uint32_t size : sizes) { |
| 4362 | out << "[" << size << "]"; |
| 4363 | } |
| 4364 | return true; |
| 4365 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4366 | [&](const core::type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4367 | out << "bool"; |
| 4368 | return true; |
| 4369 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4370 | [&](const core::type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4371 | out << "float"; |
| 4372 | return true; |
| 4373 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4374 | [&](const core::type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4375 | out << "float16_t"; |
| 4376 | return true; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 4377 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4378 | [&](const core::type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4379 | out << "int"; |
| 4380 | return true; |
| 4381 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4382 | [&](const core::type::Matrix* mat) { |
| 4383 | if (mat->type()->Is<core::type::F16>()) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4384 | // Use matrix<type, N, M> for f16 matrix |
| 4385 | out << "matrix<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4386 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4387 | return false; |
| 4388 | } |
| 4389 | out << ", " << mat->columns() << ", " << mat->rows() << ">"; |
| 4390 | return true; |
| 4391 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4392 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4393 | return false; |
| 4394 | } |
| 4395 | // Note: HLSL's matrices are declared as <type>NxM, where N is the |
| 4396 | // number of rows and M is the number of columns. Despite HLSL's |
| 4397 | // matrices being column-major by default, the index operator and |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4398 | // initializers actually operate on row-vectors, where as WGSL operates |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4399 | // on column vectors. To simplify everything we use the transpose of the |
| 4400 | // matrices. See: |
| 4401 | // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math#matrix-ordering |
| 4402 | out << mat->columns() << "x" << mat->rows(); |
| 4403 | return true; |
| 4404 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4405 | [&](const core::type::Pointer*) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4406 | TINT_ICE() << "Attempting to emit pointer type. These should have " |
| 4407 | "been removed with the SimplifyPointers transform"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4408 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4409 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4410 | [&](const core::type::Sampler* sampler) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4411 | out << "Sampler"; |
| 4412 | if (sampler->IsComparison()) { |
| 4413 | out << "Comparison"; |
| 4414 | } |
| 4415 | out << "State"; |
| 4416 | return true; |
| 4417 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4418 | [&](const core::type::Struct* str) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4419 | out << StructName(str); |
| 4420 | return true; |
| 4421 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4422 | [&](const core::type::Texture* tex) { |
| 4423 | if (TINT_UNLIKELY(tex->Is<core::type::ExternalTexture>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4424 | TINT_ICE() << "Multiplanar external texture transform was not run."; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4425 | return false; |
| 4426 | } |
Brandon Jones | 6661b28 | 2022-02-25 20:14:52 +0000 | [diff] [blame] | 4427 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4428 | auto* storage = tex->As<core::type::StorageTexture>(); |
| 4429 | auto* ms = tex->As<core::type::MultisampledTexture>(); |
| 4430 | auto* depth_ms = tex->As<core::type::DepthMultisampledTexture>(); |
| 4431 | auto* sampled = tex->As<core::type::SampledTexture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4432 | |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4433 | if (storage && storage->access() != core::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4434 | out << "RW"; |
| 4435 | } |
| 4436 | out << "Texture"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4437 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4438 | switch (tex->dim()) { |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4439 | case core::type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4440 | out << "1D"; |
| 4441 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4442 | case core::type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4443 | out << ((ms || depth_ms) ? "2DMS" : "2D"); |
| 4444 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4445 | case core::type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4446 | out << ((ms || depth_ms) ? "2DMSArray" : "2DArray"); |
| 4447 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4448 | case core::type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4449 | out << "3D"; |
| 4450 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4451 | case core::type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4452 | out << "Cube"; |
| 4453 | break; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4454 | case core::type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4455 | out << "CubeArray"; |
| 4456 | break; |
| 4457 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4458 | TINT_UNREACHABLE() << "unexpected TextureDimension " << tex->dim(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4459 | return false; |
| 4460 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4461 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4462 | if (storage) { |
| 4463 | auto* component = image_format_to_rwtexture_type(storage->texel_format()); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4464 | if (TINT_UNLIKELY(!component)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4465 | TINT_ICE() << "Unsupported StorageTexture TexelFormat: " |
| 4466 | << static_cast<int>(storage->texel_format()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4467 | return false; |
| 4468 | } |
| 4469 | out << "<" << component << ">"; |
| 4470 | } else if (depth_ms) { |
| 4471 | out << "<float4>"; |
| 4472 | } else if (sampled || ms) { |
| 4473 | auto* subtype = sampled ? sampled->type() : ms->type(); |
| 4474 | out << "<"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4475 | if (subtype->Is<core::type::F32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4476 | out << "float4"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4477 | } else if (subtype->Is<core::type::I32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4478 | out << "int4"; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4479 | } else if (TINT_LIKELY(subtype->Is<core::type::U32>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4480 | out << "uint4"; |
| 4481 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4482 | TINT_ICE() << "Unsupported multisampled texture type"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4483 | return false; |
| 4484 | } |
| 4485 | out << ">"; |
| 4486 | } |
| 4487 | return true; |
| 4488 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4489 | [&](const core::type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4490 | out << "uint"; |
| 4491 | return true; |
| 4492 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4493 | [&](const core::type::Vector* vec) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4494 | auto width = vec->Width(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4495 | if (vec->type()->Is<core::type::F32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4496 | out << "float" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4497 | } else if (vec->type()->Is<core::type::I32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4498 | out << "int" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4499 | } else if (vec->type()->Is<core::type::U32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4500 | out << "uint" << width; |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4501 | } else if (vec->type()->Is<core::type::Bool>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4502 | out << "bool" << width; |
| 4503 | } else { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4504 | // For example, use "vector<float16_t, N>" for f16 vector. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4505 | out << "vector<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4506 | if (!EmitType(out, vec->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4507 | return false; |
| 4508 | } |
| 4509 | out << ", " << width << ">"; |
| 4510 | } |
| 4511 | return true; |
| 4512 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4513 | [&](const core::type::Atomic* atomic) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4514 | return EmitType(out, atomic->Type(), address_space, access, name); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4515 | }, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4516 | [&](const core::type::Void*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4517 | out << "void"; |
| 4518 | return true; |
Ben Clayton | d6082c5 | 2023-10-26 16:02:01 +0000 | [diff] [blame] | 4519 | }, // |
| 4520 | TINT_ICE_ON_NO_MATCH); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4523 | bool ASTPrinter::EmitTypeAndName(StringStream& out, |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4524 | const core::type::Type* type, |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4525 | core::AddressSpace address_space, |
| 4526 | core::Access access, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4527 | const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4528 | bool name_printed = false; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4529 | if (!EmitType(out, type, address_space, access, name, &name_printed)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4530 | return false; |
| 4531 | } |
| 4532 | if (!name.empty() && !name_printed) { |
| 4533 | out << " " << name; |
| 4534 | } |
| 4535 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4538 | bool ASTPrinter::EmitStructType(TextBuffer* b, const core::type::Struct* str) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 4539 | auto it = emitted_structs_.emplace(str); |
| 4540 | if (!it.second) { |
| 4541 | return true; |
| 4542 | } |
| 4543 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4544 | Line(b) << "struct " << StructName(str) << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4545 | { |
| 4546 | ScopedIndent si(b); |
| 4547 | for (auto* mem : str->Members()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4548 | auto mem_name = mem->Name().Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4549 | auto* ty = mem->Type(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4550 | auto out = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4551 | std::string pre, post; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4552 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4553 | auto& attributes = mem->Attributes(); |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 4554 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4555 | if (auto location = attributes.location) { |
| 4556 | auto& pipeline_stage_uses = str->PipelineStageUses(); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4557 | if (TINT_UNLIKELY(pipeline_stage_uses.Count() != 1)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4558 | TINT_ICE() << "invalid entry point IO struct uses"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4559 | } |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4560 | if (pipeline_stage_uses.Contains(core::type::PipelineStageUsage::kVertexInput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4561 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4562 | } else if (pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4563 | core::type::PipelineStageUsage::kVertexOutput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4564 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4565 | } else if (pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4566 | core::type::PipelineStageUsage::kFragmentInput)) { |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4567 | post += " : TEXCOORD" + std::to_string(location.value()); |
Ben Clayton | 8be957f | 2024-01-29 16:00:36 +0000 | [diff] [blame] | 4568 | } else if (TINT_LIKELY(pipeline_stage_uses.Contains( |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4569 | core::type::PipelineStageUsage::kFragmentOutput))) { |
Corentin Wallez | 0880e06 | 2024-01-30 16:16:45 +0000 | [diff] [blame] | 4570 | if (auto blend_src = attributes.blend_src) { |
| 4571 | post += |
| 4572 | " : SV_Target" + std::to_string(location.value() + blend_src.value()); |
Brandon Jones | 07500d3 | 2023-07-21 22:07:03 +0000 | [diff] [blame] | 4573 | } else { |
| 4574 | post += " : SV_Target" + std::to_string(location.value()); |
| 4575 | } |
| 4576 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4577 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4578 | TINT_ICE() << "invalid use of location attribute"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4579 | } |
| 4580 | } |
| 4581 | if (auto builtin = attributes.builtin) { |
| 4582 | auto name = builtin_to_attribute(builtin.value()); |
| 4583 | if (name.empty()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 4584 | diagnostics_.AddError(diag::System::Writer, Source{}) << "unsupported builtin"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4585 | return false; |
| 4586 | } |
| 4587 | post += " : " + name; |
| 4588 | } |
| 4589 | if (auto interpolation = attributes.interpolation) { |
| 4590 | auto mod = interpolation_to_modifiers(interpolation->type, interpolation->sampling); |
| 4591 | if (mod.empty()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame^] | 4592 | diagnostics_.AddError(diag::System::Writer, Source{}) |
| 4593 | << "unsupported interpolation"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4594 | return false; |
| 4595 | } |
| 4596 | pre += mod; |
| 4597 | } |
| 4598 | if (attributes.invariant) { |
| 4599 | // Note: `precise` is not exactly the same as `invariant`, but is |
| 4600 | // stricter and therefore provides the necessary guarantees. |
| 4601 | // See discussion here: https://github.com/gpuweb/gpuweb/issues/893 |
| 4602 | pre += "precise "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4603 | } |
| 4604 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4605 | out << pre; |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4606 | if (!EmitTypeAndName(out, ty, core::AddressSpace::kUndefined, core::Access::kReadWrite, |
| 4607 | mem_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4608 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4609 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4610 | out << post << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4611 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4612 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4613 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4614 | Line(b) << "};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4615 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4618 | bool ASTPrinter::EmitUnaryOp(StringStream& out, const ast::UnaryOpExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4619 | switch (expr->op) { |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4620 | case core::UnaryOp::kIndirection: |
| 4621 | case core::UnaryOp::kAddressOf: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4622 | return EmitExpression(out, expr->expr); |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4623 | case core::UnaryOp::kComplement: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4624 | out << "~"; |
| 4625 | break; |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4626 | case core::UnaryOp::kNot: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4627 | out << "!"; |
| 4628 | break; |
Ben Clayton | 67b461b | 2023-08-08 07:58:19 +0000 | [diff] [blame] | 4629 | case core::UnaryOp::kNegation: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4630 | out << "-"; |
| 4631 | break; |
| 4632 | } |
| 4633 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4634 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4635 | if (!EmitExpression(out, expr->expr)) { |
| 4636 | return false; |
| 4637 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4638 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4639 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4640 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4641 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4644 | bool ASTPrinter::EmitVar(const ast::Var* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4645 | auto* sem = builder_.Sem().Get(var); |
| 4646 | auto* type = sem->Type()->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4647 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4648 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4649 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4650 | return false; |
| 4651 | } |
| 4652 | |
| 4653 | out << " = "; |
| 4654 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4655 | if (var->initializer) { |
| 4656 | if (!EmitExpression(out, var->initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
| 4659 | } else { |
| 4660 | if (!EmitZeroValue(out, type)) { |
| 4661 | return false; |
| 4662 | } |
| 4663 | } |
| 4664 | out << ";"; |
| 4665 | |
| 4666 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4669 | bool ASTPrinter::EmitLet(const ast::Let* let) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4670 | auto* sem = builder_.Sem().Get(let); |
| 4671 | auto* type = sem->Type()->UnwrapRef(); |
| 4672 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4673 | auto out = Line(); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4674 | if (!EmitTypeAndName(out, type, core::AddressSpace::kUndefined, core::Access::kUndefined, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4675 | let->name->symbol.Name())) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4676 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4677 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4678 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4679 | if (!EmitExpression(out, let->initializer)) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4680 | return false; |
| 4681 | } |
| 4682 | out << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4683 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4684 | return true; |
| 4685 | } |
| 4686 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4687 | template <typename F> |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4688 | bool ASTPrinter::CallBuiltinHelper(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4689 | const ast::CallExpression* call, |
Ben Clayton | d9766dc | 2023-09-21 12:41:20 +0000 | [diff] [blame] | 4690 | const sem::BuiltinFn* builtin, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4691 | F&& build) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4692 | // Generate the helper function if it hasn't been created already |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 4693 | auto fn = tint::GetOrAdd(builtins_, builtin, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4694 | TextBuffer b; |
| 4695 | TINT_DEFER(helpers_.Append(b)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4696 | |
Ben Clayton | dfc815c | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 4697 | auto fn_name = UniqueIdentifier(std::string("tint_") + wgsl::str(builtin->Fn())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4698 | std::vector<std::string> parameter_names; |
| 4699 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4700 | auto decl = Line(&b); |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4701 | if (!EmitTypeAndName(decl, builtin->ReturnType(), core::AddressSpace::kUndefined, |
| 4702 | core::Access::kUndefined, fn_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4703 | return ""; |
| 4704 | } |
| 4705 | { |
| 4706 | ScopedParen sp(decl); |
| 4707 | for (auto* param : builtin->Parameters()) { |
| 4708 | if (!parameter_names.empty()) { |
| 4709 | decl << ", "; |
| 4710 | } |
| 4711 | auto param_name = "param_" + std::to_string(parameter_names.size()); |
| 4712 | const auto* ty = param->Type(); |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4713 | if (auto* ptr = ty->As<core::type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4714 | decl << "inout "; |
| 4715 | ty = ptr->StoreType(); |
| 4716 | } |
Ben Clayton | cd52f38 | 2023-08-07 13:11:08 +0000 | [diff] [blame] | 4717 | if (!EmitTypeAndName(decl, ty, core::AddressSpace::kUndefined, |
| 4718 | core::Access::kUndefined, param_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4719 | return ""; |
| 4720 | } |
| 4721 | parameter_names.emplace_back(std::move(param_name)); |
| 4722 | } |
| 4723 | } |
| 4724 | decl << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4725 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4726 | { |
| 4727 | ScopedIndent si(&b); |
| 4728 | if (!build(&b, parameter_names)) { |
| 4729 | return ""; |
| 4730 | } |
| 4731 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4732 | Line(&b) << "}"; |
| 4733 | Line(&b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4734 | return fn_name; |
| 4735 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4736 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4737 | if (fn.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4738 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4739 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4740 | |
| 4741 | // Call the helper |
| 4742 | out << fn; |
| 4743 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 4744 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4745 | bool first = true; |
| 4746 | for (auto* arg : call->args) { |
| 4747 | if (!first) { |
| 4748 | out << ", "; |
| 4749 | } |
| 4750 | first = false; |
| 4751 | if (!EmitExpression(out, arg)) { |
| 4752 | return false; |
| 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 4759 | std::string ASTPrinter::StructName(const core::type::Struct* s) { |
Ben Clayton | 34c8e57 | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 4760 | auto name = s->Name().Name(); |
| 4761 | if (HasPrefix(name, "__")) { |
Ben Clayton | 7a27d6c | 2024-01-31 19:44:28 +0000 | [diff] [blame] | 4762 | name = tint::GetOrAdd(builtin_struct_names_, s, |
| 4763 | [&] { return UniqueIdentifier(name.substr(2)); }); |
Ben Clayton | 34c8e57 | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 4764 | } |
| 4765 | return name; |
| 4766 | } |
| 4767 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4768 | std::string ASTPrinter::UniqueIdentifier(const std::string& prefix /* = "" */) { |
Ben Clayton | 5ed099a | 2023-07-25 18:52:03 +0000 | [diff] [blame] | 4769 | return builder_.Symbols().New(prefix).Name(); |
| 4770 | } |
| 4771 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4772 | } // namespace tint::hlsl::writer |