partition_alloc: CMake and GN config
Start using "partition_alloc" in Dawn using both GN and CMake.
This is accompanied with a minimal usage to prove it works correctly.
This is a new attempt. I reverted the previous one:
https://dawn-review.googlesource.com/q/commit:c5f72f7a85aeeb21d7a9a12f28918970def41880
This patch makes the partition_alloc dependency optional. This make it
easier to embed dawn to work across every BUILD system and embedders
(skia, ...).
Next steps:
- Enable BackupRefPtr and DanglingPointerDetector in tests (GN only).
- Apply MiraclePtr rewrite.
- (stretch) clang-tidy enforcement
Bug: chromium:1464560
Change-Id: Ic262905f6cfa858377dd014b18744556dc1c3b8b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/164894
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
diff --git a/DEPS b/DEPS
index 4959bdf..ec089c2 100644
--- a/DEPS
+++ b/DEPS
@@ -287,7 +287,7 @@
# Dependencies for PartitionAlloc.
# Doc: https://docs.google.com/document/d/1wz45t0alQthsIU9P7_rQcfQyqnrBMXzrOjSzdQo-V-A
'third_party/partition_alloc': {
- 'url': '{chromium_git}/chromium/src/base/allocator/partition_allocator.git@6f90cb04abb81942abaab7b63d34c02882208172',
+ 'url': '{chromium_git}/chromium/src/base/allocator/partition_allocator.git@cb18695870869cf972b76cdd27d52f86c1752d0e',
'condition': 'dawn_standalone',
},
}
diff --git a/build_overrides/dawn.gni b/build_overrides/dawn.gni
index b5b5218..79e3911 100644
--- a/build_overrides/dawn.gni
+++ b/build_overrides/dawn.gni
@@ -45,7 +45,6 @@
dawn_jinja2_dir = "//third_party/jinja2"
dawn_glfw_dir = "//third_party/glfw"
dawn_googletest_dir = "//third_party/googletest"
-dawn_partition_alloc_dir = "//third_party/partition_alloc/"
dawn_spirv_tools_dir = "//third_party/vulkan-deps/spirv-tools/src"
dawn_swiftshader_dir = "//third_party/swiftshader"
dawn_vulkan_loader_dir = "//third_party/vulkan-deps/vulkan-loader/src"
@@ -54,6 +53,12 @@
dawn_dxc_dir = "//third_party/dxc"
dawn_dxheaders_dir = "//third_party/dxheaders"
+# PartitionAlloc is an optional dependency.
+# It does not fully support the MSVC compiler at the moment.
+if (is_clang || !is_win) {
+ dawn_partition_alloc_dir = "//third_party/partition_alloc/"
+}
+
# Optional path to a one-liner version file. Default is empty path indicating
# that git should be used to figure out the version.
dawn_version_file = ""
diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt
index f4e194d..5875f64 100644
--- a/src/dawn/CMakeLists.txt
+++ b/src/dawn/CMakeLists.txt
@@ -29,6 +29,7 @@
# Dawn projects
###############################################################################
+add_subdirectory(partition_alloc)
add_subdirectory(common)
add_subdirectory(platform)
add_subdirectory(native)
diff --git a/src/dawn/native/BUILD.gn b/src/dawn/native/BUILD.gn
index 9d3cc9f..4069852 100644
--- a/src/dawn/native/BUILD.gn
+++ b/src/dawn/native/BUILD.gn
@@ -186,6 +186,7 @@
# dawn native target
public_deps = [
":abseil",
+ "${dawn_root}/src/dawn/partition_alloc:raw_ptr",
"${dawn_root}/src/dawn/platform",
]
diff --git a/src/dawn/native/CMakeLists.txt b/src/dawn/native/CMakeLists.txt
index 296b89f..e078602 100644
--- a/src/dawn/native/CMakeLists.txt
+++ b/src/dawn/native/CMakeLists.txt
@@ -247,6 +247,7 @@
)
target_link_libraries(dawn_native
PUBLIC dawncpp_headers
+ partition_alloc
PRIVATE dawn_common
dawn_platform
dawn_internal_config
diff --git a/src/dawn/native/PipelineLayout.h b/src/dawn/native/PipelineLayout.h
index 1913f10..7d773f5 100644
--- a/src/dawn/native/PipelineLayout.h
+++ b/src/dawn/native/PipelineLayout.h
@@ -43,6 +43,7 @@
#include "dawn/native/IntegerTypes.h"
#include "dawn/native/ObjectBase.h"
#include "dawn/native/dawn_platform.h"
+#include "partition_alloc/pointers/raw_ptr.h"
namespace dawn::native {
@@ -59,7 +60,7 @@
ConstantEntry const* constants);
SingleShaderStage shaderStage;
- ShaderModuleBase* module;
+ raw_ptr<ShaderModuleBase> module;
std::string entryPoint;
size_t constantCount = 0u;
ConstantEntry const* constants = nullptr;
diff --git a/src/dawn/partition_alloc/BUILD.gn b/src/dawn/partition_alloc/BUILD.gn
new file mode 100644
index 0000000..39247b7
--- /dev/null
+++ b/src/dawn/partition_alloc/BUILD.gn
@@ -0,0 +1,25 @@
+import("//build_overrides/dawn.gni")
+
+# This file provides Dawn, the MiraclePtr "raw_ptr" definition.
+# Depending on the configuration, this is either:
+# - Delegated to the `partition_alloc` library.
+# - Provided by Dawn itself as "no-op" implementation.
+
+# This define the same targets as in partition_alloc. See Chrome's file:
+# - base/allocator/partition_allocator/BUILD.gn
+# - base/allocator/partition_allocator/src/partition_alloc/BUILD.gn
+
+if (defined(dawn_partition_alloc_dir)) {
+ group("raw_ptr") {
+ public_deps = [ "${dawn_partition_alloc_dir}:raw_ptr" ]
+ }
+} else {
+ config("public_includes") {
+ include_dirs = [ "." ]
+ }
+
+ source_set("raw_ptr") {
+ public_configs = [ ":public_includes" ]
+ public = [ "partition_alloc/pointers/raw_ptr.h" ]
+ }
+}
diff --git a/src/dawn/partition_alloc/CMakeLists.txt b/src/dawn/partition_alloc/CMakeLists.txt
new file mode 100644
index 0000000..ae49462
--- /dev/null
+++ b/src/dawn/partition_alloc/CMakeLists.txt
@@ -0,0 +1,2 @@
+add_library(partition_alloc INTERFACE)
+target_include_directories(partition_alloc INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
diff --git a/src/dawn/partition_alloc/README.md b/src/dawn/partition_alloc/README.md
new file mode 100644
index 0000000..985442f
--- /dev/null
+++ b/src/dawn/partition_alloc/README.md
@@ -0,0 +1,23 @@
+This directory provide Dawn a "no-op" partition_alloc implementation of raw_ptr. This is used only
+when `dawn_partition_alloc_dir` is not set.
+
+# Why not using the PartitionAlloc "no-op" implementation?
+For now, we decided Dawn to provide its own. This allow the partition_alloc dependency to be
+optional. It makes it easier easier to integrate into other project (Skia,
+etc...) and build system (CMake, Bazel, etc..)
+
+Moreover, partition_alloc is still depending on chromium's //build for now. It
+means it can only be used inside Dawn who has the same dependency. It can't be
+used outside.
+
+# TODO(arthursonzogni): https://crbug.com/1464560
+
+Provide additional files:
+- partition_alloc/pointers/raw_ptr_cast.h
+- partition_alloc/pointers/raw_ptr_exclusion.h
+- partition_alloc/pointers/raw_ref.h
+
+Expand the raw_ptr implementation:
+- RawPtrTraits
+- raw_ptr::* extra functions.
+- etc...
diff --git a/src/dawn/partition_alloc/partition_alloc/pointers/raw_ptr.h b/src/dawn/partition_alloc/partition_alloc/pointers/raw_ptr.h
new file mode 100644
index 0000000..9af8cae
--- /dev/null
+++ b/src/dawn/partition_alloc/partition_alloc/pointers/raw_ptr.h
@@ -0,0 +1,43 @@
+// Copyright 2023 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef SRC_DAWN_PARTITION_ALLOC_PARTITION_ALLOC_POINTERS_RAW_PTR_H_
+#define SRC_DAWN_PARTITION_ALLOC_PARTITION_ALLOC_POINTERS_RAW_PTR_H_
+
+// `raw_ptr<T>` is a non-owning smart pointer that has improved memory-safety
+// over raw pointers. See the documentation for details:
+// https://source.chromium.org/chromium/chromium/src/+/main:base/memory/raw_ptr.md
+//
+// Here, dawn provides a "no-op" implementation, because partition_alloc
+// dependendency is missing.
+//
+// TODO(arthursonzogni): https://crbug.com/1464560, Complete the "no-op"
+// implementation.
+template <typename T>
+using raw_ptr = T*;
+
+#endif // SRC_DAWN_PARTITION_ALLOC_PARTITION_ALLOC_POINTERS_RAW_PTR_H_
diff --git a/third_party/partition_alloc b/third_party/partition_alloc
index 6f90cb0..cb18695 160000
--- a/third_party/partition_alloc
+++ b/third_party/partition_alloc
@@ -1 +1 @@
-Subproject commit 6f90cb04abb81942abaab7b63d34c02882208172
+Subproject commit cb18695870869cf972b76cdd27d52f86c1752d0e