Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 7 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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.Experimental", "src\Compilers\Core\Experimental\Microsoft.CodeAnalysis.Experimental.csproj", "{C5968646-6AFF-4414-928C-2524CC43FABE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -416,6 +418,10 @@ 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
{C5968646-6AFF-4414-928C-2524CC43FABE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5968646-6AFF-4414-928C-2524CC43FABE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5968646-6AFF-4414-928C-2524CC43FABE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5968646-6AFF-4414-928C-2524CC43FABE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -491,6 +497,7 @@ 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}
{C5968646-6AFF-4414-928C-2524CC43FABE} = {A41D1B99-F489-4C43-BBDF-96D61B19A6B9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6F599E08-A9EA-4FAA-897F-5D824B0210E6}
Expand Down
7 changes: 7 additions & 0 deletions Roslyn.sln
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Net.Compilers.Too
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.CSharp.EndToEnd.UnitTests", "src\Compilers\CSharp\Test\EndToEnd\Microsoft.CodeAnalysis.CSharp.EndToEnd.UnitTests.csproj", "{C247414A-8946-4BAB-BE1F-C82B90C63EF6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.CodeAnalysis.Experimental", "src\Compilers\Core\Experimental\Microsoft.CodeAnalysis.Experimental.csproj", "{FE1E0430-D5F0-47CE-8AE1-C41631F72C12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1241,6 +1243,10 @@ Global
{C247414A-8946-4BAB-BE1F-C82B90C63EF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C247414A-8946-4BAB-BE1F-C82B90C63EF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C247414A-8946-4BAB-BE1F-C82B90C63EF6}.Release|Any CPU.Build.0 = Release|Any CPU
{FE1E0430-D5F0-47CE-8AE1-C41631F72C12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE1E0430-D5F0-47CE-8AE1-C41631F72C12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE1E0430-D5F0-47CE-8AE1-C41631F72C12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE1E0430-D5F0-47CE-8AE1-C41631F72C12}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1473,6 +1479,7 @@ Global
{6131713D-DFB4-49B5-8010-50071FED3E85} = {C52D8057-43AF-40E6-A01B-6CDBB7301985}
{A9A8ADE5-F123-4109-9FA4-4B92F1657043} = {C52D8057-43AF-40E6-A01B-6CDBB7301985}
{C247414A-8946-4BAB-BE1F-C82B90C63EF6} = {32A48625-F0AD-419D-828B-A50BDABA38EA}
{FE1E0430-D5F0-47CE-8AE1-C41631F72C12} = {A41D1B99-F489-4C43-BBDF-96D61B19A6B9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {604E6B91-7BC0-4126-AE07-D4D2FEFC3D29}
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 Key, string Value)> 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>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>

<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 Key, string Value)> 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 Key, string Value)> 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 Key, string Value)> HostOutputs { get; }
}
}
Loading