@@ -102,6 +102,11 @@ in batch file format instead of executing them.
102
102
. PARAMETER HostArchName
103
103
The architecture where the toolchain will execute.
104
104
105
+ . PARAMETER Allocator
106
+ The memory allocator used in the toolchain binaries, if it's
107
+ `mimalloc`, it uses mimalloc. Otherwise, it uses the default
108
+ allocator.
109
+
105
110
. EXAMPLE
106
111
PS> .\Build.ps1
107
112
@@ -138,6 +143,7 @@ param(
138
143
[switch ] $DebugInfo ,
139
144
[switch ] $EnableCaching ,
140
145
[string ] $Cache = " " ,
146
+ [string ] $Allocator = " " ,
141
147
[switch ] $Summary ,
142
148
[switch ] $ToBatch
143
149
)
@@ -1465,6 +1471,59 @@ function Build-Compilers() {
1465
1471
}
1466
1472
}
1467
1473
1474
+ # Reference: https://github.com/microsoft/mimalloc/tree/dev/bin#minject
1475
+ # TODO: Add ARM64
1476
+ function Build-Mimalloc () {
1477
+ [CmdletBinding (PositionalBinding = $false )]
1478
+ param
1479
+ (
1480
+ [Parameter (Position = 0 , Mandatory = $true )]
1481
+ [hashtable ]$Arch
1482
+ )
1483
+
1484
+ if ($Arch -eq $ArchX64 ) {
1485
+ $Args = @ ()
1486
+ Isolate- EnvVars {
1487
+ Invoke-VsDevShell $Arch
1488
+ # Avoid hard-coding the VC tools version number
1489
+ $VCRedistDir = (Get-ChildItem " ${env: VCToolsRedistDir} \$ ( $HostArch.ShortName ) " - Filter " Microsoft.VC*.CRT" ).FullName
1490
+ if ($VCRedistDir ) {
1491
+ $Args += " -p:VCRedistDir=$VCRedistDir \"
1492
+ }
1493
+ }
1494
+ $Args += " $SourceCache \mimalloc\ide\vs2022\mimalloc.sln"
1495
+ $Args += " -p:Configuration=Release"
1496
+ $Args += " -p:ProductArchitecture=$ ( $Arch.VSName ) "
1497
+ Invoke-Program $msbuild @Args
1498
+ $Dest = " $ ( $Arch.ToolchainInstallRoot ) \usr\bin"
1499
+ Copy-Item - Path " $SourceCache \mimalloc\out\msvc-$ ( $Arch.ShortName ) \Release\mimalloc-override.dll" `
1500
+ - Destination " $Dest "
1501
+ Copy-Item - Path " $SourceCache \mimalloc\out\msvc-$ ( $Arch.ShortName ) \Release\mimalloc-redirect.dll" `
1502
+ - Destination " $Dest "
1503
+ $MimallocExecutables = @ (" swift.exe" , " swiftc.exe" , " swift-driver.exe" , " swift-frontend.exe" )
1504
+ $MimallocExecutables += @ (" clang.exe" , " clang++.exe" , " clang-cl.exe" )
1505
+ $MimallocExecutables += @ (" lld.exe" , " lld-link.exe" , " ld.lld.exe" , " ld64.lld.exe" )
1506
+ foreach ($Exe in $MimallocExecutables ) {
1507
+ $ExePath = [IO.Path ]::Combine($Dest , $Exe )
1508
+ # Binary-patch in place
1509
+ $Args = @ ()
1510
+ $Args += " -f"
1511
+ $Args += " -i"
1512
+ $Args += " -v"
1513
+ $Args += $ExePath
1514
+ Invoke-Program " $SourceCache \mimalloc\bin\minject" @Args
1515
+ # Log the import table
1516
+ $Args = @ ()
1517
+ $Args += " -l"
1518
+ $Args += $ExePath
1519
+ Invoke-Program " $SourceCache \mimalloc\bin\minject" @Args
1520
+ dir " $ExePath "
1521
+ }
1522
+ } else {
1523
+ throw " mimalloc is currently supported for X64 only"
1524
+ }
1525
+ }
1526
+
1468
1527
function Build-LLVM ([Platform ]$Platform , $Arch ) {
1469
1528
Build-CMakeProject `
1470
1529
- Src $SourceCache \llvm- project\llvm `
@@ -2457,6 +2516,7 @@ function Build-Installer($Arch) {
2457
2516
# when cross-compiling https://github.com/apple/swift/issues/71655
2458
2517
$INCLUDE_SWIFT_INSPECT = if ($IsCrossCompiling ) { " false" } else { " true" }
2459
2518
$INCLUDE_SWIFT_DOCC = if ($IsCrossCompiling ) { " false" } else { " true" }
2519
+ $ENABLE_MIMALLOC = if ($Allocator -eq " mimalloc" -and $Arch -eq $ArchX64 ) { " true" } else { " false" }
2460
2520
2461
2521
$Properties = @ {
2462
2522
BundleFlavor = " offline" ;
@@ -2465,6 +2525,7 @@ function Build-Installer($Arch) {
2465
2525
INCLUDE_SWIFT_INSPECT = $INCLUDE_SWIFT_INSPECT ;
2466
2526
SWIFT_INSPECT_BUILD = " $ ( $Arch.BinaryCache ) \swift-inspect\release" ;
2467
2527
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC ;
2528
+ ENABLE_MIMALLOC = $ENABLE_MIMALLOC ;
2468
2529
SWIFT_DOCC_BUILD = " $ ( $Arch.BinaryCache ) \swift-docc\release" ;
2469
2530
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = " ${SourceCache} \swift-docc-render-artifact" ;
2470
2531
}
@@ -2616,6 +2677,10 @@ if (-not $SkipBuild) {
2616
2677
2617
2678
Install-HostToolchain
2618
2679
2680
+ if (-not $SkipBuild -and $Allocator -eq " mimalloc" ) {
2681
+ Invoke-BuildStep Build-Mimalloc $HostArch
2682
+ }
2683
+
2619
2684
if (-not $SkipBuild -and -not $IsCrossCompiling ) {
2620
2685
Invoke-BuildStep Build-Inspect $HostArch
2621
2686
Invoke-BuildStep Build-DocC $HostArch
0 commit comments