Skip to content

Commit ca7ed22

Browse files
committed
tests
1 parent 412b473 commit ca7ed22

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,25 +355,23 @@ ${this.ctx.pre}}`;
355355

356356
// Short Circuit Evaluation
357357
if ((op === '||' || op === '&&') && isKnownAtComptime(lhsExpr)) {
358-
let evalRhs = !lhsExpr.value;
359-
if (op === '&&') {
360-
evalRhs = !evalRhs;
361-
}
358+
const evalRhs = op === '&&' ? !!lhsExpr.value : !lhsExpr.value;
362359

363360
if (!evalRhs) {
364-
return op === '||' ? snip(true, bool, 'constant') : snip(false, bool, 'constant');
361+
return snip(op === '||', bool, 'constant');
365362
}
366363

367364
const rhsExpr = this._expression(rhs);
368365

369-
if (isKnownAtComptime(rhsExpr)) {
370-
return snip(rhsExpr.value, bool, 'constant');
371-
}
372-
373366
if (rhsExpr.dataType === UnknownData) {
374367
throw new WgslTypeError(`Right-hand side of '${op}' is of unknown type`);
375368
}
376369

370+
if (isKnownAtComptime(rhsExpr)) {
371+
return snip(rhsExpr.value, bool, 'constant');
372+
}
373+
374+
// we can skip lhs
377375
const convRhs = tryConvertSnippet(this.ctx, rhsExpr, bool, false);
378376
const rhsStr = this.ctx.resolve(convRhs.value, convRhs.dataType).value;
379377
return snip(rhsStr, bool, 'runtime');

packages/typegpu/tests/tgsl/wgslGenerator.test.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,10 +2224,11 @@ describe('wgslGenerator', () => {
22242224
state.result = true;
22252225
});
22262226

2227-
it('handles `||` short-circuit evaluation', () => {
2227+
it('handles `||`', () => {
22282228
const f = () => {
22292229
'use gpu';
22302230
let res = -1;
2231+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
22312232
if (true || getTrackedBool()) {
22322233
res = 1;
22332234
}
@@ -2246,10 +2247,11 @@ describe('wgslGenerator', () => {
22462247
expect(state.counter).toBe(0);
22472248
});
22482249

2249-
it('handles `&&` short-circuit evaluation', () => {
2250+
it('handles `&&`', () => {
22502251
const f = () => {
22512252
'use gpu';
22522253
let res = -1;
2254+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
22532255
if (false && getTrackedBool()) {
22542256
res = 1;
22552257
}
@@ -2265,12 +2267,13 @@ describe('wgslGenerator', () => {
22652267
expect(state.counter).toBe(0);
22662268
});
22672269

2268-
it('handles chained `||` short-circuit evaluation', () => {
2270+
it('handles chained `||`', () => {
22692271
state.result = false;
22702272

22712273
const f = () => {
22722274
'use gpu';
22732275
let res = -1;
2276+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
22742277
if (getTrackedBool() || true || getTrackedBool() || getTrackedBool() || getTrackedBool()) {
22752278
res = 1;
22762279
}
@@ -2289,10 +2292,11 @@ describe('wgslGenerator', () => {
22892292
expect(state.counter).toEqual(1);
22902293
});
22912294

2292-
it('handles chained `&&` short-circuit evaluation', () => {
2295+
it('handles chained `&&`', () => {
22932296
const f = () => {
22942297
'use gpu';
22952298
let res = -1;
2299+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
22962300
if (getTrackedBool() && false && getTrackedBool() && getTrackedBool() && getTrackedBool()) {
22972301
res = 1;
22982302
}
@@ -2308,10 +2312,11 @@ describe('wgslGenerator', () => {
23082312
expect(state.counter).toBe(1);
23092313
});
23102314

2311-
it('handles mixed logical operators short-circuit evaluation', () => {
2315+
it('handles mixed logical operators', () => {
23122316
const f = () => {
23132317
'use gpu';
23142318
let res = -1;
2319+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
23152320
if (true || (getTrackedBool() && getTrackedBool())) {
23162321
res = 1;
23172322
}
@@ -2329,5 +2334,51 @@ describe('wgslGenerator', () => {
23292334
`);
23302335
expect(state.counter).toBe(0);
23312336
});
2337+
2338+
it('skips lhs if known at compile time', () => {
2339+
const f1 = tgpu.fn(
2340+
[d.bool],
2341+
d.i32,
2342+
)((b) => {
2343+
'use gpu';
2344+
let res = -1;
2345+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
2346+
if (false || b) {
2347+
res = 1;
2348+
}
2349+
return res;
2350+
});
2351+
2352+
const f2 = tgpu.fn(
2353+
[d.bool],
2354+
d.i32,
2355+
)((b) => {
2356+
'use gpu';
2357+
let res = -1;
2358+
// oxlint-disable-next-line(no-constant-binary-expression) -- part of the test
2359+
if (true && b) {
2360+
res = 1;
2361+
}
2362+
return res;
2363+
});
2364+
2365+
expect(tgpu.resolve([f1, f2])).toMatchInlineSnapshot(`
2366+
"fn f1(b: bool) -> i32 {
2367+
var res = -1;
2368+
if (b) {
2369+
res = 1i;
2370+
}
2371+
return res;
2372+
}
2373+
2374+
fn f2(b: bool) -> i32 {
2375+
var res = -1;
2376+
if (b) {
2377+
res = 1i;
2378+
}
2379+
return res;
2380+
}"
2381+
`);
2382+
});
23322383
});
23332384
});

0 commit comments

Comments
 (0)