[WHLSL] Prune unreachable stdlib functions after the Checker runs
authorsbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Aug 2019 06:22:58 +0000 (06:22 +0000)
committersbarati@apple.com <sbarati@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 8 Aug 2019 06:22:58 +0000 (06:22 +0000)
https://bugs.webkit.org/show_bug.cgi?id=200518

Reviewed by Robin Morisset.

We now prune unreachable stdlib functions after the checker runs. We must
do this after the checker runs because that's when we resolve all remaining
function calls. While we can't prune unreachable user code, because we must
still report errors in it, we can prune unreachable standard library code
because we know a priori that it has no errors. This is a 10ms end-to-end
speedup in compute_boids.

* Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
(WebCore::WHLSL::prepareShared):
* Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp: Added.
(WebCore::WHLSL::pruneUnreachableStandardLibraryFunctions):
* Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.h: Added.
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@248403 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/WHLSL/WHLSLPrepare.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.h [new file with mode: 0644]
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj

index fc2086a..d9e1524 100644 (file)
@@ -1,3 +1,25 @@
+2019-08-07  Saam Barati  <sbarati@apple.com>
+
+        [WHLSL] Prune unreachable stdlib functions after the Checker runs
+        https://bugs.webkit.org/show_bug.cgi?id=200518
+
+        Reviewed by Robin Morisset.
+
+        We now prune unreachable stdlib functions after the checker runs. We must
+        do this after the checker runs because that's when we resolve all remaining
+        function calls. While we can't prune unreachable user code, because we must
+        still report errors in it, we can prune unreachable standard library code
+        because we know a priori that it has no errors. This is a 10ms end-to-end
+        speedup in compute_boids.
+
+        * Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
+        (WebCore::WHLSL::prepareShared):
+        * Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp: Added.
+        (WebCore::WHLSL::pruneUnreachableStandardLibraryFunctions):
+        * Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.h: Added.
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
 2019-08-07  Kate Cheney  <katherine_cheney@apple.com>
 
         Adopt non-deprecated CGColorSpace API
index f87f3af..73f4832 100644 (file)
@@ -42,6 +42,7 @@
 #include "WHLSLPreserveVariableLifetimes.h"
 #include "WHLSLProgram.h"
 #include "WHLSLPropertyResolver.h"
+#include "WHLSLPruneUnreachableStandardLibraryFunctions.h"
 #include "WHLSLRecursionChecker.h"
 #include "WHLSLRecursiveTypeChecker.h"
 #include "WHLSLSemanticMatcher.h"
@@ -183,6 +184,7 @@ static Expected<Program, String> prepareShared(PhaseTimes& phaseTimes, String& w
     CHECK_PASS(checkDuplicateFunctions, program);
 
     CHECK_PASS(check, program);
+    RUN_PASS(pruneUnreachableStandardLibraryFunctions, program);
 
     RUN_PASS(checkLiteralTypes, program);
     CHECK_PASS(checkTextureReferences, program);
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp
new file mode 100644 (file)
index 0000000..d15b76c
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WHLSLPruneUnreachableStandardLibraryFunctions.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLAST.h"
+#include "WHLSLProgram.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class ReachableStdlibFunctions final : public Visitor {
+public:
+    void visit(AST::FunctionDefinition& function) override
+    {
+        auto addResult = m_reachableFunctions.add(&function);
+        if (addResult.isNewEntry)
+            Visitor::visit(function);
+    }
+
+    void visit(AST::CallExpression& callExpression) override
+    {
+        Visitor::visit(callExpression);
+        if (is<AST::FunctionDefinition>(callExpression.function()))
+            visit(downcast<AST::FunctionDefinition>(callExpression.function()));
+    }
+
+    void visit(AST::PropertyAccessExpression& expression) override
+    {
+        auto visitFunction = [&] (AST::FunctionDeclaration* function) {
+            if (!function)
+                return;
+            if (is<AST::FunctionDefinition>(*function))
+                visit(downcast<AST::FunctionDefinition>(*function));
+        };
+
+        visitFunction(expression.getterFunction());
+        visitFunction(expression.anderFunction());
+        visitFunction(expression.threadAnderFunction());
+        visitFunction(expression.setterFunction());
+
+        Visitor::visit(expression);
+    }
+
+    HashSet<AST::FunctionDefinition*> takeReachableFunctions() { return WTFMove(m_reachableFunctions); }
+
+private:
+    HashSet<AST::FunctionDefinition*> m_reachableFunctions;
+};
+
+void pruneUnreachableStandardLibraryFunctions(Program& program)
+{
+    ReachableStdlibFunctions reachableStdlibFunctions;
+    Vector<UniqueRef<AST::FunctionDefinition>> functionDefinitions = WTFMove(program.functionDefinitions());
+    for (auto& function : functionDefinitions) {
+        if (function->parsingMode() != ParsingMode::StandardLibrary)
+            reachableStdlibFunctions.visit(function.get());
+        else
+            RELEASE_ASSERT(!function->entryPointType());
+    }
+
+    auto reachableFunctions = reachableStdlibFunctions.takeReachableFunctions();
+    Vector<UniqueRef<AST::FunctionDefinition>> newFunctionDefinitions;
+    for (UniqueRef<AST::FunctionDefinition>& entry : functionDefinitions) {
+        if (reachableFunctions.contains(&entry.get()))
+            newFunctionDefinitions.append(WTFMove(entry));
+    }
+
+    program.functionDefinitions() = WTFMove(newFunctionDefinitions);
+}
+
+} // namespace WHLSL
+
+} // namespace WebCore
+
+#endif // ENABLE(WEBGPU)
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.h
new file mode 100644 (file)
index 0000000..d1f745c
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(WEBGPU)
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class Program;
+
+void pruneUnreachableStandardLibraryFunctions(Program&);
+
+}
+
+}
+
+#endif
index 913939f..be43f49 100644 (file)
@@ -312,6 +312,7 @@ Modules/webgpu/WHLSL/WHLSLASTDumper.cpp
 Modules/webgpu/WHLSL/WHLSLInferTypes.cpp
 Modules/webgpu/WHLSL/WHLSLLexer.cpp
 Modules/webgpu/WHLSL/WHLSLParser.cpp
+Modules/webgpu/WHLSL/WHLSLPruneUnreachableStandardLibraryFunctions.cpp
 Modules/webgpu/WHLSL/WHLSLCheckTextureReferences.cpp
 Modules/webgpu/WHLSL/WHLSLChecker.cpp
 Modules/webgpu/WHLSL/WHLSLGatherEntryPointItems.cpp
index c574e80..072f72c 100644 (file)
                522E1A192297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLPreserveVariableLifetimes.h; sourceTree = "<group>"; };
                526724F11CB2FDF60075974D /* TextTrackRepresentationCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TextTrackRepresentationCocoa.mm; sourceTree = "<group>"; };
                526724F21CB2FDF60075974D /* TextTrackRepresentationCocoa.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextTrackRepresentationCocoa.h; sourceTree = "<group>"; };
+               526B3F0122FB7BDD0076D37D /* WHLSLPruneUnreachableStandardLibraryFunctions.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLPruneUnreachableStandardLibraryFunctions.cpp; sourceTree = "<group>"; };
+               526B3F0322FB7BDE0076D37D /* WHLSLPruneUnreachableStandardLibraryFunctions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLPruneUnreachableStandardLibraryFunctions.h; sourceTree = "<group>"; };
                52914C2A22F93E4E00578150 /* WHLSLParsingMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLParsingMode.h; sourceTree = "<group>"; };
                52914C2C22F93E5D00578150 /* WHLSLAddressEscapeMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLAddressEscapeMode.h; sourceTree = "<group>"; };
                52B0D4BD1C57FD1E0077CE53 /* PlatformView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformView.h; sourceTree = "<group>"; };
                                C24A57BB21FEAFEA004C6DD1 /* WHLSLPrepare.h */,
                                522E1A172297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.cpp */,
                                522E1A192297D6D400E5D36A /* WHLSLPreserveVariableLifetimes.h */,
+                               526B3F0122FB7BDD0076D37D /* WHLSLPruneUnreachableStandardLibraryFunctions.cpp */,
+                               526B3F0322FB7BDE0076D37D /* WHLSLPruneUnreachableStandardLibraryFunctions.h */,
                                C21BF73A21CD8D7000227979 /* WHLSLProgram.h */,
                                1CAA82F62242AE0500E84BBB /* WHLSLPropertyResolver.cpp */,
                                1CAA82F72242AE0500E84BBB /* WHLSLPropertyResolver.h */,