Skip to content

Reland "utils: remove option to control the allocator" #79151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions utils/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ in batch file format instead of executing them.
.PARAMETER HostArchName
The architecture where the toolchain will execute.

.PARAMETER Allocator
The memory allocator used in the toolchain binaries, if it's
`mimalloc`, it uses mimalloc. Otherwise, it uses the default
allocator.

.EXAMPLE
PS> .\Build.ps1

Expand Down Expand Up @@ -143,7 +138,6 @@ param(
[switch] $DebugInfo,
[switch] $EnableCaching,
[string] $Cache = "",
[string] $Allocator = "",
[switch] $Summary,
[switch] $ToBatch
)
Expand Down Expand Up @@ -1692,44 +1686,56 @@ function Build-Compilers() {
}

# Reference: https://github.com/microsoft/mimalloc/tree/dev/bin#minject
function Build-Mimalloc() {
function Build-mimalloc() {
[CmdletBinding(PositionalBinding = $false)]
param
(
[Parameter(Position = 0, Mandatory = $true)]
[hashtable]$Arch
)

$MSBuildArgs = @("$SourceCache\mimalloc\ide\vs2022\mimalloc.sln")
# TODO: migrate to the CMake build
$MSBuildArgs = @()
$MSBuildArgs += "-noLogo"
$MSBuildArgs += "-maxCpuCount"
$MSBuildArgs += "-p:Configuration=Release"
$MSBuildArgs += "-p:Platform=$($Arch.ShortName)"

$Properties = @{}
TryAdd-KeyValue $Properties Configuration Release
TryAdd-KeyValue $Properties OutDir "$($Arch.BinaryCache)\mimalloc\bin\"
TryAdd-KeyValue $Properties Platform "$($Arch.ShortName)"

Isolate-EnvVars {
Invoke-VsDevShell $Arch
# Avoid hard-coding the VC tools version number
$VCRedistDir = (Get-ChildItem "${env:VCToolsRedistDir}\$($HostArch.ShortName)" -Filter "Microsoft.VC*.CRT").FullName
if ($VCRedistDir) {
$MSBuildArgs += "-p:VCRedistDir=$VCRedistDir\"
TryAdd-KeyValue $Properties VCRedistDir "$VCRedistDir\"
}
}

Invoke-Program $msbuild @MSBuildArgs
foreach ($Property in $Properties.GetEnumerator()) {
if ($Property.Value.Contains(" ")) {
$MSBuildArgs += "-p:$($Property.Key)=$($Property.Value.Replace('\', '\\'))"
} else {
$MSBuildArgs += "-p:$($Property.Key)=$($Property.Value)"
}
}

Invoke-Program $msbuild "$SourceCache\mimalloc\ide\vs2022\mimalloc-lib.vcxproj" @MSBuildArgs "-p:IntDir=$($Arch.BinaryCache)\mimalloc\mimalloc\"
Invoke-Program $msbuild "$SourceCache\mimalloc\ide\vs2022\mimalloc-override-dll.vcxproj" @MSBuildArgs "-p:IntDir=$($Arch.BinaryCache)\mimalloc\mimalloc-override-dll\"

$HostSuffix = if ($Arch -eq $ArchX64) { "" } else { "-arm64" }
$BuildSuffix = if ($BuildArch -eq $ArchX64) { "" } else { "-arm64" }
$Products = @( "mimalloc.dll" )
foreach ($Product in $Products) {
Copy-Item -Path "$SourceCache\mimalloc\out\msvc-$($Arch.ShortName)\Release\$Product" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin"
}
Copy-Item -Path "$SourceCache\mimalloc\out\msvc-$($Arch.ShortName)\Release\mimalloc-redirect$HostSuffix.dll" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin"

Copy-Item -Path "$($Arch.BinaryCache)\mimalloc\bin\mimalloc.dll" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin\"
Copy-Item -Path "$($Arch.BinaryCache)\mimalloc\bin\mimalloc-redirect$HostSuffix.dll" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin"
# When cross-compiling, bundle the second mimalloc redirect dll as a workaround for
# https://github.com/microsoft/mimalloc/issues/997
if ($IsCrossCompiling) {
Copy-Item -Path "$SourceCache\mimalloc\out\msvc-$($Arch.ShortName)\Release\mimalloc-redirect$HostSuffix.dll" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin\mimalloc-redirect$BuildSuffix.dll"
Copy-Item -Path "$($Arch.BinaryCache)\mimalloc\bin\mimalloc-redirect$HostSuffix.dll" -Destination "$($Arch.ToolchainInstallRoot)\usr\bin\mimalloc-redirect$BuildSuffix.dll"
}

# TODO: should we split this out into its own function?
$Tools = @(
"swift.exe",
"swiftc.exe",
Expand All @@ -1746,9 +1752,9 @@ function Build-Mimalloc() {
foreach ($Tool in $Tools) {
$Binary = [IO.Path]::Combine("$($Arch.ToolchainInstallRoot)\usr\bin", $Tool)
# Binary-patch in place
Invoke-Program "$SourceCache\mimalloc\bin\minject$BuildSuffix" "-f" "-i" "-v" "$Binary"
Start-Process -Wait -WindowStyle Hidden -FilePath "$SourceCache\mimalloc\bin\minject$BuildSuffix" -ArgumentList @("-f", "-i", "-v", "$Binary")
# Log the import table
Invoke-Program "$SourceCache\mimalloc\bin\minject$BuildSuffix" "-l" "$Binary"
Start-Process -Wait -WindowStyle Hidden -FilePath "$SourceCache\mimalloc\bin\minject$BuildSuffix" -ArgumentList @("-l", "$Binary")
}
}

Expand Down Expand Up @@ -2995,16 +3001,13 @@ function Build-Installer($Arch) {
# TODO(hjyamauchi) Re-enable the swift-inspect and swift-docc builds
# when cross-compiling https://github.com/apple/swift/issues/71655
$INCLUDE_SWIFT_DOCC = if ($IsCrossCompiling) { "false" } else { "true" }
$ENABLE_MIMALLOC = if ($Allocator -eq "mimalloc") { "true" } else { "false" }
# When cross-compiling, bundle the second mimalloc redirect dll as a workaround for
# https://github.com/microsoft/mimalloc/issues/997
$WORKAROUND_MIMALLOC_ISSUE_997 = if ($IsCrossCompiling) { "true" } else { "false" }

$Properties = @{
BundleFlavor = "offline";
TOOLCHAIN_ROOT = "$($Arch.ToolchainInstallRoot)\";
ENABLE_MIMALLOC = $ENABLE_MIMALLOC;
WORKAROUND_MIMALLOC_ISSUE_997 = $WORKAROUND_MIMALLOC_ISSUE_997;
# When cross-compiling, bundle the second mimalloc redirect dll as a workaround for
# https://github.com/microsoft/mimalloc/issues/997
WORKAROUND_MIMALLOC_ISSUE_997 = if ($IsCrossCompiling) { "true" } else { "false" };
INCLUDE_SWIFT_DOCC = $INCLUDE_SWIFT_DOCC;
SWIFT_DOCC_BUILD = "$($Arch.BinaryCache)\swift-docc\release";
SWIFT_DOCC_RENDER_ARTIFACT_ROOT = "${SourceCache}\swift-docc-render-artifact";
Expand Down Expand Up @@ -3178,8 +3181,8 @@ if (-not $SkipBuild) {

Install-HostToolchain

if (-not $SkipBuild -and $Allocator -eq "mimalloc") {
Invoke-BuildStep Build-Mimalloc $HostArch
if (-not $SkipBuild) {
Invoke-BuildStep Build-mimalloc $HostArch
}

if (-not $SkipBuild -and -not $IsCrossCompiling) {
Expand Down