From 88c0ffb11e285c79616952255d98f3b53c142718 Mon Sep 17 00:00:00 2001 From: "Ramesh Errabolu (xN/A) TX" Date: Tue, 3 Mar 2015 19:41:56 -0500 Subject: [PATCH] ECR #333755 - Add helper files to load Hsail Text files and assembled them into Brig files. [git-p4: depot-paths = "//depot/stg/hsa/drivers/hsa/runtime/": change = 1127109] [ROCm/ROCR-Runtime commit: 21449d717f30e59515ed3a43876e300977e435ac] --- .../rocr-runtime/samples/hsailUtil/Makefile | 7 + .../samples/hsailUtil/assemble.cpp | 147 ++++++++++++++++++ .../samples/hsailUtil/assemble.hpp | 32 ++++ .../samples/hsailUtil/build/Makefile | 8 + .../samples/hsailUtil/build/Makefile.common | 51 ++++++ .../rocr-runtime/samples/hsailUtil/common.cpp | 48 ++++++ .../rocr-runtime/samples/hsailUtil/common.hpp | 27 ++++ 7 files changed, 320 insertions(+) create mode 100644 projects/rocr-runtime/samples/hsailUtil/Makefile create mode 100644 projects/rocr-runtime/samples/hsailUtil/assemble.cpp create mode 100644 projects/rocr-runtime/samples/hsailUtil/assemble.hpp create mode 100644 projects/rocr-runtime/samples/hsailUtil/build/Makefile create mode 100644 projects/rocr-runtime/samples/hsailUtil/build/Makefile.common create mode 100644 projects/rocr-runtime/samples/hsailUtil/common.cpp create mode 100644 projects/rocr-runtime/samples/hsailUtil/common.hpp diff --git a/projects/rocr-runtime/samples/hsailUtil/Makefile b/projects/rocr-runtime/samples/hsailUtil/Makefile new file mode 100644 index 0000000000..ae780dfdcf --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/Makefile @@ -0,0 +1,7 @@ +OPENCL_DEPTH = ../../.. + +include $(OPENCL_DEPTH)/runtimenew/runtimedefs + +SUBDIRS = build + +include $(OPENCL_DEPTH)/runtimenew/runtimerules diff --git a/projects/rocr-runtime/samples/hsailUtil/assemble.cpp b/projects/rocr-runtime/samples/hsailUtil/assemble.cpp new file mode 100644 index 0000000000..a79fb9e49d --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/assemble.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "assemble.hpp" +#include "hsa.h" +#include "hsa_ext_alt_finalize.h" +#include "HSAILDisassembler.h" +#include "HSAILParser.h" +#include "HSAILScanner.h" +#include "HSAILValidator.h" + +namespace { + std::unordered_map mod2con; +} // namespace anonymous + +hsa_status_t ModuleCreateFromHsailTextFile( + const char *hsail_text_filename, + hsa_ext_alt_module_t *module +) { + if (!hsail_text_filename) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + std::ifstream hsail_text_file(hsail_text_filename); + if (!hsail_text_file.is_open()) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + std::string hsail_string; + hsail_text_file.seekg(0, std::ios::end); + hsail_string.resize(hsail_text_file.tellg()); + hsail_text_file.seekg(0, std::ios::beg); + hsail_text_file.read(&hsail_string[0], hsail_string.size()); + hsail_text_file.close(); + + return ModuleCreateFromHsailString(hsail_string.c_str(), module); +} + +hsa_status_t ModuleCreateFromHsailString( + const char *hsail_string, + hsa_ext_alt_module_t *module +) { + if (!hsail_string || !module) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + HSAIL_ASM::BrigContainer *brig_container = NULL; + try { + brig_container = new HSAIL_ASM::BrigContainer; + } catch (const std::bad_alloc) { + return HSA_STATUS_ERROR_OUT_OF_RESOURCES; + } + + std::istringstream hsail_stream(hsail_string); + try { + HSAIL_ASM::Scanner hsail_scanner(hsail_stream); + HSAIL_ASM::Parser hsail_parser(hsail_scanner, *brig_container); + hsail_parser.parseSource(); + } catch (const SyntaxError) { + delete brig_container; + return static_cast(HSA_EXT_ALT_STATUS_ERROR_INVALID_MODULE); + } + + try { + auto insert_status = mod2con.insert( + std::make_pair( + reinterpret_cast(brig_container->getBrigModule()), + reinterpret_cast(brig_container) + ) + ); + assert(insert_status.second); + } catch (const std::bad_alloc) { + delete brig_container; + return HSA_STATUS_ERROR_OUT_OF_RESOURCES; + } + + module->handle = reinterpret_cast(brig_container->getBrigModule()); + return HSA_STATUS_SUCCESS; +} + +hsa_status_t ModuleDestroy( + hsa_ext_alt_module_t module +) { + auto find_status = mod2con.find(module.handle); + if (find_status == mod2con.end()) { + return static_cast(HSA_EXT_ALT_STATUS_ERROR_INVALID_MODULE); + } + + HSAIL_ASM::BrigContainer *brig_container = + reinterpret_cast(find_status->second); + assert(brig_container); + delete brig_container; + mod2con.erase(find_status); + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t ModuleValidate( + hsa_ext_alt_module_t module, + uint32_t *result +) { + if (!result) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + auto find_status = mod2con.find(module.handle); + if (find_status == mod2con.end()) { + return static_cast(HSA_EXT_ALT_STATUS_ERROR_INVALID_MODULE); + } + + HSAIL_ASM::BrigContainer *brig_container = + reinterpret_cast(find_status->second); + assert(brig_container); + HSAIL_ASM::Validator brig_validator(*brig_container); + *result = brig_validator.validate() ? 0 : 1; + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t ModuleDisassemble( + hsa_ext_alt_module_t module, + const char *hsail_text_filename +) { + if (!hsail_text_filename) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + auto find_status = mod2con.find(module.handle); + if (find_status == mod2con.end()) { + return static_cast(HSA_EXT_ALT_STATUS_ERROR_INVALID_MODULE); + } + + HSAIL_ASM::BrigContainer *brig_container = + reinterpret_cast(find_status->second); + assert(brig_container); + HSAIL_ASM::Disassembler brig_disassembler(*brig_container); + if (brig_disassembler.run(hsail_text_filename)) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + return HSA_STATUS_SUCCESS; +} diff --git a/projects/rocr-runtime/samples/hsailUtil/assemble.hpp b/projects/rocr-runtime/samples/hsailUtil/assemble.hpp new file mode 100644 index 0000000000..c43f8aaac7 --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/assemble.hpp @@ -0,0 +1,32 @@ +#ifndef ASSEMBLE_HPP_ +#define ASSEMBLE_HPP_ + +#include +#include "hsa.h" +#include "hsa_ext_alt_finalize.h" + +hsa_status_t ModuleCreateFromHsailTextFile( + const char *hsail_text_filename, + hsa_ext_alt_module_t *module +); + +hsa_status_t ModuleCreateFromHsailString( + const char *hsail_string, + hsa_ext_alt_module_t *module +); + +hsa_status_t ModuleDestroy( + hsa_ext_alt_module_t module +); + +hsa_status_t ModuleValidate( + hsa_ext_alt_module_t module, + uint32_t *result +); + +hsa_status_t ModuleDisassemble( + hsa_ext_alt_module_t module, + const char *hsail_text_filename +); + +#endif // ASSEMBLE_HPP_ diff --git a/projects/rocr-runtime/samples/hsailUtil/build/Makefile b/projects/rocr-runtime/samples/hsailUtil/build/Makefile new file mode 100644 index 0000000000..7e15928ce4 --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/build/Makefile @@ -0,0 +1,8 @@ +OPENCL_DEPTH = ../../../.. + +include $(OPENCL_DEPTH)/runtimenew/runtimedefs + +BUILD_SUBDIRS = $(DEFAULT_TARGETS) +BUILD_MAKEFILE = Makefile.common + +include $(OPENCL_DEPTH)/runtimenew/runtimerules diff --git a/projects/rocr-runtime/samples/hsailUtil/build/Makefile.common b/projects/rocr-runtime/samples/hsailUtil/build/Makefile.common new file mode 100644 index 0000000000..49695b9b8e --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/build/Makefile.common @@ -0,0 +1,51 @@ +include $(OPENCL_DEPTH)/hsadefs + +LIB_TARGET = testcommon +vpath %.cpp $(COMPONENT_DEPTH) +CPPFILES := $(notdir $(wildcard $(COMPONENT_DEPTH)/*.cpp)) + +ifdef ATI_BITS_64 + LIB_SUFFIX = 64 + NBITS = 64 +else + LIB_SUFFIX = + ifndef ATI_OS_WINDOWS + NBITS := 32 + endif +endif + +ifdef ATI_OS_WINDOWS + CORE_LIB = dll + LFLAGS += /subsystem:console + LIB_PREFIX = +else + CORE_LIB = so + LIB_PREFIX = lib +endif + +ifdef ATI_OS_LINUX + GCXXOPTS := $(filter-out -fno-rtti,$(GCXXOPTS)) + GCXXOPTS := $(filter-out -fno-exceptions,$(GCXXOPTS)) + LFLAGS += -L$(DIST_LIB_DEST) -lpthread $(LIBSTDCXX) -lm -ldl -lrt +endif + +export BUILD_HSA_TARGET=yes + +LCINCS := $(INCSWITCH) "$(OPENCL_DEPTH)/compiler/finalizer/HSAIL/hsail-tools/libHSAIL" +LCINCS += $(INCSWITCH) "$(OPENCL_DEPTH)/compiler/finalizer/HSAIL/hsail-tools/libHSAIL/$(FULL_BUILD_DIR)" +LCINCS += $(INCSWITCH) "$(OPENCL_DEPTH)/compiler/finalizer/Interface" +LCINCS += $(INCSWITCH) "$(OPENCL_DEPTH)/runtime/inc" + +LLLIBS := $(OPENCL_DEPTH)/compiler/finalizer/HSAIL/hsail-tools/libHSAIL/$(FULL_BUILD_DIR)/libhsail$(LIB_EXT) +LLLIBS += $(OPENCL_DEPTH)/contrib/gtest-1.6.0/$(FULL_BUILD_DIR)/libgtest$(LIB_EXT) +LLLIBS += $(OPENCL_DEPTH)/runtime/test/gcommon/$(FULL_BUILD_DIR)/gtestcommon$(LIB_EXT) + +RUNTIME_BUILD = build/$(OS_TYPE)/$(CORE_LIB)/$(BUILD_DIR) + +ifdef ATI_OS_LINUX + LFLAGS += -L$(OPENCL_DEPTH)/runtime/core/$(RUNTIME_BUILD) -lhsa-runtime$(LIB_SUFFIX) +else + LLLIBS += $(OPENCL_DEPTH)/runtime/core/$(RUNTIME_BUILD)/hsa-runtime$(LIB_SUFFIX)$(LIB_EXT) +endif + +include $(OPENCL_DEPTH)/hsarules diff --git a/projects/rocr-runtime/samples/hsailUtil/common.cpp b/projects/rocr-runtime/samples/hsailUtil/common.cpp new file mode 100644 index 0000000000..84f2ee714c --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/common.cpp @@ -0,0 +1,48 @@ +#include "common.hpp" + +void ErrorCheck(hsa_status_t hsa_error_code) { + if (hsa_error_code != HSA_STATUS_SUCCESS) { + std::cerr << "HSA reported error!" << std::endl; + exit(EXIT_FAILURE); + } +} + +hsa_status_t FindGpuDevice(hsa_agent_t agent, void *data) { + if (data == NULL) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + hsa_device_type_t hsa_device_type; + hsa_status_t hsa_error_code = hsa_agent_get_info( + agent, HSA_AGENT_INFO_DEVICE, &hsa_device_type + ); + if (hsa_error_code != HSA_STATUS_SUCCESS) { + return hsa_error_code; + } + + if (hsa_device_type == HSA_DEVICE_TYPE_GPU) { + *((hsa_agent_t*)data) = agent; + } + + return HSA_STATUS_SUCCESS; +} + +hsa_status_t FindHostRegion(hsa_region_t region, void *data) { + if (data == NULL) { + return HSA_STATUS_ERROR_INVALID_ARGUMENT; + } + + bool is_host_region = false; + hsa_status_t hsa_error_code = hsa_region_get_info( + region, (hsa_region_info_t)HSA_EXT_AMD_REGION_INFO_HOST_ACCESS, + &is_host_region); + if (hsa_error_code != HSA_STATUS_SUCCESS) { + return hsa_error_code; + } + + if (is_host_region) { + *((hsa_region_t*)data) = region; + } + + return HSA_STATUS_SUCCESS; +} diff --git a/projects/rocr-runtime/samples/hsailUtil/common.hpp b/projects/rocr-runtime/samples/hsailUtil/common.hpp new file mode 100644 index 0000000000..4596877823 --- /dev/null +++ b/projects/rocr-runtime/samples/hsailUtil/common.hpp @@ -0,0 +1,27 @@ +#ifndef COMMON_COMMON_HPP +#define COMMON_COMMON_HPP + +#include +#include + +#include "hsa.h" +#include "hsa_ext_finalize.h" +#include "hsa_ext_amd.h" + +#if defined(_MSC_VER) + #define ALIGNED_(x) __declspec(align(x)) +#else + #if defined(__GNUC__) + #define ALIGNED_(x) __attribute__ ((aligned(x))) + #endif // __GNUC__ +#endif // _MSC_VER + +#define MULTILINE(...) # __VA_ARGS__ + +void ErrorCheck(hsa_status_t hsa_error_code); + +hsa_status_t FindGpuDevice(hsa_agent_t agent, void *data); + +hsa_status_t FindHostRegion(hsa_region_t region, void *data); + +#endif // COMMON_COMMON_HPP