Skip to content

Commit 575d63d

Browse files
rauhulMaxDesiatov
authored andcommitted
Use mergeable symbols in embedded (#8654)
1 parent a67efeb commit 575d63d

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,19 @@ public final class SwiftModuleBuildDescription {
490490
args += ["-v"]
491491
}
492492

493-
// Enable batch mode whenever WMO is off.
494-
if !self.useWholeModuleOptimization {
495-
args += ["-enable-batch-mode"]
493+
if self.useWholeModuleOptimization {
494+
args.append("-whole-module-optimization")
495+
args.append("-num-threads")
496+
args.append(String(ProcessInfo.processInfo.activeProcessorCount))
497+
} else {
498+
args.append("-incremental")
499+
args.append("-enable-batch-mode")
500+
}
501+
502+
// Workaround for https://github.com/swiftlang/swift-package-manager/issues/8648
503+
if self.useMergeableSymbols {
504+
args.append("-Xfrontend")
505+
args.append("-mergeable-symbols")
496506
}
497507

498508
args += ["-serialize-diagnostics"]
@@ -779,14 +789,6 @@ public final class SwiftModuleBuildDescription {
779789
result.append(outputFileMapPath.pathString)
780790
}
781791

782-
if self.useWholeModuleOptimization {
783-
result.append("-whole-module-optimization")
784-
result.append("-num-threads")
785-
result.append(String(ProcessInfo.processInfo.activeProcessorCount))
786-
} else {
787-
result.append("-incremental")
788-
}
789-
790792
result.append("-c")
791793
result.append(contentsOf: self.sources.map(\.pathString))
792794

@@ -1038,6 +1040,12 @@ public final class SwiftModuleBuildDescription {
10381040
return true
10391041
}
10401042
}
1043+
1044+
// Workaround for https://github.com/swiftlang/swift-package-manager/issues/8648
1045+
/// Whether to build Swift code with -Xfrontend -mergeable-symbols.
1046+
package var useMergeableSymbols: Bool {
1047+
return self.target.underlying.isEmbeddedSwiftTarget
1048+
}
10411049
}
10421050

10431051
extension SwiftModuleBuildDescription {

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,17 +2232,61 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
22322232
)
22332233
XCTAssertNoDiagnostics(observability.diagnostics)
22342234

2235-
// WMO should always be on with Embedded
2236-
let plan = try await mockBuildPlan(
2235+
// -Xfrontend -mergeable symbols should be passed with Embedded
2236+
let result = try await BuildPlanResult(plan: mockBuildPlan(
22372237
graph: graph,
22382238
fileSystem: fs,
22392239
observabilityScope: observability.topScope
2240+
))
2241+
result.checkTargetsCount(1)
2242+
2243+
// Compile Swift Target
2244+
let aCompileArguments = try result.moduleBuildDescription(for: "A").swift().compileArguments()
2245+
let aCompileArgumentsPattern: [StringPattern] = ["-whole-module-optimization"]
2246+
let aCompileArgumentsNegativePattern: [StringPattern] = ["-wmo"]
2247+
XCTAssertMatch(aCompileArguments, aCompileArgumentsPattern)
2248+
XCTAssertNoMatch(aCompileArguments, aCompileArgumentsNegativePattern)
2249+
}
2250+
2251+
// Workaround for: https://github.com/swiftlang/swift-package-manager/issues/8648
2252+
func test_mergeableSymbols_enabledInEmbedded() async throws {
2253+
let Pkg: AbsolutePath = "/Pkg"
2254+
let fs: FileSystem = InMemoryFileSystem(
2255+
emptyFiles:
2256+
Pkg.appending(components: "Sources", "A", "A.swift").pathString
22402257
)
22412258

2242-
let a = try BuildPlanResult(plan: plan)
2243-
.moduleBuildDescription(for: "A").swift().emitCommandLine()
2244-
XCTAssertMatch(a, ["-whole-module-optimization"])
2245-
XCTAssertNoMatch(a, ["-wmo"])
2259+
let observability = ObservabilitySystem.makeForTesting()
2260+
let graph = try loadModulesGraph(
2261+
fileSystem: fs,
2262+
manifests: [
2263+
Manifest.createRootManifest(
2264+
displayName: "Pkg",
2265+
path: .init(validating: Pkg.pathString),
2266+
targets: [
2267+
TargetDescription(
2268+
name: "A",
2269+
settings: [.init(tool: .swift, kind: .enableExperimentalFeature("Embedded"))]
2270+
),
2271+
]
2272+
),
2273+
],
2274+
observabilityScope: observability.topScope
2275+
)
2276+
XCTAssertNoDiagnostics(observability.diagnostics)
2277+
2278+
// -Xfrontend -mergeable symbols should be passed with Embedded
2279+
let result = try await BuildPlanResult(plan: mockBuildPlan(
2280+
graph: graph,
2281+
fileSystem: fs,
2282+
observabilityScope: observability.topScope
2283+
))
2284+
result.checkTargetsCount(1)
2285+
2286+
// Compile Swift Target
2287+
let aCompileArguments = try result.moduleBuildDescription(for: "A").swift().compileArguments()
2288+
let aCompileArgumentsPattern: [StringPattern] = ["-Xfrontend", "-mergeable-symbols"]
2289+
XCTAssertMatch(aCompileArguments, aCompileArgumentsPattern)
22462290
}
22472291

22482292
func testREPLArguments() async throws {

0 commit comments

Comments
 (0)