-
-
Notifications
You must be signed in to change notification settings - Fork 71
fix: improve runtime ternary support by refining side-effect detection #2615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pullfrog
wants to merge
10
commits into
main
Choose a base branch
from
pullfrog/2587-improve-ternary-side-effect-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fed3aa2
fix: improve runtime ternary support by refining side-effect detection
pullfrog[bot] 89e4f5d
fix: improve runtime ternary support by refining side-effect detection
pullfrog[bot] 59ebd23
style: apply oxfmt formatting
pullfrog[bot] faee545
fix: add sideEffects prop to dualImpl for correct side-effect tracking
pullfrog[bot] 5bc6920
make sideEffects required in DualImplOptions
pullfrog[bot] 6538f87
chore: merge ternaryRuntime tests into ternaryOperator.test.ts
pullfrog[bot] 0d6d656
chore: address review feedback — use snip param for side-effects, fix…
pullfrog[bot] 3b6467e
fix: user-defined functions should assume side-effects are possible
pullfrog[bot] 69aee8b
Merge branch 'main' into pullfrog/2587-improve-ternary-side-effect-de…
iwoplaza 97f3455
chore: clarify docs for sideEffects and possibleSideEffects
pullfrog[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
pullfrog[bot] marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import { describe, expect } from 'vitest'; | ||
| import { it } from 'typegpu-testing-utility'; | ||
| import tgpu, { d } from '../../src/index.js'; | ||
|
|
||
| describe('runtime ternary operator', () => { | ||
| it('should handle subtraction in branches with function params', () => { | ||
| const myFn = tgpu.fn( | ||
| [d.u32, d.u32], | ||
| d.u32, | ||
| )((b, w) => { | ||
| return b > w ? b - w : w - b; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "fn myFn(b: u32, w: u32) -> u32 { | ||
| return select((w - b), (b - w), (b > w)); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle const array indexing in branches', () => { | ||
| const RotLut2Gpu = tgpu.const(d.arrayOf(d.u32, 2), [10, 20]); | ||
| const RotLut3Gpu = tgpu.const(d.arrayOf(d.u32, 3), [30, 40, 50]); | ||
|
|
||
| const myFn = tgpu.fn( | ||
| [d.u32, d.u32], | ||
| d.u32, | ||
| )((r, bitU) => { | ||
| return r === d.u32(2) ? (RotLut2Gpu.$[bitU] as number) : (RotLut3Gpu.$[bitU] as number); | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "const RotLut2Gpu: array<u32, 2> = array<u32, 2>(10u, 20u); | ||
|
|
||
| const RotLut3Gpu: array<u32, 3> = array<u32, 3>(30u, 40u, 50u); | ||
|
|
||
| fn myFn(r: u32, bitU: u32) -> u32 { | ||
| return select(RotLut3Gpu[bitU], RotLut2Gpu[bitU], (r == 2u)); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle nested runtime ternaries', () => { | ||
| const myFn = tgpu.fn( | ||
| [d.u32, d.u32, d.u32, d.u32], | ||
| d.u32, | ||
| )((r, v1, v2, v3) => { | ||
| return r === d.u32(1) ? v1 : r === d.u32(2) ? v2 : v3; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "fn myFn(r: u32, v1: u32, v2: u32, v3: u32) -> u32 { | ||
| return select(select(v3, v2, (r == 2u)), v1, (r == 1u)); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle bit shift in branch with function param', () => { | ||
| const myFn = tgpu.fn( | ||
| [d.bool], | ||
| d.u32, | ||
| )((isCustom) => { | ||
| return isCustom ? d.u32(1) << d.u32(20) : d.u32(0); | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "fn myFn(isCustom: bool) -> u32 { | ||
| return select(0u, (1u << 20u), isCustom); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle struct field access across ternaries', () => { | ||
| const Cw = d.struct({ | ||
| low: d.u32, | ||
| high: d.u32, | ||
| }); | ||
|
|
||
| const myFn = tgpu.fn( | ||
| [d.bool, Cw, Cw], | ||
| d.u32, | ||
| )((isCustom, customCw, stdCw) => { | ||
| return isCustom ? customCw.low : stdCw.low; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "struct Cw { | ||
| low: u32, | ||
| high: u32, | ||
| } | ||
|
|
||
| fn myFn(isCustom: bool, customCw: Cw, stdCw: Cw) -> u32 { | ||
| return select(stdCw.low, customCw.low, isCustom); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle buffer layout access in ternary branches', ({ root }) => { | ||
| const Cw = d.struct({ | ||
| low: d.u32, | ||
| high: d.u32, | ||
| }); | ||
|
|
||
| const Layout = d.struct({ | ||
| codewords: d.arrayOf(Cw, 64), | ||
| }); | ||
|
|
||
| const layout = root.createUniform(Layout, d.ref); | ||
|
|
||
| const myFn = tgpu.fn( | ||
| [d.bool, d.u32], | ||
| d.u32, | ||
| )((isCustom, cwIdx) => { | ||
| return isCustom ? layout.$.codewords[cwIdx]!.low : layout.$.codewords[cwIdx + d.u32(1)]!.low; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "struct Cw { | ||
| low: u32, | ||
| high: u32, | ||
| } | ||
|
|
||
| struct Layout { | ||
| codewords: array<Cw, 64>, | ||
| } | ||
|
|
||
| @group(0) @binding(0) var<uniform> layout_1: Layout; | ||
|
|
||
| fn myFn(isCustom: bool, cwIdx: u32) -> u32 { | ||
| return select(layout_1.codewords[(cwIdx + 1u)].low, layout_1.codewords[cwIdx].low, isCustom); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle ternary with comparison and unary negation in branches', () => { | ||
| const myFn = tgpu.fn( | ||
| [d.u32], | ||
| d.u32, | ||
| )((n) => { | ||
| return n > 0 ? n : -n; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "fn myFn(n: u32) -> u32 { | ||
| return select(-(n), n, (n > 0u)); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should throw when a ternary branch contains an assignment', () => { | ||
| const myFn = tgpu.fn( | ||
| [d.i32], | ||
| d.i32, | ||
| )((a) => { | ||
| let b = 0; | ||
| return a > 0 ? (b = a) : 0; | ||
| }); | ||
|
|
||
| expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(` | ||
| [Error: Resolution of the following tree failed: | ||
| - <root> | ||
| - fn:myFn: Ternary operator '(a > 0) ? (b = a) : 0' is invalid. For more complex branching, please use 'std.select' or if/else statements.] | ||
| `); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.