Skip to content
This repository was archived by the owner on Feb 19, 2026. It is now read-only.

Commit 508e8c8

Browse files
committed
Code cleanup. All supported tests passing
1 parent 80c833e commit 508e8c8

16 files changed

+314
-329
lines changed

SpawnDev.ILGPU.WebGPU/Backend/WGSLCodeGenerator.cs

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
using global::ILGPU.IR.Analyses;
1515
using global::ILGPU.IR.Types;
1616
using global::ILGPU.IR.Values;
17-
using System;
18-
using System.Collections.Generic;
1917
using System.Text;
2018

2119
namespace SpawnDev.ILGPU.WebGPU.Backend
@@ -94,7 +92,7 @@ public Variable(string name, string type)
9492
protected int labelCounter = 0;
9593
private readonly Dictionary<Value, Variable> valueVariables = new();
9694
private readonly Dictionary<BasicBlock, string> blockLabels = new();
97-
95+
9896
// Flag to tracking if we are generating code within the state machine loop
9997
protected bool IsStateMachineActive { get; set; } = false;
10098

@@ -223,7 +221,7 @@ protected void Declare(Variable variable)
223221
{
224222
if (declaredVariables.Contains(variable.Name)) return;
225223
declaredVariables.Add(variable.Name);
226-
224+
227225
AppendIndent();
228226
Builder.Append("var ");
229227
Builder.Append(variable.Name);
@@ -333,7 +331,7 @@ protected void FinishFunctionBody()
333331
protected void GenerateCodeInternal()
334332
{
335333
var blocks = Method.Blocks;
336-
334+
337335
// Setup local allocations
338336
SetupAllocations(Allocas.LocalAllocations, MemoryAddressSpace.Local);
339337

@@ -353,7 +351,7 @@ protected void GenerateCodeInternal()
353351

354352
// Multiple blocks: use loop/switch pattern
355353
IsStateMachineActive = true;
356-
354+
357355
// Declare return variable if needed
358356
if (hasReturnValue)
359357
{
@@ -390,9 +388,9 @@ protected void GenerateCodeInternal()
390388
AppendLine("default: { break; }");
391389
PopIndent();
392390
AppendLine("}"); // end switch
393-
391+
394392
AppendLine("if (current_block == -1) { break; }");
395-
393+
396394
PopIndent();
397395
AppendLine("}"); // end loop
398396

@@ -430,7 +428,7 @@ protected void SetupAllocations(AllocaKindInformation allocas, MemoryAddressSpac
430428
{
431429
var variable = Allocate(allocaInfo.Alloca);
432430
var elementType = TypeGenerator[allocaInfo.ElementType];
433-
431+
434432
if (allocaInfo.IsArray)
435433
{
436434
AppendLine($"var {variable.Name} : array<{elementType}, {allocaInfo.ArraySize}>;");
@@ -452,8 +450,8 @@ protected void SetupAllocations(AllocaKindInformation allocas, MemoryAddressSpac
452450
protected void GenerateCodeFor(Value value)
453451
{
454452
// Skip void values and already-handled values
455-
if (value.Type.IsVoidType &&
456-
!(value is TerminatorValue) &&
453+
if (value.Type.IsVoidType &&
454+
!(value is TerminatorValue) &&
457455
!(value is Store) &&
458456
!(value is MemoryBarrier) &&
459457
!(value is global::ILGPU.IR.Values.Barrier) &&
@@ -485,10 +483,10 @@ protected void GenerateCodeFor(Value value)
485483
case global::ILGPU.IR.Values.Parameter p:
486484
GenerateCode(p);
487485
break;
488-
486+
489487
case global::ILGPU.IR.Values.MethodCall v:
490-
GenerateCode(v);
491-
break;
488+
GenerateCode(v);
489+
break;
492490

493491
// Arithmetic
494492
case global::ILGPU.IR.Values.BinaryArithmeticValue v:
@@ -615,7 +613,7 @@ protected void GenerateCodeFor(Value value)
615613
GenerateCode(v);
616614
break;
617615

618-
616+
619617
// Atomics & Barriers
620618
case global::ILGPU.IR.Values.GenericAtomic v:
621619
GenerateCode(v);
@@ -758,7 +756,7 @@ private void GenerateUnOp(UnaryArithmeticValue value)
758756
{
759757
UnaryArithmeticKind.Neg => $"-{operand}",
760758
UnaryArithmeticKind.Not => TypeGenerator[value.Value.Type] == "bool" ? $"!{operand}" : $"~{operand}",
761-
759+
762760
// Math Intrinsics (Float)
763761
UnaryArithmeticKind.Abs => $"abs({operand})",
764762
UnaryArithmeticKind.SinF => $"sin({operand})",
@@ -786,7 +784,7 @@ private void GenerateUnOp(UnaryArithmeticValue value)
786784
if (result == "DEBUG_MISSING")
787785
{
788786
AppendLine($"// [WGSL] Unhandled UnaryArithmeticKind: {value.Kind}");
789-
result = $"unhandled_unary({operand})";
787+
result = $"unhandled_unary({operand})";
790788
}
791789

792790
AppendLine($"{target} = {result};");
@@ -849,7 +847,7 @@ public virtual void GenerateCode(global::ILGPU.IR.Values.Load loadVal)
849847
Declare(target);
850848

851849
var targetType = TypeGenerator[loadVal.Type];
852-
string sourceType = targetType;
850+
string sourceType = targetType;
853851
bool isAtomic = IsAtomicPointer(loadVal.Source);
854852

855853
if (loadVal.Source.Type is global::ILGPU.IR.Types.PointerType ptrType)
@@ -860,7 +858,7 @@ public virtual void GenerateCode(global::ILGPU.IR.Values.Load loadVal)
860858

861859
if (isAtomic)
862860
{
863-
AppendLine($"{target} = atomicLoad({source});");
861+
AppendLine($"{target} = atomicLoad({source});");
864862
}
865863
else if (targetType != sourceType)
866864
{
@@ -931,18 +929,19 @@ public virtual void GenerateCode(LoadFieldAddress value)
931929
Builder.Append("let ");
932930
Builder.Append(target.Name);
933931
Builder.Append(" = ");
934-
932+
935933
string fieldName = $"field_{value.FieldSpan.Index}";
936934
if (IsIndexType(value.Source.Type))
937935
{
938-
fieldName = value.FieldSpan.Index switch {
936+
fieldName = value.FieldSpan.Index switch
937+
{
939938
0 => "x",
940939
1 => "y",
941940
2 => "z",
942941
_ => fieldName
943942
};
944943
}
945-
944+
946945
Builder.Append($"&(*{source}).{fieldName};");
947946
Builder.AppendLine();
948947
}
@@ -987,7 +986,7 @@ private string FormatFloat(float value)
987986
if (float.IsNaN(value)) return "0.0";
988987
if (float.IsPositiveInfinity(value)) return "3.402823e+38";
989988
if (float.IsNegativeInfinity(value)) return "-3.402823e+38";
990-
989+
991990
var str = value.ToString("G9");
992991
if (!str.Contains('.') && !str.Contains('e') && !str.Contains('E'))
993992
str += ".0";
@@ -1043,18 +1042,19 @@ public virtual void GenerateCode(GetField value)
10431042
var target = Load(value);
10441043
var source = Load(value.ObjectValue);
10451044
Declare(target);
1046-
1045+
10471046
string fieldName = $"field_{value.FieldSpan.Index}";
10481047
if (IsIndexType(value.ObjectValue.Type))
10491048
{
1050-
fieldName = value.FieldSpan.Index switch {
1049+
fieldName = value.FieldSpan.Index switch
1050+
{
10511051
0 => "x",
10521052
1 => "y",
10531053
2 => "z",
10541054
_ => fieldName
10551055
};
10561056
}
1057-
1057+
10581058
AppendLine($"{target} = {source}.{fieldName};");
10591059
}
10601060

@@ -1065,25 +1065,26 @@ public virtual void GenerateCode(SetField value)
10651065
var fieldValue = Load(value.Value);
10661066
Declare(target);
10671067
AppendLine($"{target} = {source};");
1068-
1068+
10691069
string fieldName = $"field_{value.FieldSpan.Index}";
10701070
if (IsIndexType(value.ObjectValue.Type))
10711071
{
1072-
fieldName = value.FieldSpan.Index switch {
1072+
fieldName = value.FieldSpan.Index switch
1073+
{
10731074
0 => "x",
10741075
1 => "y",
10751076
2 => "z",
10761077
_ => fieldName
10771078
};
10781079
}
1079-
1080+
10801081
AppendLine($"{target}.{fieldName} = {fieldValue};");
10811082
}
1082-
1083+
10831084
protected bool IsIndexType(TypeNode type)
10841085
{
10851086
var typeName = type.ToString();
1086-
return typeName.Contains("Index") &&
1087+
return typeName.Contains("Index") &&
10871088
(typeName.Contains("1D") || typeName.Contains("2D") || typeName.Contains("3D"));
10881089
}
10891090

@@ -1170,7 +1171,7 @@ public virtual void GenerateCode(ReturnTerminator value)
11701171
var retVal = Load(value.ReturnValue);
11711172
AppendLine($"_ilgpu_return_val = {retVal};");
11721173
}
1173-
1174+
11741175
AppendLine("current_block = -1;");
11751176
AppendLine("break;");
11761177
}
@@ -1222,7 +1223,7 @@ public virtual void GenerateCode(SwitchBranch branch)
12221223
var selector = Load(branch.Condition);
12231224
AppendLine($"switch ({selector}) {{");
12241225
PushIndent();
1225-
1226+
12261227
for (int i = 0; i < branch.NumCasesWithoutDefault; i++)
12271228
{
12281229
var target = branch.GetCaseTarget(i);
@@ -1270,7 +1271,7 @@ public virtual void GenerateCode(MethodCall methodCall)
12701271
{
12711272
var target = Load(methodCall);
12721273
Declare(target);
1273-
1274+
12741275
string name = methodCall.Target.Name;
12751276
// Map common intrinsics if they appear as method calls
12761277
string? wgslFunc = name switch
@@ -1288,12 +1289,12 @@ var n when n.Contains("Tanh") => "tanh",
12881289
var n when n.Contains("Step") && !n.Contains("Smooth") => "step",
12891290
var n when n.Contains("SmoothStep") => "smoothstep",
12901291
var n when n.Contains("FusedMultiplyAdd") => "fma",
1291-
1292+
12921293
var n when n.Contains("PopCount") => "countOneBits",
12931294
var n when n.Contains("TrailingZeroCount") => "countTrailingZeros",
12941295
var n when n.Contains("LeadingZeroCount") => "countLeadingZeros",
12951296
var n when n.Contains("Reverse") => "reverseBits",
1296-
1297+
12971298
// Standard Math
12981299
var n when n.Contains("Sin") => "sin",
12991300
var n when n.Contains("Cos") => "cos",
@@ -1313,7 +1314,7 @@ var n when n.Contains("Round") => "round",
13131314
var n when n.Contains("Truncate") => "trunc",
13141315
var n when n.Contains("Lerp") || n.Contains("Mix") => "mix",
13151316
var n when n.Contains("Select") => "select_custom",
1316-
1317+
13171318
var n when n.Contains("Sinh") => "sinh",
13181319
var n when n.Contains("Cosh") => "cosh",
13191320
var n when n.Contains("Tanh") => "tanh",
@@ -1326,7 +1327,7 @@ var n when n.Contains("Tanh") => "tanh",
13261327
if (methodCall.Count == 1)
13271328
{
13281329
var arg = Load(methodCall[0]);
1329-
1330+
13301331
if (wgslFunc == "rcp_custom")
13311332
{
13321333
AppendLine($"{target} = 1.0 / {arg};");
@@ -1383,7 +1384,7 @@ var n when n.Contains("Tanh") => "tanh",
13831384

13841385
AppendLine($"// Call: {methodCall.Target.Name} (Unmapped)");
13851386
// Probe Value 2
1386-
AppendLine($"{target} = 12345.0;");
1387+
AppendLine($"{target} = 12345.0;");
13871388
}
13881389

13891390
// Casts
@@ -1559,22 +1560,22 @@ public virtual void GenerateCode(LanguageEmitValue value)
15591560

15601561
public virtual void GenerateThrow(Value value)
15611562
{
1562-
// WebGPU does not support exceptions.
1563-
// We emit a trap/unreachable, or just a comment if we assume it's unreachable in valid code.
1564-
AppendLine($"// [WGSL] Throw encountered: {value} (Ignored/Unreachable)");
1565-
1566-
if (IsStateMachineActive)
1567-
{
1568-
// Break out of the loop
1569-
AppendLine("current_block = -1;");
1570-
AppendLine("break;");
1571-
}
1572-
else
1573-
{
1574-
AppendLine("return;");
1575-
}
1576-
}
1577-
1563+
// WebGPU does not support exceptions.
1564+
// We emit a trap/unreachable, or just a comment if we assume it's unreachable in valid code.
1565+
AppendLine($"// [WGSL] Throw encountered: {value} (Ignored/Unreachable)");
1566+
1567+
if (IsStateMachineActive)
1568+
{
1569+
// Break out of the loop
1570+
AppendLine("current_block = -1;");
1571+
AppendLine("break;");
1572+
}
1573+
else
1574+
{
1575+
AppendLine("return;");
1576+
}
1577+
}
1578+
15781579
#endregion
15791580

15801581
#region Math Intrinsics
@@ -1605,7 +1606,7 @@ public static void GenerateSign(WebGPUBackend backend, WGSLCodeGenerator codeGen
16051606
codeGenerator.AppendLine($"{target} = {call};");
16061607
}
16071608
}
1608-
1609+
16091610
public static void GenerateRound(WebGPUBackend backend, WGSLCodeGenerator codeGenerator, Value value)
16101611
{
16111612
if (value is MethodCall methodCall)
@@ -1688,7 +1689,7 @@ public static void GenerateClamp(WebGPUBackend backend, WGSLCodeGenerator codeGe
16881689
codeGenerator.AppendLine($"{target} = clamp({val}, {min}, {max});");
16891690
}
16901691
}
1691-
1692+
16921693
public static void GenerateFusedMultiplyAdd(WebGPUBackend backend, WGSLCodeGenerator codeGenerator, Value value)
16931694
{
16941695
if (value is MethodCall methodCall)

SpawnDev.ILGPU.WebGPU/Backend/WGSLFunctionGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
using global::ILGPU.IR;
1111
using global::ILGPU.IR.Analyses;
12-
using global::ILGPU.IR.Values;
1312
using System.Text;
1413

1514
namespace SpawnDev.ILGPU.WebGPU.Backend
@@ -54,18 +53,18 @@ private void GenerateHeaderStub(StringBuilder builder)
5453
builder.Append("fn ");
5554
builder.Append(GetMethodName(Method));
5655
builder.Append("(");
57-
56+
5857
// Emit parameters
5958
bool first = true;
6059
foreach (var param in Method.Parameters)
6160
{
6261
if (!first) builder.Append(", ");
6362
first = false;
64-
63+
6564
var paramType = TypeGenerator[param.ParameterType];
6665
builder.Append($"p_{param.Id} : {paramType}");
6766
}
68-
67+
6968
builder.Append(") -> ");
7069
builder.Append(TypeGenerator[Method.ReturnType]);
7170
}

SpawnDev.ILGPU.WebGPU/Backend/WGSLIntrinsic.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// WGSL intrinsic handler delegate for WebGPU backend code generation.
88
// ---------------------------------------------------------------------------------------
99

10-
using global::ILGPU.IR.Values;
11-
1210
namespace SpawnDev.ILGPU.WebGPU.Backend
1311
{
1412
/// <summary>

0 commit comments

Comments
 (0)