IBC: Disable `-partialNGEN -minify` for SPC.dll (dotnet/coreclr#10950)
authorDaniel Podder <dapodd@microsoft.com>
Wed, 19 Apr 2017 18:59:58 +0000 (13:59 -0500)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2017 18:59:58 +0000 (13:59 -0500)
Add some fixes around the use of IBCMerge in CoreCLR, along with a BuildTools patch (which the fixes are dependent upon):
* don't restore the default optdata package through BuildTools
* don't use partial ngen by default in CoreCLR for SPC.dll

Commit migrated from https://github.com/dotnet/coreclr/commit/c6506d7ed204aa2e42cfb073c56d4895aba0eeb2

src/coreclr/Tools-Override/codeOptimization.targets [new file with mode: 0644]
src/coreclr/config.json

diff --git a/src/coreclr/Tools-Override/codeOptimization.targets b/src/coreclr/Tools-Override/codeOptimization.targets
new file mode 100644 (file)
index 0000000..4db340f
--- /dev/null
@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <UsingTask TaskName="ZipFileExtractToDirectory" AssemblyFile="$(BuildToolsTaskDir)Microsoft.DotNet.Build.Tasks.dll"/>
+
+  <PropertyGroup>
+    <OptimizationDataVersion Condition="'$(OptimizationDataVersion)'==''">2.0.0-rc-61101-16</OptimizationDataVersion>
+    <OptimizationDataPackageName Condition="'$(OptimizationDataPackageName)'==''">RoslynDependencies.OptimizationData</OptimizationDataPackageName>
+    <OptimizationDataDir Condition="'$(OptimizationDataDir)'==''">$(ToolsDir)OptimizationData/</OptimizationDataDir>
+    <RestoreDefaultOptimizationDataPackage Condition="'$(RestoreDefaultOptimizationDataPackage)'==''">true</RestoreDefaultOptimizationDataPackage>
+    <UsePartialNGENOptimization Condition="'$(UsePartialNGENOptimization)'==''">true</UsePartialNGENOptimization>
+  </PropertyGroup>
+
+  <!-- If IBC data hasn't been merged with the IL yet, preprocess it first -->
+  <Target Name="PreProcessIBCData"
+          BeforeTargets="OptimizeWithTrainingData"
+          DependsOnTargets="ResolveOptionalTools"
+          Condition="'$(OS)'=='Windows_NT' and '$(EnableProfileGuidedOptimization)'=='true' and Exists('$(OptimizationDataDir)$(AssemblyName).dll')">
+
+    <!-- Find IBCMerge as a resolved optional tool. -->
+    <PropertyGroup>
+      <IBCMergeToolPath Condition="'%(Filename)%(Extension)'=='ibcmerge.exe'">@(ResolvedOptionalToolReferences)</IBCMergeToolPath>
+    </PropertyGroup>
+
+    <!-- Enumerate the various files that need merging -->
+    <PropertyGroup>
+      <InputAssemblyFile>$(OptimizationDataDir)$(AssemblyName).dll</InputAssemblyFile>
+      <TargetOptimizationDataFile>$(OptimizationDataDir)$(AssemblyName).pgo</TargetOptimizationDataFile>
+    </PropertyGroup>
+    <ItemGroup>
+      <RawOptimizationDataFiles Include="$(OptimizationDataDir)$(AssemblyName)*.ibc" />
+    </ItemGroup>
+
+    <!-- Merge the optimization data into the source DLL -->
+    <Exec Command="$(IBCMergeToolPath) -q -f -delete -mo $(InputAssemblyFile) @(RawOptimizationDataFiles, ' ')" />
+
+    <!-- Verify that the optimization data has been merged -->
+    <Exec Command="$(IBCMergeToolPath) -mi $(InputAssemblyFile)" />
+
+    <!-- Save the module as *.pgo to match the convention expected in target OptimizeWithTrainingData -->
+    <Copy SourceFiles="$(InputAssemblyFile)" DestinationFiles="$(TargetOptimizationDataFile)" />
+  </Target>
+
+  <!-- We should only run this target on Windows and only if EnableProfileGuidedOptimization is set and we have training data -->
+  <Target Name="OptimizeWithTrainingData"
+          AfterTargets="AfterBuild"
+          BeforeTargets="CopyFilesToOutputDirectory"
+          DependsOnTargets="ResolveOptionalTools"
+          Condition="'$(OS)'=='Windows_NT' and '$(EnableProfileGuidedOptimization)'=='true' and Exists('$(OptimizationDataDir)$(AssemblyName).pgo')">
+
+    <!-- Find IBCMerge as a resolved optional tool. -->
+    <PropertyGroup>
+      <IBCMergeToolPath Condition="'%(Filename)%(Extension)'=='ibcmerge.exe'">@(ResolvedOptionalToolReferences)</IBCMergeToolPath>
+    </PropertyGroup>
+
+    <PropertyGroup>
+      <OptimizedAssemblyDir>$(IntermediateOutputPath)/OptimizedAssembly</OptimizedAssemblyDir>
+      <OptimizedAssemblyFile>$(OptimizedAssemblyDir)/$(AssemblyName).dll</OptimizedAssemblyFile>
+    </PropertyGroup>
+
+    <!-- Customize IBCMerge's arguments depending on input props -->
+    <PropertyGroup>
+      <IBCMergeArguments>-q -f -mo $(OptimizedAssemblyFile) -incremental $(OptimizationDataDir)$(AssemblyName).pgo</IBCMergeArguments>
+      <IBCMergeArguments Condition="$(UsePartialNGENOptimization)">$(IBCMergeArguments) -partialNGEN -minify</IBCMergeArguments>
+    </PropertyGroup>
+
+    <!-- Copy the compiled assembly into a folder for further processing -->
+    <MakeDir Directories="$(OptimizedAssemblyDir)" />
+    <Copy SourceFiles="@(IntermediateAssembly)" DestinationFolder="$(OptimizedAssemblyDir)" />
+
+    <!-- Apply optimization data to the compiled assembly -->
+    <Exec Command="$(IBCMergeToolPath) $(IBCMergeArguments)" />
+
+    <!-- Verify that the optimization data has been applied -->
+    <Exec Command="$(IBCMergeToolPath) -mi $(OptimizedAssemblyFile)" />
+
+    <!-- We need to make sure that the assembly that gets packaged is the one with the optimization data -->
+    <ItemGroup>
+      <IntermediateAssembly Remove="@(IntermediateAssembly)"/>
+      <IntermediateAssembly Include="$(OptimizedAssemblyDir)/$(AssemblyName).dll"/>
+    </ItemGroup>
+  </Target>
+
+  <!-- We need the OptimizationData package in order to be able to optimize the assembly -->
+  <Target Name="RestoreOptimizationDataPackage"
+          BeforeTargets="Sync"
+          Condition="'$(EnableProfileGuidedOptimization)'=='true' and '$(RestoreDefaultOptimizationDataPackage)'=='true' and !Exists('$(OptimizationDataDir)project.json')">
+
+    <!-- Dynamically create a project.json file used to restore the optimization data-->
+    <Message Text="Generating project.json for optimization data"  Importance="low" />
+    <ItemGroup>
+      <_OptimizationDataJsonLine Include="{&quot;dependencies&quot;: {" />
+      <_OptimizationDataJsonLine Include="&quot;$(OptimizationDataPackageName)&quot; : &quot;$(OptimizationDataVersion)&quot; " />
+      <_OptimizationDataJsonLine Include="},&quot;frameworks&quot;: {&quot;netcoreapp1.0&quot;: {},&quot;net46&quot;: {}}}"/>
+    </ItemGroup>
+    
+    <PropertyGroup>
+      <OptimizationDataProjectJson>$(OptimizationDataDir)project.json</OptimizationDataProjectJson>
+      <OptimizationDataNuGetFeed Condition="'$(OptimizationDataNuGetFeed)'==''">https:%2F%2Fdotnet.myget.org/F/roslyn/api/v3/index.json</OptimizationDataNuGetFeed>
+    </PropertyGroup>
+
+    <MakeDir Directories="$(OptimizationDataDir)" ContinueOnError="true" />
+    <WriteLinesToFile File="$(OptimizationDataProjectJson)" Lines="@(_OptimizationDataJsonLine)" Overwrite="true" />
+
+    <!-- Restore the OptimizationData package -->
+    <Exec Command="$(DnuRestoreCommand) $(OptimizationDataProjectJson) --source $(OptimizationDataNuGetFeed)"
+          StandardOutputImportance="Low"/>
+
+    <!-- Copy the restored files into a more accessible location -->
+    <ItemGroup>
+      <_OptimizationDataFiles Include="$(PackagesDir)/$(OptimizationDataPackageName)/$(OptimizationDataVersion)/content/OptimizationData/*.pgo" />
+    </ItemGroup>
+
+    <Copy SourceFiles="@(_OptimizationDataFiles)"
+          DestinationFiles="@(_OptimizationDataFiles->'$(OptimizationDataDir)%(Filename)%(Extension)')"
+          SkipUnchangedFiles="true"
+          ContinueOnError="true"/>
+  </Target>
+</Project>
\ No newline at end of file
index 158c1b9..c975171 100644 (file)
       "values": [ true, false ],
       "defaultValue": false
     },
+    "RestoreDefaultOptimizationDataPackage": {
+      "description": "Instructs BuildTools to restore the default OptimizationData package.",
+      "valueType": "property",
+      "values": [ true, false ],
+      "defaultValue": true
+    },
+    "UsePartialNGENOptimization": {
+      "description": "Adds the `-partialNGEN -minify` arguments when invoking IBCMerge.",
+      "valueType": "property",
+      "values": [ true, false ],
+      "defaultValue": true
+    },
     "OptimizationDataDir": {
       "description": "Sets the path where the build should look for IBC profile data.",
       "valueType": "property",
           "MsBuildLog": "default",
           "MsBuildWrn": "default",
           "MsBuildErr": "default",
-          "MsBuildEventLogging": "default"
+          "MsBuildEventLogging": "default",
+          "RestoreDefaultOptimizationDataPackage": "false",
+          "UsePartialNGENOptimization": "false"
         }
       }
     },