Блог пользователя Xiaohuba

Автор Xiaohuba, история, 20 месяцев назад, По-английски

UPD: It is a compiler bug, and is resolved since gcc 12.3. Further information is on https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116459.

[Apologize for my poor English.]

The code at https://godbolt.org/z/PP1TTdhxf outputs 1. However, if you simulate the code, the answer is obviously 20.

Further exploration showed that the compiler compiled function qpw, but did not call it. Uncomment line 22 solves the issue, surprisingly. Further more, the bug seems to only occur on gcc12.1 and gcc12.2, with -O2 enabled.

I believe the code does not contain any undefined / unspecified behavior.

  • Проголосовать: нравится
  • +15
  • Проголосовать: не нравится

»
20 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится -15 Проголосовать: не нравится

It was because when O2 enabled it does run faster however every variable you create if you don't have a value for it, it will be undefined instead of 0 like default. So therefore, you must define every variables before working on it. (So uncomment line 22 will actually solve the issue)

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It seems like the compiler used qpw function as a constexpr one to optimize the call out, but it is not marked as constexpr. Very strange, maybe it is a bug indeed.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I added an operator= function which works just fine. https://godbolt.org/z/vjb4vdczc

  • »
    »
    20 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, the original code does have undefined behavior.

    The default behavior of Matrix= is to copy the pointer mat, so the function qpw creates the matrix st * res on the stack, copies it's pointer into the global object, but then after the function executes the pointer dangles.

    I'm a bit surprised the compiler can do this. I'll investigate more.

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Xiaohuba (previous revision, new revision, compare).