コード例 #1
0
 public void AggregateCheckDependents()
 {
     var result = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
     var dep = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
     var target = new StatementAggregate(result, dep);
     Assert.AreEqual(1, target.DependentVariables.Count());
     Assert.AreEqual(dep.RawValue, target.DependentVariables.First());
 }
コード例 #2
0
        public void AggregateCodeItUp()
        {
            var result = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var target = new StatementAggregate(result, new ValSimple("5", typeof(int)));
            Assert.AreEqual(0, target.DependentVariables.Count());
            Assert.AreEqual(result.RawValue, target.ResultVariable.RawValue);
            Assert.AreEqual(result.RawValue, target.ResultVariables.First());

            target.CodeItUp().DumpToConsole();

            Assert.AreEqual(1, target.CodeItUp().Count());
            Assert.IsTrue(target.CodeItUp().First().Contains("="), "the equal sign");
        }
コード例 #3
0
        public void AggregateRenameDependent()
        {
            var result = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var dep = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var depname = dep.RawValue;
            var target = new StatementAggregate(result, dep);
            target.CodeItUp().DumpToConsole();

            target.RenameVariable(dep.RawValue, "foot");

            target.CodeItUp().DumpToConsole();

            var r = target.CodeItUp().ToArray();
            Assert.AreEqual(-1, r[0].IndexOf(depname));
            Assert.AreNotEqual(-1, r[0].IndexOf("foot"));

            Assert.AreEqual(result.RawValue, target.ResultVariable.RawValue);
            Assert.AreEqual(1, target.DependentVariables.Count());
            Assert.AreEqual("foot", target.DependentVariables.First());
        }
コード例 #4
0
        public void AggregateCombineWithRename()
        {
            // a = a + b
            // c = c + b
            // These two should combine correctly, somehow.

            var a = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var ainc = new ValSimple(string.Format("{0}+b", a.ParameterName), typeof(int));
            var s1 = new StatementAggregate(a, ainc);

            var c = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var cinc = new ValSimple(string.Format("{0}+b", c.ParameterName), typeof(int));
            var s2 = new StatementAggregate(c, cinc);

            var opt = new MyCodeOptimizer(true);
            var result = s1.TryCombineStatement(s2, opt);
            Assert.IsTrue(result, "Expected combination would work");

            Assert.AreEqual(a.ParameterName, opt.NewVariable.ParameterName, "new name not renamed to");
            Assert.AreEqual(c.ParameterName, opt.OldName, "old name for rename not right");
        }
コード例 #5
0
        public void AggregateCombineWithRenameNoChance()
        {
            // a = a + b
            // c = c + b
            // These two should combine correctly, somehow.

            var a = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var ainc = new ValSimple(string.Format("{0}+b", a.ParameterName), typeof(int));
            var s1 = new StatementAggregate(a, ainc);

            var c = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var cinc = new ValSimple(string.Format("{0}+b", c.ParameterName), typeof(int));
            var s2 = new StatementAggregate(c, cinc);

            var opt = new MyCodeOptimizer(false);
            var result = s1.TryCombineStatement(s2, opt);
            Assert.IsFalse(result, "Expected combination would work");
        }
コード例 #6
0
        public void AggregateEquivalentSameWithPreRenames()
        {
            var r1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t1 = new StatementAggregate(r1, d1);

            var r2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t2 = new StatementAggregate(r2, d2);

            var r = t1.RequiredForEquivalence(t2, new Tuple<string, string>[] { new Tuple<string, string>(d2.RawValue, d1.RawValue) });
            Assert.IsTrue(r.Item1);
            var renames = r.Item2.ToArray();
            Assert.AreEqual(1, renames.Length);
            Assert.AreEqual(r1.RawValue, renames.Where(p => p.Item1 == r2.RawValue).First().Item2);
        }
コード例 #7
0
        public void AggregateEquivalentNotSame()
        {
            var r1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t1 = new StatementAggregate(r1, d1);

            var r2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t2 = new StatementAggregate(r2, new ValSimple($"{d2.RawValue}+b", typeof(int), new IDeclaredParameter[] { d1 }));

            var r = t1.RequiredForEquivalence(t2);
            Assert.IsFalse(r.Item1);
        }
コード例 #8
0
        public void AggregateEquivalentSameWithTwoSums()
        {
            var r1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t1 = new StatementAggregate(r1, new ValSimple($"{d1.RawValue}+{d2.RawValue}", typeof(int), new IDeclaredParameter[] { d1, d2 }));

            var r2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d3 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var d4 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var t2 = new StatementAggregate(r2, new ValSimple($"{d3.RawValue}+{d4.RawValue}", typeof(int), new IDeclaredParameter[] { d4, d3 }));

            var r = t1.RequiredForEquivalence(t2);
            Assert.IsTrue(r.Item1);
            var renames = r.Item2.ToArray();
            Assert.AreEqual(3, renames.Length);
        }
コード例 #9
0
        public void DontLiftThroughTwoForStatements()
        {
            var gc = new GeneratedCode();

            var counter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            counter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(counter);

            // The two for loops
            var fc1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for1 = new StatementForLoop(fc1, new ValSimple("5", typeof(int)));
            gc.Add(for1);

            var innerCounter = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            innerCounter.InitialValue = new ValSimple("0", typeof(int));
            gc.Add(innerCounter);

            var fc2 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var for2 = new StatementForLoop(fc2, new ValSimple("5", typeof(int)));
            gc.Add(for2);

            // Now, calculation based only on fc1
            var a1 = DeclarableParameter.CreateDeclarableParameterExpression(typeof(int));
            var ass1 = new StatementAssign(a1, new ValSimple($"{fc1}*2", typeof(int), new IDeclaredParameter[] { fc1 }));
            gc.Add(ass1);
            var agg1 = new StatementAggregate(innerCounter, new ValSimple($"{innerCounter.RawValue}+{a1.RawValue}", typeof(int), new IDeclaredParameter[] { innerCounter, a1 }));
            gc.Add(agg1);

            // and the outer sum.
            gc.Pop();
            var agg2 = new StatementAggregate(counter, new ValSimple($"{counter.RawValue}+{innerCounter.RawValue}", typeof(int), new IDeclaredParameter[] { counter, innerCounter }));
            gc.Add(agg2);

            // Great!
            Console.WriteLine("Unoptimized");
            gc.DumpCodeToConsole();

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            StatementLifter.Optimize(gc);

            Console.WriteLine("Optimized");
            Console.WriteLine("");
            gc.DumpCodeToConsole();

            // Make sure the inner aggregate got lifted out properly.
            Assert.AreEqual(1, for2.Statements.Count(), "# of statements in the inner for loop");
        }