[WHLSL] Add the function stage checker
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 01:08:27 +0000 (01:08 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 01:08:27 +0000 (01:08 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193479

Reviewed by Dean Jackson and Robin Morisset.

This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/CheckNativeFuncStages.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/WHLSLFunctionStageChecker.cpp: Added.
(WebCore::WHLSL::FunctionStageChecker::FunctionStageChecker):
(WebCore::WHLSL::checkFunctionStages):
* Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.h: Added.
* Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp:
(WebCore::WHLSL::Intrinsics::add):
* Modules/webgpu/WHLSL/WHLSLIntrinsics.h:
(WebCore::WHLSL::Intrinsics::ddx const):
(WebCore::WHLSL::Intrinsics::ddy const):
(WebCore::WHLSL::Intrinsics::allMemoryBarrier const):
(WebCore::WHLSL::Intrinsics::deviceMemoryBarrier const):
(WebCore::WHLSL::Intrinsics::groupMemoryBarrier const):
(WebCore::WHLSL::Intrinsics::WTF_ARRAY_LENGTH):
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

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

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

index 2aae9ae..1d6bb13 100644 (file)
@@ -1,5 +1,35 @@
 2019-01-16  Myles C. Maxfield  <mmaxfield@apple.com>
 
+        [WHLSL] Add the function stage checker
+        https://bugs.webkit.org/show_bug.cgi?id=193479
+
+        Reviewed by Dean Jackson and Robin Morisset.
+
+        This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/CheckNativeFuncStages.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/WHLSLFunctionStageChecker.cpp: Added.
+        (WebCore::WHLSL::FunctionStageChecker::FunctionStageChecker):
+        (WebCore::WHLSL::checkFunctionStages):
+        * Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.h: Added.
+        * Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp:
+        (WebCore::WHLSL::Intrinsics::add):
+        * Modules/webgpu/WHLSL/WHLSLIntrinsics.h:
+        (WebCore::WHLSL::Intrinsics::ddx const):
+        (WebCore::WHLSL::Intrinsics::ddy const):
+        (WebCore::WHLSL::Intrinsics::allMemoryBarrier const):
+        (WebCore::WHLSL::Intrinsics::deviceMemoryBarrier const):
+        (WebCore::WHLSL::Intrinsics::groupMemoryBarrier const):
+        (WebCore::WHLSL::Intrinsics::WTF_ARRAY_LENGTH):
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
+2019-01-16  Myles C. Maxfield  <mmaxfield@apple.com>
+
         [WHLSL] Delete the 'restricted' keyword
         https://bugs.webkit.org/show_bug.cgi?id=193469
 
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.cpp
new file mode 100644 (file)
index 0000000..f4ce762
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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 "WHLSLFunctionStageChecker.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLCallExpression.h"
+#include "WHLSLEntryPointType.h"
+#include "WHLSLIntrinsics.h"
+#include "WHLSLProgram.h"
+#include "WHLSLVisitor.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class FunctionStageChecker : public Visitor {
+public:
+    FunctionStageChecker(AST::EntryPointType entryPointType, const Intrinsics& intrinsics)
+        : m_entryPointType(entryPointType)
+        , m_intrinsics(intrinsics)
+    {
+    }
+
+public:
+    void visit(AST::CallExpression& callExpression) override
+    {
+        ASSERT(callExpression.function());
+        if ((callExpression.function() == &m_intrinsics.ddx() || callExpression.function() == &m_intrinsics.ddy()) && m_entryPointType != AST::EntryPointType::Fragment) {
+            setError();
+            return;
+        }
+        if ((callExpression.function() == &m_intrinsics.allMemoryBarrier() || callExpression.function() == &m_intrinsics.deviceMemoryBarrier() || callExpression.function() == &m_intrinsics.groupMemoryBarrier())
+            && m_entryPointType != AST::EntryPointType::Compute) {
+            setError();
+            return;
+        }
+        ASSERT(callExpression.function());
+        Visitor::visit(*callExpression.function());
+    }
+
+    AST::EntryPointType m_entryPointType;
+    const Intrinsics& m_intrinsics;
+};
+
+bool checkFunctionStages(Program& program)
+{
+    for (auto& functionDefinition : program.functionDefinitions()) {
+        if (!functionDefinition->entryPointType())
+            continue;
+        FunctionStageChecker functionStageChecker(*functionDefinition->entryPointType(), program.intrinsics());
+        functionStageChecker.Visitor::visit(functionDefinition);
+        if (functionStageChecker.error())
+            return false;
+    }
+    return true;
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.h
new file mode 100644 (file)
index 0000000..21956bb
--- /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 checkFunctionStages(Program&);
+
+}
+
+}
+
+#endif
index 297e34f..8bba6b6 100644 (file)
@@ -46,9 +46,18 @@ Intrinsics::Intrinsics()
 {
 }
 
-void Intrinsics::add(AST::NativeFunctionDeclaration&)
+void Intrinsics::add(AST::NativeFunctionDeclaration& nativeFunctionDeclaration)
 {
-    // FIXME: Populate this.
+    if (nativeFunctionDeclaration.name() == "ddx")
+        m_ddx = &nativeFunctionDeclaration;
+    else if (nativeFunctionDeclaration.name() == "ddy")
+        m_ddy = &nativeFunctionDeclaration;
+    else if (nativeFunctionDeclaration.name() == "AllMemoryBarrierWithGroupSync")
+        m_allMemoryBarrier = &nativeFunctionDeclaration;
+    else if (nativeFunctionDeclaration.name() == "DeviceMemoryBarrierWithGroupSync")
+        m_deviceMemoryBarrier = &nativeFunctionDeclaration;
+    else if (nativeFunctionDeclaration.name() == "GroupMemoryBarrierWithGroupSync")
+        m_groupMemoryBarrier = &nativeFunctionDeclaration;
 }
 
 bool Intrinsics::addPrimitive(AST::NativeTypeDeclaration& nativeTypeDeclaration)
index 5f4aa30..8410c8b 100644 (file)
@@ -94,6 +94,36 @@ public:
         return *m_vectorFloat[2];
     }
 
+    AST::NativeFunctionDeclaration& ddx() const
+    {
+        ASSERT(m_ddx);
+        return *m_ddx;
+    }
+
+    AST::NativeFunctionDeclaration& ddy() const
+    {
+        ASSERT(m_ddy);
+        return *m_ddy;
+    }
+
+    AST::NativeFunctionDeclaration& allMemoryBarrier() const
+    {
+        ASSERT(m_allMemoryBarrier);
+        return *m_allMemoryBarrier;
+    }
+
+    AST::NativeFunctionDeclaration& deviceMemoryBarrier() const
+    {
+        ASSERT(m_deviceMemoryBarrier);
+        return *m_deviceMemoryBarrier;
+    }
+
+    AST::NativeFunctionDeclaration& groupMemoryBarrier() const
+    {
+        ASSERT(m_groupMemoryBarrier);
+        return *m_groupMemoryBarrier;
+    }
+
 private:
     bool addPrimitive(AST::NativeTypeDeclaration&);
     bool addVector(AST::NativeTypeDeclaration&);
@@ -104,46 +134,52 @@ private:
 
     HashSet<const AST::NativeTypeDeclaration*> m_textureSet;
 
-    AST::NativeTypeDeclaration* m_voidType;
-    AST::NativeTypeDeclaration* m_boolType;
-    AST::NativeTypeDeclaration* m_ucharType;
-    AST::NativeTypeDeclaration* m_ushortType;
-    AST::NativeTypeDeclaration* m_uintType;
-    AST::NativeTypeDeclaration* m_charType;
-    AST::NativeTypeDeclaration* m_shortType;
-    AST::NativeTypeDeclaration* m_intType;
-    AST::NativeTypeDeclaration* m_halfType;
-    AST::NativeTypeDeclaration* m_floatType;
-    AST::NativeTypeDeclaration* m_atomicIntType;
-    AST::NativeTypeDeclaration* m_atomicUintType;
-    AST::NativeTypeDeclaration* m_samplerType;
-
-    AST::NativeTypeDeclaration* m_vectorBool[3];
-    AST::NativeTypeDeclaration* m_vectorUchar[3];
-    AST::NativeTypeDeclaration* m_vectorUshort[3];
-    AST::NativeTypeDeclaration* m_vectorUint[3];
-    AST::NativeTypeDeclaration* m_vectorChar[3];
-    AST::NativeTypeDeclaration* m_vectorShort[3];
-    AST::NativeTypeDeclaration* m_vectorInt[3];
-    AST::NativeTypeDeclaration* m_vectorHalf[3];
-    AST::NativeTypeDeclaration* m_vectorFloat[3];
-
-    AST::NativeTypeDeclaration* m_matrixHalf[3][3];
-    AST::NativeTypeDeclaration* m_matrixFloat[3][3];
+    AST::NativeTypeDeclaration* m_voidType { nullptr };
+    AST::NativeTypeDeclaration* m_boolType { nullptr };
+    AST::NativeTypeDeclaration* m_ucharType { nullptr };
+    AST::NativeTypeDeclaration* m_ushortType { nullptr };
+    AST::NativeTypeDeclaration* m_uintType { nullptr };
+    AST::NativeTypeDeclaration* m_charType { nullptr };
+    AST::NativeTypeDeclaration* m_shortType { nullptr };
+    AST::NativeTypeDeclaration* m_intType { nullptr };
+    AST::NativeTypeDeclaration* m_halfType { nullptr };
+    AST::NativeTypeDeclaration* m_floatType { nullptr };
+    AST::NativeTypeDeclaration* m_atomicIntType { nullptr };
+    AST::NativeTypeDeclaration* m_atomicUintType { nullptr };
+    AST::NativeTypeDeclaration* m_samplerType { nullptr };
+
+    AST::NativeTypeDeclaration* m_vectorBool[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorUchar[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorUshort[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorUint[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorChar[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorShort[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorInt[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorHalf[3] { 0 };
+    AST::NativeTypeDeclaration* m_vectorFloat[3] { 0 };
+
+    AST::NativeTypeDeclaration* m_matrixHalf[3][3] {{ 0 }};
+    AST::NativeTypeDeclaration* m_matrixFloat[3][3] {{ 0 }};
 
     static constexpr const char* m_textureTypeNames[] = { "Texture1D", "RWTexture1D", "Texture1DArray", "RWTexture1DArray", "Texture2D", "RWTexture2D", "Texture2DArray", "RWTexture2DArray", "Texture3D", "RWTexture3D", "TextureCube" };
 
     static constexpr const char* m_textureInnerTypeNames[] = { "uchar", "ushort",  "uint", "char", "short", "int", "half", "float" };
 
-    AST::NativeTypeDeclaration* m_fullTextures[WTF_ARRAY_LENGTH(m_textureTypeNames)][WTF_ARRAY_LENGTH(m_textureInnerTypeNames)][4];
+    AST::NativeTypeDeclaration* m_fullTextures[WTF_ARRAY_LENGTH(m_textureTypeNames)][WTF_ARRAY_LENGTH(m_textureInnerTypeNames)][4] {{{ 0 }}};
 
     static constexpr const char* m_depthTextureInnerTypes[] =  { "half", "float" };
 
-    AST::NativeTypeDeclaration* m_textureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
-    AST::NativeTypeDeclaration* m_rwTextureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
-    AST::NativeTypeDeclaration* m_textureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
-    AST::NativeTypeDeclaration* m_rwTextureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
-    AST::NativeTypeDeclaration* m_textureDepthCube[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+    AST::NativeTypeDeclaration* m_textureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 };
+    AST::NativeTypeDeclaration* m_rwTextureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 };
+    AST::NativeTypeDeclaration* m_textureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 };
+    AST::NativeTypeDeclaration* m_rwTextureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 };
+    AST::NativeTypeDeclaration* m_textureDepthCube[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)] { 0 };
+
+    AST::NativeFunctionDeclaration* m_ddx { nullptr };
+    AST::NativeFunctionDeclaration* m_ddy { nullptr };
+    AST::NativeFunctionDeclaration* m_allMemoryBarrier { nullptr };
+    AST::NativeFunctionDeclaration* m_deviceMemoryBarrier { nullptr };
+    AST::NativeFunctionDeclaration* m_groupMemoryBarrier { nullptr };
 };
 
 } // namespace WHLSL
index aa00c69..b612419 100644 (file)
@@ -326,6 +326,7 @@ Modules/webgpu/WHLSL/WHLSLVisitor.cpp
 Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp
 Modules/webgpu/WHLSL/WHLSLHighZombieFinder.cpp
 Modules/webgpu/WHLSL/WHLSLLoopChecker.cpp
+Modules/webgpu/WHLSL/WHLSLFunctionStageChecker.cpp
 Modules/webgpu/WHLSL/AST/WHLSLTypeArgument.cpp
 Modules/webgpu/WHLSL/AST/WHLSLBuiltInSemantic.cpp
 Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp
index 392a171..c498fa8 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>"; };
+               1CA0C2E421EED12A00A11860 /* WHLSLFunctionStageChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLFunctionStageChecker.cpp; sourceTree = "<group>"; };
+               1CA0C2E521EED12A00A11860 /* WHLSLFunctionStageChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLFunctionStageChecker.h; 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>"; };
                                C234A9AE21E92C1A003C984D /* WHLSLCheckDuplicateFunctions.h */,
                                1C840B9B21EC400900D0500D /* WHLSLChecker.cpp */,
                                1C840B9721EC400700D0500D /* WHLSLChecker.h */,
+                               1CA0C2E421EED12A00A11860 /* WHLSLFunctionStageChecker.cpp */,
+                               1CA0C2E521EED12A00A11860 /* WHLSLFunctionStageChecker.h */,
                                1C840B9921EC400800D0500D /* WHLSLGatherEntryPointItems.cpp */,
                                1C840B9A21EC400900D0500D /* WHLSLGatherEntryPointItems.h */,
                                1C9AE5CA21ED9DF50069D5F2 /* WHLSLHighZombieFinder.cpp */,