Skip to content

Commit cd07c3c

Browse files
committed
fix: Correct ToBytes method syntax and GPU declaration parsing
This commit fixes two critical bugs in the GPU code generation: 1. **ToBytes method used invalid syntax**: Changed from the incorrect type conversion syntax to using unsafe.Slice(), which is the modern and correct approach for Go 1.17+. Before (invalid): (*[unsafe.Sizeof(Type{})]byte)[unsafe.Pointer(s)] After (correct): unsafe.Slice((*byte)(unsafe.Pointer(s)), int(unsafe.Sizeof(*s))) 2. **Parser incorrectly matched regular structs as GPU structs**: GPUStructDecl and GPUFuncDecl used `@@*` (zero or more decorators), which caused regular structs/functions without decorators to be treated as GPU code. Changed both to `@@+` (one or more decorators required), ensuring only explicitly decorated code is treated as GPU/WGSL code. This prevents calculator and other non-GPU examples from generating unnecessary GPU files and fixes compilation errors. Changes: - Updated generateToBytesMethod to use unsafe.Slice - Changed GPUStructDecl decorators from `@@*` to `@@+` - Changed GPUFuncDecl decorators from `@@*` to `@@+` - All tests passing - Pre-commit checks passing
1 parent cfb6fa7 commit cd07c3c

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

pkg/ast/gpu_ast.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type GPUDecorator struct {
1313
// GPUStructDecl represents a struct marked with @gpu decorator
1414
type GPUStructDecl struct {
1515
Pos lexer.Position
16-
Decorators []*GPUDecorator `@@*`
16+
Decorators []*GPUDecorator `@@+`
1717
Name string `"type" @Ident`
1818
Struct *GPUStructType `@@`
1919
}
@@ -54,7 +54,7 @@ type GPUBindingDecl struct {
5454
// GPUFuncDecl represents a GPU shader function
5555
type GPUFuncDecl struct {
5656
Pos lexer.Position
57-
Decorators []*GPUDecorator `@@*`
57+
Decorators []*GPUDecorator `@@+`
5858
Name string `"func" @Ident`
5959
Params []*GPUParameter `"(" (@@ ("," @@)*)? ")"`
6060
Results *GPUReturnType `@@?`

pkg/codegen/gpu_go_generator.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (g *GPUGoGenerator) generateHelperMethods(gpuStruct *guixast.GPUStructDecl)
149149
}
150150

151151
// Generate ToBytes method
152+
//
153+
// Generates: func (s *StructName) ToBytes() []byte {
154+
// return unsafe.Slice((*byte)(unsafe.Pointer(s)), int(unsafe.Sizeof(*s)))
155+
// }
152156
func (g *GPUGoGenerator) generateToBytesMethod(structName string) *ast.FuncDecl {
153157
return &ast.FuncDecl{
154158
Recv: &ast.FieldList{
@@ -175,32 +179,48 @@ func (g *GPUGoGenerator) generateToBytesMethod(structName string) *ast.FuncDecl
175179
List: []ast.Stmt{
176180
&ast.ReturnStmt{
177181
Results: []ast.Expr{
178-
&ast.IndexExpr{
179-
X: &ast.ParenExpr{
180-
X: &ast.StarExpr{
181-
X: &ast.ArrayType{
182-
Len: &ast.CallExpr{
182+
// unsafe.Slice((*byte)(unsafe.Pointer(s)), int(unsafe.Sizeof(*s)))
183+
&ast.CallExpr{
184+
Fun: &ast.SelectorExpr{
185+
X: ast.NewIdent("unsafe"),
186+
Sel: ast.NewIdent("Slice"),
187+
},
188+
Args: []ast.Expr{
189+
// (*byte)(unsafe.Pointer(s))
190+
&ast.CallExpr{
191+
Fun: &ast.ParenExpr{
192+
X: &ast.StarExpr{
193+
X: ast.NewIdent("byte"),
194+
},
195+
},
196+
Args: []ast.Expr{
197+
&ast.CallExpr{
198+
Fun: &ast.SelectorExpr{
199+
X: ast.NewIdent("unsafe"),
200+
Sel: ast.NewIdent("Pointer"),
201+
},
202+
Args: []ast.Expr{ast.NewIdent("s")},
203+
},
204+
},
205+
},
206+
// int(unsafe.Sizeof(*s))
207+
&ast.CallExpr{
208+
Fun: ast.NewIdent("int"),
209+
Args: []ast.Expr{
210+
&ast.CallExpr{
183211
Fun: &ast.SelectorExpr{
184212
X: ast.NewIdent("unsafe"),
185213
Sel: ast.NewIdent("Sizeof"),
186214
},
187215
Args: []ast.Expr{
188-
&ast.CompositeLit{
189-
Type: ast.NewIdent(structName),
216+
&ast.StarExpr{
217+
X: ast.NewIdent("s"),
190218
},
191219
},
192220
},
193-
Elt: ast.NewIdent("byte"),
194221
},
195222
},
196223
},
197-
Index: &ast.CallExpr{
198-
Fun: &ast.SelectorExpr{
199-
X: ast.NewIdent("unsafe"),
200-
Sel: ast.NewIdent("Pointer"),
201-
},
202-
Args: []ast.Expr{ast.NewIdent("s")},
203-
},
204224
},
205225
},
206226
},

0 commit comments

Comments
 (0)