Refactor vector API codegen and WideLane conversions

- Introduce IVectorAPIContext abstraction and supporting types for vectorized code generation
- Add Avx2APIContext and UtilityTemplate for AVX2-specific code emission
- Dynamically generate AVX2 sine methods in AVX2Rewriter
- Refactor WideLane<TNumber> to use Unsafe.BitCast for all Vector conversions
- Update all WideLane operators and math methods to use Unsafe.BitCast
- Change MultiplyAdd parameter names for clarity
- Remove static indices field in favor of Vector<TNumber>.Indices
- Add implicit conversion from Vector<TNumber> to WideLane<TNumber>
- Update tests and program files for compatibility
This commit is contained in:
2026-05-06 19:20:15 +09:00
parent c8f78f9d02
commit fd2d60c8f1
8 changed files with 439 additions and 84 deletions

View File

@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
namespace Misaki.HighPerformance.HPC.Generator.VectorAPI
{
internal class Avx2APIContext : IVectorAPIContext
{
private readonly List<string> _statements = new();
private int _varCount = 0;
private string? _lastAssignedVariable;
public string? LastAssignedVariable => _lastAssignedVariable;
public string GetVectorType()
{
return "Vector256";
}
public string GetVectorType<T>()
{
return $"Vector256<{VectorAPIContext.GetTypeName<T>()}>";
}
public Expression Call(string methodName, params string[] args)
{
return new Expression(this, $"{GetVectorType()}.{methodName}({string.Join(", ", args)})");
}
public Expression Assign(Expression expr, string? varName = null, bool isNew = true)
{
varName ??= $"v{_varCount++}";
var statement = isNew ? $"var {varName} = {expr.Code};" : $"{varName} = {expr.Code};";
_statements.Add(statement);
_lastAssignedVariable = varName;
expr.Clear();
return new Expression(this, varName);
}
public Code Return(Expression expr)
{
var statement = $"return {expr.Code};";
_statements.Add(statement);
expr.Clear();
var fullCode = new Code(_statements);
Reset();
return fullCode;
}
public Expression Create(string value)
{
return new Expression(this, $"{GetVectorType()}.Create({value})");
}
public Expression Zero<T>()
{
return new Expression(this, $"{GetVectorType<T>()}.Zero");
}
public Expression One<T>()
{
return new Expression(this, $"{GetVectorType<T>()}.One");
}
public Expression Count<T>()
{
return new Expression(this, $"{GetVectorType<T>()}.Count");
}
public Expression Add(Expression a, Expression b)
{
return new Expression(this, $"Avx2.Add({a}, {b})");
}
public Expression Multiply(Expression a, Expression b)
{
return new Expression(this, $"Avx2.Multiply({a}, {b})");
}
public Expression Subtract(Expression a, Expression b)
{
return new Expression(this, $"Avx2.Subtract({a}, {b})");
}
public Expression Divide(Expression a, Expression b)
{
return new Expression(this, $"Avx2.Divide({a}, {b})");
}
public Expression MultiplyAdd(Expression left, Expression right, Expression addend)
{
return new Expression(this, $"Fma.MultiplyAdd({left}, {right}, {addend})");
}
public Expression Round(Expression value)
{
return new Expression(this, $"Avx2.RoundToNearestInteger({value})");
}
public Expression Abs(Expression value)
{
return new Expression(this, $"{GetVectorType()}.Abs({value})");
}
public void Reset()
{
_statements.Clear();
_varCount = 0;
}
}
}

View File

@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Misaki.HighPerformance.HPC.Generator.VectorAPI
{
internal class Expression
{
public IVectorAPIContext API
{
get;
}
public string Code
{
get; private set;
}
public Expression(IVectorAPIContext api, string code)
{
API = api;
Code = code;
}
public Expression Assign(string? varName = null, bool isNew = true)
{
return API.Assign(this, varName, isNew);
}
public void Clear()
{
Code = string.Empty;
}
public override string ToString()
{
return Code;
}
public static Expression operator +(Expression a, Expression b)
{
return a.API.Add(a, b);
}
public static Expression operator -(Expression a, Expression b)
{
return a.API.Subtract(a, b);
}
public static Expression operator *(Expression a, Expression b)
{
return a.API.Multiply(a, b);
}
public static Expression operator /(Expression a, Expression b)
{
return a.API.Divide(a, b);
}
}
internal record Code
{
private readonly string[] _statements;
public Code(IEnumerable<string> statements)
{
_statements = statements.ToArray();
}
public string GetFullCode(string lineIndentation)
{
var sb = new StringBuilder();
foreach (var stmt in _statements)
{
sb.AppendLine(lineIndentation + stmt);
}
return sb.ToString();
}
}
internal record Method
{
public string Modifier
{
get;
}
public string ReturnType
{
get;
}
public string Name
{
get;
}
public string[] Parameters
{
get;
}
public Code Body
{
get;
}
public Method(string modifier, string returnType, string name, string[] parameters, Code body)
{
Modifier = modifier;
ReturnType = returnType;
Name = name;
Parameters = parameters;
Body = body;
}
public string GetFullCode(string lineIndentation)
{
var sb = new StringBuilder();
sb.AppendLine(lineIndentation + $"{Modifier} {ReturnType} {Name}({string.Join(", ", Parameters)})");
sb.AppendLine(lineIndentation + "{");
sb.Append(Body.GetFullCode(lineIndentation + " "));
sb.AppendLine(lineIndentation + "}");
return sb.ToString();
}
}
internal interface IVectorAPIContext
{
string? LastAssignedVariable
{
get;
}
string GetVectorType();
string GetVectorType<T>();
Expression Call(string methodName, params string[] args);
Expression Assign(Expression expr, string? varName = null, bool isNew = true);
Code Return(Expression expr);
Expression Create(string value);
Expression Zero<T>();
Expression One<T>();
Expression Count<T>();
Expression Add(Expression a, Expression b);
Expression Subtract(Expression a, Expression b);
Expression Multiply(Expression a, Expression b);
Expression Divide(Expression a, Expression b);
Expression MultiplyAdd(Expression left, Expression right, Expression addend);
Expression Round(Expression value);
Expression Abs(Expression value);
void Reset();
}
internal static class VectorAPIContext
{
public static string GetTypeName<T>()
{
return typeof(T) switch
{
_ when typeof(T) == typeof(float) => "float",
_ when typeof(T) == typeof(double) => "double",
_ when typeof(T) == typeof(byte) => "byte",
_ when typeof(T) == typeof(short) => "short",
_ when typeof(T) == typeof(int) => "int",
_ when typeof(T) == typeof(uint) => "uint",
_ when typeof(T) == typeof(long) => "long",
_ when typeof(T) == typeof(ulong) => "ulong",
_ => throw new NotSupportedException($"Type {typeof(T)} is not supported in vector operations.")
};
}
}
}