• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/writer/spirv/spv_dump.h"
16 #include "src/writer/spirv/test_helper.h"
17 
18 namespace tint {
19 namespace writer {
20 namespace spirv {
21 namespace {
22 
23 using BuilderTest = TestHelper;
24 
TEST_F(BuilderTest,Bitcast)25 TEST_F(BuilderTest, Bitcast) {
26   auto* bitcast = create<ast::BitcastExpression>(ty.u32(), Expr(2.4f));
27 
28   WrapInFunction(bitcast);
29 
30   spirv::Builder& b = Build();
31 
32   b.push_function(Function{});
33   EXPECT_EQ(b.GenerateBitcastExpression(bitcast), 1u);
34 
35   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
36 %3 = OpTypeFloat 32
37 %4 = OpConstant %3 2.4000001
38 )");
39   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
40             R"(%1 = OpBitcast %2 %4
41 )");
42 }
43 
TEST_F(BuilderTest,Bitcast_DuplicateType)44 TEST_F(BuilderTest, Bitcast_DuplicateType) {
45   auto* bitcast = create<ast::BitcastExpression>(ty.f32(), Expr(2.4f));
46 
47   WrapInFunction(bitcast);
48 
49   spirv::Builder& b = Build();
50 
51   b.push_function(Function{});
52   EXPECT_EQ(b.GenerateBitcastExpression(bitcast), 1u);
53 
54   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
55 %3 = OpConstant %2 2.4000001
56 )");
57   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
58             R"(%1 = OpCopyObject %2 %3
59 )");
60 }
61 
62 }  // namespace
63 }  // namespace spirv
64 }  // namespace writer
65 }  // namespace tint
66