From a635150b9d2bddb22ba3a9548b96395d2239f8dc Mon Sep 17 00:00:00 2001 From: Mikhail Nosov Date: Wed, 30 Mar 2022 18:56:49 +0300 Subject: [PATCH] [IE Common] Enable explicit TBlob declaration in all compilers (#11183) * Enable explicit TBlob declaration in all compilers This fixes problems when linking gcc compiled IE with clang compiled applications. Previous to this change, only clang compilers would consider TBlob templated types as declared externally. When *declared* explictly (with the `extern template` syntax), the C++ spec says that any inline methods of the templated class (such as TBlob constructors) should be ignored in favor of the externally instantiated version of that templated type: "An explicit instantiation declaration (an extern template) skips implicit instantiation step: the code that would otherwise cause an implicit instantiation instead uses the explicit instantiation definition provided elsewhere (resulting in link errors if no such instantiation exists)." However, when IE is compiled with gcc, it does not see the explicit `extern template` declarations of TBlob (due to the `#ifdef __clang__` guards in `ie_blob.h`). As an end result, presumably due to link-time-optimizations during IE library compilation(?), none of the TBlob implementations are actually included in the IE dynamic libraries. * Fix warnings for windows * Fix typo --- .ci/azure/linux.yml | 11 ++++++++- src/inference/include/ie/ie_blob.h | 9 ++++--- src/inference/src/ie_blob_common.cpp | 35 +++++++++++++--------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/.ci/azure/linux.yml b/.ci/azure/linux.yml index 25880afc586..7cccd692c8f 100644 --- a/.ci/azure/linux.yml +++ b/.ci/azure/linux.yml @@ -109,7 +109,8 @@ jobs: set -e $(REPO_DIR)/install_build_dependencies.sh # Move jdk into contrib - sudo apt --assume-yes install openjdk-11-jdk + # 'clang' compiler is to check that samples can be built using it + sudo apt --assume-yes install openjdk-11-jdk clang # For opencv-python: python3-setuptools and pip upgrade python3 -m pip install --upgrade pip python3 -m pip install -r $(REPO_DIR)/src/bindings/python/src/compatibility/openvino/requirements.txt @@ -223,6 +224,14 @@ jobs: displayName: 'Build cpp samples' continueOnError: false + - script: | + export CC=clang + export CXX=clang++ + $(INSTALL_DIR)/samples/cpp/build_samples.sh -i $(INSTALL_DIR) + workingDirectory: $(BUILD_SAMPLES_DIR) + displayName: 'Build cpp samples - clang' + continueOnError: false + - script: $(INSTALL_DIR)/samples/c/build_samples.sh -i $(INSTALL_DIR) workingDirectory: $(BUILD_SAMPLES_DIR) displayName: 'Build c samples' diff --git a/src/inference/include/ie/ie_blob.h b/src/inference/include/ie/ie_blob.h index 6583c103903..b68a5ed082e 100644 --- a/src/inference/include/ie/ie_blob.h +++ b/src/inference/include/ie/ie_blob.h @@ -599,7 +599,9 @@ public: /** *@brief Virtual destructor. */ - virtual ~TBlob(); + virtual ~TBlob() { + deallocate(); + } /** * @brief Creates an new empty rvalue LockedMemory object. @@ -806,7 +808,8 @@ protected: } }; -#ifdef __clang__ +// These should not be exported for WIN32 to avoid usage of '_handle' and '_allocator' across CRT bounaries +#ifndef WIN32 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); @@ -821,7 +824,7 @@ extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob); -#endif // __clang__ +#endif /** * @brief Creates a blob with the given tensor descriptor. diff --git a/src/inference/src/ie_blob_common.cpp b/src/inference/src/ie_blob_common.cpp index e9be76b15b8..d168a581abe 100644 --- a/src/inference/src/ie_blob_common.cpp +++ b/src/inference/src/ie_blob_common.cpp @@ -85,24 +85,21 @@ Blob::Ptr make_shared_blob(const Blob::Ptr& inputBlob, Blob::~Blob() {} MemoryBlob::~MemoryBlob() {} -template -TBlob::~TBlob() { - free(); -} - -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); -template class INFERENCE_ENGINE_API_CLASS(TBlob); +#ifndef WIN32 +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +template class TBlob; +#endif } // namespace InferenceEngine