blob: 9dc0be693eb93c6aed84d6e1199697c1fceef755 [file] [log] [blame]
// Copyright (c) 2015-2016 The Khronos Group Inc.
//
// 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.
// Assembler tests for instructions in the "Extension Instruction" section
// of the SPIR-V spec.
#include "UnitSPIRV.h"
#include "TestFixture.h"
#include "gmock/gmock.h"
#include "spirv/1.0/GLSL.std.450.h"
#include "spirv/1.0/OpenCL.std.h"
namespace {
using spvtest::Concatenate;
using spvtest::MakeInstruction;
using spvtest::MakeVector;
using spvtest::TextToBinaryTest;
using ::testing::Eq;
TEST_F(TextToBinaryTest, InvalidExtInstImportName) {
EXPECT_THAT(CompileFailure("%1 = OpExtInstImport \"Haskell.std\""),
Eq("Invalid extended instruction import 'Haskell.std'"));
}
TEST_F(TextToBinaryTest, InvalidImportId) {
EXPECT_THAT(CompileFailure("%1 = OpTypeVoid\n"
"%2 = OpExtInst %1 %1"),
Eq("Invalid extended instruction import Id 2"));
}
TEST_F(TextToBinaryTest, InvalidImportInstruction) {
const std::string input = R"(%1 = OpTypeVoid
%2 = OpExtInstImport "OpenCL.std"
%3 = OpExtInst %1 %2 not_in_the_opencl)";
EXPECT_THAT(CompileFailure(input),
Eq("Invalid extended instruction name 'not_in_the_opencl'."));
}
TEST_F(TextToBinaryTest, MultiImport) {
const std::string input = R"(%2 = OpExtInstImport "OpenCL.std"
%2 = OpExtInstImport "OpenCL.std")";
EXPECT_THAT(CompileFailure(input),
Eq("Import Id is being defined a second time"));
}
TEST_F(TextToBinaryTest, TooManyArguments) {
const std::string input = R"(%opencl = OpExtInstImport "OpenCL.std"
%2 = OpExtInst %float %opencl cos %x %oops")";
EXPECT_THAT(CompileFailure(input), Eq("Expected '=', found end of stream."));
}
TEST_F(TextToBinaryTest, ExtInstFromTwoDifferentImports) {
const std::string input = R"(%1 = OpExtInstImport "OpenCL.std"
%2 = OpExtInstImport "GLSL.std.450"
%4 = OpExtInst %3 %1 native_sqrt %5
%7 = OpExtInst %6 %2 MatrixInverse %8
)";
// Make sure it assembles correctly.
EXPECT_THAT(
CompiledInstructions(input),
Eq(Concatenate({
MakeInstruction(SpvOpExtInstImport, {1}, MakeVector("OpenCL.std")),
MakeInstruction(SpvOpExtInstImport, {2}, MakeVector("GLSL.std.450")),
MakeInstruction(
SpvOpExtInst,
{3, 4, 1, uint32_t(OpenCLLIB::Entrypoints::Native_sqrt), 5}),
MakeInstruction(SpvOpExtInst,
{6, 7, 2, uint32_t(GLSLstd450MatrixInverse), 8}),
})));
// Make sure it disassembles correctly.
EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
}
} // anonymous namespace