Пример #1
0
        [DebuggerNonUserCode] public BlockExpander(Block source, ExpansionContext ctx)
        {
            Source = source;
            Ctx    = ctx;

            Stmts     = new Block();
            ctx.Scope = Stmts;
            var cloned_locals = source.Locals.Select(l => l.DeepClone());

            cloned_locals.ForEach(local => ctx.Scope.Locals.Add(local));

            var is_root = Ctx.Parent == null || (Ctx.Parent.Stack.Count() != Ctx.Stack.Count());

            if (is_root)
            {
                RetLabel = new Label();
            }
            else
            {
                RetLabel.AssertNotNull();
            }

            source.ForEach(Expand);
            if (is_root && Stmts.LastOrDefault() is Goto)
            {
                Stmts.RemoveLast();
            }
            var gotos = Stmts.Family().OfType <Goto>().Where(@goto => @goto.LabelId == RetLabel.Id).ToReadOnly();

            if (is_root && gotos.IsNotEmpty())
            {
                Stmts.Add(RetLabel);
            }
        }
Пример #2
0
        // todo. cache jitted kernels
        public static JittedKernel DoCompile(CudaConfig cfg, Type t_kernel)
        {
            t_kernel = t_kernel.Hierarchy().AssertFirst(t => !t.Assembly.HasAttr<CompilerGeneratedAttribute>());
            using (var ctx = new JitContext(cfg, t_kernel))
            {
                var inliner_ctx = new ExpansionContext(Kernel);
                Hir = Hir.Expand(inliner_ctx);
                Log.EnsureBlankLine();
                Log.WriteLine("After inlining:");
                Log.WriteLine(Hir.DumpAsText());

                MemoryAllocator.InferAllocationScheme();
                Log.EnsureBlankLine();
                Log.WriteLine("Non-standard allocations:");
                var nonstandard_allocs = 0;
                Allocs.Fields.Where(kvp => kvp.Value != MemoryTier.Global).ForEach(kvp => { Log.WriteLine(kvp.Key.GetCSharpRef(ToCSharpOptions.Informative)); nonstandard_allocs++; });
                Allocs.Symbols.Where(kvp => kvp.Value != MemoryTier.Private).ForEach(kvp => { Log.WriteLine(kvp.Key); nonstandard_allocs++; });
                Log.WriteLine((nonstandard_allocs == 0 ? "None" : "") + Environment.NewLine);

                // todo. also implement the following:
                // 1) downgrade to SSA
                // 2) perform SCC from "Constant Propagation with Conditional Branches"
                //    when performing SCC don't forget to funcletize stuff e.g. Impl::Cfg and Impl::Device
                // 3) eliminate dead code

                Generator.Traverse(Hir);
                Log.EnsureBlankLine();
                Log.WriteLine("Generated PTX:");
                Log.WriteLine();
                Log.WriteLine(Ptx);

                return new JittedKernel(ctx);
            }
        }
Пример #3
0
 [DebuggerNonUserCode] private ExpressionExpander(Expression source, ExpansionContext ctx)
 {
     Source = source;
     Ctx    = ctx;
     Stmts  = new List <Node>();
     Result = Expand(Source);
 }
Пример #4
0
 [DebuggerNonUserCode] private ExpressionExpander(Expression source, ExpansionContext ctx)
 {
     Source = source;
     Ctx = ctx;
     Stmts = new List<Node>();
     Result = Expand(Source);
 }
Пример #5
0
        [DebuggerNonUserCode] public BlockExpander(Block source, ExpansionContext ctx)
        {
            Source = source;
            Ctx = ctx;

            Stmts = new Block();
            ctx.Scope = Stmts;
            var cloned_locals = source.Locals.Select(l => l.DeepClone());
            cloned_locals.ForEach(local => ctx.Scope.Locals.Add(local));

            var is_root = Ctx.Parent == null || (Ctx.Parent.Stack.Count() != Ctx.Stack.Count());
            if (is_root) RetLabel = new Label();
            else RetLabel.AssertNotNull();

            source.ForEach(Expand);
            if (is_root && Stmts.LastOrDefault() is Goto) Stmts.RemoveLast();
            var gotos = Stmts.Family().OfType<Goto>().Where(@goto => @goto.LabelId == RetLabel.Id).ToReadOnly();
            if (is_root && gotos.IsNotEmpty()) Stmts.Add(RetLabel);
        }
Пример #6
0
 [DebuggerNonUserCode] public static Block ExpandBlock(Block source, ExpansionContext ctx) { return new BlockExpander(source, ctx).Stmts; }
Пример #7
0
 public static Block Expand(this Block blk, ExpansionContext ctx)
 {
     return BlockExpander.ExpandBlock(blk, ctx);
 }
Пример #8
0
 public static ExpandedExpression Expand(this Expression expr, ExpansionContext ctx)
 {
     return ExpressionExpander.ExpandExpression(expr, ctx);
 }
Пример #9
0
 public static Block Expand(this Block blk, ExpansionContext ctx)
 {
     return(BlockExpander.ExpandBlock(blk, ctx));
 }
Пример #10
0
 public static ExpandedExpression Expand(this Expression expr, ExpansionContext ctx)
 {
     return(ExpressionExpander.ExpandExpression(expr, ctx));
 }
Пример #11
0
 [DebuggerNonUserCode] public static Block ExpandBlock(Block source, ExpansionContext ctx)
 {
     return(new BlockExpander(source, ctx).Stmts);
 }
Пример #12
0
 [DebuggerNonUserCode] public static ExpandedExpression ExpandExpression(Expression source, ExpansionContext ctx)
 {
     var expander = new ExpressionExpander(source, ctx); return(new ExpandedExpression(expander.Stmts, expander.Result));
 }
Пример #13
0
 [DebuggerNonUserCode] public static ExpandedExpression ExpandExpression(Expression source, ExpansionContext ctx) { var expander = new ExpressionExpander(source, ctx); return new ExpandedExpression(expander.Stmts, expander.Result); }