Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | /// Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 15 | #include "src/tint/lang/hlsl/writer/ast_printer/ast_printer.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 16 | |
| 17 | #include <algorithm> |
| 18 | #include <cmath> |
| 19 | #include <functional> |
| 20 | #include <iomanip> |
| 21 | #include <set> |
| 22 | #include <utility> |
| 23 | #include <vector> |
| 24 | |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 25 | #include "src/tint/lang/core/constant/splat.h" |
| 26 | #include "src/tint/lang/core/constant/value.h" |
| 27 | #include "src/tint/lang/core/type/array.h" |
| 28 | #include "src/tint/lang/core/type/atomic.h" |
| 29 | #include "src/tint/lang/core/type/depth_multisampled_texture.h" |
| 30 | #include "src/tint/lang/core/type/depth_texture.h" |
| 31 | #include "src/tint/lang/core/type/multisampled_texture.h" |
| 32 | #include "src/tint/lang/core/type/sampled_texture.h" |
| 33 | #include "src/tint/lang/core/type/storage_texture.h" |
| 34 | #include "src/tint/lang/core/type/texture_dimension.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 35 | #include "src/tint/lang/wgsl/ast/call_statement.h" |
| 36 | #include "src/tint/lang/wgsl/ast/id_attribute.h" |
| 37 | #include "src/tint/lang/wgsl/ast/internal_attribute.h" |
| 38 | #include "src/tint/lang/wgsl/ast/interpolate_attribute.h" |
| 39 | #include "src/tint/lang/wgsl/ast/transform/add_empty_entry_point.h" |
| 40 | #include "src/tint/lang/wgsl/ast/transform/array_length_from_uniform.h" |
| 41 | #include "src/tint/lang/wgsl/ast/transform/binding_remapper.h" |
| 42 | #include "src/tint/lang/wgsl/ast/transform/builtin_polyfill.h" |
| 43 | #include "src/tint/lang/wgsl/ast/transform/calculate_array_length.h" |
| 44 | #include "src/tint/lang/wgsl/ast/transform/canonicalize_entry_point_io.h" |
| 45 | #include "src/tint/lang/wgsl/ast/transform/decompose_memory_access.h" |
| 46 | #include "src/tint/lang/wgsl/ast/transform/demote_to_helper.h" |
| 47 | #include "src/tint/lang/wgsl/ast/transform/direct_variable_access.h" |
| 48 | #include "src/tint/lang/wgsl/ast/transform/disable_uniformity_analysis.h" |
| 49 | #include "src/tint/lang/wgsl/ast/transform/expand_compound_assignment.h" |
| 50 | #include "src/tint/lang/wgsl/ast/transform/localize_struct_array_assignment.h" |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 51 | #include "src/tint/lang/wgsl/ast/transform/manager.h" |
dan sinclair | 99181d8 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 52 | #include "src/tint/lang/wgsl/ast/transform/multiplanar_external_texture.h" |
| 53 | #include "src/tint/lang/wgsl/ast/transform/num_workgroups_from_uniform.h" |
| 54 | #include "src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.h" |
| 55 | #include "src/tint/lang/wgsl/ast/transform/promote_side_effects_to_decl.h" |
| 56 | #include "src/tint/lang/wgsl/ast/transform/remove_continue_in_switch.h" |
| 57 | #include "src/tint/lang/wgsl/ast/transform/remove_phonies.h" |
| 58 | #include "src/tint/lang/wgsl/ast/transform/robustness.h" |
| 59 | #include "src/tint/lang/wgsl/ast/transform/simplify_pointers.h" |
| 60 | #include "src/tint/lang/wgsl/ast/transform/truncate_interstage_variables.h" |
| 61 | #include "src/tint/lang/wgsl/ast/transform/unshadow.h" |
| 62 | #include "src/tint/lang/wgsl/ast/transform/vectorize_scalar_matrix_initializers.h" |
| 63 | #include "src/tint/lang/wgsl/ast/transform/zero_init_workgroup_memory.h" |
| 64 | #include "src/tint/lang/wgsl/ast/variable_decl_statement.h" |
dan sinclair | a4c1735 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 65 | #include "src/tint/lang/wgsl/helpers/append_vector.h" |
| 66 | #include "src/tint/lang/wgsl/helpers/check_supported_extensions.h" |
dan sinclair | d3b1369 | 2023-07-20 01:14:15 +0000 | [diff] [blame] | 67 | #include "src/tint/lang/wgsl/sem/block_statement.h" |
| 68 | #include "src/tint/lang/wgsl/sem/call.h" |
| 69 | #include "src/tint/lang/wgsl/sem/function.h" |
| 70 | #include "src/tint/lang/wgsl/sem/member_accessor_expression.h" |
| 71 | #include "src/tint/lang/wgsl/sem/module.h" |
| 72 | #include "src/tint/lang/wgsl/sem/statement.h" |
| 73 | #include "src/tint/lang/wgsl/sem/struct.h" |
| 74 | #include "src/tint/lang/wgsl/sem/switch_statement.h" |
| 75 | #include "src/tint/lang/wgsl/sem/value_constructor.h" |
| 76 | #include "src/tint/lang/wgsl/sem/value_conversion.h" |
| 77 | #include "src/tint/lang/wgsl/sem/variable.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 78 | #include "src/tint/utils/containers/map.h" |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 79 | #include "src/tint/utils/ice/ice.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 80 | #include "src/tint/utils/macros/compiler.h" |
| 81 | #include "src/tint/utils/macros/defer.h" |
| 82 | #include "src/tint/utils/macros/scoped_assignment.h" |
| 83 | #include "src/tint/utils/rtti/switch.h" |
Ben Clayton | 16be11d | 2023-08-01 00:37:35 +0000 | [diff] [blame^] | 84 | #include "src/tint/utils/strconv/float_to_string.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 85 | #include "src/tint/utils/text/string.h" |
| 86 | #include "src/tint/utils/text/string_stream.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 88 | using namespace tint::number_suffixes; // NOLINT |
| 89 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 90 | namespace tint::hlsl::writer { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 91 | namespace { |
| 92 | |
| 93 | const char kTempNamePrefix[] = "tint_tmp"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 94 | |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 95 | const char* image_format_to_rwtexture_type(builtin::TexelFormat image_format) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | switch (image_format) { |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 97 | case builtin::TexelFormat::kBgra8Unorm: |
| 98 | case builtin::TexelFormat::kRgba8Unorm: |
| 99 | case builtin::TexelFormat::kRgba8Snorm: |
| 100 | case builtin::TexelFormat::kRgba16Float: |
| 101 | case builtin::TexelFormat::kR32Float: |
| 102 | case builtin::TexelFormat::kRg32Float: |
| 103 | case builtin::TexelFormat::kRgba32Float: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 104 | return "float4"; |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 105 | case builtin::TexelFormat::kRgba8Uint: |
| 106 | case builtin::TexelFormat::kRgba16Uint: |
| 107 | case builtin::TexelFormat::kR32Uint: |
| 108 | case builtin::TexelFormat::kRg32Uint: |
| 109 | case builtin::TexelFormat::kRgba32Uint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 110 | return "uint4"; |
dan sinclair | ba082fd | 2023-02-19 04:14:19 +0000 | [diff] [blame] | 111 | case builtin::TexelFormat::kRgba8Sint: |
| 112 | case builtin::TexelFormat::kRgba16Sint: |
| 113 | case builtin::TexelFormat::kR32Sint: |
| 114 | case builtin::TexelFormat::kRg32Sint: |
| 115 | case builtin::TexelFormat::kRgba32Sint: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 116 | return "int4"; |
| 117 | default: |
| 118 | return nullptr; |
| 119 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 120 | } |
| 121 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 122 | void PrintF32(StringStream& out, float value) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 123 | if (std::isinf(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 124 | out << "0.0f " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 125 | } else if (std::isnan(value)) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 126 | out << "0.0f /* nan */"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 127 | } else { |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 128 | out << tint::writer::FloatToString(value) << "f"; |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 132 | void PrintF16(StringStream& out, float value) { |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 133 | if (std::isinf(value)) { |
| 134 | out << "0.0h " << (value >= 0 ? "/* inf */" : "/* -inf */"); |
| 135 | } else if (std::isnan(value)) { |
| 136 | out << "0.0h /* nan */"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 137 | } else { |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 138 | out << tint::writer::FloatToString(value) << "h"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 142 | // Helper for writing " : register(RX, spaceY)", where R is the register, X is |
| 143 | // the binding point binding value, and Y is the binding point group value. |
| 144 | struct RegisterAndSpace { |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 145 | RegisterAndSpace(char r, BindingPoint bp) : reg(r), binding_point(bp) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 146 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 147 | const char reg; |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 148 | BindingPoint const binding_point; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 151 | StringStream& operator<<(StringStream& s, const RegisterAndSpace& rs) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 152 | s << " : register(" << rs.reg << rs.binding_point.binding; |
| 153 | // Omit the space if it's 0, as it's the default. |
| 154 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better compatibility. |
| 155 | if (rs.binding_point.group == 0) { |
| 156 | s << ")"; |
| 157 | } else { |
| 158 | s << ", space" << rs.binding_point.group << ")"; |
| 159 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 160 | return s; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 163 | } // namespace |
| 164 | |
| 165 | SanitizedResult::SanitizedResult() = default; |
| 166 | SanitizedResult::~SanitizedResult() = default; |
| 167 | SanitizedResult::SanitizedResult(SanitizedResult&&) = default; |
| 168 | |
Antonio Maiorano | 7eaab38 | 2022-04-11 16:33:30 +0000 | [diff] [blame] | 169 | SanitizedResult Sanitize(const Program* in, const Options& options) { |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 170 | ast::transform::Manager manager; |
| 171 | ast::transform::DataMap data; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 172 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 173 | manager.Add<ast::transform::DisableUniformityAnalysis>(); |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 174 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 175 | // ExpandCompoundAssignment must come before BuiltinPolyfill |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 176 | manager.Add<ast::transform::ExpandCompoundAssignment>(); |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 177 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 178 | manager.Add<ast::transform::Unshadow>(); // Must come before DirectVariableAccess |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 179 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 180 | // LocalizeStructArrayAssignment must come after: |
| 181 | // * SimplifyPointers, because it assumes assignment to arrays in structs are |
| 182 | // done directly, not indirectly. |
| 183 | // TODO(crbug.com/tint/1340): See if we can get rid of the duplicate |
| 184 | // SimplifyPointers transform. Can't do it right now because |
| 185 | // LocalizeStructArrayAssignment introduces pointers. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 186 | manager.Add<ast::transform::SimplifyPointers>(); |
| 187 | manager.Add<ast::transform::LocalizeStructArrayAssignment>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 188 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 189 | manager.Add<ast::transform::PromoteSideEffectsToDecl>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 190 | |
| 191 | if (!options.disable_robustness) { |
Ben Clayton | 8525ff2 | 2023-03-06 21:05:01 +0000 | [diff] [blame] | 192 | // Robustness must come after PromoteSideEffectsToDecl |
| 193 | // Robustness must come before BuiltinPolyfill and CanonicalizeEntryPointIO |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 194 | manager.Add<ast::transform::Robustness>(); |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 195 | |
| 196 | ast::transform::Robustness::Config config = {}; |
Jiawei Shao | 2e11963 | 2023-06-21 01:43:25 +0000 | [diff] [blame] | 197 | |
Ben Clayton | 1025258 | 2023-07-25 20:53:25 +0000 | [diff] [blame] | 198 | config.bindings_ignored = std::unordered_set<BindingPoint>( |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 199 | options.binding_points_ignored_in_robustness_transform.cbegin(), |
| 200 | options.binding_points_ignored_in_robustness_transform.cend()); |
Jiawei Shao | 2e11963 | 2023-06-21 01:43:25 +0000 | [diff] [blame] | 201 | |
| 202 | // Direct3D guarantees to return zero for any resource that is accessed out of bounds, and |
| 203 | // according to the description of the assembly store_uav_typed, out of bounds addressing |
| 204 | // means nothing gets written to memory. |
| 205 | config.texture_action = ast::transform::Robustness::Action::kIgnore; |
| 206 | |
Jiawei Shao | 455e4b8 | 2023-06-10 00:32:22 +0000 | [diff] [blame] | 207 | data.Add<ast::transform::Robustness::Config>(config); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 208 | } |
| 209 | |
dan sinclair | 04d61c8 | 2023-04-11 23:02:45 +0000 | [diff] [blame] | 210 | // Note: it is more efficient for MultiplanarExternalTexture to come after Robustness |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 211 | data.Add<ast::transform::MultiplanarExternalTexture::NewBindingPoints>( |
dan sinclair | 04d61c8 | 2023-04-11 23:02:45 +0000 | [diff] [blame] | 212 | options.external_texture_options.bindings_map); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 213 | manager.Add<ast::transform::MultiplanarExternalTexture>(); |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 214 | |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 215 | // BindingRemapper must come after MultiplanarExternalTexture |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 216 | manager.Add<ast::transform::BindingRemapper>(); |
| 217 | data.Add<ast::transform::BindingRemapper::Remappings>( |
dan sinclair | 39d4065 | 2023-03-15 13:41:46 +0000 | [diff] [blame] | 218 | options.binding_remapper_options.binding_points, |
| 219 | options.binding_remapper_options.access_controls, |
| 220 | options.binding_remapper_options.allow_collisions); |
| 221 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 222 | { // Builtin polyfills |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 223 | ast::transform::BuiltinPolyfill::Builtins polyfills; |
| 224 | polyfills.acosh = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | d23f296 | 2022-06-28 15:27:44 +0000 | [diff] [blame] | 225 | polyfills.asinh = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 226 | polyfills.atanh = ast::transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 02f04d9 | 2022-11-03 19:15:17 +0000 | [diff] [blame] | 227 | polyfills.bitshift_modulo = true; |
Ben Clayton | 6dbb463 | 2022-10-31 17:54:49 +0000 | [diff] [blame] | 228 | polyfills.clamp_int = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 229 | // TODO(crbug.com/tint/1449): Some of these can map to HLSL's `firstbitlow` |
| 230 | // and `firstbithigh`. |
Ben Clayton | cc3f851 | 2023-03-09 21:26:12 +0000 | [diff] [blame] | 231 | polyfills.conv_f32_to_iu32 = true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 232 | polyfills.count_leading_zeros = true; |
| 233 | polyfills.count_trailing_zeros = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 234 | polyfills.extract_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 235 | polyfills.first_leading_bit = true; |
| 236 | polyfills.first_trailing_bit = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 237 | polyfills.insert_bits = ast::transform::BuiltinPolyfill::Level::kFull; |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 238 | polyfills.int_div_mod = true; |
Antonio Maiorano | ee665a4 | 2023-02-14 16:12:59 +0000 | [diff] [blame] | 239 | polyfills.precise_float_mod = true; |
Zhaoming Jiang | 04529be | 2023-02-27 02:59:50 +0000 | [diff] [blame] | 240 | polyfills.reflect_vec2_f32 = options.polyfill_reflect_vec2_f32; |
Ben Clayton | c4ebf2c | 2022-09-22 22:59:16 +0000 | [diff] [blame] | 241 | polyfills.texture_sample_base_clamp_to_edge_2d_f32 = true; |
James Price | 128980f | 2023-01-06 02:25:06 +0000 | [diff] [blame] | 242 | polyfills.workgroup_uniform_load = true; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 243 | data.Add<ast::transform::BuiltinPolyfill::Config>(polyfills); |
| 244 | manager.Add<ast::transform::BuiltinPolyfill>(); // Must come before DirectVariableAccess |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 245 | } |
Ben Clayton | 27aa57c | 2022-02-22 23:13:39 +0000 | [diff] [blame] | 246 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 247 | manager.Add<ast::transform::DirectVariableAccess>(); |
Antonio Maiorano | fa00fe9 | 2023-05-03 15:30:54 +0000 | [diff] [blame] | 248 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 249 | if (!options.disable_workgroup_init) { |
| 250 | // ZeroInitWorkgroupMemory must come before CanonicalizeEntryPointIO as |
| 251 | // ZeroInitWorkgroupMemory may inject new builtin parameters. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 252 | manager.Add<ast::transform::ZeroInitWorkgroupMemory>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 253 | } |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 254 | |
| 255 | // CanonicalizeEntryPointIO must come after Robustness |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 256 | manager.Add<ast::transform::CanonicalizeEntryPointIO>(); |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 257 | |
shrekshao | 8f0607a | 2023-04-18 01:34:41 +0000 | [diff] [blame] | 258 | if (options.truncate_interstage_variables) { |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 259 | // 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] | 260 | // being used in the next stage. Still, HLSL compiler register mismatch could happen, if |
| 261 | // there's builtin inputs used in the next stage. So we still run |
| 262 | // TruncateInterstageVariables transform. |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 263 | |
| 264 | // TruncateInterstageVariables itself will skip when interstage_locations matches exactly |
| 265 | // with the current stage output. |
| 266 | |
| 267 | // Build the config for internal TruncateInterstageVariables transform. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 268 | ast::transform::TruncateInterstageVariables::Config truncate_interstage_variables_cfg; |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 269 | truncate_interstage_variables_cfg.interstage_locations = |
| 270 | std::move(options.interstage_locations); |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 271 | manager.Add<ast::transform::TruncateInterstageVariables>(); |
| 272 | data.Add<ast::transform::TruncateInterstageVariables::Config>( |
shrekshao | f9c6633 | 2022-11-22 21:36:27 +0000 | [diff] [blame] | 273 | std::move(truncate_interstage_variables_cfg)); |
| 274 | } |
| 275 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 276 | // NumWorkgroupsFromUniform must come after CanonicalizeEntryPointIO, as it |
| 277 | // assumes that num_workgroups builtins only appear as struct members and are |
| 278 | // only accessed directly via member accessors. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 279 | manager.Add<ast::transform::NumWorkgroupsFromUniform>(); |
| 280 | manager.Add<ast::transform::VectorizeScalarMatrixInitializers>(); |
| 281 | manager.Add<ast::transform::SimplifyPointers>(); |
| 282 | manager.Add<ast::transform::RemovePhonies>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 283 | |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 284 | // Build the config for the internal ArrayLengthFromUniform transform. |
| 285 | auto& array_length_from_uniform = options.array_length_from_uniform; |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 286 | ast::transform::ArrayLengthFromUniform::Config array_length_from_uniform_cfg( |
Ben Clayton | 03de0e8 | 2023-03-02 20:48:48 +0000 | [diff] [blame] | 287 | array_length_from_uniform.ubo_binding); |
| 288 | array_length_from_uniform_cfg.bindpoint_to_size_index = |
| 289 | array_length_from_uniform.bindpoint_to_size_index; |
| 290 | |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 291 | // DemoteToHelper must come after CanonicalizeEntryPointIO, PromoteSideEffectsToDecl, and |
| 292 | // ExpandCompoundAssignment. |
| 293 | // 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] | 294 | manager.Add<ast::transform::DemoteToHelper>(); |
James Price | 744d0eb | 2022-11-09 19:58:59 +0000 | [diff] [blame] | 295 | |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 296 | // ArrayLengthFromUniform must come after SimplifyPointers as it assumes that the form of the |
| 297 | // array length argument is &var.array. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 298 | manager.Add<ast::transform::ArrayLengthFromUniform>(); |
| 299 | data.Add<ast::transform::ArrayLengthFromUniform::Config>( |
| 300 | std::move(array_length_from_uniform_cfg)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 301 | // DecomposeMemoryAccess must come after: |
Ben Clayton | 559e5a2 | 2023-04-17 14:24:58 +0000 | [diff] [blame] | 302 | // * SimplifyPointers, as we cannot take the address of calls to |
| 303 | // DecomposeMemoryAccess::Intrinsic and we need to fold away the address-of and dereferences |
| 304 | // of `*(&(intrinsic_load()))` expressions. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 305 | // * RemovePhonies, as phonies can be assigned a pointer to a |
| 306 | // non-constructible buffer, or dynamic array, which DMA cannot cope with. |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 307 | manager.Add<ast::transform::DecomposeMemoryAccess>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 308 | // CalculateArrayLength must come after DecomposeMemoryAccess, as |
| 309 | // DecomposeMemoryAccess special-cases the arrayLength() intrinsic, which |
| 310 | // will be transformed by CalculateArrayLength |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 311 | manager.Add<ast::transform::CalculateArrayLength>(); |
| 312 | manager.Add<ast::transform::PromoteInitializersToLet>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 313 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 314 | manager.Add<ast::transform::RemoveContinueInSwitch>(); |
Antonio Maiorano | b349710 | 2022-03-31 15:02:25 +0000 | [diff] [blame] | 315 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 316 | manager.Add<ast::transform::AddEmptyEntryPoint>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 317 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 318 | data.Add<ast::transform::CanonicalizeEntryPointIO::Config>( |
| 319 | ast::transform::CanonicalizeEntryPointIO::ShaderStyle::kHlsl); |
| 320 | data.Add<ast::transform::NumWorkgroupsFromUniform::Config>(options.root_constant_binding_point); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 321 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 322 | SanitizedResult result; |
Ben Clayton | 7494e83 | 2023-07-25 15:30:31 +0000 | [diff] [blame] | 323 | ast::transform::DataMap outputs; |
James Price | 0e6534e | 2023-05-17 01:21:45 +0000 | [diff] [blame] | 324 | result.program = manager.Run(in, data, outputs); |
| 325 | if (auto* res = outputs.Get<ast::transform::ArrayLengthFromUniform::Result>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 326 | result.used_array_length_from_uniform_indices = std::move(res->used_size_indices); |
| 327 | } |
| 328 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 329 | } |
| 330 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 331 | ASTPrinter::ASTPrinter(const Program* program) : builder_(ProgramBuilder::Wrap(program)) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 332 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 333 | ASTPrinter::~ASTPrinter() = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 334 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 335 | bool ASTPrinter::Generate() { |
| 336 | if (!tint::writer::CheckSupportedExtensions( |
| 337 | "HLSL", builder_.AST(), diagnostics_, |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 338 | Vector{ |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 339 | builtin::Extension::kChromiumDisableUniformityAnalysis, |
| 340 | builtin::Extension::kChromiumExperimentalDp4A, |
| 341 | builtin::Extension::kChromiumExperimentalFullPtrParameters, |
| 342 | builtin::Extension::kChromiumExperimentalPushConstant, |
| 343 | builtin::Extension::kF16, |
| 344 | builtin::Extension::kChromiumInternalDualSourceBlending, |
| 345 | })) { |
Ben Clayton | 1a56778 | 2022-10-14 13:38:27 +0000 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 349 | const tint::TypeInfo* last_kind = nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 350 | size_t last_padding_line = 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 351 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 352 | auto* mod = builder_.Sem().Module(); |
| 353 | for (auto* decl : mod->DependencyOrderedDeclarations()) { |
James Price | 98dc5a8 | 2023-02-04 12:20:55 +0000 | [diff] [blame] | 354 | if (decl->IsAnyOf<ast::Alias, ast::DiagnosticDirective, ast::Enable, ast::ConstAssert>()) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 355 | continue; // These are not emitted. |
James Price | 791b435 | 2022-05-11 13:50:33 +0000 | [diff] [blame] | 356 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 357 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 358 | // Emit a new line between declarations if the type of declaration has |
| 359 | // changed, or we're about to emit a function |
| 360 | auto* kind = &decl->TypeInfo(); |
| 361 | if (current_buffer_->lines.size() != last_padding_line) { |
| 362 | if (last_kind && (last_kind != kind || decl->Is<ast::Function>())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 363 | Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 364 | last_padding_line = current_buffer_->lines.size(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 365 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 366 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 367 | last_kind = kind; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 368 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 369 | bool ok = Switch( |
| 370 | decl, |
| 371 | [&](const ast::Variable* global) { // |
| 372 | return EmitGlobalVariable(global); |
| 373 | }, |
| 374 | [&](const ast::Struct* str) { |
| 375 | auto* ty = builder_.Sem().Get(str); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 376 | auto address_space_uses = ty->AddressSpaceUsage(); |
| 377 | if (address_space_uses.size() != |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 378 | (address_space_uses.count(builtin::AddressSpace::kStorage) + |
| 379 | address_space_uses.count(builtin::AddressSpace::kUniform))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 380 | // The structure is used as something other than a storage buffer or |
| 381 | // uniform buffer, so it needs to be emitted. |
| 382 | // Storage buffer are read and written to via a ByteAddressBuffer |
| 383 | // instead of true structure. |
| 384 | // Structures used as uniform buffer are read from an array of |
| 385 | // vectors instead of true structure. |
| 386 | return EmitStructType(current_buffer_, ty); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 387 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 388 | return true; |
| 389 | }, |
| 390 | [&](const ast::Function* func) { |
| 391 | if (func->IsEntryPoint()) { |
| 392 | return EmitEntryPointFunction(func); |
| 393 | } |
| 394 | return EmitFunction(func); |
| 395 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 396 | [&](Default) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 397 | TINT_ICE() << "unhandled module-scope declaration: " << decl->TypeInfo().name; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 398 | return false; |
| 399 | }); |
| 400 | |
| 401 | if (!ok) { |
| 402 | return false; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (!helpers_.lines.empty()) { |
| 407 | current_buffer_->Insert(helpers_, 0, 0); |
| 408 | } |
| 409 | |
| 410 | return true; |
| 411 | } |
| 412 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 413 | bool ASTPrinter::EmitDynamicVectorAssignment(const ast::AssignmentStatement* stmt, |
| 414 | const type::Vector* vec) { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 415 | auto name = tint::GetOrCreate(dynamic_vector_write_, vec, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 416 | std::string fn; |
| 417 | { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 418 | StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 419 | if (!EmitType(ss, vec, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 420 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 421 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 422 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 423 | fn = UniqueIdentifier("set_" + ss.str()); |
| 424 | } |
| 425 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 426 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 427 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 428 | if (!EmitTypeAndName(out, vec, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 429 | builtin::Access::kUndefined, "vec")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 430 | return ""; |
| 431 | } |
| 432 | out << ", int idx, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 433 | if (!EmitTypeAndName(out, vec->type(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 434 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 435 | return ""; |
| 436 | } |
| 437 | out << ") {"; |
| 438 | } |
| 439 | { |
| 440 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 441 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 442 | switch (vec->Width()) { |
| 443 | case 2: |
| 444 | out << "vec = (idx.xx == int2(0, 1)) ? val.xx : vec;"; |
| 445 | break; |
| 446 | case 3: |
| 447 | out << "vec = (idx.xxx == int3(0, 1, 2)) ? val.xxx : vec;"; |
| 448 | break; |
| 449 | case 4: |
| 450 | out << "vec = (idx.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : vec;"; |
| 451 | break; |
| 452 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 453 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 454 | break; |
| 455 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 456 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 457 | Line(&helpers_) << "}"; |
| 458 | Line(&helpers_); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 459 | return fn; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 460 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 461 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 462 | if (name.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 463 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 464 | } |
| 465 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 466 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 467 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 468 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 469 | out << name << "("; |
| 470 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 471 | return false; |
| 472 | } |
| 473 | out << ", "; |
| 474 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 475 | return false; |
| 476 | } |
| 477 | out << ", "; |
| 478 | if (!EmitExpression(out, stmt->rhs)) { |
| 479 | return false; |
| 480 | } |
| 481 | out << ");"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 482 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 483 | return true; |
| 484 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 485 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 486 | bool ASTPrinter::EmitDynamicMatrixVectorAssignment(const ast::AssignmentStatement* stmt, |
| 487 | const type::Matrix* mat) { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 488 | auto name = tint::GetOrCreate(dynamic_matrix_vector_write_, mat, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 489 | std::string fn; |
| 490 | { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 491 | StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 492 | if (!EmitType(ss, mat, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 493 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 494 | return ""; |
| 495 | } |
| 496 | fn = UniqueIdentifier("set_vector_" + ss.str()); |
| 497 | } |
| 498 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 499 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 500 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 501 | if (!EmitTypeAndName(out, mat, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 502 | builtin::Access::kUndefined, "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 503 | return ""; |
| 504 | } |
| 505 | out << ", int col, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 506 | if (!EmitTypeAndName(out, mat->ColumnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 507 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 508 | return ""; |
| 509 | } |
| 510 | out << ") {"; |
| 511 | } |
| 512 | { |
| 513 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 514 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 515 | { |
| 516 | ScopedIndent si2(&helpers_); |
| 517 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 518 | Line(&helpers_) << "case " << i << ": mat[" << i << "] = val; break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 521 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 522 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 523 | Line(&helpers_) << "}"; |
| 524 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 525 | return fn; |
| 526 | }); |
| 527 | |
| 528 | if (name.empty()) { |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | auto* ast_access_expr = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 533 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 534 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 535 | out << name << "("; |
| 536 | if (!EmitExpression(out, ast_access_expr->object)) { |
| 537 | return false; |
| 538 | } |
| 539 | out << ", "; |
| 540 | if (!EmitExpression(out, ast_access_expr->index)) { |
| 541 | return false; |
| 542 | } |
| 543 | out << ", "; |
| 544 | if (!EmitExpression(out, stmt->rhs)) { |
| 545 | return false; |
| 546 | } |
| 547 | out << ");"; |
| 548 | |
| 549 | return true; |
| 550 | } |
| 551 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 552 | bool ASTPrinter::EmitDynamicMatrixScalarAssignment(const ast::AssignmentStatement* stmt, |
| 553 | const type::Matrix* mat) { |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 554 | auto* lhs_row_access = stmt->lhs->As<ast::IndexAccessorExpression>(); |
| 555 | auto* lhs_col_access = lhs_row_access->object->As<ast::IndexAccessorExpression>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 556 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 557 | auto name = tint::GetOrCreate(dynamic_matrix_scalar_write_, mat, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 558 | std::string fn; |
| 559 | { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 560 | StringStream ss; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 561 | if (!EmitType(ss, mat, tint::builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 562 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 563 | return ""; |
| 564 | } |
| 565 | fn = UniqueIdentifier("set_scalar_" + ss.str()); |
| 566 | } |
| 567 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 568 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 569 | out << "void " << fn << "(inout "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 570 | if (!EmitTypeAndName(out, mat, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 571 | builtin::Access::kUndefined, "mat")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 572 | return ""; |
| 573 | } |
| 574 | out << ", int col, int row, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 575 | if (!EmitTypeAndName(out, mat->type(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 576 | builtin::Access::kUndefined, "val")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 577 | return ""; |
| 578 | } |
| 579 | out << ") {"; |
| 580 | } |
| 581 | { |
| 582 | ScopedIndent si(&helpers_); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 583 | Line(&helpers_) << "switch (col) {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 584 | { |
| 585 | ScopedIndent si2(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 586 | for (uint32_t i = 0; i < mat->columns(); ++i) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 587 | Line(&helpers_) << "case " << i << ":"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 588 | { |
| 589 | auto vec_name = "mat[" + std::to_string(i) + "]"; |
| 590 | ScopedIndent si3(&helpers_); |
| 591 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 592 | auto out = Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 593 | switch (mat->rows()) { |
| 594 | case 2: |
| 595 | out << vec_name |
| 596 | << " = (row.xx == int2(0, 1)) ? val.xx : " << vec_name |
| 597 | << ";"; |
| 598 | break; |
| 599 | case 3: |
| 600 | out << vec_name |
| 601 | << " = (row.xxx == int3(0, 1, 2)) ? val.xxx : " << vec_name |
| 602 | << ";"; |
| 603 | break; |
| 604 | case 4: |
| 605 | out << vec_name |
| 606 | << " = (row.xxxx == int4(0, 1, 2, 3)) ? val.xxxx : " |
| 607 | << vec_name << ";"; |
| 608 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 609 | default: { |
| 610 | auto* vec = TypeOf(lhs_row_access->object) |
| 611 | ->UnwrapRef() |
| 612 | ->As<type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 613 | TINT_UNREACHABLE() << "invalid vector size " << vec->Width(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 614 | break; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 615 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 618 | Line(&helpers_) << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 622 | Line(&helpers_) << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 623 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 624 | Line(&helpers_) << "}"; |
| 625 | Line(&helpers_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 626 | return fn; |
| 627 | }); |
| 628 | |
| 629 | if (name.empty()) { |
| 630 | return false; |
| 631 | } |
| 632 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 633 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 634 | out << name << "("; |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 635 | if (!EmitExpression(out, lhs_col_access->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 636 | return false; |
| 637 | } |
| 638 | out << ", "; |
| 639 | if (!EmitExpression(out, lhs_col_access->index)) { |
| 640 | return false; |
| 641 | } |
| 642 | out << ", "; |
| 643 | if (!EmitExpression(out, lhs_row_access->index)) { |
| 644 | return false; |
| 645 | } |
| 646 | out << ", "; |
| 647 | if (!EmitExpression(out, stmt->rhs)) { |
| 648 | return false; |
| 649 | } |
| 650 | out << ");"; |
| 651 | |
| 652 | return true; |
| 653 | } |
| 654 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 655 | bool ASTPrinter::EmitIndexAccessor(StringStream& out, const ast::IndexAccessorExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 656 | if (!EmitExpression(out, expr->object)) { |
| 657 | return false; |
| 658 | } |
| 659 | out << "["; |
| 660 | |
| 661 | if (!EmitExpression(out, expr->index)) { |
| 662 | return false; |
| 663 | } |
| 664 | out << "]"; |
| 665 | |
| 666 | return true; |
| 667 | } |
| 668 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 669 | bool ASTPrinter::EmitBitcast(StringStream& out, const ast::BitcastExpression* expr) { |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 670 | auto* dst_type = TypeOf(expr)->UnwrapRef(); |
| 671 | auto* src_type = TypeOf(expr->expr)->UnwrapRef(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 672 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 673 | auto* src_el_type = src_type->DeepestElement(); |
| 674 | auto* dst_el_type = dst_type->DeepestElement(); |
| 675 | |
| 676 | if (!dst_el_type->is_integer_scalar() && !dst_el_type->is_float_scalar()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 677 | diagnostics_.add_error(diag::System::Writer, |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 678 | "Unable to do bitcast to type " + dst_el_type->FriendlyName()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 682 | // Handle identity bitcast. |
| 683 | if (src_type == dst_type) { |
| 684 | return EmitExpression(out, expr->expr); |
| 685 | } |
| 686 | |
| 687 | // Handle the f16 types using polyfill functions |
| 688 | if (src_el_type->Is<type::F16>() || dst_el_type->Is<type::F16>()) { |
| 689 | auto f16_bitcast_polyfill = [&]() { |
| 690 | if (src_el_type->Is<type::F16>()) { |
| 691 | // Source type must be vec2<f16> or vec4<f16>, since type f16 and vec3<f16> can only |
| 692 | // have identity bitcast. |
| 693 | auto* src_vec = src_type->As<type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 694 | TINT_ASSERT(src_vec); |
| 695 | TINT_ASSERT(((src_vec->Width() == 2u) || (src_vec->Width() == 4u))); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 696 | |
| 697 | // Bitcast f16 types to others by converting the given f16 value to f32 and call |
| 698 | // f32tof16 to get the bits. This should be safe, because the convertion is precise |
| 699 | // for finite and infinite f16 value as they are exactly representable by f32, and |
| 700 | // WGSL spec allow any result if f16 value is NaN. |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 701 | return tint::GetOrCreate( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 702 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 703 | TextBuffer b; |
| 704 | TINT_DEFER(helpers_.Append(b)); |
| 705 | |
| 706 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_from_f16")); |
| 707 | { |
| 708 | auto decl = Line(&b); |
| 709 | if (!EmitTypeAndName(decl, dst_type, builtin::AddressSpace::kUndefined, |
| 710 | builtin::Access::kUndefined, fn_name)) { |
| 711 | return ""; |
| 712 | } |
| 713 | { |
| 714 | ScopedParen sp(decl); |
| 715 | if (!EmitTypeAndName(decl, src_type, |
| 716 | builtin::AddressSpace::kUndefined, |
| 717 | builtin::Access::kUndefined, "src")) { |
| 718 | return ""; |
| 719 | } |
| 720 | } |
| 721 | decl << " {"; |
| 722 | } |
| 723 | { |
| 724 | ScopedIndent si(&b); |
| 725 | { |
| 726 | Line(&b) << "uint" << src_vec->Width() << " r = f32tof16(float" |
| 727 | << src_vec->Width() << "(src));"; |
| 728 | |
| 729 | { |
| 730 | auto s = Line(&b); |
| 731 | s << "return as"; |
| 732 | if (!EmitType(s, dst_el_type, builtin::AddressSpace::kUndefined, |
| 733 | builtin::Access::kReadWrite, "")) { |
| 734 | return ""; |
| 735 | } |
| 736 | s << "("; |
| 737 | switch (src_vec->Width()) { |
| 738 | case 2: { |
| 739 | s << "uint((r.x & 0xffff) | ((r.y & 0xffff) << 16))"; |
| 740 | break; |
| 741 | } |
| 742 | case 4: { |
| 743 | s << "uint2((r.x & 0xffff) | ((r.y & 0xffff) << 16), " |
| 744 | "(r.z & 0xffff) | ((r.w & 0xffff) << 16))"; |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | s << ");"; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | Line(&b) << "}"; |
| 753 | Line(&b); |
| 754 | return fn_name; |
| 755 | }); |
| 756 | } else { |
| 757 | // Destination type must be vec2<f16> or vec4<f16>. |
| 758 | auto* dst_vec = dst_type->As<type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 759 | TINT_ASSERT((dst_vec && ((dst_vec->Width() == 2u) || (dst_vec->Width() == 4u)) && |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 760 | dst_el_type->Is<type::F16>())); |
| 761 | // Source type must be f32/i32/u32 or vec2<f32/i32/u32>. |
| 762 | auto* src_vec = src_type->As<type::Vector>(); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 763 | TINT_ASSERT((src_type->IsAnyOf<type::I32, type::U32, type::F32>() || |
| 764 | (src_vec && src_vec->Width() == 2u && |
| 765 | src_el_type->IsAnyOf<type::I32, type::U32, type::F32>()))); |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 766 | std::string src_type_suffix = (src_vec ? "2" : ""); |
| 767 | |
| 768 | // Bitcast other types to f16 types by reinterpreting their bits as f16 using |
| 769 | // f16tof32, and convert the result f32 to f16. This should be safe, because the |
| 770 | // convertion is precise for finite and infinite f16 result value as they are |
| 771 | // exactly representable by f32, and WGSL spec allow any result if f16 result value |
| 772 | // would be NaN. |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 773 | return tint::GetOrCreate( |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 774 | bitcast_funcs_, BinaryType{{src_type, dst_type}}, [&]() -> std::string { |
| 775 | TextBuffer b; |
| 776 | TINT_DEFER(helpers_.Append(b)); |
| 777 | |
| 778 | auto fn_name = UniqueIdentifier(std::string("tint_bitcast_to_f16")); |
| 779 | { |
| 780 | auto decl = Line(&b); |
| 781 | if (!EmitTypeAndName(decl, dst_type, builtin::AddressSpace::kUndefined, |
| 782 | builtin::Access::kUndefined, fn_name)) { |
| 783 | return ""; |
| 784 | } |
| 785 | { |
| 786 | ScopedParen sp(decl); |
| 787 | if (!EmitTypeAndName(decl, src_type, |
| 788 | builtin::AddressSpace::kUndefined, |
| 789 | builtin::Access::kUndefined, "src")) { |
| 790 | return ""; |
| 791 | } |
| 792 | } |
| 793 | decl << " {"; |
| 794 | } |
| 795 | { |
| 796 | ScopedIndent si(&b); |
| 797 | { |
| 798 | // Convert the source to uint for f16tof32. |
| 799 | Line(&b) << "uint" << src_type_suffix << " v = asuint(src);"; |
| 800 | // Reinterprete the low 16 bits and high 16 bits |
| 801 | Line(&b) << "float" << src_type_suffix |
| 802 | << " t_low = f16tof32(v & 0xffff);"; |
| 803 | Line(&b) << "float" << src_type_suffix |
| 804 | << " t_high = f16tof32((v >> 16) & 0xffff);"; |
| 805 | // Construct the result f16 vector |
| 806 | { |
| 807 | auto s = Line(&b); |
| 808 | s << "return "; |
| 809 | if (!EmitType(s, dst_type, builtin::AddressSpace::kUndefined, |
| 810 | builtin::Access::kReadWrite, "")) { |
| 811 | return ""; |
| 812 | } |
| 813 | s << "("; |
| 814 | switch (dst_vec->Width()) { |
| 815 | case 2: { |
| 816 | s << "t_low.x, t_high.x"; |
| 817 | break; |
| 818 | } |
| 819 | case 4: { |
| 820 | s << "t_low.x, t_high.x, t_low.y, t_high.y"; |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | s << ");"; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | Line(&b) << "}"; |
| 829 | Line(&b); |
| 830 | return fn_name; |
| 831 | }); |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | // Get or create the polyfill |
| 836 | auto fn = f16_bitcast_polyfill(); |
| 837 | if (fn.empty()) { |
| 838 | return false; |
| 839 | } |
| 840 | // Call the polyfill |
| 841 | out << fn; |
| 842 | { |
| 843 | ScopedParen sp(out); |
| 844 | if (!EmitExpression(out, expr->expr)) { |
| 845 | return false; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | // Otherwise, bitcasting between non-f16 types. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 853 | TINT_ASSERT((!src_el_type->Is<type::F16>() && !dst_el_type->Is<type::F16>())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 854 | out << "as"; |
Zhaoming Jiang | 6c4de8b | 2023-07-17 09:37:19 +0000 | [diff] [blame] | 855 | if (!EmitType(out, dst_el_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
| 856 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 859 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 860 | if (!EmitExpression(out, expr->expr)) { |
| 861 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 862 | } |
| 863 | out << ")"; |
| 864 | return true; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 865 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 866 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 867 | bool ASTPrinter::EmitAssign(const ast::AssignmentStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 868 | if (auto* lhs_access = stmt->lhs->As<ast::IndexAccessorExpression>()) { |
| 869 | // BUG(crbug.com/tint/1333): work around assignment of scalar to matrices |
| 870 | // with at least one dynamic index |
| 871 | if (auto* lhs_sub_access = lhs_access->object->As<ast::IndexAccessorExpression>()) { |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 872 | if (auto* mat = TypeOf(lhs_sub_access->object)->UnwrapRef()->As<type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 873 | auto* rhs_row_idx_sem = builder_.Sem().GetVal(lhs_access->index); |
| 874 | auto* rhs_col_idx_sem = builder_.Sem().GetVal(lhs_sub_access->index); |
Antonio Maiorano | f031ca2 | 2023-02-02 22:16:42 +0000 | [diff] [blame] | 875 | if (!rhs_row_idx_sem->ConstantValue() || !rhs_col_idx_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 876 | return EmitDynamicMatrixScalarAssignment(stmt, mat); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | // BUG(crbug.com/tint/1333): work around assignment of vector to matrices |
| 881 | // with dynamic indices |
| 882 | const auto* lhs_access_type = TypeOf(lhs_access->object)->UnwrapRef(); |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 883 | if (auto* mat = lhs_access_type->As<type::Matrix>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 884 | auto* lhs_index_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 885 | if (!lhs_index_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 886 | return EmitDynamicMatrixVectorAssignment(stmt, mat); |
| 887 | } |
| 888 | } |
| 889 | // BUG(crbug.com/tint/534): work around assignment to vectors with dynamic |
| 890 | // indices |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 891 | if (auto* vec = lhs_access_type->As<type::Vector>()) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 892 | auto* rhs_sem = builder_.Sem().GetVal(lhs_access->index); |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 893 | if (!rhs_sem->ConstantValue()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 894 | return EmitDynamicVectorAssignment(stmt, vec); |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 899 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 900 | if (!EmitExpression(out, stmt->lhs)) { |
| 901 | return false; |
| 902 | } |
| 903 | out << " = "; |
| 904 | if (!EmitExpression(out, stmt->rhs)) { |
| 905 | return false; |
| 906 | } |
| 907 | out << ";"; |
| 908 | return true; |
| 909 | } |
| 910 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 911 | bool ASTPrinter::EmitBinary(StringStream& out, const ast::BinaryExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 912 | if (expr->op == ast::BinaryOp::kLogicalAnd || expr->op == ast::BinaryOp::kLogicalOr) { |
| 913 | auto name = UniqueIdentifier(kTempNamePrefix); |
| 914 | |
| 915 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 916 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 917 | pre << "bool " << name << " = "; |
| 918 | if (!EmitExpression(pre, expr->lhs)) { |
| 919 | return false; |
| 920 | } |
| 921 | pre << ";"; |
| 922 | } |
| 923 | |
| 924 | if (expr->op == ast::BinaryOp::kLogicalOr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 925 | Line() << "if (!" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 926 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 927 | Line() << "if (" << name << ") {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | { |
| 931 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 932 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 933 | pre << name << " = "; |
| 934 | if (!EmitExpression(pre, expr->rhs)) { |
| 935 | return false; |
| 936 | } |
| 937 | pre << ";"; |
| 938 | } |
| 939 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 940 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 941 | |
| 942 | out << "(" << name << ")"; |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); |
| 947 | auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); |
| 948 | // Multiplying by a matrix requires the use of `mul` in order to get the |
| 949 | // type of multiply we desire. |
| 950 | if (expr->op == ast::BinaryOp::kMultiply && |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 951 | ((lhs_type->Is<type::Vector>() && rhs_type->Is<type::Matrix>()) || |
| 952 | (lhs_type->Is<type::Matrix>() && rhs_type->Is<type::Vector>()) || |
| 953 | (lhs_type->Is<type::Matrix>() && rhs_type->Is<type::Matrix>()))) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 954 | // Matrices are transposed, so swap LHS and RHS. |
| 955 | out << "mul("; |
| 956 | if (!EmitExpression(out, expr->rhs)) { |
| 957 | return false; |
| 958 | } |
| 959 | out << ", "; |
| 960 | if (!EmitExpression(out, expr->lhs)) { |
| 961 | return false; |
| 962 | } |
| 963 | out << ")"; |
| 964 | |
| 965 | return true; |
| 966 | } |
| 967 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 968 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 969 | |
| 970 | if (!EmitExpression(out, expr->lhs)) { |
| 971 | return false; |
| 972 | } |
| 973 | out << " "; |
| 974 | |
| 975 | switch (expr->op) { |
| 976 | case ast::BinaryOp::kAnd: |
| 977 | out << "&"; |
| 978 | break; |
| 979 | case ast::BinaryOp::kOr: |
| 980 | out << "|"; |
| 981 | break; |
| 982 | case ast::BinaryOp::kXor: |
| 983 | out << "^"; |
| 984 | break; |
| 985 | case ast::BinaryOp::kLogicalAnd: |
| 986 | case ast::BinaryOp::kLogicalOr: { |
| 987 | // These are both handled above. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 988 | TINT_UNREACHABLE(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 989 | return false; |
| 990 | } |
| 991 | case ast::BinaryOp::kEqual: |
| 992 | out << "=="; |
| 993 | break; |
| 994 | case ast::BinaryOp::kNotEqual: |
| 995 | out << "!="; |
| 996 | break; |
| 997 | case ast::BinaryOp::kLessThan: |
| 998 | out << "<"; |
| 999 | break; |
| 1000 | case ast::BinaryOp::kGreaterThan: |
| 1001 | out << ">"; |
| 1002 | break; |
| 1003 | case ast::BinaryOp::kLessThanEqual: |
| 1004 | out << "<="; |
| 1005 | break; |
| 1006 | case ast::BinaryOp::kGreaterThanEqual: |
| 1007 | out << ">="; |
| 1008 | break; |
| 1009 | case ast::BinaryOp::kShiftLeft: |
| 1010 | out << "<<"; |
| 1011 | break; |
| 1012 | case ast::BinaryOp::kShiftRight: |
| 1013 | // TODO(dsinclair): MSL is based on C++14, and >> in C++14 has |
| 1014 | // implementation-defined behaviour for negative LHS. We may have to |
| 1015 | // generate extra code to implement WGSL-specified behaviour for negative |
| 1016 | // LHS. |
| 1017 | out << R"(>>)"; |
| 1018 | break; |
| 1019 | |
| 1020 | case ast::BinaryOp::kAdd: |
| 1021 | out << "+"; |
| 1022 | break; |
| 1023 | case ast::BinaryOp::kSubtract: |
| 1024 | out << "-"; |
| 1025 | break; |
| 1026 | case ast::BinaryOp::kMultiply: |
| 1027 | out << "*"; |
| 1028 | break; |
| 1029 | case ast::BinaryOp::kDivide: |
| 1030 | out << "/"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | case ast::BinaryOp::kModulo: |
| 1033 | out << "%"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | case ast::BinaryOp::kNone: |
| 1036 | diagnostics_.add_error(diag::System::Writer, "missing binary operation type"); |
| 1037 | return false; |
| 1038 | } |
| 1039 | out << " "; |
| 1040 | |
| 1041 | if (!EmitExpression(out, expr->rhs)) { |
| 1042 | return false; |
| 1043 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1044 | |
| 1045 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1048 | bool ASTPrinter::EmitStatements(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1049 | for (auto* s : stmts) { |
| 1050 | if (!EmitStatement(s)) { |
| 1051 | return false; |
| 1052 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1053 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1054 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1057 | bool ASTPrinter::EmitStatementsWithIndent(VectorRef<const ast::Statement*> stmts) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1058 | ScopedIndent si(this); |
| 1059 | return EmitStatements(stmts); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1062 | bool ASTPrinter::EmitBlock(const ast::BlockStatement* stmt) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1063 | Line() << "{"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1064 | if (!EmitStatementsWithIndent(stmt->statements)) { |
| 1065 | return false; |
| 1066 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1067 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1068 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1071 | bool ASTPrinter::EmitBreak(const ast::BreakStatement*) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1072 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1073 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1076 | bool ASTPrinter::EmitBreakIf(const ast::BreakIfStatement* b) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1077 | auto out = Line(); |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 1078 | out << "if ("; |
| 1079 | if (!EmitExpression(out, b->condition)) { |
| 1080 | return false; |
| 1081 | } |
| 1082 | out << ") { break; }"; |
| 1083 | return true; |
| 1084 | } |
| 1085 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1086 | bool ASTPrinter::EmitCall(StringStream& out, const ast::CallExpression* expr) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 1087 | auto* call = builder_.Sem().Get<sem::Call>(expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1088 | auto* target = call->Target(); |
| 1089 | return Switch( |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1090 | target, // |
| 1091 | [&](const sem::Function* func) { return EmitFunctionCall(out, call, func); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1092 | [&](const sem::Builtin* builtin) { return EmitBuiltinCall(out, call, builtin); }, |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1093 | [&](const sem::ValueConversion* conv) { return EmitValueConversion(out, call, conv); }, |
| 1094 | [&](const sem::ValueConstructor* ctor) { return EmitValueConstructor(out, call, ctor); }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1095 | [&](Default) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1096 | TINT_ICE() << "unhandled call target: " << target->TypeInfo().name; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1097 | return false; |
| 1098 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1101 | bool ASTPrinter::EmitFunctionCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1102 | const sem::Call* call, |
| 1103 | const sem::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1104 | auto* expr = call->Declaration(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1105 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1106 | if (ast::HasAttribute<ast::transform::CalculateArrayLength::BufferSizeIntrinsic>( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1107 | func->Declaration()->attributes)) { |
| 1108 | // Special function generated by the CalculateArrayLength transform for |
| 1109 | // calling X.GetDimensions(Y) |
| 1110 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1111 | return false; |
| 1112 | } |
| 1113 | out << ".GetDimensions("; |
| 1114 | if (!EmitExpression(out, call->Arguments()[1]->Declaration())) { |
| 1115 | return false; |
| 1116 | } |
| 1117 | out << ")"; |
| 1118 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1119 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1120 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1121 | if (auto* intrinsic = ast::GetAttribute<ast::transform::DecomposeMemoryAccess::Intrinsic>( |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1122 | func->Declaration()->attributes)) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 1123 | switch (intrinsic->address_space) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1124 | case builtin::AddressSpace::kUniform: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1125 | return EmitUniformBufferAccess(out, expr, intrinsic); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1126 | case builtin::AddressSpace::kStorage: |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1127 | if (!intrinsic->IsAtomic()) { |
| 1128 | return EmitStorageBufferAccess(out, expr, intrinsic); |
| 1129 | } |
| 1130 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1131 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1132 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic address space:" |
| 1133 | << intrinsic->address_space; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1136 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1137 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1138 | out << func->Declaration()->name->symbol.Name() << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1139 | |
| 1140 | bool first = true; |
| 1141 | for (auto* arg : call->Arguments()) { |
| 1142 | if (!first) { |
| 1143 | out << ", "; |
| 1144 | } |
| 1145 | first = false; |
| 1146 | |
| 1147 | if (!EmitExpression(out, arg->Declaration())) { |
| 1148 | return false; |
| 1149 | } |
| 1150 | } |
| 1151 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1152 | out << ")"; |
| 1153 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1156 | bool ASTPrinter::EmitBuiltinCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1157 | const sem::Call* call, |
| 1158 | const sem::Builtin* builtin) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1159 | const auto type = builtin->Type(); |
| 1160 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1161 | auto* expr = call->Declaration(); |
| 1162 | if (builtin->IsTexture()) { |
| 1163 | return EmitTextureCall(out, call, builtin); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1164 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1165 | if (type == builtin::Function::kSelect) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1166 | return EmitSelectCall(out, expr); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1167 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1168 | if (type == builtin::Function::kModf) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1169 | return EmitModfCall(out, expr, builtin); |
| 1170 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1171 | if (type == builtin::Function::kFrexp) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1172 | return EmitFrexpCall(out, expr, builtin); |
| 1173 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1174 | if (type == builtin::Function::kDegrees) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1175 | return EmitDegreesCall(out, expr, builtin); |
| 1176 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1177 | if (type == builtin::Function::kRadians) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1178 | return EmitRadiansCall(out, expr, builtin); |
| 1179 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1180 | if (type == builtin::Function::kSign) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 1181 | return EmitSignCall(out, call, builtin); |
| 1182 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1183 | if (type == builtin::Function::kQuantizeToF16) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 1184 | return EmitQuantizeToF16Call(out, expr, builtin); |
| 1185 | } |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 1186 | if (type == builtin::Function::kTrunc) { |
| 1187 | return EmitTruncCall(out, expr, builtin); |
| 1188 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1189 | if (builtin->IsDataPacking()) { |
| 1190 | return EmitDataPackingCall(out, expr, builtin); |
| 1191 | } |
| 1192 | if (builtin->IsDataUnpacking()) { |
| 1193 | return EmitDataUnpackingCall(out, expr, builtin); |
| 1194 | } |
| 1195 | if (builtin->IsBarrier()) { |
| 1196 | return EmitBarrierCall(out, builtin); |
| 1197 | } |
| 1198 | if (builtin->IsAtomic()) { |
| 1199 | return EmitWorkgroupAtomicCall(out, expr, builtin); |
| 1200 | } |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 1201 | if (builtin->IsDP4a()) { |
| 1202 | return EmitDP4aCall(out, expr, builtin); |
| 1203 | } |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1204 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1205 | auto name = generate_builtin_name(builtin); |
| 1206 | if (name.empty()) { |
| 1207 | return false; |
| 1208 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1209 | |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1210 | // Handle single argument builtins that only accept and return uint (not int overload). We need |
| 1211 | // to explicitly cast the return value (we also cast the arg for good measure). See |
| 1212 | // crbug.com/tint/1550 |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 1213 | if (type == builtin::Function::kCountOneBits || type == builtin::Function::kReverseBits) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1214 | auto* arg = call->Arguments()[0]; |
Ben Clayton | 6c337aa | 2022-12-07 19:25:17 +0000 | [diff] [blame] | 1215 | if (arg->Type()->UnwrapRef()->is_signed_integer_scalar_or_vector()) { |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1216 | out << "asint(" << name << "(asuint("; |
| 1217 | if (!EmitExpression(out, arg->Declaration())) { |
| 1218 | return false; |
| 1219 | } |
| 1220 | out << ")))"; |
| 1221 | return true; |
| 1222 | } |
| 1223 | } |
| 1224 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1225 | out << name << "("; |
| 1226 | |
| 1227 | bool first = true; |
| 1228 | for (auto* arg : call->Arguments()) { |
| 1229 | if (!first) { |
| 1230 | out << ", "; |
| 1231 | } |
| 1232 | first = false; |
| 1233 | |
| 1234 | if (!EmitExpression(out, arg->Declaration())) { |
| 1235 | return false; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | out << ")"; |
Antonio Maiorano | ab4c035 | 2022-05-20 01:58:40 +0000 | [diff] [blame] | 1240 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1241 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1244 | bool ASTPrinter::EmitValueConversion(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1245 | const sem::Call* call, |
| 1246 | const sem::ValueConversion* conv) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1247 | if (!EmitType(out, conv->Target(), builtin::AddressSpace::kUndefined, |
| 1248 | builtin::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1252 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1253 | if (!EmitExpression(out, call->Arguments()[0]->Declaration())) { |
| 1254 | return false; |
| 1255 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1256 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1257 | out << ")"; |
| 1258 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1261 | bool ASTPrinter::EmitValueConstructor(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1262 | const sem::Call* call, |
| 1263 | const sem::ValueConstructor* ctor) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1264 | auto* type = call->Type(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1265 | |
Ben Clayton | 54a104e | 2023-02-22 20:04:40 +0000 | [diff] [blame] | 1266 | // If the value constructor arguments are empty then we need to construct with the zero value |
| 1267 | // for all components. |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1268 | if (call->Arguments().IsEmpty()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1269 | return EmitZeroValue(out, type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1270 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1271 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1272 | // Single parameter matrix initializers must be identity initializer. |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1273 | // It could also be conversions between f16 and f32 matrix when f16 is properly supported. |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 1274 | if (type->Is<type::Matrix>() && call->Arguments().Length() == 1) { |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1275 | if (!ctor->Parameters()[0]->Type()->UnwrapRef()->is_float_matrix()) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1276 | TINT_UNREACHABLE() |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 1277 | << "found a single-parameter matrix initializer that is not identity initializer"; |
Zhaoming Jiang | c5f7e8f | 2022-06-24 17:21:59 +0000 | [diff] [blame] | 1278 | return false; |
Ben Clayton | 3b5edf1 | 2022-05-16 21:14:11 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 1282 | bool brackets = type->IsAnyOf<type::Array, type::Struct>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1283 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1284 | // For single-value vector initializers, swizzle the scalar to the right |
| 1285 | // vector dimension using .x |
| 1286 | const bool is_single_value_vector_init = type->is_scalar_vector() && |
Ben Clayton | 958a464 | 2022-07-26 07:55:24 +0000 | [diff] [blame] | 1287 | call->Arguments().Length() == 1 && |
Ben Clayton | 1416b18 | 2023-06-08 20:29:30 +0000 | [diff] [blame] | 1288 | ctor->Parameters()[0]->Type()->Is<type::Scalar>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1289 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 1290 | if (brackets) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1291 | out << "{"; |
| 1292 | } else { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1293 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
| 1294 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1298 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1299 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1300 | if (is_single_value_vector_init) { |
| 1301 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1302 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1303 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1304 | bool first = true; |
| 1305 | for (auto* e : call->Arguments()) { |
| 1306 | if (!first) { |
| 1307 | out << ", "; |
| 1308 | } |
| 1309 | first = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1310 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1311 | if (!EmitExpression(out, e->Declaration())) { |
| 1312 | return false; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | if (is_single_value_vector_init) { |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 1317 | out << ")." << std::string(type->As<type::Vector>()->Width(), 'x'); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | out << (brackets ? "}" : ")"); |
| 1321 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1324 | bool ASTPrinter::EmitUniformBufferAccess( |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1325 | StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1326 | const ast::CallExpression* expr, |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1327 | const ast::transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1328 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1329 | auto* const offset = expr->args[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1330 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1331 | // offset in bytes |
| 1332 | uint32_t scalar_offset_bytes = 0; |
| 1333 | // offset in uint (4 bytes) |
| 1334 | uint32_t scalar_offset_index = 0; |
| 1335 | // expression to calculate offset in bytes |
| 1336 | std::string scalar_offset_bytes_expr; |
| 1337 | // expression to calculate offset in uint, by dividing scalar_offset_bytes_expr by 4 |
| 1338 | std::string scalar_offset_index_expr; |
| 1339 | // expression to calculate offset in uint, independently |
| 1340 | std::string scalar_offset_index_unified_expr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1341 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1342 | // If true, use scalar_offset_index, otherwise use scalar_offset_index_expr |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1343 | bool scalar_offset_constant = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1344 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1345 | if (auto* val = builder_.Sem().GetVal(offset)->ConstantValue()) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1346 | TINT_ASSERT(val->Type()->Is<type::U32>()); |
dan sinclair | b53b8cf | 2022-12-15 16:25:31 +0000 | [diff] [blame] | 1347 | scalar_offset_bytes = static_cast<uint32_t>(val->ValueAs<AInt>()); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1348 | scalar_offset_index = scalar_offset_bytes / 4; // bytes -> scalar index |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1349 | scalar_offset_constant = true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1350 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1351 | |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1352 | // If true, scalar_offset_bytes or scalar_offset_bytes_expr should be used, otherwise only use |
| 1353 | // scalar_offset_index or scalar_offset_index_unified_expr. Currently only loading f16 scalar |
| 1354 | // require using offset in bytes. |
| 1355 | const bool need_offset_in_bytes = |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1356 | intrinsic->type == ast::transform::DecomposeMemoryAccess::Intrinsic::DataType::kF16; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1357 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1358 | if (!scalar_offset_constant) { |
| 1359 | // UBO offset not compile-time known. |
| 1360 | // Calculate the scalar offset into a temporary. |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1361 | if (need_offset_in_bytes) { |
| 1362 | scalar_offset_bytes_expr = UniqueIdentifier("scalar_offset_bytes"); |
| 1363 | scalar_offset_index_expr = UniqueIdentifier("scalar_offset_index"); |
| 1364 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1365 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1366 | pre << "const uint " << scalar_offset_bytes_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1367 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
| 1370 | pre << ");"; |
| 1371 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1372 | Line() << "const uint " << scalar_offset_index_expr << " = " << scalar_offset_bytes_expr |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1373 | << " / 4;"; |
| 1374 | } else { |
| 1375 | scalar_offset_index_unified_expr = UniqueIdentifier("scalar_offset"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1376 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1377 | pre << "const uint " << scalar_offset_index_unified_expr << " = ("; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1378 | if (!EmitExpression(pre, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1379 | return false; |
| 1380 | } |
| 1381 | pre << ") / 4;"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1382 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1383 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1384 | |
Austin Eng | a0e96b5 | 2023-02-18 00:39:01 +0000 | [diff] [blame] | 1385 | const char swizzle[] = {'x', 'y', 'z', 'w'}; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1386 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1387 | using Op = ast::transform::DecomposeMemoryAccess::Intrinsic::Op; |
| 1388 | using DataType = ast::transform::DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1389 | switch (intrinsic->op) { |
| 1390 | case Op::kLoad: { |
| 1391 | auto cast = [&](const char* to, auto&& load) { |
| 1392 | out << to << "("; |
| 1393 | auto result = load(); |
| 1394 | out << ")"; |
| 1395 | return result; |
| 1396 | }; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1397 | auto load_u32_to = [&](StringStream& target) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1398 | target << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1399 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1400 | target << "[" << (scalar_offset_index / 4) << "]." |
| 1401 | << swizzle[scalar_offset_index & 3]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1402 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1403 | target << "[" << scalar_offset_index_unified_expr << " / 4][" |
| 1404 | << scalar_offset_index_unified_expr << " % 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1405 | } |
| 1406 | return true; |
| 1407 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1408 | auto load_u32 = [&] { return load_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1409 | // 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] | 1410 | auto load_vec2_u32_to = [&](StringStream& target) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1411 | if (scalar_offset_constant) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1412 | target << buffer << "[" << (scalar_offset_index / 4) << "]" |
| 1413 | << ((scalar_offset_index & 2) == 0 ? ".xy" : ".zw"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1414 | } else { |
| 1415 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1416 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1417 | auto pre = Line(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1418 | pre << "uint4 " << ubo_load << " = " << buffer << "[" |
| 1419 | << scalar_offset_index_unified_expr << " / 4];"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1420 | } |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1421 | target << "((" << scalar_offset_index_unified_expr << " & 2) ? " << ubo_load |
| 1422 | << ".zw : " << ubo_load << ".xy)"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1423 | } |
| 1424 | return true; |
| 1425 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1426 | auto load_vec2_u32 = [&] { return load_vec2_u32_to(out); }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1427 | // vec4 has a minimum alignment of 16 bytes, easiest case |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1428 | auto load_vec4_u32 = [&] { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1429 | out << buffer; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1430 | if (scalar_offset_constant) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1431 | out << "[" << (scalar_offset_index / 4) << "]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1432 | } else { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1433 | out << "[" << scalar_offset_index_unified_expr << " / 4]"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1434 | } |
| 1435 | return true; |
| 1436 | }; |
| 1437 | // 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] | 1438 | auto load_vec3_u32 = [&] { |
| 1439 | if (!load_vec4_u32()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1440 | return false; |
| 1441 | } |
| 1442 | out << ".xyz"; |
| 1443 | return true; |
| 1444 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1445 | auto load_scalar_f16 = [&] { |
| 1446 | // offset bytes = 4k, ((buffer[index].x) & 0xFFFF) |
| 1447 | // offset bytes = 4k+2, ((buffer[index].x >> 16) & 0xFFFF) |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1448 | out << "float16_t(f16tof32(((" << buffer; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1449 | if (scalar_offset_constant) { |
| 1450 | out << "[" << (scalar_offset_index / 4) << "]." |
| 1451 | << swizzle[scalar_offset_index & 3]; |
| 1452 | // WGSL spec ensure little endian memory layout. |
| 1453 | if (scalar_offset_bytes % 4 == 0) { |
| 1454 | out << ") & 0xFFFF)"; |
| 1455 | } else { |
| 1456 | out << " >> 16) & 0xFFFF)"; |
| 1457 | } |
| 1458 | } else { |
| 1459 | out << "[" << scalar_offset_index_expr << " / 4][" << scalar_offset_index_expr |
| 1460 | << " % 4] >> (" << scalar_offset_bytes_expr |
| 1461 | << " % 4 == 0 ? 0 : 16)) & 0xFFFF)"; |
| 1462 | } |
| 1463 | out << "))"; |
| 1464 | return true; |
| 1465 | }; |
| 1466 | auto load_vec2_f16 = [&] { |
| 1467 | // vec2<f16> is aligned to 4 bytes |
| 1468 | // Preclude code load the vec2<f16> data as a uint: |
| 1469 | // uint ubo_load = buffer[id0][id1]; |
| 1470 | // Loading code convert it to vec2<f16>: |
| 1471 | // vector<float16_t, 2>(float16_t(f16tof32(ubo_load & 0xFFFF)), |
| 1472 | // float16_t(f16tof32(ubo_load >> 16))) |
| 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(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1476 | // Load the 4 bytes f16 vector as an uint |
| 1477 | pre << "uint " << ubo_load << " = "; |
| 1478 | if (!load_u32_to(pre)) { |
| 1479 | return false; |
| 1480 | } |
| 1481 | pre << ";"; |
| 1482 | } |
| 1483 | out << "vector<float16_t, 2>(float16_t(f16tof32(" << ubo_load |
| 1484 | << " & 0xFFFF)), float16_t(f16tof32(" << ubo_load << " >> 16)))"; |
| 1485 | return true; |
| 1486 | }; |
| 1487 | auto load_vec3_f16 = [&] { |
| 1488 | // vec3<f16> is aligned to 8 bytes |
| 1489 | // Preclude code load the vec3<f16> data as uint2 and convert its elements to |
| 1490 | // float16_t: |
| 1491 | // uint2 ubo_load = buffer[id0].xy; |
| 1492 | // /* The low 8 bits of two uint are the x and z elements of vec3<f16> */ |
| 1493 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1494 | // 0xFFFF)); |
| 1495 | // /* The high 8 bits of first uint is the y element of vec3<f16> */ |
| 1496 | // float16_t ubo_load_y = f16tof32(ubo_load[0] >> 16); |
| 1497 | // Loading code convert it to vec3<f16>: |
| 1498 | // vector<float16_t, 3>(ubo_load_xz[0], ubo_load_y, ubo_load_xz[1]) |
| 1499 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1500 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1501 | std::string ubo_load_y = UniqueIdentifier(ubo_load + "_y"); |
| 1502 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1503 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1504 | // Load the 8 bytes uint2 with the f16 vector at lower 6 bytes |
| 1505 | pre << "uint2 " << ubo_load << " = "; |
| 1506 | if (!load_vec2_u32_to(pre)) { |
| 1507 | return false; |
| 1508 | } |
| 1509 | pre << ";"; |
| 1510 | } |
| 1511 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1512 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1513 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1514 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1515 | } |
| 1516 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1517 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1518 | pre << "float16_t " << ubo_load_y << " = f16tof32(" << ubo_load |
| 1519 | << "[0] >> 16);"; |
| 1520 | } |
| 1521 | out << "vector<float16_t, 3>(" << ubo_load_xz << "[0], " << ubo_load_y << ", " |
| 1522 | << ubo_load_xz << "[1])"; |
| 1523 | return true; |
| 1524 | }; |
| 1525 | auto load_vec4_f16 = [&] { |
| 1526 | // vec4<f16> is aligned to 8 bytes |
| 1527 | // Preclude code load the vec4<f16> data as uint2 and convert its elements to |
| 1528 | // float16_t: |
| 1529 | // uint2 ubo_load = buffer[id0].xy; |
| 1530 | // /* The low 8 bits of two uint are the x and z elements of vec4<f16> */ |
| 1531 | // vector<float16_t> ubo_load_xz = vector<float16_t, 2>(f16tof32(ubo_load & |
| 1532 | // 0xFFFF)); |
| 1533 | // /* The high 8 bits of two uint are the y and w elements of vec4<f16> */ |
| 1534 | // vector<float16_t, 2> ubo_load_yw = vector<float16_t, 2>(f16tof32(ubo_load >> |
| 1535 | // 16)); |
| 1536 | // Loading code convert it to vec4<f16>: |
| 1537 | // vector<float16_t, 4>(ubo_load_xz[0], ubo_load_yw[0], ubo_load_xz[1], |
| 1538 | // ubo_load_yw[1]) |
| 1539 | std::string ubo_load = UniqueIdentifier("ubo_load"); |
| 1540 | std::string ubo_load_xz = UniqueIdentifier(ubo_load + "_xz"); |
| 1541 | std::string ubo_load_yw = UniqueIdentifier(ubo_load + "_yw"); |
| 1542 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1543 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1544 | // Load the 8 bytes f16 vector as an uint2 |
| 1545 | pre << "uint2 " << ubo_load << " = "; |
| 1546 | if (!load_vec2_u32_to(pre)) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | pre << ";"; |
| 1550 | } |
| 1551 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1552 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1553 | pre << "vector<float16_t, 2> " << ubo_load_xz |
| 1554 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " & 0xFFFF));"; |
| 1555 | } |
| 1556 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1557 | auto pre = Line(); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1558 | pre << "vector<float16_t, 2> " << ubo_load_yw |
| 1559 | << " = vector<float16_t, 2>(f16tof32(" << ubo_load << " >> 16));"; |
| 1560 | } |
| 1561 | out << "vector<float16_t, 4>(" << ubo_load_xz << "[0], " << ubo_load_yw << "[0], " |
| 1562 | << ubo_load_xz << "[1], " << ubo_load_yw << "[1])"; |
| 1563 | return true; |
| 1564 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1565 | switch (intrinsic->type) { |
| 1566 | case DataType::kU32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1567 | return load_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1568 | case DataType::kF32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1569 | return cast("asfloat", load_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1570 | case DataType::kI32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1571 | return cast("asint", load_u32); |
| 1572 | case DataType::kF16: |
| 1573 | return load_scalar_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1574 | case DataType::kVec2U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1575 | return load_vec2_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1576 | case DataType::kVec2F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1577 | return cast("asfloat", load_vec2_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1578 | case DataType::kVec2I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1579 | return cast("asint", load_vec2_u32); |
| 1580 | case DataType::kVec2F16: |
| 1581 | return load_vec2_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1582 | case DataType::kVec3U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1583 | return load_vec3_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1584 | case DataType::kVec3F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1585 | return cast("asfloat", load_vec3_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1586 | case DataType::kVec3I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1587 | return cast("asint", load_vec3_u32); |
| 1588 | case DataType::kVec3F16: |
| 1589 | return load_vec3_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1590 | case DataType::kVec4U32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1591 | return load_vec4_u32(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1592 | case DataType::kVec4F32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1593 | return cast("asfloat", load_vec4_u32); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1594 | case DataType::kVec4I32: |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1595 | return cast("asint", load_vec4_u32); |
| 1596 | case DataType::kVec4F16: |
| 1597 | return load_vec4_f16(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1598 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1599 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1600 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
| 1603 | default: |
| 1604 | break; |
| 1605 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1606 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1607 | << static_cast<int>(intrinsic->op); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1608 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1611 | bool ASTPrinter::EmitStorageBufferAccess( |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1612 | StringStream& out, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1613 | const ast::CallExpression* expr, |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1614 | const ast::transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1615 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1616 | auto* const offset = expr->args[0]; |
Ben Clayton | 407137a | 2023-06-28 21:02:32 +0000 | [diff] [blame] | 1617 | auto* const value = expr->args.Length() > 1 ? expr->args[1] : nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1618 | |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1619 | using Op = ast::transform::DecomposeMemoryAccess::Intrinsic::Op; |
| 1620 | using DataType = ast::transform::DecomposeMemoryAccess::Intrinsic::DataType; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1621 | switch (intrinsic->op) { |
| 1622 | case Op::kLoad: { |
| 1623 | auto load = [&](const char* cast, int n) { |
| 1624 | if (cast) { |
| 1625 | out << cast << "("; |
| 1626 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1627 | out << buffer << ".Load"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1628 | if (n > 1) { |
| 1629 | out << n; |
| 1630 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1631 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1632 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1633 | return false; |
| 1634 | } |
| 1635 | if (cast) { |
| 1636 | out << ")"; |
| 1637 | } |
| 1638 | return true; |
| 1639 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1640 | // Templated load used for f16 types, requires SM6.2 or higher and DXC |
| 1641 | // Used by loading f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1642 | // to emit `buffer.Load<float16_t>(offset)`. |
| 1643 | auto templated_load = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1644 | out << buffer << ".Load<" << type << ">"; // templated load |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1645 | ScopedParen sp(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1646 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1647 | return false; |
| 1648 | } |
| 1649 | return true; |
| 1650 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1651 | switch (intrinsic->type) { |
| 1652 | case DataType::kU32: |
| 1653 | return load(nullptr, 1); |
| 1654 | case DataType::kF32: |
| 1655 | return load("asfloat", 1); |
| 1656 | case DataType::kI32: |
| 1657 | return load("asint", 1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1658 | case DataType::kF16: |
| 1659 | return templated_load("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1660 | case DataType::kVec2U32: |
| 1661 | return load(nullptr, 2); |
| 1662 | case DataType::kVec2F32: |
| 1663 | return load("asfloat", 2); |
| 1664 | case DataType::kVec2I32: |
| 1665 | return load("asint", 2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1666 | case DataType::kVec2F16: |
| 1667 | return templated_load("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1668 | case DataType::kVec3U32: |
| 1669 | return load(nullptr, 3); |
| 1670 | case DataType::kVec3F32: |
| 1671 | return load("asfloat", 3); |
| 1672 | case DataType::kVec3I32: |
| 1673 | return load("asint", 3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1674 | case DataType::kVec3F16: |
| 1675 | return templated_load("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1676 | case DataType::kVec4U32: |
| 1677 | return load(nullptr, 4); |
| 1678 | case DataType::kVec4F32: |
| 1679 | return load("asfloat", 4); |
| 1680 | case DataType::kVec4I32: |
| 1681 | return load("asint", 4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1682 | case DataType::kVec4F16: |
| 1683 | return templated_load("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1684 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1685 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1686 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1687 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1688 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1689 | |
| 1690 | case Op::kStore: { |
| 1691 | auto store = [&](int n) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1692 | out << buffer << ".Store"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1693 | if (n > 1) { |
| 1694 | out << n; |
| 1695 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1696 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1697 | if (!EmitExpression(out, offset)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1698 | return false; |
| 1699 | } |
| 1700 | out << ", asuint"; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1701 | ScopedParen sp2(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1702 | if (!EmitExpression(out, value)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1703 | return false; |
| 1704 | } |
| 1705 | return true; |
| 1706 | }; |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1707 | // Templated stored used for f16 types, requires SM6.2 or higher and DXC |
| 1708 | // Used by storing f16 types, e.g. for f16 type, set type parameter to "float16_t" |
| 1709 | // to emit `buffer.Store<float16_t>(offset)`. |
| 1710 | auto templated_store = [&](const char* type) { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1711 | out << buffer << ".Store<" << type << ">"; // templated store |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 1712 | ScopedParen sp1(out); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1713 | if (!EmitExpression(out, offset)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1714 | return false; |
| 1715 | } |
| 1716 | out << ", "; |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1717 | if (!EmitExpression(out, value)) { |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1718 | return false; |
| 1719 | } |
| 1720 | return true; |
| 1721 | }; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1722 | switch (intrinsic->type) { |
| 1723 | case DataType::kU32: |
| 1724 | return store(1); |
| 1725 | case DataType::kF32: |
| 1726 | return store(1); |
| 1727 | case DataType::kI32: |
| 1728 | return store(1); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1729 | case DataType::kF16: |
| 1730 | return templated_store("float16_t"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1731 | case DataType::kVec2U32: |
| 1732 | return store(2); |
| 1733 | case DataType::kVec2F32: |
| 1734 | return store(2); |
| 1735 | case DataType::kVec2I32: |
| 1736 | return store(2); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1737 | case DataType::kVec2F16: |
| 1738 | return templated_store("vector<float16_t, 2> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1739 | case DataType::kVec3U32: |
| 1740 | return store(3); |
| 1741 | case DataType::kVec3F32: |
| 1742 | return store(3); |
| 1743 | case DataType::kVec3I32: |
| 1744 | return store(3); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1745 | case DataType::kVec3F16: |
| 1746 | return templated_store("vector<float16_t, 3> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1747 | case DataType::kVec4U32: |
| 1748 | return store(4); |
| 1749 | case DataType::kVec4F32: |
| 1750 | return store(4); |
| 1751 | case DataType::kVec4I32: |
| 1752 | return store(4); |
Zhaoming Jiang | ab9b5f3 | 2022-11-24 05:25:35 +0000 | [diff] [blame] | 1753 | case DataType::kVec4F16: |
| 1754 | return templated_store("vector<float16_t, 4> "); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1755 | } |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1756 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::DataType: " |
| 1757 | << static_cast<int>(intrinsic->type); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1758 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1759 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1760 | default: |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1761 | // Break out to error case below |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1762 | // Note that atomic intrinsics are generated as functions. |
| 1763 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1766 | TINT_UNREACHABLE() << "unsupported DecomposeMemoryAccess::Intrinsic::Op: " |
| 1767 | << static_cast<int>(intrinsic->op); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1768 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1771 | bool ASTPrinter::EmitStorageAtomicIntrinsic( |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1772 | const ast::Function* func, |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 1773 | const ast::transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { |
| 1774 | using Op = ast::transform::DecomposeMemoryAccess::Intrinsic::Op; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1775 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1776 | const sem::Function* sem_func = builder_.Sem().Get(func); |
| 1777 | auto* result_ty = sem_func->ReturnType(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1778 | const auto name = func->name->symbol.Name(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1779 | auto& buf = *current_buffer_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1780 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 1781 | auto const buffer = intrinsic->Buffer()->identifier->symbol.Name(); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1782 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1783 | auto rmw = [&](const char* hlsl) -> bool { |
| 1784 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1785 | auto fn = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1786 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1787 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1788 | return false; |
| 1789 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1790 | fn << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1791 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1792 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1793 | return false; |
| 1794 | } |
| 1795 | fn << ") {"; |
| 1796 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1797 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1798 | buf.IncrementIndent(); |
| 1799 | TINT_DEFER({ |
| 1800 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1801 | Line(&buf) << "}"; |
| 1802 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1803 | }); |
| 1804 | |
| 1805 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1806 | auto l = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1807 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1808 | builtin::Access::kUndefined, "original_value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1809 | return false; |
| 1810 | } |
| 1811 | l << " = 0;"; |
| 1812 | } |
| 1813 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1814 | auto l = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1815 | l << buffer << "." << hlsl << "(offset, "; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1816 | if (intrinsic->op == Op::kAtomicSub) { |
| 1817 | l << "-"; |
| 1818 | } |
| 1819 | l << "value, original_value);"; |
| 1820 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1821 | Line(&buf) << "return original_value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1822 | return true; |
| 1823 | }; |
| 1824 | |
| 1825 | switch (intrinsic->op) { |
| 1826 | case Op::kAtomicAdd: |
| 1827 | return rmw("InterlockedAdd"); |
| 1828 | |
| 1829 | case Op::kAtomicSub: |
| 1830 | // Use add with the operand negated. |
| 1831 | return rmw("InterlockedAdd"); |
| 1832 | |
| 1833 | case Op::kAtomicMax: |
| 1834 | return rmw("InterlockedMax"); |
| 1835 | |
| 1836 | case Op::kAtomicMin: |
| 1837 | return rmw("InterlockedMin"); |
| 1838 | |
| 1839 | case Op::kAtomicAnd: |
| 1840 | return rmw("InterlockedAnd"); |
| 1841 | |
| 1842 | case Op::kAtomicOr: |
| 1843 | return rmw("InterlockedOr"); |
| 1844 | |
| 1845 | case Op::kAtomicXor: |
| 1846 | return rmw("InterlockedXor"); |
| 1847 | |
| 1848 | case Op::kAtomicExchange: |
| 1849 | return rmw("InterlockedExchange"); |
| 1850 | |
| 1851 | case Op::kAtomicLoad: { |
| 1852 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 1853 | // InterlockedOr using 0 as the OR value |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1854 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1855 | auto fn = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1856 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1857 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1858 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1859 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1860 | fn << "(uint offset) {"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | buf.IncrementIndent(); |
| 1864 | TINT_DEFER({ |
| 1865 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1866 | Line(&buf) << "}"; |
| 1867 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1868 | }); |
| 1869 | |
| 1870 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1871 | auto l = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1872 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1873 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1874 | return false; |
| 1875 | } |
| 1876 | l << " = 0;"; |
| 1877 | } |
| 1878 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1879 | Line(&buf) << buffer << ".InterlockedOr(offset, 0, value);"; |
| 1880 | Line(&buf) << "return value;"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1881 | return true; |
| 1882 | } |
| 1883 | case Op::kAtomicStore: { |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1884 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1885 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 1886 | // InterlockedExchange and discard the returned value |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1887 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1888 | auto fn = Line(&buf); |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1889 | fn << "void " << name << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1890 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1891 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1892 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1893 | } |
| 1894 | fn << ") {"; |
| 1895 | } |
| 1896 | |
| 1897 | buf.IncrementIndent(); |
| 1898 | TINT_DEFER({ |
| 1899 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1900 | Line(&buf) << "}"; |
| 1901 | Line(&buf); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1902 | }); |
| 1903 | |
| 1904 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1905 | auto l = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1906 | if (!EmitTypeAndName(l, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1907 | builtin::Access::kUndefined, "ignored")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1908 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1909 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1910 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1911 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1912 | Line(&buf) << buffer << ".InterlockedExchange(offset, value, ignored);"; |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1913 | return true; |
| 1914 | } |
| 1915 | case Op::kAtomicCompareExchangeWeak: { |
Ben Clayton | 4b8a3d3 | 2023-06-13 16:55:57 +0000 | [diff] [blame] | 1916 | if (!EmitStructType(&helpers_, result_ty->As<type::Struct>())) { |
| 1917 | return false; |
| 1918 | } |
| 1919 | |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1920 | auto* const value_ty = sem_func->Parameters()[1]->Type()->UnwrapRef(); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1921 | // NOTE: We don't need to emit the return type struct here as DecomposeMemoryAccess |
| 1922 | // 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] | 1923 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1924 | auto fn = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1925 | if (!EmitTypeAndName(fn, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1926 | builtin::Access::kUndefined, name)) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1927 | return false; |
| 1928 | } |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1929 | fn << "(uint offset, "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1930 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1931 | builtin::Access::kUndefined, "compare")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1932 | return false; |
| 1933 | } |
| 1934 | fn << ", "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1935 | if (!EmitTypeAndName(fn, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1936 | builtin::Access::kUndefined, "value")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1937 | return false; |
| 1938 | } |
| 1939 | fn << ") {"; |
| 1940 | } |
| 1941 | |
| 1942 | buf.IncrementIndent(); |
| 1943 | TINT_DEFER({ |
| 1944 | buf.DecrementIndent(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1945 | Line(&buf) << "}"; |
| 1946 | Line(&buf); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1947 | }); |
| 1948 | |
| 1949 | { // T result = {0}; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1950 | auto l = Line(&buf); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1951 | if (!EmitTypeAndName(l, result_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1952 | builtin::Access::kUndefined, "result")) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1953 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1954 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1955 | l << "="; |
| 1956 | if (!EmitZeroValue(l, result_ty)) { |
| 1957 | return false; |
| 1958 | } |
| 1959 | l << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1960 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1961 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1962 | Line(&buf) << buffer |
Ben Clayton | 1a1b527 | 2023-02-24 17:16:55 +0000 | [diff] [blame] | 1963 | << ".InterlockedCompareExchange(offset, compare, value, result.old_value);"; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1964 | Line(&buf) << "result.exchanged = result.old_value == compare;"; |
| 1965 | Line(&buf) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1966 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1967 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1968 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1969 | default: |
| 1970 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1971 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1972 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 1973 | TINT_UNREACHABLE() << "unsupported atomic DecomposeMemoryAccess::Intrinsic::Op: " |
| 1974 | << static_cast<int>(intrinsic->op); |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 1975 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 1978 | bool ASTPrinter::EmitWorkgroupAtomicCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 1979 | const ast::CallExpression* expr, |
| 1980 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1981 | std::string result = UniqueIdentifier("atomic_result"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1982 | |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 1983 | if (!builtin->ReturnType()->Is<type::Void>()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1984 | auto pre = Line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 1985 | if (!EmitTypeAndName(pre, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 1986 | builtin::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1987 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1988 | } |
| 1989 | pre << " = "; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1990 | if (!EmitZeroValue(pre, builtin->ReturnType())) { |
| 1991 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1992 | } |
| 1993 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1994 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1995 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1996 | auto call = [&](const char* name) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 1997 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 1998 | pre << name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1999 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2000 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2001 | ScopedParen sp(pre); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 2002 | for (size_t i = 0; i < expr->args.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2003 | auto* arg = expr->args[i]; |
| 2004 | if (i > 0) { |
| 2005 | pre << ", "; |
| 2006 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2007 | if (i == 1 && builtin->Type() == builtin::Function::kAtomicSub) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2008 | // Sub uses InterlockedAdd with the operand negated. |
| 2009 | pre << "-"; |
| 2010 | } |
| 2011 | if (!EmitExpression(pre, arg)) { |
| 2012 | return false; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | pre << ", " << result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2017 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2018 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2019 | pre << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2020 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2021 | out << result; |
| 2022 | return true; |
| 2023 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2024 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2025 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2026 | case builtin::Function::kAtomicLoad: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2027 | // HLSL does not have an InterlockedLoad, so we emulate it with |
| 2028 | // InterlockedOr using 0 as the OR value |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2029 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2030 | pre << "InterlockedOr"; |
| 2031 | { |
| 2032 | ScopedParen sp(pre); |
| 2033 | if (!EmitExpression(pre, expr->args[0])) { |
| 2034 | return false; |
| 2035 | } |
| 2036 | pre << ", 0, " << result; |
| 2037 | } |
| 2038 | pre << ";"; |
| 2039 | |
| 2040 | out << result; |
| 2041 | return true; |
| 2042 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2043 | case builtin::Function::kAtomicStore: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2044 | // HLSL does not have an InterlockedStore, so we emulate it with |
| 2045 | // InterlockedExchange and discard the returned value |
| 2046 | { // T result = 0; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2047 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2048 | auto* value_ty = builtin->Parameters()[1]->Type()->UnwrapRef(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2049 | if (!EmitTypeAndName(pre, value_ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2050 | builtin::Access::kUndefined, result)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2051 | return false; |
| 2052 | } |
| 2053 | pre << " = "; |
| 2054 | if (!EmitZeroValue(pre, value_ty)) { |
| 2055 | return false; |
| 2056 | } |
| 2057 | pre << ";"; |
| 2058 | } |
| 2059 | |
| 2060 | out << "InterlockedExchange"; |
| 2061 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2062 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2063 | if (!EmitExpression(out, expr->args[0])) { |
| 2064 | return false; |
| 2065 | } |
| 2066 | out << ", "; |
| 2067 | if (!EmitExpression(out, expr->args[1])) { |
| 2068 | return false; |
| 2069 | } |
| 2070 | out << ", " << result; |
| 2071 | } |
| 2072 | return true; |
| 2073 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2074 | case builtin::Function::kAtomicCompareExchangeWeak: { |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 2075 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<type::Struct>())) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2079 | auto* dest = expr->args[0]; |
| 2080 | auto* compare_value = expr->args[1]; |
| 2081 | auto* value = expr->args[2]; |
| 2082 | |
| 2083 | std::string compare = UniqueIdentifier("atomic_compare_value"); |
| 2084 | |
| 2085 | { // T compare_value = <compare_value>; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2086 | auto pre = Line(); |
Antonio Maiorano | f99671b | 2022-06-23 13:14:54 +0000 | [diff] [blame] | 2087 | if (!EmitTypeAndName(pre, TypeOf(compare_value)->UnwrapRef(), |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2088 | builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | 61c16eb | 2023-01-21 23:44:38 +0000 | [diff] [blame] | 2089 | compare)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2090 | return false; |
| 2091 | } |
| 2092 | pre << " = "; |
| 2093 | if (!EmitExpression(pre, compare_value)) { |
| 2094 | return false; |
| 2095 | } |
| 2096 | pre << ";"; |
| 2097 | } |
| 2098 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2099 | { // InterlockedCompareExchange(dst, compare, value, result.old_value); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2100 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2101 | pre << "InterlockedCompareExchange"; |
| 2102 | { |
| 2103 | ScopedParen sp(pre); |
| 2104 | if (!EmitExpression(pre, dest)) { |
| 2105 | return false; |
| 2106 | } |
| 2107 | pre << ", " << compare << ", "; |
| 2108 | if (!EmitExpression(pre, value)) { |
| 2109 | return false; |
| 2110 | } |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2111 | pre << ", " << result << ".old_value"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2112 | } |
| 2113 | pre << ";"; |
| 2114 | } |
| 2115 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 2116 | // result.exchanged = result.old_value == compare; |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2117 | Line() << result << ".exchanged = " << result << ".old_value == " << compare << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2118 | |
| 2119 | out << result; |
| 2120 | return true; |
| 2121 | } |
| 2122 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2123 | case builtin::Function::kAtomicAdd: |
| 2124 | case builtin::Function::kAtomicSub: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2125 | return call("InterlockedAdd"); |
| 2126 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2127 | case builtin::Function::kAtomicMax: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2128 | return call("InterlockedMax"); |
| 2129 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2130 | case builtin::Function::kAtomicMin: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2131 | return call("InterlockedMin"); |
| 2132 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2133 | case builtin::Function::kAtomicAnd: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2134 | return call("InterlockedAnd"); |
| 2135 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2136 | case builtin::Function::kAtomicOr: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2137 | return call("InterlockedOr"); |
| 2138 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2139 | case builtin::Function::kAtomicXor: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2140 | return call("InterlockedXor"); |
| 2141 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2142 | case builtin::Function::kAtomicExchange: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2143 | return call("InterlockedExchange"); |
| 2144 | |
| 2145 | default: |
| 2146 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2149 | TINT_UNREACHABLE() << "unsupported atomic builtin: " << builtin->Type(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2150 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2153 | bool ASTPrinter::EmitSelectCall(StringStream& out, const ast::CallExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2154 | auto* expr_false = expr->args[0]; |
| 2155 | auto* expr_true = expr->args[1]; |
| 2156 | auto* expr_cond = expr->args[2]; |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 2157 | ScopedParen paren(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2158 | if (!EmitExpression(out, expr_cond)) { |
| 2159 | return false; |
| 2160 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2161 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2162 | out << " ? "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2163 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2164 | if (!EmitExpression(out, expr_true)) { |
| 2165 | return false; |
| 2166 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2167 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2168 | out << " : "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2169 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2170 | if (!EmitExpression(out, expr_false)) { |
| 2171 | return false; |
| 2172 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2173 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2174 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2177 | bool ASTPrinter::EmitModfCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2178 | const ast::CallExpression* expr, |
| 2179 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2180 | return CallBuiltinHelper( |
| 2181 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2182 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2183 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2184 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2185 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2186 | if (auto* vec = ty->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2187 | width = std::to_string(vec->Width()); |
| 2188 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2189 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2190 | // Emit the builtin return type unique to this overload. This does not |
| 2191 | // exist in the AST, so it will not be generated in Generate(). |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 2192 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2193 | return false; |
| 2194 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2195 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2196 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2197 | auto l = Line(b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2198 | if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2199 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2200 | return false; |
| 2201 | } |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2202 | l << " result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2203 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2204 | Line(b) << "result.fract = modf(" << params[0] << ", result.whole);"; |
| 2205 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2206 | return true; |
| 2207 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2210 | bool ASTPrinter::EmitFrexpCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2211 | const ast::CallExpression* expr, |
| 2212 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2213 | return CallBuiltinHelper( |
| 2214 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2215 | auto* ty = builtin->Parameters()[0]->Type(); |
| 2216 | auto in = params[0]; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2217 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2218 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2219 | if (auto* vec = ty->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2220 | width = std::to_string(vec->Width()); |
| 2221 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2222 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2223 | // Emit the builtin return type unique to this overload. This does not |
| 2224 | // exist in the AST, so it will not be generated in Generate(). |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 2225 | if (!EmitStructType(&helpers_, builtin->ReturnType()->As<type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2226 | return false; |
| 2227 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2228 | |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2229 | std::string member_type; |
Ben Clayton | 471a015 | 2023-06-09 10:07:16 +0000 | [diff] [blame] | 2230 | if (Is<type::F16>(ty->DeepestElement())) { |
Zhaoming Jiang | 20cddbf | 2022-08-05 15:11:44 +0000 | [diff] [blame] | 2231 | member_type = width.empty() ? "float16_t" : ("vector<float16_t, " + width + ">"); |
| 2232 | } else { |
| 2233 | member_type = "float" + width; |
| 2234 | } |
| 2235 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2236 | Line(b) << member_type << " exp;"; |
| 2237 | Line(b) << member_type << " fract = sign(" << in << ") * frexp(" << in << ", exp);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2238 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2239 | auto l = Line(b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2240 | if (!EmitType(l, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2241 | builtin::Access::kUndefined, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2242 | return false; |
| 2243 | } |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 2244 | l << " result = {fract, int" << width << "(exp)};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2245 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2246 | Line(b) << "return result;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2247 | return true; |
| 2248 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2251 | bool ASTPrinter::EmitDegreesCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2252 | const ast::CallExpression* expr, |
| 2253 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2254 | return CallBuiltinHelper(out, expr, builtin, |
| 2255 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2256 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2257 | << sem::kRadToDeg << ";"; |
| 2258 | return true; |
| 2259 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2262 | bool ASTPrinter::EmitRadiansCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2263 | const ast::CallExpression* expr, |
| 2264 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2265 | return CallBuiltinHelper(out, expr, builtin, |
| 2266 | [&](TextBuffer* b, const std::vector<std::string>& params) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2267 | Line(b) << "return " << params[0] << " * " << std::setprecision(20) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2268 | << sem::kDegToRad << ";"; |
| 2269 | return true; |
| 2270 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2273 | // The HLSL `sign` method always returns an `int` result (scalar or vector). In WGSL the result is |
| 2274 | // expected to be the same type as the argument. This injects a cast to the expected WGSL result |
| 2275 | // type after the call to `sign`. |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2276 | bool ASTPrinter::EmitSignCall(StringStream& out, const sem::Call* call, const sem::Builtin*) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2277 | auto* arg = call->Arguments()[0]; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 2278 | if (!EmitType(out, arg->Type(), builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 2279 | "")) { |
dan sinclair | 7092786 | 2023-01-11 13:18:29 +0000 | [diff] [blame] | 2280 | return false; |
| 2281 | } |
| 2282 | out << "(sign("; |
| 2283 | if (!EmitExpression(out, arg->Declaration())) { |
| 2284 | return false; |
| 2285 | } |
| 2286 | out << "))"; |
| 2287 | return true; |
| 2288 | } |
| 2289 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2290 | bool ASTPrinter::EmitQuantizeToF16Call(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2291 | const ast::CallExpression* expr, |
| 2292 | const sem::Builtin* builtin) { |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2293 | // Cast to f16 and back |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2294 | std::string width; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2295 | if (auto* vec = builtin->ReturnType()->As<type::Vector>()) { |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2296 | width = std::to_string(vec->Width()); |
| 2297 | } |
Antonio Maiorano | 51249b8 | 2023-03-31 08:00:33 +0000 | [diff] [blame] | 2298 | out << "f16tof32(f32tof16" |
| 2299 | << "("; |
Ben Clayton | 2bea905 | 2022-11-02 00:09:50 +0000 | [diff] [blame] | 2300 | if (!EmitExpression(out, expr->args[0])) { |
| 2301 | return false; |
| 2302 | } |
| 2303 | out << "))"; |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2307 | bool ASTPrinter::EmitTruncCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2308 | const ast::CallExpression* expr, |
| 2309 | const sem::Builtin* builtin) { |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2310 | // HLSL's trunc is broken for very large/small float values. |
| 2311 | // See crbug.com/tint/1883 |
| 2312 | return CallBuiltinHelper( // |
| 2313 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2314 | // value < 0 ? ceil(value) : floor(value) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2315 | Line(b) << "return " << params[0] << " < 0 ? ceil(" << params[0] << ") : floor(" |
Antonio Maiorano | 5cf943e | 2023-03-27 18:55:25 +0000 | [diff] [blame] | 2316 | << params[0] << ");"; |
| 2317 | return true; |
| 2318 | }); |
| 2319 | } |
| 2320 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2321 | bool ASTPrinter::EmitDataPackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2322 | const ast::CallExpression* expr, |
| 2323 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2324 | return CallBuiltinHelper( |
| 2325 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2326 | uint32_t dims = 2; |
| 2327 | bool is_signed = false; |
| 2328 | uint32_t scale = 65535; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2329 | if (builtin->Type() == builtin::Function::kPack4X8Snorm || |
| 2330 | builtin->Type() == builtin::Function::kPack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2331 | dims = 4; |
| 2332 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2333 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2334 | if (builtin->Type() == builtin::Function::kPack4X8Snorm || |
| 2335 | builtin->Type() == builtin::Function::kPack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2336 | is_signed = true; |
| 2337 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2338 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2339 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2340 | case builtin::Function::kPack4X8Snorm: |
| 2341 | case builtin::Function::kPack4X8Unorm: |
| 2342 | case builtin::Function::kPack2X16Snorm: |
| 2343 | case builtin::Function::kPack2X16Unorm: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2344 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2345 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2346 | l << (is_signed ? "" : "u") << "int" << dims |
| 2347 | << " i = " << (is_signed ? "" : "u") << "int" << dims << "(round(clamp(" |
| 2348 | << params[0] << ", " << (is_signed ? "-1.0" : "0.0") << ", 1.0) * " |
| 2349 | << scale << ".0))"; |
| 2350 | if (is_signed) { |
| 2351 | l << " & " << (dims == 4 ? "0xff" : "0xffff"); |
| 2352 | } |
| 2353 | l << ";"; |
| 2354 | } |
| 2355 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2356 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2357 | l << "return "; |
| 2358 | if (is_signed) { |
| 2359 | l << "asuint"; |
| 2360 | } |
| 2361 | l << "(i.x | i.y << " << (32 / dims); |
| 2362 | if (dims == 4) { |
| 2363 | l << " | i.z << 16 | i.w << 24"; |
| 2364 | } |
| 2365 | l << ");"; |
| 2366 | } |
| 2367 | break; |
| 2368 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2369 | case builtin::Function::kPack2X16Float: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2370 | Line(b) << "uint2 i = f32tof16(" << params[0] << ");"; |
| 2371 | Line(b) << "return i.x | (i.y << 16);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2372 | break; |
| 2373 | } |
| 2374 | default: |
| 2375 | diagnostics_.add_error(diag::System::Writer, |
| 2376 | "Internal error: unhandled data packing builtin"); |
| 2377 | return false; |
| 2378 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2379 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2380 | return true; |
| 2381 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2384 | bool ASTPrinter::EmitDataUnpackingCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2385 | const ast::CallExpression* expr, |
| 2386 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2387 | return CallBuiltinHelper( |
| 2388 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2389 | uint32_t dims = 2; |
| 2390 | bool is_signed = false; |
| 2391 | uint32_t scale = 65535; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2392 | if (builtin->Type() == builtin::Function::kUnpack4X8Snorm || |
| 2393 | builtin->Type() == builtin::Function::kUnpack4X8Unorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2394 | dims = 4; |
| 2395 | scale = 255; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2396 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2397 | if (builtin->Type() == builtin::Function::kUnpack4X8Snorm || |
| 2398 | builtin->Type() == builtin::Function::kUnpack2X16Snorm) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2399 | is_signed = true; |
| 2400 | scale = (scale - 1) / 2; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2401 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2402 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2403 | case builtin::Function::kUnpack4X8Snorm: |
| 2404 | case builtin::Function::kUnpack2X16Snorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2405 | Line(b) << "int j = int(" << params[0] << ");"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2406 | { // Perform sign extension on the converted values. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2407 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2408 | l << "int" << dims << " i = int" << dims << "("; |
| 2409 | if (dims == 2) { |
| 2410 | l << "j << 16, j) >> 16"; |
| 2411 | } else { |
| 2412 | l << "j << 24, j << 16, j << 8, j) >> 24"; |
| 2413 | } |
| 2414 | l << ";"; |
| 2415 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2416 | Line(b) << "return clamp(float" << dims << "(i) / " << scale << ".0, " |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2417 | << (is_signed ? "-1.0" : "0.0") << ", 1.0);"; |
| 2418 | break; |
| 2419 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2420 | case builtin::Function::kUnpack4X8Unorm: |
| 2421 | case builtin::Function::kUnpack2X16Unorm: { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2422 | Line(b) << "uint j = " << params[0] << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2423 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2424 | auto l = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2425 | l << "uint" << dims << " i = uint" << dims << "("; |
| 2426 | l << "j & " << (dims == 2 ? "0xffff" : "0xff") << ", "; |
| 2427 | if (dims == 4) { |
| 2428 | l << "(j >> " << (32 / dims) << ") & 0xff, (j >> 16) & 0xff, j >> 24"; |
| 2429 | } else { |
| 2430 | l << "j >> " << (32 / dims); |
| 2431 | } |
| 2432 | l << ");"; |
| 2433 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2434 | Line(b) << "return float" << dims << "(i) / " << scale << ".0;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2435 | break; |
| 2436 | } |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2437 | case builtin::Function::kUnpack2X16Float: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2438 | Line(b) << "uint i = " << params[0] << ";"; |
| 2439 | Line(b) << "return f16tof32(uint2(i & 0xffff, i >> 16));"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | default: |
| 2442 | diagnostics_.add_error(diag::System::Writer, |
| 2443 | "Internal error: unhandled data packing builtin"); |
| 2444 | return false; |
| 2445 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2446 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2447 | return true; |
| 2448 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2451 | bool ASTPrinter::EmitDP4aCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2452 | const ast::CallExpression* expr, |
| 2453 | const sem::Builtin* builtin) { |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2454 | // TODO(crbug.com/tint/1497): support the polyfill version of DP4a functions. |
| 2455 | return CallBuiltinHelper( |
| 2456 | out, expr, builtin, [&](TextBuffer* b, const std::vector<std::string>& params) { |
| 2457 | std::string functionName; |
| 2458 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2459 | case builtin::Function::kDot4I8Packed: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2460 | Line(b) << "int accumulator = 0;"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2461 | functionName = "dot4add_i8packed"; |
| 2462 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2463 | case builtin::Function::kDot4U8Packed: |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2464 | Line(b) << "uint accumulator = 0u;"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2465 | functionName = "dot4add_u8packed"; |
| 2466 | break; |
| 2467 | default: |
| 2468 | diagnostics_.add_error(diag::System::Writer, |
| 2469 | "Internal error: unhandled DP4a builtin"); |
| 2470 | return false; |
| 2471 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2472 | Line(b) << "return " << functionName << "(" << params[0] << ", " << params[1] |
Jiawei Shao | 1c75921 | 2022-05-15 13:53:21 +0000 | [diff] [blame] | 2473 | << ", accumulator);"; |
Jiawei Shao | ab97570 | 2022-05-13 00:09:56 +0000 | [diff] [blame] | 2474 | |
| 2475 | return true; |
| 2476 | }); |
| 2477 | } |
| 2478 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2479 | bool ASTPrinter::EmitBarrierCall(StringStream& out, const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2480 | // TODO(crbug.com/tint/661): Combine sequential barriers to a single |
| 2481 | // instruction. |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2482 | if (builtin->Type() == builtin::Function::kWorkgroupBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2483 | out << "GroupMemoryBarrierWithGroupSync()"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2484 | } else if (builtin->Type() == builtin::Function::kStorageBarrier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2485 | out << "DeviceMemoryBarrierWithGroupSync()"; |
| 2486 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2487 | TINT_UNREACHABLE() << "unexpected barrier builtin type " << builtin::str(builtin->Type()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2488 | return false; |
| 2489 | } |
| 2490 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2493 | bool ASTPrinter::EmitTextureCall(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2494 | const sem::Call* call, |
| 2495 | const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2496 | using Usage = sem::ParameterUsage; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2497 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2498 | auto& signature = builtin->Signature(); |
| 2499 | auto* expr = call->Declaration(); |
| 2500 | auto arguments = expr->args; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2501 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2502 | // Returns the argument with the given usage |
| 2503 | auto arg = [&](Usage usage) { |
| 2504 | int idx = signature.IndexOf(usage); |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 2505 | return (idx >= 0) ? arguments[static_cast<size_t>(idx)] : nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2506 | }; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2507 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2508 | auto* texture = arg(Usage::kTexture); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2509 | if (TINT_UNLIKELY(!texture)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2510 | TINT_ICE() << "missing texture argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2513 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2514 | auto* texture_type = TypeOf(texture)->UnwrapRef()->As<type::Texture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2515 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2516 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2517 | case builtin::Function::kTextureDimensions: |
| 2518 | case builtin::Function::kTextureNumLayers: |
| 2519 | case builtin::Function::kTextureNumLevels: |
| 2520 | case builtin::Function::kTextureNumSamples: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2521 | // All of these builtins use the GetDimensions() method on the texture |
| 2522 | bool is_ms = |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2523 | texture_type->IsAnyOf<type::MultisampledTexture, type::DepthMultisampledTexture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2524 | int num_dimensions = 0; |
| 2525 | std::string swizzle; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2526 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2527 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2528 | case builtin::Function::kTextureDimensions: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2529 | switch (texture_type->dim()) { |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2530 | case type::TextureDimension::kNone: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2531 | TINT_ICE() << "texture dimension is kNone"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2532 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2533 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2534 | num_dimensions = 1; |
| 2535 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2536 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2537 | num_dimensions = is_ms ? 3 : 2; |
| 2538 | swizzle = is_ms ? ".xy" : ""; |
| 2539 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2540 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2541 | num_dimensions = is_ms ? 4 : 3; |
| 2542 | swizzle = ".xy"; |
| 2543 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2544 | case type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2545 | num_dimensions = 3; |
| 2546 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2547 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2548 | num_dimensions = 2; |
| 2549 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2550 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2551 | num_dimensions = 3; |
| 2552 | swizzle = ".xy"; |
| 2553 | break; |
| 2554 | } |
| 2555 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2556 | case builtin::Function::kTextureNumLayers: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2557 | switch (texture_type->dim()) { |
| 2558 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2559 | TINT_ICE() << "texture dimension is not arrayed"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2560 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2561 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2562 | num_dimensions = is_ms ? 4 : 3; |
| 2563 | swizzle = ".z"; |
| 2564 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2565 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2566 | num_dimensions = 3; |
| 2567 | swizzle = ".z"; |
| 2568 | break; |
| 2569 | } |
| 2570 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2571 | case builtin::Function::kTextureNumLevels: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2572 | switch (texture_type->dim()) { |
| 2573 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2574 | TINT_ICE() << "texture dimension does not support mips"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2575 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2576 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2577 | num_dimensions = 2; |
| 2578 | swizzle = ".y"; |
| 2579 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2580 | case type::TextureDimension::k2d: |
| 2581 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2582 | num_dimensions = 3; |
| 2583 | swizzle = ".z"; |
| 2584 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2585 | case type::TextureDimension::k2dArray: |
| 2586 | case type::TextureDimension::k3d: |
| 2587 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2588 | num_dimensions = 4; |
| 2589 | swizzle = ".w"; |
| 2590 | break; |
| 2591 | } |
| 2592 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2593 | case builtin::Function::kTextureNumSamples: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2594 | switch (texture_type->dim()) { |
| 2595 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2596 | TINT_ICE() << "texture dimension does not support multisampling"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2597 | return false; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2598 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2599 | num_dimensions = 3; |
| 2600 | swizzle = ".z"; |
| 2601 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 2602 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2603 | num_dimensions = 4; |
| 2604 | swizzle = ".w"; |
| 2605 | break; |
| 2606 | } |
| 2607 | break; |
| 2608 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2609 | TINT_ICE() << "unexpected builtin"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2610 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2611 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2612 | |
| 2613 | auto* level_arg = arg(Usage::kLevel); |
| 2614 | |
| 2615 | if (level_arg) { |
| 2616 | // `NumberOfLevels` is a non-optional argument if `MipLevel` was passed. |
| 2617 | // Increment the number of dimensions for the temporary vector to |
| 2618 | // accommodate this. |
| 2619 | num_dimensions++; |
| 2620 | |
| 2621 | // If the swizzle was empty, the expression will evaluate to the whole |
| 2622 | // vector. As we've grown the vector by one element, we now need to |
| 2623 | // swizzle to keep the result expression equivalent. |
| 2624 | if (swizzle.empty()) { |
| 2625 | static constexpr const char* swizzles[] = {"", ".x", ".xy", ".xyz"}; |
| 2626 | swizzle = swizzles[num_dimensions - 1]; |
| 2627 | } |
| 2628 | } |
| 2629 | |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2630 | if (TINT_UNLIKELY(num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2631 | TINT_ICE() << "Texture query builtin temporary vector has " << num_dimensions |
| 2632 | << " dimensions"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2633 | return false; |
| 2634 | } |
| 2635 | |
| 2636 | // Declare a variable to hold the queried texture info |
| 2637 | auto dims = UniqueIdentifier(kTempNamePrefix); |
| 2638 | if (num_dimensions == 1) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2639 | Line() << "uint " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2640 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2641 | Line() << "uint" << num_dimensions << " " << dims << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | { // texture.GetDimensions(...) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2645 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2646 | if (!EmitExpression(pre, texture)) { |
| 2647 | return false; |
| 2648 | } |
| 2649 | pre << ".GetDimensions("; |
| 2650 | |
| 2651 | if (level_arg) { |
| 2652 | if (!EmitExpression(pre, level_arg)) { |
| 2653 | return false; |
| 2654 | } |
| 2655 | pre << ", "; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2656 | } else if (builtin->Type() == builtin::Function::kTextureNumLevels) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2657 | pre << "0, "; |
| 2658 | } |
| 2659 | |
| 2660 | if (num_dimensions == 1) { |
| 2661 | pre << dims; |
| 2662 | } else { |
| 2663 | static constexpr char xyzw[] = {'x', 'y', 'z', 'w'}; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2664 | if (TINT_UNLIKELY(num_dimensions < 0 || num_dimensions > 4)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2665 | TINT_ICE() << "vector dimensions are " << num_dimensions; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2666 | return false; |
| 2667 | } |
| 2668 | for (int i = 0; i < num_dimensions; i++) { |
| 2669 | if (i > 0) { |
| 2670 | pre << ", "; |
| 2671 | } |
| 2672 | pre << dims << "." << xyzw[i]; |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | pre << ");"; |
| 2677 | } |
| 2678 | |
| 2679 | // The out parameters of the GetDimensions() call is now in temporary |
| 2680 | // `dims` variable. This may be packed with other data, so the final |
| 2681 | // expression may require a swizzle. |
| 2682 | out << dims << swizzle; |
| 2683 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2684 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2685 | default: |
| 2686 | break; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2687 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2688 | |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2689 | if (!EmitExpression(out, texture)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2690 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2691 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2692 | |
| 2693 | // If pack_level_in_coords is true, then the mip level will be appended as the |
| 2694 | // last value of the coordinates argument. If the WGSL builtin overload does |
| 2695 | // not have a level parameter and pack_level_in_coords is true, then a zero |
| 2696 | // mip level will be inserted. |
| 2697 | bool pack_level_in_coords = false; |
| 2698 | |
| 2699 | uint32_t hlsl_ret_width = 4u; |
| 2700 | |
| 2701 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2702 | case builtin::Function::kTextureSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2703 | out << ".Sample("; |
| 2704 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2705 | case builtin::Function::kTextureSampleBias: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2706 | out << ".SampleBias("; |
| 2707 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2708 | case builtin::Function::kTextureSampleLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2709 | out << ".SampleLevel("; |
| 2710 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2711 | case builtin::Function::kTextureSampleGrad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2712 | out << ".SampleGrad("; |
| 2713 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2714 | case builtin::Function::kTextureSampleCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2715 | out << ".SampleCmp("; |
| 2716 | hlsl_ret_width = 1; |
| 2717 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2718 | case builtin::Function::kTextureSampleCompareLevel: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2719 | out << ".SampleCmpLevelZero("; |
| 2720 | hlsl_ret_width = 1; |
| 2721 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2722 | case builtin::Function::kTextureLoad: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2723 | out << ".Load("; |
| 2724 | // Multisampled textures do not support mip-levels. |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 2725 | if (!texture_type->Is<type::MultisampledTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2726 | pack_level_in_coords = true; |
| 2727 | } |
| 2728 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2729 | case builtin::Function::kTextureGather: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2730 | out << ".Gather"; |
| 2731 | if (builtin->Parameters()[0]->Usage() == sem::ParameterUsage::kComponent) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 2732 | switch (call->Arguments()[0]->ConstantValue()->ValueAs<AInt>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2733 | case 0: |
| 2734 | out << "Red"; |
| 2735 | break; |
| 2736 | case 1: |
| 2737 | out << "Green"; |
| 2738 | break; |
| 2739 | case 2: |
| 2740 | out << "Blue"; |
| 2741 | break; |
| 2742 | case 3: |
| 2743 | out << "Alpha"; |
| 2744 | break; |
| 2745 | } |
| 2746 | } |
| 2747 | out << "("; |
| 2748 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2749 | case builtin::Function::kTextureGatherCompare: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2750 | out << ".GatherCmp("; |
| 2751 | break; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2752 | case builtin::Function::kTextureStore: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2753 | out << "["; |
| 2754 | break; |
| 2755 | default: |
| 2756 | diagnostics_.add_error(diag::System::Writer, |
| 2757 | "Internal compiler error: Unhandled texture builtin '" + |
| 2758 | std::string(builtin->str()) + "'"); |
| 2759 | return false; |
| 2760 | } |
| 2761 | |
| 2762 | if (auto* sampler = arg(Usage::kSampler)) { |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2763 | if (!EmitExpression(out, sampler)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2764 | return false; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 2765 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2766 | out << ", "; |
| 2767 | } |
| 2768 | |
| 2769 | auto* param_coords = arg(Usage::kCoords); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2770 | if (TINT_UNLIKELY(!param_coords)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2771 | TINT_ICE() << "missing coords argument"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2772 | return false; |
| 2773 | } |
| 2774 | |
| 2775 | auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 2776 | auto* i32 = builder_.create<type::I32>(); |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 2777 | auto* zero = builder_.Expr(0_i); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2778 | auto* stmt = builder_.Sem().Get(vector)->Stmt(); |
Ben Clayton | 3fb9a3f | 2023-02-04 21:20:26 +0000 | [diff] [blame] | 2779 | builder_.Sem().Add(zero, builder_.create<sem::ValueExpression>( |
| 2780 | zero, i32, sem::EvaluationStage::kRuntime, stmt, |
| 2781 | /* constant_value */ nullptr, |
| 2782 | /* has_side_effects */ false)); |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2783 | auto* packed = tint::writer::AppendVector(&builder_, vector, zero); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2784 | return EmitExpression(out, packed->Declaration()); |
| 2785 | }; |
| 2786 | |
| 2787 | auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { |
| 2788 | if (auto* level = arg(Usage::kLevel)) { |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2789 | auto* packed = tint::writer::AppendVector(&builder_, vector, level); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2790 | return EmitExpression(out, packed->Declaration()); |
| 2791 | } |
| 2792 | return emit_vector_appended_with_i32_zero(vector); |
| 2793 | }; |
| 2794 | |
| 2795 | if (auto* array_index = arg(Usage::kArrayIndex)) { |
| 2796 | // Array index needs to be appended to the coordinates. |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2797 | auto* packed = tint::writer::AppendVector(&builder_, param_coords, array_index); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2798 | if (pack_level_in_coords) { |
| 2799 | // Then mip level needs to be appended to the coordinates. |
| 2800 | if (!emit_vector_appended_with_level(packed->Declaration())) { |
| 2801 | return false; |
| 2802 | } |
| 2803 | } else { |
| 2804 | if (!EmitExpression(out, packed->Declaration())) { |
| 2805 | return false; |
| 2806 | } |
| 2807 | } |
| 2808 | } else if (pack_level_in_coords) { |
| 2809 | // Mip level needs to be appended to the coordinates. |
| 2810 | if (!emit_vector_appended_with_level(param_coords)) { |
| 2811 | return false; |
| 2812 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2813 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2814 | if (!EmitExpression(out, param_coords)) { |
| 2815 | return false; |
| 2816 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2817 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2818 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2819 | for (auto usage : {Usage::kDepthRef, Usage::kBias, Usage::kLevel, Usage::kDdx, Usage::kDdy, |
| 2820 | Usage::kSampleIndex, Usage::kOffset}) { |
| 2821 | if (usage == Usage::kLevel && pack_level_in_coords) { |
| 2822 | continue; // mip level already packed in coordinates. |
| 2823 | } |
| 2824 | if (auto* e = arg(usage)) { |
| 2825 | out << ", "; |
| 2826 | if (!EmitExpression(out, e)) { |
| 2827 | return false; |
| 2828 | } |
| 2829 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2830 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2831 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2832 | if (builtin->Type() == builtin::Function::kTextureStore) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2833 | out << "] = "; |
| 2834 | if (!EmitExpression(out, arg(Usage::kValue))) { |
| 2835 | return false; |
| 2836 | } |
| 2837 | } else { |
| 2838 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2839 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2840 | // If the builtin return type does not match the number of elements of the |
| 2841 | // HLSL builtin, we need to swizzle the expression to generate the correct |
| 2842 | // number of components. |
| 2843 | uint32_t wgsl_ret_width = 1; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 2844 | if (auto* vec = builtin->ReturnType()->As<type::Vector>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2845 | wgsl_ret_width = vec->Width(); |
| 2846 | } |
| 2847 | if (wgsl_ret_width < hlsl_ret_width) { |
| 2848 | out << "."; |
| 2849 | for (uint32_t i = 0; i < wgsl_ret_width; i++) { |
| 2850 | out << "xyz"[i]; |
| 2851 | } |
| 2852 | } |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 2853 | if (TINT_UNLIKELY(wgsl_ret_width > hlsl_ret_width)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 2854 | TINT_ICE() << "WGSL return width (" << wgsl_ret_width |
| 2855 | << ") is wider than HLSL return width (" << hlsl_ret_width << ") for " |
| 2856 | << builtin->Type(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2857 | return false; |
| 2858 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2859 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2860 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2861 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2864 | std::string ASTPrinter::generate_builtin_name(const sem::Builtin* builtin) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2865 | switch (builtin->Type()) { |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2866 | case builtin::Function::kAbs: |
| 2867 | case builtin::Function::kAcos: |
| 2868 | case builtin::Function::kAll: |
| 2869 | case builtin::Function::kAny: |
| 2870 | case builtin::Function::kAsin: |
| 2871 | case builtin::Function::kAtan: |
| 2872 | case builtin::Function::kAtan2: |
| 2873 | case builtin::Function::kCeil: |
| 2874 | case builtin::Function::kClamp: |
| 2875 | case builtin::Function::kCos: |
| 2876 | case builtin::Function::kCosh: |
| 2877 | case builtin::Function::kCross: |
| 2878 | case builtin::Function::kDeterminant: |
| 2879 | case builtin::Function::kDistance: |
| 2880 | case builtin::Function::kDot: |
| 2881 | case builtin::Function::kExp: |
| 2882 | case builtin::Function::kExp2: |
| 2883 | case builtin::Function::kFloor: |
| 2884 | case builtin::Function::kFrexp: |
| 2885 | case builtin::Function::kLdexp: |
| 2886 | case builtin::Function::kLength: |
| 2887 | case builtin::Function::kLog: |
| 2888 | case builtin::Function::kLog2: |
| 2889 | case builtin::Function::kMax: |
| 2890 | case builtin::Function::kMin: |
| 2891 | case builtin::Function::kModf: |
| 2892 | case builtin::Function::kNormalize: |
| 2893 | case builtin::Function::kPow: |
| 2894 | case builtin::Function::kReflect: |
| 2895 | case builtin::Function::kRefract: |
| 2896 | case builtin::Function::kRound: |
| 2897 | case builtin::Function::kSaturate: |
| 2898 | case builtin::Function::kSin: |
| 2899 | case builtin::Function::kSinh: |
| 2900 | case builtin::Function::kSqrt: |
| 2901 | case builtin::Function::kStep: |
| 2902 | case builtin::Function::kTan: |
| 2903 | case builtin::Function::kTanh: |
| 2904 | case builtin::Function::kTranspose: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2905 | return builtin->str(); |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2906 | case builtin::Function::kCountOneBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2907 | return "countbits"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2908 | case builtin::Function::kDpdx: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2909 | return "ddx"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2910 | case builtin::Function::kDpdxCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2911 | return "ddx_coarse"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2912 | case builtin::Function::kDpdxFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2913 | return "ddx_fine"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2914 | case builtin::Function::kDpdy: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2915 | return "ddy"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2916 | case builtin::Function::kDpdyCoarse: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2917 | return "ddy_coarse"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2918 | case builtin::Function::kDpdyFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2919 | return "ddy_fine"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2920 | case builtin::Function::kFaceForward: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2921 | return "faceforward"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2922 | case builtin::Function::kFract: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2923 | return "frac"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2924 | case builtin::Function::kFma: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2925 | return "mad"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2926 | case builtin::Function::kFwidth: |
| 2927 | case builtin::Function::kFwidthCoarse: |
| 2928 | case builtin::Function::kFwidthFine: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2929 | return "fwidth"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2930 | case builtin::Function::kInverseSqrt: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2931 | return "rsqrt"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2932 | case builtin::Function::kMix: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2933 | return "lerp"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2934 | case builtin::Function::kReverseBits: // uint |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2935 | return "reversebits"; |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 2936 | case builtin::Function::kSmoothstep: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2937 | return "smoothstep"; |
| 2938 | default: |
| 2939 | diagnostics_.add_error(diag::System::Writer, |
| 2940 | "Unknown builtin method: " + std::string(builtin->str())); |
| 2941 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2942 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2943 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2946 | bool ASTPrinter::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2947 | auto* stmt = s->body[case_idx]; |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 2948 | auto* sem = builder_.Sem().Get<sem::CaseStatement>(stmt); |
| 2949 | for (auto* selector : sem->Selectors()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2950 | auto out = Line(); |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 2951 | if (selector->IsDefault()) { |
| 2952 | out << "default"; |
| 2953 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2954 | out << "case "; |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 2955 | if (!EmitConstant(out, selector->Value(), /* is_variable_initializer */ false)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2956 | return false; |
| 2957 | } |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 2958 | } |
| 2959 | out << ":"; |
| 2960 | if (selector == sem->Selectors().back()) { |
| 2961 | out << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2965 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2966 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2967 | DecrementIndent(); |
| 2968 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2969 | }); |
| 2970 | |
| 2971 | // Emit the case statement |
| 2972 | if (!EmitStatements(stmt->body->statements)) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2973 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2974 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2975 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2976 | if (!tint::IsAnyOf<ast::BreakStatement>(stmt->body->Last())) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2977 | Line() << "break;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2978 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2979 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2980 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2983 | bool ASTPrinter::EmitContinue(const ast::ContinueStatement*) { |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 2984 | if (!emit_continuing_ || !emit_continuing_()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2985 | return false; |
| 2986 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2987 | Line() << "continue;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2988 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 2991 | bool ASTPrinter::EmitDiscard(const ast::DiscardStatement*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2992 | // TODO(dsinclair): Verify this is correct when the discard semantics are |
| 2993 | // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 2994 | Line() << "discard;"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 2995 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2998 | bool ASTPrinter::EmitExpression(StringStream& out, const ast::Expression* expr) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 2999 | if (auto* sem = builder_.Sem().GetVal(expr)) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3000 | if (auto* constant = sem->ConstantValue()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3001 | bool is_variable_initializer = false; |
| 3002 | if (auto* stmt = sem->Stmt()) { |
| 3003 | if (auto* decl = As<ast::VariableDeclStatement>(stmt->Declaration())) { |
| 3004 | is_variable_initializer = decl->variable->initializer == expr; |
| 3005 | } |
| 3006 | } |
| 3007 | return EmitConstant(out, constant, is_variable_initializer); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3010 | return Switch( |
Ben Clayton | b90b6bf | 2022-08-23 16:23:05 +0000 | [diff] [blame] | 3011 | expr, // |
| 3012 | [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, |
| 3013 | [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, |
| 3014 | [&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); }, |
| 3015 | [&](const ast::CallExpression* c) { return EmitCall(out, c); }, |
| 3016 | [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, |
| 3017 | [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, |
| 3018 | [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, |
| 3019 | [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, |
| 3020 | [&](Default) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3021 | diagnostics_.add_error(diag::System::Writer, "unknown expression type: " + |
| 3022 | std::string(expr->TypeInfo().name)); |
| 3023 | return false; |
| 3024 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3027 | bool ASTPrinter::EmitIdentifier(StringStream& out, const ast::IdentifierExpression* expr) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3028 | out << expr->identifier->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3029 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3032 | bool ASTPrinter::EmitIf(const ast::IfStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3033 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3034 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3035 | out << "if ("; |
| 3036 | if (!EmitExpression(out, stmt->condition)) { |
| 3037 | return false; |
| 3038 | } |
| 3039 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3040 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3041 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3042 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
James Price | 26ebe5e | 2022-04-29 00:14:53 +0000 | [diff] [blame] | 3043 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3044 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3045 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3046 | if (stmt->else_statement) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3047 | Line() << "} else {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3048 | if (auto* block = stmt->else_statement->As<ast::BlockStatement>()) { |
| 3049 | if (!EmitStatementsWithIndent(block->statements)) { |
| 3050 | return false; |
| 3051 | } |
| 3052 | } else { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3053 | if (!EmitStatementsWithIndent(Vector{stmt->else_statement})) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3054 | return false; |
| 3055 | } |
| 3056 | } |
| 3057 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3058 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3059 | |
| 3060 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3063 | bool ASTPrinter::EmitFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3064 | auto* sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3065 | |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3066 | // Emit storage atomic helpers |
| 3067 | if (auto* intrinsic = |
James Price | b4acbb8 | 2023-05-11 21:27:16 +0000 | [diff] [blame] | 3068 | ast::GetAttribute<ast::transform::DecomposeMemoryAccess::Intrinsic>(func->attributes)) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3069 | if (intrinsic->address_space == builtin::AddressSpace::kStorage && intrinsic->IsAtomic()) { |
Antonio Maiorano | 08f4b55 | 2022-05-31 13:20:28 +0000 | [diff] [blame] | 3070 | if (!EmitStorageAtomicIntrinsic(func, intrinsic)) { |
| 3071 | return false; |
| 3072 | } |
| 3073 | } |
| 3074 | return true; |
| 3075 | } |
| 3076 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3077 | if (ast::HasAttribute<ast::InternalAttribute>(func->attributes)) { |
| 3078 | // An internal function. Do not emit. |
| 3079 | return true; |
| 3080 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3081 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3082 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3083 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3084 | auto name = func->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3085 | // If the function returns an array, then we need to declare a typedef for |
| 3086 | // this. |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3087 | if (sem->ReturnType()->Is<type::Array>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3088 | auto typedef_name = UniqueIdentifier(name + "_ret"); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3089 | auto pre = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3090 | pre << "typedef "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3091 | if (!EmitTypeAndName(pre, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3092 | builtin::Access::kReadWrite, typedef_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3093 | return false; |
| 3094 | } |
| 3095 | pre << ";"; |
| 3096 | out << typedef_name; |
| 3097 | } else { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3098 | if (!EmitType(out, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3099 | builtin::Access::kReadWrite, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3100 | return false; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | out << " " << name << "("; |
| 3105 | |
| 3106 | bool first = true; |
| 3107 | |
| 3108 | for (auto* v : sem->Parameters()) { |
| 3109 | if (!first) { |
| 3110 | out << ", "; |
| 3111 | } |
| 3112 | first = false; |
| 3113 | |
| 3114 | auto const* type = v->Type(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3115 | auto address_space = builtin::AddressSpace::kUndefined; |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3116 | auto access = builtin::Access::kUndefined; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3117 | |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 3118 | if (auto* ptr = type->As<type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3119 | type = ptr->StoreType(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3120 | switch (ptr->AddressSpace()) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3121 | case builtin::AddressSpace::kStorage: |
| 3122 | case builtin::AddressSpace::kUniform: |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3123 | // Not allowed by WGSL, but is used by certain transforms (e.g. DMA) to pass |
| 3124 | // storage buffers and uniform buffers down into transform-generated |
| 3125 | // functions. In this situation we want to generate the parameter without an |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3126 | // 'inout', using the address space and access from the pointer. |
| 3127 | address_space = ptr->AddressSpace(); |
Ben Clayton | 2032d03 | 2022-06-15 19:32:37 +0000 | [diff] [blame] | 3128 | access = ptr->Access(); |
| 3129 | break; |
| 3130 | default: |
| 3131 | // Transform regular WGSL pointer parameters in to `inout` parameters. |
| 3132 | out << "inout "; |
| 3133 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3136 | // Note: WGSL only allows for AddressSpace::kUndefined on parameters, however |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3137 | // the sanitizer transforms generates load / store functions for storage |
| 3138 | // or uniform buffers. These functions have a buffer parameter with |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3139 | // AddressSpace::kStorage or AddressSpace::kUniform. This is required to |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3140 | // correctly translate the parameter to a [RW]ByteAddressBuffer for |
| 3141 | // storage buffers and a uint4[N] for uniform buffers. |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3142 | if (!EmitTypeAndName(out, type, address_space, access, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3143 | v->Declaration()->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3144 | return false; |
| 3145 | } |
| 3146 | } |
| 3147 | out << ") {"; |
| 3148 | } |
| 3149 | |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3150 | if (sem->DiscardStatement() && !sem->ReturnType()->Is<type::Void>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3151 | // BUG(crbug.com/tint/1081): work around non-void functions with discard |
| 3152 | // failing compilation sometimes |
| 3153 | if (!EmitFunctionBodyWithDiscard(func)) { |
| 3154 | return false; |
| 3155 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3156 | } else { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3157 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3158 | return false; |
| 3159 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3162 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3163 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3164 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3167 | bool ASTPrinter::EmitFunctionBodyWithDiscard(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3168 | // FXC sometimes fails to compile functions that discard with 'Not all control |
| 3169 | // paths return a value'. We work around this by wrapping the function body |
| 3170 | // within an "if (true) { <body> } return <default return type obj>;" so that |
| 3171 | // there is always an (unused) return statement. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3172 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3173 | auto* sem = builder_.Sem().Get(func); |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3174 | TINT_ASSERT(sem->DiscardStatement() && !sem->ReturnType()->Is<type::Void>()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3175 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3176 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3177 | Line() << "if (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3178 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3179 | if (!EmitStatementsWithIndent(func->body->statements)) { |
| 3180 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3181 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3182 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3183 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3184 | |
| 3185 | // Return an unused result that matches the type of the return value |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3186 | auto name = builder_.Symbols().New("unused").Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3187 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3188 | auto out = Line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3189 | if (!EmitTypeAndName(out, sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3190 | builtin::Access::kReadWrite, name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3191 | return false; |
| 3192 | } |
| 3193 | out << ";"; |
| 3194 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3195 | Line() << "return " << name << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3196 | |
| 3197 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3200 | bool ASTPrinter::EmitGlobalVariable(const ast::Variable* global) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3201 | return Switch( |
| 3202 | global, // |
| 3203 | [&](const ast::Var* var) { |
| 3204 | auto* sem = builder_.Sem().Get(global); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3205 | switch (sem->AddressSpace()) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3206 | case builtin::AddressSpace::kUniform: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3207 | return EmitUniformVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3208 | case builtin::AddressSpace::kStorage: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3209 | return EmitStorageVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3210 | case builtin::AddressSpace::kHandle: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3211 | return EmitHandleVariable(var, sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3212 | case builtin::AddressSpace::kPrivate: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3213 | return EmitPrivateVariable(sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3214 | case builtin::AddressSpace::kWorkgroup: |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3215 | return EmitWorkgroupVariable(sem); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3216 | case builtin::AddressSpace::kPushConstant: |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3217 | diagnostics_.add_error( |
| 3218 | diag::System::Writer, |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3219 | "unhandled address space " + tint::ToString(sem->AddressSpace())); |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3220 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3221 | default: { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3222 | TINT_ICE() << "unhandled address space " << sem->AddressSpace(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3223 | return false; |
dan sinclair | 8dbd4d0 | 2022-07-27 18:54:05 +0000 | [diff] [blame] | 3224 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3225 | } |
| 3226 | }, |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3227 | [&](const ast::Override*) { |
| 3228 | // Override is removed with SubstituteOverride |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3229 | diagnostics_.add_error(diag::System::Writer, |
Ben Clayton | f10a579 | 2022-10-13 13:47:39 +0000 | [diff] [blame] | 3230 | "override-expressions should have been removed with the " |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3231 | "SubstituteOverride transform"); |
dan sinclair | f6a9404 | 2022-09-09 16:16:19 +0000 | [diff] [blame] | 3232 | return false; |
| 3233 | }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3234 | [&](const ast::Const*) { |
| 3235 | return true; // Constants are embedded at their use |
| 3236 | }, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3237 | [&](Default) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3238 | TINT_ICE() << "unhandled global variable type " << global->TypeInfo().name; |
dan sinclair | 4abf28e | 2022-08-02 15:55:35 +0000 | [diff] [blame] | 3239 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3240 | return false; |
| 3241 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3244 | bool ASTPrinter::EmitUniformVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3245 | auto binding_point = *sem->As<sem::GlobalVariable>()->BindingPoint(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3246 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3247 | auto name = var->name->symbol.Name(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3248 | Line() << "cbuffer cbuffer_" << name << RegisterAndSpace('b', binding_point) << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3249 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3250 | { |
| 3251 | ScopedIndent si(this); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3252 | auto out = Line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3253 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kUniform, sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3254 | return false; |
| 3255 | } |
| 3256 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3257 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3258 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3259 | Line() << "};"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3260 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3261 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3264 | bool ASTPrinter::EmitStorageVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3265 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3266 | auto out = Line(); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3267 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kStorage, sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3268 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3269 | return false; |
| 3270 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3271 | |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3272 | auto* global_sem = sem->As<sem::GlobalVariable>(); |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3273 | out << RegisterAndSpace(sem->Access() == builtin::Access::kRead ? 't' : 'u', |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3274 | *global_sem->BindingPoint()) |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3275 | << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3276 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3277 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3280 | bool ASTPrinter::EmitHandleVariable(const ast::Var* var, const sem::Variable* sem) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3281 | auto* unwrapped_type = sem->Type()->UnwrapRef(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3282 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3283 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3284 | auto name = var->name->symbol.Name(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 3285 | auto* type = sem->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3286 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3287 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3288 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3289 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3290 | const char* register_space = nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3291 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 3292 | if (unwrapped_type->Is<type::Texture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3293 | register_space = "t"; |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 3294 | if (unwrapped_type->Is<type::StorageTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3295 | register_space = "u"; |
| 3296 | } |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 3297 | } else if (unwrapped_type->Is<type::Sampler>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3298 | register_space = "s"; |
| 3299 | } |
| 3300 | |
| 3301 | if (register_space) { |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 3302 | auto bp = sem->As<sem::GlobalVariable>()->BindingPoint(); |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3303 | out << " : register(" << register_space << bp->binding; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3304 | // Omit the space if it's 0, as it's the default. |
| 3305 | // SM 5.0 doesn't support spaces, so we don't emit them if group is 0 for better |
| 3306 | // compatibility. |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3307 | if (bp->group == 0) { |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3308 | out << ")"; |
| 3309 | } else { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 3310 | out << ", space" << bp->group << ")"; |
Peng Huang | c00ff7f | 2023-03-31 17:55:19 +0000 | [diff] [blame] | 3311 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | out << ";"; |
| 3315 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3318 | bool ASTPrinter::EmitPrivateVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3319 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3320 | auto out = Line(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3321 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3322 | out << "static "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3323 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3324 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3325 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3326 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3327 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3328 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3329 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3330 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3331 | if (auto* initializer = decl->initializer) { |
| 3332 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3333 | return false; |
| 3334 | } |
| 3335 | } else { |
| 3336 | if (!EmitZeroValue(out, var->Type()->UnwrapRef())) { |
| 3337 | return false; |
| 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | out << ";"; |
| 3342 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3345 | bool ASTPrinter::EmitWorkgroupVariable(const sem::Variable* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3346 | auto* decl = var->Declaration(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3347 | auto out = Line(); |
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 | out << "groupshared "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3350 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3351 | auto name = decl->name->symbol.Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3352 | auto* type = var->Type()->UnwrapRef(); |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3353 | if (!EmitTypeAndName(out, type, var->AddressSpace(), var->Access(), name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3354 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3355 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3356 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3357 | if (auto* initializer = decl->initializer) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3358 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 3359 | if (!EmitExpression(out, initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3360 | return false; |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | out << ";"; |
| 3365 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3368 | std::string ASTPrinter::builtin_to_attribute(builtin::BuiltinValue builtin) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3369 | switch (builtin) { |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3370 | case builtin::BuiltinValue::kPosition: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3371 | return "SV_Position"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3372 | case builtin::BuiltinValue::kVertexIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3373 | return "SV_VertexID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3374 | case builtin::BuiltinValue::kInstanceIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3375 | return "SV_InstanceID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3376 | case builtin::BuiltinValue::kFrontFacing: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3377 | return "SV_IsFrontFace"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3378 | case builtin::BuiltinValue::kFragDepth: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3379 | return "SV_Depth"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3380 | case builtin::BuiltinValue::kLocalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3381 | return "SV_GroupThreadID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3382 | case builtin::BuiltinValue::kLocalInvocationIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3383 | return "SV_GroupIndex"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3384 | case builtin::BuiltinValue::kGlobalInvocationId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3385 | return "SV_DispatchThreadID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3386 | case builtin::BuiltinValue::kWorkgroupId: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3387 | return "SV_GroupID"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3388 | case builtin::BuiltinValue::kSampleIndex: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3389 | return "SV_SampleIndex"; |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 3390 | case builtin::BuiltinValue::kSampleMask: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3391 | return "SV_Coverage"; |
| 3392 | default: |
| 3393 | break; |
| 3394 | } |
| 3395 | return ""; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3398 | std::string ASTPrinter::interpolation_to_modifiers(builtin::InterpolationType type, |
| 3399 | builtin::InterpolationSampling sampling) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3400 | std::string modifiers; |
| 3401 | switch (type) { |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3402 | case builtin::InterpolationType::kPerspective: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3403 | modifiers += "linear "; |
| 3404 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3405 | case builtin::InterpolationType::kLinear: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3406 | modifiers += "noperspective "; |
| 3407 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3408 | case builtin::InterpolationType::kFlat: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3409 | modifiers += "nointerpolation "; |
| 3410 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3411 | case builtin::InterpolationType::kUndefined: |
Ben Clayton | f9ed9d3 | 2022-10-11 19:49:17 +0000 | [diff] [blame] | 3412 | break; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3413 | } |
| 3414 | switch (sampling) { |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3415 | case builtin::InterpolationSampling::kCentroid: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3416 | modifiers += "centroid "; |
| 3417 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3418 | case builtin::InterpolationSampling::kSample: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3419 | modifiers += "sample "; |
| 3420 | break; |
dan sinclair | 993a658 | 2023-02-20 08:37:45 +0000 | [diff] [blame] | 3421 | case builtin::InterpolationSampling::kCenter: |
| 3422 | case builtin::InterpolationSampling::kUndefined: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | } |
| 3425 | return modifiers; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3428 | bool ASTPrinter::EmitEntryPointFunction(const ast::Function* func) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3429 | auto* func_sem = builder_.Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3430 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3431 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3432 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3433 | if (func->PipelineStage() == ast::PipelineStage::kCompute) { |
| 3434 | // Emit the workgroup_size attribute. |
| 3435 | auto wgsize = func_sem->WorkgroupSize(); |
| 3436 | out << "[numthreads("; |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 3437 | for (size_t i = 0; i < 3; i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3438 | if (i > 0) { |
| 3439 | out << ", "; |
| 3440 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3441 | if (!wgsize[i].has_value()) { |
| 3442 | diagnostics_.add_error( |
| 3443 | diag::System::Writer, |
Ben Clayton | f10a579 | 2022-10-13 13:47:39 +0000 | [diff] [blame] | 3444 | "override-expressions should have been removed with the SubstituteOverride " |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3445 | "transform"); |
| 3446 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3447 | } |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 3448 | out << std::to_string(wgsize[i].value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3449 | } |
| 3450 | out << ")]" << std::endl; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3453 | if (!EmitTypeAndName(out, func_sem->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3454 | builtin::Access::kUndefined, func->name->symbol.Name())) { |
Ben Clayton | 1906857 | 2023-02-07 21:28:09 +0000 | [diff] [blame] | 3455 | return false; |
| 3456 | } |
| 3457 | out << "("; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3458 | |
| 3459 | bool first = true; |
| 3460 | |
| 3461 | // Emit entry point parameters. |
| 3462 | for (auto* var : func->params) { |
| 3463 | auto* sem = builder_.Sem().Get(var); |
| 3464 | auto* type = sem->Type(); |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 3465 | if (TINT_UNLIKELY(!type->Is<type::Struct>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3466 | // ICE likely indicates that the CanonicalizeEntryPointIO transform was |
| 3467 | // not run, or a builtin parameter was added after it was run. |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3468 | TINT_ICE() << "Unsupported non-struct entry point parameter"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | if (!first) { |
| 3472 | out << ", "; |
| 3473 | } |
| 3474 | first = false; |
| 3475 | |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 3476 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3477 | var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3478 | return false; |
| 3479 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3480 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3481 | |
| 3482 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3485 | { |
| 3486 | ScopedIndent si(this); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3487 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3488 | if (!EmitStatements(func->body->statements)) { |
| 3489 | return false; |
| 3490 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3491 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3492 | if (!Is<ast::ReturnStatement>(func->body->Last())) { |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 3493 | ast::ReturnStatement ret(GenerationID(), ast::NodeID{}, Source{}); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3494 | if (!EmitStatement(&ret)) { |
| 3495 | return false; |
| 3496 | } |
| 3497 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3500 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3501 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3502 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3505 | bool ASTPrinter::EmitConstant(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3506 | const constant::Value* constant, |
| 3507 | bool is_variable_initializer) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3508 | return Switch( |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3509 | constant->Type(), // |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3510 | [&](const type::Bool*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3511 | out << (constant->ValueAs<AInt>() ? "true" : "false"); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3512 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3513 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3514 | [&](const type::F32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3515 | PrintF32(out, constant->ValueAs<f32>()); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3516 | return true; |
| 3517 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3518 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3519 | // emit a f16 scalar with explicit float16_t type declaration. |
| 3520 | out << "float16_t("; |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3521 | PrintF16(out, constant->ValueAs<f16>()); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3522 | out << ")"; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3523 | return true; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3524 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3525 | [&](const type::I32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3526 | out << constant->ValueAs<AInt>(); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3527 | return true; |
| 3528 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3529 | [&](const type::U32*) { |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 3530 | out << constant->ValueAs<AInt>() << "u"; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3531 | return true; |
| 3532 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3533 | [&](const type::Vector* v) { |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3534 | if (auto* splat = constant->As<constant::Splat>()) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3535 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3536 | ScopedParen sp(out); |
James Price | 7bca4d7 | 2023-03-13 19:05:16 +0000 | [diff] [blame] | 3537 | if (!EmitConstant(out, splat->el, is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3538 | return false; |
| 3539 | } |
| 3540 | } |
| 3541 | out << "."; |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3542 | for (size_t i = 0; i < v->Width(); i++) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3543 | out << "x"; |
| 3544 | } |
| 3545 | return true; |
| 3546 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3547 | |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3548 | if (!EmitType(out, v, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3549 | "")) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3550 | return false; |
| 3551 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3552 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3553 | ScopedParen sp(out); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3554 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3555 | for (size_t i = 0; i < v->Width(); i++) { |
| 3556 | if (i > 0) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3557 | out << ", "; |
| 3558 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3559 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3560 | return false; |
| 3561 | } |
| 3562 | } |
| 3563 | return true; |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3564 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3565 | [&](const type::Matrix* m) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3566 | if (!EmitType(out, m, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 3567 | "")) { |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3568 | return false; |
| 3569 | } |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3570 | |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3571 | ScopedParen sp(out); |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3572 | |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3573 | for (size_t i = 0; i < m->columns(); i++) { |
| 3574 | if (i > 0) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3575 | out << ", "; |
| 3576 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3577 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 5041480 | 2022-06-24 08:06:19 +0000 | [diff] [blame] | 3578 | return false; |
| 3579 | } |
| 3580 | } |
| 3581 | return true; |
| 3582 | }, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3583 | [&](const type::Array* a) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3584 | if (constant->AllZero()) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3585 | out << "("; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3586 | if (!EmitType(out, a, builtin::AddressSpace::kUndefined, |
| 3587 | builtin::Access::kUndefined, "")) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3588 | return false; |
| 3589 | } |
| 3590 | out << ")0"; |
| 3591 | return true; |
| 3592 | } |
| 3593 | |
| 3594 | out << "{"; |
| 3595 | TINT_DEFER(out << "}"); |
| 3596 | |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3597 | auto count = a->ConstantCount(); |
| 3598 | if (!count) { |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3599 | diagnostics_.add_error(diag::System::Writer, |
| 3600 | type::Array::kErrExpectedConstantCount); |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 3601 | return false; |
| 3602 | } |
| 3603 | |
| 3604 | for (size_t i = 0; i < count; i++) { |
Ben Clayton | aa037ac | 2022-06-29 19:07:30 +0000 | [diff] [blame] | 3605 | if (i > 0) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3606 | out << ", "; |
| 3607 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3608 | if (!EmitConstant(out, constant->Index(i), is_variable_initializer)) { |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 3609 | return false; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | return true; |
| 3614 | }, |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 3615 | [&](const type::Struct* s) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3616 | if (!EmitStructType(&helpers_, s)) { |
| 3617 | return false; |
| 3618 | } |
| 3619 | |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3620 | if (constant->AllZero()) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3621 | out << "(" << StructName(s) << ")0"; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3622 | return true; |
| 3623 | } |
| 3624 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3625 | auto emit_member_values = [&](StringStream& o) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3626 | o << "{"; |
dan sinclair | ad9cd0a | 2022-12-06 20:01:54 +0000 | [diff] [blame] | 3627 | for (size_t i = 0; i < s->Members().Length(); i++) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3628 | if (i > 0) { |
| 3629 | o << ", "; |
| 3630 | } |
| 3631 | if (!EmitConstant(o, constant->Index(i), is_variable_initializer)) { |
| 3632 | return false; |
| 3633 | } |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3634 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3635 | o << "}"; |
| 3636 | return true; |
| 3637 | }; |
| 3638 | |
| 3639 | if (is_variable_initializer) { |
| 3640 | if (!emit_member_values(out)) { |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3641 | return false; |
| 3642 | } |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3643 | } else { |
| 3644 | // HLSL requires structure initializers to be assigned directly to a variable. |
| 3645 | auto name = UniqueIdentifier("c"); |
| 3646 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3647 | auto decl = Line(); |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 3648 | decl << "const " << StructName(s) << " " << name << " = "; |
| 3649 | if (!emit_member_values(decl)) { |
| 3650 | return false; |
| 3651 | } |
| 3652 | decl << ";"; |
| 3653 | } |
| 3654 | out << name; |
Ben Clayton | 6c098ba | 2022-07-14 20:46:39 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | return true; |
| 3658 | }, |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3659 | [&](Default) { |
Ben Clayton | 1545ca1 | 2023-05-03 16:26:40 +0000 | [diff] [blame] | 3660 | diagnostics_.add_error(diag::System::Writer, |
| 3661 | "unhandled constant type: " + constant->Type()->FriendlyName()); |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3662 | return false; |
| 3663 | }); |
| 3664 | } |
| 3665 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3666 | bool ASTPrinter::EmitLiteral(StringStream& out, const ast::LiteralExpression* lit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3667 | return Switch( |
| 3668 | lit, |
| 3669 | [&](const ast::BoolLiteralExpression* l) { |
| 3670 | out << (l->value ? "true" : "false"); |
| 3671 | return true; |
| 3672 | }, |
Ben Clayton | 3ad927c | 2022-05-25 23:12:14 +0000 | [diff] [blame] | 3673 | [&](const ast::FloatLiteralExpression* l) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3674 | if (l->suffix == ast::FloatLiteralExpression::Suffix::kH) { |
| 3675 | // Emit f16 literal with explicit float16_t type declaration. |
| 3676 | out << "float16_t("; |
Antonio Maiorano | 679cf4f | 2022-09-03 21:43:01 +0000 | [diff] [blame] | 3677 | PrintF16(out, static_cast<float>(l->value)); |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3678 | out << ")"; |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3679 | } |
Ben Clayton | e9f8b09 | 2022-06-01 13:14:39 +0000 | [diff] [blame] | 3680 | PrintF32(out, static_cast<float>(l->value)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3681 | return true; |
| 3682 | }, |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 3683 | [&](const ast::IntLiteralExpression* i) { |
| 3684 | out << i->value; |
| 3685 | switch (i->suffix) { |
| 3686 | case ast::IntLiteralExpression::Suffix::kNone: |
| 3687 | case ast::IntLiteralExpression::Suffix::kI: |
| 3688 | return true; |
| 3689 | case ast::IntLiteralExpression::Suffix::kU: |
| 3690 | out << "u"; |
| 3691 | return true; |
| 3692 | } |
| 3693 | diagnostics_.add_error(diag::System::Writer, "unknown integer literal suffix type"); |
| 3694 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3695 | }, |
| 3696 | [&](Default) { |
| 3697 | diagnostics_.add_error(diag::System::Writer, "unknown literal type"); |
| 3698 | return false; |
| 3699 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3702 | bool ASTPrinter::EmitValue(StringStream& out, const type::Type* type, int value) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3703 | return Switch( |
| 3704 | type, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3705 | [&](const type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3706 | out << (value == 0 ? "false" : "true"); |
| 3707 | return true; |
| 3708 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3709 | [&](const type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3710 | out << value << ".0f"; |
| 3711 | return true; |
| 3712 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3713 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 3714 | out << "float16_t(" << value << ".0h)"; |
| 3715 | return true; |
| 3716 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3717 | [&](const type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3718 | out << value; |
| 3719 | return true; |
| 3720 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 3721 | [&](const type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3722 | out << value << "u"; |
| 3723 | return true; |
| 3724 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3725 | [&](const type::Vector* vec) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3726 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3727 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3728 | return false; |
| 3729 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3730 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3731 | for (uint32_t i = 0; i < vec->Width(); i++) { |
| 3732 | if (i != 0) { |
| 3733 | out << ", "; |
| 3734 | } |
| 3735 | if (!EmitValue(out, vec->type(), value)) { |
| 3736 | return false; |
| 3737 | } |
| 3738 | } |
| 3739 | return true; |
| 3740 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3741 | [&](const type::Matrix* mat) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3742 | if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, |
Ben Clayton | 1b90f93 | 2023-02-18 12:37:34 +0000 | [diff] [blame] | 3743 | "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3744 | return false; |
| 3745 | } |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 3746 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3747 | for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { |
| 3748 | if (i != 0) { |
| 3749 | out << ", "; |
| 3750 | } |
| 3751 | if (!EmitValue(out, mat->type(), value)) { |
| 3752 | return false; |
| 3753 | } |
| 3754 | } |
| 3755 | return true; |
| 3756 | }, |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 3757 | [&](const type::Struct*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3758 | out << "("; |
| 3759 | TINT_DEFER(out << ")" << value); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3760 | return EmitType(out, type, builtin::AddressSpace::kUndefined, |
| 3761 | builtin::Access::kUndefined, ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3762 | }, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 3763 | [&](const type::Array*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3764 | out << "("; |
| 3765 | TINT_DEFER(out << ")" << value); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 3766 | return EmitType(out, type, builtin::AddressSpace::kUndefined, |
| 3767 | builtin::Access::kUndefined, ""); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3768 | }, |
| 3769 | [&](Default) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3770 | diagnostics_.add_error(diag::System::Writer, |
| 3771 | "Invalid type for value emission: " + type->FriendlyName()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3772 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3773 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3776 | bool ASTPrinter::EmitZeroValue(StringStream& out, const type::Type* type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3777 | return EmitValue(out, type, 0); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3780 | bool ASTPrinter::EmitLoop(const ast::LoopStatement* stmt) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 3781 | auto emit_continuing = [this, stmt] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3782 | if (stmt->continuing && !stmt->continuing->Empty()) { |
| 3783 | if (!EmitBlock(stmt->continuing)) { |
| 3784 | return false; |
| 3785 | } |
| 3786 | } |
| 3787 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3788 | }; |
| 3789 | |
| 3790 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3791 | Line() << "while (true) {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3792 | { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3793 | ScopedIndent si(this); |
| 3794 | if (!EmitStatements(stmt->body->statements)) { |
| 3795 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3796 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3797 | if (!emit_continuing_()) { |
| 3798 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3799 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3800 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3801 | Line() << "}"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3802 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3803 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3806 | bool ASTPrinter::EmitForLoop(const ast::ForLoopStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3807 | // Nest a for loop with a new block. In HLSL the initializer scope is not |
| 3808 | // nested by the for-loop, so we may get variable redefinitions. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3809 | Line() << "{"; |
| 3810 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3811 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3812 | DecrementIndent(); |
| 3813 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3814 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3815 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3816 | TextBuffer init_buf; |
| 3817 | if (auto* init = stmt->initializer) { |
| 3818 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); |
| 3819 | if (!EmitStatement(init)) { |
| 3820 | return false; |
| 3821 | } |
| 3822 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3823 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3824 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3825 | StringStream cond_buf; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3826 | if (auto* cond = stmt->condition) { |
| 3827 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 3828 | if (!EmitExpression(cond_buf, cond)) { |
| 3829 | return false; |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | TextBuffer cont_buf; |
| 3834 | if (auto* cont = stmt->continuing) { |
| 3835 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cont_buf); |
| 3836 | if (!EmitStatement(cont)) { |
| 3837 | return false; |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | // If the for-loop has a multi-statement conditional and / or continuing, then |
| 3842 | // we cannot emit this as a regular for-loop in HLSL. Instead we need to |
| 3843 | // generate a `while(true)` loop. |
| 3844 | bool emit_as_loop = cond_pre.lines.size() > 0 || cont_buf.lines.size() > 1; |
| 3845 | |
| 3846 | // If the for-loop has multi-statement initializer, or is going to be emitted |
| 3847 | // as a `while(true)` loop, then declare the initializer statement(s) before |
| 3848 | // the loop. |
| 3849 | if (init_buf.lines.size() > 1 || (stmt->initializer && emit_as_loop)) { |
| 3850 | current_buffer_->Append(init_buf); |
| 3851 | init_buf.lines.clear(); // Don't emit the initializer again in the 'for' |
| 3852 | } |
| 3853 | |
| 3854 | if (emit_as_loop) { |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 3855 | auto emit_continuing = [&] { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3856 | current_buffer_->Append(cont_buf); |
| 3857 | return true; |
| 3858 | }; |
| 3859 | |
| 3860 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3861 | Line() << "while (true) {"; |
| 3862 | IncrementIndent(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3863 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3864 | DecrementIndent(); |
| 3865 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3866 | }); |
| 3867 | |
| 3868 | if (stmt->condition) { |
| 3869 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3870 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | if (!EmitStatements(stmt->body->statements)) { |
| 3874 | return false; |
| 3875 | } |
| 3876 | |
| 3877 | if (!emit_continuing_()) { |
| 3878 | return false; |
| 3879 | } |
| 3880 | } else { |
| 3881 | // For-loop can be generated. |
| 3882 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3883 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3884 | out << "for"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3885 | { |
| 3886 | ScopedParen sp(out); |
| 3887 | |
| 3888 | if (!init_buf.lines.empty()) { |
| 3889 | out << init_buf.lines[0].content << " "; |
| 3890 | } else { |
| 3891 | out << "; "; |
| 3892 | } |
| 3893 | |
| 3894 | out << cond_buf.str() << "; "; |
| 3895 | |
| 3896 | if (!cont_buf.lines.empty()) { |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3897 | out << tint::TrimSuffix(cont_buf.lines[0].content, ";"); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3898 | } |
| 3899 | } |
| 3900 | out << " {"; |
| 3901 | } |
| 3902 | { |
| 3903 | auto emit_continuing = [] { return true; }; |
| 3904 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 3905 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 3906 | return false; |
| 3907 | } |
| 3908 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3909 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
| 3912 | return true; |
| 3913 | } |
| 3914 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3915 | bool ASTPrinter::EmitWhile(const ast::WhileStatement* stmt) { |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3916 | TextBuffer cond_pre; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3917 | StringStream cond_buf; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3918 | { |
| 3919 | auto* cond = stmt->condition; |
| 3920 | TINT_SCOPED_ASSIGNMENT(current_buffer_, &cond_pre); |
| 3921 | if (!EmitExpression(cond_buf, cond)) { |
| 3922 | return false; |
| 3923 | } |
| 3924 | } |
| 3925 | |
Ben Clayton | a0aabaf | 2023-06-22 23:53:09 +0000 | [diff] [blame] | 3926 | auto emit_continuing = [&] { return true; }; |
dan sinclair | 4b88dbc | 2022-06-16 15:27:38 +0000 | [diff] [blame] | 3927 | TINT_SCOPED_ASSIGNMENT(emit_continuing_, emit_continuing); |
| 3928 | |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3929 | // If the while has a multi-statement conditional, then we cannot emit this |
| 3930 | // as a regular while in HLSL. Instead we need to generate a `while(true)` loop. |
| 3931 | bool emit_as_loop = cond_pre.lines.size() > 0; |
| 3932 | if (emit_as_loop) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3933 | Line() << "while (true) {"; |
| 3934 | IncrementIndent(); |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3935 | TINT_DEFER({ |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3936 | DecrementIndent(); |
| 3937 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3938 | }); |
| 3939 | |
| 3940 | current_buffer_->Append(cond_pre); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3941 | Line() << "if (!(" << cond_buf.str() << ")) { break; }"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3942 | if (!EmitStatements(stmt->body->statements)) { |
| 3943 | return false; |
| 3944 | } |
| 3945 | } else { |
| 3946 | // While can be generated. |
| 3947 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3948 | auto out = Line(); |
Antonio Maiorano | 06844a5 | 2022-09-29 16:53:58 +0000 | [diff] [blame] | 3949 | out << "while"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3950 | { |
| 3951 | ScopedParen sp(out); |
| 3952 | out << cond_buf.str(); |
| 3953 | } |
| 3954 | out << " {"; |
| 3955 | } |
| 3956 | if (!EmitStatementsWithIndent(stmt->body->statements)) { |
| 3957 | return false; |
| 3958 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3959 | Line() << "}"; |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | return true; |
| 3963 | } |
| 3964 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 3965 | bool ASTPrinter::EmitMemberAccessor(StringStream& out, const ast::MemberAccessorExpression* expr) { |
Ben Clayton | ad31565 | 2023-02-05 12:36:50 +0000 | [diff] [blame] | 3966 | if (!EmitExpression(out, expr->object)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3967 | return false; |
| 3968 | } |
| 3969 | out << "."; |
| 3970 | |
Ben Clayton | 2f9a988 | 2022-12-17 02:20:04 +0000 | [diff] [blame] | 3971 | auto* sem = builder_.Sem().Get(expr)->UnwrapLoad(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3972 | |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3973 | return Switch( |
| 3974 | sem, |
| 3975 | [&](const sem::Swizzle*) { |
| 3976 | // Swizzles output the name directly |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3977 | out << expr->member->symbol.Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3978 | return true; |
| 3979 | }, |
| 3980 | [&](const sem::StructMemberAccess* member_access) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 3981 | out << member_access->Member()->Name().Name(); |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3982 | return true; |
| 3983 | }, |
| 3984 | [&](Default) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 3985 | TINT_ICE() << "unknown member access type: " << sem->TypeInfo().name; |
Ben Clayton | 10fae7a | 2022-11-14 15:29:29 +0000 | [diff] [blame] | 3986 | return false; |
| 3987 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 3990 | bool ASTPrinter::EmitReturn(const ast::ReturnStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3991 | if (stmt->value) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3992 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 3993 | out << "return "; |
| 3994 | if (!EmitExpression(out, stmt->value)) { |
| 3995 | return false; |
| 3996 | } |
| 3997 | out << ";"; |
| 3998 | } else { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 3999 | Line() << "return;"; |
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 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4004 | bool ASTPrinter::EmitStatement(const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4005 | return Switch( |
| 4006 | stmt, |
| 4007 | [&](const ast::AssignmentStatement* a) { // |
| 4008 | return EmitAssign(a); |
| 4009 | }, |
| 4010 | [&](const ast::BlockStatement* b) { // |
| 4011 | return EmitBlock(b); |
| 4012 | }, |
| 4013 | [&](const ast::BreakStatement* b) { // |
| 4014 | return EmitBreak(b); |
| 4015 | }, |
dan sinclair | b8b0c21 | 2022-10-20 22:45:50 +0000 | [diff] [blame] | 4016 | [&](const ast::BreakIfStatement* b) { // |
| 4017 | return EmitBreakIf(b); |
| 4018 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4019 | [&](const ast::CallStatement* c) { // |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4020 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4021 | if (!EmitCall(out, c->expr)) { |
| 4022 | return false; |
| 4023 | } |
| 4024 | out << ";"; |
| 4025 | return true; |
| 4026 | }, |
| 4027 | [&](const ast::ContinueStatement* c) { // |
| 4028 | return EmitContinue(c); |
| 4029 | }, |
| 4030 | [&](const ast::DiscardStatement* d) { // |
| 4031 | return EmitDiscard(d); |
| 4032 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4033 | [&](const ast::IfStatement* i) { // |
| 4034 | return EmitIf(i); |
| 4035 | }, |
| 4036 | [&](const ast::LoopStatement* l) { // |
| 4037 | return EmitLoop(l); |
| 4038 | }, |
| 4039 | [&](const ast::ForLoopStatement* l) { // |
| 4040 | return EmitForLoop(l); |
| 4041 | }, |
dan sinclair | 49d1a2d | 2022-06-16 12:01:27 +0000 | [diff] [blame] | 4042 | [&](const ast::WhileStatement* l) { // |
| 4043 | return EmitWhile(l); |
| 4044 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4045 | [&](const ast::ReturnStatement* r) { // |
| 4046 | return EmitReturn(r); |
| 4047 | }, |
| 4048 | [&](const ast::SwitchStatement* s) { // |
| 4049 | return EmitSwitch(s); |
| 4050 | }, |
| 4051 | [&](const ast::VariableDeclStatement* v) { // |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4052 | return Switch( |
| 4053 | v->variable, // |
| 4054 | [&](const ast::Var* var) { return EmitVar(var); }, |
| 4055 | [&](const ast::Let* let) { return EmitLet(let); }, |
Ben Clayton | 19576e9 | 2022-06-28 12:44:16 +0000 | [diff] [blame] | 4056 | [&](const ast::Const*) { |
| 4057 | return true; // Constants are embedded at their use |
| 4058 | }, |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4059 | [&](Default) { // |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4060 | TINT_ICE() << "unknown variable type: " << v->variable->TypeInfo().name; |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4061 | return false; |
| 4062 | }); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4063 | }, |
Ben Clayton | c98d57d | 2023-01-24 14:59:43 +0000 | [diff] [blame] | 4064 | [&](const ast::ConstAssert*) { |
Ben Clayton | b4744ac | 2022-08-03 07:01:08 +0000 | [diff] [blame] | 4065 | return true; // Not emitted |
| 4066 | }, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4067 | [&](Default) { // |
| 4068 | diagnostics_.add_error(diag::System::Writer, |
| 4069 | "unknown statement type: " + std::string(stmt->TypeInfo().name)); |
| 4070 | return false; |
| 4071 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4074 | bool ASTPrinter::EmitDefaultOnlySwitch(const ast::SwitchStatement* stmt) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4075 | TINT_ASSERT(stmt->body.Length() == 1 && stmt->body[0]->ContainsDefault()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4076 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4077 | // FXC fails to compile a switch with just a default case, ignoring the |
| 4078 | // default case body. We work around this here by emitting the default case |
| 4079 | // without the switch. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4080 | |
Antonio Maiorano | eab1f62 | 2023-02-01 15:46:34 +0000 | [diff] [blame] | 4081 | // Emit the switch condition as-is if it has side-effects (e.g. |
| 4082 | // 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] | 4083 | if (auto* sem_cond = builder_.Sem().GetVal(stmt->condition); sem_cond->HasSideEffects()) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4084 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4085 | if (!EmitExpression(out, stmt->condition)) { |
| 4086 | return false; |
| 4087 | } |
| 4088 | out << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4089 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4090 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4091 | // Emit "do { <default case body> } while(false);". We use a 'do' loop so |
| 4092 | // that break statements work as expected, and make it 'while (false)' in |
| 4093 | // case there isn't a break statement. |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4094 | Line() << "do {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4095 | { |
| 4096 | ScopedIndent si(this); |
| 4097 | if (!EmitStatements(stmt->body[0]->body->statements)) { |
| 4098 | return false; |
| 4099 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4100 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4101 | Line() << "} while (false);"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4102 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4105 | bool ASTPrinter::EmitSwitch(const ast::SwitchStatement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4106 | // BUG(crbug.com/tint/1188): work around default-only switches |
dan sinclair | f148f08 | 2022-10-19 15:55:02 +0000 | [diff] [blame] | 4107 | if (stmt->body.Length() == 1 && stmt->body[0]->selectors.Length() == 1 && |
| 4108 | stmt->body[0]->ContainsDefault()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4109 | return EmitDefaultOnlySwitch(stmt); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4110 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4111 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4112 | { // switch(expr) { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4113 | auto out = Line(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4114 | out << "switch("; |
| 4115 | if (!EmitExpression(out, stmt->condition)) { |
| 4116 | return false; |
| 4117 | } |
| 4118 | out << ") {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4119 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4120 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4121 | { |
| 4122 | ScopedIndent si(this); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 4123 | for (size_t i = 0; i < stmt->body.Length(); i++) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4124 | if (!EmitCase(stmt, i)) { |
| 4125 | return false; |
| 4126 | } |
| 4127 | } |
| 4128 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4129 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4130 | Line() << "}"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4131 | |
| 4132 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4135 | bool ASTPrinter::EmitType(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4136 | const type::Type* type, |
| 4137 | builtin::AddressSpace address_space, |
| 4138 | builtin::Access access, |
| 4139 | const std::string& name, |
| 4140 | bool* name_printed /* = nullptr */) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4141 | if (name_printed) { |
| 4142 | *name_printed = false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4143 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4144 | switch (address_space) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4145 | case builtin::AddressSpace::kStorage: |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4146 | if (access != builtin::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4147 | out << "RW"; |
| 4148 | } |
| 4149 | out << "ByteAddressBuffer"; |
| 4150 | return true; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4151 | case builtin::AddressSpace::kUniform: { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4152 | auto array_length = (type->Size() + 15) / 16; |
| 4153 | out << "uint4 " << name << "[" << array_length << "]"; |
| 4154 | if (name_printed) { |
| 4155 | *name_printed = true; |
| 4156 | } |
| 4157 | return true; |
| 4158 | } |
| 4159 | default: |
| 4160 | break; |
| 4161 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4162 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4163 | return Switch( |
| 4164 | type, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4165 | [&](const type::Array* ary) { |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 4166 | const type::Type* base_type = ary; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4167 | std::vector<uint32_t> sizes; |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4168 | while (auto* arr = base_type->As<type::Array>()) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4169 | if (TINT_UNLIKELY(arr->Count()->Is<type::RuntimeArrayCount>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4170 | TINT_ICE() |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4171 | << "runtime arrays may only exist in storage buffers, which should have " |
Ben Clayton | 3a68ab4 | 2022-06-24 08:30:28 +0000 | [diff] [blame] | 4172 | "been transformed into a ByteAddressBuffer"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4173 | return false; |
| 4174 | } |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4175 | const auto count = arr->ConstantCount(); |
| 4176 | if (!count) { |
| 4177 | diagnostics_.add_error(diag::System::Writer, |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4178 | type::Array::kErrExpectedConstantCount); |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 4179 | return false; |
| 4180 | } |
| 4181 | |
| 4182 | sizes.push_back(count.value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4183 | base_type = arr->ElemType(); |
| 4184 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4185 | if (!EmitType(out, base_type, address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4186 | return false; |
| 4187 | } |
| 4188 | if (!name.empty()) { |
| 4189 | out << " " << name; |
| 4190 | if (name_printed) { |
| 4191 | *name_printed = true; |
| 4192 | } |
| 4193 | } |
| 4194 | for (uint32_t size : sizes) { |
| 4195 | out << "[" << size << "]"; |
| 4196 | } |
| 4197 | return true; |
| 4198 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4199 | [&](const type::Bool*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4200 | out << "bool"; |
| 4201 | return true; |
| 4202 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4203 | [&](const type::F32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4204 | out << "float"; |
| 4205 | return true; |
| 4206 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4207 | [&](const type::F16*) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4208 | out << "float16_t"; |
| 4209 | return true; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 4210 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4211 | [&](const type::I32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4212 | out << "int"; |
| 4213 | return true; |
| 4214 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4215 | [&](const type::Matrix* mat) { |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4216 | if (mat->type()->Is<type::F16>()) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4217 | // Use matrix<type, N, M> for f16 matrix |
| 4218 | out << "matrix<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4219 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4220 | return false; |
| 4221 | } |
| 4222 | out << ", " << mat->columns() << ", " << mat->rows() << ">"; |
| 4223 | return true; |
| 4224 | } |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4225 | if (!EmitType(out, mat->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4226 | return false; |
| 4227 | } |
| 4228 | // Note: HLSL's matrices are declared as <type>NxM, where N is the |
| 4229 | // number of rows and M is the number of columns. Despite HLSL's |
| 4230 | // matrices being column-major by default, the index operator and |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4231 | // initializers actually operate on row-vectors, where as WGSL operates |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4232 | // on column vectors. To simplify everything we use the transpose of the |
| 4233 | // matrices. See: |
| 4234 | // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math#matrix-ordering |
| 4235 | out << mat->columns() << "x" << mat->rows(); |
| 4236 | return true; |
| 4237 | }, |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 4238 | [&](const type::Pointer*) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4239 | TINT_ICE() << "Attempting to emit pointer type. These should have " |
| 4240 | "been removed with the SimplifyPointers transform"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4241 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4242 | }, |
dan sinclair | 5ee58b6 | 2022-12-08 15:25:18 +0000 | [diff] [blame] | 4243 | [&](const type::Sampler* sampler) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4244 | out << "Sampler"; |
| 4245 | if (sampler->IsComparison()) { |
| 4246 | out << "Comparison"; |
| 4247 | } |
| 4248 | out << "State"; |
| 4249 | return true; |
| 4250 | }, |
Ben Clayton | bc9e422 | 2023-04-27 18:31:44 +0000 | [diff] [blame] | 4251 | [&](const type::Struct* str) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4252 | out << StructName(str); |
| 4253 | return true; |
| 4254 | }, |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 4255 | [&](const type::Texture* tex) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4256 | if (TINT_UNLIKELY(tex->Is<type::ExternalTexture>())) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4257 | TINT_ICE() << "Multiplanar external texture transform was not run."; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4258 | return false; |
| 4259 | } |
Brandon Jones | 6661b28 | 2022-02-25 20:14:52 +0000 | [diff] [blame] | 4260 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 4261 | auto* storage = tex->As<type::StorageTexture>(); |
| 4262 | auto* ms = tex->As<type::MultisampledTexture>(); |
| 4263 | auto* depth_ms = tex->As<type::DepthMultisampledTexture>(); |
| 4264 | auto* sampled = tex->As<type::SampledTexture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4265 | |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4266 | if (storage && storage->access() != builtin::Access::kRead) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4267 | out << "RW"; |
| 4268 | } |
| 4269 | out << "Texture"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4270 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4271 | switch (tex->dim()) { |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4272 | case type::TextureDimension::k1d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4273 | out << "1D"; |
| 4274 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4275 | case type::TextureDimension::k2d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4276 | out << ((ms || depth_ms) ? "2DMS" : "2D"); |
| 4277 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4278 | case type::TextureDimension::k2dArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4279 | out << ((ms || depth_ms) ? "2DMSArray" : "2DArray"); |
| 4280 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4281 | case type::TextureDimension::k3d: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4282 | out << "3D"; |
| 4283 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4284 | case type::TextureDimension::kCube: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4285 | out << "Cube"; |
| 4286 | break; |
dan sinclair | 3cbf3fc | 2023-01-21 19:16:15 +0000 | [diff] [blame] | 4287 | case type::TextureDimension::kCubeArray: |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4288 | out << "CubeArray"; |
| 4289 | break; |
| 4290 | default: |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4291 | TINT_UNREACHABLE() << "unexpected TextureDimension " << tex->dim(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4292 | return false; |
| 4293 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4294 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4295 | if (storage) { |
| 4296 | auto* component = image_format_to_rwtexture_type(storage->texel_format()); |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4297 | if (TINT_UNLIKELY(!component)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4298 | TINT_ICE() << "Unsupported StorageTexture TexelFormat: " |
| 4299 | << static_cast<int>(storage->texel_format()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4300 | return false; |
| 4301 | } |
| 4302 | out << "<" << component << ">"; |
| 4303 | } else if (depth_ms) { |
| 4304 | out << "<float4>"; |
| 4305 | } else if (sampled || ms) { |
| 4306 | auto* subtype = sampled ? sampled->type() : ms->type(); |
| 4307 | out << "<"; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4308 | if (subtype->Is<type::F32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4309 | out << "float4"; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4310 | } else if (subtype->Is<type::I32>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4311 | out << "int4"; |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 4312 | } else if (TINT_LIKELY(subtype->Is<type::U32>())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4313 | out << "uint4"; |
| 4314 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4315 | TINT_ICE() << "Unsupported multisampled texture type"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4316 | return false; |
| 4317 | } |
| 4318 | out << ">"; |
| 4319 | } |
| 4320 | return true; |
| 4321 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4322 | [&](const type::U32*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4323 | out << "uint"; |
| 4324 | return true; |
| 4325 | }, |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4326 | [&](const type::Vector* vec) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4327 | auto width = vec->Width(); |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4328 | if (vec->type()->Is<type::F32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4329 | out << "float" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4330 | } else if (vec->type()->Is<type::I32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4331 | out << "int" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4332 | } else if (vec->type()->Is<type::U32>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4333 | out << "uint" << width; |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4334 | } else if (vec->type()->Is<type::Bool>() && width >= 1 && width <= 4) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4335 | out << "bool" << width; |
| 4336 | } else { |
Zhaoming Jiang | a5988a3 | 2022-07-11 15:43:38 +0000 | [diff] [blame] | 4337 | // For example, use "vector<float16_t, N>" for f16 vector. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4338 | out << "vector<"; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4339 | if (!EmitType(out, vec->type(), address_space, access, "")) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4340 | return false; |
| 4341 | } |
| 4342 | out << ", " << width << ">"; |
| 4343 | } |
| 4344 | return true; |
| 4345 | }, |
dan sinclair | d8a0845 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 4346 | [&](const type::Atomic* atomic) { |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4347 | return EmitType(out, atomic->Type(), address_space, access, name); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4348 | }, |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 4349 | [&](const type::Void*) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4350 | out << "void"; |
| 4351 | return true; |
| 4352 | }, |
| 4353 | [&](Default) { |
| 4354 | diagnostics_.add_error(diag::System::Writer, "unknown type in EmitType"); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4355 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4356 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4359 | bool ASTPrinter::EmitTypeAndName(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4360 | const type::Type* type, |
| 4361 | builtin::AddressSpace address_space, |
| 4362 | builtin::Access access, |
| 4363 | const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4364 | bool name_printed = false; |
dan sinclair | ff7cf21 | 2022-10-03 14:05:23 +0000 | [diff] [blame] | 4365 | if (!EmitType(out, type, address_space, access, name, &name_printed)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4366 | return false; |
| 4367 | } |
| 4368 | if (!name.empty() && !name_printed) { |
| 4369 | out << " " << name; |
| 4370 | } |
| 4371 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4374 | bool ASTPrinter::EmitStructType(TextBuffer* b, const type::Struct* str) { |
Ben Clayton | 329dfd7 | 2022-11-23 00:05:05 +0000 | [diff] [blame] | 4375 | auto it = emitted_structs_.emplace(str); |
| 4376 | if (!it.second) { |
| 4377 | return true; |
| 4378 | } |
| 4379 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4380 | Line(b) << "struct " << StructName(str) << " {"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4381 | { |
| 4382 | ScopedIndent si(b); |
| 4383 | for (auto* mem : str->Members()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4384 | auto mem_name = mem->Name().Name(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4385 | auto* ty = mem->Type(); |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4386 | auto out = Line(b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4387 | std::string pre, post; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4388 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4389 | auto& attributes = mem->Attributes(); |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 4390 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4391 | if (auto location = attributes.location) { |
| 4392 | auto& pipeline_stage_uses = str->PipelineStageUses(); |
| 4393 | if (TINT_UNLIKELY(pipeline_stage_uses.size() != 1)) { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4394 | TINT_ICE() << "invalid entry point IO struct uses"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4395 | } |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4396 | if (pipeline_stage_uses.count(type::PipelineStageUsage::kVertexInput)) { |
| 4397 | post += " : TEXCOORD" + std::to_string(location.value()); |
| 4398 | } else if (pipeline_stage_uses.count(type::PipelineStageUsage::kVertexOutput)) { |
| 4399 | post += " : TEXCOORD" + std::to_string(location.value()); |
| 4400 | } else if (pipeline_stage_uses.count(type::PipelineStageUsage::kFragmentInput)) { |
| 4401 | post += " : TEXCOORD" + std::to_string(location.value()); |
| 4402 | } else if (TINT_LIKELY(pipeline_stage_uses.count( |
| 4403 | type::PipelineStageUsage::kFragmentOutput))) { |
Brandon Jones | 07500d3 | 2023-07-21 22:07:03 +0000 | [diff] [blame] | 4404 | if (auto index = attributes.index) { |
| 4405 | post += " : SV_Target" + std::to_string(location.value() + index.value()); |
| 4406 | } else { |
| 4407 | post += " : SV_Target" + std::to_string(location.value()); |
| 4408 | } |
| 4409 | |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4410 | } else { |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 4411 | TINT_ICE() << "invalid use of location attribute"; |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | if (auto builtin = attributes.builtin) { |
| 4415 | auto name = builtin_to_attribute(builtin.value()); |
| 4416 | if (name.empty()) { |
| 4417 | diagnostics_.add_error(diag::System::Writer, "unsupported builtin"); |
| 4418 | return false; |
| 4419 | } |
| 4420 | post += " : " + name; |
| 4421 | } |
| 4422 | if (auto interpolation = attributes.interpolation) { |
| 4423 | auto mod = interpolation_to_modifiers(interpolation->type, interpolation->sampling); |
| 4424 | if (mod.empty()) { |
| 4425 | diagnostics_.add_error(diag::System::Writer, "unsupported interpolation"); |
| 4426 | return false; |
| 4427 | } |
| 4428 | pre += mod; |
| 4429 | } |
| 4430 | if (attributes.invariant) { |
| 4431 | // Note: `precise` is not exactly the same as `invariant`, but is |
| 4432 | // stricter and therefore provides the necessary guarantees. |
| 4433 | // See discussion here: https://github.com/gpuweb/gpuweb/issues/893 |
| 4434 | pre += "precise "; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4437 | out << pre; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4438 | if (!EmitTypeAndName(out, ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4439 | builtin::Access::kReadWrite, mem_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4440 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4441 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4442 | out << post << ";"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4443 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4444 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4445 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4446 | Line(b) << "};"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4447 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4450 | bool ASTPrinter::EmitUnaryOp(StringStream& out, const ast::UnaryOpExpression* expr) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4451 | switch (expr->op) { |
| 4452 | case ast::UnaryOp::kIndirection: |
| 4453 | case ast::UnaryOp::kAddressOf: |
| 4454 | return EmitExpression(out, expr->expr); |
| 4455 | case ast::UnaryOp::kComplement: |
| 4456 | out << "~"; |
| 4457 | break; |
| 4458 | case ast::UnaryOp::kNot: |
| 4459 | out << "!"; |
| 4460 | break; |
| 4461 | case ast::UnaryOp::kNegation: |
| 4462 | out << "-"; |
| 4463 | break; |
| 4464 | } |
| 4465 | out << "("; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4466 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4467 | if (!EmitExpression(out, expr->expr)) { |
| 4468 | return false; |
| 4469 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4470 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4471 | out << ")"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4472 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4473 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4476 | bool ASTPrinter::EmitVar(const ast::Var* var) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4477 | auto* sem = builder_.Sem().Get(var); |
| 4478 | auto* type = sem->Type()->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4479 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4480 | auto out = Line(); |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4481 | if (!EmitTypeAndName(out, type, sem->AddressSpace(), sem->Access(), var->name->symbol.Name())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4482 | return false; |
| 4483 | } |
| 4484 | |
| 4485 | out << " = "; |
| 4486 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4487 | if (var->initializer) { |
| 4488 | if (!EmitExpression(out, var->initializer)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4489 | return false; |
| 4490 | } |
| 4491 | } else { |
| 4492 | if (!EmitZeroValue(out, type)) { |
| 4493 | return false; |
| 4494 | } |
| 4495 | } |
| 4496 | out << ";"; |
| 4497 | |
| 4498 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4501 | bool ASTPrinter::EmitLet(const ast::Let* let) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4502 | auto* sem = builder_.Sem().Get(let); |
| 4503 | auto* type = sem->Type()->UnwrapRef(); |
| 4504 | |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4505 | auto out = Line(); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4506 | out << "const "; |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4507 | if (!EmitTypeAndName(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kUndefined, |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 4508 | let->name->symbol.Name())) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4509 | return false; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4510 | } |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4511 | out << " = "; |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 4512 | if (!EmitExpression(out, let->initializer)) { |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4513 | return false; |
| 4514 | } |
| 4515 | out << ";"; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4516 | |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 4517 | return true; |
| 4518 | } |
| 4519 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4520 | template <typename F> |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4521 | bool ASTPrinter::CallBuiltinHelper(StringStream& out, |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4522 | const ast::CallExpression* call, |
| 4523 | const sem::Builtin* builtin, |
| 4524 | F&& build) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4525 | // Generate the helper function if it hasn't been created already |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 4526 | auto fn = tint::GetOrCreate(builtins_, builtin, [&]() -> std::string { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4527 | TextBuffer b; |
| 4528 | TINT_DEFER(helpers_.Append(b)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4529 | |
dan sinclair | 9543f74 | 2023-03-09 01:20:16 +0000 | [diff] [blame] | 4530 | auto fn_name = UniqueIdentifier(std::string("tint_") + builtin::str(builtin->Type())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4531 | std::vector<std::string> parameter_names; |
| 4532 | { |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4533 | auto decl = Line(&b); |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4534 | if (!EmitTypeAndName(decl, builtin->ReturnType(), builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4535 | builtin::Access::kUndefined, fn_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4536 | return ""; |
| 4537 | } |
| 4538 | { |
| 4539 | ScopedParen sp(decl); |
| 4540 | for (auto* param : builtin->Parameters()) { |
| 4541 | if (!parameter_names.empty()) { |
| 4542 | decl << ", "; |
| 4543 | } |
| 4544 | auto param_name = "param_" + std::to_string(parameter_names.size()); |
| 4545 | const auto* ty = param->Type(); |
dan sinclair | 4d56b48 | 2022-12-08 17:50:50 +0000 | [diff] [blame] | 4546 | if (auto* ptr = ty->As<type::Pointer>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4547 | decl << "inout "; |
| 4548 | ty = ptr->StoreType(); |
| 4549 | } |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 4550 | if (!EmitTypeAndName(decl, ty, builtin::AddressSpace::kUndefined, |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 4551 | builtin::Access::kUndefined, param_name)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4552 | return ""; |
| 4553 | } |
| 4554 | parameter_names.emplace_back(std::move(param_name)); |
| 4555 | } |
| 4556 | } |
| 4557 | decl << " {"; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4558 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4559 | { |
| 4560 | ScopedIndent si(&b); |
| 4561 | if (!build(&b, parameter_names)) { |
| 4562 | return ""; |
| 4563 | } |
| 4564 | } |
dan sinclair | 67a1893 | 2023-06-26 19:54:49 +0000 | [diff] [blame] | 4565 | Line(&b) << "}"; |
| 4566 | Line(&b); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4567 | return fn_name; |
| 4568 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4569 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4570 | if (fn.empty()) { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4571 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4572 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4573 | |
| 4574 | // Call the helper |
| 4575 | out << fn; |
| 4576 | { |
dan sinclair | b2ba57b | 2023-02-28 15:14:09 +0000 | [diff] [blame] | 4577 | ScopedParen sp(out); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 4578 | bool first = true; |
| 4579 | for (auto* arg : call->args) { |
| 4580 | if (!first) { |
| 4581 | out << ", "; |
| 4582 | } |
| 4583 | first = false; |
| 4584 | if (!EmitExpression(out, arg)) { |
| 4585 | return false; |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4592 | std::string ASTPrinter::UniqueIdentifier(const std::string& prefix /* = "" */) { |
Ben Clayton | 5ed099a | 2023-07-25 18:52:03 +0000 | [diff] [blame] | 4593 | return builder_.Symbols().New(prefix).Name(); |
| 4594 | } |
| 4595 | |
dan sinclair | 0bfeaf9 | 2023-07-26 17:39:51 +0000 | [diff] [blame] | 4596 | } // namespace tint::hlsl::writer |