From d8d9ecbfd0f141e86f0bacba24931a8d3c5d2e02 Mon Sep 17 00:00:00 2001 From: Peter Bell Date: Thu, 11 Aug 2022 19:17:32 +0100 Subject: [PATCH] Fix building with Werror (#83275) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #82146 made `Block` non-copyable by adding a `unique_ptr` member, and used this hacky work-around to copy it anyway. However, it fails under -Werror with this message: ``` ../c10/cuda/CUDACachingAllocator.cpp:1411:51: error: ‘void* memcpy(void*, const void*, size_t)’ writing to an object of type ‘struct c10::cuda::CUDACachingAllocator::{anonymous}::Block’ with no trivial copy-assignment [-Werror=class-memaccess] 1411 | std::memcpy(&key, &p.search_key, sizeof(Block)); ``` Instead, this constructs a new `Block` with all the relevant properties copied. Pull Request resolved: https://github.com/pytorch/pytorch/pull/83275 Approved by: https://github.com/malfet --- c10/cuda/CUDACachingAllocator.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/c10/cuda/CUDACachingAllocator.cpp b/c10/cuda/CUDACachingAllocator.cpp index c82363f3291..c6f91f5a599 100644 --- a/c10/cuda/CUDACachingAllocator.cpp +++ b/c10/cuda/CUDACachingAllocator.cpp @@ -1407,9 +1407,12 @@ class DeviceCachingAllocator { BlockPool& pool = *p.pool; // because of std::unique_ptr, block cannot be trivially copied - Block key(0, 0, 0); - std::memcpy(&key, &p.search_key, sizeof(Block)); - key.history.release(); + Block key( + p.search_key.device, + p.search_key.stream, + p.search_key.size, + p.search_key.pool, + p.search_key.ptr); key.size = (key.size < CachingAllocatorConfig::max_split_size()) ? CachingAllocatorConfig::max_split_size() : key.size;