Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions Compilers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "vbc-arm64", "src\Compilers\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.EndToEnd.UnitTests", "src\Compilers\CSharp\Test\EndToEnd\Microsoft.CodeAnalysis.CSharp.EndToEnd.UnitTests.csproj", "{F3D9264A-7CAE-4265-AF48-0C863301F51E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.CodeAnalysis.Experimental", "src\Compilers\Core\Experimental\Microsoft.CodeAnalysis.Experimental\Microsoft.CodeAnalysis.Experimental.csproj", "{62945D52-AB03-4403-97C3-EF39D64FDE77}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to add an image load to Razor scenarios and at some ponit Rsolyn ones too. We should pre-clear this with the VS perf team.

EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -416,6 +418,14 @@ Global
{F3D9264A-7CAE-4265-AF48-0C863301F51E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3D9264A-7CAE-4265-AF48-0C863301F51E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3D9264A-7CAE-4265-AF48-0C863301F51E}.Release|Any CPU.Build.0 = Release|Any CPU
{4CD05601-023D-4A9E-9E6B-D2416CD3B1A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4CD05601-023D-4A9E-9E6B-D2416CD3B1A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CD05601-023D-4A9E-9E6B-D2416CD3B1A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CD05601-023D-4A9E-9E6B-D2416CD3B1A2}.Release|Any CPU.Build.0 = Release|Any CPU
{62945D52-AB03-4403-97C3-EF39D64FDE77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62945D52-AB03-4403-97C3-EF39D64FDE77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62945D52-AB03-4403-97C3-EF39D64FDE77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62945D52-AB03-4403-97C3-EF39D64FDE77}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -491,6 +501,8 @@ Global
{810B02AD-2EA5-4422-88AC-B71B8AB0DF0B} = {C65C6143-BED3-46E6-869E-9F0BE6E84C37}
{48C93F90-8776-4847-96D8-127B896D6C80} = {C65C6143-BED3-46E6-869E-9F0BE6E84C37}
{F3D9264A-7CAE-4265-AF48-0C863301F51E} = {32A48625-F0AD-419D-828B-A50BDABA38EA}
{4CD05601-023D-4A9E-9E6B-D2416CD3B1A2} = {FD0FAF5F-1DED-485C-99FA-84B97F3A8EEC}
{62945D52-AB03-4403-97C3-EF39D64FDE77} = {A41D1B99-F489-4C43-BBDF-96D61B19A6B9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F599E08-A9EA-4FAA-897F-5D824B0210E6}
Expand Down
33 changes: 33 additions & 0 deletions src/Compilers/Core/Experimental/GeneratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.PooledObjects;

namespace Microsoft.CodeAnalysis.Experimental
{
public static partial class ExperimentalApis
{
public static void RegisterHostOutput<TSource>(ref this IncrementalGeneratorInitializationContext @this, IncrementalValuesProvider<TSource> source, Action<HostProductionContext, TSource, CancellationToken> action)
{
source.Node.RegisterOutput(new HostOutputNode<TSource>(source.Node, action));
}

public static ImmutableArray<(string, string)> GetHostOutputs(this GeneratorRunResult runResult) => runResult.HostOutputs;
}

public readonly struct HostProductionContext
{
internal readonly ArrayBuilder<(string, string)> Outputs;

internal HostProductionContext(ArrayBuilder<(string, string)> outputs)
{
Outputs = outputs;
}

public void AddOutput(string name, string value) => Outputs.Add((name, value));
}
}
96 changes: 96 additions & 0 deletions src/Compilers/Core/Experimental/HostOutputNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis.Experimental;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
using TOutput = System.Collections.Immutable.ImmutableArray<(string, string)>;

namespace Microsoft.CodeAnalysis
{
internal sealed class HostOutputNode<TInput> : IIncrementalGeneratorOutputNode, IIncrementalGeneratorNode<TOutput>
{
private readonly IIncrementalGeneratorNode<TInput> _source;

private readonly Action<HostProductionContext, TInput, CancellationToken> _action;

public HostOutputNode(IIncrementalGeneratorNode<TInput> source, Action<HostProductionContext, TInput, CancellationToken> action)
{
_source = source;
_action = action;
}

public IncrementalGeneratorOutputKind Kind => (IncrementalGeneratorOutputKind)0b100000; // several steps higher than IncrementalGeneratorOutputKind.Implementation

public NodeStateTable<TOutput> UpdateStateTable(DriverStateTable.Builder graphState, NodeStateTable<TOutput> previousTable, CancellationToken cancellationToken)
{
string stepName = "HostOutput";
var sourceTable = graphState.GetLatestStateTableForNode(_source);
if (sourceTable.IsCached)
{
if (graphState.DriverState.TrackIncrementalSteps)
{
return previousTable.CreateCachedTableWithUpdatedSteps(sourceTable, stepName, EqualityComparer<TOutput>.Default);
}
return previousTable;
}

var nodeTable = graphState.CreateTableBuilder(previousTable, stepName, EqualityComparer<TOutput>.Default);
foreach (var entry in sourceTable)
{
var inputs = nodeTable.TrackIncrementalSteps ? ImmutableArray.Create((entry.Step!, entry.OutputIndex)) : default;
if (entry.State == EntryState.Removed)
{
nodeTable.TryRemoveEntries(TimeSpan.Zero, inputs);
}
else if (entry.State != EntryState.Cached || !nodeTable.TryUseCachedEntries(TimeSpan.Zero, inputs))
{
ArrayBuilder<(string, string)> output = ArrayBuilder<(string, string)>.GetInstance();
HostProductionContext context = new HostProductionContext(output);
var stopwatch = SharedStopwatch.StartNew();
_action(context, entry.Item, cancellationToken);
nodeTable.AddEntry(output.ToImmutableAndFree(), EntryState.Added, stopwatch.Elapsed, inputs, EntryState.Added);
}
}

return nodeTable.ToImmutableAndFree();
}

public void AppendOutputs(IncrementalExecutionContext context, CancellationToken cancellationToken)
{
// get our own state table
Debug.Assert(context.TableBuilder is not null);
var table = context.TableBuilder.GetLatestStateTableForNode(this);

// add each non-removed entry to the context
foreach (var (list, state, _, _) in table)
{
if (state != EntryState.Removed)
{
context.HostOutputBuilder.AddRange(list);
}
}

if (context.GeneratorRunStateBuilder.RecordingExecutedSteps)
{
context.GeneratorRunStateBuilder.RecordStepsFromOutputNodeUpdate(table);
}
}


IIncrementalGeneratorNode<TOutput> IIncrementalGeneratorNode<TOutput>.WithComparer(IEqualityComparer<TOutput> comparer) => throw ExceptionUtilities.Unreachable;

public IIncrementalGeneratorNode<TOutput> WithTrackingName(string name) => throw ExceptionUtilities.Unreachable;

void IIncrementalGeneratorNode<TOutput>.RegisterOutput(IIncrementalGeneratorOutputNode output) => throw ExceptionUtilities.Unreachable;


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>

<IsPackable>true</IsPackable>
<PackageId>Microsoft.CodeAnalysis.Experimental</PackageId>
<PackageDescription>
This package provides access to experimental Microsoft .NET Compiler Platform ("Roslyn") APIs.
The APIs exposed in this package are not supported in any way, and may be changed or removed without notice.
</PackageDescription>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Portable\Microsoft.CodeAnalysis.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.VisualBasic" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Experimental"/>
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExpressionEvaluator.ExpressionCompiler" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.CSharp.ExpressionEvaluator.ExpressionCompiler" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator.ExpressionCompiler" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public GeneratorDriverRunResult GetRunResult()
generatedSources: getGeneratorSources(generatorState),
elapsedTime: generatorState.ElapsedTime,
namedSteps: generatorState.ExecutedSteps,
outputSteps: generatorState.OutputSteps));
outputSteps: generatorState.OutputSteps,
hostOutputs: generatorState.HostOutputs));
return new GeneratorDriverRunResult(results, _state.RunTime);

static ImmutableArray<GeneratedSourceResult> getGeneratorSources(GeneratorState generatorState)
Expand Down Expand Up @@ -293,10 +294,10 @@ internal GeneratorDriverState RunGeneratorsCore(Compilation compilation, Diagnos
{
// We do not support incremental step tracking for v1 generators, as the pipeline is implicitly defined.
var context = UpdateOutputs(generatorState.OutputNodes, IncrementalGeneratorOutputKind.Source | IncrementalGeneratorOutputKind.Implementation, new GeneratorRunStateTable.Builder(state.TrackIncrementalSteps), cancellationToken, driverStateBuilder);
(var sources, var generatorDiagnostics, var generatorRunStateTable) = context.ToImmutableAndFree();
(var sources, var generatorDiagnostics, var generatorRunStateTable, var hostOutputs) = context.ToImmutableAndFree();
generatorDiagnostics = FilterDiagnostics(compilation, generatorDiagnostics, driverDiagnostics: diagnosticsBag, cancellationToken);

stateBuilder[i] = generatorState.WithResults(ParseAdditionalSources(state.Generators[i], sources, cancellationToken), generatorDiagnostics, generatorRunStateTable.ExecutedSteps, generatorRunStateTable.OutputSteps, generatorTimer.Elapsed);
stateBuilder[i] = generatorState.WithResults(ParseAdditionalSources(state.Generators[i], sources, cancellationToken), generatorDiagnostics, generatorRunStateTable.ExecutedSteps, generatorRunStateTable.OutputSteps, hostOutputs, generatorTimer.Elapsed);
}
catch (UserFunctionException ufe)
{
Expand Down
31 changes: 29 additions & 2 deletions src/Compilers/Core/Portable/SourceGeneration/GeneratorState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics;

namespace Microsoft.CodeAnalysis
{
/// <summary>
Expand All @@ -20,18 +21,38 @@ internal readonly struct GeneratorState
ImmutableArray<Diagnostic>.Empty,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableArray<(string, string)>.Empty,
exception: null,
elapsedTime: TimeSpan.Zero);

/// <summary>
/// Creates a new generator state that contains information, constant trees and an execution pipeline
/// </summary>
public GeneratorState(ImmutableArray<GeneratedSyntaxTree> postInitTrees, ImmutableArray<SyntaxInputNode> inputNodes, ImmutableArray<IIncrementalGeneratorOutputNode> outputNodes)
: this(postInitTrees, inputNodes, outputNodes, ImmutableArray<GeneratedSyntaxTree>.Empty, ImmutableArray<Diagnostic>.Empty, ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty, ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty, exception: null, elapsedTime: TimeSpan.Zero)
: this(postInitTrees,
inputNodes,
outputNodes,
ImmutableArray<GeneratedSyntaxTree>.Empty,
ImmutableArray<Diagnostic>.Empty,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableArray<(string, string)>.Empty,
exception: null,
elapsedTime: TimeSpan.Zero)
{
}

private GeneratorState(ImmutableArray<GeneratedSyntaxTree> postInitTrees, ImmutableArray<SyntaxInputNode> inputNodes, ImmutableArray<IIncrementalGeneratorOutputNode> outputNodes, ImmutableArray<GeneratedSyntaxTree> generatedTrees, ImmutableArray<Diagnostic> diagnostics, ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> executedSteps, ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> outputSteps, Exception? exception, TimeSpan elapsedTime)
private GeneratorState(
ImmutableArray<GeneratedSyntaxTree> postInitTrees,
ImmutableArray<SyntaxInputNode> inputNodes,
ImmutableArray<IIncrementalGeneratorOutputNode> outputNodes,
ImmutableArray<GeneratedSyntaxTree> generatedTrees,
ImmutableArray<Diagnostic> diagnostics,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> executedSteps,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> outputSteps,
ImmutableArray<(string, string)> hostOutputs,
Exception? exception,
TimeSpan elapsedTime)
{
this.Initialized = true;
this.PostInitTrees = postInitTrees;
Expand All @@ -41,6 +62,7 @@ private GeneratorState(ImmutableArray<GeneratedSyntaxTree> postInitTrees, Immuta
this.Diagnostics = diagnostics;
this.ExecutedSteps = executedSteps;
this.OutputSteps = outputSteps;
this.HostOutputs = hostOutputs;
this.Exception = exception;
this.ElapsedTime = elapsedTime;
}
Expand All @@ -49,6 +71,7 @@ public GeneratorState WithResults(ImmutableArray<GeneratedSyntaxTree> generatedT
ImmutableArray<Diagnostic> diagnostics,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> executedSteps,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> outputSteps,
ImmutableArray<(string, string)> hostOutputs,
TimeSpan elapsedTime)
{
return new GeneratorState(this.PostInitTrees,
Expand All @@ -58,6 +81,7 @@ public GeneratorState WithResults(ImmutableArray<GeneratedSyntaxTree> generatedT
diagnostics,
executedSteps,
outputSteps,
hostOutputs,
exception: null,
elapsedTime);
}
Expand All @@ -71,6 +95,7 @@ public GeneratorState WithError(Exception exception, Diagnostic error, TimeSpan
ImmutableArray.Create(error),
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>>.Empty,
ImmutableArray<(string, string)>.Empty,
exception,
elapsedTime);
}
Expand All @@ -94,5 +119,7 @@ public GeneratorState WithError(Exception exception, Diagnostic error, TimeSpan
internal ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> ExecutedSteps { get; }

internal ImmutableDictionary<string, ImmutableArray<IncrementalGeneratorRunStep>> OutputSteps { get; }

internal ImmutableArray<(string, string)> HostOutputs { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -166,16 +167,19 @@ internal readonly struct IncrementalExecutionContext

internal readonly GeneratorRunStateTable.Builder GeneratorRunStateBuilder;

internal readonly ArrayBuilder<(string, string)> HostOutputBuilder;

public IncrementalExecutionContext(DriverStateTable.Builder? tableBuilder, GeneratorRunStateTable.Builder generatorRunStateBuilder, AdditionalSourcesCollection sources)
{
TableBuilder = tableBuilder;
GeneratorRunStateBuilder = generatorRunStateBuilder;
Sources = sources;
HostOutputBuilder = ArrayBuilder<(string, string)>.GetInstance();
Diagnostics = DiagnosticBag.GetInstance();
}

internal (ImmutableArray<GeneratedSourceText> sources, ImmutableArray<Diagnostic> diagnostics, GeneratorRunStateTable executedSteps) ToImmutableAndFree()
=> (Sources.ToImmutableAndFree(), Diagnostics.ToReadOnlyAndFree(), GeneratorRunStateBuilder.ToImmutableAndFree());
internal (ImmutableArray<GeneratedSourceText> sources, ImmutableArray<Diagnostic> diagnostics, GeneratorRunStateTable executedSteps, ImmutableArray<(string, string)> hostOutputs) ToImmutableAndFree()
=> (Sources.ToImmutableAndFree(), Diagnostics.ToReadOnlyAndFree(), GeneratorRunStateBuilder.ToImmutableAndFree(), HostOutputBuilder.ToImmutableAndFree());

internal void Free()
{
Expand Down
Loading