Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cxx-interop][stdlib] windows - use new hash inline functions like other platforms #78161

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stdlib/public/Cxx/cxxshim/libcxxstdlibshim.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

/// Used for std::string conformance to Swift.Hashable
typedef std::hash<std::string> __swift_interopHashOfString;
inline std::size_t __swift_interopComputeHashOfString(std::string str) {
inline std::size_t __swift_interopComputeHashOfString(const std::string &str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess making the parameter a const reference avoids the IR verifier failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

return __swift_interopHashOfString()(str);
}

/// Used for std::u16string conformance to Swift.Hashable
typedef std::hash<std::u16string> __swift_interopHashOfU16String;
inline std::size_t __swift_interopComputeHashOfU16String(std::u16string str) {
inline std::size_t __swift_interopComputeHashOfU16String(const std::u16string &str) {
return __swift_interopHashOfU16String()(str);
}

/// Used for std::u32string conformance to Swift.Hashable
typedef std::hash<std::u32string> __swift_interopHashOfU32String;
inline std::size_t __swift_interopComputeHashOfU32String(std::u32string str) {
inline std::size_t __swift_interopComputeHashOfU32String(const std::u32string &str) {
return __swift_interopHashOfU32String()(str);
}

Expand Down
12 changes: 0 additions & 12 deletions stdlib/public/Cxx/std/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ extension std.string: Hashable {
@_alwaysEmitIntoClient
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::string>::operator()
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
let cxxHash = __swift_interopHashOfString().callAsFunction(self)
#else
let cxxHash = __swift_interopComputeHashOfString(self)
#endif
hasher.combine(cxxHash)
}
}
Expand All @@ -211,11 +207,7 @@ extension std.u16string: Hashable {
@_alwaysEmitIntoClient
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::u16string>::operator()
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
let cxxHash = __swift_interopHashOfU16String().callAsFunction(self)
#else
let cxxHash = __swift_interopComputeHashOfU16String(self)
#endif
hasher.combine(cxxHash)
}
}
Expand All @@ -224,11 +216,7 @@ extension std.u32string: Hashable {
@_alwaysEmitIntoClient
public func hash(into hasher: inout Hasher) {
// Call std::hash<std::u32string>::operator()
#if os(Windows) // FIXME: https://github.com/swiftlang/swift/issues/77856
let cxxHash = __swift_interopHashOfU32String().callAsFunction(self)
#else
let cxxHash = __swift_interopComputeHashOfU32String(self)
#endif
hasher.combine(cxxHash)
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ module StdString {
export *
}

module StdStringAndVector {
header "std-string-and-vector.h"
requires cplusplus
export *
}

module StdStringView {
header "std-string-view.h"
requires cplusplus
Expand Down
11 changes: 11 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-string-and-vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <string>
#include <vector>

struct Item {
std::vector<std::string> keys;
std::vector<std::string> values;
};

inline Item get_item() {
return {};
}
20 changes: 20 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-string-with-opts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=default -Xcc -std=c++20 -O)
//
// REQUIRES: executable_test

// Tests optimizations related to CxxStdlib.

import StdlibUnittest
import CxxStdlib
import StdStringAndVector

var StdStringOptTestSuite = TestSuite("StdStringWithOpts")

StdStringOptTestSuite.test("std::string with Hashable conformance optimized") {
let item = get_item()
let dict = Dictionary(uniqueKeysWithValues: zip(item.keys, item.values).lazy)

expectEqual(dict.count, 0)
}

runAllTests()