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 <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
This commit is contained in:
Austin Eng 2022-04-28 17:58:33 +00:00 committed by Dawn LUCI CQ
parent 71b5ea803d
commit b0b53ba403
12 changed files with 53 additions and 13 deletions

View File

@ -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",

View File

@ -45,6 +45,7 @@ target_sources(dawn_common PRIVATE
"Math.h"
"NSRef.h"
"NonCopyable.h"
"Numeric.h"
"PlacementAllocated.h"
"Platform.h"
"Preprocessor.h"

42
src/dawn/common/Numeric.h Normal file
View File

@ -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 <limits>
namespace detail {
template <typename T>
inline constexpr uint32_t u32_sizeof() {
static_assert(sizeof(T) <= std::numeric_limits<uint32_t>::max());
return uint32_t(sizeof(T));
}
template <typename T>
inline constexpr uint32_t u32_alignof() {
static_assert(alignof(T) <= std::numeric_limits<uint32_t>::max());
return uint32_t(alignof(T));
}
} // namespace detail
template <typename T>
inline constexpr uint32_t u32_sizeof = detail::u32_sizeof<T>();
template <typename T>
inline constexpr uint32_t u32_alignof = detail::u32_alignof<T>();
#endif // SRC_DAWN_COMMON_NUMERIC_H_

View File

@ -60,10 +60,10 @@ SlabAllocatorImpl::Index SlabAllocatorImpl::kInvalidIndex =
SlabAllocatorImpl::SlabAllocatorImpl(Index blocksPerSlab,
uint32_t objectSize,
uint32_t objectAlignment)
: mAllocationAlignment(std::max(static_cast<uint32_t>(alignof(Slab)), objectAlignment)),
mSlabBlocksOffset(Align(sizeof(Slab), objectAlignment)),
: mAllocationAlignment(std::max(u32_alignof<Slab>, objectAlignment)),
mSlabBlocksOffset(Align(u32_sizeof<Slab>, objectAlignment)),
mIndexLinkNodeOffset(Align(objectSize, alignof(IndexLinkNode))),
mBlockStride(Align(mIndexLinkNodeOffset + sizeof(IndexLinkNode), objectAlignment)),
mBlockStride(Align(mIndexLinkNodeOffset + u32_sizeof<IndexLinkNode>, objectAlignment)),
mBlocksPerSlab(blocksPerSlab),
mTotalAllocationSize(
// required allocation size

View File

@ -19,6 +19,7 @@
#include <type_traits>
#include <utility>
#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 <typename T>
class SlabAllocator : public SlabAllocatorImpl {
public:
SlabAllocator(size_t totalObjectBytes,
uint32_t objectSize = sizeof(T),
uint32_t objectAlignment = alignof(T))
uint32_t objectSize = u32_sizeof<T>,
uint32_t objectAlignment = u32_alignof<T>)
: SlabAllocatorImpl(totalObjectBytes / objectSize, objectSize, objectAlignment) {
}

View File

@ -17,7 +17,6 @@
#include <utility>
#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"

View File

@ -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;

View File

@ -14,8 +14,6 @@
#include "dawn/native/opengl/BindGroupLayoutGL.h"
#include "dawn/native/opengl/BindGroupGL.h"
namespace dawn::native::opengl {
BindGroupLayout::BindGroupLayout(DeviceBase* device,

View File

@ -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 {

View File

@ -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"

View File

@ -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;

View File

@ -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 {