CodeGenCapturing.cs 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Roslyn.Test.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using System.Collections;
using System.Collections.Immutable;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
    public class CodeGenCapturing : CSharpTestBase
    {
        private class CaptureContext
        {
            // Stores a mapping from scope index (0-based count of scopes
            // from `this` to most nested scope)
            public readonly List<IList<string>> VariablesByScope = new List<IList<string>>();

            private CaptureContext() { }

            public CaptureContext(int MaxVariables)
            {
                // Fields are shared among methods, so we also share them in the
                // capture context when cloning
                var fieldsBuilder = ImmutableArray.CreateBuilder<string>(MaxVariables);
                for (int i = 0; i < MaxVariables; i++)
                {
                    fieldsBuilder.Add($"field_{i}");
                }
                VariablesByScope.Add(fieldsBuilder.MoveToImmutable());
            }

            public void Add(int depth, string varName)
            {
                if (VariablesByScope.Count <= depth ||
                    VariablesByScope[depth] == null)
                {
                    VariablesByScope.Insert(depth, new List<string>() { varName });
                }
                else
                {
                    VariablesByScope[depth].Add(varName);
                }
            }

            public CaptureContext Clone()
            {
                var fields = VariablesByScope[0];
                var newCtx = new CaptureContext();
                newCtx.VariablesByScope.Add(fields);
                newCtx.VariablesByScope.AddRange(
                    this.VariablesByScope
                    .Skip(1)
                    .Select(list => list == null ? null : new List<string>(list)));
                newCtx.CaptureNameIndex = this.CaptureNameIndex;
                return newCtx;
            }

            public int CaptureNameIndex = 0;
        }

        private static string MakeLocalFunc(int nameIndex, string captureExpression)
            => $@"int Local_{nameIndex}() => {captureExpression};";

        private static string MakeCaptureExpression(IList<int> varsToCapture, CaptureContext ctx)
        {
            var varNames = new List<string>();
            for (int varDepth = 0; varDepth < varsToCapture.Count; varDepth++)
            {
                var variablesByScope = ctx.VariablesByScope;
                // Do we have any variables in this scope depth?
                // If not, initialize an empty list
                if (variablesByScope.Count <= varDepth)
                {
                    variablesByScope.Add(new List<string>());
                }

                var varsAtCurrentDepth = variablesByScope[varDepth];

                int numToCapture = varsToCapture[varDepth];
                int numVarsAvailable = variablesByScope[varDepth].Count;
                // If we have enough variables to capture in the context
                // just add them
                if (numVarsAvailable >= numToCapture)
                {
                    // Capture the last variables added since if there are more
                    // vars in the context than the max vars to capture we'll never
                    // have code coverage of the newest vars added to the context.
                    varNames.AddRange(varsAtCurrentDepth
                        .Skip(numVarsAvailable - numToCapture)
                        .Take(numToCapture));
                }
                else
                {
                    // Not enough variables in the context -- add more
                    for (int i = 0; i < numToCapture - numVarsAvailable; i++)
                    {
                        varsAtCurrentDepth.Add($"captureVar_{ctx.CaptureNameIndex++}");
                    }

                    varNames.AddRange(varsAtCurrentDepth);
                }
            }

            return varNames.Count == 0 ? "0" : string.Join(" + ", varNames);
        }

        /// <summary>
        /// Generates all combinations of distributing a sum to a list of subsets.
        /// This is equivalent to the "stars and bars" combinatorics construction.
        /// </summary>
        private static IEnumerable<IList<int>> GenerateAllSetCombinations(int sum, int numSubsets)
        {
            Assert.True(numSubsets > 0);
            return GenerateAll(sum, 0, ImmutableList<int>.Empty);

            IEnumerable<ImmutableList<int>> GenerateAll(
                int remainingSum,
                int setIndex, // 0-based index of subset we're generating
                ImmutableList<int> setsSoFar)
            {
                for (int i = 0; i <= remainingSum; i++)
                {
                    var newSets = setsSoFar.Add(i);
                    if (setIndex == numSubsets - 1)
                    {
                        yield return newSets;
                    }
                    else
                    {
                        foreach (var captures in GenerateAll(remainingSum - i,
                                                             setIndex + 1,
                                                             newSets))
                        {
                            yield return captures;
                        }
                    }
                }
            }
        }

        [Fact]
        public void GenerateAllTest()
        {
            Assert.Equal(new[]
            {
                ImmutableList<int>.Empty.Add(0),
                ImmutableList<int>.Empty.Add(1),
                ImmutableList<int>.Empty.Add(2),
                ImmutableList<int>.Empty.Add(3)
            }, GenerateAllSetCombinations(3, 1));
            Assert.Equal(new[]
            {
                ImmutableList<int>.Empty.Add(0).Add(0),
                ImmutableList<int>.Empty.Add(0).Add(1),
                ImmutableList<int>.Empty.Add(0).Add(2),
                ImmutableList<int>.Empty.Add(0).Add(3),
                ImmutableList<int>.Empty.Add(1).Add(0),
                ImmutableList<int>.Empty.Add(1).Add(1),
                ImmutableList<int>.Empty.Add(1).Add(2),
                ImmutableList<int>.Empty.Add(2).Add(0),
                ImmutableList<int>.Empty.Add(2).Add(1),
                ImmutableList<int>.Empty.Add(3).Add(0)
            }, GenerateAllSetCombinations(3, 2));
        }

        [Fact]
        public void ExpressionGeneratorTest01()
        {
            var ctx = new CaptureContext(1);
            int[] captures = { 1 }; // Capture 1 var at the 0 depth
            var expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_0", expr);
            VerifyContext(new[]
            {
                new[] { "field_0"}
            }, ctx.VariablesByScope);

            ctx = new CaptureContext(3);
            captures = new[] { 3 }; // Capture 3 vars at 0 depth
            expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_0 + field_1 + field_2", expr);
            VerifyContext(new[]
            {
                new[] { "field_0", "field_1", "field_2" }
            }, ctx.VariablesByScope);

            ctx = new CaptureContext(3);
            captures = new[] { 1, 1, 1 }; // Capture 1 var at each of 3 depths
            expr = MakeCaptureExpression(captures, ctx);
            Assert.Equal("field_2 + captureVar_0 + captureVar_1", expr);
            VerifyContext(new[]
            {
                new[] { "field_0", "field_1", "field_2"},
                new[] { "captureVar_0"},
                new[] { "captureVar_1"}
            }, ctx.VariablesByScope);

            void VerifyContext(IList<IEnumerable<string>> expectedCtx, List<IList<string>> actualCtx)
            {
                Assert.Equal(expectedCtx.Count, ctx.VariablesByScope.Count);
                for (int depth = 0; depth < expectedCtx.Count; depth++)
                {
                    AssertEx.Equal(expectedCtx[depth], ctx.VariablesByScope[depth]);
                }
            }
        }
        private struct LayoutEnumerator : IEnumerator<(int depth, int localFuncIndex)>
        {
            private readonly IList<int> _layout;
            private (int depth, int localFuncIndex) _current;

            public LayoutEnumerator(IList<int> layout)
            {
                _layout = layout;
                _current = (-1, -1);
            }

            public (int depth, int localFuncIndex) Current => _current;

            object IEnumerator.Current => throw new NotImplementedException();

            public void Dispose() => throw new NotImplementedException();

            public bool MoveNext()
            {
                if (_current.depth < 0)
                {
                    return FindNonEmptyDepth(0, _layout, out _current);
                }
                else
                {
                    int newIndex = _current.localFuncIndex + 1;
                    if (newIndex == _layout[_current.depth])
                    {
                        return FindNonEmptyDepth(_current.depth + 1, _layout, out _current);
                    }

                    _current = (_current.depth, newIndex);
                    return true;
                }

                bool FindNonEmptyDepth(int startingDepth, IList<int> layout, out (int depth, int localFuncIndex) newCurrent)
                {
                    for (int depth = startingDepth; depth < layout.Count; depth++)
                    {
                        if (layout[depth] > 0)
                        {
                            newCurrent = (depth, 0);
                            return true;
                        }
                    }
                    newCurrent = (layout.Count, 0);
                    return false;
                }
            }

            public void Reset() => throw new NotImplementedException();
        }

        private class MethodInfo
        {
            public MethodInfo(int MaxCaptures)
            {
                LocalFuncs = new List<IList<string>>();
                CaptureContext = new CaptureContext(MaxCaptures);
            }

            private MethodInfo() { }

            public List<IList<string>> LocalFuncs { get; private set; }

            public CaptureContext CaptureContext { get; private set; }

            public int TotalLocalFuncs { get; set; }

            public MethodInfo Clone()
            {
                return new MethodInfo
                {
                    LocalFuncs = this.LocalFuncs
                        .Select(x => x == null 
                                     ? null 
                                     : (IList<string>)new List<string>(x)).ToList(),
                    CaptureContext = this.CaptureContext.Clone()
                };
            }
        }

        private static IEnumerable<MethodInfo> MakeMethodsWithLayout(IList<int> localFuncLayout)
        {
            const int MaxCaptures = 3;

            var enumerator = new LayoutEnumerator(localFuncLayout);
            if (!enumerator.MoveNext())
            {
                return Array.Empty<MethodInfo>();
            }

            var methods = new List<MethodInfo>();
            DfsLayout(enumerator, new MethodInfo(MaxCaptures), 0);
            return methods;

            // Note that the enumerator is a struct, so every new var
            // is a copy
            void DfsLayout(LayoutEnumerator e, MethodInfo methodSoFar, int localFuncNameIndex)
            {
                var (depth, localFuncIndex) = e.Current;

                bool isLastFunc = !e.MoveNext();

                foreach (var captureCombo in GenerateAllSetCombinations(MaxCaptures, depth + 2))
                {
                    var copy = methodSoFar.Clone();
                    var expr = MakeCaptureExpression(captureCombo, copy.CaptureContext);
                    if (depth >= copy.LocalFuncs.Count)
                    {
                        copy.LocalFuncs.AddRange(Enumerable.Repeat<List<string>>(null, depth - copy.LocalFuncs.Count));
                        copy.LocalFuncs.Insert(depth, new List<string>());
                    }
                    string localFuncName = $"Local_{localFuncNameIndex}";
                    copy.LocalFuncs[depth].Add($"int {localFuncName}() => {expr};");
                    copy.CaptureContext.Add(depth + 1, $"{localFuncName}()");

                    if (!isLastFunc)
                    {
                        DfsLayout(e, copy, localFuncNameIndex + 1);
                    }
                    else
                    {
                        copy.TotalLocalFuncs = localFuncNameIndex + 1;
                        methods.Add(copy);
                    }
                }
            }
        }

        private static IEnumerable<MethodInfo> MakeAllMethods()
        {
            const int MaxDepth = 3;
            const int MaxLocalFuncs = 3;

            // Set combinations indicate what depth we will place local functions
            // at. For instance, { 0, 1 } indicates 0 local functions at method
            // depth and 1 local function at one nested scope below method level.
            foreach (var localFuncLayout in GenerateAllSetCombinations(MaxLocalFuncs, MaxDepth))
            {
                // Given a local function map, we need to generate capture
                // expressions for each local func at each depth
                foreach (var method in MakeMethodsWithLayout(localFuncLayout))
                    yield return method;
            }
        }

        private void SerializeMethod(MethodInfo methodInfo, StringBuilder builder, int methodIndex)
        {
            int totalLocalFuncs = methodInfo.TotalLocalFuncs;

            var methodText = new StringBuilder();
            var localFuncs = methodInfo.LocalFuncs;
            for (int depth = 0; depth < localFuncs.Count; depth++)
            {
                if (depth > 0)
                {
                    methodText.Append(' ', 4 * (depth + 1));
                    methodText.AppendLine("{");
                }

                var captureVars = methodInfo.CaptureContext.VariablesByScope;
                if (captureVars.Count > (depth + 1) &&
                    captureVars[depth + 1] != null)
                {
                    foreach (var captureVar in captureVars[depth + 1])
                    {
                        if (captureVar.EndsWith("()"))
                        {
                            continue;
                        }

                        methodText.Append(' ', 4 * (depth + 2));
                        methodText.AppendLine($"int {captureVar} = 0;");
                    }
                }

                if (localFuncs[depth] != null)
                {
                    foreach (var localFunc in localFuncs[depth])
                    {
                        methodText.Append(' ', 4 * (depth + 2));
                        methodText.AppendLine(localFunc);
                    }
                }
            }

            var localFuncCalls = string.Join(" + ",
                Enumerable.Range(0, totalLocalFuncs).Select(f => $"Local_{f}()"));
            methodText.AppendLine($"Console.WriteLine({localFuncCalls});");

            for (int depth = localFuncs.Count - 1; depth > 0; depth--)
            {
                methodText.Append(' ', 4 * (depth + 1));
                methodText.AppendLine("}");
            }

            builder.Append($@"
    public void M_{methodIndex}()
    {{
{methodText.ToString()}
    }}");

        }

        /// <summary>
        /// This test exercises the C# local function capturing analysis by generating
        /// all possible combinations of capturing within a certain complexity. The
        /// generating functions use a maximum number of variables captured per local function,
        /// a maximum number of local functions, and a maximum scope depth to decide the
        /// limits of the combinations.
        /// </summary>
425
        [NoIOperationValidationFact]
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
        public void AllCaptureTests()
        {
            var methods = MakeAllMethods().ToList();

            var fields = methods.First().CaptureContext.VariablesByScope[0];

            const int PartitionSize = 500;
            const string ClassFmt = @"
using System;
public class C
{{
    {0}";
            StringBuilder GetClassStart()
                => new StringBuilder(string.Format(ClassFmt,
                    string.Join("\r\n", fields.Select(f => $"public int {f} = 0;"))));

            Parallel.ForEach(Partitioner.Create(0, methods.Count, PartitionSize), (range, state) =>
            {
                var methodsText = GetClassStart();

                for (int methodIndex = range.Item1; methodIndex < range.Item2; methodIndex++)
                {
                    var methodInfo = methods[methodIndex];

                    if (methodInfo.TotalLocalFuncs == 0)
                    {
                        continue;
                    }

                    SerializeMethod(methodInfo, methodsText, methodIndex);
                }

                methodsText.AppendLine("\r\n}");
J
Jared Parsons 已提交
459
                CreateCompilation(methodsText.ToString()).VerifyEmitDiagnostics();
460 461 462 463
            });
        }
    }
}