blob: b30feaf3b85dcba5a24cc89791ff238a8f34c736 [file] [log] [blame]
dan sinclair0ebbc5c2023-11-29 17:53:24 +00001// Copyright 2023 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/msl/ir/builtin_call.h"
James Pricefd3a0142024-06-11 18:24:06 +000029
30#include "gtest/gtest.h"
31#include "src/tint/lang/core/fluent_types.h"
dan sinclair0ebbc5c2023-11-29 17:53:24 +000032#include "src/tint/lang/core/ir/ir_helper_test.h"
James Pricefd3a0142024-06-11 18:24:06 +000033#include "src/tint/lang/core/ir/validator.h"
34#include "src/tint/lang/core/type/sampled_texture.h"
35#include "src/tint/lang/core/type/texture_dimension.h"
36#include "src/tint/utils/result/result.h"
37
38using namespace tint::core::fluent_types; // NOLINT
dan sinclair0ebbc5c2023-11-29 17:53:24 +000039
40namespace tint::msl::ir {
41namespace {
42
43using namespace tint::core::number_suffixes; // NOLINT
44 //
45using IR_MslBuiltinCallTest = core::ir::IRTestHelper;
46
47TEST_F(IR_MslBuiltinCallTest, Clone) {
48 auto* builtin = b.Call<BuiltinCall>(mod.Types().void_(), BuiltinFn::kThreadgroupBarrier, 0_u);
49
50 auto* new_b = clone_ctx.Clone(builtin);
51
52 EXPECT_NE(builtin, new_b);
53 EXPECT_NE(builtin->Result(0), new_b->Result(0));
54 EXPECT_EQ(mod.Types().void_(), new_b->Result(0)->Type());
55
56 EXPECT_EQ(BuiltinFn::kThreadgroupBarrier, new_b->Func());
57
58 auto args = new_b->Args();
59 EXPECT_EQ(1u, args.Length());
60
61 auto* val0 = args[0]->As<core::ir::Constant>()->Value();
62 EXPECT_EQ(0_u, val0->As<core::constant::Scalar<core::u32>>()->ValueAs<core::u32>());
63}
64
James Pricefd3a0142024-06-11 18:24:06 +000065TEST_F(IR_MslBuiltinCallTest, DoesNotMatchMemberFunction) {
66 auto* t = b.FunctionParam(
67 "t", ty.Get<core::type::SampledTexture>(core::type::TextureDimension::k2d, ty.f32()));
68 auto* s = b.FunctionParam("s", ty.sampler());
69 auto* coords = b.FunctionParam("coords", ty.vec2<f32>());
70 auto* func = b.Function("foo", ty.vec4<f32>());
71 func->SetParams({t, s, coords});
72 b.Append(func->Block(), [&] {
73 auto* result = b.Call<BuiltinCall>(ty.vec4<f32>(), msl::BuiltinFn::kSample, t, s, coords);
74 b.Return(func, result);
75 });
76
77 auto res = core::ir::Validate(mod);
78 ASSERT_NE(res, Success);
79 EXPECT_EQ(
80 res.Failure().reason.Str(),
81 R"(:3:20 error: msl.sample: no matching call to 'msl.sample(texture_2d<f32>, sampler, vec2<f32>)'
82
83 %5:vec4<f32> = msl.sample %t, %s, %coords
84 ^^^^^^^^^^
85
86:2:3 note: in block
87 $B1: {
88 ^^^
89
90note: # Disassembly
91%foo = func(%t:texture_2d<f32>, %s:sampler, %coords:vec2<f32>):vec4<f32> {
92 $B1: {
93 %5:vec4<f32> = msl.sample %t, %s, %coords
94 ret %5
95 }
96}
97)");
98}
99
dan sinclair0ebbc5c2023-11-29 17:53:24 +0000100} // namespace
101} // namespace tint::msl::ir