[WHLSL] Implement the recursion checker
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 00:53:40 +0000 (00:53 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 00:53:40 +0000 (00:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193436

Reviewed by Saam Barati.

This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/RecursionChecker.mjs into C++.

No new tests because it isn't hooked up yet. Not enough of the compiler exists to have any meaningful sort
of test. When enough of the compiler is present, I'll port the reference implementation's test suite.

* Modules/webgpu/WHLSL/AST/WHLSLCallExpression.h:
(WebCore::WHLSL::AST::CallExpression::function):
* Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp: Copied from Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp.
(WebCore::WHLSL::checkRecursion):
* Modules/webgpu/WHLSL/WHLSLRecursionChecker.h: Added.
* Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

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

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

index 30b5777..e92536b 100644 (file)
@@ -1,5 +1,26 @@
 2019-01-16  Myles C. Maxfield  <mmaxfield@apple.com>
 
+        [WHLSL] Implement the recursion checker
+        https://bugs.webkit.org/show_bug.cgi?id=193436
+
+        Reviewed by Saam Barati.
+
+        This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/RecursionChecker.mjs into C++.
+
+        No new tests because it isn't hooked up yet. Not enough of the compiler exists to have any meaningful sort
+        of test. When enough of the compiler is present, I'll port the reference implementation's test suite.
+
+        * Modules/webgpu/WHLSL/AST/WHLSLCallExpression.h:
+        (WebCore::WHLSL::AST::CallExpression::function):
+        * Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp: Copied from Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp.
+        (WebCore::WHLSL::checkRecursion):
+        * Modules/webgpu/WHLSL/WHLSLRecursionChecker.h: Added.
+        * Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
+2019-01-16  Myles C. Maxfield  <mmaxfield@apple.com>
+
         [WHLSL] Add the literal type checker
         https://bugs.webkit.org/show_bug.cgi?id=193430
 
index 0446ada..bab3181 100644 (file)
@@ -75,6 +75,8 @@ public:
         m_overloads = overloads;
     }
 
+    FunctionDeclaration* function() { return m_function; }
+
     void setFunction(FunctionDeclaration& functionDeclaration)
     {
         assert(!m_function);
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp
new file mode 100644 (file)
index 0000000..43cf9cb
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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 "WHLSLRecursionChecker.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLCallExpression.h"
+#include "WHLSLFunctionDefinition.h"
+#include "WHLSLVisitor.h"
+#include <wtf/HashSet.h>
+
+namespace WebCore {
+
+namespace WHLSL {
+
+// Makes sure there is no function recursion.
+class RecursionChecker : public Visitor {
+private:
+    void visit(AST::FunctionDefinition& functionDefinition) override
+    {
+        auto addResult = m_visitingSet.add(&functionDefinition);
+        if (!addResult.isNewEntry) {
+            setError();
+            return;
+        }
+
+        Visitor::visit(functionDefinition);
+
+        auto success = m_visitingSet.remove(&functionDefinition);
+        ASSERT_UNUSED(success, success);
+    }
+
+    void visit(AST::CallExpression& callExpression) override
+    {
+        ASSERT(callExpression.function());
+        Visitor::visit(*callExpression.function());
+    }
+
+    HashSet<AST::FunctionDefinition*> m_visitingSet;
+};
+
+bool checkRecursion(Program& program)
+{
+    RecursionChecker recursionChecker;
+    recursionChecker.Visitor::visit(program);
+    return !recursionChecker.error();
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursionChecker.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursionChecker.h
new file mode 100644 (file)
index 0000000..885dff0
--- /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;
+
+bool checkRecursion(Program&);
+
+}
+
+}
+
+#endif
index d4e31ea..aa00c69 100644 (file)
@@ -321,6 +321,7 @@ Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp
 Modules/webgpu/WHLSL/WHLSLNameContext.cpp
 Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
 Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp
+Modules/webgpu/WHLSL/WHLSLRecursionChecker.cpp
 Modules/webgpu/WHLSL/WHLSLVisitor.cpp
 Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp
 Modules/webgpu/WHLSL/WHLSLHighZombieFinder.cpp
index eb80596..392a171 100644 (file)
                1C840B9A21EC400900D0500D /* WHLSLGatherEntryPointItems.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLGatherEntryPointItems.h; sourceTree = "<group>"; };
                1C840B9B21EC400900D0500D /* WHLSLChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLChecker.cpp; sourceTree = "<group>"; };
                1C904DF90BA9D2C80081E9D0 /* Version.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Version.xcconfig; sourceTree = "<group>"; };
+               1CA0C2DE21EEB5F400A11860 /* WHLSLRecursionChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLRecursionChecker.h; sourceTree = "<group>"; };
+               1CA0C2E021EEB5F500A11860 /* WHLSLRecursionChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLRecursionChecker.cpp; sourceTree = "<group>"; };
                1CA0C2EA21EED6F500A11860 /* WHLSLLiteralTypeChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLLiteralTypeChecker.h; sourceTree = "<group>"; };
                1CA0C2EC21EED6F600A11860 /* WHLSLLiteralTypeChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLLiteralTypeChecker.cpp; sourceTree = "<group>"; };
                1C9AE5CA21ED9DF50069D5F2 /* WHLSLHighZombieFinder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLHighZombieFinder.cpp; sourceTree = "<group>"; };
                                C21BF73721CD8A0200227979 /* WHLSLParser.cpp */,
                                C21BF73821CD8A0300227979 /* WHLSLParser.h */,
                                C21BF73A21CD8D7000227979 /* WHLSLProgram.h */,
+                               1CA0C2E021EEB5F500A11860 /* WHLSLRecursionChecker.cpp */,
+                               1CA0C2DE21EEB5F400A11860 /* WHLSLRecursionChecker.h */,
                                C234A9AD21E92C19003C984D /* WHLSLRecursiveTypeChecker.cpp */,
                                C234A9AB21E92C18003C984D /* WHLSLRecursiveTypeChecker.h */,
                                C234A99921E90F29003C984D /* WHLSLResolveOverloadImpl.cpp */,