Bug 196488

Summary: [CMake] Detect SSE2 at compile time
Product: WebKit Reporter: Xan Lopez <xan.lopez>
Component: CMakeAssignee: Xan Lopez <xan.lopez>
Status: RESOLVED FIXED    
Severity: Normal CC: annulen, cgarcia, commit-queue, ews-watchlist, karogyoker2+webkit, mcatanzaro, psaavedra, xan.lopez
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=223987
Attachments:
Description Flags
Detect SSE2 with CMake
none
Detect SSE2 with CMake
none
Detect SSE2 with CMake
none
Detect SSE2 with CMake
none
Detect SSE2 with CMake
none
Detect SSE2 with CMake
ews-watchlist: commit-queue-
Archive of layout-test-results from ews213 for win-future
none
Detect SSE2 with CMake
ews-watchlist: commit-queue-
Archive of layout-test-results from ews210 for win-future
none
Detect SSE2 with CMake
ews-watchlist: commit-queue-
Archive of layout-test-results from ews213 for win-future
none
Detect SSE2 with CMake
none
Detect SSE2 with CMake
none
Archive of layout-test-results from ews210 for win-future none

Description Xan Lopez 2019-04-02 03:49:15 PDT
This gets rid of a (bogus) static_assert, inert until now because of a typo fixed in bug #196204, and adds a simple CMake macro to detect SSE2 support.
Comment 1 Xan Lopez 2019-04-02 03:51:53 PDT
Created attachment 366483 [details]
Detect SSE2 with CMake
Comment 2 EWS Watchlist 2019-04-02 03:54:08 PDT
Attachment 366483 [details] did not pass style-queue:


ERROR: Source/cmake/FindSSE2.cmake:48:  No space between command "return" and its parentheses, should be "return("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:52:  One space between command "if" and its parentheses, should be "if ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:53:  One space between command "if" and its parentheses, should be "if ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:55:  One space between command "endif" and its parentheses, should be "endif ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:56:  One space between command "elseif" and its parentheses, should be "elseif ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:57:  One space between command "if" and its parentheses, should be "if ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:60:  One space between command "endif" and its parentheses, should be "endif ("  [whitespace/parentheses] [5]
ERROR: Source/cmake/FindSSE2.cmake:61:  One space between command "endif" and its parentheses, should be "endif ("  [whitespace/parentheses] [5]
Total errors found: 8 in 4 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Xan Lopez 2019-04-02 04:13:36 PDT
Created attachment 366485 [details]
Detect SSE2 with CMake
Comment 4 Xan Lopez 2019-04-02 06:37:43 PDT
Created attachment 366492 [details]
Detect SSE2 with CMake
Comment 5 Michael Catanzaro 2019-04-02 06:38:04 PDT
Comment on attachment 366485 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=366485&action=review

> CMakeLists.txt:123
> +#---------------------------
> +# Make sure SSE2 is present.
> +#---------------------------
> +include(FindSSE2)
> +if (NOT SSE2_SUPPORT_FOUND)
> +    message(FATAL_ERROR "SSE2 support is required to compile WebKit")
> +endif ()

MIPS bot is failing. This should be guarded by if (WTF_CPU_X86) right?
Comment 6 Xan Lopez 2019-04-02 07:42:57 PDT
(In reply to Michael Catanzaro from comment #5)
> 
> MIPS bot is failing. This should be guarded by if (WTF_CPU_X86) right?

Yeah, this was already fixed in a new patch but forgot to mark the previous one as obsolete.
Comment 7 WebKit Commit Bot 2019-04-08 03:16:27 PDT
Comment on attachment 366492 [details]
Detect SSE2 with CMake

Clearing flags on attachment: 366492

Committed r243989: <https://trac.webkit.org/changeset/243989>
Comment 8 WebKit Commit Bot 2019-04-08 03:16:29 PDT
All reviewed patches have been landed.  Closing bug.
Comment 9 Michael Catanzaro 2019-04-10 11:25:06 PDT
Something seems to be going wrong with the CMake check and the CMake build system is not adding required flags for SSE2, so we'll need to try again.

There is more discussion in: https://gitlab.gnome.org/GNOME/gnome-build-meta/merge_requests/277
Comment 10 Michael Catanzaro 2019-04-10 11:27:30 PDT
Reverted r243989 for reason:

Broke i686 builds

Committed r244138: <https://trac.webkit.org/changeset/244138>
Comment 11 Konstantin Tokarev 2019-04-10 11:41:16 PDT
Comment on attachment 366492 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=366492&action=review

> CMakeLists.txt:123
> +        message(FATAL_ERROR "SSE2 support is required to compile WebKit")

This is wrong, we should switch to CLoop if SSE2 is not supported, otherwise add -msse2 to cflags
Comment 12 Xan Lopez 2019-04-10 11:54:44 PDT
(In reply to Konstantin Tokarev from comment #11)
> Comment on attachment 366492 [details]
> Detect SSE2 with CMake
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=366492&action=review
> 
> > CMakeLists.txt:123
> > +        message(FATAL_ERROR "SSE2 support is required to compile WebKit")
> 
> This is wrong, we should switch to CLoop if SSE2 is not supported, otherwise
> add -msse2 to cflags

The flags are added in webkitdirs.pm. We are effectively requriring SSE2 to run WebKit now (see bug #194853 and the thread in webkit-dev linked from there). I think Michael's build error comes from the fact that distros do not use webkitdirs.pm, so they should be adding the SSE2 flags in their non x86-64 builds? Unless we want to do this ourselves somewhere else?
Comment 13 Xan Lopez 2019-04-10 11:58:47 PDT
(In reply to Xan Lopez from comment #12)
> The flags are added in webkitdirs.pm. We are effectively requriring SSE2 to
> run WebKit now (see bug #194853 and the thread in webkit-dev linked from
> there). I think Michael's build error comes from the fact that distros do
> not use webkitdirs.pm, so they should be adding the SSE2 flags in their non
> x86-64 builds? Unless we want to do this ourselves somewhere else?

Seems WebkitCompilerFlags.cmake could be a good place to add the flags for x86, so I'll re-upload the patch with this tomorrow.
Comment 14 Konstantin Tokarev 2019-04-10 12:01:57 PDT
(In reply to Xan Lopez from comment #12)
> (In reply to Konstantin Tokarev from comment #11)
> > Comment on attachment 366492 [details]
> > Detect SSE2 with CMake
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=366492&action=review
> > 
> > > CMakeLists.txt:123
> > > +        message(FATAL_ERROR "SSE2 support is required to compile WebKit")
> > 
> > This is wrong, we should switch to CLoop if SSE2 is not supported, otherwise
> > add -msse2 to cflags
> 
> The flags are added in webkitdirs.pm. We are effectively requriring SSE2 to
> run WebKit now (see bug #194853 and the thread in webkit-dev linked from
> there). 

My point is that we should't require SSE2 because we don't have to use JIT. Or this check should be guarget by if(ENABLE_JIT AND WTF_CPU_X86), which may align better with our policy of failing eagerly instead of degrading features silently based on system detection
Comment 15 Konstantin Tokarev 2019-04-10 12:02:58 PDT
Distros indeed don't use webkitdirs.pm, AFAIK build-webkit and related files aren't even included into tarball
Comment 16 Xan Lopez 2019-04-10 12:06:16 PDT
(In reply to Konstantin Tokarev from comment #14)
> My point is that we should't require SSE2 because we don't have to use JIT.
> Or this check should be guarget by if(ENABLE_JIT AND WTF_CPU_X86), which may
> align better with our policy of failing eagerly instead of degrading
> features silently based on system detection

This is also relevant for LLInt, even if you use CLoop, because the compiler flags you use to build LLInt/CLoop matter. Compiling it without SSE2 will effectively introduce "bugs" related to floating point precision.

In any case computers that do not have SSE2 capabilities will almost certainly not be able to run a current WebKit, I'd say. We are talking about almost 20 years old hardware.

Feel free to re-open the thread in webkit-dev if you want though, I think it's better to have the conversation there.
Comment 17 Konstantin Tokarev 2019-04-10 12:11:02 PDT
>Compiling it without SSE2 will effectively introduce "bugs" related to floating point precision.

Precision of x87 calculations is more or equal than SSE2 because the former uses 80-bit registers and the latter - 64-bit. However, if we dig down that deep we will inevitably come to conclusion that all non-x86 platforms are all "buggy" (no)
Comment 18 Xan Lopez 2019-04-10 12:14:36 PDT
(In reply to Konstantin Tokarev from comment #17)
> >Compiling it without SSE2 will effectively introduce "bugs" related to floating point precision.
> 
> Precision of x87 calculations is more or equal than SSE2 because the former
> uses 80-bit registers and the latter - 64-bit. However, if we dig down that
> deep we will inevitably come to conclusion that all non-x86 platforms are
> all "buggy" (no)

That's why I wrote "bugs", not bugs. The issue is that those differences do add up, and differences in rounding lead to strange failures that take a long time to figure out. So I proposed to just use SSE2 everywhere, which is what Firefox and Chrome are already doing, and save ourselves some time in the future. In any case, again, feel free to comment on the thread, I did make a public RFC in webkit-dev.
Comment 19 karogyoker2+webkit 2019-04-19 11:49:41 PDT
(In reply to Xan Lopez from comment #16)
> In any case computers that do not have SSE2 capabilities will almost
> certainly not be able to run a current WebKit, I'd say. We are talking about
> almost 20 years old hardware.
> 

Well, I was using WebKit until now, but since the latest release it is broken and I cannot start my browser at all (GNOME Web (epiphany-browser)).

I have an Athlon XP running Lubuntu 18 and I was using WebKit. I had to fix WebKit last year because it required SSE2: https://bugs.webkit.org/show_bug.cgi?id=188145

Since my fix I was using WebKit, but now I'm unable to use it again.

My computer is not older than 20 years, the CPU got onto the market in December 2004, so it is not even 15 years old yet.

Every other program I use on Linux runs fine without SSE2 (VLC, LibreOffice, whatever else). Isn't there an other solution than making SSE2 a requirement?

I've chosen WebKit because all the other browsers are requiring SSE2. If I cannot use WebKit then the latest browser with the most features is NetSurf compiled from git sources. It doesn't even capable of handling simpler JavaScripts in practice (there is only theoretic support). In contrast, with WebKit I could even watch YouTube videos.
Comment 20 Pablo Saavedra 2019-05-01 01:26:49 PDT
The removed `isSSE2Present()`function is still present in assertions  included in MacroAssemblerX86Common.h.


Those assertions are reached trying to compile WPE for PC  using gcc 7.3.0:



```
In file included from DerivedSources/ForwardingHeaders/wtf/StdLibExtras.h:32:0,
                 from DerivedSources/ForwardingHeaders/wtf/FastMalloc.h:25,
                 from ../Source/JavaScriptCore/config.h:37,
                 from ../Source/JavaScriptCore/llint/LLIntOffsetsExtractor.cpp:26:
../Source/JavaScriptCore/assembler/MacroAssemblerX86Common.h: In member function ‘void JSC::MacroAssemblerX86Common::moveDouble(JSC::AbstractMacroAssembler<JSC::X86Assembler>::FPRegisterID, JSC::AbstractMacroAssembler<JSC::X86Assembler>::FPRegisterID)’:
../Source/JavaScriptCore/assembler/MacroAssemblerX86Common.h:1458:16: error: ‘isSSE2Present’ was not declared in this scope
         ASSERT(isSSE2Present());
                ^
...
```
Comment 21 Michael Catanzaro 2019-05-01 07:20:26 PDT
This commit was rolled out. I guess I'll add the rollout to the backports list for 2.24.
Comment 22 Xan Lopez 2019-05-03 07:54:25 PDT
Created attachment 368924 [details]
Detect SSE2 with CMake

New patch.

The SSE2 flags are now added in WebKitCompilerFlags.cmake. We try to detect SSE2 right after (with the flags in place). webkitdirs.pm has been modified to not do this anymore, since it would be redundant.

This works locally, but if Michael or someone could give it a shot before pushing again that would be nice.
Comment 23 Xan Lopez 2019-05-03 08:00:41 PDT
Created attachment 368925 [details]
Detect SSE2 with CMake

And now make it apply to ToT.
Comment 24 EWS Watchlist 2019-05-03 08:02:03 PDT
Attachment 368925 [details] did not pass style-queue:


ERROR: Tools/ChangeLog:1:  ChangeLog entry has no bug number  [changelog/bugnumber] [5]
ERROR: ChangeLog:1:  ChangeLog entry has no bug number  [changelog/bugnumber] [5]
ERROR: Source/JavaScriptCore/ChangeLog:1:  ChangeLog entry has no bug number  [changelog/bugnumber] [5]
Total errors found: 3 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 25 Xan Lopez 2019-05-03 08:04:36 PDT
Created attachment 368926 [details]
Detect SSE2 with CMake

Style fix.
Comment 26 Konstantin Tokarev 2019-05-03 11:40:42 PDT
Comment on attachment 368926 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=368926&action=review

> Source/cmake/FindSSE2.cmake:52
> +    if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)

Should be if (COMPILER_IS_GCC_OR_CLANG)

> Source/cmake/WebKitCompilerFlags.cmake:149
> +    if (WTF_CPU_X86 AND NOT WIN32)

Should be if (WTF_CPU_X86 AND COMPILER_IS_GCC_OR_CLANG)
Comment 27 Michael Catanzaro 2019-05-03 15:03:52 PDT
Comment on attachment 368926 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=368926&action=review

> Source/cmake/WebKitCompilerFlags.cmake:150
> +        WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-march=pentium4 -msse2 -mfpmath=sse)

Is -march=pentium4 required? I think distros usually use -march=generic.
Comment 28 EWS Watchlist 2019-05-03 16:49:12 PDT Comment hidden (obsolete)
Comment 29 EWS Watchlist 2019-05-03 16:49:14 PDT Comment hidden (obsolete)
Comment 30 Konstantin Tokarev 2019-05-04 06:02:22 PDT
Comment on attachment 368926 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=368926&action=review

>> Source/cmake/WebKitCompilerFlags.cmake:150
>> +        WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-march=pentium4 -msse2 -mfpmath=sse)
> 
> Is -march=pentium4 required? I think distros usually use -march=generic.

It is not required, and may introduce a regression if someone uses toolchain with custom default -march and relies on that default value
Comment 31 Xan Lopez 2019-05-05 09:22:11 PDT
(In reply to Michael Catanzaro from comment #27)
> Comment on attachment 368926 [details]
> Detect SSE2 with CMake
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=368926&action=review
> 
> > Source/cmake/WebKitCompilerFlags.cmake:150
> > +        WEBKIT_PREPEND_GLOBAL_COMPILER_FLAGS(-march=pentium4 -msse2 -mfpmath=sse)
> 
> Is -march=pentium4 required? I think distros usually use -march=generic.

-march=generic does not exist, and -mtune does not make sense here. That line has been around forever, but I assume the intent was to explicitly limit the available instruction set. I'll leave it out.
Comment 32 Xan Lopez 2019-05-05 09:23:07 PDT
(In reply to Konstantin Tokarev from comment #26)
> 
> > Source/cmake/WebKitCompilerFlags.cmake:149
> > +    if (WTF_CPU_X86 AND NOT WIN32)
> 
> Should be if (WTF_CPU_X86 AND COMPILER_IS_GCC_OR_CLANG)

The whole block is already inside an if (COMPILER_IS_GCC_OR_CLANG), so I assume WIN32 is not necessary?
Comment 33 Xan Lopez 2019-05-05 09:23:25 PDT
Created attachment 369092 [details]
Detect SSE2 with CMake

New patch.
Comment 34 EWS Watchlist 2019-05-05 11:31:16 PDT Comment hidden (obsolete)
Comment 35 EWS Watchlist 2019-05-05 11:31:18 PDT Comment hidden (obsolete)
Comment 36 Konstantin Tokarev 2019-05-05 16:05:15 PDT
Comment on attachment 369092 [details]
Detect SSE2 with CMake

View in context: https://bugs.webkit.org/attachment.cgi?id=369092&action=review

> Source/cmake/FindSSE2.cmake:50
> +        HAVE_SSE2_EXTENSIONS)

This test may fail in cross-compilation. Shouldn't it use check_cxx_source_compiles()?

> Source/cmake/WebKitCompilerFlags.cmake:151
> +        include(FindSSE2)

find_package(SSE2 REQUIRED)

Note that to make REQUIRED work you should also add FIND_PACKAGE_HANDLE_STANDARD_ARGS to FindSSE2.cmake
Comment 37 Xan Lopez 2019-05-06 01:18:24 PDT
(In reply to Konstantin Tokarev from comment #36)
> Comment on attachment 369092 [details]
> Detect SSE2 with CMake
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=369092&action=review
> 
> > Source/cmake/FindSSE2.cmake:50
> > +        HAVE_SSE2_EXTENSIONS)
> 
> This test may fail in cross-compilation. Shouldn't it use
> check_cxx_source_compiles()?

The build script used to leave this alone when crosscompiling, which honestly makes sense to me. So I've added an extra guard for this in the first if() check, let me know what you think.

> 
> > Source/cmake/WebKitCompilerFlags.cmake:151
> > +        include(FindSSE2)
> 
> find_package(SSE2 REQUIRED)
> 
> Note that to make REQUIRED work you should also add
> FIND_PACKAGE_HANDLE_STANDARD_ARGS to FindSSE2.cmake

My impression is that for the cmake modules WebKit ships include() is used, while find_package() is used for external libraries. So find_package() seems overkill?
Comment 38 Xan Lopez 2019-05-06 01:18:44 PDT
Created attachment 369115 [details]
Detect SSE2 with CMake

New patch.
Comment 39 EWS Watchlist 2019-05-06 03:13:04 PDT Comment hidden (obsolete)
Comment 40 EWS Watchlist 2019-05-06 03:13:07 PDT
Created attachment 369118 [details]
Archive of layout-test-results from ews213 for win-future

The attached test failures were seen while running run-webkit-tests on the win-ews.
Bot: ews213  Port: win-future  Platform: CYGWIN_NT-10.0-17763-3.0.5-338.x86_64-x86_64-64bit
Comment 41 Michael Catanzaro 2019-05-06 05:16:50 PDT
Suggesting find_package() to find something that's not a package seems like a design problem.
Comment 42 Konstantin Tokarev 2019-05-06 05:23:16 PDT
If we don't want to use find_package, module name should not start with Find*
Comment 43 Konstantin Tokarev 2019-05-06 05:57:04 PDT
(In reply to Xan Lopez from comment #37)
> (In reply to Konstantin Tokarev from comment #36)
> > Comment on attachment 369092 [details]
> > Detect SSE2 with CMake
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=369092&action=review
> > 
> > > Source/cmake/FindSSE2.cmake:50
> > > +        HAVE_SSE2_EXTENSIONS)
> > 
> > This test may fail in cross-compilation. Shouldn't it use
> > check_cxx_source_compiles()?
> 
> The build script used to leave this alone when crosscompiling, which
> honestly makes sense to me. So I've added an extra guard for this in the
> first if() check, let me know what you think.

It still makes sense to check that target compiler supports SSE2 intrinsics. OTOH, I see little sense in checking that SSE2 is available on CPU where WebKit is compiled, because it's not necessary the machine where build products are meant to run on.

> 
> > 
> > > Source/cmake/WebKitCompilerFlags.cmake:151
> > > +        include(FindSSE2)
> > 
> > find_package(SSE2 REQUIRED)
> > 
> > Note that to make REQUIRED work you should also add
> > FIND_PACKAGE_HANDLE_STANDARD_ARGS to FindSSE2.cmake
> 
> My impression is that for the cmake modules WebKit ships include() is used,
> while find_package() is used for external libraries. So find_package() seems
> overkill?

No, we consistently use find_package() for Find modules inside Source/cmake
Comment 44 Konstantin Tokarev 2019-05-06 06:49:13 PDT
> OTOH, I see little sense in checking that SSE2 is available on CPU where
> WebKit is compiled, because it's not necessary the machine where build
> products are meant to run on.

Though run check may be useful for DEVELOPER_MODE
Comment 45 Xan Lopez 2019-05-09 00:38:54 PDT
Created attachment 369473 [details]
Detect SSE2 with CMake

New patch.
- Rename FindSSE2.cmake to DetectSSE2.cmake, since this is not trying to find any package in any meaningful way.
- Do not run the SSE2 check when crosscompiling, since the building machine won't be the one running the binary anyway (this is what build-jsc does/did too).
Comment 46 Xan Lopez 2019-05-09 01:01:11 PDT
Created attachment 369476 [details]
Detect SSE2 with CMake

Fix weird path arrangement in commit message.
Comment 47 EWS Watchlist 2019-05-09 02:30:57 PDT
Comment on attachment 369476 [details]
Detect SSE2 with CMake

Attachment 369476 [details] did not pass win-ews (win):
Output: https://webkit-queues.webkit.org/results/12141961

New failing tests:
http/tests/css/filters-on-iframes.html
Comment 48 EWS Watchlist 2019-05-09 02:30:59 PDT
Created attachment 369481 [details]
Archive of layout-test-results from ews210 for win-future

The attached test failures were seen while running run-webkit-tests on the win-ews.
Bot: ews210  Port: win-future  Platform: CYGWIN_NT-10.0-17763-3.0.5-338.x86_64-x86_64-64bit
Comment 49 WebKit Commit Bot 2019-05-09 03:43:48 PDT
Comment on attachment 369476 [details]
Detect SSE2 with CMake

Clearing flags on attachment: 369476

Committed r245127: <https://trac.webkit.org/changeset/245127>
Comment 50 WebKit Commit Bot 2019-05-09 03:43:50 PDT
All reviewed patches have been landed.  Closing bug.
Comment 51 karogyoker2+webkit 2019-05-23 08:28:57 PDT
> So I proposed to just use SSE2 everywhere, which is
> what Firefox and Chrome are already doing, and save ourselves some time in
> the future. In any case, again, feel free to comment on the thread, I did
> make a public RFC in webkit-dev.

Just for the record - for my greatest surprise - Firefox 67 works without SSE2. I've just watched a YouTube video at 144p on my Athlon XP 2600+.