Fix GCC 5.0.0 build errors
cover Fix GCC 5.0.0 build errors
GCC is more strict now.
Radim Krčmář (2):
fix GCC 5.0.0 logical-not-parentheses warnings
milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
hw/misc/milkymist-pfpu.c | 18 ++++++++---------- hw/net/virtio-net.c | 2 +- kvm-all.c | 2 +- qemu-img.c | 3 ++- 4 files changed, 12 insertions(+), 13 deletions(-)
Signature · 1 line
2.3.0
PATCH 1/2 fix GCC 5.0.0 logical-not-parentheses warnings
man gcc:
Warn about logical not used on the left hand side operand of a comparison. This option does not warn if the RHS operand is of a boolean type.
By preferring bool over int where sensible, but without modifying any depending code, make GCC happy in cases like this,
qemu-img.c: In function ‘compare_sectors’:
qemu-img.c:992:39: error: logical not is only applied to the left hand side of comparison [-Werror=logical-not-parentheses]
if (!!memcmp(buf1, buf2, 512) != res) {hw/ide/core.c:1836 doesn't throw an error,
assert(!!s->error == !!(s->status & ERR_STAT));
even thought the second operand is int (and first hunk of this patch has a very similar case), maybe GCC developers still have a little faith in C programmers.
--- hw/net/virtio-net.c | 2 +- kvm-all.c | 2 +- qemu-img.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +1 -1
@@ -120,7 +120,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)return ; } -if (!!n->vhost_started == +if ((!!n->vhost_started) == (virtio_net_started (n, status) && !nc->peer->link_down)) {return ; }
kvm-all.c +1 -1
@@ -366,7 +366,7 @@ static void kvm_log_stop(MemoryListener *listener, } } -static int kvm_set_migration_log (int enable) +static int kvm_set_migration_log (bool enable) {KVMState *s = kvm_state;KVMSlot *mem;
qemu-img.c +2 -1
@@ -977,7 +977,8 @@ static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,static int compare_sectors (const uint8_t *buf1,const uint8_t *buf2,int n,int *pnum) { -int res, i; +bool res; +int i;if (n <=0 ) { *pnum =0 ;
Signature · 1 line
2.3.0
Re: [Qemu-devel] [PATCH 1/2] fix GCC 5.0.0 logical-not-parentheses warnings
Quoted 30 lines · Radim Krčmář
man gcc:
Warn about logical not used on the left hand side operand of a comparison. This option does not warn if the RHS operand is of a boolean type.By preferring bool over int where sensible, but without modifying any depending code, make GCC happy in cases like this,
qemu-img.c: In function ‘compare_sectors’:
qemu-img.c:992:39: error: logical not is only applied to the left hand side of comparison [-Werror=logical-not-parentheses]if (!!memcmp(buf1, buf2, 512) != res) {
hw/ide/core.c:1836doesn't throw an error,
assert(!!s->error == !!(s->status & ERR_STAT));
even thought the second operand is int (and first hunk of this patch has a very similar case), maybe GCC developers still have a little faith in C programmers.Signed-off-by Radim Krčmář <[email protected]>--- hw/net/virtio-net.c | 2 +- kvm-all.c | 2 +- qemu-img.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)hw/net/virtio-net.c +0
-0
hw/net/virtio-net.c static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) · line 124
Quoted 6 lines
hw/net/virtio-net.c +1
-1@@ -120,7 +120,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)return ; } -if (!!n->vhost_started == +if ((!!n->vhost_started) == (virtio_net_started (n, status) && !nc->peer->link_down)) {
Does it still break if you just swap the terms?
Paolo
Quoted 30 lines
hw/net/virtio-net.c +0
-0return ; }kvm-all.c +1
-1@@ -366,7 +366,7 @@ static void kvm_log_stop(MemoryListener *listener, } } -static int kvm_set_migration_log (int enable) +static int kvm_set_migration_log (bool enable) {KVMState *s = kvm_state;KVMSlot *mem;qemu-img.c +2
-1@@ -977,7 +977,8 @@ static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,static int compare_sectors (const uint8_t *buf1,const uint8_t *buf2,int n,int *pnum) { -int res, i; +bool res; +int i;if (n <=0 ) { *pnum =0 ;
Re: [Qemu-devel] [PATCH 1/2] fix GCC 5.0.0 logical-not-parentheses warnings
2015-02-20 15:38+0100, Paolo Bonzini:
Quoted 6 lines · Radim Krčmář
hw/net/virtio-net.c static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) · line 124
Quoted 3 lines
hw/net/virtio-net.c +1
-1-if (!!n->vhost_started == +if ((!!n->vhost_started) == (virtio_net_started (n, status) && !nc->peer->link_down)) {Does it still break if you just swap the terms?
No (there is no logical not on left side then), will do that.
PATCH 2/2 milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
In reply to Radim Krčmářman gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.
Refactored the code a bit to avoid the GCC warning, in an objectionable
way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be reached after undefined behavior [-Werror=aggressive-loop-optimizations]
if (i++ >= MICROCODE_WORDS) {
^
hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here
uint32_t insn = s->microcode[pc];
^--- hw/misc/milkymist-pfpu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
hw/misc/milkymist-pfpu.c +8 -10
@@ -164,6 +164,13 @@ output_queue_advance(MilkymistPFPUState *s)static int pfpu_decode_insn (MilkymistPFPUState *s) {uint32_t pc = s->regs[R_PC ]; + +if (pc >MICROCODE_WORDS ) { +error_report ("milkymist_pfpu: too many instructions " +"executed in microcode. No VECTOUT?" ); +return 0 ; + } +uint32_t insn = s->microcode[pc];uint32_t reg_a = (insn >>18 ) &0x7f ;uint32_t reg_b = (insn >>11 ) &0x7f ;
@@ -348,7 +355,6 @@ static int pfpu_decode_insn(MilkymistPFPUState *s)static void pfpu_start (MilkymistPFPUState *s) {int x, y; -int i;for (y =0 ; y <= s->regs[R_VMESHLAST ]; y++) {for (x =0 ; x <= s->regs[R_HMESHLAST ]; x++) {
@@ -359,15 +365,7 @@ static void pfpu_start(MilkymistPFPUState *s) s->gp_regs[GPR_Y ] = y;/* run microcode on this position */ - i =0 ; -while (pfpu_decode_insn (s)) { -/* decode at most MICROCODE_WORDS instructions */ -if (i++ >=MICROCODE_WORDS ) { -error_report ("milkymist_pfpu: too many instructions " -"executed in microcode. No VECTOUT?" ); -break ; - } - } +while (pfpu_decode_insn (s));/* reset pc for next run */ s->regs[R_PC ] =0 ;
Signature · 1 line
2.3.0
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
2015-02-20 15:18+0100, Radim Krčmář:
hw/misc/milkymist-pfpu.c output_queue_advance(MilkymistPFPUState *s) · line 168
Quoted 7 lines
hw/misc/milkymist-pfpu.c +2
-0@@ -164,6 +164,13 @@ output_queue_advance(MilkymistPFPUState *s)static int pfpu_decode_insn (MilkymistPFPUState *s) {uint32_t pc = s->regs[R_PC ]; + +if (pc >MICROCODE_WORDS ) {
Ugh, should have been '>=' here.
Quoted 6 lines
hw/misc/milkymist-pfpu.c +5
-0+error_report ("milkymist_pfpu: too many instructions " +"executed in microcode. No VECTOUT?" ); +return 0 ; + } +uint32_t insn = s->microcode[pc];
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
In reply to Radim KrčmářQuoted 47 lines · Radim Krčmář
man gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.Refactored the code a bit to avoid the GCC warning, in an objectionable
way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be reached after undefined behavior [-Werror=aggressive-loop-optimizations]if (i++ >= MICROCODE_WORDS) { ^ hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here uint32_t insn = s->microcode[pc]; ^Signed-off-by Radim Krčmář <[email protected]>--- hw/misc/milkymist-pfpu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)hw/misc/milkymist-pfpu.c +7
-1@@ -164,6 +164,13 @@ output_queue_advance(MilkymistPFPUState *s)static int pfpu_decode_insn (MilkymistPFPUState *s) {uint32_t pc = s->regs[R_PC ]; + +if (pc >MICROCODE_WORDS ) { +error_report ("milkymist_pfpu: too many instructions " +"executed in microcode. No VECTOUT?" ); +return 0 ; + } +uint32_t insn = s->microcode[pc];uint32_t reg_a = (insn >>18 ) &0x7f ;uint32_t reg_b = (insn >>11 ) &0x7f ;@@ -348,7 +355,6 @@ static int pfpu_decode_insn(MilkymistPFPUState *s)static void pfpu_start (MilkymistPFPUState *s) {int x, y; -int i;for (y =0 ; y <= s->regs[R_VMESHLAST ]; y++) {for (x =0 ; x <= s->regs[R_HMESHLAST ]; x++) {
hw/misc/milkymist-pfpu.c static void pfpu_start(MilkymistPFPUState *s) · line 365
Quoted 7 lines
hw/misc/milkymist-pfpu.c +0
-4@@ -359,15 +365,7 @@ static void pfpu_start(MilkymistPFPUState *s) s->gp_regs[GPR_Y ] = y;/* run microcode on this position */ - i =0 ; -while (pfpu_decode_insn (s)) { -/* decode at most MICROCODE_WORDS instructions */ -if (i++ >=MICROCODE_WORDS ) {
Isn't the fix just to say "++i" instead of "i++"?
Paolo
Quoted 10 lines
hw/misc/milkymist-pfpu.c +1
-5-error_report ("milkymist_pfpu: too many instructions " -"executed in microcode. No VECTOUT?" ); -break ; - } - } +while (pfpu_decode_insn (s));/* reset pc for next run */ s->regs[R_PC ] =0 ;
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
Am 2015-02-20 15:36, schrieb Paolo Bonzini:
Quoted 23 lines · Radim Krčmář
Quoted 22 lines
man gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.Refactored the code a bit to avoid the GCC warning, in an objectionable way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be
reached after undefined behavior [-Werror=aggressive-loop-optimizations]if (i++ >= MICROCODE_WORDS) { ^ hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here uint32_t insn = s->microcode[pc]; ^Signed-off-by Radim Krčmář <[email protected]>--- hw/misc/milkymist-pfpu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
Quoted 15 lines
Quoted 5 lines
hw/misc/milkymist-pfpu.c +0
-0hw/misc/milkymist-pfpu.c output_queue_advance(MilkymistPFPUState *s) · line 173
Quoted 10 lines
hw/misc/milkymist-pfpu.c +7
-0@@ -164,6 +164,13 @@ output_queue_advance(MilkymistPFPUState *s)static int pfpu_decode_insn (MilkymistPFPUState *s) {uint32_t pc = s->regs[R_PC ]; + +if (pc >MICROCODE_WORDS ) { +error_report ("milkymist_pfpu: too many instructions " +"executed in microcode. No VECTOUT?" ); +return 0 ; + } +
I don't like this syntax, eg declaration, statements, declaration. Can you just declare the variable first and then assign them? Also the error message is then misleading. I'd prefer something like "milkymist_pfpu: program counter out of bounds. No VECTOUT?"
Quoted 14 lines
Quoted 14 lines
hw/misc/milkymist-pfpu.c +0
-1uint32_t insn = s->microcode[pc];uint32_t reg_a = (insn >>18 ) &0x7f ;uint32_t reg_b = (insn >>11 ) &0x7f ;@@ -348,7 +355,6 @@ static int pfpu_decode_insn(MilkymistPFPUState *s)static void pfpu_start (MilkymistPFPUState *s) {int x, y; -int i;for (y =0 ; y <= s->regs[R_VMESHLAST ]; y++) {for (x =0 ; x <= s->regs[R_HMESHLAST ]; x++) {@@ -359,15 +365,7 @@ static void pfpu_start(MilkymistPFPUState *s) s->gp_regs[GPR_Y ] = y;
Quoted 7 lines
hw/misc/milkymist-pfpu.c static void pfpu_start(MilkymistPFPUState *s) · line 365
Quoted 5 lines
hw/misc/milkymist-pfpu.c +0
-4/* run microcode on this position */ - i =0 ; -while (pfpu_decode_insn (s)) { -/* decode at most MICROCODE_WORDS instructions */ -if (i++ >=MICROCODE_WORDS ) {Isn't the fix just to say "++i" instead of "i++"?
In the first run, s->regs[R_PC] may have any value, therefore the "insn = s->microcode[pc]" from above may access out of bounds.
-michael
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
Quoted 10 lines · Michael Walle
Quoted 7 lines
hw/misc/milkymist-pfpu.c static void pfpu_start(MilkymistPFPUState *s) · line 365
Quoted 5 lines
hw/misc/milkymist-pfpu.c +0
-4- i =0 ; -while (pfpu_decode_insn (s)) { -/* decode at most MICROCODE_WORDS instructions */ -if (i++ >=MICROCODE_WORDS ) {Isn't the fix just to say "++i" instead of "i++"?
In the first run, s->regs[R_PC] may have any value, therefore the "insn = s->microcode[pc]" from above may access out of bounds.
Then should pfpu_decode_insn access s->microcode[pc & (MICROCODE_WORDS - 1)]? That's likely what happens in hardware, and the purpose of the error is just to avoid an infinite loop in device code.
Paolo
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
2015-02-20 15:55+0100, Paolo Bonzini:
Quoted 9 lines · Michael Walle
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/misc/milkymist-pfpu.c +0
-4- i =0 ; -while (pfpu_decode_insn (s)) { -/* decode at most MICROCODE_WORDS instructions */ -if (i++ >=MICROCODE_WORDS ) {
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Isn't the fix just to say "++i" instead of "i++"?
In the first run, s->regs[R_PC] may have any value, therefore the "insn = s->microcode[pc]" from above may access out of bounds.
Then should
pfpu_decode_insnaccess s->microcode[pc & (MICROCODE_WORDS- 1)]? That's likely what happens in hardware, and the purpose of the error is just to avoid an infinite loop in device code.
http://www.milkymist.org/socdoc/pfpu.pdf is dead, but the source isn't:
https://github.com/m-labs/milkymist/blob/master/cores/pfpu/doc/pfpu.tex
I don't see the PC register mentioned in interface, so hiding it would probably be a good start.
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
In reply to Radim KrčmářQuoted 9 lines · Radim Krčmář
man gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.Refactored the code a bit to avoid the GCC warning, in an objectionable
way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be reached after undefined behavior [-Werror=aggressive-loop-optimizations]
Quoted 5 lines
if (i++ >= MICROCODE_WORDS) {
^
hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here
uint32_t insn = s->microcode[pc];
^Why can't we just fix this by fixing the loop boundary condition, which is the actual bug here? There should be no need to move the check into the function (where it does not belong).
(We try to stop before overflowing the s->microcode[] array, but because 'i++' is a post increment and we do a >= comparison, the last loop round will try to write to s->microcode[MICROCODE_WORDS]. Easiest fix is to use "++i" I suppose, though it might be better to just separate the increment and the conditional instead.)
There is probably some actual hardware behaviour we're failing to model correctly here, since it's a safe bet the h/w doesn't print an error message in this situation. However we should just fix the bogus array handling for now.
-- PMM
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
2015-02-20 23:40+0900, Peter Maydell:
Quoted 16 lines · Radim Krčmář
Quoted 9 lines
man gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.Refactored the code a bit to avoid the GCC warning, in an objectionable
way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be reached after undefined behavior [-Werror=aggressive-loop-optimizations]Quoted 5 lines
if (i++ >= MICROCODE_WORDS) { ^ hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here uint32_t insn = s->microcode[pc]; ^
Quoted 4 lines
Why can't we just fix this by fixing the loop boundary condition, which is the actual bug here? There should be no need to move the check into the function (where it does not belong).
It would work now, but GCC could get more intelligent with time and realize that there still can be undefined behavior ...
Quoted 11 lines
(We try to stop before overflowing the s->microcode[] array, but because 'i++' is a post increment and we do a >= comparison, the last loop round will try to write to s->microcode[MICROCODE_WORDS]. Easiest fix is to use "++i" I suppose, though it might be better to just separate the increment and the conditional instead.)
There is probably some actual hardware behaviour we're failing to model correctly here, since it's a safe bet the h/w doesn't print an error message in this situation. However we should just fix the bogus array handling for now.
Ok, I will do just ++i for v2 and keep the other bug for later.
Re: [Qemu-devel] [PATCH 2/2] milkymist-pfpu: fix GCC 5.0.0 aggressive-loop-optimizations warning
Am 2015-02-20 16:37, schrieb Radim Krčmář:
Quoted 32 lines
2015-02-20 23:40+0900, Peter Maydell:
Quoted 16 lines · Radim Krčmář
Quoted 9 lines
man gcc:
Warn if in a loop with constant number of iterations the compiler detects undefined behavior in some statement during one or more of the iterations.Refactored the code a bit to avoid the GCC warning, in an objectionable
way,
hw/misc/milkymist-pfpu.c: In function ‘pfpu_write’:
hw/misc/milkymist-pfpu.c:365:20: error: loop exit may only be reached after undefined behavior [-Werror=aggressive-loop-optimizations]Quoted 5 lines
if (i++ >= MICROCODE_WORDS) { ^ hw/misc/milkymist-pfpu.c:167:14: note: possible undefined statement is here uint32_t insn = s->microcode[pc]; ^Quoted 4 lines
Why can't we just fix this by fixing the loop boundary condition, which is the actual bug here? There should be no need to move the check into the function (where it does not belong).
It would work now, but GCC could get more intelligent with time and realize that there still can be undefined behavior ...
Quoted 7 lines
(We try to stop before overflowing the s->microcode[] array, but because 'i++' is a post increment and we do a >= comparison, the last loop round will try to write to s->microcode[MICROCODE_WORDS]. Easiest fix is to use "++i" I suppose, though it might be better to just separate the increment and the conditional instead.)
Quoted 6 lines
Quoted 4 lines
There is probably some actual hardware behaviour we're failing to model correctly here, since it's a safe bet the h/w doesn't print an error message in this situation. However we should just fix the bogus array handling for now.
Ok, I will do just ++i for v2 and keep the other bug for later.
Ok, i'll post a patch later.
-michael