diff --git a/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Values.cs b/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Values.cs index c53e782cb..0d76cf865 100644 --- a/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Values.cs +++ b/Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Values.cs @@ -65,11 +65,11 @@ public void Visit(PhiValue phiValue) public void Visit(UnaryArithmeticValue value) { var argument = Load(value.Value); - var target = Allocate(value); + var target = Allocate(value, value.ArithmeticBasicValueType); using (var statement = BeginStatement(target)) { - statement.AppendCast(value.BasicValueType); + statement.AppendCast(value.ArithmeticBasicValueType); var operation = CLInstructions.GetArithmeticOperation( value.Kind, value.BasicValueType.IsFloat(), @@ -93,10 +93,10 @@ public void Visit(BinaryArithmeticValue value) var left = Load(value.Left); var right = Load(value.Right); - var target = Allocate(value); + var target = Allocate(value, value.ArithmeticBasicValueType); using (var statement = BeginStatement(target)) { - statement.AppendCast(value.BasicValueType); + statement.AppendCast(value.ArithmeticBasicValueType); var operation = CLInstructions.GetArithmeticOperation( value.Kind, value.BasicValueType.IsFloat(), @@ -140,10 +140,10 @@ public void Visit(TernaryArithmeticValue value) var second = Load(value.Second); var third = Load(value.Third); - var targetRegister = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + var target = Allocate(value, value.ArithmeticBasicValueType); + using (var statement = BeginStatement(target)) { - statement.AppendCast(value.BasicValueType); + statement.AppendCast(value.ArithmeticBasicValueType); statement.AppendCommand(operation); statement.BeginArguments(); @@ -169,8 +169,8 @@ public void Visit(CompareValue value) var left = Load(value.Left); var right = Load(value.Right); - var targetRegister = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + var target = Allocate(value); + using (var statement = BeginStatement(target)) { statement.AppendCast(value.CompareType); statement.AppendArgument(left); @@ -187,10 +187,11 @@ public void Visit(ConvertValue value) { var sourceValue = Load(value.Value); - var targetRegister = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + var target = Allocate(value, value.TargetType); + using (var statement = BeginStatement(target)) { statement.AppendCast(value.TargetType); + statement.AppendCast(value.SourceType); statement.AppendArgument(sourceValue); } } @@ -200,8 +201,8 @@ public void Visit(PointerCast value) { var sourceValue = Load(value.Value); - var targetRegister = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + var target = Allocate(value); + using (var statement = BeginStatement(target)) { statement.AppendCast(value.TargetType); statement.AppendArgument(sourceValue); @@ -212,9 +213,9 @@ public void Visit(PointerCast value) public void Visit(FloatAsIntCast value) { var source = Load(value.Value); - var targetRegister = Allocate(value); + var target = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + using (var statement = BeginStatement(target)) { statement.AppendCommand( value.BasicValueType == BasicValueType.Int64 ? @@ -230,9 +231,9 @@ public void Visit(FloatAsIntCast value) public void Visit(IntAsFloatCast value) { var source = Load(value.Value); - var targetRegister = Allocate(value); + var target = Allocate(value); - using (var statement = BeginStatement(targetRegister)) + using (var statement = BeginStatement(target)) { statement.AppendCommand( value.BasicValueType == BasicValueType.Float64 ? @@ -251,8 +252,8 @@ public void Visit(Predicate predicate) var trueValue = Load(predicate.TrueValue); var falseValue = Load(predicate.FalseValue); - var targetRegister = Allocate(predicate); - using (var statement = BeginStatement(targetRegister)) + var target = Allocate(predicate); + using (var statement = BeginStatement(target)) { statement.AppendArgument(condition); statement.AppendCommand(CLInstructions.SelectOperation1); diff --git a/Src/ILGPU/Backends/VariableAllocator.cs b/Src/ILGPU/Backends/VariableAllocator.cs index d13c9365a..3f61c11d5 100644 --- a/Src/ILGPU/Backends/VariableAllocator.cs +++ b/Src/ILGPU/Backends/VariableAllocator.cs @@ -11,6 +11,7 @@ using ILGPU.IR; using ILGPU.IR.Types; +using ILGPU.Util; using System; using System.Collections.Generic; @@ -66,7 +67,7 @@ public sealed class PrimitiveVariable : Variable /// /// The current variable id. /// The basic value type. - internal PrimitiveVariable(int id, BasicValueType basicValueType) + internal PrimitiveVariable(int id, ArithmeticBasicValueType basicValueType) : base(id) { BasicValueType = basicValueType; @@ -75,7 +76,7 @@ internal PrimitiveVariable(int id, BasicValueType basicValueType) /// /// Returns the associated basic value type. /// - public BasicValueType BasicValueType { get; } + public ArithmeticBasicValueType BasicValueType { get; } } /// @@ -213,6 +214,21 @@ public Variable Allocate(Value value) return variable; } + /// + /// Allocates a new variable. + /// + /// The value to allocate. + /// The actual type to allocate. + /// The allocated variable. + public Variable Allocate(Value value, ArithmeticBasicValueType basicValueType) + { + if (variableLookup.TryGetValue(value, out Variable variable)) + return variable; + variable = AllocateType(basicValueType); + variableLookup.Add(value, variable); + return variable; + } + /// /// Allocates a new variable as type . /// @@ -227,9 +243,17 @@ public T AllocateAs(Value value) /// /// The type to allocate. /// The allocated variable. - public Variable AllocateType(BasicValueType basicValueType) => + public Variable AllocateType(ArithmeticBasicValueType basicValueType) => new PrimitiveVariable(idCounter++, basicValueType); + /// + /// Allocates the given type. + /// + /// The type to allocate. + /// The allocated variable. + public Variable AllocateType(BasicValueType basicValueType) => + new PrimitiveVariable(idCounter++, basicValueType.GetArithmeticBasicValueType(false)); + /// /// Allocates a pointer type. ///