third_party_spirv-tools/test/ImmediateInt.cpp

316 lines
13 KiB
C++
Raw Normal View History

// Copyright (c) 2015 The Khronos Group Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and/or associated documentation files (the
// "Materials"), to deal in the Materials without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Materials, and to
// permit persons to whom the Materials are furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Materials.
//
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
// https://www.khronos.org/registry/
//
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include <cassert>
2015-09-11 20:34:49 +00:00
#include <string>
#include <vector>
#include <gmock/gmock.h>
#include "TestFixture.h"
#include "util/bitutils.h"
namespace {
using spvtest::Concatenate;
using spvtest::MakeInstruction;
using spvtest::TextToBinaryTest;
using spvutils::BitwiseCast;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::StrEq;
const auto kCAF = SPV_ASSEMBLY_SYNTAX_FORMAT_CANONICAL;
TEST_F(TextToBinaryTest, ImmediateIntOpCode) {
SetText("!0x00FF00FF");
ASSERT_EQ(SPV_SUCCESS,
spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
EXPECT_EQ(0x00FF00FF, binary->code[5]);
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}
}
TEST_F(TextToBinaryTest, ImmediateIntOperand) {
SetText("OpCapability !0x00FF00FF");
EXPECT_EQ(SPV_SUCCESS,
spvTextToBinary(text.str, text.length, opcodeTable, operandTable,
extInstTable, &binary, &diagnostic));
EXPECT_EQ(0x00FF00FF, binary->code[6]);
if (diagnostic) {
spvDiagnosticPrint(diagnostic);
}
}
using ImmediateIntTest = TextToBinaryTest;
TEST_F(ImmediateIntTest, AnyWordInSimpleStatement) {
EXPECT_THAT(CompiledInstructions("!0x00040018 %a %b %123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 3})));
EXPECT_THAT(CompiledInstructions("OpTypeMatrix !1 %b %123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 1, 2})));
EXPECT_THAT(CompiledInstructions("OpTypeMatrix %1 !2 %123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 2})));
EXPECT_THAT(CompiledInstructions("OpTypeMatrix %a %b !123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 123})));
EXPECT_THAT(CompiledInstructions("!0x00040018 %1 !2 %123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 2})));
EXPECT_THAT(CompiledInstructions("OpTypeMatrix !1 %b !123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 1, 123})));
EXPECT_THAT(CompiledInstructions("!0x00040018 !1 !2 !123", kCAF),
Eq(MakeInstruction(SpvOpTypeMatrix, {1, 2, 123})));
}
TEST_F(ImmediateIntTest, AnyWordAfterEqualsAndOpCode) {
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 %c 123"),
Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 2, 123})));
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 123"),
Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b %c !123"),
Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength %b !3 !123"),
Eq(MakeInstruction(SpvOpArrayLength, {1, 2, 3, 123})));
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 123"),
Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 3, 123})));
EXPECT_THAT(CompiledInstructions("%a = OpArrayLength !2 !3 !123"),
Eq(MakeInstruction(SpvOpArrayLength, {2, 1, 3, 123})));
}
TEST_F(ImmediateIntTest, ResultIdInAssignment) {
EXPECT_EQ("!2 not allowed before =.",
CompileFailure("!2 = OpArrayLength %12 %1 123"));
EXPECT_EQ("!2 not allowed before =.",
CompileFailure("!2 = !0x00040044 %12 %1 123"));
}
TEST_F(ImmediateIntTest, OpCodeInAssignment) {
EXPECT_EQ("Invalid Opcode prefix '!0x00040044'.",
CompileFailure("%2 = !0x00040044 %12 %1 123"));
}
// Literal integers after !<integer> are handled correctly.
TEST_F(ImmediateIntTest, IntegerFollowingImmediate) {
2015-09-28 15:51:32 +00:00
const SpirvVector original = CompiledInstructions("OpTypeInt %1 8 1", kCAF);
EXPECT_EQ(original, CompiledInstructions("!0x00040015 1 8 1", kCAF));
EXPECT_EQ(original, CompiledInstructions("OpTypeInt !1 8 1", kCAF));
// With !<integer>, we can (and can only) accept 32-bit number literals,
// even when we declare the return type is 64-bit.
EXPECT_EQ(Concatenate({
MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
MakeInstruction(SpvOpConstant, {1, 2, 4294967295}),
}),
CompiledInstructions("OpTypeInt %i64 64 0\n"
"OpConstant %i64 !2 4294967295",
kCAF));
// 64-bit integer literal.
EXPECT_EQ("Invalid word following !<integer>: 5000000000",
CompileFailure("OpConstant !1 %2 5000000000", kCAF));
EXPECT_EQ("Invalid word following !<integer>: 5000000000",
CompileFailure("OpTypeInt %i64 64 0\n"
"OpConstant %i64 !2 5000000000",
kCAF));
// Negative integer.
EXPECT_EQ(CompiledInstructions("OpTypeInt %i64 32 1\n"
"OpConstant %i64 %2 -123",
kCAF),
CompiledInstructions("OpTypeInt %i64 32 1\n"
"OpConstant %i64 !2 -123",
kCAF));
2015-09-08 18:20:12 +00:00
2015-09-29 21:07:21 +00:00
// TODO(deki): uncomment assertions below and make them pass.
2015-09-08 18:20:12 +00:00
// Hex value(s).
// EXPECT_EQ(CompileSuccessfully("OpConstant %10 %1 0x12345678", kCAF),
// CompileSuccessfully("OpConstant %10 !1 0x12345678", kCAF));
// EXPECT_EQ(
// CompileSuccessfully("OpConstant %10 %1 0x12345678 0x87654321", kCAF),
// CompileSuccessfully("OpConstant %10 !1 0x12345678 0x87654321", kCAF));
}
// Literal floats after !<integer> are handled correctly.
TEST_F(ImmediateIntTest, FloatFollowingImmediate) {
EXPECT_EQ(
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 %2 0.123", kCAF),
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 !2 0.123", kCAF));
EXPECT_EQ(
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 %2 -0.5", kCAF),
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 !2 -0.5", kCAF));
EXPECT_EQ(
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 %2 0.123", kCAF),
CompiledInstructions("OpTypeFloat %1 32\n!0x0004002b %1 %2 0.123", kCAF));
EXPECT_EQ(
CompiledInstructions("OpTypeFloat %1 32\nOpConstant %1 %2 -0.5", kCAF),
CompiledInstructions("OpTypeFloat %1 32\n!0x0004002b %1 %2 -0.5", kCAF));
EXPECT_EQ(Concatenate({
MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
MakeInstruction(SpvOpConstant, {1, 2, 0xb, 0xa}),
MakeInstruction(SpvOpSwitch,
{2, 1234, BitwiseCast<uint32_t>(2.5f), 3}),
}),
CompiledInstructions("%i64 = OpTypeInt 64 0\n"
"%big = OpConstant %i64 0xa0000000b\n"
"OpSwitch %big !1234 2.5 %target\n"));
}
// Literal strings after !<integer> are handled correctly.
TEST_F(ImmediateIntTest, StringFollowingImmediate) {
// Try a variety of strings, including empty and single-character.
2015-09-11 20:34:49 +00:00
for (std::string name : {"", "s", "longish", "really looooooooooooooooong"}) {
const SpirvVector original =
CompiledInstructions("OpMemberName %10 4 \"" + name + "\"", kCAF);
EXPECT_EQ(original, CompiledInstructions(
"OpMemberName %10 !4 \"" + name + "\"", kCAF))
2015-09-11 20:34:49 +00:00
<< name;
EXPECT_EQ(original,
CompiledInstructions("OpMemberName !1 !4 \"" + name + "\"", kCAF))
2015-09-11 20:34:49 +00:00
<< name;
const uint32_t wordCount = 4 + name.size() / 4;
const uint32_t firstWord = spvOpcodeMake(wordCount, SpvOpMemberName);
EXPECT_EQ(original, CompiledInstructions("!" + std::to_string(firstWord) +
" %10 !4 \"" + name + "\"",
kCAF))
2015-09-11 20:34:49 +00:00
<< name;
}
}
// IDs after !<integer> are handled correctly.
TEST_F(ImmediateIntTest, IdFollowingImmediate) {
EXPECT_EQ(CompileSuccessfully("OpDecorationGroup %123", kCAF),
CompileSuccessfully("!0x00020049 %123", kCAF));
EXPECT_EQ(CompileSuccessfully("OpDecorationGroup %group", kCAF),
CompileSuccessfully("!0x00020049 %group", kCAF));
}
// !<integer> after !<integer> is handled correctly.
TEST_F(ImmediateIntTest, ImmediateFollowingImmediate) {
const SpirvVector original =
CompiledInstructions("OpTypeMatrix %a %b 7", kCAF);
EXPECT_EQ(original, CompiledInstructions("OpTypeMatrix %a !2 !7", kCAF));
EXPECT_EQ(original, CompiledInstructions("!0x00040018 %a !2 !7", kCAF));
}
TEST_F(ImmediateIntTest, InvalidStatement) {
EXPECT_THAT(
Subvector(CompileSuccessfully("!4 !3 !2 !1", kCAF), kFirstInstruction),
ElementsAre(4, 3, 2, 1));
}
TEST_F(ImmediateIntTest, InvalidStatementBetweenValidOnes) {
EXPECT_THAT(Subvector(CompileSuccessfully(
"OpTypeFloat %10 32 !5 !6 !7 OpEmitVertex", kCAF),
kFirstInstruction),
ElementsAre(spvOpcodeMake(3, SpvOpTypeFloat), 1, 32, 5, 6, 7,
spvOpcodeMake(1, SpvOpEmitVertex)));
}
TEST_F(ImmediateIntTest, NextOpcodeRecognized) {
const SpirvVector original = CompileSuccessfully(R"(
OpLoad %10 %1 %2 Volatile
OpCompositeInsert %11 %4 %1 %3 0 1 2
)",
kCAF);
const SpirvVector alternate = CompileSuccessfully(R"(
OpLoad %10 %1 %2 !1
OpCompositeInsert %11 %4 %1 %3 0 1 2
)",
kCAF);
EXPECT_EQ(original, alternate);
}
TEST_F(ImmediateIntTest, WrongLengthButNextOpcodeStillRecognized) {
const SpirvVector original = CompileSuccessfully(R"(
OpLoad %10 %1 %2 Volatile
OpCopyMemorySized %3 %4 %1
)",
kCAF);
const SpirvVector alternate = CompileSuccessfully(R"(
!0x0002003D %10 %1 %2 !1
OpCopyMemorySized %3 %4 %1
)",
kCAF);
EXPECT_EQ(0x0002003D, alternate[kFirstInstruction]);
EXPECT_EQ(Subvector(original, kFirstInstruction + 1),
Subvector(alternate, kFirstInstruction + 1));
}
// Like NextOpcodeRecognized, but next statement is in assignment form.
2015-09-11 20:34:49 +00:00
TEST_F(ImmediateIntTest, NextAssignmentRecognized) {
const SpirvVector original = CompileSuccessfully(R"(
%1 = OpLoad %10 %2 None
%4 = OpFunctionCall %10 %3 %123
2015-09-02 21:17:42 +00:00
)");
const SpirvVector alternate = CompileSuccessfully(R"(
2015-09-11 20:34:49 +00:00
%1 = OpLoad %10 %2 !0
%4 = OpFunctionCall %10 %3 %123
2015-09-02 21:17:42 +00:00
)");
EXPECT_EQ(original, alternate);
}
// Two instructions in a row each have !<integer> opcode.
TEST_F(ImmediateIntTest, ConsecutiveImmediateOpcodes) {
const SpirvVector original = CompileSuccessfully(R"(
%1 = OpConstantSampler %10 Clamp 78 Linear
%4 = OpFRem %11 %3 %2
%5 = OpIsValidEvent %12 %2
)");
const SpirvVector alternate = CompileSuccessfully(R"(
!0x0006002D %10 %1 !2 78 !1
!0x0005008C %11 %4 %3 %2
%5 = OpIsValidEvent %12 %2
)");
EXPECT_EQ(original, alternate);
}
// !<integer> followed by, eg, an enum or '=' or a random bareword.
TEST_F(ImmediateIntTest, ForbiddenOperands) {
EXPECT_THAT(CompileFailure("OpMemoryModel !0 OpenCL"), HasSubstr("OpenCL"));
EXPECT_THAT(CompileFailure("!1 %0 = !2"), HasSubstr("="));
EXPECT_THAT(CompileFailure("OpMemoryModel !0 random_bareword"),
HasSubstr("random_bareword"));
// Immediate integers longer than one 32-bit word.
EXPECT_THAT(CompileFailure("!5000000000"), HasSubstr("5000000000"));
2015-09-29 21:07:21 +00:00
EXPECT_THAT(CompileFailure("!999999999999999999"),
HasSubstr("999999999999999999"));
EXPECT_THAT(CompileFailure("!0x00020049 !5000000000"),
HasSubstr("5000000000"));
// Negative numbers.
EXPECT_THAT(CompileFailure("!0x00020049 !-123"), HasSubstr("-123"));
}
TEST_F(ImmediateIntTest, NotInteger) {
EXPECT_THAT(CompileFailure("!abc"), StrEq("Invalid immediate integer: !abc"));
EXPECT_THAT(CompileFailure("!12.3"),
StrEq("Invalid immediate integer: !12.3"));
EXPECT_THAT(CompileFailure("!12K"), StrEq("Invalid immediate integer: !12K"));
}
} // anonymous namespace