mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[cxx-interop] Add support for benchmarks with C++ interop.
* Adds support for benchmarks that use C++ modules. * Adds "CreateObjects" benchmark that creates C++ objects.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
//
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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(
|
||||
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
@@ -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}")
|
||||
@@ -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")
|
||||
|
||||
31
benchmark/cxx-source/CreateObjects.swift
Normal file
31
benchmark/cxx-source/CreateObjects.swift
Normal file
@@ -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) {
|
||||
for i in 0...(N * 10_000) {
|
||||
let x = Int32(i)
|
||||
let f = CxxLoadableIntWrapper(value: x)
|
||||
blackHole(f)
|
||||
}
|
||||
}
|
||||
3
benchmark/utils/CxxTests/CreateObjects.h
Normal file
3
benchmark/utils/CxxTests/CreateObjects.h
Normal file
@@ -0,0 +1,3 @@
|
||||
struct CxxLoadableIntWrapper {
|
||||
int value;
|
||||
};
|
||||
3
benchmark/utils/CxxTests/module.modulemap
Normal file
3
benchmark/utils/CxxTests/module.modulemap
Normal file
@@ -0,0 +1,3 @@
|
||||
module CxxCreateObjects {
|
||||
header "CreateObjects.h"
|
||||
}
|
||||
@@ -48,6 +48,7 @@ import Chars
|
||||
import ClassArrayGetter
|
||||
import Codable
|
||||
import Combos
|
||||
import CreateObjects
|
||||
import DataBenchmarks
|
||||
import DeadArray
|
||||
import DevirtualizeProtocolComposition
|
||||
@@ -235,6 +236,7 @@ registerBenchmark(Chars)
|
||||
registerBenchmark(Codable)
|
||||
registerBenchmark(Combos)
|
||||
registerBenchmark(ClassArrayGetter)
|
||||
registerBenchmark(CreateObjects)
|
||||
registerBenchmark(DataBenchmarks)
|
||||
registerBenchmark(DeadArray)
|
||||
registerBenchmark(DevirtualizeProtocolComposition)
|
||||
|
||||
Reference in New Issue
Block a user