https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119280

            Bug ID: 119280
           Summary: Unexpected inline asm rdcycle code misplaced cause
                    wrong cycle calculation
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fanghuaqi at vip dot qq.com
  Target Milestone: ---

Hello, 

here is a case for riscv gcc -march=rv32imafc -mabi=ilp32f -O3

see https://godbolt.org/z/T9zGhhqGT

// test.c
#include <stdint.h>
#include <stdio.h>

void test(int32_t *a) {
    unsigned long begin_cycle, end_cycle;
    int32_t res;

    __asm volatile("rdcycle %0" : "=r"(begin_cycle) : : "memory");
    res = a[0] * a[0];
    __asm volatile("rdcycle %0" : "=r"(end_cycle) : : "memory");
    printf("acc: %d, cost %lu\n", res, end_cycle-begin_cycle);
}

gcc output

.LC0:
        .string "acc: %d, cost %lu\n"
test:
        rdcycle a5
        lw      a1,0(a0)
        rdcycle a2
        mul     a1,a1,a1
        lui     a0,%hi(.LC0)
        sub     a2,a2,a5
        addi    a0,a0,%lo(.LC0)
        tail    printf

llvm output

test:
        rdcycle a2
        lw      a0, 0(a0)
        rdcycle a3
        mul     a1, a0, a0
        sub     a2, a3, a2
        lui     a0, %hi(.L.str)
        addi    a0, a0, %lo(.L.str)
        tail    printf

.L.str:
        .asciz  "acc: %d, cost %lu\n"

.Ldebug_list_header_start0:
        .half   5
        .byte   4
        .byte   0
        .word   4
.Ldebug_list_header_end0:

As above, you can see mul is done after cycle read, but I am expecting to
calculate the cycle cost of mul, but it didn't.

Is this expected, if expected, how can I achieve a correct cycle calculation of
a piece of code.

Thanks in advance

Reply via email to