Skip to content

Commit

Permalink
Added support for unsigned types in VariableAllocator and CLCodeGener…
Browse files Browse the repository at this point in the history
…ator.
  • Loading branch information
m4rs-mt committed Nov 30, 2019
1 parent 1120333 commit efd7506
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
39 changes: 20 additions & 19 deletions Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Values.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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 ?
Expand All @@ -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 ?
Expand All @@ -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);
Expand Down
30 changes: 27 additions & 3 deletions Src/ILGPU/Backends/VariableAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using ILGPU.IR;
using ILGPU.IR.Types;
using ILGPU.Util;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -66,7 +67,7 @@ public sealed class PrimitiveVariable : Variable
/// </summary>
/// <param name="id">The current variable id.</param>
/// <param name="basicValueType">The basic value type.</param>
internal PrimitiveVariable(int id, BasicValueType basicValueType)
internal PrimitiveVariable(int id, ArithmeticBasicValueType basicValueType)
: base(id)
{
BasicValueType = basicValueType;
Expand All @@ -75,7 +76,7 @@ internal PrimitiveVariable(int id, BasicValueType basicValueType)
/// <summary>
/// Returns the associated basic value type.
/// </summary>
public BasicValueType BasicValueType { get; }
public ArithmeticBasicValueType BasicValueType { get; }
}

/// <summary>
Expand Down Expand Up @@ -213,6 +214,21 @@ public Variable Allocate(Value value)
return variable;
}

/// <summary>
/// Allocates a new variable.
/// </summary>
/// <param name="value">The value to allocate.</param>
/// <param name="basicValueType">The actual type to allocate.</param>
/// <returns>The allocated variable.</returns>
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;
}

/// <summary>
/// Allocates a new variable as type <typeparamref name="T"/>.
/// </summary>
Expand All @@ -227,9 +243,17 @@ public T AllocateAs<T>(Value value)
/// </summary>
/// <param name="basicValueType">The type to allocate.</param>
/// <returns>The allocated variable.</returns>
public Variable AllocateType(BasicValueType basicValueType) =>
public Variable AllocateType(ArithmeticBasicValueType basicValueType) =>
new PrimitiveVariable(idCounter++, basicValueType);

/// <summary>
/// Allocates the given type.
/// </summary>
/// <param name="basicValueType">The type to allocate.</param>
/// <returns>The allocated variable.</returns>
public Variable AllocateType(BasicValueType basicValueType) =>
new PrimitiveVariable(idCounter++, basicValueType.GetArithmeticBasicValueType(false));

/// <summary>
/// Allocates a pointer type.
/// </summary>
Expand Down

0 comments on commit efd7506

Please sign in to comment.