toolchain icon indicating copy to clipboard operation
toolchain copied to clipboard

builtin-arith-overflow-12.c: Incorrect return value from `__builtin_mul_overflow`

Open luismgsilva opened this issue 1 year ago • 1 comments

In the latest branch arc-2024.06, an anomaly has been detected in a test case builtin-arith-overflow-12.c across multiple configurations — archs, em, em4_fpuda, arcem, and more.

Below is a simplified test case along with the execution steps:

$ cat test.c
#include <stdio.h>

__attribute__((noinline, noclone)) signed int t108_1mul (unsigned int x, unsigned int y) {
  signed int r;
  if (__builtin_mul_overflow (x, y, &r)) {
    printf("Overflow! - t108_1mul\n");
  }
  return r; 
}

int
main (void)
{
  unsigned int x = (0x7fffffff / 31);
  unsigned int y = (32);
  t108_1mul (x, y);

  return 0;
}
$ arc-elf32-gcc                                         \
        -fdiagnostics-plain-output                      \
        -O2 --specs=nsim.specs                          \
        -Wl,--defsym=__DEFAULT_HEAP_SIZE=256m           \
        -Wl,--defsym=__DEFAULT_STACK_SIZE=1024m         \
        -lm test.c -o ./test.x

$ qemu-system-arc                                       \
        -semihosting -cpu archs -M arc-sim              \
        -m 2G -nographic -no-reboot -monitor none       \
        -kernel ./test.x

The issue is that the __builtin_mul_overflow function is expected to return true for overflow but it does not, unlike in the arc-2023.09 branch.

By removing the noinline attribute, the internal function does return true for the overflow.

2023.09

         .file   "test.c"
        .cpu HS
        .arc_attribute Tag_ARC_PCS_config, 2
        .arc_attribute Tag_ARC_ABI_rf16, 0
        .arc_attribute Tag_ARC_ABI_pic, 0
        .arc_attribute Tag_ARC_ABI_tls, 0
        .arc_attribute Tag_ARC_ABI_sda, 2
        .arc_attribute Tag_ARC_ABI_exceptions, 0
        .arc_attribute Tag_ARC_CPU_variation, 2
        .section        .text
        .section        .rodata.str1.4,"aMS",@progbits,1
        .align 4
.LC0:
        .string "deu overflow! - t108_1mul"
        .section        .text
        .align 4
        .global t108_1mul
        .type   t108_1mul, @function
t108_1mul:
        push_s  blink
        push_s  r13
        mpymu   r5,r0,r1
        mpy     r4,r0,r1
        mov_s   r3,r5   ;4
        tst r4,r4
        setne r3, r3, 0
        mov_s   r12,1   ;3
        mov.p r12,r3
        mov_s   r3,r12  ;4
        tst_s r12,r12
        mov_s.ne        r0,@.LC0
        blne.d @puts;1
        mov_s   r13,r4  ;4
        mov_s   r0,r13  ;4
        ld      blink,[sp,4]    ;23
        j_s.d   [blink]
        ld.ab   r13,[sp,8]      ;23
        .size   t108_1mul, .-t108_1mul
        .section        .text.startup,"ax",@progbits
        .align 4
        .global main
        .type   main, @function
main:
        push_s  blink
        mov_s   r0,69273666     ;13
        bl.d @t108_1mul;1
        mov_s   r1,32   ;3
        mov_s   r0,0    ;3
        pop_s   blink
        j_s     [blink]
        .size   main, .-main
        .ident  "GCC: (GNU) 13.1.1 20230516"
        .section        .note.GNU-stack,"",@progbits

2024.06

        .file   "test.c"
        .cpu HS
        .arc_attribute Tag_ARC_PCS_config, 2
        .arc_attribute Tag_ARC_ABI_rf16, 0
        .arc_attribute Tag_ARC_ABI_pic, 0
        .arc_attribute Tag_ARC_ABI_tls, 0
        .arc_attribute Tag_ARC_ABI_sda, 2
        .arc_attribute Tag_ARC_ABI_exceptions, 0
        .arc_attribute Tag_ARC_CPU_variation, 2
        .section        .text
        .section        .rodata.str1.4,"aMS",@progbits,1
        .align 4
.LC0:
        .string "deu overflow! - t108_1mul"
        .section        .text
        .align 4
        .global t108_1mul
        .type   t108_1mul, @function
t108_1mul:
        push_s  blink
        push_s  r13
        mpy.f   0,r0,r1
        mpy     r2,r0,r1
        mpymu   r3,r0,r1
        mov.p   r2,r3
        mov.n   r2,1
        setne.p r2,r2,0
        tst_s   r2,r2
        mpy     r13,r0,r1
        mov_s.ne        r0,@.LC0
        blne    @puts;1
        mov_s   r0,r13  ;4
        ld      blink,[sp,4]    ;23
        j_s.d   [blink]
        ld.ab   r13,[sp,8]      ;23
        .size   t108_1mul, .-t108_1mul
        .section        .text.startup,"ax",@progbits
        .align 4
        .global main
        .type   main, @function
main:
        push_s  blink
        mov_s   r0,69273666     ;13
        bl.d    @t108_1mul;1
        mov_s   r1,32   ;3
        mov_s   r0,0    ;3
        pop_s   blink
        j_s     [blink]
        .size   main, .-main
        .ident  "GCC: (GNU) 14.1.0"
        .section        .note.GNU-stack,"",@progbits

Original issue: https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/issues/627

luismgsilva avatar Jun 28 '24 10:06 luismgsilva