|
| 1 | +// Copyright 2025 The Dawn & Tint Authors |
| 2 | +// |
| 3 | +// Redistribution and use in source and binary forms, with or without |
| 4 | +// modification, are permitted provided that the following conditions are met: |
| 5 | +// |
| 6 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | +// list of conditions and the following disclaimer. |
| 8 | +// |
| 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. |
| 27 | + |
| 28 | +#include "src/tint/lang/core/ir/validator_test.h" |
| 29 | + |
| 30 | +#include "gtest/gtest.h" |
| 31 | + |
| 32 | +#include "src/tint/lang/core/ir/builder.h" |
| 33 | +#include "src/tint/lang/core/ir/validator.h" |
| 34 | +#include "src/tint/lang/core/type/pointer.h" |
| 35 | +#include "src/tint/lang/core/type/struct.h" |
| 36 | + |
| 37 | +namespace tint::core::ir { |
| 38 | + |
| 39 | +using namespace tint::core::fluent_types; // NOLINT |
| 40 | + |
| 41 | +// Helper to make a module-scope immediate variable with given store type. |
| 42 | +static Var* MakeImmediate(Builder& b, |
| 43 | + Module& mod, |
| 44 | + const char* name, |
| 45 | + const core::type::Type* store_ty) { |
| 46 | + auto* v = b.Var(name, core::AddressSpace::kImmediate, store_ty); |
| 47 | + mod.root_block->Append(v); |
| 48 | + return v; |
| 49 | +} |
| 50 | + |
| 51 | +class IR_ValidatorImmediateTest : public IR_ValidatorTest {}; |
| 52 | + |
| 53 | +TEST_F(IR_ValidatorImmediateTest, ValidateSingleUserImmediate_None) { |
| 54 | + // Empty module (no immediates) |
| 55 | + auto res = ValidateSingleUserImmediate(mod); |
| 56 | + ASSERT_EQ(res, Success) << res.Failure(); |
| 57 | + EXPECT_EQ(res.Get(), 0u); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F(IR_ValidatorImmediateTest, ValidateSingleUserImmediate_One) { |
| 61 | + // Single immediate variable with size 12 -> rounded to 12 (already multiple of 4) |
| 62 | + auto* s = ty.Struct(mod.symbols.New("S"), {{mod.symbols.New("a"), ty.array<i32, 3>()}}); |
| 63 | + MakeImmediate(b, mod, "immediate_data", s); |
| 64 | + auto res = ValidateSingleUserImmediate(mod); |
| 65 | + ASSERT_EQ(res, Success) << res.Failure(); |
| 66 | + EXPECT_EQ(res.Get(), 12u); // 3 * 4 bytes |
| 67 | +} |
| 68 | + |
| 69 | +// Round-up behavior is implicitly covered by using a struct whose size is already a multiple of 4. |
| 70 | + |
| 71 | +TEST_F(IR_ValidatorImmediateTest, ValidateSingleUserImmediate_Multiple) { |
| 72 | + auto* s = ty.Struct(mod.symbols.New("S"), {{mod.symbols.New("a"), ty.i32()}}); |
| 73 | + MakeImmediate(b, mod, "immediate0", s); |
| 74 | + MakeImmediate(b, mod, "immediate1", s); |
| 75 | + auto res = ValidateSingleUserImmediate(mod); |
| 76 | + ASSERT_NE(res, Success); |
| 77 | + EXPECT_THAT(res.Failure().reason, |
| 78 | + ::testing::HasSubstr("multiple user-declared immediate data")); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_BasicSuccess) { |
| 82 | + // No user immediate, two internal ranges non-overlapping |
| 83 | + std::vector<ImmediateInfo> immediates = { |
| 84 | + {0u, 16u}, // starts after user (none), size 16 |
| 85 | + {32u, 8u}, |
| 86 | + }; |
| 87 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 88 | + ASSERT_EQ(res, Success) << res.Failure(); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_OverlapUser) { |
| 92 | + // User immediate occupies first 32 bytes |
| 93 | + std::vector<ImmediateInfo> immediates = { |
| 94 | + {16u, 8u}, |
| 95 | + }; |
| 96 | + auto res = ValidateInternalImmediateOffset(0x1000u, 32u, immediates); |
| 97 | + ASSERT_NE(res, Success); |
| 98 | + EXPECT_THAT(res.Failure().reason, |
| 99 | + ::testing::HasSubstr("overlaps user-declared immediate data")); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_ZeroSize) { |
| 103 | + std::vector<ImmediateInfo> immediates = { |
| 104 | + {64u, 0u}, |
| 105 | + }; |
| 106 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 107 | + ASSERT_NE(res, Success); |
| 108 | + EXPECT_THAT(res.Failure().reason, ::testing::HasSubstr("zero size")); |
| 109 | +} |
| 110 | + |
| 111 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_Misaligned) { |
| 112 | + std::vector<ImmediateInfo> immediates = { |
| 113 | + {10u, 4u}, |
| 114 | + }; |
| 115 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 116 | + ASSERT_NE(res, Success); |
| 117 | + EXPECT_THAT(res.Failure().reason, ::testing::HasSubstr("not 4-byte aligned")); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_ExceedsMax) { |
| 121 | + std::vector<ImmediateInfo> immediates = { |
| 122 | + {0x2000u, 4u}, |
| 123 | + }; |
| 124 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 125 | + ASSERT_NE(res, Success); |
| 126 | + EXPECT_THAT(res.Failure().reason, ::testing::HasSubstr("offset exceeds")); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_OffsetPlusSizeExceeds) { |
| 130 | + std::vector<ImmediateInfo> immediates = { |
| 131 | + {0x0FFCu, 16u}, // 0x0FFC + 16 > 0x1000 |
| 132 | + }; |
| 133 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 134 | + ASSERT_NE(res, Success); |
| 135 | + EXPECT_THAT(res.Failure().reason, ::testing::HasSubstr("(offset + size)")); |
| 136 | +} |
| 137 | + |
| 138 | +TEST_F(IR_ValidatorImmediateTest, ValidateInternalImmediateOffset_RangesOverlap) { |
| 139 | + std::vector<ImmediateInfo> immediates = { |
| 140 | + {64u, 32u}, {80u, 16u}, // overlaps previous (64..96) vs (80..96) |
| 141 | + }; |
| 142 | + auto res = ValidateInternalImmediateOffset(0x1000u, 0u, immediates); |
| 143 | + ASSERT_NE(res, Success); |
| 144 | + EXPECT_THAT(res.Failure().reason, ::testing::HasSubstr("ranges overlap")); |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace tint::core::ir |
0 commit comments