Comments (5)
There is a provision for this in the Google style guide.
(This prohibition does not apply to a static variable within function scope, since its initialization order is well-defined and does not occur until control passes through its declaration.)
Also since static objects are not allowed by the style guide, this should not be an issue. The only objects which CAN call this function during destruction would be function local objects with static lifetimes. The destruction of these objects is well defined by the standard. If one of the function local objects were to access this method in the destructor then you can define the order of creation(and therefore destruction) by calling the GetModuleOrder before the other object is created.
int main() {
GetModuleOrder();
OtherFunctionWhichDependsOnGetModuleOrderInDestructor();
...
// Profit??
}
This approach is also thread safe in the C++11 standard. Reference: http://stackoverflow.com/a/8102145/523374
Where as the dynamically allocated vector would require synchronization mechanism to achieve the same effect.
from spirv-tools.
Umar, I'm not sure I agree with your guide interpretation. The exception you quote does not reverse the ban on function static variables (additionally reinforced by the second paragraph). Instead, it's an exception to the sentence immediately preceding it:
Therefore in addition to banning globals of class type, we do not allow static POD variables to be initialized with the result of a function...
Here's how the dangling-reference danger could play out:
struct Foo {
Foo(const vector<vector<SpvOp>>& moduleOrder) : mo_(moduleOrder) {}
~Foo() { cout << mo_[0][0] << endl; }
const vector<vector<SpvOp>>& mo_;
}
int main() {
static Foo foo(GetModuleOrder());
}
Although mo_
is a reference to a static variable, that variable may be destroyed before foo
's destructor is called.
from spirv-tools.
The order in which Foo
and the module order vector is well defined in the example you posted. The code you posed will yield the correct(intended?) results because the GetModuleOrder
function is called before the Foo
object is initialized. Here is an example where it would cause issues you brought up:
struct Foo {
Foo() {}
~Foo() { cout << GetModuleOrder()[0][0] << endl; }
}
int main() {
static Foo foo();
GetModuleOrder();
return 0;
}
This code will cause a problem because GetModuleOrder is called after the static Foo object is initialized. And since order of destruction is the opposite of the other of construction the vector module is called In order to fix this type of issue all you would need to do is to call GetModuleOrder
before the Foo construction like so:
int main() {
GetModuleOrder();
static Foo foo();
return 0;
}
Anyway, the only reason the Google style guide gives is that the order is not defined for static objects. Function local static objects have a defined order of construction and destruction so I don't think that rule should apply to those objects.
from spirv-tools.
Thanks for correcting my example; I agree that it didn't illustrate the danger well because of the deterministic destruction order. Of course, it's still too easy to accidentally reorder GetModuleOrder()
and foo()
-- two lines that don't appear related to each other when you look at them alone -- and get slapped with undefined behaviour.
I'm not comfortable with ignoring parts of the style guide (however much I may find them objectionable) without a broad agreement and a written-down list of exceptions first.
from spirv-tools.
The function is only used to support ValidationState_t::isOpcodeInCurrentLayoutStage
That's a table lookup that could be implemented as a set of switch statements. That would eliminate the complexity and I think would be at least as readable.
from spirv-tools.
Related Issues (20)
- Wrong validation error when using RTA in SPV1.4? HOT 2
- Request: allow SpirvToolsEliminateDeadInputComponents() to strip non-trailing members from blocks HOT 2
- SpirvToolsEliminateDeadInputComponents() doesn't seem to be trimming array size down HOT 1
- spirv-val false positive error HOT 6
- Compilation error in validate_atomics.cpp HOT 2
- Using opt C API makes no change to shader HOT 1
- v2024.1 flatpak build fails: validation_state.cpp error: ‘BlockMatchSamplerQCOM’ is not a member of ‘spv::Decoration’ HOT 2
- How to add a new OpName with DescriptorSet and Binding HOT 2
- spirv-opt fma folding causes large performance degradation on Adreno HOT 6
- how to remove the OpVariable inst??
- Implicit LOD functions can be supported when compute shader derivatives are supported HOT 6
- How to compile on arm platform HOT 3
- Disallows TypeManager::GetId on non unique types
- Slang produces spv that works on win/linux but not with MoltenVk
- Incorrect codegen when there is redundant `float16_t` conversion HOT 1
- spirv-opt: loop unroll pass with shader produces spirv-val failure
- Generator comment in spirv-dis output is failing to look up name from spir-v.xml HOT 5
- Inability to use system versions of dependencies. HOT 1
- spirv-tools build is failing for gcc 12.4 HOT 4
- SPIRV-Tools-toolsTargets.cmake references spirv-objdump but the file does not exist HOT 4
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 spirv-tools.