Compiler Optimization Causing Incorrect Results

  Kiến thức lập trình

I am doing development on a Zynq Ultrascale+ ARM core, and I am having a weird issue where compiler optimization being turned on causes a loop to be prematurely broken out of. The compiler is “aarch64-none-elf-g++”.

Here is the code that is code. Originally, the program was more complicated, but in debugging, I removed as much as I could while still reproducing the bug.

#include <cstdint>
#include <string>
#include <vector>

class TestClass
{
private:
    std::vector<int> command_buffer;
public:
    TestClass() : command_buffer({}) {}
    void read_command();
};

void TestClass::read_command()
{
    printf("Before %lunr", 67ul);

    size_t s = 42;
    while (true)
    {
        s = this->command_buffer.size();

        if (s > 0)
        {
            break;
        }
    }

    printf("Size %lu:nr", s);
}

int main()
{
    TestClass tester{ };
    tester.read_command();
}

Basically, I expect the program to loop continuously, checking the size of a vector until it is non-zero. Since I don’t ever add any elements to the vector, it should loop forever. When the optimization level is set to -O0, this works as expected. However, setting it to -O2 causes the program to break out of the loop immediately and print the size of the vector, which is 0.

The console output without optimization (-O0):

Before: 67

The console output with optimization (-O2):

Before: 67
After: 0

Looking at the assembly code generated by the compiler, it looks like the size of the vector is calculated but never checked, it is just assumed to be non-zero.

.LC0:
    .string "Before: %lunr"
    .align  3
.LC1:
    .string "After: %lunr"
    .text
    .align  2
    .p2align 4,,11
    .global _ZN9TestClass12read_commandEv
    .type   _ZN9TestClass12read_commandEv, %function
_ZN9TestClass12read_commandEv:
.LFB2157:
    .cfi_startproc
    stp x29, x30, [sp, -32]!
    .cfi_def_cfa_offset 32
    .cfi_offset 29, -32
    .cfi_offset 30, -24
    adrp    x1, .LC0
    mov x29, sp
    str x19, [sp, 16]
    .cfi_offset 19, -16
    mov x19, x0
    add x0, x1, :lo12:.LC0
    mov x1, 67
    bl  printf
    ldp x2, x1, [x19]
    adrp    x0, .LC1
    ldr x19, [sp, 16]
    add x0, x0, :lo12:.LC1
    ldp x29, x30, [sp], 32
    .cfi_restore 30
    .cfi_restore 29
    .cfi_restore 19
    .cfi_def_cfa_offset 0
    sub x1, x1, x2
    asr x1, x1, 2
    b   printf
    .cfi_endproc

Any ideas as to why this is happening?

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT