Comments (4)
I have been thinking about this during the weekend, and I don't think your outline is the best approach. As far as I can tell, you get problems no matter which subset of Expression you choose for ConstExpression:
- If you only allow things that are constant by construction (e.g. Constants, BinaryOp and UnaryOp), then the frontend needs to do inlining, and at that point it might be easier to just evaluate the expression.
- If you extend it to include e.g. LocalVariable and GlobalVariable, then you have no guarantee that it is actually a constant fragment, and then the type doesn't really make sense.
In both cases we get friction from having duplication between Expression and ConstExpression, and we also need to do extra work if we want constant folding optimizations for normal expressions.
Instead I propose that we change ArraySize::Static to contain a normal Expression. Then we can implement constant folding for all expressions in the IR, and have a step in the validator that checks that all expressions in ArraySize::Static are Constant after the constant folding.
Does that make sense, or have I missed something?
from naga.
I thought this is something we'll need, but now there is a bit of doubt, see gpuweb/gpuweb#905
It doesn't necessarily block us (if we have constant propagation but WGSL doesn't, we can work with that, and the other way around - too) though.
If you only allow things that are constant by construction (e.g. Constants, BinaryOp and UnaryOp), then the frontend needs to do inlining, and at that point it might be easier to just evaluate the expression.
I'm not sure what you mean here by "the frontend needs to do inlining", please elaborate!
The way I see it: when the front-end encounters a constant, its expression parsing gets the "constant" mode. So seeing A + B constants, it will use the constant expression BinaryOp
. If any of A
or B
are not constants, it will switch to the regular expression mode and convert any previously constant A
or B
into expressions by doing Expression::Constant
variant. This seems doable without too much code or complex logic. Please tell me if you think otherwise:)
If you extend it to include e.g. LocalVariable and GlobalVariable, then you have no guarantee that it is actually a constant fragment, and then the type doesn't really make sense.
Variables are not constants by definition :) constant expression enum doesn't need to include those variants.
In both cases we get friction from having duplication between Expression and ConstExpression
I don't know how much duplication there's really going to be. Roughly, we'll need the following variants:
- AccessIndex
- Constant
- Compose
- Unary
- Binary
- Intrinsic
This doesn't look too scary to me, although I agree that some friction will be present.
The dot/cross product need to be removed from the Expression
variants anyway, to be in line with other stdlib functions and intrinsics.
and we also need to do extra work if we want constant folding optimizations for normal expressions.
Wouldn't this make it easier? Constant folding would require precisely the same separation of constant mode vs regular expression mode that I described, so it seems like it would be a piece of cake to get if we need it.
Concluding a bit, I know what can be done here, but I don't know (or have a strong feeling) on what should be done.
We can proceed with your GLSL changes and see what WebGPU group decides. It could be that we'll just leave ArraySize::Static
to store a plain integer value.
from naga.
So, the group's resolution for now is to not have the constant propagation in WGSL.
However, the group also decided to have the specialization constants... and now we are sorta in trouble.
Say, WGSL or SPIR-V defines a private variable at global scope, initialized by a constant that happens to be a specialization constant. We don't have Arena<Expression>
outside of functions, so we can't have Handle<Expression>
in global scope. At the same time, we can't have a pure value in there, since it depends on something we don't know at compile time (the value of the spec constant). So it sounds to me that we'll need a ConstExpression
enum anyway!
from naga.
Another option would be to remove the serializer entirely from the local variables... which is fine, semantically speaking, since everything is expected to be zero-initialized, at least in wgpu
. It may be less fine from the performance perspective, since assigning a value would map to OpStore
, while the initializer is free in SPIR-V.
from naga.
Related Issues (20)
- Panic: gsamplerCubeArrayShadow isn't supported HOT 3
- [wgsl-in] Comparing scalar with vector type incorrectly passes validation
- Support for the .glsl extension HOT 3
- [wgsl-in] `sign()` incorrectly does not accept i32 arguments
- WGSL backend writes invalid WGSL in some cases HOT 7
- `indexmap` dependency is out of date HOT 1
- [hlsl-out] Miscompilation of array return types HOT 5
- Implement WGSL abstract types HOT 2
- In Firefox, Naga may prefer `std`'s default DOS-resistant hash functions HOT 2
- Missing validation: Adding initializer to workgroup variable declaration in WGSL causes shader compiler error
- Show exact place where shader failed in Firefox Nightly HOT 1
- [wgsl-in] vector constructor and splat constructors require decimal point for float types HOT 2
- Shader translation error, no function with name 'fwidth' HOT 11
- [wgsl-in] Why can't I use mat2x2<f32> as an instance input? HOT 1
- wgpu error: Validation Error HOT 1
- OOM triggered when compiling wgsl shader HOT 1
- Index out of bounds in lower/mod.rs
- Wrong number of arguments error is wrong/misleading HOT 9
- DO NOT FILE ISSUES ON THIS REPOSITORY
- A stack overflow vulnerability was found HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from naga.