'Method to force store operation on C variable
Is there a way to guarantee that the compiler writes the value of a variable to the stack location of that variable for a specific assignment operation? In essence, I'm doing
int x = 0;
x = foo(x);
work1
work2
where x isn't used until work2, but work1 will scramble the register that holds x, so I want to guarantee that the assignment writes it to the stack. I know I can use the volatile keyword (i.e. volatile int x = 0), however I don't need that level of optimization of all accesses to x, only this specific one.
The details of the register scrambling have to do with parallel execution and cannot be avoided.
Edit: I'm compiling with clang 12.0.0 on an x86_64 system running linux. I'm executing on the same system.
Edit2:
I mentioned in the comments, but it's a bit more nuanced than being clobbered. Essentially one core may execute x = foo(x) and another core will longjump into the function and execute work1. Once both have completed the work, one of them at random will continue with work2. The reason it can't be stored in a register and must be written to memory is because although the two cores are operating on the same stack frame, they don't necessarily have the same registers after the second core longjumps into work1, and the store operation can happen after the longjump. None of this can be changed, the only question is whether I can force x to operate as though it were volatile for a single operation and then act normally afterwards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
