Skip to content

Commit aa04872

Browse files
authored
Merge pull request #32721 from zoecarver/cxx/benchmark/create-objects
[cxx-interop] Add support for benchmarks with C++ interop.
2 parents 59f1f10 + 01b12cf commit aa04872

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

benchmark/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ set(SWIFT_BENCH_MODULES
189189
single-source/Walsh
190190
single-source/WordCount
191191
single-source/XorLoop
192+
cxx-source/CreateObjects
192193
)
193194

194195
set(SWIFT_MULTISOURCE_SWIFT_BENCHES

benchmark/Package.swift

+24-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ var singleSourceLibraries: [String] = singleSourceLibraryDirs.flatMap {
4949
getSingleSourceLibraries(subDirectory: $0)
5050
}
5151

52+
var cxxSingleSourceLibraryDirs: [String] = ["cxx-source"]
53+
var cxxSingleSourceLibraries: [String] = cxxSingleSourceLibraryDirs.flatMap {
54+
getSingleSourceLibraries(subDirectory: $0)
55+
}
56+
5257
//===---
5358
// Multi Source Libraries
5459
//
@@ -80,6 +85,7 @@ products.append(.library(name: "ObjectiveCTests", type: .static, targets: ["Obje
8085
products.append(.executable(name: "SwiftBench", targets: ["SwiftBench"]))
8186

8287
products += singleSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
88+
products += cxxSingleSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
8389
products += multiSourceLibraries.map {
8490
return .library(name: $0.name, type: .static, targets: [$0.name])
8591
}
@@ -103,13 +109,18 @@ swiftBenchDeps.append(.target(name: "ObjectiveCTests"))
103109
#endif
104110
swiftBenchDeps.append(.target(name: "DriverUtils"))
105111
swiftBenchDeps += singleSourceLibraries.map { .target(name: $0) }
112+
swiftBenchDeps += cxxSingleSourceLibraries.map { .target(name: $0) }
106113
swiftBenchDeps += multiSourceLibraries.map { .target(name: $0.name) }
107114

108115
targets.append(
109116
.target(name: "SwiftBench",
110117
dependencies: swiftBenchDeps,
111118
path: "utils",
112-
sources: ["main.swift"]))
119+
sources: ["main.swift"],
120+
swiftSettings: [.unsafeFlags(["-Xfrontend",
121+
"-enable-cxx-interop",
122+
"-I",
123+
"utils/CxxTests"])]))
113124

114125
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
115126
targets.append(
@@ -139,6 +150,18 @@ targets += singleSourceLibraries.map { name in
139150
sources: ["\(name).swift"])
140151
}
141152

153+
targets += cxxSingleSourceLibraries.map { name in
154+
return .target(
155+
name: name,
156+
dependencies: singleSourceDeps,
157+
path: "cxx-source",
158+
sources: ["\(name).swift"],
159+
swiftSettings: [.unsafeFlags(["-Xfrontend",
160+
"-enable-cxx-interop",
161+
"-I",
162+
"utils/CxxTests"])])
163+
}
164+
142165
targets += multiSourceLibraries.map { lib in
143166
return .target(
144167
name: lib.name,

benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake

+10
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,12 @@ function (swift_benchmark_compile_archopts)
478478
list(APPEND SWIFT_BENCH_OBJFILES "${objfile}")
479479
list(APPEND bench_library_swiftmodules "${swiftmodule}")
480480

481+
# Only set "enable-cxx-interop" for tests in the "cxx-source" directory.
482+
set(cxx_options "")
483+
if ("${module_name_path}" MATCHES ".*cxx-source/.*")
484+
list(APPEND cxx_options "-Xfrontend" "-enable-cxx-interop" "-I" "${srcdir}/utils/CxxTests/")
485+
endif()
486+
481487
if ("${bench_flags}" MATCHES "-whole-module.*")
482488
set(output_option "-o" "${objfile}")
483489
else()
@@ -501,6 +507,7 @@ function (swift_benchmark_compile_archopts)
501507
"-module-name" "${module_name}"
502508
"-emit-module-path" "${swiftmodule}"
503509
"-I" "${objdir}"
510+
${cxx_options}
504511
${output_option}
505512
"${source}")
506513
if (SWIFT_BENCHMARK_EMIT_SIB)
@@ -518,6 +525,7 @@ function (swift_benchmark_compile_archopts)
518525
${SWIFT_BENCHMARK_EXTRA_FLAGS}
519526
"-module-name" "${module_name}"
520527
"-I" "${objdir}"
528+
${cxx_options}
521529
"-emit-sib"
522530
"-o" "${sibfile}"
523531
"${source}")
@@ -582,6 +590,8 @@ function (swift_benchmark_compile_archopts)
582590
"-whole-module-optimization"
583591
"-emit-module" "-module-name" "${module_name}"
584592
"-I" "${objdir}"
593+
"-Xfrontend" "-enable-cxx-interop"
594+
"-I" "${srcdir}/utils/CxxTests/"
585595
"-o" "${objdir}/${module_name}.o"
586596
"${source}")
587597
list(APPEND SWIFT_BENCH_OBJFILES "${objdir}/${module_name}.o")
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- CreateObjects.swift ----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This is a simple test that creates thousands of C++ objects and does nothing
14+
// with them.
15+
16+
import TestsUtils
17+
import CxxCreateObjects
18+
19+
public let CreateObjects = BenchmarkInfo(
20+
name: "CreateObjects",
21+
runFunction: run_CreateObjects,
22+
tags: [.validation, .bridging])
23+
24+
@inline(never)
25+
public func run_CreateObjects(_ N: Int) {
26+
for i in 0...(N * 10_000) {
27+
let x = Int32(i)
28+
let f = CxxLoadableIntWrapper(value: x)
29+
blackHole(f)
30+
}
31+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct CxxLoadableIntWrapper {
2+
int value;
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module CxxCreateObjects {
2+
header "CreateObjects.h"
3+
}

benchmark/utils/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import Chars
4848
import ClassArrayGetter
4949
import Codable
5050
import Combos
51+
import CreateObjects
5152
import DataBenchmarks
5253
import DeadArray
5354
import DevirtualizeProtocolComposition
@@ -236,6 +237,7 @@ registerBenchmark(Chars)
236237
registerBenchmark(Codable)
237238
registerBenchmark(Combos)
238239
registerBenchmark(ClassArrayGetter)
240+
registerBenchmark(CreateObjects)
239241
registerBenchmark(DataBenchmarks)
240242
registerBenchmark(DeadArray)
241243
registerBenchmark(DevirtualizeProtocolComposition)

0 commit comments

Comments
 (0)