diff --git a/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs b/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs index 6c8273db8..bcc5e4882 100644 --- a/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs +++ b/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs @@ -156,83 +156,6 @@ protected interface IParametersSetupLogic Variable? HandleIntrinsicParameter(int parameterOffset, Parameter parameter); } - /// - /// Represents a specialized phi binding allocator. - /// - private readonly struct PhiBindingAllocator : IPhiBindingAllocator - { - private readonly Dictionary> phiMapping; - - /// - /// Constructs a new phi binding allocator. - /// - /// The parent code generator. - /// The blocks to use. - public PhiBindingAllocator( - CLCodeGenerator parent, - in BasicBlockCollection blocks) - { - phiMapping = new Dictionary>( - blocks.Count); - Parent = parent; - Dominators = blocks.CreateDominators(); - } - - /// - /// Returns the parent code generator. - /// - public CLCodeGenerator Parent { get; } - - /// - /// Returns the referenced dominators. - /// - public Dominators Dominators { get; } - - /// - /// Does not perform any operation. - /// - public void Process(BasicBlock block, Phis phis) { } - - /// - /// Allocates a new phi value in the dominator block. - /// - public void Allocate(BasicBlock block, PhiValue phiValue) - { - var variable = Parent.Allocate(phiValue); - - var targetBlock = block; - foreach (var argument in phiValue) - { - targetBlock = argument.BasicBlock == null - ? Dominators.Root - : Dominators.GetImmediateCommonDominator( - targetBlock, - argument.BasicBlock); - - if (targetBlock == Dominators.Root) - break; - } - - if (!phiMapping.TryGetValue(targetBlock, out var phiVariables)) - { - phiVariables = new List(); - phiMapping.Add(targetBlock, phiVariables); - } - phiVariables.Add(variable); - } - - /// - /// Tries to get phi variables to declare in the given block. - /// - /// The block. - /// The variables to declare (if any). - /// True, if there are some phi variables to declare. - public bool TryGetPhis( - BasicBlock block, - [NotNullWhen(true)] out List? phisToDeclare) => - phiMapping.TryGetValue(block, out phisToDeclare); - } - #endregion #region Static @@ -508,8 +431,34 @@ protected void GenerateCodeInternal() blockLookup.Add(block, DeclareLabel()); // Find all phi nodes, allocate target registers and setup internal mapping - var bindingAllocator = new PhiBindingAllocator(this, blocks); - var phiBindings = PhiBindings.Create(blocks, bindingAllocator); + var phiMapping = new Dictionary>(); + var dominators = Method.Blocks.CreateDominators(); + var phiBindings = PhiBindings.Create( + blocks, + (block, phiValue) => + { + var variable = Allocate(phiValue); + + var targetBlock = block; + foreach (var argument in phiValue) + { + targetBlock = argument.BasicBlock == null + ? dominators.Root + : dominators.GetImmediateCommonDominator( + targetBlock, + argument.BasicBlock); + + if (targetBlock == dominators.Root) + break; + } + + if (!phiMapping.TryGetValue(targetBlock, out var phiVariables)) + { + phiVariables = new List(); + phiMapping.Add(targetBlock, phiVariables); + } + phiVariables.Add(variable); + }); var intermediatePhiVariables = new Dictionary( phiBindings.MaxNumIntermediatePhis); @@ -520,7 +469,7 @@ protected void GenerateCodeInternal() MarkLabel(blockLookup[block]); // Declare phi variables (if any) - if (bindingAllocator.TryGetPhis(block, out var phisToDeclare)) + if (phiMapping.TryGetValue(block, out var phisToDeclare)) { foreach (var phiVariable in phisToDeclare) Declare(phiVariable); diff --git a/Src/ILGPU/Backends/PTX/Analyses/PTXBlockSchedule.cs b/Src/ILGPU/Backends/PTX/Analyses/PTXBlockSchedule.cs index faf6baa71..1b65610ac 100644 --- a/Src/ILGPU/Backends/PTX/Analyses/PTXBlockSchedule.cs +++ b/Src/ILGPU/Backends/PTX/Analyses/PTXBlockSchedule.cs @@ -12,6 +12,8 @@ using ILGPU.IR; using ILGPU.IR.Analyses.ControlFlowDirection; using ILGPU.IR.Analyses.TraversalOrders; +using ILGPU.IR.Values; +using System; using System.Collections.Immutable; namespace ILGPU.Backends.PTX.Analyses @@ -57,11 +59,10 @@ protected PTXBlockSchedule( /// /// Creates a new phi bindings mapping. /// - /// The custom allocator type. /// The allocator to use. /// The created phi bindings. - public abstract PhiBindings ComputePhiBindings(TAllocator allocator) - where TAllocator : struct, IPhiBindingAllocator; + public abstract PhiBindings ComputePhiBindings( + Action allocator); /// /// Returns true if the given is an implicit @@ -127,11 +128,10 @@ protected PTXBlockSchedule(in BasicBlockCollection blocks) /// /// Creates a new phi bindings mapping. /// - /// The custom allocator type. /// The allocator to use. /// The created phi bindings. - public override PhiBindings ComputePhiBindings( - TAllocator allocator) => + public override PhiBindings ComputePhiBindings( + Action allocator) => PhiBindings.Create(BasicBlockCollection, allocator); #endregion diff --git a/Src/ILGPU/Backends/PTX/PTXCodeGenerator.cs b/Src/ILGPU/Backends/PTX/PTXCodeGenerator.cs index 4b1713178..618a76a09 100644 --- a/Src/ILGPU/Backends/PTX/PTXCodeGenerator.cs +++ b/Src/ILGPU/Backends/PTX/PTXCodeGenerator.cs @@ -190,37 +190,6 @@ internal interface IParameterSetupLogic null; } - /// - /// Represents a specialized phi binding allocator. - /// - private readonly struct PhiBindingAllocator : IPhiBindingAllocator - { - /// - /// Constructs a new phi binding allocator. - /// - /// The parent code generator. - public PhiBindingAllocator(PTXCodeGenerator parent) - { - Parent = parent; - } - - /// - /// Returns the parent code generator. - /// - public PTXCodeGenerator Parent { get; } - - /// - /// Does not perform any operation. - /// - public void Process(BasicBlock block, Phis phis) { } - - /// - /// Allocates a new phi node in the parent code generator. - /// - public void Allocate(BasicBlock block, PhiValue phiValue) => - Parent.Allocate(phiValue); - } - #endregion #region Static @@ -518,7 +487,7 @@ protected void GenerateCodeInternal(int registerOffset) // Find all phi nodes, allocate target registers and setup internal mapping var phiBindings = Schedule.ComputePhiBindings( - new PhiBindingAllocator(this)); + (_, phiValue) => Allocate(phiValue)); var intermediatePhiRegisters = new Dictionary( phiBindings.MaxNumIntermediatePhis); Builder.AppendLine();