Title: [248904] trunk/Source/WebCore
Revision
248904
Author
sbar...@apple.com
Date
2019-08-20 10:19:04 -0700 (Tue, 20 Aug 2019)

Log Message

[WHLSL] Only take the pointer of a variable or global variable reference if it is used
https://bugs.webkit.org/show_bug.cgi?id=200908

Reviewed by Dean Jackson.

Previously, we would always emit Metal code to produce an lvalue pointer
even when it wasn't used. This patch adds a mechanism to lazily generate
such pointers when they're actually needed, since we often don't use them.
This is a 7% Metal compile time speedup on compute_boids with a p value of
0.0001.

* Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::appendRightValueWithNullability):
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::appendLeftValue):
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::takeLastLeftValue):
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
* Modules/webgpu/WHLSL/Metal/WHLSLMangledNames.h:
(WebCore::WHLSL::Metal::MangledVariableName::operator bool const):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (248903 => 248904)


--- trunk/Source/WebCore/ChangeLog	2019-08-20 17:14:37 UTC (rev 248903)
+++ trunk/Source/WebCore/ChangeLog	2019-08-20 17:19:04 UTC (rev 248904)
@@ -1,3 +1,24 @@
+2019-08-20  Saam Barati  <sbar...@apple.com>
+
+        [WHLSL] Only take the pointer of a variable or global variable reference if it is used
+        https://bugs.webkit.org/show_bug.cgi?id=200908
+
+        Reviewed by Dean Jackson.
+
+        Previously, we would always emit Metal code to produce an lvalue pointer
+        even when it wasn't used. This patch adds a mechanism to lazily generate
+        such pointers when they're actually needed, since we often don't use them.
+        This is a 7% Metal compile time speedup on compute_boids with a p value of
+        0.0001.
+
+        * Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::appendRightValueWithNullability):
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::appendLeftValue):
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::takeLastLeftValue):
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
+        * Modules/webgpu/WHLSL/Metal/WHLSLMangledNames.h:
+        (WebCore::WHLSL::Metal::MangledVariableName::operator bool const):
+
 2019-08-20  Chris Dumez  <cdu...@apple.com>
 
         Unsafe usage of CookieStorageObserver from a background thread

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp (248903 => 248904)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp	2019-08-20 17:14:37 UTC (rev 248903)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp	2019-08-20 17:19:04 UTC (rev 248904)
@@ -138,9 +138,10 @@
 
     struct StackItem {
         MangledVariableName value;
-        Optional<MangledVariableName> leftValue;
+        MangledVariableName leftValue;
         Nullability valueNullability;
         Nullability leftValueNullability;
+        std::function<MangledVariableName()> generateLeftValue;
     };
 
     struct StackValue {
@@ -155,7 +156,7 @@
     // a non-null lvalue.
     void appendRightValueWithNullability(AST::_expression_&, MangledVariableName value, Nullability nullability)
     {
-        m_stack.append({ WTFMove(value), WTF::nullopt, nullability, Nullability::CanBeNull });
+        m_stack.append({ WTFMove(value), { }, nullability, Nullability::CanBeNull, { } });
     }
 
     void appendRightValue(AST::_expression_& _expression_, MangledVariableName value)
@@ -163,10 +164,11 @@
         appendRightValueWithNullability(_expression_, WTFMove(value), Nullability::CanBeNull);
     }
 
-    void appendLeftValue(AST::_expression_& _expression_, MangledVariableName value, MangledVariableName leftValue, Nullability nullability)
+    void appendLeftValue(AST::_expression_& _expression_, MangledVariableName value, MangledVariableName leftValue, Nullability nullability, std::function<MangledVariableName()> generateLeftValue = { })
     {
         ASSERT_UNUSED(_expression_, _expression_.typeAnnotation().leftAddressSpace());
-        m_stack.append({ WTFMove(value), WTFMove(leftValue), Nullability::CanBeNull, nullability });
+        ASSERT(leftValue || generateLeftValue);
+        m_stack.append({ WTFMove(value), WTFMove(leftValue), Nullability::CanBeNull, nullability, WTFMove(generateLeftValue) });
     }
 
     MangledVariableName takeLastValue()
@@ -182,9 +184,10 @@
 
     StackValue takeLastLeftValue()
     {
-        ASSERT(m_stack.last().leftValue);
         auto last = m_stack.takeLast();
-        return { *last.leftValue, last.leftValueNullability };
+        if (!last.leftValue)
+            last.leftValue = last.generateLeftValue();
+        return { last.leftValue, last.leftValueNullability };
     }
 
     enum class BreakContext {
@@ -522,14 +525,24 @@
 void FunctionDefinitionWriter::visit(AST::GlobalVariableReference& globalVariableReference)
 {
     auto valueName = generateNextVariableName();
-    auto pointerName = generateNextVariableName();
-    auto mangledTypeName = m_typeNamer.mangledNameForType(globalVariableReference.resolvedType());
+    MangledTypeName mangledTypeName = m_typeNamer.mangledNameForType(globalVariableReference.resolvedType());
+
     checkErrorAndVisit(globalVariableReference.base());
+    MangledVariableName structVariable = takeLastValue();
+
+    MangledStructureElementName mangledFieldName = m_typeNamer.mangledNameForStructureElement(globalVariableReference.structField());
+
     m_stringBuilder.append(
-        m_indent, "thread ", mangledTypeName, "* ", pointerName, " = &", takeLastValue(), "->", m_typeNamer.mangledNameForStructureElement(globalVariableReference.structField()), ";\n",
-        m_indent, mangledTypeName, ' ', valueName, " = ", "*", pointerName, ";\n"
-    );
-    appendLeftValue(globalVariableReference, valueName, pointerName, Nullability::NotNull);
+        m_indent, mangledTypeName, ' ', valueName, " = ", structVariable, "->", mangledFieldName, ";\n");
+
+    Indentation<4> indent = m_indent;
+    appendLeftValue(globalVariableReference, valueName, { }, Nullability::NotNull,
+        [this, mangledTypeName, structVariable, mangledFieldName, indent] {
+            auto pointerName = generateNextVariableName();
+            m_stringBuilder.append(
+                indent, "thread ", mangledTypeName, "* ", pointerName, " = &", structVariable, "->", mangledFieldName, ";\n");
+            return pointerName;
+        });
 }
 
 void FunctionDefinitionWriter::visit(AST::IndexExpression& indexExpression)
@@ -644,7 +657,7 @@
     if (nullability == Nullability::CanBeNull) {
         m_stringBuilder.append(
             m_indent, "if (", resultPointer, ")\n",
-            m_indent, "    ", resultValue, " = *", inputPointer, ";\n",
+            m_indent, "    ", resultValue, " = *", resultPointer, ";\n",
             m_indent, "else\n",
             m_indent, "    ", resultValue, " = { };\n"
         );
@@ -750,9 +763,15 @@
     auto iterator = m_variableMapping.find(variableReference.variable());
     ASSERT(iterator != m_variableMapping.end());
 
-    auto pointerName = generateNextVariableName();
-    m_stringBuilder.append(m_indent, "thread ", m_typeNamer.mangledNameForType(variableReference.resolvedType()), "* ", pointerName, " = &", iterator->value, ";\n");
-    appendLeftValue(variableReference, iterator->value, pointerName, Nullability::NotNull);
+    MangledVariableName variableName = iterator->value;
+
+    Indentation<4> indent = m_indent;
+    appendLeftValue(variableReference, variableName, { }, Nullability::NotNull,
+        [this, &variableReference, variableName, indent] {
+            auto pointerName = generateNextVariableName();
+            m_stringBuilder.append(indent, "thread ", m_typeNamer.mangledNameForType(variableReference.resolvedType()), "* ", pointerName, " = &", variableName, ";\n");
+            return pointerName;
+        });
 }
 
 void FunctionDefinitionWriter::emitConstantExpressionString(AST::ConstantExpression& constantExpression)

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMangledNames.h (248903 => 248904)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMangledNames.h	2019-08-20 17:14:37 UTC (rev 248903)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLMangledNames.h	2019-08-20 17:19:04 UTC (rev 248904)
@@ -38,7 +38,8 @@
 namespace Metal {
 
 struct MangledVariableName {
-    unsigned value;
+    explicit operator bool() const { return value != std::numeric_limits<unsigned>::max(); }
+    unsigned value { std::numeric_limits<unsigned>::max() };
     static constexpr const char* prefix = "variable";
 };
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to