From 53069240308e3d3ca4236b1ddea15420065e0576 Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Tue, 12 Sep 2023 09:49:25 -0700 Subject: [PATCH 1/5] Add test fixture for experimental-lto-mode --- .../LTO/SwiftAndCTargets/.gitignore | 8 ++++++++ .../LTO/SwiftAndCTargets/Package.swift | 12 ++++++++++++ .../LTO/SwiftAndCTargets/Sources/cLib/cLib.c | 7 +++++++ .../SwiftAndCTargets/Sources/cLib/include/cLib.h | 3 +++ .../LTO/SwiftAndCTargets/Sources/exe/main.swift | 5 +++++ .../Sources/swiftLib/swiftLib.swift | 3 +++ Tests/FunctionalTests/MiscellaneousTests.swift | 16 ++++++++++++++++ 7 files changed, 54 insertions(+) create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/.gitignore create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Package.swift create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/cLib.c create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/include/cLib.h create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/exe/main.swift create mode 100644 Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/swiftLib/swiftLib.swift diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/.gitignore b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/.gitignore new file mode 100644 index 00000000000..0023a534063 --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Package.swift b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Package.swift new file mode 100644 index 00000000000..fb06627fc9e --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Package.swift @@ -0,0 +1,12 @@ +// swift-tools-version: 5.9 + +import PackageDescription + +let package = Package( + name: "SwiftAndCTargets", + targets: [ + .target(name: "cLib"), + .executableTarget(name: "exe", dependencies: ["cLib", "swiftLib"]), + .target(name: "swiftLib"), + ] +) diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/cLib.c b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/cLib.c new file mode 100644 index 00000000000..b52375f8c67 --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/cLib.c @@ -0,0 +1,7 @@ +#include + +#include "include/cLib.h" + +void cPrint(int value) { + printf("c value: %i\n", value); +} diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/include/cLib.h b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/include/cLib.h new file mode 100644 index 00000000000..26f79cc4baa --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/cLib/include/cLib.h @@ -0,0 +1,3 @@ +#pragma once + +extern void cPrint(int); diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/exe/main.swift b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/exe/main.swift new file mode 100644 index 00000000000..48ced4c693e --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/exe/main.swift @@ -0,0 +1,5 @@ +import swiftLib +import cLib + +cPrint(1) +swiftPrint(value: 2) diff --git a/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/swiftLib/swiftLib.swift b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/swiftLib/swiftLib.swift new file mode 100644 index 00000000000..f986bfaa7f9 --- /dev/null +++ b/Fixtures/Miscellaneous/LTO/SwiftAndCTargets/Sources/swiftLib/swiftLib.swift @@ -0,0 +1,3 @@ +public func swiftPrint(value: Int) { + print("swift value: \(value)") +} diff --git a/Tests/FunctionalTests/MiscellaneousTests.swift b/Tests/FunctionalTests/MiscellaneousTests.swift index 51b87f3d733..874ed675824 100644 --- a/Tests/FunctionalTests/MiscellaneousTests.swift +++ b/Tests/FunctionalTests/MiscellaneousTests.swift @@ -337,6 +337,22 @@ class MiscellaneousTestCase: XCTestCase { } } + func testLTO() throws { + try fixture(name: "Miscellaneous/LTO/SwiftAndCTargets") { fixturePath in + do { + let output = try executeSwiftBuild( + fixturePath, + extraArgs: ["--experimental-lto-mode=full"]) + // FIXME: On macOS dsymutil cannot find temporary .o files? (#6890) + // Ensure warnings like the following are not present in build output + // warning: (arm64) /var/folders/ym/6l_0x8vj0b70sz_4h9d70p440000gn/T/main-e120de.o unable to open object file: No such file or directory + // XCTAssertNoMatch(output.stdout, .contains("unable to open object file")) + } catch { + XCTFail("\(error)") + } + } + } + func testUnicode() throws { #if !os(Linux) && !os(Android) // TODO: - Linux has trouble with this and needs investigation. try fixture(name: "Miscellaneous/Unicode") { fixturePath in From 55fc055d63b1f51523079471c7ef208dd7a56cdd Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Thu, 21 Sep 2023 10:57:10 -0700 Subject: [PATCH 2/5] Add missing lto linker flags --- .../BuildDescription/ProductBuildDescription.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/Build/BuildDescription/ProductBuildDescription.swift b/Sources/Build/BuildDescription/ProductBuildDescription.swift index bd63fa749c4..4a64ee91fa0 100644 --- a/Sources/Build/BuildDescription/ProductBuildDescription.swift +++ b/Sources/Build/BuildDescription/ProductBuildDescription.swift @@ -325,6 +325,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription // User arguments (from -Xlinker) should follow generated arguments to allow user overrides args += self.buildParameters.flags.linkerFlags.asSwiftcLinkerFlags() + // Enable the correct lto mode if requested. + switch self.buildParameters.linkingParameters.linkTimeOptimizationMode { + case nil: + break + case .full: + args += ["-lto=llvm-full"] + case .thin: + args += ["-lto=llvm-thin"] + } + // Pass default library paths from the toolchain. for librarySearchPath in self.buildParameters.toolchain.librarySearchPaths { args += ["-L", librarySearchPath.pathString] From 8aa32419c8507f6b40500917e998aa01f9fd9cb8 Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Mon, 6 Nov 2023 15:40:03 -0800 Subject: [PATCH 3/5] Add --verbose flag add a verbose flag to be able to help debug why this fixture is failing on linux --- Tests/FunctionalTests/MiscellaneousTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/FunctionalTests/MiscellaneousTests.swift b/Tests/FunctionalTests/MiscellaneousTests.swift index 874ed675824..8826f1d0665 100644 --- a/Tests/FunctionalTests/MiscellaneousTests.swift +++ b/Tests/FunctionalTests/MiscellaneousTests.swift @@ -342,7 +342,7 @@ class MiscellaneousTestCase: XCTestCase { do { let output = try executeSwiftBuild( fixturePath, - extraArgs: ["--experimental-lto-mode=full"]) + extraArgs: ["--experimental-lto-mode=full", "--verbose"]) // FIXME: On macOS dsymutil cannot find temporary .o files? (#6890) // Ensure warnings like the following are not present in build output // warning: (arm64) /var/folders/ym/6l_0x8vj0b70sz_4h9d70p440000gn/T/main-e120de.o unable to open object file: No such file or directory From 51093ead923352aac425e081eb9a2e896898d4b4 Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Tue, 5 Dec 2023 21:21:03 -0800 Subject: [PATCH 4/5] only run test on macOS to work around missing swift-driver --- Tests/FunctionalTests/MiscellaneousTests.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Tests/FunctionalTests/MiscellaneousTests.swift b/Tests/FunctionalTests/MiscellaneousTests.swift index 8826f1d0665..1d02e294561 100644 --- a/Tests/FunctionalTests/MiscellaneousTests.swift +++ b/Tests/FunctionalTests/MiscellaneousTests.swift @@ -338,6 +338,14 @@ class MiscellaneousTestCase: XCTestCase { } func testLTO() throws { + #if os(macOS) + // FIXME: this test requires swift-driver to be installed + // Currently swift-ci does not build/install swift-driver before running + // swift-package-manager tests which results in this test failing. + // See the following additional discussion: + // - https://github.com/apple/swift/pull/69696 + // - https://github.com/apple/swift/pull/61766 + // - https://github.com/apple/swift-package-manager/pull/5842#issuecomment-1301632685 try fixture(name: "Miscellaneous/LTO/SwiftAndCTargets") { fixturePath in do { let output = try executeSwiftBuild( @@ -351,6 +359,7 @@ class MiscellaneousTestCase: XCTestCase { XCTFail("\(error)") } } + #endif } func testUnicode() throws { From 77c0f9f15dd5127c4f86ef471b9aeaf1d456d9a2 Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Wed, 6 Dec 2023 09:49:14 -0800 Subject: [PATCH 5/5] review comments --- Tests/FunctionalTests/MiscellaneousTests.swift | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Tests/FunctionalTests/MiscellaneousTests.swift b/Tests/FunctionalTests/MiscellaneousTests.swift index 1d02e294561..c4fe10d65ca 100644 --- a/Tests/FunctionalTests/MiscellaneousTests.swift +++ b/Tests/FunctionalTests/MiscellaneousTests.swift @@ -347,17 +347,13 @@ class MiscellaneousTestCase: XCTestCase { // - https://github.com/apple/swift/pull/61766 // - https://github.com/apple/swift-package-manager/pull/5842#issuecomment-1301632685 try fixture(name: "Miscellaneous/LTO/SwiftAndCTargets") { fixturePath in - do { - let output = try executeSwiftBuild( - fixturePath, - extraArgs: ["--experimental-lto-mode=full", "--verbose"]) - // FIXME: On macOS dsymutil cannot find temporary .o files? (#6890) - // Ensure warnings like the following are not present in build output - // warning: (arm64) /var/folders/ym/6l_0x8vj0b70sz_4h9d70p440000gn/T/main-e120de.o unable to open object file: No such file or directory - // XCTAssertNoMatch(output.stdout, .contains("unable to open object file")) - } catch { - XCTFail("\(error)") - } + let output = try executeSwiftBuild( + fixturePath, + extraArgs: ["--experimental-lto-mode=full", "--verbose"]) + // FIXME: On macOS dsymutil cannot find temporary .o files? (#6890) + // Ensure warnings like the following are not present in build output + // warning: (arm64) /var/folders/ym/6l_0x8vj0b70sz_4h9d70p440000gn/T/main-e120de.o unable to open object file: No such file or directory + // XCTAssertNoMatch(output.stdout, .contains("unable to open object file")) } #endif }