Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2023 The Dawn & Tint Authors |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 27 | |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 28 | #include "src/tint/lang/core/ir/validator.h" |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 29 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 31 | #include <cstdint> |
dan sinclair | fbc4ce7 | 2024-10-21 19:25:39 +0000 | [diff] [blame] | 32 | #include <functional> |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 33 | #include <memory> |
dan sinclair | cff18f3 | 2023-06-05 21:36:29 +0000 | [diff] [blame] | 34 | #include <string> |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 35 | #include <string_view> |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 36 | #include <utility> |
| 37 | |
Ben Clayton | 6a56e27 | 2023-09-06 02:01:07 +0000 | [diff] [blame] | 38 | #include "src/tint/lang/core/intrinsic/table.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 39 | #include "src/tint/lang/core/ir/access.h" |
| 40 | #include "src/tint/lang/core/ir/binary.h" |
| 41 | #include "src/tint/lang/core/ir/bitcast.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 42 | #include "src/tint/lang/core/ir/block_param.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 43 | #include "src/tint/lang/core/ir/break_if.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 44 | #include "src/tint/lang/core/ir/constant.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 45 | #include "src/tint/lang/core/ir/construct.h" |
| 46 | #include "src/tint/lang/core/ir/continue.h" |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 47 | #include "src/tint/lang/core/ir/control_instruction.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 48 | #include "src/tint/lang/core/ir/convert.h" |
| 49 | #include "src/tint/lang/core/ir/core_builtin_call.h" |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 50 | #include "src/tint/lang/core/ir/disassembler.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 51 | #include "src/tint/lang/core/ir/discard.h" |
| 52 | #include "src/tint/lang/core/ir/exit_if.h" |
| 53 | #include "src/tint/lang/core/ir/exit_loop.h" |
| 54 | #include "src/tint/lang/core/ir/exit_switch.h" |
| 55 | #include "src/tint/lang/core/ir/function.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 56 | #include "src/tint/lang/core/ir/function_param.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 57 | #include "src/tint/lang/core/ir/if.h" |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 58 | #include "src/tint/lang/core/ir/instruction.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 59 | #include "src/tint/lang/core/ir/instruction_result.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 60 | #include "src/tint/lang/core/ir/let.h" |
| 61 | #include "src/tint/lang/core/ir/load.h" |
| 62 | #include "src/tint/lang/core/ir/load_vector_element.h" |
| 63 | #include "src/tint/lang/core/ir/loop.h" |
James Price | c34ca5f | 2024-06-11 18:24:06 +0000 | [diff] [blame] | 64 | #include "src/tint/lang/core/ir/member_builtin_call.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 65 | #include "src/tint/lang/core/ir/multi_in_block.h" |
| 66 | #include "src/tint/lang/core/ir/next_iteration.h" |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 67 | #include "src/tint/lang/core/ir/referenced_module_vars.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 68 | #include "src/tint/lang/core/ir/return.h" |
| 69 | #include "src/tint/lang/core/ir/store.h" |
| 70 | #include "src/tint/lang/core/ir/store_vector_element.h" |
| 71 | #include "src/tint/lang/core/ir/switch.h" |
| 72 | #include "src/tint/lang/core/ir/swizzle.h" |
| 73 | #include "src/tint/lang/core/ir/terminate_invocation.h" |
| 74 | #include "src/tint/lang/core/ir/unary.h" |
| 75 | #include "src/tint/lang/core/ir/unreachable.h" |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 76 | #include "src/tint/lang/core/ir/unused.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 77 | #include "src/tint/lang/core/ir/user_call.h" |
| 78 | #include "src/tint/lang/core/ir/var.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 79 | #include "src/tint/lang/core/type/bool.h" |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 80 | #include "src/tint/lang/core/type/f32.h" |
James Price | d31f5e4 | 2024-08-01 04:47:58 +0000 | [diff] [blame] | 81 | #include "src/tint/lang/core/type/i8.h" |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 82 | #include "src/tint/lang/core/type/memory_view.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 83 | #include "src/tint/lang/core/type/pointer.h" |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 84 | #include "src/tint/lang/core/type/reference.h" |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 85 | #include "src/tint/lang/core/type/type.h" |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 86 | #include "src/tint/lang/core/type/u32.h" |
James Price | d31f5e4 | 2024-08-01 04:47:58 +0000 | [diff] [blame] | 87 | #include "src/tint/lang/core/type/u8.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 88 | #include "src/tint/lang/core/type/vector.h" |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 89 | #include "src/tint/lang/core/type/void.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 90 | #include "src/tint/utils/containers/hashset.h" |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 91 | #include "src/tint/utils/containers/predicates.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 92 | #include "src/tint/utils/containers/reverse.h" |
Ben Clayton | 6a56e27 | 2023-09-06 02:01:07 +0000 | [diff] [blame] | 93 | #include "src/tint/utils/containers/transform.h" |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 94 | #include "src/tint/utils/diagnostic/diagnostic.h" |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 95 | #include "src/tint/utils/ice/ice.h" |
| 96 | #include "src/tint/utils/macros/defer.h" |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 97 | #include "src/tint/utils/result/result.h" |
| 98 | #include "src/tint/utils/rtti/castable.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 99 | #include "src/tint/utils/rtti/switch.h" |
Ben Clayton | 425cb42 | 2024-05-01 15:43:55 +0000 | [diff] [blame] | 100 | #include "src/tint/utils/text/styled_text.h" |
Ben Clayton | 710b62f | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 101 | #include "src/tint/utils/text/text_style.h" |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 102 | |
James Price | db46be1 | 2023-08-01 17:15:35 +0000 | [diff] [blame] | 103 | /// If set to 1 then the Tint will dump the IR when validating. |
| 104 | #define TINT_DUMP_IR_WHEN_VALIDATING 0 |
| 105 | #if TINT_DUMP_IR_WHEN_VALIDATING |
| 106 | #include <iostream> |
James Price | 2ee4546 | 2024-05-03 17:37:14 +0000 | [diff] [blame] | 107 | #include "src/tint/utils/text/styled_text_printer.h" |
James Price | db46be1 | 2023-08-01 17:15:35 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
dan sinclair | ce6dffe | 2023-08-14 21:01:40 +0000 | [diff] [blame] | 110 | using namespace tint::core::fluent_types; // NOLINT |
| 111 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 112 | namespace tint::core::ir { |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 113 | |
dan sinclair | fbc4ce7 | 2024-10-21 19:25:39 +0000 | [diff] [blame] | 114 | struct ValidatedType { |
| 115 | const type::Type* ty; |
| 116 | Capabilities caps; |
| 117 | }; |
| 118 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 119 | namespace { |
| 120 | |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 121 | /// @returns the parent block of @p block |
| 122 | const Block* ParentBlockOf(const Block* block) { |
| 123 | if (auto* parent = block->Parent()) { |
| 124 | return parent->Block(); |
| 125 | } |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | /// @returns true if @p block directly or transitively holds the instruction @p inst |
| 130 | bool TransitivelyHolds(const Block* block, const Instruction* inst) { |
| 131 | for (auto* b = inst->Block(); b; b = ParentBlockOf(b)) { |
| 132 | if (b == block) { |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | |
Ryan Harrison | e5c5c65 | 2024-10-01 22:23:03 +0000 | [diff] [blame] | 139 | /// @returns true if @p attr contains both a location and builtin decoration |
Ryan Harrison | ceb3a5c5 | 2024-09-26 21:39:25 +0000 | [diff] [blame] | 140 | bool HasLocationAndBuiltin(const tint::core::IOAttributes& attr) { |
| 141 | return attr.builtin.has_value() && attr.location.has_value(); |
| 142 | } |
| 143 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 144 | /// @returns true if @p attr contains one of location or builtin decoration |
| 145 | bool HasLocationOrBuiltin(const tint::core::IOAttributes& attr) { |
| 146 | return attr.builtin.has_value() || attr.location.has_value(); |
Ryan Harrison | a7b805c | 2024-10-04 18:02:16 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Ryan Harrison | ee7a5b1 | 2024-10-02 22:03:03 +0000 | [diff] [blame] | 149 | /// @return true if @param attr does not have invariant decoration or if it also has position |
| 150 | /// decoration |
| 151 | bool InvariantOnlyIfAlsoPosition(const tint::core::IOAttributes& attr) { |
| 152 | return !attr.invariant || attr.builtin == BuiltinValue::kPosition; |
| 153 | } |
| 154 | |
Ryan Harrison | e5c5c65 | 2024-10-01 22:23:03 +0000 | [diff] [blame] | 155 | /// @returns true if @p ty meets the basic function parameter rules (i.e. one of constructible, |
| 156 | /// pointer, sampler or texture). |
| 157 | /// |
| 158 | /// Note: Does not handle corner cases like if certain capabilities are |
| 159 | /// enabled. |
| 160 | bool IsValidFunctionParamType(const core::type::Type* ty) { |
| 161 | return ty->IsConstructible() || ty->Is<type::Pointer>() || ty->Is<type::Texture>() || |
| 162 | ty->Is<type::Sampler>(); |
| 163 | } |
| 164 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 165 | /// @returns true if @p ty is a non-struct and decorated with @builtin(position), or if it is a |
| 166 | /// struct and one of its members is decorated, otherwise false. |
| 167 | /// @param attr attributes attached to data |
| 168 | /// @param ty type of the data being tested |
| 169 | bool IsPositionPresent(const IOAttributes& attr, const core::type::Type* ty) { |
| 170 | if (auto* ty_struct = ty->As<core::type::Struct>()) { |
| 171 | for (const auto* mem : ty_struct->Members()) { |
| 172 | if (mem->Attributes().builtin == BuiltinValue::kPosition) { |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return attr.builtin == BuiltinValue::kPosition; |
| 180 | } |
| 181 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 182 | /// Utility for running checks on attributes. |
| 183 | /// If the type that the attributes are attached to is a struct, the check is run over the members, |
| 184 | /// otherwise it run on the attributes directly. |
| 185 | /// |
| 186 | /// @param msg_anchor what to associate errors with, i.e. the 'foo' of AddError(foo) |
| 187 | /// @param ty_attr the directly attached attributes |
| 188 | /// @param ty the type of the thing that the attributes are attached to |
| 189 | /// @param is_not_struct_impl has the signature 'void(const MSG_ANCHOR*, const IOAttributes&)' and |
| 190 | /// is called when @p ty is not a struct |
| 191 | /// @param is_struct_impl has the signature 'void(const MSG_ANCHOR*, const IOAttributes&)' and is |
| 192 | /// called when @p ty is a struct |
| 193 | template <typename MSG_ANCHOR, typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 194 | void CheckIOAttributes(const MSG_ANCHOR* msg_anchor, |
| 195 | const IOAttributes& ty_attr, |
| 196 | const core::type::Type* ty, |
| 197 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 198 | IS_STRUCT&& is_struct_impl) { |
| 199 | if (auto* ty_struct = ty->As<core::type::Struct>()) { |
| 200 | for (const auto* mem : ty_struct->Members()) { |
| 201 | is_struct_impl(msg_anchor, mem->Attributes()); |
| 202 | } |
| 203 | } else { |
| 204 | is_not_struct_impl(msg_anchor, ty_attr); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// Helper for calling CheckIOAttributes on a function return |
| 209 | /// @param func function whose return is to be tested |
| 210 | /// See @ref CheckIOAttributes for more details |
| 211 | template <typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 212 | void CheckFunctionReturnAttributes(const Function* func, |
| 213 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 214 | IS_STRUCT&& is_struct_impl) { |
| 215 | CheckIOAttributes(func, func->ReturnAttributes(), func->ReturnType(), |
| 216 | std::forward<IS_NOT_STRUCT>(is_not_struct_impl), |
| 217 | std::forward<IS_STRUCT>(is_struct_impl)); |
| 218 | } |
| 219 | |
| 220 | /// Helper for calling CheckIOAttributes on a function param |
| 221 | /// @param param function param to be tested |
| 222 | /// See @ref CheckIOAttributes for more details |
| 223 | template <typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 224 | void CheckFunctionParamAttributes(const FunctionParam* param, |
| 225 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 226 | IS_STRUCT&& is_struct_impl) { |
| 227 | CheckIOAttributes(param, param->Attributes(), param->Type(), |
| 228 | std::forward<IS_NOT_STRUCT>(is_not_struct_impl), |
| 229 | std::forward<IS_STRUCT>(is_struct_impl)); |
| 230 | } |
| 231 | |
| 232 | /// Utility for running checks on attributes and type. |
| 233 | /// If the type that the attributes are attached to is a struct, the check is run over the members, |
| 234 | /// otherwise it run on the attributes directly. |
| 235 | /// |
| 236 | /// @param msg_anchor what to associate errors with, i.e. the 'foo' of AddError(foo) |
| 237 | /// @param ty_attr the directly attached attributes |
| 238 | /// @param ty the type of the thing that the attributes are attached to |
| 239 | /// @param is_not_struct_impl has the signature 'void(const MSG_ANCHOR*, const IOAttributes&, const |
| 240 | /// core::type::Type* ty)' |
| 241 | /// and is called when @p ty is not a struct |
| 242 | /// @param is_struct_impl has the signature 'void(const MSG_ANCHOR*, const IOAttributes&, const |
| 243 | /// core::type::Type* ty)' |
| 244 | /// and is called when @p ty is a struct |
| 245 | template <typename MSG_ANCHOR, typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 246 | void CheckIOAttributesAndType(const MSG_ANCHOR* msg_anchor, |
| 247 | const IOAttributes& ty_attr, |
| 248 | const core::type::Type* ty, |
| 249 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 250 | IS_STRUCT&& is_struct_impl) { |
| 251 | if (auto* ty_struct = ty->As<core::type::Struct>()) { |
| 252 | for (const auto* mem : ty_struct->Members()) { |
| 253 | is_struct_impl(msg_anchor, mem->Attributes(), mem->Type()); |
| 254 | } |
| 255 | } else { |
| 256 | is_not_struct_impl(msg_anchor, ty_attr, ty); |
| 257 | } |
| 258 | } |
| 259 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 260 | // Wrapper for CheckIOAttributesAndType, when the struct and non-struct impl are the same |
| 261 | /// See @ref IOAttributesAndType for more details |
| 262 | template <typename MSG_ANCHOR, typename IMPL> |
| 263 | void CheckIOAttributesAndType(const MSG_ANCHOR* msg_anchor, |
| 264 | const IOAttributes& ty_attr, |
| 265 | const core::type::Type* ty, |
| 266 | IMPL&& impl) { |
| 267 | CheckIOAttributesAndType(msg_anchor, ty_attr, ty, impl, impl); |
| 268 | } |
| 269 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 270 | /// Helper for calling IOAttributesAndType on a function param |
| 271 | /// @param param function param to be tested |
| 272 | /// See @ref IOAttributesAndType for more details |
| 273 | template <typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 274 | void CheckFunctionParamAttributesAndType(const FunctionParam* param, |
| 275 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 276 | IS_STRUCT&& is_struct_impl) { |
| 277 | CheckIOAttributesAndType(param, param->Attributes(), param->Type(), |
| 278 | std::forward<IS_NOT_STRUCT>(is_not_struct_impl), |
| 279 | std::forward<IS_STRUCT>(is_struct_impl)); |
| 280 | } |
| 281 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 282 | /// Helper for calling IOAttributesAndType on a function param |
| 283 | /// @param param function param to be tested |
| 284 | /// See @ref IOAttributesAndType for more details |
| 285 | template <typename IMPL> |
| 286 | void CheckFunctionParamAttributesAndType(const FunctionParam* param, IMPL&& impl) { |
| 287 | CheckIOAttributesAndType(param, param->Attributes(), param->Type(), std::forward<IMPL>(impl)); |
| 288 | } |
| 289 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 290 | /// Helper for calling IOAttributesAndType on a function return |
| 291 | /// @param func function's return to be tested |
| 292 | /// See @ref IOAttributesAndType for more details |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 293 | template <typename IS_NOT_STRUCT, typename IS_STRUCT> |
| 294 | void CheckFunctionReturnAttributesAndType(const Function* func, |
| 295 | IS_NOT_STRUCT&& is_not_struct_impl, |
| 296 | IS_STRUCT&& is_struct_impl) { |
| 297 | CheckIOAttributesAndType(func, func->ReturnAttributes(), func->ReturnType(), |
| 298 | std::forward<IS_NOT_STRUCT>(is_not_struct_impl), |
| 299 | std::forward<IS_STRUCT>(is_struct_impl)); |
| 300 | } |
| 301 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 302 | /// Helper for calling IOAttributesAndType on a function return |
| 303 | /// @param func function's return to be tested |
| 304 | /// See @ref IOAttributesAndType for more details |
| 305 | template <typename IMPL> |
| 306 | void CheckFunctionReturnAttributesAndType(const Function* func, IMPL&& impl) { |
| 307 | CheckIOAttributesAndType(func, func->ReturnAttributes(), func->ReturnType(), |
| 308 | std::forward<IMPL>(impl)); |
| 309 | } |
| 310 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 311 | /// A BuiltinChecker is the interface used to check that a usage of a builtin attribute meets the |
| 312 | /// basic spec rules, i.e. correct shader stage, data type, and IO direction. |
| 313 | /// It does not test more sophisticated rules like location and builtins being mutually exclusive or |
| 314 | /// the correct capabilities are enabled. |
| 315 | struct BuiltinChecker { |
| 316 | /// User friendly name to print in logging messages |
| 317 | const char* name; |
| 318 | |
| 319 | /// What type of entry point is this builtin legal for |
| 320 | EnumSet<Function::PipelineStage> stages; |
| 321 | |
| 322 | enum IODirection : uint8_t { kInput, kOutput }; |
| 323 | /// Is this expected to be a param going into the entry point or a result coming out |
| 324 | IODirection direction; |
| 325 | |
| 326 | /// Implements logic for checking if the given type is valid or not |
| 327 | using TypeCheckFn = bool(const core::type::Type* type); |
| 328 | |
| 329 | /// @see #TypeCheckFn |
| 330 | TypeCheckFn* const type_check; |
| 331 | |
| 332 | /// Message that should logged if the type check fails |
| 333 | const char* type_error; |
| 334 | }; |
| 335 | |
| 336 | std::string_view ToString(BuiltinChecker::IODirection value) { |
| 337 | switch (value) { |
| 338 | case BuiltinChecker::IODirection::kInput: |
| 339 | return "input"; |
| 340 | case BuiltinChecker::IODirection::kOutput: |
| 341 | return "output"; |
| 342 | } |
| 343 | TINT_ICE() << "Unknown enum passed to ToString(BuiltinChecker::IODirection)"; |
| 344 | } |
| 345 | |
| 346 | constexpr BuiltinChecker kPointSizeChecker{ |
| 347 | /* name */ "__point_size", |
| 348 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kVertex), |
| 349 | /* direction */ BuiltinChecker::IODirection::kOutput, |
| 350 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::F32>(); }, |
| 351 | /* type_error */ "__point_size must be a f32", |
| 352 | }; |
| 353 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 354 | constexpr BuiltinChecker kFragDepthChecker{ |
| 355 | /* name */ "frag_depth", |
| 356 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kFragment), |
| 357 | /* direction */ BuiltinChecker::IODirection::kOutput, |
| 358 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::F32>(); }, |
| 359 | /* type_error */ "frag_depth must be a f32", |
| 360 | }; |
| 361 | |
| 362 | constexpr BuiltinChecker kFrontFacingChecker{ |
| 363 | /* name */ "front_facing", |
| 364 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kFragment), |
| 365 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 366 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::Bool>(); }, |
| 367 | /* type_error */ "front_facing must be a bool", |
| 368 | }; |
| 369 | |
| 370 | constexpr BuiltinChecker kGlobalInvocationIdChecker{ |
| 371 | /* name */ "global_invocation_id", |
| 372 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kCompute), |
| 373 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 374 | /* type_check */ |
| 375 | [](const core::type::Type* ty) -> bool { |
| 376 | return ty->IsUnsignedIntegerVector() && ty->Elements().count == 3; |
| 377 | }, |
| 378 | /* type_error */ "global_invocation_id must be an vec3<u32>", |
| 379 | }; |
| 380 | |
| 381 | constexpr BuiltinChecker kInstanceIndexChecker{ |
| 382 | /* name */ "instance_index", |
| 383 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kVertex), |
| 384 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 385 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 386 | /* type_error */ "instance_index must be an u32", |
| 387 | }; |
| 388 | |
| 389 | constexpr BuiltinChecker kLocalInvocationIdChecker{ |
| 390 | /* name */ "local_invocation_id", |
| 391 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kCompute), |
| 392 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 393 | /* type_check */ |
| 394 | [](const core::type::Type* ty) -> bool { |
| 395 | return ty->IsUnsignedIntegerVector() && ty->Elements().count == 3; |
| 396 | }, |
| 397 | /* type_error */ "local_invocation_id must be an vec3<u32>", |
| 398 | }; |
| 399 | |
| 400 | constexpr BuiltinChecker kLocalInvocationIndexChecker{ |
| 401 | /* name */ "local_invocation_index", |
| 402 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kCompute), |
| 403 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 404 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 405 | /* type_error */ "local_invocation_index must be an u32", |
| 406 | }; |
| 407 | |
| 408 | constexpr BuiltinChecker kNumWorkgroupsChecker{ |
| 409 | /* name */ "num_workgroups", |
| 410 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kCompute), |
| 411 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 412 | /* type_check */ |
| 413 | [](const core::type::Type* ty) -> bool { |
| 414 | return ty->IsUnsignedIntegerVector() && ty->Elements().count == 3; |
| 415 | }, |
| 416 | /* type_error */ "num_workgroups must be an vec3<u32>", |
| 417 | }; |
| 418 | |
| 419 | constexpr BuiltinChecker kSampleIndexChecker{ |
| 420 | /* name */ "sample_index", |
| 421 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kFragment), |
| 422 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 423 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 424 | /* type_error */ "sample_index must be an u32", |
| 425 | }; |
| 426 | |
| 427 | constexpr BuiltinChecker kSubgroupInvocationIdChecker{ |
| 428 | /* name */ "subgroup_invocation_id", |
| 429 | /* stages */ |
| 430 | EnumSet<Function::PipelineStage>(Function::PipelineStage::kFragment, |
| 431 | Function::PipelineStage::kCompute), |
| 432 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 433 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 434 | /* type_error */ "subgroup_invocation_id must be an u32", |
| 435 | }; |
| 436 | |
| 437 | constexpr BuiltinChecker kSubgroupSizeChecker{ |
| 438 | /* name */ "subgroup_size", |
| 439 | /* stages */ |
| 440 | EnumSet<Function::PipelineStage>(Function::PipelineStage::kFragment, |
| 441 | Function::PipelineStage::kCompute), |
| 442 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 443 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 444 | /* type_error */ "subgroup_size must be an u32", |
| 445 | }; |
| 446 | |
| 447 | constexpr BuiltinChecker kVertexIndexChecker{ |
| 448 | /* name */ "vertex_index", |
| 449 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kVertex), |
| 450 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 451 | /* type_check */ [](const core::type::Type* ty) -> bool { return ty->Is<core::type::U32>(); }, |
| 452 | /* type_error */ "vertex_index must be an u32", |
| 453 | }; |
| 454 | |
| 455 | constexpr BuiltinChecker kWorkgroupIdChecker{ |
| 456 | /* name */ "workgroup_id", |
| 457 | /* stages */ EnumSet<Function::PipelineStage>(Function::PipelineStage::kCompute), |
| 458 | /* direction */ BuiltinChecker::IODirection::kInput, |
| 459 | /* type_check */ |
| 460 | [](const core::type::Type* ty) -> bool { |
| 461 | return ty->IsUnsignedIntegerVector() && ty->Elements().count == 3; |
| 462 | }, |
| 463 | /* type_error */ "workgroup_id must be an vec3<u32>", |
| 464 | }; |
| 465 | |
| 466 | /// @returns an appropriate BuiltInCheck for @p builtin, ICEs when one isn't defined |
| 467 | const BuiltinChecker& BuiltinCheckerFor(BuiltinValue builtin) { |
| 468 | switch (builtin) { |
| 469 | case BuiltinValue::kPointSize: |
| 470 | return kPointSizeChecker; |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 471 | case BuiltinValue::kFragDepth: |
| 472 | return kFragDepthChecker; |
| 473 | case BuiltinValue::kFrontFacing: |
| 474 | return kFrontFacingChecker; |
| 475 | case BuiltinValue::kGlobalInvocationId: |
| 476 | return kGlobalInvocationIdChecker; |
| 477 | case BuiltinValue::kInstanceIndex: |
| 478 | return kInstanceIndexChecker; |
| 479 | case BuiltinValue::kLocalInvocationId: |
| 480 | return kLocalInvocationIdChecker; |
| 481 | case BuiltinValue::kLocalInvocationIndex: |
| 482 | return kLocalInvocationIndexChecker; |
| 483 | case BuiltinValue::kNumWorkgroups: |
| 484 | return kNumWorkgroupsChecker; |
| 485 | case BuiltinValue::kSampleIndex: |
| 486 | return kSampleIndexChecker; |
| 487 | case BuiltinValue::kSubgroupInvocationId: |
| 488 | return kSubgroupInvocationIdChecker; |
| 489 | case BuiltinValue::kSubgroupSize: |
| 490 | return kSubgroupSizeChecker; |
| 491 | case BuiltinValue::kVertexIndex: |
| 492 | return kVertexIndexChecker; |
| 493 | case BuiltinValue::kWorkgroupId: |
| 494 | return kWorkgroupIdChecker; |
| 495 | case BuiltinValue::kPosition: |
| 496 | TINT_ICE() << "BuiltinValue::kPosition requires special handling, so does not have a " |
| 497 | "checker defined"; |
| 498 | case BuiltinValue::kSampleMask: |
| 499 | TINT_ICE() << "BuiltinValue::kSampleMask requires special handling, so does not have a " |
| 500 | "checker defined"; |
| 501 | default: |
| 502 | TINT_ICE() << builtin << " is does not have a checker defined for it"; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /// Validates the basic spec rules for @builtin(position) usage |
| 507 | /// @param stage the shader stage the builtin is being used |
| 508 | /// @param is_input the IO direction of usage, true if input, false if output |
| 509 | /// @param ty the data type being decorated by the builtin |
| 510 | /// @returns Success if a valid usage, or reason for invalidity in Failure |
| 511 | Result<SuccessType, std::string> ValidatePositionBuiltIn(Function::PipelineStage stage, |
| 512 | bool is_input, |
| 513 | const core::type::Type* ty) { |
| 514 | if (stage != Function::PipelineStage::kVertex && stage != Function::PipelineStage::kFragment) { |
| 515 | return std::string("position must be used in a fragment or vertex shader entry point"); |
| 516 | } |
| 517 | |
| 518 | if (stage == Function::PipelineStage::kVertex && is_input) { |
| 519 | return std::string("position must be an output for a vertex entry point"); |
| 520 | } |
| 521 | |
| 522 | if (stage == Function::PipelineStage::kFragment && !is_input) { |
| 523 | return std::string("position must be an input for a fragment entry point"); |
| 524 | } |
| 525 | |
| 526 | if (!ty->IsFloatVector() || ty->Elements().count != 4 || |
| 527 | !ty->Element(0)->Is<core::type::F32>()) { |
| 528 | return std::string("position must be an vec4<f32>"); |
| 529 | } |
| 530 | |
| 531 | return Success; |
| 532 | } |
| 533 | |
| 534 | /// Validates the basic spec rules for @builtin(sample_mask) usage |
| 535 | /// @param stage the shader stage the builtin is being used |
| 536 | /// @param ty the data type being decorated by the builtin |
| 537 | /// @returns Success if a valid usage, or reason for invalidity in Failure |
| 538 | Result<SuccessType, std::string> ValidateSampleMaskBuiltIn(Function::PipelineStage stage, |
| 539 | const core::type::Type* ty) { |
| 540 | if (stage != Function::PipelineStage::kFragment) { |
| 541 | return std::string("sample_mask must be used in a fragment entry point"); |
| 542 | } |
| 543 | |
| 544 | if (!ty->Is<core::type::U32>()) { |
| 545 | return std::string("sample_mask must be an u32"); |
| 546 | } |
| 547 | |
| 548 | return Success; |
| 549 | } |
| 550 | |
Antonio Maiorano | 66652df | 2024-10-22 23:20:27 +0000 | [diff] [blame] | 551 | /// Validates the basic spec rules for @builtin(clip_distance) usage |
| 552 | /// @param stage the shader stage the builtin is being used |
| 553 | /// @param is_input the IO direction of usage, true if input, false if output |
| 554 | /// @param capabilities the optional capabilities that are allowed |
| 555 | /// @param ty the data type being decorated by the builtin |
| 556 | /// @returns Success if a valid usage, or reason for invalidity in Failure |
| 557 | Result<SuccessType, std::string> ValidateBuiltinClipDistances(Function::PipelineStage stage, |
| 558 | bool is_input, |
| 559 | const Capabilities& capabilities, |
| 560 | const core::type::Type* ty) { |
| 561 | if (stage != Function::PipelineStage::kVertex) { |
| 562 | return std::string("clip_distances must be used in a vertex shader entry point"); |
| 563 | } |
| 564 | |
| 565 | if (is_input) { |
| 566 | return std::string("clip_distances must be an output of a shader entry point"); |
| 567 | } |
| 568 | |
| 569 | auto is_valid_array = [&] { |
| 570 | const auto elems = ty->Elements(); |
| 571 | return elems.type && elems.type->Is<core::type::F32>() && elems.count <= 8; |
| 572 | }; |
| 573 | |
| 574 | if (capabilities.Contains(Capability::kAllowClipDistancesOnF32)) { |
| 575 | if (!ty->Is<core::type::F32>() && !is_valid_array()) { |
| 576 | return std::string("clip_distances must be an f32 or an array<f32, N>, where N <= 8"); |
| 577 | } |
| 578 | } else if (!is_valid_array()) { |
| 579 | return std::string("clip_distances must be an array<f32, N>, where N <= 8"); |
| 580 | } |
| 581 | |
| 582 | return Success; |
| 583 | } |
| 584 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 585 | /// Validates the basic spec rules for builtin usage |
| 586 | /// @param builtin the builtin to test |
| 587 | /// @param stage the shader stage the builtin is being used |
| 588 | /// @param is_input the IO direction of usage, true if input, false if output |
| 589 | /// @param ty the data type being decorated by the builtin |
| 590 | /// @returns Success if a valid usage, or reason for invalidity in Failure |
| 591 | Result<SuccessType, std::string> ValidateBuiltIn(BuiltinValue builtin, |
| 592 | Function::PipelineStage stage, |
| 593 | bool is_input, |
Antonio Maiorano | 66652df | 2024-10-22 23:20:27 +0000 | [diff] [blame] | 594 | const Capabilities& capabilities, |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 595 | const core::type::Type* ty) { |
| 596 | // This is not an entry point function, either it is dead code and thus never called, or any |
| 597 | // issues will be detected when validating the calling entry point. |
| 598 | if (stage == Function::PipelineStage::kUndefined) { |
| 599 | return Success; |
| 600 | } |
| 601 | |
| 602 | // Some builtins have multiple contexts that they are valid in, so have special handling |
| 603 | // instead of making the checker/lookup table more complex. |
| 604 | switch (builtin) { |
| 605 | case BuiltinValue::kPosition: |
| 606 | return ValidatePositionBuiltIn(stage, is_input, ty); |
| 607 | case BuiltinValue::kSampleMask: |
| 608 | return ValidateSampleMaskBuiltIn(stage, ty); |
Antonio Maiorano | 66652df | 2024-10-22 23:20:27 +0000 | [diff] [blame] | 609 | case BuiltinValue::kClipDistances: |
| 610 | return ValidateBuiltinClipDistances(stage, is_input, capabilities, ty); |
| 611 | default: |
| 612 | break; |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | const auto& checker = BuiltinCheckerFor(builtin); |
| 616 | std::stringstream msg; |
| 617 | if (!checker.stages.Contains(stage)) { |
| 618 | auto stages_size = checker.stages.Size(); |
| 619 | switch (stages_size) { |
| 620 | case 1: |
| 621 | msg << checker.name << " must be used in a " << ToString(*checker.stages.begin()) |
| 622 | << " shader entry point"; |
| 623 | break; |
| 624 | case 2: |
| 625 | msg << checker.name << " must be used in a " << ToString(*checker.stages.begin()) |
| 626 | << " or " << ToString(*(++checker.stages.begin())) << " shader entry point"; |
| 627 | break; |
| 628 | default: |
| 629 | TINT_ICE() << "Unexpected number of stages set, " << stages_size; |
| 630 | } |
| 631 | return msg.str(); |
| 632 | } |
| 633 | |
| 634 | auto io_direction = |
| 635 | is_input ? BuiltinChecker::IODirection::kInput : BuiltinChecker::IODirection::kOutput; |
| 636 | if (io_direction != checker.direction) { |
| 637 | msg << checker.name << " must be an " << ToString(checker.direction) |
| 638 | << " of a shader entry point"; |
| 639 | return msg.str(); |
| 640 | } |
| 641 | |
| 642 | if (!checker.type_check(ty)) { |
| 643 | return std::string(checker.type_error); |
| 644 | } |
| 645 | |
| 646 | return Success; |
| 647 | } |
| 648 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 649 | /// The core IR validator. |
| 650 | class Validator { |
| 651 | public: |
| 652 | /// Create a core validator |
| 653 | /// @param mod the module to be validated |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 654 | /// @param capabilities the optional capabilities that are allowed |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 655 | explicit Validator(const Module& mod, Capabilities capabilities); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 656 | |
| 657 | /// Destructor |
| 658 | ~Validator(); |
| 659 | |
| 660 | /// Runs the validator over the module provided during construction |
Ben Clayton | 16fb254 | 2023-09-25 11:43:19 +0000 | [diff] [blame] | 661 | /// @returns success or failure |
| 662 | Result<SuccessType> Run(); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 663 | |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 664 | private: |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 665 | /// Runs validation to confirm the structural soundness of the module. |
| 666 | /// Also runs any validation that is not dependent on the entire module being |
| 667 | /// sound and sets up data structures for later checks. |
| 668 | void RunStructuralSoundnessChecks(); |
| 669 | |
| 670 | /// Checks that there are no orphaned instructions |
| 671 | /// Depends on CheckStructuralSoundness() having previously been run |
| 672 | void CheckForOrphanedInstructions(); |
| 673 | |
| 674 | /// Checks that there are no discards called by non-fragment entrypoints |
| 675 | /// Depends on CheckStructuralSoundness() having previously been run |
| 676 | void CheckForNonFragmentDiscards(); |
| 677 | |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 678 | /// @returns the IR disassembly, performing a disassemble if this is the first call. |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 679 | ir::Disassembler& Disassemble(); |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 680 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 681 | /// Adds an error for the @p inst and highlights the instruction in the disassembly |
| 682 | /// @param inst the instruction |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 683 | /// @returns the diagnostic |
| 684 | diag::Diagnostic& AddError(const Instruction* inst); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 685 | |
| 686 | /// Adds an error for the @p inst operand at @p idx and highlights the operand in the |
| 687 | /// disassembly |
Ben Clayton | 425cb42 | 2024-05-01 15:43:55 +0000 | [diff] [blame] | 688 | /// @param inst the instruction |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 689 | /// @param idx the operand index |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 690 | /// @returns the diagnostic |
| 691 | diag::Diagnostic& AddError(const Instruction* inst, size_t idx); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 692 | |
| 693 | /// Adds an error for the @p inst result at @p idx and highlgihts the result in the disassembly |
| 694 | /// @param inst the instruction |
| 695 | /// @param idx the result index |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 696 | /// @returns the diagnostic |
| 697 | diag::Diagnostic& AddResultError(const Instruction* inst, size_t idx); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 698 | |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 699 | /// Adds an error for the @p block and highlights the block header in the disassembly |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 700 | /// @param blk the block |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 701 | /// @returns the diagnostic |
| 702 | diag::Diagnostic& AddError(const Block* blk); |
| 703 | |
James Price | 4f491bf | 2024-04-24 21:15:04 +0000 | [diff] [blame] | 704 | /// Adds an error for the @p param and highlights the parameter in the disassembly |
| 705 | /// @param param the parameter |
| 706 | /// @returns the diagnostic |
| 707 | diag::Diagnostic& AddError(const BlockParam* param); |
| 708 | |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 709 | /// Adds an error for the @p func and highlights the function in the disassembly |
| 710 | /// @param func the function |
| 711 | /// @returns the diagnostic |
| 712 | diag::Diagnostic& AddError(const Function* func); |
| 713 | |
| 714 | /// Adds an error for the @p param and highlights the parameter in the disassembly |
| 715 | /// @param param the parameter |
| 716 | /// @returns the diagnostic |
| 717 | diag::Diagnostic& AddError(const FunctionParam* param); |
| 718 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 719 | /// Adds an error the @p block and highlights the block header in the disassembly |
| 720 | /// @param src the source lines to highlight |
| 721 | /// @returns the diagnostic |
| 722 | diag::Diagnostic& AddError(Source src); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 723 | |
| 724 | /// Adds a note to @p inst and highlights the instruction in the disassembly |
| 725 | /// @param inst the instruction |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 726 | diag::Diagnostic& AddNote(const Instruction* inst); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 727 | |
James Price | e5380b7 | 2024-04-24 20:52:59 +0000 | [diff] [blame] | 728 | /// Adds a note to @p func and highlights the function in the disassembly |
| 729 | /// @param func the function |
| 730 | diag::Diagnostic& AddNote(const Function* func); |
| 731 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 732 | /// Adds a note to @p inst for operand @p idx and highlights the operand in the disassembly |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 733 | /// @param inst the instruction |
| 734 | /// @param idx the operand index |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 735 | diag::Diagnostic& AddOperandNote(const Instruction* inst, size_t idx); |
| 736 | |
| 737 | /// Adds a note to @p inst for result @p idx and highlights the result in the disassembly |
| 738 | /// @param inst the instruction |
| 739 | /// @param idx the result index |
| 740 | diag::Diagnostic& AddResultNote(const Instruction* inst, size_t idx); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 741 | |
| 742 | /// Adds a note to @p blk and highlights the block in the disassembly |
| 743 | /// @param blk the block |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 744 | diag::Diagnostic& AddNote(const Block* blk); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 745 | |
| 746 | /// Adds a note to the diagnostics |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 747 | /// @param src the source lines to highlight |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 748 | diag::Diagnostic& AddNote(Source src = {}); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 749 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 750 | /// Adds a note to the diagnostics highlighting where the value instruction or block is |
| 751 | /// declared, if it has a source location. |
| 752 | /// @param decl the value instruction or block |
| 753 | void AddDeclarationNote(const CastableBase* decl); |
| 754 | |
| 755 | /// Adds a note to the diagnostics highlighting where the block is declared, if it has a source |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 756 | /// location. |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 757 | /// @param block the block |
| 758 | void AddDeclarationNote(const Block* block); |
| 759 | |
| 760 | /// Adds a note to the diagnostics highlighting where the block parameter is declared, if it |
| 761 | /// has a source location. |
| 762 | /// @param param the block parameter |
| 763 | void AddDeclarationNote(const BlockParam* param); |
| 764 | |
| 765 | /// Adds a note to the diagnostics highlighting where the function is declared, if it has a |
| 766 | /// source location. |
| 767 | /// @param fn the function |
| 768 | void AddDeclarationNote(const Function* fn); |
| 769 | |
| 770 | /// Adds a note to the diagnostics highlighting where the function parameter is declared, if it |
| 771 | /// has a source location. |
| 772 | /// @param param the function parameter |
| 773 | void AddDeclarationNote(const FunctionParam* param); |
| 774 | |
| 775 | /// Adds a note to the diagnostics highlighting where the instruction is declared, if it has a |
| 776 | /// source location. |
| 777 | /// @param inst the inst |
| 778 | void AddDeclarationNote(const Instruction* inst); |
| 779 | |
| 780 | /// Adds a note to the diagnostics highlighting where instruction result was declared, if it has |
| 781 | /// a source location. |
| 782 | /// @param res the res |
| 783 | void AddDeclarationNote(const InstructionResult* res); |
| 784 | |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 785 | /// @param decl the type, value, instruction or block to get the name for |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 786 | /// @returns the styled name for the given value, instruction or block |
| 787 | StyledText NameOf(const CastableBase* decl); |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 788 | |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 789 | // @param ty the type to get the name for |
| 790 | /// @returns the styled name for the given type |
| 791 | StyledText NameOf(const type::Type* ty); |
| 792 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 793 | /// @param v the value to get the name for |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 794 | /// @returns the styled name for the given value |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 795 | StyledText NameOf(const Value* v); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 796 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 797 | /// @param inst the instruction to get the name for |
| 798 | /// @returns the styled name for the given instruction |
| 799 | StyledText NameOf(const Instruction* inst); |
| 800 | |
| 801 | /// @param block the block to get the name for |
| 802 | /// @returns the styled name for the given block |
| 803 | StyledText NameOf(const Block* block); |
| 804 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 805 | /// Checks the given result is not null and its type is not null |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 806 | /// @param inst the instruction |
| 807 | /// @param idx the result index |
| 808 | /// @returns true if the result is not null |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 809 | bool CheckResult(const Instruction* inst, size_t idx); |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 810 | |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 811 | /// Checks the results (and their types) for @p inst are not null. If count is specified then |
| 812 | /// number of results is checked to be exact. |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 813 | /// @param inst the instruction |
| 814 | /// @param count the number of results to check |
| 815 | /// @returns true if the results count is as expected and none are null |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 816 | bool CheckResults(const ir::Instruction* inst, std::optional<size_t> count); |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 817 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 818 | /// Checks the given operand is not null and its type is not null |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 819 | /// @param inst the instruction |
| 820 | /// @param idx the operand index |
| 821 | /// @returns true if the operand is not null |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 822 | bool CheckOperand(const Instruction* inst, size_t idx); |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 823 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 824 | /// Checks the number of operands provided to @p inst and that none of them are null. Also |
| 825 | /// checks that the types for the operands are not null |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 826 | /// @param inst the instruction |
| 827 | /// @param min_count the minimum number of operands to expect |
| 828 | /// @param max_count the maximum number of operands to expect, if not set, than only the minimum |
| 829 | /// number is checked. |
| 830 | /// @returns true if the number of operands is in the expected range and none are null |
| 831 | bool CheckOperands(const ir::Instruction* inst, |
| 832 | size_t min_count, |
| 833 | std::optional<size_t> max_count); |
| 834 | |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 835 | /// Checks the operands (and their types) for @p inst are not null. If count is specified then |
| 836 | /// number of operands is checked to be exact. |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 837 | /// @param inst the instruction |
| 838 | /// @param count the number of operands to check |
| 839 | /// @returns true if the operands count is as expected and none are null |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 840 | bool CheckOperands(const ir::Instruction* inst, std::optional<size_t> count); |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 841 | |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 842 | /// Checks the number of results for @p inst are exactly equal to @p num_results and the number |
| 843 | /// of operands is correctly. Both results and operands are confirmed to be non-null. |
| 844 | /// @param inst the instruction |
| 845 | /// @param num_results expected number of results for the instruction |
| 846 | /// @param min_operands the minimum number of operands to expect |
| 847 | /// @param max_operands the maximum number of operands to expect, if not set, than only the |
| 848 | /// minimum number is checked. |
| 849 | /// @returns true if the result and operand counts are as expected and none are null |
| 850 | bool CheckResultsAndOperandRange(const ir::Instruction* inst, |
| 851 | size_t num_results, |
| 852 | size_t min_operands, |
| 853 | std::optional<size_t> max_operands); |
| 854 | |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 855 | /// Checks the number of results and operands for @p inst are exactly equal to num_results |
| 856 | /// and num_operands, respectively, and that none of them are null. |
| 857 | /// @param inst the instruction |
| 858 | /// @param num_results expected number of results for the instruction |
| 859 | /// @param num_operands expected number of operands for the instruction |
| 860 | /// @returns true if the result and operand counts are as expected and none are null |
| 861 | bool CheckResultsAndOperands(const ir::Instruction* inst, |
| 862 | size_t num_results, |
| 863 | size_t num_operands); |
| 864 | |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 865 | /// Checks that @p type does not use any types that are prohibited by the target capabilities. |
| 866 | /// @param type the type |
| 867 | /// @param diag a function that creates an error diagnostic for the source of the type |
| 868 | /// @param ignore_caps a set of capabilities to ignore for this check |
| 869 | void CheckType(const core::type::Type* type, |
| 870 | std::function<diag::Diagnostic&()> diag, |
| 871 | Capabilities ignore_caps = {}); |
| 872 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 873 | /// Validates the root block |
| 874 | /// @param blk the block |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 875 | void CheckRootBlock(const Block* blk); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 876 | |
| 877 | /// Validates the given function |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 878 | /// @param func the function to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 879 | void CheckFunction(const Function* func); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 880 | |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 881 | /// Validates the specific function as a vertex entry point |
| 882 | /// @param ep the function to validate |
| 883 | void CheckVertexEntryPoint(const Function* ep); |
| 884 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 885 | /// @returns a function that validates rules for invariant decorations |
| 886 | /// @param err error message to log when check fails |
| 887 | template <typename MSG_ANCHOR> |
| 888 | auto CheckInvariantFunc(const std::string& err) { |
| 889 | return [this, err](const MSG_ANCHOR* msg_anchor, const IOAttributes& attr) { |
| 890 | if (!InvariantOnlyIfAlsoPosition(attr)) { |
| 891 | AddError(msg_anchor) << err; |
| 892 | } |
| 893 | }; |
| 894 | } |
| 895 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 896 | /// @returns a function that validates builtins on function params |
| 897 | auto CheckBuiltinFunctionParam(const std::string& err) { |
| 898 | return [this, err](const FunctionParam* param, const IOAttributes& attr, |
| 899 | const type::Type* ty) { |
| 900 | if (!attr.builtin.has_value()) { |
| 901 | return; |
| 902 | } |
Antonio Maiorano | 66652df | 2024-10-22 23:20:27 +0000 | [diff] [blame] | 903 | auto result = ValidateBuiltIn(attr.builtin.value(), param->Function()->Stage(), true, |
| 904 | capabilities_, ty); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 905 | if (result != Success) { |
| 906 | AddError(param) << err << result.Failure(); |
| 907 | } |
| 908 | }; |
| 909 | } |
| 910 | |
| 911 | /// @returns a function that validates builtins on function returns |
| 912 | auto CheckBuiltinFunctionReturn(const std::string& err) { |
| 913 | return [this, err](const Function* func, const IOAttributes& attr, const type::Type* ty) { |
| 914 | if (!attr.builtin.has_value()) { |
| 915 | return; |
| 916 | } |
Antonio Maiorano | 66652df | 2024-10-22 23:20:27 +0000 | [diff] [blame] | 917 | auto result = |
| 918 | ValidateBuiltIn(attr.builtin.value(), func->Stage(), false, capabilities_, ty); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 919 | if (result != Success) { |
| 920 | AddError(func) << err << result.Failure(); |
| 921 | } |
| 922 | }; |
| 923 | } |
| 924 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 925 | /// @returns a function that validates that location and builtin attributes are not present at |
| 926 | /// the same time |
| 927 | /// @param err error message to log when check fails |
| 928 | template <typename MSG_ANCHOR> |
| 929 | auto CheckDoesNotHaveBothLocationAndBuiltinFunc(const std::string& err) { |
| 930 | return [this, err](const MSG_ANCHOR* msg_anchor, const IOAttributes& attr) { |
| 931 | if (HasLocationAndBuiltin(attr)) { |
| 932 | AddError(msg_anchor) << err; |
| 933 | } |
| 934 | }; |
| 935 | } |
| 936 | |
| 937 | /// @returns a function that validates that either a location or builtin attribute are present |
| 938 | /// @param err error message to log when check fails |
| 939 | template <typename MSG_ANCHOR> |
| 940 | auto CheckHasLocationOrBuiltinFunc(const std::string& err) { |
| 941 | return [this, err](const MSG_ANCHOR* msg_anchor, const IOAttributes& attr) { |
| 942 | if (!HasLocationOrBuiltin(attr)) { |
| 943 | AddError(msg_anchor) << err; |
| 944 | } |
| 945 | }; |
| 946 | } |
| 947 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 948 | /// @returns a function that validates that type is bool iff decorated with |
| 949 | /// @builtin(front_facing) |
| 950 | /// @param err error message to log when check fails |
| 951 | template <typename MSG_ANCHOR> |
| 952 | auto CheckFrontFacingIfBoolFunc(const std::string& err) { |
| 953 | return [this, err](const MSG_ANCHOR* msg_anchor, const IOAttributes& attr, |
| 954 | const type::Type* ty) { |
| 955 | if (ty->Is<core::type::Bool>() && attr.builtin != BuiltinValue::kFrontFacing) { |
| 956 | AddError(msg_anchor) << err; |
| 957 | } |
| 958 | }; |
| 959 | } |
| 960 | |
| 961 | /// @returns a function that validates that type is not bool |
| 962 | /// @param err error message to log when check fails |
| 963 | template <typename MSG_ANCHOR> |
| 964 | auto CheckNotBool(const std::string& err) { |
| 965 | return [this, err](const MSG_ANCHOR* msg_anchor, [[maybe_unused]] const IOAttributes& attr, |
| 966 | const type::Type* ty) { |
| 967 | if (ty->Is<core::type::Bool>()) { |
| 968 | AddError(msg_anchor) << err; |
| 969 | } |
| 970 | }; |
| 971 | } |
| 972 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 973 | /// Validates the given instruction |
| 974 | /// @param inst the instruction to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 975 | void CheckInstruction(const Instruction* inst); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 976 | |
| 977 | /// Validates the given var |
| 978 | /// @param var the var to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 979 | void CheckVar(const Var* var); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 980 | |
| 981 | /// Validates the given let |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 982 | /// @param l the let to validate |
| 983 | void CheckLet(const Let* l); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 984 | |
| 985 | /// Validates the given call |
| 986 | /// @param call the call to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 987 | void CheckCall(const Call* call); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 988 | |
Ryan Harrison | d8b7465 | 2024-08-12 23:46:57 +0000 | [diff] [blame] | 989 | /// Validates the given bitcast |
| 990 | /// @param bitcast the bitcast to validate |
| 991 | void CheckBitcast(const Bitcast* bitcast); |
| 992 | |
Ben Clayton | 6a56e27 | 2023-09-06 02:01:07 +0000 | [diff] [blame] | 993 | /// Validates the given builtin call |
| 994 | /// @param call the call to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 995 | void CheckBuiltinCall(const BuiltinCall* call); |
Ben Clayton | 6a56e27 | 2023-09-06 02:01:07 +0000 | [diff] [blame] | 996 | |
James Price | c34ca5f | 2024-06-11 18:24:06 +0000 | [diff] [blame] | 997 | /// Validates the given member builtin call |
| 998 | /// @param call the member call to validate |
| 999 | void CheckMemberBuiltinCall(const MemberBuiltinCall* call); |
| 1000 | |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 1001 | /// Validates the given construct |
| 1002 | /// @param construct the construct to validate |
| 1003 | void CheckConstruct(const Construct* construct); |
| 1004 | |
Ryan Harrison | f9761ac | 2024-08-08 18:56:30 +0000 | [diff] [blame] | 1005 | /// Validates the given convert |
| 1006 | /// @param convert the convert to validate |
| 1007 | void CheckConvert(const Convert* convert); |
| 1008 | |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 1009 | /// Validates the given discard |
| 1010 | /// @note Does not validate that the discard is in a fragment shader, that |
| 1011 | /// needs to be handled later in the validation. |
| 1012 | /// @param discard the discard to validate |
| 1013 | void CheckDiscard(const Discard* discard); |
| 1014 | |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1015 | /// Validates the given user call |
| 1016 | /// @param call the call to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1017 | void CheckUserCall(const UserCall* call); |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1018 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1019 | /// Validates the given access |
| 1020 | /// @param a the access to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1021 | void CheckAccess(const Access* a); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1022 | |
| 1023 | /// Validates the given binary |
| 1024 | /// @param b the binary to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1025 | void CheckBinary(const Binary* b); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1026 | |
| 1027 | /// Validates the given unary |
| 1028 | /// @param u the unary to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1029 | void CheckUnary(const Unary* u); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1030 | |
| 1031 | /// Validates the given if |
| 1032 | /// @param if_ the if to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1033 | void CheckIf(const If* if_); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1034 | |
| 1035 | /// Validates the given loop |
| 1036 | /// @param l the loop to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1037 | void CheckLoop(const Loop* l); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1038 | |
James Price | a002503 | 2024-05-29 15:38:19 +0000 | [diff] [blame] | 1039 | /// Validates the loop body block |
| 1040 | /// @param l the loop to validate |
| 1041 | void CheckLoopBody(const Loop* l); |
| 1042 | |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 1043 | /// Validates the loop continuing block |
| 1044 | /// @param l the loop to validate |
| 1045 | void CheckLoopContinuing(const Loop* l); |
| 1046 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1047 | /// Validates the given switch |
| 1048 | /// @param s the switch to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1049 | void CheckSwitch(const Switch* s); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1050 | |
Ryan Harrison | 8b8ef02 | 2024-08-14 22:44:24 +0000 | [diff] [blame] | 1051 | /// Validates the given swizzle |
| 1052 | /// @param s the swizzle to validate |
| 1053 | void CheckSwizzle(const Swizzle* s); |
| 1054 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1055 | /// Validates the given terminator |
| 1056 | /// @param b the terminator to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1057 | void CheckTerminator(const Terminator* b); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1058 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1059 | /// Validates the break if instruction |
| 1060 | /// @param b the break if to validate |
| 1061 | void CheckBreakIf(const BreakIf* b); |
| 1062 | |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 1063 | /// Validates the continue instruction |
| 1064 | /// @param c the continue to validate |
| 1065 | void CheckContinue(const Continue* c); |
| 1066 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1067 | /// Validates the given exit |
| 1068 | /// @param e the exit to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1069 | void CheckExit(const Exit* e); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1070 | |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 1071 | /// Validates the next iteration instruction |
| 1072 | /// @param n the next iteration to validate |
| 1073 | void CheckNextIteration(const NextIteration* n); |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 1074 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1075 | /// Validates the given exit if |
| 1076 | /// @param e the exit if to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1077 | void CheckExitIf(const ExitIf* e); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1078 | |
| 1079 | /// Validates the given return |
| 1080 | /// @param r the return to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1081 | void CheckReturn(const Return* r); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1082 | |
Ryan Harrison | e2d11b0 | 2024-09-24 19:32:29 +0000 | [diff] [blame] | 1083 | /// Validates the given unreachable |
| 1084 | /// @param u the unreachable to validate |
| 1085 | void CheckUnreachable(const Unreachable* u); |
| 1086 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1087 | /// Validates the @p exit targets a valid @p control instruction where the instruction may jump |
| 1088 | /// over if control instructions. |
| 1089 | /// @param exit the exit to validate |
| 1090 | /// @param control the control instruction targeted |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1091 | void CheckControlsAllowingIf(const Exit* exit, const Instruction* control); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1092 | |
| 1093 | /// Validates the given exit switch |
| 1094 | /// @param s the exit switch to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1095 | void CheckExitSwitch(const ExitSwitch* s); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1096 | |
| 1097 | /// Validates the given exit loop |
| 1098 | /// @param l the exit loop to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1099 | void CheckExitLoop(const ExitLoop* l); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1100 | |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 1101 | /// Validates the given load |
| 1102 | /// @param l the load to validate |
| 1103 | void CheckLoad(const Load* l); |
| 1104 | |
James Price | 8fade13 | 2023-09-20 11:42:42 +0000 | [diff] [blame] | 1105 | /// Validates the given store |
| 1106 | /// @param s the store to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1107 | void CheckStore(const Store* s); |
James Price | 8fade13 | 2023-09-20 11:42:42 +0000 | [diff] [blame] | 1108 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1109 | /// Validates the given load vector element |
| 1110 | /// @param l the load vector element to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1111 | void CheckLoadVectorElement(const LoadVectorElement* l); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1112 | |
| 1113 | /// Validates the given store vector element |
| 1114 | /// @param s the store vector element to validate |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1115 | void CheckStoreVectorElement(const StoreVectorElement* s); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1116 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1117 | /// Validates that the number and types of the source instruction operands match the target's |
| 1118 | /// values. |
| 1119 | /// @param source_inst the source instruction |
| 1120 | /// @param source_operand_offset the index of the first operand of the source instruction |
| 1121 | /// @param source_operand_count the number of operands of the source instruction |
| 1122 | /// @param target the receiver of the operand values |
| 1123 | /// @param target_values the receiver of the operand values |
| 1124 | void CheckOperandsMatchTarget(const Instruction* source_inst, |
| 1125 | size_t source_operand_offset, |
| 1126 | size_t source_operand_count, |
| 1127 | const CastableBase* target, |
| 1128 | VectorRef<const Value*> target_values); |
| 1129 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1130 | /// @param inst the instruction |
| 1131 | /// @param idx the operand index |
| 1132 | /// @returns the vector pointer type for the given instruction operand |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1133 | const core::type::Type* GetVectorPtrElementType(const Instruction* inst, size_t idx); |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1134 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1135 | /// Executes all the pending tasks |
| 1136 | void ProcessTasks(); |
| 1137 | |
| 1138 | /// Queues the block to be validated with ProcessTasks() |
| 1139 | /// @param blk the block to validate |
| 1140 | void QueueBlock(const Block* blk); |
| 1141 | |
| 1142 | /// Queues the list of instructions starting with @p inst to be validated |
| 1143 | /// @param inst the first instruction |
| 1144 | void QueueInstructions(const Instruction* inst); |
| 1145 | |
| 1146 | /// Begins validation of the block @p blk, and its instructions. |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1147 | /// BeginBlock() pushes a new scope for values. |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1148 | /// Must be paired with a call to EndBlock(). |
| 1149 | void BeginBlock(const Block* blk); |
| 1150 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1151 | /// Ends validation of the block opened with BeginBlock() and closes the block's scope for |
| 1152 | /// values. |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1153 | void EndBlock(); |
| 1154 | |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 1155 | /// Get the function that contains an instruction. |
| 1156 | /// @param inst the instruction |
| 1157 | /// @returns the function |
| 1158 | const ir::Function* ContainingFunction(const ir::Instruction* inst) { |
| 1159 | return block_to_function_.GetOrAdd(inst->Block(), [&] { // |
| 1160 | return ContainingFunction(inst->Block()->Parent()); |
| 1161 | }); |
| 1162 | } |
| 1163 | |
| 1164 | /// Get any endpoints that call a function. |
| 1165 | /// @param f the function |
| 1166 | /// @returns all end points that call the function |
| 1167 | Hashset<const ir::Function*, 4> ContainingEndPoints(const ir::Function* f) { |
| 1168 | Hashset<const ir::Function*, 4> result{}; |
| 1169 | Hashset<const ir::Function*, 4> visited{f}; |
| 1170 | |
| 1171 | auto call_sites = user_func_calls_.GetOr(f, Hashset<const ir::UserCall*, 4>()).Vector(); |
| 1172 | while (!call_sites.IsEmpty()) { |
| 1173 | auto call_site = call_sites.Pop(); |
| 1174 | auto calling_function = ContainingFunction(call_site); |
| 1175 | if (visited.Contains(calling_function)) { |
| 1176 | continue; |
| 1177 | } |
| 1178 | visited.Add(calling_function); |
| 1179 | |
| 1180 | if (calling_function->Stage() != Function::PipelineStage::kUndefined) { |
| 1181 | result.Add(calling_function); |
| 1182 | } |
| 1183 | |
| 1184 | for (auto new_call_sites : |
| 1185 | user_func_calls_.GetOr(f, Hashset<const ir::UserCall*, 4>())) { |
| 1186 | call_sites.Push(new_call_sites); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | return result; |
| 1191 | } |
| 1192 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1193 | /// ScopeStack holds a stack of values that are currently in scope |
| 1194 | struct ScopeStack { |
| 1195 | void Push() { stack_.Push({}); } |
| 1196 | void Pop() { stack_.Pop(); } |
| 1197 | void Add(const Value* value) { stack_.Back().Add(value); } |
| 1198 | bool Contains(const Value* value) { |
| 1199 | return stack_.Any([&](auto& v) { return v.Contains(value); }); |
| 1200 | } |
| 1201 | bool IsEmpty() const { return stack_.IsEmpty(); } |
| 1202 | |
| 1203 | private: |
| 1204 | Vector<Hashset<const Value*, 8>, 4> stack_; |
| 1205 | }; |
| 1206 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1207 | const Module& mod_; |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 1208 | Capabilities capabilities_; |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1209 | std::optional<ir::Disassembler> disassembler_; // Use Disassemble() |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1210 | diag::List diagnostics_; |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1211 | Hashset<const Function*, 4> all_functions_; |
| 1212 | Hashset<const Instruction*, 4> visited_instructions_; |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 1213 | Hashmap<const Loop*, const Continue*, 4> first_continues_; |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1214 | Vector<const ControlInstruction*, 8> control_stack_; |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1215 | Vector<const Block*, 8> block_stack_; |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1216 | ScopeStack scope_stack_; |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1217 | Vector<std::function<void()>, 16> tasks_; |
Ben Clayton | 7e9f1a6 | 2024-05-22 18:42:55 +0000 | [diff] [blame] | 1218 | SymbolTable symbols_ = SymbolTable::Wrap(mod_.symbols); |
| 1219 | type::Manager type_mgr_ = type::Manager::Wrap(mod_.Types()); |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 1220 | Hashmap<const ir::Block*, const ir::Function*, 64> block_to_function_{}; |
| 1221 | Hashmap<const ir::Function*, Hashset<const ir::UserCall*, 4>, 4> user_func_calls_; |
| 1222 | Hashset<const ir::Discard*, 4> discards_; |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1223 | core::ir::ReferencedModuleVars<const Module> referenced_module_vars_; |
dan sinclair | fbc4ce7 | 2024-10-21 19:25:39 +0000 | [diff] [blame] | 1224 | |
| 1225 | Hashset<ValidatedType, 16> validated_types_{}; |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 1226 | }; |
| 1227 | |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 1228 | Validator::Validator(const Module& mod, Capabilities capabilities) |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1229 | : mod_(mod), capabilities_(capabilities), referenced_module_vars_(mod) {} |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 1230 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1231 | Validator::~Validator() = default; |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 1232 | |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1233 | Disassembler& Validator::Disassemble() { |
| 1234 | if (!disassembler_) { |
| 1235 | disassembler_.emplace(ir::Disassembler(mod_)); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1236 | } |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1237 | return *disassembler_; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1238 | } |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 1239 | |
Ben Clayton | 16fb254 | 2023-09-25 11:43:19 +0000 | [diff] [blame] | 1240 | Result<SuccessType> Validator::Run() { |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 1241 | RunStructuralSoundnessChecks(); |
| 1242 | |
| 1243 | CheckForOrphanedInstructions(); |
| 1244 | CheckForNonFragmentDiscards(); |
| 1245 | |
| 1246 | if (diagnostics_.ContainsErrors()) { |
| 1247 | diagnostics_.AddNote(Source{}) << "# Disassembly\n" << Disassemble().Text(); |
| 1248 | return Failure{std::move(diagnostics_)}; |
| 1249 | } |
| 1250 | return Success; |
| 1251 | } |
| 1252 | |
| 1253 | void Validator::CheckForOrphanedInstructions() { |
| 1254 | if (diagnostics_.ContainsErrors()) { |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | // Check for orphaned instructions. |
| 1259 | for (auto* inst : mod_.Instructions()) { |
| 1260 | if (!visited_instructions_.Contains(inst)) { |
| 1261 | AddError(inst) << "orphaned instruction: " << inst->FriendlyName(); |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | void Validator::CheckForNonFragmentDiscards() { |
| 1267 | if (diagnostics_.ContainsErrors()) { |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | // Check for discards in non-fragments |
| 1272 | for (const auto& d : discards_) { |
| 1273 | const auto* f = ContainingFunction(d); |
| 1274 | for (const Function* ep : ContainingEndPoints(f)) { |
| 1275 | if (ep->Stage() != Function::PipelineStage::kFragment) { |
| 1276 | AddError(d) << "cannot be called in non-fragment end point"; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | void Validator::RunStructuralSoundnessChecks() { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1283 | scope_stack_.Push(); |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1284 | TINT_DEFER({ |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1285 | scope_stack_.Pop(); |
| 1286 | TINT_ASSERT(scope_stack_.IsEmpty()); |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1287 | TINT_ASSERT(tasks_.IsEmpty()); |
| 1288 | TINT_ASSERT(control_stack_.IsEmpty()); |
| 1289 | TINT_ASSERT(block_stack_.IsEmpty()); |
| 1290 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1291 | CheckRootBlock(mod_.root_block); |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 1292 | |
Ben Clayton | acef310 | 2023-11-20 17:09:35 +0000 | [diff] [blame] | 1293 | for (auto& func : mod_.functions) { |
| 1294 | if (!all_functions_.Add(func.Get())) { |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1295 | AddError(func) << "function " << NameOf(func.Get()) |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 1296 | << " added to module multiple times"; |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1297 | } |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1298 | scope_stack_.Add(func); |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Ben Clayton | acef310 | 2023-11-20 17:09:35 +0000 | [diff] [blame] | 1301 | for (auto& func : mod_.functions) { |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 1302 | block_to_function_.Add(func->Block(), func); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1303 | CheckFunction(func); |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 1304 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1305 | } |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 1306 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1307 | diag::Diagnostic& Validator::AddError(const Instruction* inst) { |
Ben Clayton | fc36dcf | 2024-05-09 20:36:58 +0000 | [diff] [blame] | 1308 | diagnostics_.ReserveAdditional(2); // Ensure diagnostics don't resize alive after AddNote() |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1309 | auto src = Disassemble().InstructionSource(inst); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1310 | auto& diag = AddError(src) << inst->FriendlyName() << ": "; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1311 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1312 | if (!block_stack_.IsEmpty()) { |
| 1313 | AddNote(block_stack_.Back()) << "in block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1314 | } |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1315 | return diag; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1318 | diag::Diagnostic& Validator::AddError(const Instruction* inst, size_t idx) { |
Ben Clayton | fc36dcf | 2024-05-09 20:36:58 +0000 | [diff] [blame] | 1319 | diagnostics_.ReserveAdditional(2); // Ensure diagnostics don't resize alive after AddNote() |
Ben Clayton | aaeb83a | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1320 | auto src = |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1321 | Disassemble().OperandSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)}); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1322 | auto& diag = AddError(src) << inst->FriendlyName() << ": "; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1323 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1324 | if (!block_stack_.IsEmpty()) { |
| 1325 | AddNote(block_stack_.Back()) << "in block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1326 | } |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1327 | return diag; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1330 | diag::Diagnostic& Validator::AddResultError(const Instruction* inst, size_t idx) { |
Ben Clayton | fc36dcf | 2024-05-09 20:36:58 +0000 | [diff] [blame] | 1331 | diagnostics_.ReserveAdditional(2); // Ensure diagnostics don't resize alive after AddNote() |
Ben Clayton | aaeb83a | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1332 | auto src = |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1333 | Disassemble().ResultSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)}); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1334 | auto& diag = AddError(src) << inst->FriendlyName() << ": "; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1335 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1336 | if (!block_stack_.IsEmpty()) { |
| 1337 | AddNote(block_stack_.Back()) << "in block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1338 | } |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1339 | return diag; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1342 | diag::Diagnostic& Validator::AddError(const Block* blk) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1343 | auto src = Disassemble().BlockSource(blk); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1344 | return AddError(src); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
James Price | 4f491bf | 2024-04-24 21:15:04 +0000 | [diff] [blame] | 1347 | diag::Diagnostic& Validator::AddError(const BlockParam* param) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1348 | auto src = Disassemble().BlockParamSource(param); |
James Price | 4f491bf | 2024-04-24 21:15:04 +0000 | [diff] [blame] | 1349 | return AddError(src); |
| 1350 | } |
| 1351 | |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 1352 | diag::Diagnostic& Validator::AddError(const Function* func) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1353 | auto src = Disassemble().FunctionSource(func); |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 1354 | return AddError(src); |
| 1355 | } |
| 1356 | |
| 1357 | diag::Diagnostic& Validator::AddError(const FunctionParam* param) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1358 | auto src = Disassemble().FunctionParamSource(param); |
James Price | 21517e4 | 2024-04-24 17:36:34 +0000 | [diff] [blame] | 1359 | return AddError(src); |
| 1360 | } |
| 1361 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1362 | diag::Diagnostic& Validator::AddNote(const Instruction* inst) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1363 | auto src = Disassemble().InstructionSource(inst); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1364 | return AddNote(src); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
James Price | e5380b7 | 2024-04-24 20:52:59 +0000 | [diff] [blame] | 1367 | diag::Diagnostic& Validator::AddNote(const Function* func) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1368 | auto src = Disassemble().FunctionSource(func); |
James Price | e5380b7 | 2024-04-24 20:52:59 +0000 | [diff] [blame] | 1369 | return AddNote(src); |
| 1370 | } |
| 1371 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1372 | diag::Diagnostic& Validator::AddOperandNote(const Instruction* inst, size_t idx) { |
Ben Clayton | aaeb83a | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1373 | auto src = |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1374 | Disassemble().OperandSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)}); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1375 | return AddNote(src); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1378 | diag::Diagnostic& Validator::AddResultNote(const Instruction* inst, size_t idx) { |
| 1379 | auto src = |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1380 | Disassemble().ResultSource(Disassembler::IndexedValue{inst, static_cast<uint32_t>(idx)}); |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1381 | return AddNote(src); |
| 1382 | } |
| 1383 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1384 | diag::Diagnostic& Validator::AddNote(const Block* blk) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1385 | auto src = Disassemble().BlockSource(blk); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1386 | return AddNote(src); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1389 | diag::Diagnostic& Validator::AddError(Source src) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 1390 | auto& diag = diagnostics_.AddError(src); |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1391 | diag.owned_file = Disassemble().File(); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1392 | return diag; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1395 | diag::Diagnostic& Validator::AddNote(Source src) { |
Ben Clayton | 415bd73 | 2024-05-02 14:36:02 +0000 | [diff] [blame] | 1396 | auto& diag = diagnostics_.AddNote(src); |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1397 | diag.owned_file = Disassemble().File(); |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1398 | return diag; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1401 | void Validator::AddDeclarationNote(const CastableBase* decl) { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1402 | tint::Switch( |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1403 | decl, // |
| 1404 | [&](const Block* block) { AddDeclarationNote(block); }, |
| 1405 | [&](const BlockParam* param) { AddDeclarationNote(param); }, |
| 1406 | [&](const Function* fn) { AddDeclarationNote(fn); }, |
| 1407 | [&](const FunctionParam* param) { AddDeclarationNote(param); }, |
| 1408 | [&](const Instruction* inst) { AddDeclarationNote(inst); }, |
| 1409 | [&](const InstructionResult* res) { AddDeclarationNote(res); }); |
| 1410 | } |
| 1411 | |
| 1412 | void Validator::AddDeclarationNote(const Block* block) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1413 | auto src = Disassemble().BlockSource(block); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1414 | if (src.file) { |
| 1415 | AddNote(src) << NameOf(block) << " declared here"; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | void Validator::AddDeclarationNote(const BlockParam* param) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1420 | auto src = Disassemble().BlockParamSource(param); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1421 | if (src.file) { |
| 1422 | AddNote(src) << NameOf(param) << " declared here"; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | void Validator::AddDeclarationNote(const Function* fn) { |
| 1427 | AddNote(fn) << NameOf(fn) << " declared here"; |
| 1428 | } |
| 1429 | |
| 1430 | void Validator::AddDeclarationNote(const FunctionParam* param) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1431 | auto src = Disassemble().FunctionParamSource(param); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1432 | if (src.file) { |
| 1433 | AddNote(src) << NameOf(param) << " declared here"; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | void Validator::AddDeclarationNote(const Instruction* inst) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1438 | auto src = Disassemble().InstructionSource(inst); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1439 | if (src.file) { |
| 1440 | AddNote(src) << NameOf(inst) << " declared here"; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | void Validator::AddDeclarationNote(const InstructionResult* res) { |
| 1445 | if (auto* inst = res->Instruction()) { |
| 1446 | auto results = inst->Results(); |
| 1447 | for (size_t i = 0; i < results.Length(); i++) { |
| 1448 | if (results[i] == res) { |
| 1449 | AddResultNote(res->Instruction(), i) << NameOf(res) << " declared here"; |
| 1450 | return; |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1451 | } |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | StyledText Validator::NameOf(const CastableBase* decl) { |
| 1457 | return tint::Switch( |
| 1458 | decl, // |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 1459 | [&](const type::Type* ty) { return NameOf(ty); }, |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1460 | [&](const Value* value) { return NameOf(value); }, |
| 1461 | [&](const Instruction* inst) { return NameOf(inst); }, |
| 1462 | [&](const Block* block) { return NameOf(block); }, // |
| 1463 | TINT_ICE_ON_NO_MATCH); |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 1466 | StyledText Validator::NameOf(const type::Type* ty) { |
| 1467 | auto name = ty ? ty->FriendlyName() : "undef"; |
| 1468 | return StyledText{} << style::Type(name); |
| 1469 | } |
| 1470 | |
Ben Clayton | 35db5b5 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1471 | StyledText Validator::NameOf(const Value* value) { |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1472 | return Disassemble().NameOf(value); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1475 | StyledText Validator::NameOf(const Instruction* inst) { |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 1476 | auto name = inst ? inst->FriendlyName() : "undef"; |
| 1477 | return StyledText{} << style::Instruction(name); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | StyledText Validator::NameOf(const Block* block) { |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 1481 | auto parent_name = block->Parent() ? block->Parent()->FriendlyName() : "undef"; |
| 1482 | return StyledText{} << style::Instruction(parent_name) << " block " |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 1483 | << Disassemble().NameOf(block); |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1486 | bool Validator::CheckResult(const Instruction* inst, size_t idx) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1487 | auto* result = inst->Result(idx); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1488 | if (DAWN_UNLIKELY(result == nullptr)) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1489 | AddResultError(inst, idx) << "result is undefined"; |
| 1490 | return false; |
| 1491 | } |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1492 | |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1493 | if (DAWN_UNLIKELY(result->Type() == nullptr)) { |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1494 | AddResultError(inst, idx) << "result type is undefined"; |
| 1495 | return false; |
| 1496 | } |
| 1497 | |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 1498 | if (DAWN_UNLIKELY(result->Instruction() == nullptr)) { |
| 1499 | AddResultError(inst, idx) << "result instruction is undefined"; |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1503 | return true; |
| 1504 | } |
| 1505 | |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1506 | bool Validator::CheckResults(const ir::Instruction* inst, std::optional<size_t> count = {}) { |
| 1507 | if (count.has_value()) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1508 | if (DAWN_UNLIKELY(inst->Results().Length() != count.value())) { |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1509 | AddError(inst) << "expected exactly " << count.value() << " results, got " |
| 1510 | << inst->Results().Length(); |
| 1511 | return false; |
| 1512 | } |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | bool passed = true; |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1516 | for (size_t i = 0; i < inst->Results().Length(); i++) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1517 | if (DAWN_UNLIKELY(!CheckResult(inst, i))) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1518 | passed = false; |
| 1519 | } |
| 1520 | } |
| 1521 | return passed; |
| 1522 | } |
| 1523 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1524 | bool Validator::CheckOperand(const Instruction* inst, size_t idx) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1525 | auto* operand = inst->Operand(idx); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1526 | if (DAWN_UNLIKELY(operand == nullptr)) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1527 | AddError(inst, idx) << "operand is undefined"; |
| 1528 | return false; |
| 1529 | } |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1530 | |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1531 | // ir::Unused is a internal value used by some transforms to track unused entries, and is |
| 1532 | // removed as part of generating an output shader. |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1533 | if (DAWN_UNLIKELY(operand->Is<ir::Unused>())) { |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1534 | return true; |
| 1535 | } |
| 1536 | |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1537 | // ir::Function does not have a meaningful type, so does not override the default Type() |
| 1538 | // behaviour. |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1539 | if (DAWN_UNLIKELY(!operand->Is<ir::Function>() && operand->Type() == nullptr)) { |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 1540 | AddError(inst, idx) << "operand type is undefined"; |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 1544 | if (DAWN_UNLIKELY(!operand->Alive())) { |
| 1545 | AddError(inst, idx) << "operand is not alive"; |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
| 1549 | if (DAWN_UNLIKELY(!operand->HasUsage(inst, idx))) { |
| 1550 | AddError(inst, idx) << "operand missing usage"; |
| 1551 | return false; |
| 1552 | } |
| 1553 | |
| 1554 | if (auto fn = operand->As<Function>(); fn && !all_functions_.Contains(fn)) { |
| 1555 | AddError(inst, idx) << NameOf(operand) << " is not part of the module"; |
| 1556 | return false; |
| 1557 | } |
| 1558 | |
| 1559 | if (DAWN_UNLIKELY(!operand->Is<ir::Unused>() && !operand->Is<Constant>() && |
| 1560 | !scope_stack_.Contains(operand))) { |
| 1561 | AddError(inst, idx) << NameOf(operand) << " is not in scope"; |
| 1562 | AddDeclarationNote(operand); |
| 1563 | return false; |
| 1564 | } |
| 1565 | |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1566 | return true; |
| 1567 | } |
| 1568 | |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 1569 | bool Validator::CheckOperands(const ir::Instruction* inst, |
| 1570 | size_t min_count, |
| 1571 | std::optional<size_t> max_count) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1572 | if (DAWN_UNLIKELY(inst->Operands().Length() < min_count)) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 1573 | if (max_count.has_value()) { |
| 1574 | AddError(inst) << "expected between " << min_count << " and " << max_count.value() |
| 1575 | << " operands, got " << inst->Operands().Length(); |
| 1576 | } else { |
| 1577 | AddError(inst) << "expected at least " << min_count << " operands, got " |
| 1578 | << inst->Operands().Length(); |
| 1579 | } |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1583 | if (DAWN_UNLIKELY(max_count.has_value() && inst->Operands().Length() > max_count.value())) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 1584 | AddError(inst) << "expected between " << min_count << " and " << max_count.value() |
| 1585 | << " operands, got " << inst->Operands().Length(); |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
| 1589 | bool passed = true; |
| 1590 | for (size_t i = 0; i < inst->Operands().Length(); i++) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1591 | if (DAWN_UNLIKELY(!CheckOperand(inst, i))) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 1592 | passed = false; |
| 1593 | } |
| 1594 | } |
| 1595 | return passed; |
| 1596 | } |
| 1597 | |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1598 | bool Validator::CheckOperands(const ir::Instruction* inst, std::optional<size_t> count = {}) { |
| 1599 | if (count.has_value()) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1600 | if (DAWN_UNLIKELY(inst->Operands().Length() != count.value())) { |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1601 | AddError(inst) << "expected exactly " << count.value() << " operands, got " |
| 1602 | << inst->Operands().Length(); |
| 1603 | return false; |
| 1604 | } |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | bool passed = true; |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 1608 | for (size_t i = 0; i < inst->Operands().Length(); i++) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1609 | if (DAWN_UNLIKELY(!CheckOperand(inst, i))) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1610 | passed = false; |
| 1611 | } |
| 1612 | } |
| 1613 | return passed; |
| 1614 | } |
| 1615 | |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 1616 | bool Validator::CheckResultsAndOperandRange(const ir::Instruction* inst, |
| 1617 | size_t num_results, |
| 1618 | size_t min_operands, |
| 1619 | std::optional<size_t> max_operands = {}) { |
| 1620 | // Intentionally avoiding short-circuiting here |
| 1621 | bool results_passed = CheckResults(inst, num_results); |
| 1622 | bool operands_passed = CheckOperands(inst, min_operands, max_operands); |
| 1623 | return results_passed && operands_passed; |
| 1624 | } |
| 1625 | |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 1626 | bool Validator::CheckResultsAndOperands(const ir::Instruction* inst, |
| 1627 | size_t num_results, |
| 1628 | size_t num_operands) { |
| 1629 | // Intentionally avoiding short-circuiting here |
| 1630 | bool results_passed = CheckResults(inst, num_results); |
| 1631 | bool operands_passed = CheckOperands(inst, num_operands); |
| 1632 | return results_passed && operands_passed; |
| 1633 | } |
| 1634 | |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 1635 | void Validator::CheckType(const core::type::Type* root, |
| 1636 | std::function<diag::Diagnostic&()> diag, |
| 1637 | Capabilities ignore_caps) { |
dan sinclair | fbc4ce7 | 2024-10-21 19:25:39 +0000 | [diff] [blame] | 1638 | if (root == nullptr) { |
| 1639 | return; |
| 1640 | } |
| 1641 | |
| 1642 | if (!validated_types_.Add(ValidatedType{root, ignore_caps})) { |
| 1643 | return; |
| 1644 | } |
| 1645 | |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 1646 | auto visit = [&](const type::Type* type) { |
| 1647 | return tint::Switch( |
| 1648 | type, |
| 1649 | [&](const type::Reference*) { |
| 1650 | // Reference types are guarded by the AllowRefTypes capability. |
| 1651 | if (!capabilities_.Contains(Capability::kAllowRefTypes) || |
| 1652 | ignore_caps.Contains(Capability::kAllowRefTypes)) { |
| 1653 | diag() << "reference types are not permitted here"; |
| 1654 | return false; |
James Price | fcf6cc0 | 2024-08-01 04:41:28 +0000 | [diff] [blame] | 1655 | } else if (type != root) { |
| 1656 | // If they are allowed, reference types still cannot be nested. |
| 1657 | diag() << "nested reference types are not permitted"; |
| 1658 | return false; |
| 1659 | } |
| 1660 | return true; |
| 1661 | }, |
| 1662 | [&](const type::Pointer*) { |
| 1663 | if (type != root) { |
| 1664 | // Nesting pointer types inside structures is guarded by a capability. |
| 1665 | if (!(root->Is<type::Struct>() && |
| 1666 | capabilities_.Contains(Capability::kAllowPointersInStructures))) { |
| 1667 | diag() << "nested pointer types are not permitted"; |
| 1668 | return false; |
| 1669 | } |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 1670 | } |
| 1671 | return true; |
| 1672 | }, |
James Price | d31f5e4 | 2024-08-01 04:47:58 +0000 | [diff] [blame] | 1673 | [&](const type::I8*) { |
| 1674 | // i8 types are guarded by the Allow8BitIntegers capability. |
| 1675 | if (!capabilities_.Contains(Capability::kAllow8BitIntegers)) { |
| 1676 | diag() << "8-bit integer types are not permitted"; |
| 1677 | return false; |
| 1678 | } |
| 1679 | return true; |
| 1680 | }, |
| 1681 | [&](const type::U8*) { |
| 1682 | // u8 types are guarded by the Allow8BitIntegers capability. |
| 1683 | if (!capabilities_.Contains(Capability::kAllow8BitIntegers)) { |
| 1684 | diag() << "8-bit integer types are not permitted"; |
| 1685 | return false; |
| 1686 | } |
| 1687 | return true; |
| 1688 | }, |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 1689 | [](Default) { return true; }); |
| 1690 | }; |
| 1691 | |
| 1692 | Vector<const type::Type*, 8> stack{root}; |
| 1693 | Hashset<const type::Type*, 8> seen{}; |
| 1694 | while (!stack.IsEmpty()) { |
| 1695 | auto* ty = stack.Pop(); |
| 1696 | if (!ty) { |
| 1697 | continue; |
| 1698 | } |
| 1699 | if (!visit(ty)) { |
| 1700 | return; |
| 1701 | } |
| 1702 | |
| 1703 | if (auto* view = ty->As<type::MemoryView>(); view && seen.Add(view)) { |
James Price | fcf6cc0 | 2024-08-01 04:41:28 +0000 | [diff] [blame] | 1704 | stack.Push(view->StoreType()); |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 1705 | continue; |
| 1706 | } |
| 1707 | |
| 1708 | auto type_count = ty->Elements(); |
| 1709 | if (type_count.type && seen.Add(type_count.type)) { |
| 1710 | stack.Push(type_count.type); |
| 1711 | continue; |
| 1712 | } |
| 1713 | |
| 1714 | for (uint32_t i = 0; i < type_count.count; i++) { |
| 1715 | if (auto* subtype = ty->Element(i); subtype && seen.Add(subtype)) { |
| 1716 | stack.Push(subtype); |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1722 | void Validator::CheckRootBlock(const Block* blk) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1723 | block_stack_.Push(blk); |
| 1724 | TINT_DEFER(block_stack_.Pop()); |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 1725 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1726 | for (auto* inst : *blk) { |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1727 | if (inst->Block() != blk) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 1728 | AddError(inst) << "instruction in root block does not have root block as parent"; |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 1729 | continue; |
| 1730 | } |
dan sinclair | 9fa7c89 | 2024-06-29 21:13:48 +0000 | [diff] [blame] | 1731 | |
| 1732 | tint::Switch( |
| 1733 | inst, // |
| 1734 | [&](const core::ir::Var* var) { CheckInstruction(var); }, |
| 1735 | [&](const core::ir::Let* let) { |
| 1736 | if (capabilities_.Contains(Capability::kAllowModuleScopeLets)) { |
| 1737 | CheckInstruction(let); |
| 1738 | } else { |
| 1739 | AddError(inst) << "root block: invalid instruction: " << inst->TypeInfo().name; |
| 1740 | } |
| 1741 | }, |
| 1742 | [&](const core::ir::Construct* c) { |
| 1743 | if (capabilities_.Contains(Capability::kAllowModuleScopeLets)) { |
| 1744 | CheckInstruction(c); |
| 1745 | } else { |
| 1746 | AddError(inst) << "root block: invalid instruction: " << inst->TypeInfo().name; |
| 1747 | } |
| 1748 | }, |
| 1749 | [&](Default) { |
| 1750 | AddError(inst) << "root block: invalid instruction: " << inst->TypeInfo().name; |
| 1751 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 1755 | void Validator::CheckFunction(const Function* func) { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1756 | // Scope holds the parameters and block |
| 1757 | scope_stack_.Push(); |
| 1758 | TINT_DEFER(scope_stack_.Pop()); |
| 1759 | |
Ryan Harrison | 1c61352 | 2024-10-31 16:54:33 +0000 | [diff] [blame] | 1760 | Hashset<const FunctionParam*, 4> param_set{}; |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 1761 | for (auto* param : func->Params()) { |
James Price | e5380b7 | 2024-04-24 20:52:59 +0000 | [diff] [blame] | 1762 | if (!param->Alive()) { |
| 1763 | AddError(param) << "destroyed parameter found in function parameter list"; |
| 1764 | return; |
| 1765 | } |
| 1766 | if (!param->Function()) { |
| 1767 | AddError(param) << "function parameter has nullptr parent function"; |
| 1768 | return; |
| 1769 | } else if (param->Function() != func) { |
| 1770 | AddError(param) << "function parameter has incorrect parent function"; |
| 1771 | AddNote(param->Function()) << "parent function declared here"; |
| 1772 | return; |
| 1773 | } |
| 1774 | |
James Price | 5fc8c9e | 2024-05-24 16:11:13 +0000 | [diff] [blame] | 1775 | if (!param->Type()) { |
| 1776 | AddError(param) << "function parameter has nullptr type"; |
| 1777 | return; |
| 1778 | } |
| 1779 | |
Ryan Harrison | 1c61352 | 2024-10-31 16:54:33 +0000 | [diff] [blame] | 1780 | if (!param_set.Add(param)) { |
| 1781 | AddError(param) << "function parameter is not unique"; |
| 1782 | continue; |
| 1783 | } |
| 1784 | |
Ryan Harrison | e5c5c65 | 2024-10-01 22:23:03 +0000 | [diff] [blame] | 1785 | // References not allowed on function signatures even with Capability::kAllowRefTypes. |
| 1786 | CheckType( |
| 1787 | param->Type(), [&]() -> diag::Diagnostic& { return AddError(param); }, |
| 1788 | Capabilities{Capability::kAllowRefTypes}); |
| 1789 | |
| 1790 | if (!IsValidFunctionParamType(param->Type())) { |
| 1791 | auto struct_ty = param->Type()->As<core::type::Struct>(); |
| 1792 | if (!capabilities_.Contains(Capability::kAllowPointersInStructures) || !struct_ty || |
| 1793 | struct_ty->Members().Any([](const core::type::StructMember* m) { |
| 1794 | return !IsValidFunctionParamType(m->Type()); |
| 1795 | })) { |
| 1796 | AddError(param) << "function parameter type must be constructible, a pointer, a " |
| 1797 | "texture, or a sampler"; |
Ryan Harrison | e5c5c65 | 2024-10-01 22:23:03 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 1801 | CheckFunctionParamAttributesAndType(param, CheckBuiltinFunctionParam("")); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1802 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1803 | CheckFunctionParamAttributes( |
| 1804 | param, |
| 1805 | CheckInvariantFunc<FunctionParam>( |
| 1806 | "invariant can only decorate a param iff it is also decorated with position"), |
| 1807 | CheckInvariantFunc<FunctionParam>( |
| 1808 | "invariant can only decorate a param member iff it is also " |
| 1809 | "decorated with position")); |
| 1810 | CheckFunctionParamAttributes( |
| 1811 | param, |
| 1812 | CheckDoesNotHaveBothLocationAndBuiltinFunc<FunctionParam>( |
| 1813 | "a builtin and location cannot be both declared for a param"), |
| 1814 | CheckDoesNotHaveBothLocationAndBuiltinFunc<FunctionParam>( |
| 1815 | "a builtin and location cannot be both declared for a struct member")); |
Ryan Harrison | ceb3a5c5 | 2024-09-26 21:39:25 +0000 | [diff] [blame] | 1816 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 1817 | if (func->Stage() == Function::PipelineStage::kFragment) { |
| 1818 | CheckFunctionParamAttributesAndType( |
| 1819 | param, |
| 1820 | CheckFrontFacingIfBoolFunc<FunctionParam>( |
| 1821 | "fragment entry point params can only be a bool if decorated with " |
| 1822 | "@builtin(front_facing)"), |
| 1823 | CheckFrontFacingIfBoolFunc<FunctionParam>( |
| 1824 | "fragment entry point param memebers can only be a bool if " |
| 1825 | "decorated with @builtin(front_facing)")); |
| 1826 | } else if (func->Stage() != Function::PipelineStage::kUndefined) { |
| 1827 | CheckFunctionParamAttributesAndType( |
| 1828 | param, CheckNotBool<FunctionParam>( |
| 1829 | "entry point params can only be a bool for fragment shaders")); |
| 1830 | } |
| 1831 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1832 | scope_stack_.Add(param); |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 1833 | } |
Ben Clayton | d8bde2c | 2024-05-28 13:46:59 +0000 | [diff] [blame] | 1834 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1835 | // References not allowed on function signatures even with Capability::kAllowRefTypes. |
| 1836 | CheckType( |
| 1837 | func->ReturnType(), [&]() -> diag::Diagnostic& { return AddError(func); }, |
| 1838 | Capabilities{Capability::kAllowRefTypes}); |
| 1839 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 1840 | CheckFunctionReturnAttributesAndType(func, CheckBuiltinFunctionReturn("")); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1841 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1842 | CheckFunctionReturnAttributes( |
| 1843 | func, |
| 1844 | CheckInvariantFunc<Function>( |
| 1845 | "invariant can only decorate outputs iff they are also position builtins"), |
| 1846 | CheckInvariantFunc<Function>( |
| 1847 | "invariant can only decorate output members iff they are also position builtins")); |
| 1848 | |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1849 | CheckFunctionReturnAttributes( |
| 1850 | func, |
| 1851 | CheckDoesNotHaveBothLocationAndBuiltinFunc<Function>( |
| 1852 | "a builtin and location cannot be both declared for a function return"), |
| 1853 | CheckDoesNotHaveBothLocationAndBuiltinFunc<Function>( |
| 1854 | "a builtin and location cannot be both declared for a struct member")); |
| 1855 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1856 | // void needs to be filtered out, since it isn't constructible, but used in the IR when no |
| 1857 | // return is specified. |
| 1858 | if (DAWN_UNLIKELY(!func->ReturnType()->Is<core::type::Void>() && |
| 1859 | !func->ReturnType()->IsConstructible())) { |
| 1860 | AddError(func) << "function return type must be constructible"; |
| 1861 | } |
| 1862 | |
| 1863 | if (func->Stage() != Function::PipelineStage::kUndefined) { |
| 1864 | if (DAWN_UNLIKELY(mod_.NameOf(func).Name().empty())) { |
| 1865 | AddError(func) << "entry points must have names"; |
| 1866 | } |
| 1867 | } |
| 1868 | |
Ben Clayton | d8bde2c | 2024-05-28 13:46:59 +0000 | [diff] [blame] | 1869 | if (func->Stage() == Function::PipelineStage::kCompute) { |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 1870 | if (DAWN_UNLIKELY(!func->WorkgroupSize().has_value())) { |
Ben Clayton | d8bde2c | 2024-05-28 13:46:59 +0000 | [diff] [blame] | 1871 | AddError(func) << "compute entry point requires workgroup size attribute"; |
| 1872 | } |
Ryan Harrison | 1034514 | 2024-09-23 22:10:55 +0000 | [diff] [blame] | 1873 | |
| 1874 | if (DAWN_UNLIKELY(func->ReturnType() && !func->ReturnType()->Is<core::type::Void>())) { |
| 1875 | AddError(func) << "compute entry point must not have a return type"; |
| 1876 | } |
Ryan Harrison | 3854878 | 2024-08-12 21:37:58 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Ryan Harrison | d2bb5a1 | 2024-10-22 22:27:12 +0000 | [diff] [blame] | 1879 | if (DAWN_UNLIKELY(func->Stage() != Function::PipelineStage::kCompute && |
| 1880 | func->WorkgroupSize().has_value())) { |
| 1881 | AddError(func) << "workgroup size attribute only valid on compute entry point"; |
| 1882 | } |
| 1883 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1884 | if (func->Stage() == Function::PipelineStage::kFragment) { |
| 1885 | if (!func->ReturnType()->Is<core::type::Void>()) { |
| 1886 | CheckFunctionReturnAttributes( |
| 1887 | func, |
| 1888 | CheckHasLocationOrBuiltinFunc<Function>( |
| 1889 | "a non-void return for an entry point must have a " |
| 1890 | "builtin or location decoration"), |
| 1891 | CheckHasLocationOrBuiltinFunc<Function>( |
| 1892 | "members of struct used for returns of entry points must have a builtin or " |
| 1893 | "location decoration")); |
| 1894 | } |
| 1895 | } |
| 1896 | |
Ryan Harrison | 96ea474 | 2024-10-29 18:02:45 +0000 | [diff] [blame] | 1897 | if (func->Stage() != Function::PipelineStage::kUndefined) { |
| 1898 | CheckFunctionReturnAttributesAndType( |
| 1899 | func, CheckFrontFacingIfBoolFunc<Function>("entry point returns can not be bool"), |
| 1900 | CheckFrontFacingIfBoolFunc<Function>("entry point return members can not be bool")); |
| 1901 | |
| 1902 | for (auto var : referenced_module_vars_.TransitiveReferences(func)) { |
| 1903 | const auto* mv = var->Result(0)->Type()->As<type::MemoryView>(); |
| 1904 | const auto* ty = var->Result(0)->Type()->UnwrapPtrOrRef(); |
| 1905 | const auto attr = var->Attributes(); |
| 1906 | if (!mv || !ty) { |
| 1907 | continue; |
| 1908 | } |
| 1909 | |
| 1910 | if (mv->AddressSpace() != AddressSpace::kIn && |
| 1911 | mv->AddressSpace() != AddressSpace::kOut) { |
| 1912 | continue; |
| 1913 | } |
| 1914 | |
| 1915 | if (func->Stage() == Function::PipelineStage::kFragment && |
| 1916 | mv->AddressSpace() == AddressSpace::kIn) { |
| 1917 | CheckIOAttributesAndType( |
| 1918 | func, attr, ty, |
| 1919 | CheckFrontFacingIfBoolFunc<Function>("input address space values referenced by " |
| 1920 | "fragment shaders can only be a bool if " |
| 1921 | "decorated with @builtin(front_facing)")); |
| 1922 | |
| 1923 | } else { |
| 1924 | CheckIOAttributesAndType( |
| 1925 | func, attr, ty, |
| 1926 | CheckNotBool<Function>( |
| 1927 | "IO address space values referenced by shader entry points can only be " |
| 1928 | "bool if " |
| 1929 | "in the input space, used only by fragment shaders and decorated with " |
| 1930 | "@builtin(front_facing)")); |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1935 | if (func->Stage() == Function::PipelineStage::kVertex) { |
| 1936 | CheckVertexEntryPoint(func); |
| 1937 | } |
| 1938 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1939 | QueueBlock(func->Block()); |
| 1940 | ProcessTasks(); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1941 | } |
dan sinclair | 7686852 | 2023-06-28 16:50:16 +0000 | [diff] [blame] | 1942 | |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1943 | void Validator::CheckVertexEntryPoint(const Function* ep) { |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1944 | bool contains_position = IsPositionPresent(ep->ReturnAttributes(), ep->ReturnType()); |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1945 | |
| 1946 | for (auto var : referenced_module_vars_.TransitiveReferences(ep)) { |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1947 | const auto* ty = var->Result(0)->Type()->UnwrapPtrOrRef(); |
| 1948 | const auto attr = var->Attributes(); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1949 | if (!ty) { |
| 1950 | continue; |
| 1951 | } |
| 1952 | |
| 1953 | if (!contains_position) { |
| 1954 | contains_position = IsPositionPresent(attr, ty); |
| 1955 | } |
| 1956 | |
Ryan Harrison | 6a8b16c | 2024-10-09 21:49:35 +0000 | [diff] [blame] | 1957 | CheckIOAttributes( |
| 1958 | ep, attr, ty, |
| 1959 | CheckInvariantFunc<Function>( |
| 1960 | "invariant can only decorate vars iff they are also position builtins"), |
| 1961 | CheckInvariantFunc<Function>( |
| 1962 | "invariant can only decorate members iff they are also position builtins")); |
Ryan Harrison | fa3f89b | 2024-10-21 20:30:08 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Builtin rules are not checked on module-scope variables, because they are often generated |
| 1965 | // as part of the backend transforms, and have different rules for correctness. |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | if (DAWN_UNLIKELY(!contains_position)) { |
| 1969 | AddError(ep) << "position must be declared for vertex entry point output"; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1973 | void Validator::ProcessTasks() { |
| 1974 | while (!tasks_.IsEmpty()) { |
| 1975 | tasks_.Pop()(); |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | void Validator::QueueBlock(const Block* blk) { |
| 1980 | tasks_.Push([this] { EndBlock(); }); |
| 1981 | tasks_.Push([this, blk] { BeginBlock(blk); }); |
| 1982 | } |
| 1983 | |
| 1984 | void Validator::BeginBlock(const Block* blk) { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 1985 | scope_stack_.Push(); |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 1986 | block_stack_.Push(blk); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 1987 | |
James Price | 4f491bf | 2024-04-24 21:15:04 +0000 | [diff] [blame] | 1988 | if (auto* mb = blk->As<MultiInBlock>()) { |
| 1989 | for (auto* param : mb->Params()) { |
| 1990 | if (!param->Alive()) { |
| 1991 | AddError(param) << "destroyed parameter found in block parameter list"; |
| 1992 | return; |
| 1993 | } |
| 1994 | if (!param->Block()) { |
| 1995 | AddError(param) << "block parameter has nullptr parent block"; |
| 1996 | return; |
| 1997 | } else if (param->Block() != mb) { |
| 1998 | AddError(param) << "block parameter has incorrect parent block"; |
| 1999 | AddNote(param->Block()) << "parent block declared here"; |
| 2000 | return; |
| 2001 | } |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 2002 | |
| 2003 | // References not allowed on block parameters even with Capability::kAllowRefTypes. |
| 2004 | CheckType( |
| 2005 | param->Type(), [&]() -> diag::Diagnostic& { return AddError(param); }, |
| 2006 | Capabilities{Capability::kAllowRefTypes}); |
| 2007 | |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 2008 | scope_stack_.Add(param); |
James Price | 4f491bf | 2024-04-24 21:15:04 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | |
Ben Clayton | b9d3e1c | 2023-11-16 21:15:09 +0000 | [diff] [blame] | 2012 | if (!blk->Terminator()) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2013 | AddError(blk) << "block does not end in a terminator instruction"; |
dan sinclair | 7686852 | 2023-06-28 16:50:16 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2016 | // Validate the instructions w.r.t. the parent block |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2017 | for (auto* inst : *blk) { |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2018 | if (inst->Block() != blk) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2019 | AddError(inst) << "block instruction does not have same block as parent"; |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2020 | AddNote(blk) << "in block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2021 | } |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 2022 | } |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Enqueue validation of the instructions of the block |
| 2025 | if (!blk->IsEmpty()) { |
| 2026 | QueueInstructions(blk->Instructions()); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | void Validator::EndBlock() { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 2031 | scope_stack_.Pop(); |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2032 | block_stack_.Pop(); |
| 2033 | } |
| 2034 | |
| 2035 | void Validator::QueueInstructions(const Instruction* inst) { |
| 2036 | tasks_.Push([this, inst] { |
| 2037 | CheckInstruction(inst); |
| 2038 | if (inst->next) { |
| 2039 | QueueInstructions(inst->next); |
| 2040 | } |
| 2041 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2042 | } |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 2043 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2044 | void Validator::CheckInstruction(const Instruction* inst) { |
James Price | c00c569 | 2023-10-03 14:39:27 +0000 | [diff] [blame] | 2045 | visited_instructions_.Add(inst); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2046 | if (!inst->Alive()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2047 | AddError(inst) << "destroyed instruction found in instruction list"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2048 | return; |
dan sinclair | de3b699 | 2023-07-12 12:51:11 +0000 | [diff] [blame] | 2049 | } |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 2050 | |
Ben Clayton | a89d664 | 2023-11-17 22:41:32 +0000 | [diff] [blame] | 2051 | auto results = inst->Results(); |
| 2052 | for (size_t i = 0; i < results.Length(); ++i) { |
| 2053 | auto* res = results[i]; |
| 2054 | if (!res) { |
Ben Clayton | a89d664 | 2023-11-17 22:41:32 +0000 | [diff] [blame] | 2055 | continue; |
| 2056 | } |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 2057 | CheckType(res->Type(), [&]() -> diag::Diagnostic& { return AddResultError(inst, i); }); |
dan sinclair | de3b699 | 2023-07-12 12:51:11 +0000 | [diff] [blame] | 2058 | } |
dan sinclair | 165fdaa | 2023-07-12 13:03:21 +0000 | [diff] [blame] | 2059 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2060 | auto ops = inst->Operands(); |
| 2061 | for (size_t i = 0; i < ops.Length(); ++i) { |
| 2062 | auto* op = ops[i]; |
| 2063 | if (!op) { |
| 2064 | continue; |
| 2065 | } |
| 2066 | |
James Price | 189f0be | 2024-07-31 23:10:41 +0000 | [diff] [blame] | 2067 | CheckType(op->Type(), [&]() -> diag::Diagnostic& { return AddError(inst, i); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | tint::Switch( |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2071 | inst, // |
| 2072 | [&](const Access* a) { CheckAccess(a); }, // |
| 2073 | [&](const Binary* b) { CheckBinary(b); }, // |
| 2074 | [&](const Call* c) { CheckCall(c); }, // |
| 2075 | [&](const If* if_) { CheckIf(if_); }, // |
| 2076 | [&](const Let* let) { CheckLet(let); }, // |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 2077 | [&](const Load* load) { CheckLoad(load); }, // |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2078 | [&](const LoadVectorElement* l) { CheckLoadVectorElement(l); }, // |
| 2079 | [&](const Loop* l) { CheckLoop(l); }, // |
| 2080 | [&](const Store* s) { CheckStore(s); }, // |
| 2081 | [&](const StoreVectorElement* s) { CheckStoreVectorElement(s); }, // |
| 2082 | [&](const Switch* s) { CheckSwitch(s); }, // |
Ryan Harrison | 8b8ef02 | 2024-08-14 22:44:24 +0000 | [diff] [blame] | 2083 | [&](const Swizzle* s) { CheckSwizzle(s); }, // |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2084 | [&](const Terminator* b) { CheckTerminator(b); }, // |
| 2085 | [&](const Unary* u) { CheckUnary(u); }, // |
| 2086 | [&](const Var* var) { CheckVar(var); }, // |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2087 | [&](const Default) { AddError(inst) << "missing validation"; }); |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 2088 | |
| 2089 | for (auto* result : results) { |
| 2090 | scope_stack_.Add(result); |
| 2091 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2094 | void Validator::CheckVar(const Var* var) { |
Ryan Harrison | 4ce6f5a | 2024-07-16 19:11:16 +0000 | [diff] [blame] | 2095 | // Intentionally not checking operands, since Var may have a null operand |
| 2096 | if (!CheckResults(var, Var::kNumResults)) { |
| 2097 | return; |
| 2098 | } |
| 2099 | |
Ryan Harrison | 8b06674 | 2024-07-18 16:53:50 +0000 | [diff] [blame] | 2100 | // Check that initializer and result type match |
| 2101 | if (var->Initializer()) { |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 2102 | if (!CheckOperand(var, ir::Var::kInitializerOperandOffset)) { |
| 2103 | return; |
| 2104 | } |
| 2105 | |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2106 | if (var->Initializer()->Type() != var->Result(0)->Type()->UnwrapPtrOrRef()) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2107 | AddError(var) << "initializer type " |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 2108 | << style::Type(var->Initializer()->Type()->FriendlyName()) |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2109 | << " does not match store type " |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 2110 | << style::Type(var->Result(0)->Type()->UnwrapPtrOrRef()->FriendlyName()); |
Ryan Harrison | 8b06674 | 2024-07-18 16:53:50 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | } |
| 2113 | } |
| 2114 | |
Ryan Harrison | f22d846 | 2024-07-22 19:47:16 +0000 | [diff] [blame] | 2115 | auto* result_type = var->Result(0)->Type(); |
| 2116 | if (result_type == nullptr) { |
Ryan Harrison | 9531e03 | 2024-08-12 14:26:29 +0000 | [diff] [blame] | 2117 | AddError(var) << "result type is undefined"; |
Ryan Harrison | 8b06674 | 2024-07-18 16:53:50 +0000 | [diff] [blame] | 2118 | return; |
| 2119 | } |
| 2120 | |
Ryan Harrison | 9531e03 | 2024-08-12 14:26:29 +0000 | [diff] [blame] | 2121 | auto* mv = result_type->As<type::MemoryView>(); |
| 2122 | if (!mv) { |
| 2123 | AddError(var) << "result type must be a pointer or a reference"; |
| 2124 | return; |
| 2125 | } |
Ryan Harrison | f22d846 | 2024-07-22 19:47:16 +0000 | [diff] [blame] | 2126 | |
Ryan Harrison | 9531e03 | 2024-08-12 14:26:29 +0000 | [diff] [blame] | 2127 | // Check that only resource variables have @group and @binding set |
| 2128 | switch (mv->AddressSpace()) { |
| 2129 | case AddressSpace::kHandle: |
dan sinclair | 680183f | 2024-10-07 19:57:38 +0000 | [diff] [blame] | 2130 | if (!capabilities_.Contains(Capability::kAllowHandleVarsWithoutBindings)) { |
| 2131 | if (!var->BindingPoint().has_value()) { |
| 2132 | AddError(var) << "resource variable missing binding points"; |
| 2133 | } |
| 2134 | } |
| 2135 | break; |
Ryan Harrison | 9531e03 | 2024-08-12 14:26:29 +0000 | [diff] [blame] | 2136 | case AddressSpace::kStorage: |
| 2137 | case AddressSpace::kUniform: |
| 2138 | if (!var->BindingPoint().has_value()) { |
| 2139 | AddError(var) << "resource variable missing binding points"; |
| 2140 | } |
| 2141 | break; |
| 2142 | default: |
| 2143 | break; |
| 2144 | } |
| 2145 | |
| 2146 | // Check that non-handle variables don't have @input_attachment_index set |
| 2147 | if (var->InputAttachmentIndex().has_value() && mv->AddressSpace() != AddressSpace::kHandle) { |
| 2148 | AddError(var) << "'@input_attachment_index' is not valid for non-handle var"; |
| 2149 | return; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2150 | } |
Ryan Harrison | ceb3a5c5 | 2024-09-26 21:39:25 +0000 | [diff] [blame] | 2151 | |
| 2152 | if (HasLocationAndBuiltin(var->Attributes())) { |
| 2153 | AddError(var) << "a builtin and location cannot be both declared for a var"; |
| 2154 | return; |
| 2155 | } |
| 2156 | |
| 2157 | if (auto* s = var->Result(0)->Type()->UnwrapPtrOrRef()->As<core::type::Struct>()) { |
| 2158 | for (auto* mem : s->Members()) { |
| 2159 | if (HasLocationAndBuiltin(mem->Attributes())) { |
| 2160 | AddError(var) |
| 2161 | << "a builtin and location cannot be both declared for a struct member"; |
| 2162 | return; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 2168 | void Validator::CheckLet(const Let* l) { |
| 2169 | if (!CheckResultsAndOperands(l, Let::kNumResults, Let::kNumOperands)) { |
| 2170 | return; |
| 2171 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2172 | |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 2173 | if (l->Result(0) && l->Value()) { |
| 2174 | if (l->Result(0)->Type() != l->Value()->Type()) { |
Ryan Harrison | efed88f | 2024-10-31 16:55:04 +0000 | [diff] [blame^] | 2175 | auto result_type_name = |
| 2176 | l->Result(0)->Type() ? l->Result(0)->Type()->FriendlyName() : "undef"; |
| 2177 | auto value_type_name = |
| 2178 | l->Value()->Type() ? l->Value()->Type()->FriendlyName() : "undef"; |
| 2179 | AddError(l) << "result type " << style::Type(result_type_name) |
| 2180 | << " does not match value type " << style::Type(value_type_name); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2185 | void Validator::CheckCall(const Call* call) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2186 | tint::Switch( |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 2187 | call, // |
| 2188 | [&](const Bitcast* b) { CheckBitcast(b); }, // |
| 2189 | [&](const BuiltinCall* c) { CheckBuiltinCall(c); }, // |
| 2190 | [&](const MemberBuiltinCall* c) { CheckMemberBuiltinCall(c); }, // |
| 2191 | [&](const Construct* c) { CheckConstruct(c); }, // |
| 2192 | [&](const Convert* c) { CheckConvert(c); }, // |
| 2193 | [&](const Discard* d) { // |
| 2194 | discards_.Add(d); // |
| 2195 | CheckDiscard(d); // |
| 2196 | }, // |
| 2197 | [&](const UserCall* c) { // |
| 2198 | if (c->Target()) { // |
| 2199 | auto calls = // |
| 2200 | user_func_calls_.GetOr(c->Target(), // |
| 2201 | Hashset<const ir::UserCall*, 4>{}); // |
| 2202 | calls.Add(c); // |
| 2203 | user_func_calls_.Replace(c->Target(), calls); // |
| 2204 | } // |
| 2205 | CheckUserCall(c); // |
| 2206 | }, // |
dan sinclair | 774b6a4 | 2023-09-06 21:04:30 +0000 | [diff] [blame] | 2207 | [&](Default) { |
| 2208 | // Validation of custom IR instructions |
| 2209 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Ryan Harrison | d8b7465 | 2024-08-12 23:46:57 +0000 | [diff] [blame] | 2212 | void Validator::CheckBitcast(const Bitcast* bitcast) { |
| 2213 | CheckResultsAndOperands(bitcast, Bitcast::kNumResults, Bitcast::kNumOperands); |
| 2214 | } |
| 2215 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2216 | void Validator::CheckBuiltinCall(const BuiltinCall* call) { |
Ryan Harrison | c84af33 | 2024-07-30 22:39:37 +0000 | [diff] [blame] | 2217 | auto args = |
| 2218 | Transform<8>(call->Args(), [&](const ir::Value* v) { return v ? v->Type() : nullptr; }); |
| 2219 | if (args.Any([&](const type::Type* ty) { return ty == nullptr; })) { |
| 2220 | AddError(call) << "argument to builtin has undefined type"; |
| 2221 | return; |
| 2222 | } |
| 2223 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2224 | intrinsic::Context context{ |
| 2225 | call->TableData(), |
Ben Clayton | 7e9f1a6 | 2024-05-22 18:42:55 +0000 | [diff] [blame] | 2226 | type_mgr_, |
| 2227 | symbols_, |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2228 | }; |
dan sinclair | b88e1c7 | 2023-09-07 13:59:10 +0000 | [diff] [blame] | 2229 | |
Ryan Harrison | a2d786c | 2024-07-23 00:26:51 +0000 | [diff] [blame] | 2230 | auto builtin = core::intrinsic::LookupFn(context, call->FriendlyName().c_str(), call->FuncId(), |
| 2231 | Empty, args, core::EvaluationStage::kRuntime); |
| 2232 | if (builtin != Success) { |
| 2233 | AddError(call) << builtin.Failure(); |
Ben Clayton | cf05e92 | 2024-01-05 14:44:21 +0000 | [diff] [blame] | 2234 | return; |
| 2235 | } |
| 2236 | |
Ryan Harrison | a2d786c | 2024-07-23 00:26:51 +0000 | [diff] [blame] | 2237 | TINT_ASSERT(builtin->return_type); |
| 2238 | |
| 2239 | if (call->Result(0) == nullptr) { |
| 2240 | AddError(call) << "call to builtin does not have a return type"; |
| 2241 | return; |
| 2242 | } |
| 2243 | |
| 2244 | if (builtin->return_type != call->Result(0)->Type()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2245 | AddError(call) << "call result type does not match builtin return type"; |
Ryan Harrison | a2d786c | 2024-07-23 00:26:51 +0000 | [diff] [blame] | 2246 | return; |
James Price | 94a6a81 | 2023-09-13 16:59:52 +0000 | [diff] [blame] | 2247 | } |
Ben Clayton | 6a56e27 | 2023-09-06 02:01:07 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
James Price | c34ca5f | 2024-06-11 18:24:06 +0000 | [diff] [blame] | 2250 | void Validator::CheckMemberBuiltinCall(const MemberBuiltinCall* call) { |
| 2251 | auto args = Vector<const type::Type*, 8>({call->Object()->Type()}); |
| 2252 | for (auto* arg : call->Args()) { |
| 2253 | args.Push(arg->Type()); |
| 2254 | } |
| 2255 | intrinsic::Context context{ |
| 2256 | call->TableData(), |
| 2257 | type_mgr_, |
| 2258 | symbols_, |
| 2259 | }; |
| 2260 | |
| 2261 | auto result = |
| 2262 | core::intrinsic::LookupMemberFn(context, call->FriendlyName().c_str(), call->FuncId(), |
| 2263 | Empty, std::move(args), core::EvaluationStage::kRuntime); |
| 2264 | if (result != Success) { |
| 2265 | AddError(call) << result.Failure(); |
| 2266 | return; |
| 2267 | } |
| 2268 | |
| 2269 | if (result->return_type != call->Result(0)->Type()) { |
Antonio Maiorano | 38cce38 | 2024-10-11 20:04:59 +0000 | [diff] [blame] | 2270 | AddError(call) << "member call result type (" |
| 2271 | << style::Type(call->Result(0)->Type()->FriendlyName()) |
| 2272 | << ") does not match builtin return type (" |
| 2273 | << style::Type(result->return_type->FriendlyName()) << ")"; |
James Price | c34ca5f | 2024-06-11 18:24:06 +0000 | [diff] [blame] | 2274 | } |
| 2275 | } |
| 2276 | |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 2277 | void Validator::CheckConstruct(const Construct* construct) { |
Ryan Harrison | 2c6d2ea1 | 2024-08-08 19:55:22 +0000 | [diff] [blame] | 2278 | if (!CheckResultsAndOperandRange(construct, Construct::kNumResults, Construct::kMinOperands)) { |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
| 2281 | |
Ryan Harrison | 2c6d2ea1 | 2024-08-08 19:55:22 +0000 | [diff] [blame] | 2282 | auto args = construct->Args(); |
| 2283 | if (args.IsEmpty()) { |
| 2284 | // Zero-value constructors are valid for all constructible types. |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 2285 | return; |
| 2286 | } |
| 2287 | |
dan sinclair | 7cd0dfe | 2024-07-11 04:41:29 +0000 | [diff] [blame] | 2288 | if (auto* str = As<type::Struct>(construct->Result(0)->Type())) { |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 2289 | auto members = str->Members(); |
| 2290 | if (args.Length() != str->Members().Length()) { |
| 2291 | AddError(construct) << "structure has " << members.Length() |
| 2292 | << " members, but construct provides " << args.Length() |
| 2293 | << " arguments"; |
| 2294 | return; |
| 2295 | } |
| 2296 | for (size_t i = 0; i < args.Length(); i++) { |
Ryan Harrison | 71f1b39 | 2024-08-08 13:27:46 +0000 | [diff] [blame] | 2297 | if (args[i]->Is<ir::Unused>()) { |
| 2298 | continue; |
| 2299 | } |
| 2300 | if (args[i]->Type() != members[i]->Type()) { |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 2301 | AddError(construct, Construct::kArgsOperandOffset + i) |
dan sinclair | 2791ca9 | 2024-06-26 01:16:05 +0000 | [diff] [blame] | 2302 | << "structure member " << i << " is of type " |
James Price | 8527c13 | 2024-06-05 12:24:04 +0000 | [diff] [blame] | 2303 | << style::Type(members[i]->Type()->FriendlyName()) |
| 2304 | << ", but argument is of type " << style::Type(args[i]->Type()->FriendlyName()); |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
Ryan Harrison | f9761ac | 2024-08-08 18:56:30 +0000 | [diff] [blame] | 2310 | void Validator::CheckConvert(const Convert* convert) { |
| 2311 | CheckResultsAndOperands(convert, Convert::kNumResults, Convert::kNumOperands); |
| 2312 | } |
| 2313 | |
Ryan Harrison | 5183d1a | 2024-08-13 21:49:44 +0000 | [diff] [blame] | 2314 | void Validator::CheckDiscard(const tint::core::ir::Discard* discard) { |
| 2315 | CheckResultsAndOperands(discard, Discard::kNumResults, Discard::kNumOperands); |
| 2316 | } |
| 2317 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2318 | void Validator::CheckUserCall(const UserCall* call) { |
Ryan Harrison | d1b935e | 2024-07-16 21:02:03 +0000 | [diff] [blame] | 2319 | if (!CheckResultsAndOperandRange(call, UserCall::kNumResults, UserCall::kMinOperands)) { |
| 2320 | return; |
| 2321 | } |
| 2322 | |
Ryan Harrison | 72ae799 | 2024-07-18 21:01:12 +0000 | [diff] [blame] | 2323 | if (!call->Target()) { |
| 2324 | AddError(call, UserCall::kFunctionOperandOffset) << "target not defined or not a function"; |
| 2325 | return; |
| 2326 | } |
| 2327 | |
James Price | fbabce4 | 2024-01-15 21:09:26 +0000 | [diff] [blame] | 2328 | if (call->Target()->Stage() != Function::PipelineStage::kUndefined) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2329 | AddError(call, UserCall::kFunctionOperandOffset) |
| 2330 | << "call target must not have a pipeline stage"; |
James Price | fbabce4 | 2024-01-15 21:09:26 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Ben Clayton | a343074 | 2024-01-10 18:20:01 +0000 | [diff] [blame] | 2333 | auto args = call->Args(); |
| 2334 | auto params = call->Target()->Params(); |
| 2335 | if (args.Length() != params.Length()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2336 | AddError(call, UserCall::kFunctionOperandOffset) |
| 2337 | << "function has " << params.Length() << " parameters, but call provides " |
Ben Clayton | a343074 | 2024-01-10 18:20:01 +0000 | [diff] [blame] | 2338 | << args.Length() << " arguments"; |
Ben Clayton | a343074 | 2024-01-10 18:20:01 +0000 | [diff] [blame] | 2339 | return; |
| 2340 | } |
| 2341 | |
| 2342 | for (size_t i = 0; i < args.Length(); i++) { |
| 2343 | if (args[i]->Type() != params[i]->Type()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2344 | AddError(call, UserCall::kArgsOperandOffset + i) |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2345 | << "function parameter " << i << " is of type " |
| 2346 | << style::Type(params[i]->Type()->FriendlyName()) << ", but argument is of type " |
| 2347 | << style::Type(args[i]->Type()->FriendlyName()); |
Ben Clayton | a343074 | 2024-01-10 18:20:01 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
Ben Clayton | d0dfa74 | 2023-09-25 15:38:43 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2352 | void Validator::CheckAccess(const Access* a) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 2353 | if (!CheckResultsAndOperandRange(a, Access::kNumResults, Access::kMinNumOperands)) { |
Ryan Harrison | de3007a | 2024-07-11 00:51:27 +0000 | [diff] [blame] | 2354 | return; |
| 2355 | } |
| 2356 | |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2357 | auto* obj_view = a->Object()->Type()->As<core::type::MemoryView>(); |
| 2358 | auto* ty = obj_view ? obj_view->StoreType() : a->Object()->Type(); |
dan sinclair | 2bd5e04 | 2024-07-03 21:04:44 +0000 | [diff] [blame] | 2359 | |
| 2360 | enum Kind : uint8_t { kPtr, kRef, kValue }; |
| 2361 | |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2362 | auto kind_of = [&](const core::type::Type* type) { |
| 2363 | return tint::Switch( |
| 2364 | type, // |
| 2365 | [&](const core::type::Pointer*) { return kPtr; }, // |
| 2366 | [&](const core::type::Reference*) { return kRef; }, // |
| 2367 | [&](Default) { return kValue; }); |
| 2368 | }; |
dan sinclair | 2bd5e04 | 2024-07-03 21:04:44 +0000 | [diff] [blame] | 2369 | |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2370 | const Kind in_kind = kind_of(a->Object()->Type()); |
| 2371 | auto desc_of = [&](Kind kind, const core::type::Type* type) { |
| 2372 | switch (kind) { |
| 2373 | case kPtr: |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2374 | return StyledText{} |
| 2375 | << style::Type("ptr<", obj_view->AddressSpace(), ", ", type->FriendlyName(), |
| 2376 | ", ", obj_view->Access(), ">"); |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2377 | case kRef: |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2378 | return StyledText{} |
| 2379 | << style::Type("ref<", obj_view->AddressSpace(), ", ", type->FriendlyName(), |
| 2380 | ", ", obj_view->Access(), ">"); |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2381 | default: |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2382 | return StyledText{} << style::Type(type->FriendlyName()); |
Ben Clayton | aa77484 | 2024-01-12 18:28:20 +0000 | [diff] [blame] | 2383 | } |
| 2384 | }; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2385 | |
| 2386 | for (size_t i = 0; i < a->Indices().Length(); i++) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2387 | auto err = [&]() -> diag::Diagnostic& { |
| 2388 | return AddError(a, i + Access::kIndicesOperandOffset); |
| 2389 | }; |
| 2390 | auto note = [&]() -> diag::Diagnostic& { |
Ben Clayton | 3aebf9e | 2024-05-02 10:01:02 +0000 | [diff] [blame] | 2391 | return AddOperandNote(a, i + Access::kIndicesOperandOffset); |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2392 | }; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2393 | |
| 2394 | auto* index = a->Indices()[i]; |
Ryan Harrison | ebdd6ad | 2024-10-29 19:57:43 +0000 | [diff] [blame] | 2395 | if (DAWN_UNLIKELY(!index->Type() || !index->Type()->IsIntegerScalar())) { |
Ryan Harrison | efed88f | 2024-10-31 16:55:04 +0000 | [diff] [blame^] | 2396 | auto name = index->Type() ? index->Type()->FriendlyName() : "undef"; |
| 2397 | err() << "index must be integer, got " << name; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2398 | return; |
| 2399 | } |
| 2400 | |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 2401 | if (!capabilities_.Contains(Capability::kAllowVectorElementPointer)) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2402 | if (in_kind != kValue && ty->Is<core::type::Vector>()) { |
| 2403 | err() << "cannot obtain address of vector element"; |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 2404 | return; |
| 2405 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | if (auto* const_index = index->As<ir::Constant>()) { |
| 2409 | auto* value = const_index->Value(); |
Ryan Harrison | ebdd6ad | 2024-10-29 19:57:43 +0000 | [diff] [blame] | 2410 | if (!value->Type() || value->Type()->IsSignedIntegerScalar()) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2411 | // index is a signed integer scalar. Check that the index isn't negative. |
| 2412 | // If the index is unsigned, we can skip this. |
| 2413 | auto idx = value->ValueAs<AInt>(); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2414 | if (DAWN_UNLIKELY(idx < 0)) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2415 | err() << "constant index must be positive, got " << idx; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2416 | return; |
| 2417 | } |
dan sinclair | 165fdaa | 2023-07-12 13:03:21 +0000 | [diff] [blame] | 2418 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2419 | |
| 2420 | auto idx = value->ValueAs<uint32_t>(); |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2421 | auto* el = ty->Element(idx); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2422 | if (DAWN_UNLIKELY(!el)) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2423 | // Is index in bounds? |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2424 | if (auto el_count = ty->Elements().count; el_count != 0 && idx >= el_count) { |
| 2425 | err() << "index out of bounds for type " << desc_of(in_kind, ty); |
| 2426 | note() << "acceptable range: [0.." << (el_count - 1) << "]"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2427 | return; |
| 2428 | } |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2429 | err() << "type " << desc_of(in_kind, ty) << " cannot be indexed"; |
dan sinclair | 165fdaa | 2023-07-12 13:03:21 +0000 | [diff] [blame] | 2430 | return; |
| 2431 | } |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2432 | ty = el; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2433 | } else { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2434 | auto* el = ty->Elements().type; |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2435 | if (DAWN_UNLIKELY(!el)) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2436 | err() << "type " << desc_of(in_kind, ty) << " cannot be dynamically indexed"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2437 | return; |
dan sinclair | 4574f07 | 2023-07-12 17:10:21 +0000 | [diff] [blame] | 2438 | } |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2439 | ty = el; |
dan sinclair | 165fdaa | 2023-07-12 13:03:21 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2442 | |
Ben Clayton | aa77484 | 2024-01-12 18:28:20 +0000 | [diff] [blame] | 2443 | auto* want = a->Result(0)->Type(); |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2444 | auto* want_view = want->As<type::MemoryView>(); |
James Price | 230657c | 2024-05-15 18:56:08 +0000 | [diff] [blame] | 2445 | bool ok = true; |
| 2446 | if (obj_view) { |
| 2447 | // Pointer source always means pointer result. |
| 2448 | ok = want_view && ty == want_view->StoreType(); |
| 2449 | if (ok) { |
| 2450 | // Also check that the address space and access modes match. |
| 2451 | ok = obj_view->Is<type::Pointer>() == want_view->Is<type::Pointer>() && |
| 2452 | obj_view->AddressSpace() == want_view->AddressSpace() && |
| 2453 | obj_view->Access() == want_view->Access(); |
| 2454 | } |
| 2455 | } else { |
| 2456 | // Otherwise, result types should exactly match. |
| 2457 | ok = ty == want; |
Ben Clayton | aa77484 | 2024-01-12 18:28:20 +0000 | [diff] [blame] | 2458 | } |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2459 | if (DAWN_UNLIKELY(!ok)) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2460 | AddError(a) << "result of access chain is type " << desc_of(in_kind, ty) |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2461 | << " but instruction type is " << style::Type(want->FriendlyName()); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2464 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2465 | void Validator::CheckBinary(const Binary* b) { |
Ryan Harrison | fb61de4 | 2024-07-23 17:09:40 +0000 | [diff] [blame] | 2466 | if (!CheckResultsAndOperandRange(b, Binary::kNumResults, Binary::kNumOperands)) { |
| 2467 | return; |
| 2468 | } |
| 2469 | |
Ben Clayton | 514a18a | 2024-01-09 10:44:27 +0000 | [diff] [blame] | 2470 | if (b->LHS() && b->RHS()) { |
Ben Clayton | 7e9f1a6 | 2024-05-22 18:42:55 +0000 | [diff] [blame] | 2471 | intrinsic::Context context{b->TableData(), type_mgr_, symbols_}; |
Ben Clayton | 514a18a | 2024-01-09 10:44:27 +0000 | [diff] [blame] | 2472 | |
| 2473 | auto overload = |
| 2474 | core::intrinsic::LookupBinary(context, b->Op(), b->LHS()->Type(), b->RHS()->Type(), |
| 2475 | core::EvaluationStage::kRuntime, /* is_compound */ false); |
| 2476 | if (overload != Success) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2477 | AddError(b) << overload.Failure(); |
Ben Clayton | 514a18a | 2024-01-09 10:44:27 +0000 | [diff] [blame] | 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | if (auto* result = b->Result(0)) { |
| 2482 | if (overload->return_type != result->Type()) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2483 | AddError(b) << "result value type " << style::Type(result->Type()->FriendlyName()) |
| 2484 | << " does not match " |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 2485 | << style::Instruction(Disassemble().NameOf(b->Op())) << " result type " |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2486 | << style::Type(overload->return_type->FriendlyName()); |
Ben Clayton | 514a18a | 2024-01-09 10:44:27 +0000 | [diff] [blame] | 2487 | } |
| 2488 | } |
| 2489 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2492 | void Validator::CheckUnary(const Unary* u) { |
Ryan Harrison | 7944dde | 2024-07-23 19:38:41 +0000 | [diff] [blame] | 2493 | if (!CheckResultsAndOperandRange(u, Unary::kNumResults, Unary::kNumOperands)) { |
| 2494 | return; |
| 2495 | } |
| 2496 | |
Ben Clayton | b865a1e | 2024-01-08 13:01:50 +0000 | [diff] [blame] | 2497 | if (u->Val()) { |
Ben Clayton | 7e9f1a6 | 2024-05-22 18:42:55 +0000 | [diff] [blame] | 2498 | intrinsic::Context context{u->TableData(), type_mgr_, symbols_}; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2499 | |
Ben Clayton | b865a1e | 2024-01-08 13:01:50 +0000 | [diff] [blame] | 2500 | auto overload = core::intrinsic::LookupUnary(context, u->Op(), u->Val()->Type(), |
| 2501 | core::EvaluationStage::kRuntime); |
| 2502 | if (overload != Success) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2503 | AddError(u) << overload.Failure(); |
Ben Clayton | b865a1e | 2024-01-08 13:01:50 +0000 | [diff] [blame] | 2504 | return; |
| 2505 | } |
| 2506 | |
| 2507 | if (auto* result = u->Result(0)) { |
| 2508 | if (overload->return_type != result->Type()) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2509 | AddError(u) << "result value type " << style::Type(result->Type()->FriendlyName()) |
| 2510 | << " does not match " |
dan sinclair | 650bc3e | 2024-06-11 23:26:53 +0000 | [diff] [blame] | 2511 | << style::Instruction(Disassemble().NameOf(u->Op())) << " result type " |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2512 | << style::Type(overload->return_type->FriendlyName()); |
Ben Clayton | b865a1e | 2024-01-08 13:01:50 +0000 | [diff] [blame] | 2513 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2518 | void Validator::CheckIf(const If* if_) { |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 2519 | CheckResults(if_); |
| 2520 | CheckOperand(if_, If::kConditionOperandOffset); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2521 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2522 | if (if_->Condition() && !if_->Condition()->Type()->Is<core::type::Bool>()) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2523 | AddError(if_, If::kConditionOperandOffset) |
| 2524 | << "condition type must be " << style::Type("bool"); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2527 | tasks_.Push([this] { control_stack_.Pop(); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2528 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2529 | if (!if_->False()->IsEmpty()) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2530 | QueueBlock(if_->False()); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2531 | } |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2532 | |
| 2533 | QueueBlock(if_->True()); |
| 2534 | |
| 2535 | tasks_.Push([this, if_] { control_stack_.Push(if_); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2538 | void Validator::CheckLoop(const Loop* l) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2539 | // Note: Tasks are queued in reverse order of their execution |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2540 | tasks_.Push([this, l] { |
| 2541 | first_continues_.Remove(l); // No need for this any more. Free memory. |
| 2542 | control_stack_.Pop(); |
| 2543 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2544 | if (!l->Initializer()->IsEmpty()) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2545 | tasks_.Push([this] { EndBlock(); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2546 | } |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2547 | tasks_.Push([this] { EndBlock(); }); |
| 2548 | if (!l->Continuing()->IsEmpty()) { |
| 2549 | tasks_.Push([this] { EndBlock(); }); |
| 2550 | } |
| 2551 | |
| 2552 | // ⎡Initializer ⎤ |
| 2553 | // ⎢ ⎡Body ⎤⎥ |
| 2554 | // ⎣ ⎣ [Continuing ] ⎦⎦ |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2555 | |
| 2556 | if (!l->Continuing()->IsEmpty()) { |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2557 | tasks_.Push([this, l] { |
| 2558 | CheckLoopContinuing(l); |
| 2559 | BeginBlock(l->Continuing()); |
| 2560 | }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2561 | } |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2562 | |
James Price | a002503 | 2024-05-29 15:38:19 +0000 | [diff] [blame] | 2563 | tasks_.Push([this, l] { |
| 2564 | CheckLoopBody(l); |
| 2565 | BeginBlock(l->Body()); |
| 2566 | }); |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2567 | if (!l->Initializer()->IsEmpty()) { |
| 2568 | tasks_.Push([this, l] { BeginBlock(l->Initializer()); }); |
| 2569 | } |
| 2570 | tasks_.Push([this, l] { control_stack_.Push(l); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
James Price | a002503 | 2024-05-29 15:38:19 +0000 | [diff] [blame] | 2573 | void Validator::CheckLoopBody(const Loop* loop) { |
| 2574 | // If the body block has parameters, there must be an initializer block. |
| 2575 | if (!loop->Body()->Params().IsEmpty()) { |
| 2576 | if (!loop->HasInitializer()) { |
| 2577 | AddError(loop) << "loop with body block parameters must have an initializer"; |
| 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2582 | void Validator::CheckLoopContinuing(const Loop* loop) { |
| 2583 | if (!loop->HasContinuing()) { |
| 2584 | return; |
| 2585 | } |
| 2586 | |
| 2587 | // Ensure that values used in the loop continuing are not from the loop body, after a |
| 2588 | // continue instruction. |
| 2589 | if (auto* first_continue = first_continues_.GetOr(loop, nullptr)) { |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 2590 | // Find the instruction in the body block that is or holds the first continue |
| 2591 | // instruction. |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2592 | const Instruction* holds_continue = first_continue; |
| 2593 | while (holds_continue && holds_continue->Block() && |
| 2594 | holds_continue->Block() != loop->Body()) { |
| 2595 | holds_continue = holds_continue->Block()->Parent(); |
| 2596 | } |
| 2597 | |
| 2598 | // Check that all subsequent instruction values are not used in the continuing block. |
| 2599 | for (auto* inst = holds_continue; inst; inst = inst->next) { |
| 2600 | for (auto* result : inst->Results()) { |
dan sinclair | 3cfca9f | 2024-08-06 14:06:39 +0000 | [diff] [blame] | 2601 | result->ForEachUseUnsorted([&](Usage use) { |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2602 | if (TransitivelyHolds(loop->Continuing(), use.instruction)) { |
| 2603 | AddError(use.instruction, use.operand_index) |
| 2604 | << NameOf(result) |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 2605 | << " cannot be used in continuing block as it is declared after " |
| 2606 | "the " |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2607 | "first " |
| 2608 | << style::Instruction("continue") << " in the loop's body"; |
| 2609 | AddDeclarationNote(result); |
| 2610 | AddNote(first_continue) |
| 2611 | << "loop body's first " << style::Instruction("continue"); |
| 2612 | } |
| 2613 | }); |
| 2614 | } |
| 2615 | } |
| 2616 | } |
| 2617 | } |
| 2618 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2619 | void Validator::CheckSwitch(const Switch* s) { |
Ryan Harrison | e8b4867 | 2024-09-25 17:21:11 +0000 | [diff] [blame] | 2620 | CheckOperand(s, Switch::kConditionOperandOffset); |
dan sinclair | a9c7be0 | 2024-06-21 17:35:20 +0000 | [diff] [blame] | 2621 | |
dan sinclair | 06b574a | 2024-08-26 17:19:25 +0000 | [diff] [blame] | 2622 | if (s->Condition() && !s->Condition()->Type()->IsIntegerScalar()) { |
dan sinclair | a9c7be0 | 2024-06-21 17:35:20 +0000 | [diff] [blame] | 2623 | AddError(s, Switch::kConditionOperandOffset) << "condition type must be an integer scalar"; |
| 2624 | } |
| 2625 | |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2626 | tasks_.Push([this] { control_stack_.Pop(); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2627 | |
dan sinclair | a9c7be0 | 2024-06-21 17:35:20 +0000 | [diff] [blame] | 2628 | bool found_default = false; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2629 | for (auto& cse : s->Cases()) { |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2630 | QueueBlock(cse.block); |
dan sinclair | a9c7be0 | 2024-06-21 17:35:20 +0000 | [diff] [blame] | 2631 | |
| 2632 | for (const auto& sel : cse.selectors) { |
| 2633 | if (sel.IsDefault()) { |
| 2634 | found_default = true; |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | if (!found_default) { |
| 2640 | AddError(s) << "missing default case for switch"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2641 | } |
Ben Clayton | 833b892 | 2024-05-01 21:07:24 +0000 | [diff] [blame] | 2642 | |
| 2643 | tasks_.Push([this, s] { control_stack_.Push(s); }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Ryan Harrison | 8b8ef02 | 2024-08-14 22:44:24 +0000 | [diff] [blame] | 2646 | void Validator::CheckSwizzle(const Swizzle* s) { |
| 2647 | if (!CheckResultsAndOperands(s, Swizzle::kNumResults, Swizzle::kNumOperands)) { |
| 2648 | return; |
| 2649 | } |
| 2650 | |
| 2651 | auto indices = s->Indices(); |
| 2652 | if (indices.Length() < Swizzle::kMinNumIndices) { |
| 2653 | AddError(s) << "expected at least " << Swizzle::kMinNumIndices << " indices"; |
| 2654 | } |
| 2655 | |
| 2656 | if (indices.Length() > Swizzle::kMaxNumIndices) { |
| 2657 | AddError(s) << "expected at most " << Swizzle::kMaxNumIndices << " indices"; |
| 2658 | } |
| 2659 | |
| 2660 | for (auto& idx : indices) { |
| 2661 | if (idx > Swizzle::kMaxIndexValue) { |
| 2662 | AddError(s) << "invalid index value"; |
| 2663 | } |
| 2664 | } |
| 2665 | } |
| 2666 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2667 | void Validator::CheckTerminator(const Terminator* b) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2668 | // Note, transforms create `undef` terminator arguments (this is done in MergeReturn and |
| 2669 | // DemoteToHelper) so we can't add validation. |
| 2670 | |
| 2671 | tint::Switch( |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 2672 | b, // |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2673 | [&](const ir::BreakIf* i) { CheckBreakIf(i); }, // |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 2674 | [&](const ir::Continue* c) { CheckContinue(c); }, // |
| 2675 | [&](const ir::Exit* e) { CheckExit(e); }, // |
| 2676 | [&](const ir::NextIteration* n) { CheckNextIteration(n); }, // |
| 2677 | [&](const ir::Return* ret) { CheckReturn(ret); }, // |
| 2678 | [&](const ir::TerminateInvocation*) {}, // |
Ryan Harrison | e2d11b0 | 2024-09-24 19:32:29 +0000 | [diff] [blame] | 2679 | [&](const ir::Unreachable* u) { CheckUnreachable(u); }, // |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2680 | [&](Default) { AddError(b) << "missing validation"; }); |
Ben Clayton | f46acc5 | 2024-05-21 20:18:17 +0000 | [diff] [blame] | 2681 | |
| 2682 | if (b->next) { |
| 2683 | AddError(b) << "must be the last instruction in the block"; |
| 2684 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2687 | void Validator::CheckBreakIf(const BreakIf* b) { |
| 2688 | auto* loop = b->Loop(); |
| 2689 | if (loop == nullptr) { |
| 2690 | AddError(b) << "has no associated loop"; |
| 2691 | return; |
| 2692 | } |
| 2693 | |
| 2694 | if (loop->Continuing() != b->Block()) { |
| 2695 | AddError(b) << "must only be called directly from loop continuing"; |
| 2696 | } |
| 2697 | |
| 2698 | auto next_iter_values = b->NextIterValues(); |
| 2699 | if (auto* body = loop->Body()) { |
| 2700 | CheckOperandsMatchTarget(b, b->ArgsOperandOffset(), next_iter_values.Length(), body, |
| 2701 | body->Params()); |
| 2702 | } |
| 2703 | |
| 2704 | auto exit_values = b->ExitValues(); |
| 2705 | CheckOperandsMatchTarget(b, b->ArgsOperandOffset() + next_iter_values.Length(), |
| 2706 | exit_values.Length(), loop, loop->Results()); |
| 2707 | } |
| 2708 | |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2709 | void Validator::CheckContinue(const Continue* c) { |
| 2710 | auto* loop = c->Loop(); |
| 2711 | if (loop == nullptr) { |
| 2712 | AddError(c) << "has no associated loop"; |
| 2713 | return; |
| 2714 | } |
| 2715 | if (!TransitivelyHolds(loop->Body(), c)) { |
| 2716 | if (control_stack_.Any(Eq<const ControlInstruction*>(loop))) { |
| 2717 | AddError(c) << "must only be called from loop body"; |
| 2718 | } else { |
| 2719 | AddError(c) << "called outside of associated loop"; |
| 2720 | } |
| 2721 | } |
| 2722 | |
Ben Clayton | fac459a | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2723 | if (auto* cont = loop->Continuing()) { |
| 2724 | CheckOperandsMatchTarget(c, Continue::kArgsOperandOffset, c->Args().Length(), cont, |
| 2725 | cont->Params()); |
| 2726 | } |
| 2727 | |
Ben Clayton | 7b35ff1 | 2024-05-13 16:44:35 +0000 | [diff] [blame] | 2728 | first_continues_.Add(loop, c); |
| 2729 | } |
| 2730 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2731 | void Validator::CheckExit(const Exit* e) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2732 | if (e->ControlInstruction() == nullptr) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2733 | AddError(e) << "has no parent control instruction"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | } |
| 2736 | |
| 2737 | if (control_stack_.IsEmpty()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2738 | AddError(e) << "found outside all control instructions"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2739 | return; |
| 2740 | } |
| 2741 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2742 | auto args = e->Args(); |
Ben Clayton | 48360ea | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2743 | CheckOperandsMatchTarget(e, e->ArgsOperandOffset(), args.Length(), e->ControlInstruction(), |
| 2744 | e->ControlInstruction()->Results()); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2745 | |
| 2746 | tint::Switch( |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2747 | e, // |
| 2748 | [&](const ir::ExitIf* i) { CheckExitIf(i); }, // |
| 2749 | [&](const ir::ExitLoop* l) { CheckExitLoop(l); }, // |
| 2750 | [&](const ir::ExitSwitch* s) { CheckExitSwitch(s); }, // |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2751 | [&](Default) { AddError(e) << "missing validation"; }); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 2754 | void Validator::CheckNextIteration(const NextIteration* n) { |
| 2755 | auto* loop = n->Loop(); |
| 2756 | if (loop == nullptr) { |
| 2757 | AddError(n) << "has no associated loop"; |
| 2758 | return; |
| 2759 | } |
| 2760 | if (!TransitivelyHolds(loop->Initializer(), n) && !TransitivelyHolds(loop->Continuing(), n)) { |
| 2761 | if (control_stack_.Any(Eq<const ControlInstruction*>(loop))) { |
| 2762 | AddError(n) << "must only be called from loop initializer or continuing"; |
| 2763 | } else { |
| 2764 | AddError(n) << "called outside of associated loop"; |
| 2765 | } |
| 2766 | } |
Ben Clayton | 6654d1f | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2767 | |
| 2768 | if (auto* body = loop->Body()) { |
| 2769 | CheckOperandsMatchTarget(n, NextIteration::kArgsOperandOffset, n->Args().Length(), body, |
| 2770 | body->Params()); |
| 2771 | } |
Ben Clayton | cabf622 | 2024-05-13 17:11:12 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2774 | void Validator::CheckExitIf(const ExitIf* e) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2775 | if (control_stack_.Back() != e->If()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2776 | AddError(e) << "if target jumps over other control instructions"; |
| 2777 | AddNote(control_stack_.Back()) << "first control instruction jumped"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2781 | void Validator::CheckReturn(const Return* ret) { |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 2782 | if (!CheckResultsAndOperandRange(ret, Return::kNumResults, Return::kMinOperands, |
| 2783 | Return::kMaxOperands)) { |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 2784 | return; |
| 2785 | } |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 2786 | |
| 2787 | auto* func = ret->Func(); |
| 2788 | if (func == nullptr) { |
Ryan Harrison | 2cbfbd4 | 2024-09-23 16:59:01 +0000 | [diff] [blame] | 2789 | // Func() returning nullptr after CheckResultsAndOperandRange is due to the first |
| 2790 | // operand being not a function |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 2791 | AddError(ret) << "expected function for first operand"; |
| 2792 | return; |
| 2793 | } |
| 2794 | |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 2795 | if (func->ReturnType()->Is<core::type::Void>()) { |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 2796 | if (ret->Value()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2797 | AddError(ret) << "unexpected return value"; |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 2798 | } |
| 2799 | } else { |
| 2800 | if (!ret->Value()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2801 | AddError(ret) << "expected return value"; |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 2802 | } else if (ret->Value()->Type() != func->ReturnType()) { |
Ryan Harrison | 8570a64 | 2024-08-15 21:33:38 +0000 | [diff] [blame] | 2803 | AddError(ret) << "return value type " << NameOf(ret->Value()->Type()) |
| 2804 | << " does not match function return type " << NameOf(func->ReturnType()); |
James Price | 17d6eda | 2023-07-21 15:08:02 +0000 | [diff] [blame] | 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
Ryan Harrison | e2d11b0 | 2024-09-24 19:32:29 +0000 | [diff] [blame] | 2809 | void Validator::CheckUnreachable(const Unreachable* u) { |
| 2810 | CheckResultsAndOperands(u, Unreachable::kNumResults, Unreachable::kNumOperands); |
| 2811 | } |
| 2812 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2813 | void Validator::CheckControlsAllowingIf(const Exit* exit, const Instruction* control) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2814 | bool found = false; |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 2815 | for (auto ctrl : tint::Reverse(control_stack_)) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2816 | if (ctrl == control) { |
| 2817 | found = true; |
| 2818 | break; |
| 2819 | } |
| 2820 | // A exit switch can step over if instructions, but no others. |
| 2821 | if (!ctrl->Is<ir::If>()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2822 | AddError(exit) << control->FriendlyName() |
| 2823 | << " target jumps over other control instructions"; |
| 2824 | AddNote(ctrl) << "first control instruction jumped"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2825 | return; |
| 2826 | } |
| 2827 | } |
| 2828 | if (!found) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2829 | AddError(exit) << control->FriendlyName() << " not found in parent control instructions"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2833 | void Validator::CheckExitSwitch(const ExitSwitch* s) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2834 | CheckControlsAllowingIf(s, s->ControlInstruction()); |
| 2835 | } |
| 2836 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2837 | void Validator::CheckExitLoop(const ExitLoop* l) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2838 | CheckControlsAllowingIf(l, l->ControlInstruction()); |
| 2839 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2840 | const Instruction* inst = l; |
| 2841 | const Loop* control = l->Loop(); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2842 | while (inst) { |
| 2843 | // Found parent loop |
| 2844 | if (inst->Block()->Parent() == control) { |
| 2845 | if (inst->Block() == control->Continuing()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2846 | AddError(l) << "loop exit jumps out of continuing block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2847 | if (control->Continuing() != l->Block()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2848 | AddNote(control->Continuing()) << "in continuing block"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2849 | } |
| 2850 | } else if (inst->Block() == control->Initializer()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2851 | AddError(l) << "loop exit not permitted in loop initializer"; |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2852 | if (control->Initializer() != l->Block()) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2853 | AddNote(control->Initializer()) << "in initializer block"; |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2854 | } |
| 2855 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2856 | break; |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2857 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2858 | inst = inst->Block()->Parent(); |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2859 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2860 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2861 | |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 2862 | void Validator::CheckLoad(const Load* l) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 2863 | if (!CheckResultsAndOperands(l, Load::kNumResults, Load::kNumOperands)) { |
Ryan Harrison | 23c42fc | 2024-07-12 02:32:25 +0000 | [diff] [blame] | 2864 | return; |
| 2865 | } |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 2866 | |
| 2867 | if (auto* from = l->From()) { |
| 2868 | auto* mv = from->Type()->As<core::type::MemoryView>(); |
| 2869 | if (!mv) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2870 | AddError(l, Load::kFromOperandOffset) << "load source operand is not a memory view"; |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 2871 | return; |
| 2872 | } |
| 2873 | if (l->Result(0)->Type() != mv->StoreType()) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2874 | AddError(l, Load::kFromOperandOffset) |
| 2875 | << "result type " << style::Type(l->Result(0)->Type()->FriendlyName()) |
| 2876 | << " does not match source store type " |
| 2877 | << style::Type(mv->StoreType()->FriendlyName()); |
James Price | 87a211c | 2024-01-11 15:11:17 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2882 | void Validator::CheckStore(const Store* s) { |
Ryan Harrison | 244e32b | 2024-07-15 23:08:14 +0000 | [diff] [blame] | 2883 | if (!CheckResultsAndOperands(s, Store::kNumResults, Store::kNumOperands)) { |
Ryan Harrison | ad42394 | 2024-07-12 00:48:07 +0000 | [diff] [blame] | 2884 | return; |
| 2885 | } |
James Price | 8fade13 | 2023-09-20 11:42:42 +0000 | [diff] [blame] | 2886 | |
| 2887 | if (auto* from = s->From()) { |
| 2888 | if (auto* to = s->To()) { |
dan sinclair | 7cd0dfe | 2024-07-11 04:41:29 +0000 | [diff] [blame] | 2889 | auto* mv = As<core::type::MemoryView>(to->Type()); |
Ben Clayton | 9887c6e | 2024-01-09 22:50:11 +0000 | [diff] [blame] | 2890 | if (!mv) { |
James Price | 1b0c895 | 2024-05-30 14:32:11 +0000 | [diff] [blame] | 2891 | AddError(s, Store::kToOperandOffset) << "store target operand is not a memory view"; |
Ben Clayton | 9887c6e | 2024-01-09 22:50:11 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | } |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2894 | auto* value_type = from->Type(); |
| 2895 | auto* store_type = mv->StoreType(); |
Ryan Harrison | 54b6c1c | 2024-07-29 20:50:19 +0000 | [diff] [blame] | 2896 | if (value_type != store_type) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2897 | AddError(s, Store::kFromOperandOffset) |
| 2898 | << "value type " << style::Type(value_type->FriendlyName()) |
| 2899 | << " does not match store type " << style::Type(store_type->FriendlyName()); |
James Price | 8fade13 | 2023-09-20 11:42:42 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | } |
| 2904 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2905 | void Validator::CheckLoadVectorElement(const LoadVectorElement* l) { |
Ryan Harrison | 585946e | 2024-07-25 19:18:49 +0000 | [diff] [blame] | 2906 | if (!CheckResultsAndOperands(l, LoadVectorElement::kNumResults, |
| 2907 | LoadVectorElement::kNumOperands)) { |
| 2908 | return; |
| 2909 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2910 | |
Ben Clayton | a89d664 | 2023-11-17 22:41:32 +0000 | [diff] [blame] | 2911 | if (auto* res = l->Result(0)) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2912 | if (auto* el_ty = GetVectorPtrElementType(l, LoadVectorElement::kFromOperandOffset)) { |
| 2913 | if (res->Type() != el_ty) { |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2914 | AddResultError(l, 0) << "result type " << style::Type(res->Type()->FriendlyName()) |
| 2915 | << " does not match vector pointer element type " |
| 2916 | << style::Type(el_ty->FriendlyName()); |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2920 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2921 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2922 | void Validator::CheckStoreVectorElement(const StoreVectorElement* s) { |
Ryan Harrison | 773bce4 | 2024-07-30 00:38:07 +0000 | [diff] [blame] | 2923 | if (!CheckResultsAndOperands(s, StoreVectorElement::kNumResults, |
| 2924 | StoreVectorElement::kNumOperands)) { |
| 2925 | return; |
| 2926 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2927 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2928 | if (auto* value = s->Value()) { |
| 2929 | if (auto* el_ty = GetVectorPtrElementType(s, StoreVectorElement::kToOperandOffset)) { |
| 2930 | if (value->Type() != el_ty) { |
Ben Clayton | c27315a | 2024-02-26 20:24:06 +0000 | [diff] [blame] | 2931 | AddError(s, StoreVectorElement::kValueOperandOffset) |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2932 | << "value type " << style::Type(value->Type()->FriendlyName()) |
| 2933 | << " does not match vector pointer element type " |
| 2934 | << style::Type(el_ty->FriendlyName()); |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2935 | } |
| 2936 | } |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2937 | } |
| 2938 | } |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2939 | |
Ben Clayton | c951b86 | 2024-05-22 07:36:00 +0000 | [diff] [blame] | 2940 | void Validator::CheckOperandsMatchTarget(const Instruction* source_inst, |
| 2941 | size_t source_operand_offset, |
| 2942 | size_t source_operand_count, |
| 2943 | const CastableBase* target, |
| 2944 | VectorRef<const Value*> target_values) { |
| 2945 | if (source_operand_count != target_values.Length()) { |
| 2946 | auto values = [&](size_t n) { return n == 1 ? " value" : " values"; }; |
| 2947 | AddError(source_inst) << "provides " << source_operand_count << values(source_operand_count) |
| 2948 | << " but " << NameOf(target) << " expects " << target_values.Length() |
| 2949 | << values(target_values.Length()); |
| 2950 | AddDeclarationNote(target); |
| 2951 | } |
| 2952 | size_t count = std::min(source_operand_count, target_values.Length()); |
| 2953 | for (size_t i = 0; i < count; i++) { |
| 2954 | auto* source_value = source_inst->Operand(source_operand_offset + i); |
| 2955 | auto* target_value = target_values[i]; |
| 2956 | if (!source_value || !target_value) { |
| 2957 | continue; // Caller should be checking operands are not null |
| 2958 | } |
| 2959 | auto* source_type = source_value->Type(); |
| 2960 | auto* target_type = target_value->Type(); |
| 2961 | if (source_type != target_type) { |
| 2962 | AddError(source_inst, source_operand_offset + i) |
| 2963 | << "operand with type " << style::Type(source_type->FriendlyName()) |
| 2964 | << " does not match " << NameOf(target) << " target type " |
| 2965 | << style::Type(target_type->FriendlyName()); |
| 2966 | AddDeclarationNote(target_value); |
| 2967 | } |
| 2968 | } |
| 2969 | } |
| 2970 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 2971 | const core::type::Type* Validator::GetVectorPtrElementType(const Instruction* inst, size_t idx) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2972 | auto* operand = inst->Operands()[idx]; |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2973 | if (DAWN_UNLIKELY(!operand)) { |
Ben Clayton | a8fce7c | 2023-07-12 18:39:00 +0000 | [diff] [blame] | 2974 | return nullptr; |
| 2975 | } |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 2976 | |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2977 | auto* type = operand->Type(); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2978 | if (DAWN_UNLIKELY(!type)) { |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2979 | return nullptr; |
| 2980 | } |
| 2981 | |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2982 | auto* memory_view_ty = type->As<core::type::MemoryView>(); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2983 | if (DAWN_LIKELY(memory_view_ty)) { |
Ben Clayton | 120ca8e | 2024-03-28 16:22:29 +0000 | [diff] [blame] | 2984 | auto* vec_ty = memory_view_ty->StoreType()->As<core::type::Vector>(); |
dan sinclair | a6941c6 | 2024-08-29 21:34:20 +0000 | [diff] [blame] | 2985 | if (DAWN_LIKELY(vec_ty)) { |
dan sinclair | eb2da25 | 2024-08-26 16:57:23 +0000 | [diff] [blame] | 2986 | return vec_ty->Type(); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
Ben Clayton | 0a60d52 | 2024-05-09 16:42:22 +0000 | [diff] [blame] | 2990 | AddError(inst, idx) << "operand must be a pointer to vector, got " |
| 2991 | << style::Type(type->FriendlyName()); |
dan sinclair | e4952f3 | 2023-07-14 17:47:29 +0000 | [diff] [blame] | 2992 | return nullptr; |
| 2993 | } |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 2994 | |
Ben Clayton | 4ebed9d | 2023-09-05 14:39:05 +0000 | [diff] [blame] | 2995 | } // namespace |
| 2996 | |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 2997 | Result<SuccessType> Validate(const Module& mod, Capabilities capabilities) { |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 2998 | Validator v(mod, capabilities); |
Ben Clayton | 16fb254 | 2023-09-25 11:43:19 +0000 | [diff] [blame] | 2999 | return v.Run(); |
dan sinclair | 5ccafa4 | 2023-06-03 14:57:42 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Ben Clayton | 1e7b312 | 2023-11-20 12:38:31 +0000 | [diff] [blame] | 3002 | Result<SuccessType> ValidateAndDumpIfNeeded([[maybe_unused]] const Module& ir, |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 3003 | [[maybe_unused]] const char* msg, |
Ben Clayton | 7711bfc | 2024-03-28 17:38:16 +0000 | [diff] [blame] | 3004 | [[maybe_unused]] Capabilities capabilities) { |
James Price | 2eae44a | 2023-09-20 23:24:15 +0000 | [diff] [blame] | 3005 | #if TINT_DUMP_IR_WHEN_VALIDATING |
James Price | 2ee4546 | 2024-05-03 17:37:14 +0000 | [diff] [blame] | 3006 | auto printer = StyledTextPrinter::Create(stdout); |
dan sinclair | 8700be5 | 2024-05-31 18:07:26 +0000 | [diff] [blame] | 3007 | std::cout << "=========================================================\n"; |
| 3008 | std::cout << "== IR dump before " << msg << ":\n"; |
| 3009 | std::cout << "=========================================================\n"; |
James Price | 57c1580 | 2024-06-13 06:23:47 +0000 | [diff] [blame] | 3010 | printer->Print(Disassembler(ir).Text()); |
James Price | 2eae44a | 2023-09-20 23:24:15 +0000 | [diff] [blame] | 3011 | #endif |
| 3012 | |
James Price | db46be1 | 2023-08-01 17:15:35 +0000 | [diff] [blame] | 3013 | #ifndef NDEBUG |
James Price | 03ecbbf | 2024-01-17 17:01:30 +0000 | [diff] [blame] | 3014 | auto result = Validate(ir, capabilities); |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 3015 | if (result != Success) { |
Ben Clayton | 16fb254 | 2023-09-25 11:43:19 +0000 | [diff] [blame] | 3016 | return result.Failure(); |
James Price | db46be1 | 2023-08-01 17:15:35 +0000 | [diff] [blame] | 3017 | } |
| 3018 | #endif |
| 3019 | |
James Price | db46be1 | 2023-08-01 17:15:35 +0000 | [diff] [blame] | 3020 | return Success; |
| 3021 | } |
| 3022 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 3023 | } // namespace tint::core::ir |
dan sinclair | fbc4ce7 | 2024-10-21 19:25:39 +0000 | [diff] [blame] | 3024 | |
| 3025 | namespace std { |
| 3026 | |
| 3027 | template <> |
| 3028 | struct hash<tint::core::ir::ValidatedType> { |
| 3029 | size_t operator()(const tint::core::ir::ValidatedType& v) const { return Hash(v.ty, v.caps); } |
| 3030 | }; |
| 3031 | |
| 3032 | template <> |
| 3033 | struct equal_to<tint::core::ir::ValidatedType> { |
| 3034 | bool operator()(const tint::core::ir::ValidatedType& a, |
| 3035 | const tint::core::ir::ValidatedType& b) const { |
| 3036 | return a.ty->Equals(*(b.ty)) && a.caps == b.caps; |
| 3037 | } |
| 3038 | }; |
| 3039 | |
| 3040 | } // namespace std |