--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/D185C88E-AB3A-463A-9A50-7912B0E99B0C%40livius.net.
__asm__ __volatile__("":::"memory");
Not sure this has to do with initialization issues.Outside of a bug in GCC, the answer is "No", volatile accesses can't be reordered with respect to each other across sequence points (different statements, basically). However, non-volatile access could be reordered across volatile accesses. To prevent this, you'd need a platform specific barrier instruction, or the following portable inline "assembly":__asm__ __volatile__("":::"memory");
This may be GCC specific and not part of ISO/ANSI C.
Here's a good answer on SO: https://stackoverflow.com/questions/22106843/gccs-reordering-of-read-write-instructions-Evan
On Monday, December 4, 2017 at 5:53:19 PM UTC-6, Liviu Ionescu wrote:(This is a question for Palmer and/or the other compiler gurus)
With a sequence like:
```c
volatile uint32_t array[2];
volatile uint32_t *p = array;
uint32_t v1 = *p;
uint32_t v2 = *(p+1);
```
Given the current ISO/ANSI specs, and the current behaviour of RISC-V GCC, are the last two instructions always executed in this order, or, for whatever reason, the compiler may reorder them, and the accesses are done in reverse order?
If the instructions may be reordered, is there any trick available (memory barriers, atomics, or anything else) to prevent this?
Same question for
```c
volatile uint64_t long_variable;
volatile uint32_t *p = (uint32_t*)&long_variable;
uint32_t v1 = *p;
uint32_t v2 = *(p+1);
```
In other words, is it possible, on a 32-bits platform, with the current RISC-V GCC, to find a way to always access the two halves of a 64-bits variable (or memory mapped register) in the desired order?
Thank you,
Liviu
--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+unsubscribe@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/2fd0bd3e-8213-498a-8723-939b7accac4d%40groups.riscv.org.
> On 5 Dec 2017, at 02:47, Jatin Bhateja <jatin....@gmail.com> wrote:
>
> GCC/LLVM's address sanitizer' ability to catch initialization ordering issues seems to be at your respite here, this can be enabled with ASAN_OPTIONS=check_initialization_order
I'm not sure I follow you.
the question is not how to build a custom toolchain, the question is what is the behaviour with the official toolchain, and if I can get the desired order.
regards,
Liviu
On Tuesday, December 5, 2017, <evan...@maximintegrated.com> wrote:Not sure this has to do with initialization issues.Outside of a bug in GCC, the answer is "No", volatile accesses can't be reordered with respect to each other across sequence points (different statements, basically). However, non-volatile access could be reordered across volatile accesses. To prevent this, you'd need a platform specific barrier instruction, or the following portable inline "assembly":__asm__ __volatile__("":::"memory");
Fences ensures that memory instructions gets committed to main memory from caches / store buffers, globals gets their initialization values either at the load time if constant or during static initialization which happens before entry to main. So if we can inject a barrier after each global in static initialization then off course we can prevent such ordering issues, but i am not aware if there is any such mechanism to access static initialization from a high level language. But compilers can do so while emitting code under a special mode.
--
This may be GCC specific and not part of ISO/ANSI C.
Here's a good answer on SO: https://stackoverflow.com/questions/22106843/gccs-reordering-of-read-write-instructions-Evan
On Monday, December 4, 2017 at 5:53:19 PM UTC-6, Liviu Ionescu wrote:(This is a question for Palmer and/or the other compiler gurus)
With a sequence like:
```c
volatile uint32_t array[2];
volatile uint32_t *p = array;
uint32_t v1 = *p;
uint32_t v2 = *(p+1);
```
Given the current ISO/ANSI specs, and the current behaviour of RISC-V GCC, are the last two instructions always executed in this order, or, for whatever reason, the compiler may reorder them, and the accesses are done in reverse order?
If the instructions may be reordered, is there any trick available (memory barriers, atomics, or anything else) to prevent this?
Same question for
```c
volatile uint64_t long_variable;
volatile uint32_t *p = (uint32_t*)&long_variable;
uint32_t v1 = *p;
uint32_t v2 = *(p+1);
```
In other words, is it possible, on a 32-bits platform, with the current RISC-V GCC, to find a way to always access the two halves of a 64-bits variable (or memory mapped register) in the desired order?
Thank you,
Liviu
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/2fd0bd3e-8213-498a-8723-939b7accac4d%40groups.riscv.org.
--
Jatin Bhateja
--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+unsubscribe@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/CAFyWVabStBuF6X3yoiDjC6OCGwKrvA8ygcreDqPoLtbtJgz7dg%40mail.gmail.com.
I know this is not the solution and only a hack, but what about wrapping the read in a function and then with __attribute force no optimalizations to avoid the compiler trying to improve it. Probably as the compiler evolves it still could break, so it won't be safe. The mentioned assembler would be safer
__attribute__((optimize("O0"))) long read(int addr) {
}