blob: 1bee1870b1d151484cec2ff6f14659416490d05f [file] [log] [blame]
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "gmock/gmock.h"
#include "src/reader/spirv/parser_impl.h"
#include "src/reader/spirv/parser_impl_test_helper.h"
#include "src/reader/spirv/spirv_tools_helpers_test.h"
namespace tint {
namespace reader {
namespace spirv {
namespace {
using ::testing::HasSubstr;
using ::testing::Not;
TEST_F(SpvParserTest, ModuleScopeVar_NoVar) {
auto p = parser(test::Assemble(""));
EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty());
const auto module_ast = p->module().to_str();
EXPECT_THAT(module_ast, Not(HasSubstr("Variable")));
}
TEST_F(SpvParserTest, ModuleScopeVar_BadStorageClass) {
auto p = parser(test::Assemble(R"(
%float = OpTypeFloat 32
%ptr = OpTypePointer CrossWorkgroup %float
%52 = OpVariable %ptr CrossWorkgroup
)"));
EXPECT_TRUE(p->BuildInternalModule());
// Normally we should run ParserImpl::RegisterTypes before emitting
// variables. But defensive coding in EmitModuleScopeVariables lets
// us catch this error.
EXPECT_FALSE(p->EmitModuleScopeVariables()) << p->error();
EXPECT_THAT(p->error(), HasSubstr("unknown SPIR-V storage class: 5"));
}
TEST_F(SpvParserTest, ModuleScopeVar_BadPointerType) {
auto p = parser(test::Assemble(R"(
%float = OpTypeFloat 32
%fn_ty = OpTypeFunction %float
%fn_ptr_ty = OpTypePointer Private %fn_ty
%52 = OpVariable %ptr Private
)"));
EXPECT_TRUE(p->BuildInternalModule());
// Normally we should run ParserImpl::RegisterTypes before emitting
// variables. But defensive coding in EmitModuleScopeVariables lets
// us catch this error.
EXPECT_FALSE(p->EmitModuleScopeVariables());
EXPECT_THAT(p->error(), HasSubstr("internal error: failed to register Tint "
"AST type for SPIR-V type with ID: 4"));
}
TEST_F(SpvParserTest, ModuleScopeVar_AnonWorkgroupVar) {
auto p = parser(test::Assemble(R"(
%float = OpTypeFloat 32
%ptr = OpTypePointer Workgroup %float
%52 = OpVariable %ptr Workgroup
)"));
EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
x_52
workgroup
__f32
})"));
}
TEST_F(SpvParserTest, ModuleScopeVar_NamedWorkgroupVar) {
auto p = parser(test::Assemble(R"(
OpName %52 "the_counter"
%float = OpTypeFloat 32
%ptr = OpTypePointer Workgroup %float
%52 = OpVariable %ptr Workgroup
)"));
EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
the_counter
workgroup
__f32
})"));
}
TEST_F(SpvParserTest, ModuleScopeVar_PrivateVar) {
auto p = parser(test::Assemble(R"(
OpName %52 "my_own_private_idaho"
%float = OpTypeFloat 32
%ptr = OpTypePointer Private %float
%52 = OpVariable %ptr Private
)"));
EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
my_own_private_idaho
private
__f32
})"));
}
} // namespace
} // namespace spirv
} // namespace reader
} // namespace tint