Skip to content

[cxx-interop] Add support for benchmarks with C++ interop. #32721

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

Merged
merged 1 commit into from
Nov 20, 2020
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
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ set(SWIFT_BENCH_MODULES
single-source/Walsh
single-source/WordCount
single-source/XorLoop
cxx-source/CreateObjects
)

set(SWIFT_MULTISOURCE_SWIFT_BENCHES
Expand Down
25 changes: 24 additions & 1 deletion benchmark/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ var singleSourceLibraries: [String] = singleSourceLibraryDirs.flatMap {
getSingleSourceLibraries(subDirectory: $0)
}

var cxxSingleSourceLibraryDirs: [String] = ["cxx-source"]
var cxxSingleSourceLibraries: [String] = cxxSingleSourceLibraryDirs.flatMap {
getSingleSourceLibraries(subDirectory: $0)
}

//===---
// Multi Source Libraries
//
Expand Down Expand Up @@ -80,6 +85,7 @@ products.append(.library(name: "ObjectiveCTests", type: .static, targets: ["Obje
products.append(.executable(name: "SwiftBench", targets: ["SwiftBench"]))

products += singleSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
products += cxxSingleSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
products += multiSourceLibraries.map {
return .library(name: $0.name, type: .static, targets: [$0.name])
}
Expand All @@ -103,13 +109,18 @@ swiftBenchDeps.append(.target(name: "ObjectiveCTests"))
#endif
swiftBenchDeps.append(.target(name: "DriverUtils"))
swiftBenchDeps += singleSourceLibraries.map { .target(name: $0) }
swiftBenchDeps += cxxSingleSourceLibraries.map { .target(name: $0) }
swiftBenchDeps += multiSourceLibraries.map { .target(name: $0.name) }

targets.append(
.target(name: "SwiftBench",
dependencies: swiftBenchDeps,
path: "utils",
sources: ["main.swift"]))
sources: ["main.swift"],
swiftSettings: [.unsafeFlags(["-Xfrontend",
"-enable-cxx-interop",
"-I",
"utils/CxxTests"])]))

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

targets += cxxSingleSourceLibraries.map { name in
return .target(
name: name,
dependencies: singleSourceDeps,
path: "cxx-source",
sources: ["\(name).swift"],
swiftSettings: [.unsafeFlags(["-Xfrontend",
"-enable-cxx-interop",
"-I",
"utils/CxxTests"])])
}

targets += multiSourceLibraries.map { lib in
return .target(
name: lib.name,
Expand Down
10 changes: 10 additions & 0 deletions benchmark/cmake/modules/AddSwiftBenchmarkSuite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ function (swift_benchmark_compile_archopts)
list(APPEND SWIFT_BENCH_OBJFILES "${objfile}")
list(APPEND bench_library_swiftmodules "${swiftmodule}")

# Only set "enable-cxx-interop" for tests in the "cxx-source" directory.
set(cxx_options "")
if ("${module_name_path}" MATCHES ".*cxx-source/.*")
list(APPEND cxx_options "-Xfrontend" "-enable-cxx-interop" "-I" "${srcdir}/utils/CxxTests/")
endif()

if ("${bench_flags}" MATCHES "-whole-module.*")
set(output_option "-o" "${objfile}")
else()
Expand All @@ -501,6 +507,7 @@ function (swift_benchmark_compile_archopts)
"-module-name" "${module_name}"
"-emit-module-path" "${swiftmodule}"
"-I" "${objdir}"
${cxx_options}
${output_option}
"${source}")
if (SWIFT_BENCHMARK_EMIT_SIB)
Expand All @@ -518,6 +525,7 @@ function (swift_benchmark_compile_archopts)
${SWIFT_BENCHMARK_EXTRA_FLAGS}
"-module-name" "${module_name}"
"-I" "${objdir}"
${cxx_options}
"-emit-sib"
"-o" "${sibfile}"
"${source}")
Expand Down Expand Up @@ -582,6 +590,8 @@ function (swift_benchmark_compile_archopts)
"-whole-module-optimization"
"-emit-module" "-module-name" "${module_name}"
"-I" "${objdir}"
"-Xfrontend" "-enable-cxx-interop"
"-I" "${srcdir}/utils/CxxTests/"
"-o" "${objdir}/${module_name}.o"
"${source}")
list(APPEND SWIFT_BENCH_OBJFILES "${objdir}/${module_name}.o")
Expand Down
31 changes: 31 additions & 0 deletions benchmark/cxx-source/CreateObjects.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===--- CreateObjects.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// This is a simple test that creates thousands of C++ objects and does nothing
// with them.

import TestsUtils
import CxxCreateObjects

public let CreateObjects = BenchmarkInfo(
name: "CreateObjects",
runFunction: run_CreateObjects,
tags: [.validation, .bridging])

@inline(never)
public func run_CreateObjects(_ N: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this benchmark is just there to validate that C++ support is compiling fine.
I think it makes sense to disable this benchmark from the regular run, because it's not doing anything interesting.
Can you add the ".skip" benchmark tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I'd like to test the performance of creating C++ objects. For example, I expect that this will be much slower after #30630 (when we actually have to call the constructor instead of just allocating an empty object).

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Makes sense

for i in 0...(N * 10_000) {
let x = Int32(i)
let f = CxxLoadableIntWrapper(value: x)
blackHole(f)
}
}
3 changes: 3 additions & 0 deletions benchmark/utils/CxxTests/CreateObjects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct CxxLoadableIntWrapper {
int value;
};
3 changes: 3 additions & 0 deletions benchmark/utils/CxxTests/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module CxxCreateObjects {
header "CreateObjects.h"
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Chars
import ClassArrayGetter
import Codable
import Combos
import CreateObjects
import DataBenchmarks
import DeadArray
import DevirtualizeProtocolComposition
Expand Down Expand Up @@ -235,6 +236,7 @@ registerBenchmark(Chars)
registerBenchmark(Codable)
registerBenchmark(Combos)
registerBenchmark(ClassArrayGetter)
registerBenchmark(CreateObjects)
registerBenchmark(DataBenchmarks)
registerBenchmark(DeadArray)
registerBenchmark(DevirtualizeProtocolComposition)
Expand Down