Title: [249816] trunk/Source/WebCore
Revision
249816
Author
sbar...@apple.com
Date
2019-09-12 14:47:25 -0700 (Thu, 12 Sep 2019)

Log Message

[WHLSL] Slim down WSLMatrix and inline constructors in native code
https://bugs.webkit.org/show_bug.cgi?id=201568

Reviewed by Robin Morisset.

Before, our WSL Matrix in MSL had templates to figure out how we're
constructing it. For example, with a list of elements, or a list of
columns. However, we can remove this template code since when we're
emitting Metal code, we know exactly how we're constructing the WSL
matrix. So the NativeFunctionWriter now inlines the proper stores
into the WSLMatrix elements.

This patch speeds up Metal compile times in boids by ~4ms (16%) with
a p-value of 0.0001.

Covered by existing tests.

* Modules/webgpu/WHLSL/Metal/WHLSLMetalCodeGenerator.cpp:
(WebCore::WHLSL::Metal::metalCodePrologue):
* Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
(WebCore::WHLSL::Metal::inlineNativeFunction):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (249815 => 249816)


--- trunk/Source/WebCore/ChangeLog	2019-09-12 20:18:07 UTC (rev 249815)
+++ trunk/Source/WebCore/ChangeLog	2019-09-12 21:47:25 UTC (rev 249816)
@@ -1,3 +1,27 @@
+2019-09-12  Saam Barati  <sbar...@apple.com>
+
+        [WHLSL] Slim down WSLMatrix and inline constructors in native code
+        https://bugs.webkit.org/show_bug.cgi?id=201568
+
+        Reviewed by Robin Morisset.
+
+        Before, our WSL Matrix in MSL had templates to figure out how we're 
+        constructing it. For example, with a list of elements, or a list of
+        columns. However, we can remove this template code since when we're
+        emitting Metal code, we know exactly how we're constructing the WSL
+        matrix. So the NativeFunctionWriter now inlines the proper stores
+        into the WSLMatrix elements.
+        
+        This patch speeds up Metal compile times in boids by ~4ms (16%) with
+        a p-value of 0.0001.
+
+        Covered by existing tests.
+
+        * Modules/webgpu/WHLSL/Metal/WHLSLMetalCodeGenerator.cpp:
+        (WebCore::WHLSL::Metal::metalCodePrologue):
+        * Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
+        (WebCore::WHLSL::Metal::inlineNativeFunction):
+
 2019-09-12  Wenson Hsieh  <wenson_hs...@apple.com>
 
         [Cocoa] Text indicator for an image link on the front page of apple.com looks wrong

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMetalCodeGenerator.cpp (249815 => 249816)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMetalCodeGenerator.cpp	2019-09-12 20:18:07 UTC (rev 249815)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMetalCodeGenerator.cpp	2019-09-12 21:47:25 UTC (rev 249816)
@@ -58,132 +58,17 @@
         "{\n"
         "    vec<T, Rows> columns[Cols];\n"
         "    private:\n"
-        "    template <typename U, int... R>\n"
-        "    static vec<T, Rows> build_col(initializer_list<U> col, _integer_sequence<int, R...>)\n"
-        "    {\n"
-        "        return {(R < col.size() ? *(col.begin() + R) : U())...};\n"
-        "    }\n"
-        "    template <int... R>\n"
-        "    static vec<T, Rows> build_full_col(int c, initializer_list<T> elems, _integer_sequence<int, R...>)\n"
-        "    {\n"
-        "        return {*(elems.begin() + c * Rows + R)...};\n"
-        "    }\n"
-        "    struct cols_init_tag { };\n"
-        "    struct cols_all_tag { };\n"
-        "    struct elems_all_tag { };\n"
-        "    template <int... C>\n"
-        "    inline explicit WSLMatrix(cols_init_tag, initializer_list<vec<T, Rows>> cols, _integer_sequence<int, C...>) thread\n"
-        "        : columns{(C < cols.size() ? *(cols.begin() + C) : vec<T, Rows>())...}\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(cols_all_tag, U... cols) thread\n"
-        "        : columns{ cols... }\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(elems_all_tag, U... elems) thread\n"
-        "        : WSLMatrix({T(elems)...}, _make_integer_sequence<int, Cols>())\n"
-        "        {\n"
-        "        }\n"
-        "    template <int... C>\n"
-        "    inline explicit WSLMatrix(initializer_list<T> elems, _integer_sequence<int, C...>) thread\n"
-        "        : columns{build_full_col(C, elems, _make_integer_sequence<int, Rows>())...}\n"
-        "    {\n"
-        "    }\n"
-        "    template <int... C>\n"
-        "    inline explicit WSLMatrix(cols_init_tag, initializer_list<vec<T, Rows>> cols, _integer_sequence<int, C...>) constant\n"
-        "        : columns{(C < cols.size() ? *(cols.begin() + C) : vec<T, Rows>())...}\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(cols_all_tag, U... cols) constant\n"
-        "        : columns{ cols... }\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(elems_all_tag, U... elems) constant\n"
-        "        : WSLMatrix({T(elems)...}, _make_integer_sequence<int, Cols>())\n"
-        "        {\n"
-        "        }\n"
-        "    template <int... C>\n"
-        "    inline explicit WSLMatrix(initializer_list<T> elems, _integer_sequence<int, C...>) constant\n"
-        "        : columns{build_full_col(C, elems, _make_integer_sequence<int, Rows>())...}\n"
-        "    {\n"
-        "    }\n"
         "    public:\n"
         "    inline WSLMatrix() thread = default;\n"
-        "    inline WSLMatrix(initializer_list<vec<T, Rows>> cols) thread\n"
-        "        : WSLMatrix(cols_init_tag(), cols, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(U... vals) thread\n"
-        "        : WSLMatrix(conditional_t<sizeof...(U) == Cols, cols_all_tag, elems_all_tag>(), vals...)\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix() constant = default;\n"
-        "    inline WSLMatrix(initializer_list<vec<T, Rows>> cols) constant\n"
-        "        : WSLMatrix(cols_init_tag(), cols, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
-        "    inline explicit WSLMatrix(T val) constant\n"
-        "        : WSLMatrix(val, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
-        "    template <typename... U>\n"
-        "    inline explicit WSLMatrix(U... vals) constant\n"
-        "        : WSLMatrix(conditional_t<sizeof...(U) == Cols, cols_all_tag, elems_all_tag>(), vals...)\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const thread WSLMatrix<T, Cols, Rows> &that) thread = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const thread WSLMatrix<U, Cols, Rows> &that) thread\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const device WSLMatrix<T, Cols, Rows> &that) thread = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const device WSLMatrix<U, Cols, Rows> &that) thread\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const constant WSLMatrix<T, Cols, Rows> &that) thread = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const constant WSLMatrix<U, Cols, Rows> &that) thread\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const threadgroup WSLMatrix<T, Cols, Rows> &that) thread = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const threadgroup WSLMatrix<U, Cols, Rows> &that) thread\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const thread WSLMatrix<T, Cols, Rows> &that) constant = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const thread WSLMatrix<U, Cols, Rows> &that) constant\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const device WSLMatrix<T, Cols, Rows> &that) constant = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const device WSLMatrix<U, Cols, Rows> &that) constant\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const constant WSLMatrix<T, Cols, Rows> &that) constant = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const constant WSLMatrix<U, Cols, Rows> &that) constant\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    inline WSLMatrix(const threadgroup WSLMatrix<T, Cols, Rows> &that) constant = default;\n"
-        "    template <typename U>\n"
-        "    inline explicit WSLMatrix(const threadgroup WSLMatrix<U, Cols, Rows> &that) constant\n"
-        "        : WSLMatrix(that, _make_integer_sequence<int, Cols>())\n"
-        "    {\n"
-        "    }\n"
         "    public:\n"
         "    inline thread vec<T, Rows> &operator[](int r) thread\n"
         "    {\n"

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp (249815 => 249816)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp	2019-09-12 20:18:07 UTC (rev 249815)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp	2019-09-12 21:47:25 UTC (rev 249816)
@@ -182,13 +182,33 @@
         }
 
         if (auto* matrixType = asMatrixType(returnType)) {
-            stringBuilder.append(metalReturnTypeName, '(');
-            for (size_t i = 0; i < args.size(); ++i) {
-                if (i)
-                    stringBuilder.append(", ");
-                stringBuilder.append(args[i]);
+            // We're either constructing with all individual elements, or with
+            // vectors for each column.
+
+            stringBuilder.append('(');
+            if (args.size() == matrixType->numberOfMatrixColumns()) {
+                // Constructing with vectors for each column.
+                for (size_t i = 0; i < args.size(); ++i) {
+                    if (i)
+                        stringBuilder.append(", ");
+                    stringBuilder.append(resultName, ".columns[", i, "] = ", args[i]);
+                }
+            } else {
+                // Constructing with all elements.
+                RELEASE_ASSERT(args.size() == matrixType->numberOfMatrixColumns() * matrixType->numberOfMatrixRows());
+
+                size_t argNumber = 0;
+                for (size_t i = 0; i < matrixType->numberOfMatrixColumns(); ++i) {
+                    for (size_t j = 0; j < matrixType->numberOfMatrixRows(); ++j) {
+                        if (argNumber)
+                            stringBuilder.append(", ");
+                        stringBuilder.append(resultName, ".columns[", i, "][", j, "] = ", args[argNumber]);
+                        ++argNumber;
+                    }
+                }
             }
-            stringBuilder.append(')');
+
+            stringBuilder.append(", ", resultName, ')');
             return;
         }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to