godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.26k stars 101 forks source link

Change SSE4.2 build flag on MSVC #12648

Open tanksdude opened 2 weeks ago

tanksdude commented 2 weeks ago

Describe the project you are working on

Godot's build system

Describe the problem or limitation you are having in your project

As of godotengine/godot#59595 and 4.5-dev-5, the SCons build system sets the x86_64 instruction level to SSE4.2. However, on MSVC this is done using the undocumented /d2archSSE42 flag, while there exists an official /arch:SSE4.2 flag.

/d2archSSE42 is mainly known through this StackOverflow post, and is not documented in Microsoft Learn.

/arch:SSE4.2 has been documented since around December 2024 (according to the Internet Archive), and added/known since November 2024.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Pros:

Cons:

/d2archSSE42 and /arch:SSE4.2 have identical outputs from elfx86exts. Surprisingly, they do output slightly different binaries. Using Windows's fc, here are the differences (built on 48f361a on latest MSVC):

> fc "godot-d2-undocumented.exe" "godot-arch-official.exe"
00000130: E8 4B
00000131: 26 2D
07EC7004: E8 4B
07EC7005: 26 2D
> fc "godot-d2-undocumented.console.exe" "godot-arch-official.console.exe"
00000110: EC 50
00000111: 26 2D
00020304: EC 50
00020305: 26 2D

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

In SConstruct, change this:

if env.msvc and not methods.using_clang(env):
    # https://stackoverflow.com/questions/64053597/how-do-i-enable-sse4-1-and-sse3-but-not-avx-in-msvc/69328426
    env.Append(CCFLAGS=["/d2archSSE42"])
else:
    # `-msse2` is implied when compiling for x86_64.
    env.Append(CCFLAGS=["-msse4.2"])

To this:

if env.msvc and not methods.using_clang(env):
    # https://learn.microsoft.com/en-us/cpp/build/reference/arch-x64?view=msvc-160
    env.Append(CCFLAGS=["/arch:SSE4.2"]) ### CHANGED HERE (and the comment above) ###
else:
    # `-msse2` is implied when compiling for x86_64.
    env.Append(CCFLAGS=["-msse4.2"])

(Note that the comment leads to the Visual Studio 2019 help page, instead of 2022, as Godot's minimum is 2019.)

If this enhancement will not be used often, can it be worked around with a few lines of script?

No

Is there a reason why this should be core and not an add-on in the asset library?

Buildsystem

Calinou commented 2 weeks ago

We still support MSVC 2019 (and outdated MSVC 2022) for now, so I wouldn't use the new flag until a new MSVC version comes out (at which point it's reasonable to drop MSVC 2019 support).

tanksdude commented 2 weeks ago

It seems I could have explained that part better: MSVC 2019 does support /arch:SSE4.2, according to the official docs (I did not test that myself), however versions of Visual Studio (2019, 2022, and apparently 2015 & 2017) before November 2024 do not.