1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
//===- DeserializationTest.cpp - SPIR-V Deserialization Tests -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// The purpose of this file is to provide negative deserialization tests.
// For positive deserialization tests, please use serialization and
// deserialization for roundtripping.
//
//===----------------------------------------------------------------------===//
#include "mlir/Target/SPIRV/Deserialization.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Target/SPIRV/SPIRVBinaryUtils.h"
#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:
DeserializationTest() {
context.getOrLoadDialect<mlir::spirv::SPIRVDialect>();
// Register a diagnostic handler to capture the diagnostic so that we can
// check it later.
context.getDiagEngine().registerHandler([&](Diagnostic &diag) {
diagnostic = std::make_unique<Diagnostic>(std::move(diag));
});
}
/// Performs deserialization and returns the constructed spirv.module op.
OwningOpRef<spirv::ModuleOp> deserialize() {
return spirv::deserialize(binary, &context);
}
/// Checks there is a diagnostic generated with the given `errorMessage`.
void expectDiagnostic(StringRef errorMessage) {
ASSERT_NE(nullptr, diagnostic.get());
// TODO: check error location too.
EXPECT_THAT(diagnostic->str(), StrEq(std::string(errorMessage)));
}
//===--------------------------------------------------------------------===//
// SPIR-V builder methods
//===--------------------------------------------------------------------===//
/// Adds the SPIR-V module header to `binary`.
void addHeader() {
spirv::appendModuleHeader(binary, spirv::Version::V_1_0, /*idBound=*/0);
}
/// Adds the SPIR-V instruction into `binary`.
void addInstruction(spirv::Opcode op, ArrayRef<uint32_t> operands) {
uint32_t wordCount = 1 + operands.size();
binary.push_back(spirv::getPrefixedOpcode(wordCount, op));
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;
}
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;
}
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;
}
void addFunctionEnd() { addInstruction(spirv::Opcode::OpFunctionEnd, {}); }
void addReturn() { addInstruction(spirv::Opcode::OpReturn, {}); }
protected:
SmallVector<uint32_t, 5> binary;
uint32_t nextID = 1;
MLIRContext context;
std::unique_ptr<Diagnostic> diagnostic;
};
//===----------------------------------------------------------------------===//
// Basics
//===----------------------------------------------------------------------===//
TEST_F(DeserializationTest, EmptyModuleFailure) {
ASSERT_FALSE(deserialize());
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
ASSERT_FALSE(deserialize());
expectDiagnostic("incorrect magic number");
}
TEST_F(DeserializationTest, OnlyHeaderSuccess) {
addHeader();
EXPECT_TRUE(deserialize());
}
TEST_F(DeserializationTest, ZeroWordCountFailure) {
addHeader();
binary.push_back(0); // OpNop with zero word count
ASSERT_FALSE(deserialize());
expectDiagnostic("word count cannot be zero");
}
TEST_F(DeserializationTest, InsufficientWordFailure) {
addHeader();
binary.push_back((2u << 16) |
static_cast<uint32_t>(spirv::Opcode::OpTypeVoid));
// Missing word for type <id>.
ASSERT_FALSE(deserialize());
expectDiagnostic("insufficient words for the last instruction");
}
//===----------------------------------------------------------------------===//
// Types
//===----------------------------------------------------------------------===//
TEST_F(DeserializationTest, IntTypeMissingSignednessFailure) {
addHeader();
addInstruction(spirv::Opcode::OpTypeInt, {nextID++, 32});
ASSERT_FALSE(deserialize());
expectDiagnostic("OpTypeInt must have bitwidth and signedness parameters");
}
//===----------------------------------------------------------------------===//
// 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};
(void)spirv::encodeStringLiteralInto(operands1, "i1");
addInstruction(spirv::Opcode::OpMemberName, operands1);
SmallVector<uint32_t, 5> operands2 = {structType, 1};
(void)spirv::encodeStringLiteralInto(operands2, "i2");
addInstruction(spirv::Opcode::OpMemberName, operands2);
binary.append(typeDecl.begin(), typeDecl.end());
EXPECT_TRUE(deserialize());
}
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());
ASSERT_FALSE(deserialize());
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};
(void)spirv::encodeStringLiteralInto(operands, "int32");
operands.push_back(42);
addInstruction(spirv::Opcode::OpMemberName, operands);
binary.append(typeDecl.begin(), typeDecl.end());
ASSERT_FALSE(deserialize());
expectDiagnostic("unexpected trailing words in OpMemberName instruction");
}
//===----------------------------------------------------------------------===//
// Functions
//===----------------------------------------------------------------------===//
TEST_F(DeserializationTest, FunctionMissingEndFailure) {
addHeader();
auto voidType = addVoidType();
auto fnType = addFunctionType(voidType, {});
addFunction(voidType, fnType);
// Missing OpFunctionEnd.
ASSERT_FALSE(deserialize());
expectDiagnostic("expected OpFunctionEnd instruction");
}
TEST_F(DeserializationTest, FunctionMissingParameterFailure) {
addHeader();
auto voidType = addVoidType();
auto i32Type = addIntType(32);
auto fnType = addFunctionType(voidType, {i32Type});
addFunction(voidType, fnType);
// Missing OpFunctionParameter.
ASSERT_FALSE(deserialize());
expectDiagnostic("expected OpFunctionParameter instruction");
}
TEST_F(DeserializationTest, FunctionMissingLabelForFirstBlockFailure) {
addHeader();
auto voidType = addVoidType();
auto fnType = addFunctionType(voidType, {});
addFunction(voidType, fnType);
// Missing OpLabel.
addReturn();
addFunctionEnd();
ASSERT_FALSE(deserialize());
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();
ASSERT_FALSE(deserialize());
expectDiagnostic("OpLabel should only have result <id>");
}
|