Skip to content

Commit 097ff37

Browse files
Fix SpecialType build errors in SortingNetworkGenerator
PR #82 changed SIMD emitter methods to accept SpecialType enum instead of string, but left callers in SortingNetworkGenerator still passing strings. This caused 26 CS1503/CS7036 build errors. Changes: - GetNativeIntDelegateTypes now returns (SpecialType, SpecialType)? instead of (string, string)? - Added delegateKeySpecialTypes dictionary to track SpecialType for each delegate key - Use GetKeywordName() to convert SpecialType back to string for code generation (method names, cast expressions) - Pass SpecialType to Emit/EmitAvx2Fallback/Emit ARM calls for nint/nuint delegate types Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cc5bf24 commit 097ff37

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

SortingNetworks.Generators/SortingNetworkGenerator.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
213213
var simdArmStepsByRequest = new Dictionary<string, List<List<(int A, int B)>>>();
214214
// Track nint/nuint → delegate type SIMD info (keyed by delegate type key, e.g. "int_16")
215215
var nativeIntSimdKeys = new Dictionary<string, HashSet<string>>();
216+
var delegateKeySpecialTypes = new Dictionary<string, SpecialType>();
216217
foreach (var request in validRequests)
217218
{
218219
var key = $"{request.TypeName}_{request.Size}";
@@ -249,14 +250,16 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
249250

250251
foreach (var delegateType in new[] { type32, type64 })
251252
{
252-
var delegateKey = $"{delegateType}_{request.Size}";
253+
var delegateTypeName = GetKeywordName(delegateType)!;
254+
var delegateKey = $"{delegateTypeName}_{request.Size}";
253255
bool delegateCanEmitSimd = SimdX86Emitter.CanEmit(delegateType, request.Size);
254256
bool delegateCanEmitAvx2 = SimdX86Emitter.CanEmitAvx2Fallback(delegateType, request.Size);
255257
bool delegateCanEmitArm = SimdArmEmitter.CanEmit(delegateType, request.Size);
256258

257259
if (delegateCanEmitSimd || delegateCanEmitAvx2 || delegateCanEmitArm)
258260
{
259261
delegateKeys.Add(delegateKey);
262+
delegateKeySpecialTypes[delegateKey] = delegateType;
260263
if (delegateCanEmitSimd && !simdStepsByRequest.ContainsKey(delegateKey))
261264
simdStepsByRequest[delegateKey] = decomposedSteps;
262265
if (delegateCanEmitAvx2 && !avx2FallbackStepsByRequest.ContainsKey(delegateKey))
@@ -398,13 +401,15 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
398401
{
399402
var nativeDelegate = GetNativeIntDelegateTypes(typeName)!.Value;
400403
var (type32, type64) = nativeDelegate;
404+
var typeName64 = GetKeywordName(type64)!;
405+
var typeName32 = GetKeywordName(type32)!;
401406

402407
// 64-bit path (nint.Size == 8)
403408
{
404409
var sizes64 = new List<NetworkRequest>();
405410
foreach (var request in nativeIntSimdSizes)
406411
{
407-
var delegateKey = $"{type64}_{request.Size}";
412+
var delegateKey = $"{typeName64}_{request.Size}";
408413
if (simdStepsByRequest.ContainsKey(delegateKey) ||
409414
avx2FallbackStepsByRequest.ContainsKey(delegateKey) ||
410415
simdArmStepsByRequest.ContainsKey(delegateKey))
@@ -425,7 +430,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
425430
foreach (var request in sizes64)
426431
{
427432
if (SimdX86Emitter.CanEmit(type64, request.Size))
428-
sb.AppendLine($" if (n == {request.Size}) {{ SortSimd{request.Size}_{type64}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {type64}>(span)); return; }}");
433+
sb.AppendLine($" if (n == {request.Size}) {{ SortSimd{request.Size}_{typeName64}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {typeName64}>(span)); return; }}");
429434
}
430435
sb.AppendLine(" }");
431436
}
@@ -438,7 +443,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
438443
foreach (var request in sizes64)
439444
{
440445
if (SimdArmEmitter.CanEmit(type64, request.Size))
441-
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdArm{request.Size}_{type64}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {type64}>(span)); return; }}");
446+
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdArm{request.Size}_{typeName64}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {typeName64}>(span)); return; }}");
442447
}
443448
sb.AppendLine(" }");
444449
}
@@ -451,7 +456,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
451456
var sizes32 = new List<NetworkRequest>();
452457
foreach (var request in nativeIntSimdSizes)
453458
{
454-
var delegateKey = $"{type32}_{request.Size}";
459+
var delegateKey = $"{typeName32}_{request.Size}";
455460
if (simdStepsByRequest.ContainsKey(delegateKey) ||
456461
avx2FallbackStepsByRequest.ContainsKey(delegateKey) ||
457462
simdArmStepsByRequest.ContainsKey(delegateKey))
@@ -472,7 +477,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
472477
foreach (var request in sizes32)
473478
{
474479
if (SimdX86Emitter.CanEmit(type32, request.Size))
475-
sb.AppendLine($" if (n == {request.Size}) {{ SortSimd{request.Size}_{type32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {type32}>(span)); return; }}");
480+
sb.AppendLine($" if (n == {request.Size}) {{ SortSimd{request.Size}_{typeName32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {typeName32}>(span)); return; }}");
476481
}
477482
sb.AppendLine(" }");
478483
}
@@ -485,7 +490,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
485490
foreach (var request in sizes32)
486491
{
487492
if (SimdX86Emitter.CanEmitAvx2Fallback(type32, request.Size))
488-
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdAvx2_{request.Size}_{type32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {type32}>(span)); return; }}");
493+
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdAvx2_{request.Size}_{typeName32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {typeName32}>(span)); return; }}");
489494
}
490495
sb.AppendLine(" }");
491496
}
@@ -498,7 +503,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
498503
foreach (var request in sizes32)
499504
{
500505
if (SimdArmEmitter.CanEmit(type32, request.Size))
501-
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdArm{request.Size}_{type32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {type32}>(span)); return; }}");
506+
sb.AppendLine($" if (n == {request.Size}) {{ SortSimdArm{request.Size}_{typeName32}(System.Runtime.InteropServices.MemoryMarshal.Cast<{typeName}, {typeName32}>(span)); return; }}");
502507
}
503508
sb.AppendLine(" }");
504509
}
@@ -588,13 +593,14 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
588593
var parts = delegateKey.Split('_');
589594
var delegateType = parts[0];
590595
var delegateSize = int.Parse(parts[1]);
596+
var delegateSpecialType = delegateKeySpecialTypes[delegateKey];
591597

592598
if (simdStepsByRequest.TryGetValue(delegateKey, out var delegateSimdSteps))
593599
{
594600
var methodName = $"SortSimd{delegateSize}_{delegateType}";
595601
if (!emittedSimdMethods.Contains(methodName))
596602
{
597-
var (simdMethod, _) = SimdX86Emitter.Emit(delegateSize, delegateType, delegateSimdSteps);
603+
var (simdMethod, _) = SimdX86Emitter.Emit(delegateSize, delegateType, delegateSpecialType, delegateSimdSteps);
598604
if (!string.IsNullOrEmpty(simdMethod))
599605
{
600606
emittedSimdMethods.Add(methodName);
@@ -609,7 +615,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
609615
var methodName = $"SortSimdAvx2_{delegateSize}_{delegateType}";
610616
if (!emittedSimdMethods.Contains(methodName))
611617
{
612-
var (avx2Method, _) = SimdX86Emitter.EmitAvx2Fallback(delegateSize, delegateType, delegateAvx2Steps);
618+
var (avx2Method, _) = SimdX86Emitter.EmitAvx2Fallback(delegateSize, delegateType, delegateSpecialType, delegateAvx2Steps);
613619
if (!string.IsNullOrEmpty(avx2Method))
614620
{
615621
emittedSimdMethods.Add(methodName);
@@ -624,7 +630,7 @@ private static void Execute(SourceProductionContext context, ImmutableArray<Gene
624630
var methodName = $"SortSimdArm{delegateSize}_{delegateType}";
625631
if (!emittedSimdMethods.Contains(methodName))
626632
{
627-
var (armMethod, _) = SimdArmEmitter.Emit(delegateSize, delegateType, delegateArmSteps);
633+
var (armMethod, _) = SimdArmEmitter.Emit(delegateSize, delegateType, delegateSpecialType, delegateArmSteps);
628634
if (!string.IsNullOrEmpty(armMethod))
629635
{
630636
emittedSimdMethods.Add(methodName);
@@ -695,10 +701,10 @@ public GenerationInfo(string className, string? ns, NetworkRequest[] requests)
695701
/// For nint: 32-bit=int, 64-bit=long. For nuint: 32-bit=uint, 64-bit=ulong.
696702
/// Returns null for non-native integer types.
697703
/// </summary>
698-
private static (string Type32, string Type64)? GetNativeIntDelegateTypes(string typeName)
704+
private static (SpecialType Type32, SpecialType Type64)? GetNativeIntDelegateTypes(string typeName)
699705
{
700-
if (typeName == "nint") return ("int", "long");
701-
if (typeName == "nuint") return ("uint", "ulong");
706+
if (typeName == "nint") return (SpecialType.System_Int32, SpecialType.System_Int64);
707+
if (typeName == "nuint") return (SpecialType.System_UInt32, SpecialType.System_UInt64);
702708
return null;
703709
}
704710

0 commit comments

Comments
 (0)