@@ -102,6 +102,11 @@ in batch file format instead of executing them.
102102. PARAMETER HostArchName
103103The architecture where the toolchain will execute.
104104
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+
105110. EXAMPLE
106111PS> .\Build.ps1
107112
@@ -138,6 +143,7 @@ param(
138143 [switch ] $DebugInfo ,
139144 [switch ] $EnableCaching ,
140145 [string ] $Cache = " " ,
146+ [string ] $Allocator = " " ,
141147 [switch ] $Summary ,
142148 [switch ] $ToBatch
143149)
@@ -1465,6 +1471,59 @@ function Build-Compilers() {
14651471 }
14661472}
14671473
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+
14681527function Build-LLVM ([Platform ]$Platform , $Arch ) {
14691528 Build-CMakeProject `
14701529 - Src $SourceCache \llvm- project\llvm `
@@ -2461,6 +2520,7 @@ function Build-Installer($Arch) {
24612520 # when cross-compiling https://github.com/apple/swift/issues/71655
24622521 $INCLUDE_SWIFT_INSPECT = if ($IsCrossCompiling ) { " false" } else { " true" }
24632522 $INCLUDE_SWIFT_DOCC = if ($IsCrossCompiling ) { " false" } else { " true" }
2523+ $ENABLE_MIMALLOC = if ($Allocator -eq " mimalloc" -and $Arch -eq $ArchX64 ) { " true" } else { " false" }
24642524
24652525 $Properties = @ {
24662526 BundleFlavor = " offline" ;
@@ -2469,6 +2529,7 @@ function Build-Installer($Arch) {
24692529 INCLUDE_SWIFT_INSPECT = $INCLUDE_SWIFT_INSPECT ;
24702530 SWIFT_INSPECT_BUILD = " $ ( $Arch.BinaryCache ) \swift-inspect\release" ;
24712531 INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC ;
2532+ ENABLE_MIMALLOC = $ENABLE_MIMALLOC ;
24722533 SWIFT_DOCC_BUILD = " $ ( $Arch.BinaryCache ) \swift-docc\release" ;
24732534 SWIFT_DOCC_RENDER_ARTIFACT_ROOT = " ${SourceCache} \swift-docc-render-artifact" ;
24742535 }
@@ -2620,6 +2681,10 @@ if (-not $SkipBuild) {
26202681
26212682Install-HostToolchain
26222683
2684+ if (-not $SkipBuild -and $Allocator -eq " mimalloc" ) {
2685+ Invoke-BuildStep Build-Mimalloc $HostArch
2686+ }
2687+
26232688if (-not $SkipBuild -and -not $IsCrossCompiling ) {
26242689 Invoke-BuildStep Build-Inspect $HostArch
26252690 Invoke-BuildStep Build-DocC $HostArch
0 commit comments