2019-07-31 01:21:25 +08:00
|
|
|
//===- DeserializationTest.cpp - SPIR-V Deserialization Tests -------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-07-31 01:21:25 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-31 01:21:25 +08:00
|
|
|
//
|
|
|
|
// The purpose of this file is to provide negative deserialization tests.
|
|
|
|
// For positive deserialization tests, please use serialization and
|
|
|
|
// deserialization for roundtripping.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-12-15 00:40:34 +08:00
|
|
|
#include "mlir/Target/SPIRV/Deserialization.h"
|
2020-12-17 23:55:45 +08:00
|
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
|
|
|
|
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
|
2019-07-31 01:21:25 +08:00
|
|
|
#include "mlir/IR/Diagnostics.h"
|
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2020-12-15 00:40:34 +08:00
|
|
|
#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
|
2019-07-31 01:21:25 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
using ::testing::StrEq;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Test Fixture
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// A deserialization test fixture providing minimal SPIR-V building and
|
|
|
|
/// diagnostic checking utilities.
|
|
|
|
class DeserializationTest : public ::testing::Test {
|
|
|
|
protected:
|
2020-10-24 04:19:35 +08:00
|
|
|
DeserializationTest() {
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
context.getOrLoadDialect<mlir::spirv::SPIRVDialect>();
|
2019-07-31 01:21:25 +08:00
|
|
|
// Register a diagnostic handler to capture the diagnostic so that we can
|
|
|
|
// check it later.
|
2019-09-24 02:24:28 +08:00
|
|
|
context.getDiagEngine().registerHandler([&](Diagnostic &diag) {
|
2019-07-31 01:21:25 +08:00
|
|
|
diagnostic.reset(new Diagnostic(std::move(diag)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs deserialization and returns the constructed spv.module op.
|
2021-05-07 05:16:55 +08:00
|
|
|
OwningOpRef<spirv::ModuleOp> deserialize() {
|
2019-07-31 01:21:25 +08:00
|
|
|
return spirv::deserialize(binary, &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks there is a diagnostic generated with the given `errorMessage`.
|
|
|
|
void expectDiagnostic(StringRef errorMessage) {
|
|
|
|
ASSERT_NE(nullptr, diagnostic.get());
|
|
|
|
|
2020-07-07 16:35:23 +08:00
|
|
|
// TODO: check error location too.
|
2020-01-29 03:23:46 +08:00
|
|
|
EXPECT_THAT(diagnostic->str(), StrEq(std::string(errorMessage)));
|
2019-07-31 01:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// SPIR-V builder methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Adds the SPIR-V module header to `binary`.
|
2020-03-12 04:04:25 +08:00
|
|
|
void addHeader() {
|
|
|
|
spirv::appendModuleHeader(binary, spirv::Version::V_1_0, /*idBound=*/0);
|
|
|
|
}
|
2019-07-31 01:21:25 +08:00
|
|
|
|
|
|
|
/// Adds the SPIR-V instruction into `binary`.
|
|
|
|
void addInstruction(spirv::Opcode op, ArrayRef<uint32_t> operands) {
|
|
|
|
uint32_t wordCount = 1 + operands.size();
|
2019-09-24 08:10:49 +08:00
|
|
|
binary.push_back(spirv::getPrefixedOpcode(wordCount, op));
|
2019-07-31 01:21:25 +08:00
|
|
|
binary.append(operands.begin(), operands.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t addVoidType() {
|
|
|
|
auto id = nextID++;
|
|
|
|
addInstruction(spirv::Opcode::OpTypeVoid, {id});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t addIntType(uint32_t bitwidth) {
|
|
|
|
auto id = nextID++;
|
|
|
|
addInstruction(spirv::Opcode::OpTypeInt, {id, bitwidth, /*signedness=*/1});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:49 +08:00
|
|
|
uint32_t addStructType(ArrayRef<uint32_t> memberTypes) {
|
|
|
|
auto id = nextID++;
|
|
|
|
SmallVector<uint32_t, 2> words;
|
|
|
|
words.push_back(id);
|
|
|
|
words.append(memberTypes.begin(), memberTypes.end());
|
|
|
|
addInstruction(spirv::Opcode::OpTypeStruct, words);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-07-31 01:21:25 +08:00
|
|
|
uint32_t addFunctionType(uint32_t retType, ArrayRef<uint32_t> paramTypes) {
|
|
|
|
auto id = nextID++;
|
|
|
|
SmallVector<uint32_t, 4> operands;
|
|
|
|
operands.push_back(id);
|
|
|
|
operands.push_back(retType);
|
|
|
|
operands.append(paramTypes.begin(), paramTypes.end());
|
|
|
|
addInstruction(spirv::Opcode::OpTypeFunction, operands);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t addFunction(uint32_t retType, uint32_t fnType) {
|
|
|
|
auto id = nextID++;
|
|
|
|
addInstruction(spirv::Opcode::OpFunction,
|
|
|
|
{retType, id,
|
|
|
|
static_cast<uint32_t>(spirv::FunctionControl::None),
|
|
|
|
fnType});
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:50:58 +08:00
|
|
|
void addFunctionEnd() { addInstruction(spirv::Opcode::OpFunctionEnd, {}); }
|
|
|
|
|
|
|
|
void addReturn() { addInstruction(spirv::Opcode::OpReturn, {}); }
|
2019-07-31 01:21:25 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SmallVector<uint32_t, 5> binary;
|
|
|
|
uint32_t nextID = 1;
|
|
|
|
MLIRContext context;
|
|
|
|
std::unique_ptr<Diagnostic> diagnostic;
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basics
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, EmptyModuleFailure) {
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("SPIR-V binary module must have a 5-word header");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, WrongMagicNumberFailure) {
|
|
|
|
addHeader();
|
|
|
|
binary.front() = 0xdeadbeef; // Change to a wrong magic number
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("incorrect magic number");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, OnlyHeaderSuccess) {
|
|
|
|
addHeader();
|
2020-07-07 20:28:25 +08:00
|
|
|
EXPECT_TRUE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, ZeroWordCountFailure) {
|
|
|
|
addHeader();
|
|
|
|
binary.push_back(0); // OpNop with zero word count
|
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("word count cannot be zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, InsufficientWordFailure) {
|
|
|
|
addHeader();
|
|
|
|
binary.push_back((2u << 16) |
|
|
|
|
static_cast<uint32_t>(spirv::Opcode::OpTypeVoid));
|
2020-07-12 06:15:22 +08:00
|
|
|
// Missing word for type <id>.
|
2019-07-31 01:21:25 +08:00
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("insufficient words for the last instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, IntTypeMissingSignednessFailure) {
|
|
|
|
addHeader();
|
|
|
|
addInstruction(spirv::Opcode::OpTypeInt, {nextID++, 32});
|
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("OpTypeInt must have bitwidth and signedness parameters");
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:10:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StructType
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, OpMemberNameSuccess) {
|
|
|
|
addHeader();
|
|
|
|
SmallVector<uint32_t, 5> typeDecl;
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
auto int32Type = addIntType(32);
|
|
|
|
auto structType = addStructType({int32Type, int32Type});
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
SmallVector<uint32_t, 5> operands1 = {structType, 0};
|
2021-02-05 06:57:50 +08:00
|
|
|
(void)spirv::encodeStringLiteralInto(operands1, "i1");
|
2019-09-24 08:10:49 +08:00
|
|
|
addInstruction(spirv::Opcode::OpMemberName, operands1);
|
|
|
|
|
|
|
|
SmallVector<uint32_t, 5> operands2 = {structType, 1};
|
2021-02-05 06:57:50 +08:00
|
|
|
(void)spirv::encodeStringLiteralInto(operands2, "i2");
|
2019-09-24 08:10:49 +08:00
|
|
|
addInstruction(spirv::Opcode::OpMemberName, operands2);
|
|
|
|
|
|
|
|
binary.append(typeDecl.begin(), typeDecl.end());
|
2020-07-07 20:28:25 +08:00
|
|
|
EXPECT_TRUE(deserialize());
|
2019-09-24 08:10:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, OpMemberNameMissingOperands) {
|
|
|
|
addHeader();
|
|
|
|
SmallVector<uint32_t, 5> typeDecl;
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
auto int32Type = addIntType(32);
|
|
|
|
auto int64Type = addIntType(64);
|
|
|
|
auto structType = addStructType({int32Type, int64Type});
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
SmallVector<uint32_t, 5> operands1 = {structType};
|
|
|
|
addInstruction(spirv::Opcode::OpMemberName, operands1);
|
|
|
|
|
|
|
|
binary.append(typeDecl.begin(), typeDecl.end());
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-09-24 08:10:49 +08:00
|
|
|
expectDiagnostic("OpMemberName must have at least 3 operands");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, OpMemberNameExcessOperands) {
|
|
|
|
addHeader();
|
|
|
|
SmallVector<uint32_t, 5> typeDecl;
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
auto int32Type = addIntType(32);
|
|
|
|
auto structType = addStructType({int32Type});
|
|
|
|
std::swap(typeDecl, binary);
|
|
|
|
|
|
|
|
SmallVector<uint32_t, 5> operands = {structType, 0};
|
2021-02-05 06:57:50 +08:00
|
|
|
(void)spirv::encodeStringLiteralInto(operands, "int32");
|
2019-09-24 08:10:49 +08:00
|
|
|
operands.push_back(42);
|
|
|
|
addInstruction(spirv::Opcode::OpMemberName, operands);
|
|
|
|
|
|
|
|
binary.append(typeDecl.begin(), typeDecl.end());
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-09-24 08:10:49 +08:00
|
|
|
expectDiagnostic("unexpected trailing words in OpMemberName instruction");
|
|
|
|
}
|
|
|
|
|
2019-07-31 01:21:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, FunctionMissingEndFailure) {
|
|
|
|
addHeader();
|
|
|
|
auto voidType = addVoidType();
|
|
|
|
auto fnType = addFunctionType(voidType, {});
|
|
|
|
addFunction(voidType, fnType);
|
2020-07-12 06:15:22 +08:00
|
|
|
// Missing OpFunctionEnd.
|
2019-07-31 01:21:25 +08:00
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("expected OpFunctionEnd instruction");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, FunctionMissingParameterFailure) {
|
|
|
|
addHeader();
|
|
|
|
auto voidType = addVoidType();
|
|
|
|
auto i32Type = addIntType(32);
|
|
|
|
auto fnType = addFunctionType(voidType, {i32Type});
|
|
|
|
addFunction(voidType, fnType);
|
2020-07-12 06:15:22 +08:00
|
|
|
// Missing OpFunctionParameter.
|
2019-07-31 01:21:25 +08:00
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-07-31 01:21:25 +08:00
|
|
|
expectDiagnostic("expected OpFunctionParameter instruction");
|
|
|
|
}
|
2019-08-28 01:50:58 +08:00
|
|
|
|
|
|
|
TEST_F(DeserializationTest, FunctionMissingLabelForFirstBlockFailure) {
|
|
|
|
addHeader();
|
|
|
|
auto voidType = addVoidType();
|
|
|
|
auto fnType = addFunctionType(voidType, {});
|
|
|
|
addFunction(voidType, fnType);
|
2020-07-12 06:15:22 +08:00
|
|
|
// Missing OpLabel.
|
2019-08-28 01:50:58 +08:00
|
|
|
addReturn();
|
|
|
|
addFunctionEnd();
|
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-08-28 01:50:58 +08:00
|
|
|
expectDiagnostic("a basic block must start with OpLabel");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(DeserializationTest, FunctionMalformedLabelFailure) {
|
|
|
|
addHeader();
|
|
|
|
auto voidType = addVoidType();
|
|
|
|
auto fnType = addFunctionType(voidType, {});
|
|
|
|
addFunction(voidType, fnType);
|
|
|
|
addInstruction(spirv::Opcode::OpLabel, {}); // Malformed OpLabel
|
|
|
|
addReturn();
|
|
|
|
addFunctionEnd();
|
|
|
|
|
2020-07-07 20:28:25 +08:00
|
|
|
ASSERT_FALSE(deserialize());
|
2019-08-28 01:50:58 +08:00
|
|
|
expectDiagnostic("OpLabel should only have result <id>");
|
|
|
|
}
|