[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); } }
// 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); } }
[DebuggerNonUserCode] private ExpressionExpander(Expression source, ExpansionContext ctx) { Source = source; Ctx = ctx; Stmts = new List <Node>(); Result = Expand(Source); }
[DebuggerNonUserCode] private ExpressionExpander(Expression source, ExpansionContext ctx) { Source = source; Ctx = ctx; Stmts = new List<Node>(); Result = Expand(Source); }
[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); }
[DebuggerNonUserCode] public static Block ExpandBlock(Block source, ExpansionContext ctx) { return new BlockExpander(source, ctx).Stmts; }
public static Block Expand(this Block blk, ExpansionContext ctx) { return BlockExpander.ExpandBlock(blk, ctx); }
public static ExpandedExpression Expand(this Expression expr, ExpansionContext ctx) { return ExpressionExpander.ExpandExpression(expr, ctx); }
public static Block Expand(this Block blk, ExpansionContext ctx) { return(BlockExpander.ExpandBlock(blk, ctx)); }
public static ExpandedExpression Expand(this Expression expr, ExpansionContext ctx) { return(ExpressionExpander.ExpandExpression(expr, ctx)); }
[DebuggerNonUserCode] public static Block ExpandBlock(Block source, ExpansionContext ctx) { return(new BlockExpander(source, ctx).Stmts); }
[DebuggerNonUserCode] public static ExpandedExpression ExpandExpression(Expression source, ExpansionContext ctx) { var expander = new ExpressionExpander(source, ctx); return(new ExpandedExpression(expander.Stmts, expander.Result)); }
[DebuggerNonUserCode] public static ExpandedExpression ExpandExpression(Expression source, ExpansionContext ctx) { var expander = new ExpressionExpander(source, ctx); return new ExpandedExpression(expander.Stmts, expander.Result); }