Title: [247675] trunk
Revision
247675
Author
[email protected]
Date
2019-07-21 11:31:53 -0700 (Sun, 21 Jul 2019)

Log Message

[WHLSL] Return the zero-value enum in the enum-from-integer constructor when the integer is not a valid enum value
https://bugs.webkit.org/show_bug.cgi?id=199853

Reviewed by Dean Jackson.

Source/WebCore:

Test: webgpu/whlsl/enum-integer-constructor.html

* Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
(WebCore::WHLSL::Metal::writeNativeFunction):

LayoutTests:

* webgpu/whlsl/enum-integer-constructor-expected.txt: Added.
* webgpu/whlsl/enum-integer-constructor.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (247674 => 247675)


--- trunk/LayoutTests/ChangeLog	2019-07-21 17:23:54 UTC (rev 247674)
+++ trunk/LayoutTests/ChangeLog	2019-07-21 18:31:53 UTC (rev 247675)
@@ -1,3 +1,13 @@
+2019-07-21  Saam Barati  <[email protected]>
+
+        [WHLSL] Return the zero-value enum in the enum-from-integer constructor when the integer is not a valid enum value
+        https://bugs.webkit.org/show_bug.cgi?id=199853
+
+        Reviewed by Dean Jackson.
+
+        * webgpu/whlsl/enum-integer-constructor-expected.txt: Added.
+        * webgpu/whlsl/enum-integer-constructor.html: Added.
+
 2019-07-21  Myles C. Maxfield  <[email protected]>
 
         [iOS] Fast and complex text codepaths disagree about how to lay out bopomofo with tone marks

Added: trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor-expected.txt (0 => 247675)


--- trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor-expected.txt	2019-07-21 18:31:53 UTC (rev 247675)
@@ -0,0 +1,6 @@
+
+PASS simpleEnumFromInt 
+PASS simpleEnumFromUint 
+PASS weirdEnumFromInt 
+PASS weirdEnumFromUint 
+

Added: trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html (0 => 247675)


--- trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html	2019-07-21 18:31:53 UTC (rev 247675)
@@ -0,0 +1,103 @@
+<!DOCTYPE html>
+<html>
+<meta charset=utf-8>
+<meta name="timeout" content="long">
+<title>Enums.</title>
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script>
+const whlslTests = {};
+
+whlslTests.simpleEnumFromInt = async () => {
+    let program = `
+        enum Foo {
+            A,
+            B,
+            C
+        }
+
+        int foo(int x) {
+            return int(Foo(x));
+        }
+    `;
+
+    assert_equals(await callIntFunction(program, "foo", [makeInt(42)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(1)]), 1);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(2)]), 2);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(-1)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(2147483647)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(-2147483648)]), 0);
+};
+
+whlslTests.simpleEnumFromUint = async () => {
+    let program = `
+        enum Foo : uint {
+            A,
+            B,
+            C
+        }
+
+        uint foo(uint x) {
+            return uint(Foo(x));
+        }
+    `;
+
+    assert_equals(await callUintFunction(program, "foo", [makeUint(0)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(1)]), 1);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(2)]), 2);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(42)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(1000000)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(4294967295)]), 0);
+};
+
+whlslTests.weirdEnumFromInt = async () => {
+    let program = `
+        enum Foo : int {
+            A = 42,
+            B = -1000,
+            C = 101010,
+            D = 0
+        }
+
+        int foo(int x) {
+            return int(Foo(x));
+        }
+    `;
+
+    assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(42)]), 42);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(-1000)]), -1000);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(101010)]), 101010);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(-1)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(2147483647)]), 0);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(-2147483648)]), 0);
+};
+
+whlslTests.weirdEnumFromUint = async () => {
+    let program = `
+        enum Foo : uint {
+            A = 42,
+            C = 101010,
+            D = 0
+        }
+
+        uint foo(uint x) {
+            return uint(Foo(x));
+        }
+    `;
+
+    assert_equals(await callUintFunction(program, "foo", [makeUint(0)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(42)]), 42);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(101010)]), 101010);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(4294967295)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(1)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(41)]), 0);
+    assert_equals(await callUintFunction(program, "foo", [makeUint(101011)]), 0);
+};
+
+runTests(whlslTests);
+</script>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (247674 => 247675)


--- trunk/Source/WebCore/ChangeLog	2019-07-21 17:23:54 UTC (rev 247674)
+++ trunk/Source/WebCore/ChangeLog	2019-07-21 18:31:53 UTC (rev 247675)
@@ -1,3 +1,15 @@
+2019-07-21  Saam Barati  <[email protected]>
+
+        [WHLSL] Return the zero-value enum in the enum-from-integer constructor when the integer is not a valid enum value
+        https://bugs.webkit.org/show_bug.cgi?id=199853
+
+        Reviewed by Dean Jackson.
+
+        Test: webgpu/whlsl/enum-integer-constructor.html
+
+        * Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp:
+        (WebCore::WHLSL::Metal::writeNativeFunction):
+
 2019-07-21  Myles C. Maxfield  <[email protected]>
 
         [iOS] Fast and complex text codepaths disagree about how to lay out bopomofo with tone marks

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp (247674 => 247675)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp	2019-07-21 17:23:54 UTC (rev 247674)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp	2019-07-21 18:31:53 UTC (rev 247675)
@@ -31,6 +31,7 @@
 #include "NotImplemented.h"
 #include "WHLSLAddressSpace.h"
 #include "WHLSLArrayType.h"
+#include "WHLSLEnumerationDefinition.h"
 #include "WHLSLInferTypes.h"
 #include "WHLSLIntrinsics.h"
 #include "WHLSLNamedType.h"
@@ -123,7 +124,8 @@
 {
     StringBuilder stringBuilder;
     if (nativeFunctionDeclaration.isCast()) {
-        auto metalReturnName = typeNamer.mangledNameForType(nativeFunctionDeclaration.type());
+        auto& returnType = nativeFunctionDeclaration.type();
+        auto metalReturnName = typeNamer.mangledNameForType(returnType);
         if (!nativeFunctionDeclaration.parameters().size()) {
             stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, "() {\n"));
             stringBuilder.append(makeString("    ", metalReturnName, " x;\n"));
@@ -134,8 +136,28 @@
         }
 
         ASSERT(nativeFunctionDeclaration.parameters().size() == 1);
-        auto metalParameterName = typeNamer.mangledNameForType(*nativeFunctionDeclaration.parameters()[0]->type());
+        auto& parameterType = *nativeFunctionDeclaration.parameters()[0]->type();
+        auto metalParameterName = typeNamer.mangledNameForType(parameterType);
         stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, '(', metalParameterName, " x) {\n"));
+
+        {
+            auto isEnumerationDefinition = [] (auto& type) {
+                return is<AST::NamedType>(type) && is<AST::EnumerationDefinition>(downcast<AST::NamedType>(type));
+            };
+            auto& unifiedReturnType = returnType.unifyNode();
+            if (isEnumerationDefinition(unifiedReturnType) && !isEnumerationDefinition(parameterType.unifyNode())) { 
+                auto& enumerationDefinition = downcast<AST::EnumerationDefinition>(downcast<AST::NamedType>(unifiedReturnType));
+                stringBuilder.append("    switch (x) {\n");
+                bool hasZeroCase = false;
+                for (auto& member : enumerationDefinition.enumerationMembers()) {
+                    hasZeroCase |= !member.get().value();
+                    stringBuilder.append(makeString("        case ", member.get().value(), ": break;\n"));
+                }
+                ASSERT_UNUSED(hasZeroCase, hasZeroCase);
+                stringBuilder.append("        default: x = 0; break; }\n");
+            }
+        }
+
         stringBuilder.append(makeString("    return static_cast<", metalReturnName, ">(x);\n"));
         stringBuilder.append("}\n");
         return stringBuilder.toString();
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to