From de0fa510228085cd1eae4df7eec011e61668b567 Mon Sep 17 00:00:00 2001 From: Sean Keely Date: Sat, 4 Feb 2017 15:08:07 -0600 Subject: [PATCH] Fix Api table copy operation and tools version checking. Change-Id: Ia76d16f3ea6d0abb931813f90bc3bc2119da5999 [ROCm/ROCR-Runtime commit: 505d722b7d36bae364526803669950c3c425b5b6] --- .../hsa-runtime/core/runtime/runtime.cpp | 10 +-- .../runtime/hsa-runtime/core/util/utils.h | 11 +++ .../runtime/hsa-runtime/inc/hsa_api_trace.h | 85 +++++++++---------- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index 06462f2c76..d205400752 100755 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -1011,9 +1011,13 @@ void Runtime::LoadExtensions() { // Update Hsa Api Table with handle of Image extension Apis extensions_.LoadFinalizer(kFinalizerLib[os_index(os::current_os)]); + hsa_api_table_.LinkExts(&extensions_.finalizer_api, + core::HsaApiTable::HSA_EXT_FINALIZER_API_TABLE_ID); // Update Hsa Api Table with handle of Finalizer extension Apis extensions_.LoadImage(kImageLib[os_index(os::current_os)]); + hsa_api_table_.LinkExts(&extensions_.image_api, + core::HsaApiTable::HSA_EXT_IMAGE_API_TABLE_ID); } void Runtime::UnloadExtensions() { extensions_.Unload(); } @@ -1074,12 +1078,6 @@ void Runtime::LoadTools() { typedef Agent* (*tool_wrap_t)(Agent*); typedef void (*tool_add_t)(Runtime*); - // Link HSA Extensions for Finalizer and Images for Api interception - hsa_api_table_.LinkExts(&extensions_.finalizer_api, - core::HsaApiTable::HSA_EXT_FINALIZER_API_TABLE_ID); - hsa_api_table_.LinkExts(&extensions_.image_api, - core::HsaApiTable::HSA_EXT_IMAGE_API_TABLE_ID); - // Load tool libs std::string tool_names = flag_.tools_lib_names(); if (tool_names != "") { diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h b/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h index 880472af38..b3e60a13e9 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/utils.h @@ -92,6 +92,17 @@ static __forceinline unsigned long long int strtoull(const char* str, #define PASTE2(x, y) x##y #define PASTE(x, y) PASTE2(x, y) +#ifdef NDEBUG +#define debug_warning(exp) +#else +#define debug_warning(exp) \ + do { \ + if (!(exp)) \ + fprintf(stderr, "Warning: " STRING(exp) " in %s, " __FILE__ ":" STRING(__LINE__) "\n", \ + __PRETTY_FUNCTION__); \ + } while (false); +#endif + // A macro to disallow the copy and move constructor and operator= functions // This should be used in the private: declarations for a class #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ diff --git a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h index 4ccc8c24df..5ecdf7bb09 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h +++ b/projects/rocr-runtime/runtime/hsa-runtime/inc/hsa_api_trace.h @@ -78,6 +78,8 @@ static inline uint32_t Min(const uint32_t a, const uint32_t b) { } // Structure of Version used to identify an instance of Api table +// Must be the first member (offsetof == 0) of all API tables. +// This is the root of the table passing ABI. struct ApiTableVersion { uint32_t major_id; uint32_t minor_id; @@ -109,9 +111,9 @@ struct ImageExtTable { decltype(hsa_ext_image_destroy)* hsa_ext_image_destroy_fn; decltype(hsa_ext_sampler_create)* hsa_ext_sampler_create_fn; decltype(hsa_ext_sampler_destroy)* hsa_ext_sampler_destroy_fn; - decltype(hsa_ext_image_get_capability_with_layout)* hsa_ext_image_get_capability_with_layout_fn; - decltype(hsa_ext_image_data_get_info_with_layout)* hsa_ext_image_data_get_info_with_layout_fn; - decltype(hsa_ext_image_create_with_layout)* hsa_ext_image_create_with_layout_fn; + decltype(hsa_ext_image_get_capability_with_layout)* hsa_ext_image_get_capability_with_layout_fn; + decltype(hsa_ext_image_data_get_info_with_layout)* hsa_ext_image_data_get_info_with_layout_fn; + decltype(hsa_ext_image_create_with_layout)* hsa_ext_image_create_with_layout_fn; }; // Table to export AMD Extension Apis @@ -393,59 +395,48 @@ void inline copyApi(void* src, void* dest, size_t size) { (size - sizeof(ApiTableVersion))); } +// Copy Api child tables if valid. +static void inline copyElement(ApiTableVersion* dest, ApiTableVersion* src) { + if (dest->major_id == src->major_id) { + dest->step_id = src->step_id; + dest->minor_id = Min(dest->minor_id, src->minor_id); + copyApi(dest, src, dest->minor_id); + } else { + dest->major_id = 0; + dest->minor_id = 0; + dest->step_id = 0; + } +} + // Copy constructor for all Api tables. The function assumes the // user has initialized an instance of tables container correctly // for the Major, Minor and Stepping Ids of Root and Child Api tables. // The function will overwrite the value of Minor Id by taking the // minimum of source and destination parameters. It will also overwrite // the stepping Id with value from source parameter. -static const -void inline copyTables(const HsaApiTable* src, HsaApiTableContainer* dest) { - - // Verify Major Id of source and destination tables are valid - assert(dest->root.version.major_id == src->version.major_id); - assert(dest->core.version.major_id == src->core_->version.major_id); - assert(dest->amd_ext.version.major_id == src->amd_ext_->version.major_id); - assert(dest->finalizer_ext.version.major_id == src->finalizer_ext_->version.major_id); - assert(dest->image_ext.version.major_id == src->image_ext_->version.major_id); +static void inline copyTables(const HsaApiTable* src, HsaApiTable* dest) { + // Verify Major Id of source and destination tables match + if (dest->version.major_id != src->version.major_id) { + dest->version.major_id = 0; + dest->version.minor_id = 0; + dest->version.step_id = 0; + return; + } // Initialize the stepping id and minor id of root table. For the // minor id which encodes struct size, take the minimum of source // and destination parameters - dest->root.version.step_id = src->version.step_id; - dest->root.version.minor_id = Min(dest->root.version.minor_id, src->version.minor_id); - - // Copy the Core Api table - size_t size = dest->root.version.minor_id; - if (size > offsetof(HsaApiTable, core_)) { - dest->core.version.step_id = src->core_->version.step_id; - dest->core.version.minor_id = Min(dest->core.version.minor_id, - src->core_->version.minor_id); - copyApi(&dest->core, src->core_, dest->core.version.minor_id); - } - - // Copy the Amd Ext Api table - if (size > offsetof(HsaApiTable, amd_ext_)) { - dest->amd_ext.version.step_id = src->amd_ext_->version.step_id; - dest->amd_ext.version.minor_id = Min(dest->core.version.minor_id, - src->amd_ext_->version.minor_id); - copyApi(&dest->amd_ext, src->amd_ext_, dest->amd_ext.version.minor_id); - } - - // Copy the Finalizer Ext Api table - if (size > offsetof(HsaApiTable, finalizer_ext_)) { - dest->finalizer_ext.version.step_id = src->finalizer_ext_->version.step_id; - dest->finalizer_ext.version.minor_id = Min(dest->core.version.minor_id, - src->finalizer_ext_->version.minor_id); - copyApi(&dest->finalizer_ext, src->finalizer_ext_, dest->finalizer_ext.version.minor_id); - } - - // Copy the Image Ext Api table - if (size > offsetof(HsaApiTable, image_ext_)) { - dest->image_ext.version.step_id = src->image_ext_->version.step_id; - dest->image_ext.version.minor_id = Min(dest->core.version.minor_id, - src->image_ext_->version.minor_id); - copyApi(&dest->image_ext, src->image_ext_, dest->image_ext.version.minor_id); - } + dest->version.step_id = src->version.step_id; + dest->version.minor_id = Min(dest->version.minor_id, src->version.minor_id); + + // Copy child tables if present + if ((offsetof(HsaApiTable, core_) < dest->version.minor_id)) + copyElement(&dest->core_->version, &src->core_->version); + if ((offsetof(HsaApiTable, amd_ext_) < dest->version.minor_id)) + copyElement(&dest->amd_ext_->version, &src->amd_ext_->version); + if ((offsetof(HsaApiTable, finalizer_ext_) < dest->version.minor_id)) + copyElement(&dest->finalizer_ext_->version, &src->finalizer_ext_->version); + if ((offsetof(HsaApiTable, image_ext_) < dest->version.minor_id)) + copyElement(&dest->image_ext_->version, &src->image_ext_->version); } #endif