[WHLSL] Implement the loop checker
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 Jan 2019 00:27:00 +0000 (00:27 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 Jan 2019 00:27:00 +0000 (00:27 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193434

Reviewed by Saam Barati.

This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/LoopChecker.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/WHLSLLoopChecker.cpp: Added.
(WebCore::WHLSL::findHighZombies):
* Modules/webgpu/WHLSL/WHLSLLoopChecker.h: Added.
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

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

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

index 0386473..e00d314 100644 (file)
@@ -1,3 +1,21 @@
+2019-01-15  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [WHLSL] Implement the loop checker
+        https://bugs.webkit.org/show_bug.cgi?id=193434
+
+        Reviewed by Saam Barati.
+
+        This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/LoopChecker.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/WHLSLLoopChecker.cpp: Added.
+        (WebCore::WHLSL::findHighZombies):
+        * Modules/webgpu/WHLSL/WHLSLLoopChecker.h: Added.
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
 2019-01-15  Chris Dumez  <cdumez@apple.com>
 
         Unreviewed, rolling out r239993, r239995, r239997, and
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLoopChecker.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLoopChecker.cpp
new file mode 100644 (file)
index 0000000..7173eeb
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * 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 "WHLSLLoopChecker.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLBreak.h"
+#include "WHLSLContinue.h"
+#include "WHLSLDoWhileLoop.h"
+#include "WHLSLFallthrough.h"
+#include "WHLSLForLoop.h"
+#include "WHLSLSwitchStatement.h"
+#include "WHLSLVisitor.h"
+#include "WHLSLWhileLoop.h"
+#include <wtf/SetForScope.h>
+
+namespace WebCore {
+
+namespace WHLSL {
+
+// This makes sure that every occurance of "continue," "break," and "fallthrough" appear within the relevant control flow structure.
+class LoopChecker : public Visitor {
+public:
+    LoopChecker() = default;
+
+    virtual ~LoopChecker() = default;
+
+private:
+    void visit(AST::FunctionDefinition& functionDefinition) override
+    {
+        ASSERT(!m_loopDepth);
+        ASSERT(!m_switchDepth);
+        Visitor::visit(functionDefinition);
+    }
+
+    void visit(AST::WhileLoop& whileLoop) override
+    {
+        checkErrorAndVisit(whileLoop.conditional());
+
+        SetForScope<unsigned> change(m_loopDepth, m_loopDepth + 1);
+        checkErrorAndVisit(whileLoop.body());
+        ASSERT(m_loopDepth);
+    }
+
+    void visit(AST::DoWhileLoop& doWhileLoop) override
+    {
+        {
+            SetForScope<unsigned> change(m_loopDepth, m_loopDepth + 1);
+            checkErrorAndVisit(doWhileLoop.body());
+            ASSERT(m_loopDepth);
+        }
+
+        checkErrorAndVisit(doWhileLoop.conditional());
+    }
+
+    void visit(AST::ForLoop& forLoop) override
+    {
+        WTF::visit(WTF::makeVisitor([&](AST::VariableDeclarationsStatement& variableDeclarationsStatement) {
+            checkErrorAndVisit(variableDeclarationsStatement);
+        }, [&](UniqueRef<AST::Expression>& expression) {
+            checkErrorAndVisit(expression);
+        }), forLoop.initialization());
+        if (forLoop.condition())
+            checkErrorAndVisit(*forLoop.condition());
+        if (forLoop.increment())
+            checkErrorAndVisit(*forLoop.increment());
+
+        SetForScope<unsigned> change(m_loopDepth, m_loopDepth + 1);
+        checkErrorAndVisit(forLoop.body());
+        ASSERT(m_loopDepth);
+    }
+
+    void visit(AST::SwitchStatement& switchStatement) override
+    {
+        checkErrorAndVisit(switchStatement.value());
+
+        SetForScope<unsigned> change(m_switchDepth, m_switchDepth + 1);
+        for (auto& switchCase : switchStatement.switchCases())
+            checkErrorAndVisit(switchCase);
+        ASSERT(m_switchDepth);
+    }
+
+    void visit(AST::Break& breakStatement) override
+    {
+        if (!m_loopDepth && !m_switchDepth) {
+            setError();
+            return;
+        }
+        Visitor::visit(breakStatement);
+    }
+
+    void visit(AST::Continue& continueStatement) override
+    {
+        if (!m_loopDepth) {
+            setError();
+            return;
+        }
+        Visitor::visit(continueStatement);
+    }
+
+    void visit(AST::Fallthrough& fallthroughStatement) override
+    {
+        if (!m_switchDepth) {
+            setError();
+            return;
+        }
+        Visitor::visit(fallthroughStatement);
+    }
+
+    unsigned m_loopDepth { 0 };
+    unsigned m_switchDepth { 0 };
+};
+
+bool findHighZombies(Program& program)
+{
+    LoopChecker loopChecker;
+    loopChecker.Visitor::visit(program);
+    return !loopChecker.error();
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLoopChecker.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLoopChecker.h
new file mode 100644 (file)
index 0000000..4af926f
--- /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 findHighZombies(Program&);
+
+}
+
+}
+
+#endif
index d822f07..e3201d2 100644 (file)
@@ -28,6 +28,9 @@
 
 #if ENABLE(WEBGPU)
 
+#include "WHLSLStructureDefinition.h"
+#include "WHLSLTypeDefinition.h"
+#include "WHLSLTypeReference.h"
 #include "WHLSLVisitor.h"
 #include <wtf/HashSet.h>
 
index 03a45de..446a806 100644 (file)
@@ -322,6 +322,7 @@ Modules/webgpu/WHLSL/WHLSLNameContext.cpp
 Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
 Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp
 Modules/webgpu/WHLSL/WHLSLVisitor.cpp
+Modules/webgpu/WHLSL/WHLSLLoopChecker.cpp
 Modules/webgpu/WHLSL/AST/WHLSLTypeArgument.cpp
 Modules/webgpu/WHLSL/AST/WHLSLBuiltInSemantic.cpp
 Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp
index e16ab9d..b96fa2d 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>"; };
+               1C9AE5CF21EDA27E0069D5F2 /* WHLSLLoopChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLLoopChecker.cpp; sourceTree = "<group>"; };
+               1C9AE5D021EDA27E0069D5F2 /* WHLSLLoopChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLLoopChecker.h; sourceTree = "<group>"; };
                1CA19E030DC255950065A994 /* EventLoopMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = EventLoopMac.mm; sourceTree = "<group>"; };
                1CA19E150DC255CA0065A994 /* EventLoop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EventLoop.h; sourceTree = "<group>"; };
                1CAF347E0A6C405200ABE06E /* WebScriptObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebScriptObject.h; sourceTree = "<group>"; };
                                C234A9B621E92CC0003C984D /* WHLSLIntrinsics.h */,
                                C210E91121B4BD1000B7F83D /* WHLSLLexer.cpp */,
                                C210E91221B4BD1000B7F83D /* WHLSLLexer.h */,
+                               1C9AE5CF21EDA27E0069D5F2 /* WHLSLLoopChecker.cpp */,
+                               1C9AE5D021EDA27E0069D5F2 /* WHLSLLoopChecker.h */,
                                C234A98D21E88884003C984D /* WHLSLNameContext.cpp */,
                                C234A98E21E88885003C984D /* WHLSLNameContext.h */,
                                C234A98A21E8883E003C984D /* WHLSLNameResolver.cpp */,