Skip to content

Commit

Permalink
gosl: read-or-write var flag working for Context like variables that …
Browse files Browse the repository at this point in the history
…are mostly read-only but sometimes writeable.
  • Loading branch information
rcoreilly committed Jan 9, 2025
1 parent eaaa433 commit 094a26b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gosl/examples/basic/shaders/Compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn ParamStruct_IntegFromRaw(ps: ParamStruct, idx: i32) {
SubStruct_IntegFromRaw(ps.Sub, idx);
}
fn Compute(i: u32) { //gosl:kernel
let Params=Params[0]; ParamStruct_IntegFromRaw(Params, i32(i));
let params=Params[0]; ParamStruct_IntegFromRaw(params, i32(i));
}

//////// import: "atomic.go"
Expand Down
2 changes: 1 addition & 1 deletion gosl/gotosl/gengpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (st *State) GenGPUSystemInit(sy *System) string {
} else {
b.WriteString(fmt.Sprintf("\t\t\tvr = sgp.AddStruct(%q, int(unsafe.Sizeof(%s{})), 1, gpu.ComputeShader)\n", vr.Name, vr.SLType()))
}
if vr.ReadOnly {
if vr.ReadOnly && !vr.ReadOrWrite {
b.WriteString("\t\t\tvr.ReadOnly = true\n")
}
}
Expand Down
3 changes: 1 addition & 2 deletions gosl/gotosl/genkernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func (st *State) GenKernelHeader(sy *System, kn *Kernel, avars map[string]*Var)
}
for vi, vr := range gp.Vars {
access := ", read_write"
rw := st.VarIsReadWrite(vr.Name)
if vr.ReadOnly && !rw {
if vr.ReadOnly && !vr.ReadOrWrite {
access = ", read"
}
if gp.Uniform {
Expand Down
8 changes: 7 additions & 1 deletion gosl/gotosl/gotosl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Kernel struct {
// Lines is full shader code
Lines [][]byte

// ReadWriteVars are variables marked as read_write for current kernel.
// ReadWriteVars are variables marked as read-write for current kernel.
ReadWriteVars map[string]bool
}

Expand All @@ -71,6 +71,12 @@ type Var struct {
// It is important to optimize GPU memory usage to indicate this.
ReadOnly bool

// ReadOrWrite indicates that this variable defaults to ReadOnly
// but is also flagged as read-write in some cases. It is registered
// as read_write in the gpu ComputeSystem, but processed as ReadOnly
// by default except for kernels that declare it as read-write.
ReadOrWrite bool

// True if a tensor type
Tensor bool

Expand Down
6 changes: 5 additions & 1 deletion gosl/gotosl/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2690,8 +2690,12 @@ func (p *printer) systemVars(d *ast.GenDecl, sysname string) {
vs := s.(*ast.ValueSpec)
dirs, docs := p.findDirective(vs.Doc)
readOnly := false
readOrWrite := false
if hasDirective(dirs, "read-only") {
readOnly = true
} else if hasDirective(dirs, "read-or-write") {
readOnly = true
readOrWrite = true
}
if gpnm, ok := directiveAfter(dirs, "group"); ok {
if gpnm == "" {
Expand Down Expand Up @@ -2754,7 +2758,7 @@ func (p *printer) systemVars(d *ast.GenDecl, sysname string) {
}
typ = sid.Name + "." + sel.Sel.Name
}
vr := &Var{Name: nm, Type: typ, ReadOnly: readOnly}
vr := &Var{Name: nm, Type: typ, ReadOnly: readOnly, ReadOrWrite: readOrWrite}
if strings.HasPrefix(typ, "tensor.") {
vr.Tensor = true
dstr, ok := directiveAfter(dirs, "dims")
Expand Down
2 changes: 1 addition & 1 deletion gosl/gotosl/testdata/Compute.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var<storage, read> TensorStrides: array<u32>;
@group(0) @binding(1)
var<storage, read> Params: array<ParamStruct>;
@group(0) @binding(2)
var<storage, read> Ctx: array<Context>;
var<storage, read_write> Ctx: array<Context>;
// // Data is the data on which the computation operates. // 2D: outer index is data, inner index is: Raw, Integ, Exp vars. //
@group(1) @binding(0)
var<storage, read_write> Data: array<f32>;
Expand Down
5 changes: 3 additions & 2 deletions gosl/gotosl/testdata/basic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions gosl/gotosl/testdata/basic.goal
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ var (
//gosl:read-only
Params []ParamStruct

// Ctx provides additional context.
//gosl:read-only
// Ctx provides additional context, and is usually read-only,
// but is updated in a specific kernel flagged as read-write.
//gosl:read-or-write
Ctx []Context

// Data is the data on which the computation operates.
Expand Down
1 change: 0 additions & 1 deletion gosl/gotosl/testdata/gosl.golden
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func GPUInit() {
vr = sgp.AddStruct("Params", int(unsafe.Sizeof(ParamStruct{})), 1, gpu.ComputeShader)
vr.ReadOnly = true
vr = sgp.AddStruct("Ctx", int(unsafe.Sizeof(Context{})), 1, gpu.ComputeShader)
vr.ReadOnly = true
sgp.SetNValues(1)
}
{
Expand Down

0 comments on commit 094a26b

Please sign in to comment.