Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msvc 2022 + /O1 cannot generate correct instructions #272

Open
waruqi opened this issue Jan 10, 2025 · 3 comments
Open

msvc 2022 + /O1 cannot generate correct instructions #272

waruqi opened this issue Jan 10, 2025 · 3 comments

Comments

@waruqi
Copy link
Member

waruqi commented Jan 10, 2025

When I use msvc 2022 and set the /O1 optimization option, the compiler generates wrong instructions, causing reading the file to always return 0.

https://developercommunity.visualstudio.com/t/Codegen-bug-on-x64-with-O1-optimize/10825899

#include <stdio.h>
#include <windows.h>

int main(int argc, char** argv) {
    HANDLE file = CreateFileW(L"D:/a.cpp", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (file != INVALID_HANDLE_VALUE) {
        
        DWORD read = 2; // set a initial value, just to verify the optimization results
        BYTE data[8196];
        __int64 r = ReadFile((HANDLE)file, data, (DWORD)sizeof(data), &read, NULL)? (__int64)read : -1;
        printf("Read: %d, toInt64: %lld r: %lld\n", read, (unsigned __int64)read, r);
        CloseHandle(file);
    }
    return 0;
}
target("testf")
    set_kind("binary")
    add_files("src/*.c")
    add_cxflags("/O1")
PS C:\Users\wangrunqing\Downloads\testf> xmake -rv
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.43.34618\bin\HostX64\x64\cl.exe
checking for Microsoft Visual Studio (x64) version ... 2022
checking for Microsoft C/C++ Compiler (x64) version ... 19.43.34618
checking for nim ... no
checking for nim ... no
checking for link.exe ... C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.43.34618\bin\HostX64\x64\link.exe
checking for the linker (ld) ... link.exe
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.43.34618\bin\HostX64\x64\cl.exe
checking for the c compiler (cc) ... cl.exe
checking for C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.43.34618\bin\HostX64\x64\cl.exe ... ok
checking for flags (/O1) ... ok
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.43.34618\bin\HostX64\x64\cl.exe
checking for the c++ compiler (cxx) ... cl.exe
[ 50%]: compiling.release src\main.c
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.43.34618\\bin\\HostX64\\x64\\cl.exe" -c -nologo /O1 -Fobuild\.objs\testf\windows\x64\release\src\main.c.obj src\main.c
checking for flags (cl_sourceDependencies) ... ok
[ 75%]: linking.release testf.exe
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.43.34618\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 -out:build\windows\x64\release\testf.exe build\.objs\testf\windows\x64\release\src\main.c.obj
[100%]: build ok, spent 0.907s
PS C:\Users\wangrunqing\Downloads\testf> xmake r
Read: 138, toInt64: 2 r: 2 <--- use incorrect initial value

tb_long_t tb_file_read(tb_file_ref_t file, tb_byte_t* data, tb_size_t size)
{
// check
tb_assert_and_check_return_val(file && data, -1);
tb_check_return_val(size, 0);
// read
DWORD real_size = 0;
return ReadFile((HANDLE)file, data, (DWORD)size, &real_size, tb_null)? (tb_long_t)real_size : -1;
}

tb_long_t -> __init64 here

@waruqi
Copy link
Member Author

waruqi commented Jan 10, 2025

tb_long_t tb_file_read(tb_file_ref_t file, tb_byte_t* data, tb_size_t size)
{
    // check
    tb_assert_and_check_return_val(file && data, -1);
    tb_check_return_val(size, 0);
    
    // read
    DWORD real_size = 2;
    __int64 r = ReadFile((HANDLE)file, data, (DWORD)size, &real_size, tb_null)? (__int64)real_size : -1;
    tb_trace_i("real: %d real64: %lld ret: %lld\n", real_size, (__int64)real_size, r);
    return r;
}
image image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


image image

@Juvwxyz
Copy link

Juvwxyz commented Jan 10, 2025

This bug has been reported to the Microsoft C++ team. https://developercommunity.visualstudio.com/t/Codegen-bug-on-x64-with-O1-optimize/10825899

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants