2018-12-12 20:39:27 +01:00
|
|
|
//===- AMDGPUMetadataVerifier.cpp - MsgPack Types ---------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2018-12-12 20:39:27 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// Implements a verifier for AMDGPU HSA metadata.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
|
|
|
|
#include "llvm/Support/AMDGPUMetadata.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace AMDGPU {
|
|
|
|
namespace HSAMD {
|
|
|
|
namespace V3 {
|
|
|
|
|
|
|
|
bool MetadataVerifier::verifyScalar(
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::DocNode &Node, msgpack::Type SKind,
|
|
|
|
function_ref<bool(msgpack::DocNode &)> verifyValue) {
|
|
|
|
if (!Node.isScalar())
|
|
|
|
return false;
|
|
|
|
if (Node.getKind() != SKind) {
|
2018-12-12 20:39:27 +01:00
|
|
|
if (Strict)
|
|
|
|
return false;
|
|
|
|
// If we are not strict, we interpret string values as "implicitly typed"
|
|
|
|
// and attempt to coerce them to the expected type here.
|
2019-03-13 19:55:50 +01:00
|
|
|
if (Node.getKind() != msgpack::Type::String)
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
StringRef StringValue = Node.getString();
|
|
|
|
Node.fromString(StringValue);
|
|
|
|
if (Node.getKind() != SKind)
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (verifyValue)
|
2019-03-13 19:55:50 +01:00
|
|
|
return verifyValue(Node);
|
2018-12-12 20:39:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:55:50 +01:00
|
|
|
bool MetadataVerifier::verifyInteger(msgpack::DocNode &Node) {
|
|
|
|
if (!verifyScalar(Node, msgpack::Type::UInt))
|
|
|
|
if (!verifyScalar(Node, msgpack::Type::Int))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetadataVerifier::verifyArray(
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::DocNode &Node, function_ref<bool(msgpack::DocNode &)> verifyNode,
|
2018-12-12 20:39:27 +01:00
|
|
|
Optional<size_t> Size) {
|
2019-03-13 19:55:50 +01:00
|
|
|
if (!Node.isArray())
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
auto &Array = Node.getArray();
|
2018-12-12 20:39:27 +01:00
|
|
|
if (Size && Array.size() != *Size)
|
|
|
|
return false;
|
|
|
|
for (auto &Item : Array)
|
2019-03-13 19:55:50 +01:00
|
|
|
if (!verifyNode(Item))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetadataVerifier::verifyEntry(
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
|
|
|
|
function_ref<bool(msgpack::DocNode &)> verifyNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
auto Entry = MapNode.find(Key);
|
|
|
|
if (Entry == MapNode.end())
|
|
|
|
return !Required;
|
2019-03-13 19:55:50 +01:00
|
|
|
return verifyNode(Entry->second);
|
2018-12-12 20:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MetadataVerifier::verifyScalarEntry(
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::MapDocNode &MapNode, StringRef Key, bool Required,
|
|
|
|
msgpack::Type SKind,
|
|
|
|
function_ref<bool(msgpack::DocNode &)> verifyValue) {
|
|
|
|
return verifyEntry(MapNode, Key, Required, [=](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyScalar(Node, SKind, verifyValue);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:55:50 +01:00
|
|
|
bool MetadataVerifier::verifyIntegerEntry(msgpack::MapDocNode &MapNode,
|
2018-12-12 20:39:27 +01:00
|
|
|
StringRef Key, bool Required) {
|
2019-03-13 19:55:50 +01:00
|
|
|
return verifyEntry(MapNode, Key, Required, [this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyInteger(Node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:55:50 +01:00
|
|
|
bool MetadataVerifier::verifyKernelArgs(msgpack::DocNode &Node) {
|
|
|
|
if (!Node.isMap())
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
auto &ArgsMap = Node.getMap();
|
2018-12-12 20:39:27 +01:00
|
|
|
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".name", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".type_name", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(ArgsMap, ".size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(ArgsMap, ".offset", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".value_kind", true,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("by_value", true)
|
|
|
|
.Case("global_buffer", true)
|
|
|
|
.Case("dynamic_shared_pointer", true)
|
|
|
|
.Case("sampler", true)
|
|
|
|
.Case("image", true)
|
|
|
|
.Case("pipe", true)
|
|
|
|
.Case("queue", true)
|
|
|
|
.Case("hidden_global_offset_x", true)
|
|
|
|
.Case("hidden_global_offset_y", true)
|
|
|
|
.Case("hidden_global_offset_z", true)
|
|
|
|
.Case("hidden_none", true)
|
|
|
|
.Case("hidden_printf_buffer", true)
|
|
|
|
.Case("hidden_default_queue", true)
|
|
|
|
.Case("hidden_completion_action", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".value_type", true,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("struct", true)
|
|
|
|
.Case("i8", true)
|
|
|
|
.Case("u8", true)
|
|
|
|
.Case("i16", true)
|
|
|
|
.Case("u16", true)
|
|
|
|
.Case("f16", true)
|
|
|
|
.Case("i32", true)
|
|
|
|
.Case("u32", true)
|
|
|
|
.Case("f32", true)
|
|
|
|
.Case("i64", true)
|
|
|
|
.Case("u64", true)
|
|
|
|
.Case("f64", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(ArgsMap, ".pointee_align", false))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".address_space", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("private", true)
|
|
|
|
.Case("global", true)
|
|
|
|
.Case("constant", true)
|
|
|
|
.Case("local", true)
|
|
|
|
.Case("generic", true)
|
|
|
|
.Case("region", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".access", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("read_only", true)
|
|
|
|
.Case("write_only", true)
|
|
|
|
.Case("read_write", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".actual_access", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("read_only", true)
|
|
|
|
.Case("write_only", true)
|
|
|
|
.Case("read_write", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".is_const", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::Boolean))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".is_restrict", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::Boolean))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".is_volatile", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::Boolean))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(ArgsMap, ".is_pipe", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::Boolean))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:55:50 +01:00
|
|
|
bool MetadataVerifier::verifyKernel(msgpack::DocNode &Node) {
|
|
|
|
if (!Node.isMap())
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
auto &KernelMap = Node.getMap();
|
2018-12-12 20:39:27 +01:00
|
|
|
|
|
|
|
if (!verifyScalarEntry(KernelMap, ".name", true,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(KernelMap, ".symbol", true,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(KernelMap, ".language", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String,
|
|
|
|
[](msgpack::DocNode &SNode) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return StringSwitch<bool>(SNode.getString())
|
|
|
|
.Case("OpenCL C", true)
|
|
|
|
.Case("OpenCL C++", true)
|
|
|
|
.Case("HCC", true)
|
|
|
|
.Case("HIP", true)
|
|
|
|
.Case("OpenMP", true)
|
|
|
|
.Case("Assembler", true)
|
|
|
|
.Default(false);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyEntry(
|
2019-03-13 19:55:50 +01:00
|
|
|
KernelMap, ".language_version", false, [this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyArray(
|
|
|
|
Node,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) { return verifyInteger(Node); }, 2);
|
2018-12-12 20:39:27 +01:00
|
|
|
}))
|
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
if (!verifyEntry(KernelMap, ".args", false, [this](msgpack::DocNode &Node) {
|
|
|
|
return verifyArray(Node, [this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyKernelArgs(Node);
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyEntry(KernelMap, ".reqd_workgroup_size", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyArray(Node,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyInteger(Node);
|
|
|
|
},
|
|
|
|
3);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyEntry(KernelMap, ".workgroup_size_hint", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyArray(Node,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyInteger(Node);
|
|
|
|
},
|
|
|
|
3);
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(KernelMap, ".vec_type_hint", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyScalarEntry(KernelMap, ".device_enqueue_symbol", false,
|
2019-03-13 19:55:50 +01:00
|
|
|
msgpack::Type::String))
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".kernarg_segment_size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".group_segment_fixed_size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".private_segment_fixed_size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".kernarg_segment_align", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".wavefront_size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".sgpr_count", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".vgpr_count", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".max_flat_workgroup_size", true))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".sgpr_spill_count", false))
|
|
|
|
return false;
|
|
|
|
if (!verifyIntegerEntry(KernelMap, ".vgpr_spill_count", false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:55:50 +01:00
|
|
|
bool MetadataVerifier::verify(msgpack::DocNode &HSAMetadataRoot) {
|
|
|
|
if (!HSAMetadataRoot.isMap())
|
2018-12-12 20:39:27 +01:00
|
|
|
return false;
|
2019-03-13 19:55:50 +01:00
|
|
|
auto &RootMap = HSAMetadataRoot.getMap();
|
2018-12-12 20:39:27 +01:00
|
|
|
|
|
|
|
if (!verifyEntry(
|
2019-03-13 19:55:50 +01:00
|
|
|
RootMap, "amdhsa.version", true, [this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyArray(
|
|
|
|
Node,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) { return verifyInteger(Node); }, 2);
|
2018-12-12 20:39:27 +01:00
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyEntry(
|
2019-03-13 19:55:50 +01:00
|
|
|
RootMap, "amdhsa.printf", false, [this](msgpack::DocNode &Node) {
|
|
|
|
return verifyArray(Node, [this](msgpack::DocNode &Node) {
|
|
|
|
return verifyScalar(Node, msgpack::Type::String);
|
2018-12-12 20:39:27 +01:00
|
|
|
});
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
if (!verifyEntry(RootMap, "amdhsa.kernels", true,
|
2019-03-13 19:55:50 +01:00
|
|
|
[this](msgpack::DocNode &Node) {
|
|
|
|
return verifyArray(Node, [this](msgpack::DocNode &Node) {
|
2018-12-12 20:39:27 +01:00
|
|
|
return verifyKernel(Node);
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace V3
|
|
|
|
} // end namespace HSAMD
|
|
|
|
} // end namespace AMDGPU
|
|
|
|
} // end namespace llvm
|