From b0b53ba4038d2b20e05011d350f1f3fea2be358d Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Thu, 28 Apr 2022 17:58:33 +0000 Subject: [PATCH] Fix 64 to 32 bit narrowing in dawn/common Bug: dawn:1377 Change-Id: I25981cf18dc768cc0b6d4f6a6463b4dc169ca6c1 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/87672 Kokoro: Kokoro Reviewed-by: Corentin Wallez Commit-Queue: Austin Eng Reviewed-by: Loko Kung --- src/dawn/common/BUILD.gn | 1 + src/dawn/common/CMakeLists.txt | 1 + src/dawn/common/Numeric.h | 42 +++++++++++++++++++ src/dawn/common/SlabAllocator.cpp | 6 +-- src/dawn/common/SlabAllocator.h | 5 ++- .../native/d3d12/BindGroupLayoutD3D12.cpp | 1 - src/dawn/native/d3d12/BindGroupLayoutD3D12.h | 2 +- src/dawn/native/opengl/BindGroupLayoutGL.cpp | 2 - src/dawn/native/opengl/BindGroupLayoutGL.h | 2 +- src/dawn/native/vulkan/BindGroupLayoutVk.cpp | 1 - src/dawn/native/vulkan/BindGroupLayoutVk.h | 2 +- src/dawn/native/vulkan/BindGroupVk.h | 1 - 12 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 src/dawn/common/Numeric.h diff --git a/src/dawn/common/BUILD.gn b/src/dawn/common/BUILD.gn index d0b008672a..8a9c26db1f 100644 --- a/src/dawn/common/BUILD.gn +++ b/src/dawn/common/BUILD.gn @@ -204,6 +204,7 @@ if (is_win || is_linux || is_chromeos || is_mac || is_fuchsia || is_android) { "Math.h", "NSRef.h", "NonCopyable.h", + "Numeric.h", "PlacementAllocated.h", "Platform.h", "Preprocessor.h", diff --git a/src/dawn/common/CMakeLists.txt b/src/dawn/common/CMakeLists.txt index f02acde21d..e455d49203 100644 --- a/src/dawn/common/CMakeLists.txt +++ b/src/dawn/common/CMakeLists.txt @@ -45,6 +45,7 @@ target_sources(dawn_common PRIVATE "Math.h" "NSRef.h" "NonCopyable.h" + "Numeric.h" "PlacementAllocated.h" "Platform.h" "Preprocessor.h" diff --git a/src/dawn/common/Numeric.h b/src/dawn/common/Numeric.h new file mode 100644 index 0000000000..f8e559599e --- /dev/null +++ b/src/dawn/common/Numeric.h @@ -0,0 +1,42 @@ +// Copyright 2022 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef SRC_DAWN_COMMON_NUMERIC_H_ +#define SRC_DAWN_COMMON_NUMERIC_H_ + +#include + +namespace detail { + + template + inline constexpr uint32_t u32_sizeof() { + static_assert(sizeof(T) <= std::numeric_limits::max()); + return uint32_t(sizeof(T)); + } + + template + inline constexpr uint32_t u32_alignof() { + static_assert(alignof(T) <= std::numeric_limits::max()); + return uint32_t(alignof(T)); + } + +} // namespace detail + +template +inline constexpr uint32_t u32_sizeof = detail::u32_sizeof(); + +template +inline constexpr uint32_t u32_alignof = detail::u32_alignof(); + +#endif // SRC_DAWN_COMMON_NUMERIC_H_ diff --git a/src/dawn/common/SlabAllocator.cpp b/src/dawn/common/SlabAllocator.cpp index fad35d3870..23540f57c9 100644 --- a/src/dawn/common/SlabAllocator.cpp +++ b/src/dawn/common/SlabAllocator.cpp @@ -60,10 +60,10 @@ SlabAllocatorImpl::Index SlabAllocatorImpl::kInvalidIndex = SlabAllocatorImpl::SlabAllocatorImpl(Index blocksPerSlab, uint32_t objectSize, uint32_t objectAlignment) - : mAllocationAlignment(std::max(static_cast(alignof(Slab)), objectAlignment)), - mSlabBlocksOffset(Align(sizeof(Slab), objectAlignment)), + : mAllocationAlignment(std::max(u32_alignof, objectAlignment)), + mSlabBlocksOffset(Align(u32_sizeof, objectAlignment)), mIndexLinkNodeOffset(Align(objectSize, alignof(IndexLinkNode))), - mBlockStride(Align(mIndexLinkNodeOffset + sizeof(IndexLinkNode), objectAlignment)), + mBlockStride(Align(mIndexLinkNodeOffset + u32_sizeof, objectAlignment)), mBlocksPerSlab(blocksPerSlab), mTotalAllocationSize( // required allocation size diff --git a/src/dawn/common/SlabAllocator.h b/src/dawn/common/SlabAllocator.h index bcfadf0d54..c94bc254f0 100644 --- a/src/dawn/common/SlabAllocator.h +++ b/src/dawn/common/SlabAllocator.h @@ -19,6 +19,7 @@ #include #include +#include "dawn/common/Numeric.h" #include "dawn/common/PlacementAllocated.h" // The SlabAllocator allocates objects out of one or more fixed-size contiguous "slabs" of memory. @@ -165,8 +166,8 @@ template class SlabAllocator : public SlabAllocatorImpl { public: SlabAllocator(size_t totalObjectBytes, - uint32_t objectSize = sizeof(T), - uint32_t objectAlignment = alignof(T)) + uint32_t objectSize = u32_sizeof, + uint32_t objectAlignment = u32_alignof) : SlabAllocatorImpl(totalObjectBytes / objectSize, objectSize, objectAlignment) { } diff --git a/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp b/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp index ebfd512eb0..7ef374540a 100644 --- a/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp +++ b/src/dawn/native/d3d12/BindGroupLayoutD3D12.cpp @@ -17,7 +17,6 @@ #include #include "dawn/common/BitSetIterator.h" -#include "dawn/native/d3d12/BindGroupD3D12.h" #include "dawn/native/d3d12/DeviceD3D12.h" #include "dawn/native/d3d12/SamplerHeapCacheD3D12.h" #include "dawn/native/d3d12/StagingDescriptorAllocatorD3D12.h" diff --git a/src/dawn/native/d3d12/BindGroupLayoutD3D12.h b/src/dawn/native/d3d12/BindGroupLayoutD3D12.h index 0045831578..1e051a44eb 100644 --- a/src/dawn/native/d3d12/BindGroupLayoutD3D12.h +++ b/src/dawn/native/d3d12/BindGroupLayoutD3D12.h @@ -21,11 +21,11 @@ #include "dawn/common/SlabAllocator.h" #include "dawn/common/ityp_stack_vec.h" +#include "dawn/native/d3d12/BindGroupD3D12.h" #include "dawn/native/d3d12/d3d12_platform.h" namespace dawn::native::d3d12 { - class BindGroup; class CPUDescriptorHeapAllocation; class Device; class StagingDescriptorAllocator; diff --git a/src/dawn/native/opengl/BindGroupLayoutGL.cpp b/src/dawn/native/opengl/BindGroupLayoutGL.cpp index 1cc1474989..ca23169414 100644 --- a/src/dawn/native/opengl/BindGroupLayoutGL.cpp +++ b/src/dawn/native/opengl/BindGroupLayoutGL.cpp @@ -14,8 +14,6 @@ #include "dawn/native/opengl/BindGroupLayoutGL.h" -#include "dawn/native/opengl/BindGroupGL.h" - namespace dawn::native::opengl { BindGroupLayout::BindGroupLayout(DeviceBase* device, diff --git a/src/dawn/native/opengl/BindGroupLayoutGL.h b/src/dawn/native/opengl/BindGroupLayoutGL.h index 5e14798240..c774c7d184 100644 --- a/src/dawn/native/opengl/BindGroupLayoutGL.h +++ b/src/dawn/native/opengl/BindGroupLayoutGL.h @@ -17,10 +17,10 @@ #include "dawn/common/SlabAllocator.h" #include "dawn/native/BindGroupLayout.h" +#include "dawn/native/opengl/BindGroupGL.h" namespace dawn::native::opengl { - class BindGroup; class Device; class BindGroupLayout final : public BindGroupLayoutBase { diff --git a/src/dawn/native/vulkan/BindGroupLayoutVk.cpp b/src/dawn/native/vulkan/BindGroupLayoutVk.cpp index f6e8c6b4b0..04eeff6d7d 100644 --- a/src/dawn/native/vulkan/BindGroupLayoutVk.cpp +++ b/src/dawn/native/vulkan/BindGroupLayoutVk.cpp @@ -20,7 +20,6 @@ #include "dawn/common/BitSetIterator.h" #include "dawn/common/ityp_vector.h" #include "dawn/native/CacheKey.h" -#include "dawn/native/vulkan/BindGroupVk.h" #include "dawn/native/vulkan/DescriptorSetAllocator.h" #include "dawn/native/vulkan/DeviceVk.h" #include "dawn/native/vulkan/FencedDeleter.h" diff --git a/src/dawn/native/vulkan/BindGroupLayoutVk.h b/src/dawn/native/vulkan/BindGroupLayoutVk.h index 4d85a34a9e..096a22727b 100644 --- a/src/dawn/native/vulkan/BindGroupLayoutVk.h +++ b/src/dawn/native/vulkan/BindGroupLayoutVk.h @@ -21,6 +21,7 @@ #include "dawn/common/SlabAllocator.h" #include "dawn/common/vulkan_platform.h" +#include "dawn/native/vulkan/BindGroupVk.h" namespace dawn::native { class CacheKey; @@ -28,7 +29,6 @@ namespace dawn::native { namespace dawn::native::vulkan { - class BindGroup; struct DescriptorSetAllocation; class DescriptorSetAllocator; class Device; diff --git a/src/dawn/native/vulkan/BindGroupVk.h b/src/dawn/native/vulkan/BindGroupVk.h index af7680aebf..0b08c6caee 100644 --- a/src/dawn/native/vulkan/BindGroupVk.h +++ b/src/dawn/native/vulkan/BindGroupVk.h @@ -19,7 +19,6 @@ #include "dawn/common/PlacementAllocated.h" #include "dawn/common/vulkan_platform.h" -#include "dawn/native/vulkan/BindGroupLayoutVk.h" #include "dawn/native/vulkan/DescriptorSetAllocation.h" namespace dawn::native::vulkan {