[dawn] Use absl::string_view where absl containers are used.
- Places that use absl::flat_hash_map/set cannot always
implicitly convert std::string_view (i.e. in Google3), so
for now, explicitly use absl::string_view.
- Conditionally includes an implicit converter from
the Dawn internal StringView to absl::string_view, and then
use absl::string_view instead of std::string_view when using
absl utilities.
Change-Id: Iaa9fa7f7b1ff47073660520e3b84ce120b46d4c1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/208955
Commit-Queue: Loko Kung <lokokung@google.com>
Reviewed-by: James Price <jrprice@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/generator/templates/dawn/native/api_structs.h b/generator/templates/dawn/native/api_structs.h
index 8c49cba..0affb83 100644
--- a/generator/templates/dawn/native/api_structs.h
+++ b/generator/templates/dawn/native/api_structs.h
@@ -32,6 +32,7 @@
#ifndef {{DIR}}_{{namespace.upper()}}_STRUCTS_H_
#define {{DIR}}_{{namespace.upper()}}_STRUCTS_H_
+#include "absl/strings/string_view.h"
{% set api = metadata.api.lower() %}
{% set CAPI = metadata.c_prefix %}
#include "dawn/{{api}}_cpp.h"
@@ -107,6 +108,19 @@
//* Custom string constructors / conversion
{% if type.name.get() == "string view" %}
{{wgpu_string_members(CppType) | indent(8)}}
+
+ #ifndef ABSL_USES_STD_STRING_VIEW
+ // NOLINTNEXTLINE(runtime/explicit) allow implicit conversion
+ operator absl::string_view() const {
+ if (this->length == wgpu::kStrlen) {
+ if (IsUndefined()) {
+ return {};
+ }
+ return {this->data};
+ }
+ return {this->data, this->length};
+ }
+ #endif
{% endif %}
{% if type.any_member_requires_struct_defaulting %}
diff --git a/src/dawn/native/Pipeline.cpp b/src/dawn/native/Pipeline.cpp
index 3091266..25a94dd 100644
--- a/src/dawn/native/Pipeline.cpp
+++ b/src/dawn/native/Pipeline.cpp
@@ -31,6 +31,7 @@
#include <utility>
#include "absl/container/flat_hash_set.h"
+#include "absl/strings/string_view.h"
#include "dawn/common/Enumerator.h"
#include "dawn/native/BindGroupLayout.h"
#include "dawn/native/Device.h"
@@ -105,7 +106,7 @@
// Keep an initialized constants sets to handle duplicate initialization cases
absl::flat_hash_set<std::string_view> stageInitializedConstantIdentifiers;
for (uint32_t i = 0; i < constantCount; i++) {
- std::string_view key = constants[i].key;
+ absl::string_view key = {constants[i].key};
double value = constants[i].value;
DAWN_INVALID_IF(metadata.overrides.count(key) == 0,
diff --git a/src/dawn/native/ShaderModule.cpp b/src/dawn/native/ShaderModule.cpp
index 2fb5c6b..46b9536 100644
--- a/src/dawn/native/ShaderModule.cpp
+++ b/src/dawn/native/ShaderModule.cpp
@@ -1394,7 +1394,7 @@
return ObjectType::ShaderModule;
}
-bool ShaderModuleBase::HasEntryPoint(std::string_view entryPoint) const {
+bool ShaderModuleBase::HasEntryPoint(absl::string_view entryPoint) const {
return mEntryPoints.contains(entryPoint);
}
@@ -1415,7 +1415,7 @@
return mStrictMath;
}
-const EntryPointMetadata& ShaderModuleBase::GetEntryPoint(std::string_view entryPoint) const {
+const EntryPointMetadata& ShaderModuleBase::GetEntryPoint(absl::string_view entryPoint) const {
DAWN_ASSERT(HasEntryPoint(entryPoint));
return *mEntryPoints.at(entryPoint);
}
diff --git a/src/dawn/native/ShaderModule.h b/src/dawn/native/ShaderModule.h
index f91609d..6ba8db7 100644
--- a/src/dawn/native/ShaderModule.h
+++ b/src/dawn/native/ShaderModule.h
@@ -38,6 +38,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
+#include "absl/strings/string_view.h"
#include "dawn/common/Constants.h"
#include "dawn/common/ContentLessObjectCacheable.h"
#include "dawn/common/MutexProtected.h"
@@ -304,7 +305,7 @@
ObjectType GetType() const override;
// Return true iff the program has an entrypoint called `entryPoint`.
- bool HasEntryPoint(std::string_view entryPoint) const;
+ bool HasEntryPoint(absl::string_view entryPoint) const;
// Return the number of entry points for a stage.
size_t GetEntryPointCount(SingleShaderStage stage) const { return mEntryPointCounts[stage]; }
@@ -315,7 +316,7 @@
// Return the metadata for the given `entryPoint`. HasEntryPoint with the same argument
// must be true.
- const EntryPointMetadata& GetEntryPoint(std::string_view entryPoint) const;
+ const EntryPointMetadata& GetEntryPoint(absl::string_view entryPoint) const;
// Functions necessary for the unordered_set<ShaderModuleBase*>-based cache.
size_t ComputeContentHash() override;
diff --git a/src/dawn/native/webgpu_absl_format.cpp b/src/dawn/native/webgpu_absl_format.cpp
index a286833..2984617 100644
--- a/src/dawn/native/webgpu_absl_format.cpp
+++ b/src/dawn/native/webgpu_absl_format.cpp
@@ -701,11 +701,7 @@
}
s->Append("\"");
-#ifdef ABSL_USES_STD_STRING_VIEW
- s->Append(std::string_view(value));
-#else
- s->Append(absl::string_view(value.data, value.length));
-#endif
+ s->Append(absl::string_view(value));
s->Append("\"");
return {true};
}