You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: HACKING.md
+67-2Lines changed: 67 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,53 @@ While simdjson distributes just two files from the singleheader/ directory, we *
64
64
multiple files under include/ and src/. include/simdjson.h and src/simdjson.cpp are the "spine" for
65
65
these, and you can include
66
66
67
+
68
+
69
+
Runtime Dispatching
70
+
--------------------
71
+
72
+
A key feature of simdjson is the ability to compile different processing kernels, optimized for specific instruction sets, and to select
73
+
the most appropriate kernel at runtime. This ensures that users get the very best performance while still enabling simdjson to run everywhere.
74
+
This technique is frequently called runtime dispatching. The simdjson achieves runtime dispatching entirely in C++: we do not assume
75
+
that the user is building the code using CMake, for example.
76
+
77
+
To make runtime dispatching work, it is critical that the code be compiled for the lowest supported processor. In particular, you should
78
+
not use flags such as -mavx2, /arch:AVX2 and so forth while compiling simdjson. When you do so, you allow the compiler to use advanced
79
+
instructions. In turn, these advanced instructions present in the code may cause a runtime failure if the runtime processor does not
80
+
support them. Even a simple loop, compiled with these flags, might generate binary code that only run on advanced processors.
81
+
82
+
So we compile simdjson for a generic processor. Our users should do the same if they want simdjson's runtime dispatch to work. It is important
83
+
to understand that if runtime dispatching does not work, then simdjson will cause crashes on older processors. Of course, if a user chooses
84
+
to compile their code for a specific instruction set (e.g., AVX2), they are responsible for the failures if they later run their code
85
+
on a processor that does not support AVX2. Yet, if we were to entice these users to do so, we would share the blame: thus we carefully instruct
86
+
users to compile their code in a generic way without doing anything to enable advanced instructions.
87
+
88
+
89
+
We only use runtime dispatching on x64 (AMD/Intel) platforms, at the moment. On ARM processors, we would need a standard way to query, at runtime,
90
+
the processor for its supported features. We do not know how to do so on ARM systems in general. Thankfully it is not yet a concern: 64-bit ARM
91
+
processors are fairly uniform as far as the instruction sets they support.
92
+
93
+
94
+
In all cases, simdjson uses advanced instructions by relying on "intrinsic functions": we do not write assembly code. The intrinsic functions
95
+
are special functions that the compiler might recognize and translate into fast code. To make runtime dispatching work, we rely on the fact that
96
+
the header providing these instructions
97
+
(intrin.h under Visual Studio, x86intrin.h elsewhere) defines all of the intrinsic functions, including those that are not supported
98
+
processor.
99
+
100
+
At this point, we are require to use one of two main strategies.
101
+
102
+
1. On POSIX systems, the main compilers (LLVM clang, GNU gcc) allow us to use any intrinsic function after including the header, but they fail to inline the resulting instruction if the target processor does not support them. Because we compile for a generic processor, we would not be able to use most intrinsic functions. Thankfully, more recent versions of these compilers allow us to flag a region of code with a specific target, so that we can compile only some of the code with support for advanced instructions. Thus in our C++, one might notice macros like `TARGET_HASWELL`. It is then our responsability, at runtime, to only run the regions of code (that we call kernels) matching the properties of the runtime processor. The benefit of this approach is that the compiler not only let us use intrinsic functions, but it can also optimize the rest of the code in the kernel with advanced instructions we enabled.
103
+
104
+
2. Under Visual Studio, the problem is somewhat simpler. Visual Studio will not only provide the intrinsic functions, but it will also allow us to use them. They will compile just fine. It is at runtime that they may cause a crash. So we do not need to mark regions of code for compilation toward advanced processors (e.g., with `TARGET_HASWELL` macros). The downside of the Visual Studio approach is that the compiler is not allowed to use advanced instructions others than those we specify. In principle, this means that Visual Studio has weaker optimization opportunities.
105
+
106
+
107
+
108
+
We also handle the special case where a user is compiling using LLVM clang under Windows, [using the Visual Studio toolchain](https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/). If you compile with LLVM clang under Visual Studio, then the header files (intrin.h or x86intrin.h) no longer provides the intrinsic functions that are unsupported by the processor. This appears to be deliberate on the part of the LLVM engineers. With a few lines of code, we handle this scenario just like LLVM clang under a POSIX system, but forcing the inclusion of the specific headers, and rolling our own intrinsic function as needed.
109
+
110
+
111
+
112
+
113
+
67
114
Regenerating Single Headers From Master
68
115
---------------------------------------
69
116
@@ -103,7 +150,7 @@ pkg install bash
103
150
pkg install cmake
104
151
```
105
152
106
-
You need a recent compiler like clang or gcc. We recommend at least GNU GCC/G++ 7 or LLVM clang 6.
153
+
You need a recent compiler like clang or gcc. We recommend at least GNU GCC/G++ 7 or LLVM clang 6.
107
154
108
155
109
156
Building: While in the project repository, do the following:
@@ -153,10 +200,28 @@ We assume you have a common 64-bit Windows PC with at least Visual Studio 2017 a
153
200
- Grab the simdjson code from GitHub, e.g., by cloning it using [GitHub Desktop](https://desktop.github.com/).
154
201
- Install [CMake](https://cmake.org/download/). When you install it, make sure to ask that `cmake` be made available from the command line. Please choose a recent version of cmake.
155
202
- Create a subdirectory within simdjson, such as `VisualStudio`.
156
-
- Using a shell, go to this newly created directory. You can start a shell directly from GitHub Desktop (Repository > Open in Command Prompt).
203
+
- Using a shell, go to this newly created directory. You can start a shell directly from GitHub Desktop (Repository > Open in Command Prompt).
157
204
- Type `cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..` in the shell while in the `VisualStudio` repository. (Alternatively, if you want to build a DLL, you may use the command line `cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DSIMDJSON_BUILD_STATIC=OFF ..`.)
158
205
- This last command (`cmake ...`) created a Visual Studio solution file in the newly created directory (e.g., `simdjson.sln`). Open this file in Visual Studio. You should now be able to build the project and run the tests. For example, in the `Solution Explorer` window (available from the `View` menu), right-click `ALL_BUILD` and select `Build`. To test the code, still in the `Solution Explorer` window, select `RUN_TESTS` and select `Build`.
159
206
207
+
208
+
Though having Visual Studio installed is necessary, one can build simdjson using only cmake commands:
209
+
210
+
-`mkdir build`
211
+
-`cd build`
212
+
-`cmake ..`
213
+
-`cmake --build . -config Release`
214
+
215
+
216
+
Furthermore, if you have installed LLVM clang on Windows, for example as a component of Visual Studio 2019, you can configure and build simdjson using LLVM clang on Windows using cmake:
217
+
218
+
219
+
-`mkdir build`
220
+
-`cd build`
221
+
-`cmake .. -T ClangCL`
222
+
-`cmake --build . -config Release`
223
+
224
+
160
225
### Usage (Using `vcpkg` on 64-bit Windows, Linux and macOS)
161
226
162
227
[vcpkg](https://github.com/Microsoft/vcpkg) users on Windows, Linux and macOS can download and install `simdjson` with one single command from their favorite shell.
Copy file name to clipboardExpand all lines: doc/basics.md
+2-3Lines changed: 2 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,9 +19,8 @@ An overview of what you need to know to use simdjson, with examples.
19
19
Requirements
20
20
------------------
21
21
22
-
- A recent compiler (LLVM clang6 or better, GNU GCC 7 or better, Visual Studio 2017 or better). We require C++11 support as a minimum.
23
-
- A 64-bit system (ARM or x64 Intel/AMD). We run tests on macOS, freeBSD, Linux and Windows. We recommend that Visual Studio users target a 64-bit build (x64) instead of a 32-bit build (x86).
24
-
22
+
- A recent compiler (LLVM clang6 or better, GNU GCC 7 or better) on a 64-bit (ARM or x64 Intel/AMD) POSIX systems such as macOS, freeBSD or Linux. We require that the compiler supports the C++11 standard or better.
23
+
- Visual Studio 2017 or better under 64-bit Windows. Users should target a 64-bit build (x64) instead of a 32-bit build (x86). We support the LLVM clang compiler under Visual Studio (clangcl) as well as as the regular Visual Studio compiler.
0 commit comments