Title: [247666] trunk
Revision
247666
Author
[email protected]
Date
2019-07-20 05:40:22 -0700 (Sat, 20 Jul 2019)

Log Message

[WHLSL] Make enums work
https://bugs.webkit.org/show_bug.cgi?id=199634

Reviewed by Robin Morisset.

Source/WebCore:

This patch makes enums work. There were a couple changes needed to do this work:
1. In the metal codegen, we were emitting code like "EnumType.EnumValue"
instead of "EnumType::EnumValue".
        
2. Inside the enumeration declaration AST node, we track enums in a HashMap
because we need to look up names of enum values in that map in parts of the
compiler. However, HashMap is not ordered but we were relying on the iteration
order of it to generate the default values of enums. E.g, in: "enum { Foo = 10, Bar }",
Bar should be 11. However, there was no guarantee that we iterated over Bar
after Foo. Instead, we just track the default values as we parse an enum
inside of the parser. This allows us to continue to keep using HashMap without
also adding an auxiliary data structure (or using more memory in some way) to
track the order of the enum elements.
        
I've also filed a patch to do follow-up work on implementing the correct
operational behavior when constructing an enum from its underlying type when
the underlying value does not match any enum value:
https://bugs.webkit.org/show_bug.cgi?id=199853

Tests: webgpu/whlsl/enums-2.html
       webgpu/whlsl/enums.html

* Modules/webgpu/WHLSL/AST/WHLSLEnumerationMember.h:
(WebCore::WHLSL::AST::EnumerationMember::EnumerationMember):
(WebCore::WHLSL::AST::EnumerationMember::value):
(WebCore::WHLSL::AST::EnumerationMember::setValue): Deleted.
* Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
(WebCore::WHLSL::Metal::FunctionDefinitionWriter::constantExpressionString):
* Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp:
(WebCore::WHLSL::Metal::TypeNamer::emitNamedTypeDefinition):
* Modules/webgpu/WHLSL/WHLSLASTDumper.cpp:
(WebCore::WHLSL::ASTDumper::visit):
* Modules/webgpu/WHLSL/WHLSLChecker.cpp:
(WebCore::WHLSL::Checker::visit):
* Modules/webgpu/WHLSL/WHLSLParser.cpp:
(WebCore::WHLSL::Parser::parseEnumerationDefinition):
(WebCore::WHLSL::Parser::parseEnumerationMember):
* Modules/webgpu/WHLSL/WHLSLParser.h:
* Modules/webgpu/WHLSL/WHLSLVisitor.cpp:
(WebCore::WHLSL::Visitor::visit):

LayoutTests:

* webgpu/whlsl/enums-2-expected.txt: Added.
* webgpu/whlsl/enums-2.html: Added.
* webgpu/whlsl/enums-expected.txt: Added.
* webgpu/whlsl/enums.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (247665 => 247666)


--- trunk/LayoutTests/ChangeLog	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/LayoutTests/ChangeLog	2019-07-20 12:40:22 UTC (rev 247666)
@@ -1,3 +1,15 @@
+2019-07-20  Saam Barati  <[email protected]>
+
+        [WHLSL] Make enums work
+        https://bugs.webkit.org/show_bug.cgi?id=199634
+
+        Reviewed by Robin Morisset.
+
+        * webgpu/whlsl/enums-2-expected.txt: Added.
+        * webgpu/whlsl/enums-2.html: Added.
+        * webgpu/whlsl/enums-expected.txt: Added.
+        * webgpu/whlsl/enums.html: Added.
+
 2019-07-19  Jonathan Bedard  <[email protected]>
 
         fast/scrolling/ipad/* tests running on Mac

Added: trunk/LayoutTests/webgpu/whlsl/enums-2-expected.txt (0 => 247666)


--- trunk/LayoutTests/webgpu/whlsl/enums-2-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enums-2-expected.txt	2019-07-20 12:40:22 UTC (rev 247666)
@@ -0,0 +1,4 @@
+
+PASS simpleEnum 
+PASS enumWithExplicitIntBase 
+

Added: trunk/LayoutTests/webgpu/whlsl/enums-2.html (0 => 247666)


--- trunk/LayoutTests/webgpu/whlsl/enums-2.html	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enums-2.html	2019-07-20 12:40:22 UTC (rev 247666)
@@ -0,0 +1,359 @@
+<!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.simpleEnum = async () => {
+    let program = `
+        enum Foo {
+            War,
+            Famine,
+            Pestilence,
+            Death
+        }
+
+        Foo _war()
+        {
+            return Foo.War;
+        }
+
+        bool war()
+        {
+            return _war() == Foo.War;
+        }
+
+        Foo _famine()
+        {
+            return Foo.Famine;
+        }
+
+        bool famine()
+        {
+            return _famine() == Foo.Famine;
+        }
+
+        Foo _pestilence()
+        {
+            return Foo.Pestilence;
+        }
+
+        bool pestilence()
+        {
+            return _pestilence() == Foo.Pestilence;
+        }
+
+        Foo _death()
+        {
+            return Foo.Death;
+        }
+
+        bool death()
+        {
+            return _death() == Foo.Death;
+        }
+
+        bool _equals(Foo a, Foo b)
+        {
+            return a == b;
+        }
+
+        bool _notEquals(Foo a, Foo b)
+        {
+            return a != b;
+        }
+
+        bool testSimpleEqual()
+        {
+            return _equals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherEqual()
+        {
+            return _equals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotEqual()
+        {
+            return _equals(Foo.Famine, Foo.Death);
+        }
+
+        bool testSimpleNotEqual()
+        {
+            return _notEquals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherNotEqual()
+        {
+            return _notEquals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotNotEqual()
+        {
+            return _notEquals(Foo.Famine, Foo.Death);
+        }
+
+        int _intWar()
+        {
+            return int(_war());
+        }
+
+        int intWar()
+        {
+            return _intWar();
+        }
+
+        int _intFamine()
+        {
+            return int(_famine());
+        }
+
+        int intFamine()
+        {
+            return _intFamine();
+        }
+
+        int _intPestilence()
+        {
+            return int(_pestilence());
+        }
+
+        int intPestilence()
+        {
+            return _intPestilence();
+        }
+
+        int _intDeath()
+        {
+            return int(_death());
+        }
+
+        int intDeath()
+        {
+            return _intDeath();
+        }
+
+        bool intWarBackwards()
+        {
+            return Foo(_intWar()) == Foo.War;
+        }
+
+        bool intFamineBackwards()
+        {
+            return Foo(_intFamine()) == Foo.Famine;
+        }
+
+        bool intPestilenceBackwards()
+        {
+            return Foo(_intPestilence()) == Foo.Pestilence;
+        }
+
+        bool intDeathBackwards()
+        {
+            return Foo(_intDeath()) == Foo.Death;
+        }
+    `;
+
+    assert_equals(await callBoolFunction(program, "war", []), true);
+    assert_equals(await callBoolFunction(program, "famine", []), true);
+    assert_equals(await callBoolFunction(program, "pestilence", []), true);
+    assert_equals(await callBoolFunction(program, "death", []), true);
+
+    assert_equals(await callBoolFunction(program, "testSimpleEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testAnotherEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testSimpleNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testAnotherNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testNotNotEqual", []), true);
+    assert_equals(await callIntFunction(program, "intWar", []), 0);
+    assert_equals(await callIntFunction(program, "intFamine", []), 1);
+    assert_equals(await callIntFunction(program, "intPestilence", []), 2);
+    assert_equals(await callIntFunction(program, "intDeath", []), 3);
+
+    assert_equals(await callBoolFunction(program, "intWarBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intFamineBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intPestilenceBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intDeathBackwards", []), true);
+};
+
+whlslTests.enumWithExplicitIntBase = async () => {
+    let program = `
+        enum Foo : int {
+            War,
+            Famine,
+            Pestilence,
+            Death
+        }
+
+        Foo _war()
+        {
+            return Foo.War;
+        }
+
+        bool war()
+        {
+            return _war() == Foo.War;
+        }
+
+        Foo _famine()
+        {
+            return Foo.Famine;
+        }
+
+        bool famine()
+        {
+            return _famine() == Foo.Famine;
+        }
+
+        Foo _pestilence()
+        {
+            return Foo.Pestilence;
+        }
+
+        bool pestilence()
+        {
+            return _pestilence() == Foo.Pestilence;
+        }
+
+        Foo _death()
+        {
+            return Foo.Death;
+        }
+
+        bool death()
+        {
+            return _death() == Foo.Death;
+        }
+
+        bool _equals(Foo a, Foo b)
+        {
+            return a == b;
+        }
+
+        bool _notEquals(Foo a, Foo b)
+        {
+            return a != b;
+        }
+
+        bool testSimpleEqual()
+        {
+            return _equals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherEqual()
+        {
+            return _equals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotEqual()
+        {
+            return _equals(Foo.Famine, Foo.Death);
+        }
+
+        bool testSimpleNotEqual()
+        {
+            return _notEquals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherNotEqual()
+        {
+            return _notEquals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotNotEqual()
+        {
+            return _notEquals(Foo.Famine, Foo.Death);
+        }
+
+        int _intWar()
+        {
+            return int(_war());
+        }
+
+        int intWar()
+        {
+            return _intWar();
+        }
+
+        int _intFamine()
+        {
+            return int(_famine());
+        }
+
+        int intFamine()
+        {
+            return _intFamine();
+        }
+
+        int _intPestilence()
+        {
+            return int(_pestilence());
+        }
+
+        int intPestilence()
+        {
+            return _intPestilence();
+        }
+
+        int _intDeath()
+        {
+            return int(_death());
+        }
+
+        int intDeath()
+        {
+            return _intDeath();
+        }
+
+        bool intWarBackwards()
+        {
+            return Foo(_intWar()) == Foo.War;
+        }
+
+        bool intFamineBackwards()
+        {
+            return Foo(_intFamine()) == Foo.Famine;
+        }
+
+        bool intPestilenceBackwards()
+        {
+            return Foo(_intPestilence()) == Foo.Pestilence;
+        }
+
+        bool intDeathBackwards()
+        {
+            return Foo(_intDeath()) == Foo.Death;
+        }
+    `;
+
+    assert_equals(await callBoolFunction(program, "war", []), true);
+    assert_equals(await callBoolFunction(program, "famine", []), true);
+    assert_equals(await callBoolFunction(program, "pestilence", []), true);
+    assert_equals(await callBoolFunction(program, "death", []), true);
+
+    assert_equals(await callBoolFunction(program, "testSimpleEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testAnotherEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testSimpleNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testAnotherNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testNotNotEqual", []), true);
+    assert_equals(await callIntFunction(program, "intWar", []), 0);
+    assert_equals(await callIntFunction(program, "intFamine", []), 1);
+    assert_equals(await callIntFunction(program, "intPestilence", []), 2);
+    assert_equals(await callIntFunction(program, "intDeath", []), 3);
+
+    assert_equals(await callBoolFunction(program, "intWarBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intFamineBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intPestilenceBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "intDeathBackwards", []), true);
+};
+
+runTests(whlslTests);
+</script>
+</html>

Added: trunk/LayoutTests/webgpu/whlsl/enums-expected.txt (0 => 247666)


--- trunk/LayoutTests/webgpu/whlsl/enums-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enums-expected.txt	2019-07-20 12:40:22 UTC (rev 247666)
@@ -0,0 +1,14 @@
+
+PASS failTrickyDoubleZero 
+PASS simpleDeclaration 
+PASS enumWithUintBase 
+PASS enumWithoutZero 
+PASS enumNegativeUint 
+PASS pastIntMax 
+PASS enumWithSomeManualValues 
+PASS enumSwitchBreakExhaustive 
+PASS enumSwitchBreakNotQuiteExhaustiveWithDefault 
+PASS enumWithManualValues 
+PASS enumSwitchBreakNotQuiteExhaustive 
+PASS copyConstructors 
+

Added: trunk/LayoutTests/webgpu/whlsl/enums.html (0 => 247666)


--- trunk/LayoutTests/webgpu/whlsl/enums.html	                        (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enums.html	2019-07-20 12:40:22 UTC (rev 247666)
@@ -0,0 +1,422 @@
+<!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.failTrickyDoubleZero = async () => {
+    const program = `
+        enum E {
+            F,
+            G = -2,
+            H,
+            I // should get zero, and that's a dupe.
+        }
+        int foo()
+        {
+            E e = E.Foo;
+            return 42;
+        }
+    `;
+    await checkFail(program);
+};
+
+whlslTests.simpleDeclaration = async () => {
+    const program = `
+        enum E {
+            Foo,
+            Bar
+        }
+        int foo()
+        {
+            E e = E.Foo;
+            return 42;
+        }
+    `;
+    assert_equals(await callIntFunction(program, "foo", []), 42);
+};
+
+whlslTests.enumWithUintBase = async () => {
+    let program = `
+        enum Foo : uint {
+            War,
+            Famine,
+            Pestilence,
+            Death
+        }
+
+        Foo _war()
+        {
+            return Foo.War;
+        }
+
+        bool war()
+        {
+            return _war() == Foo.War;
+        }
+
+        Foo _famine()
+        {
+            return Foo.Famine;
+        }
+
+        bool famine()
+        {
+            return _famine() == Foo.Famine;
+        }
+
+        Foo _pestilence()
+        {
+            return Foo.Pestilence;
+        }
+
+        bool pestilence()
+        {
+            return _pestilence() == Foo.Pestilence;
+        }
+
+        Foo _death()
+        {
+            return Foo.Death;
+        }
+
+        bool death()
+        {
+            return _death() == Foo.Death;
+        }
+
+        bool _equals(Foo a, Foo b)
+        {
+            return a == b;
+        }
+
+        bool _notEquals(Foo a, Foo b)
+        {
+            return a != b;
+        }
+
+        bool testSimpleEqual()
+        {
+            return _equals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherEqual()
+        {
+            return _equals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotEqual()
+        {
+            return _equals(Foo.Famine, Foo.Death);
+        }
+
+        bool testSimpleNotEqual()
+        {
+            return _notEquals(Foo.War, Foo.War);
+        }
+
+        bool testAnotherNotEqual()
+        {
+            return _notEquals(Foo.Pestilence, Foo.Pestilence);
+        }
+
+        bool testNotNotEqual()
+        {
+            return _notEquals(Foo.Famine, Foo.Death);
+        }
+
+        uint _uintWar()
+        {
+            return uint(_war());
+        }
+
+        uint uintWar()
+        {
+            return _uintWar();
+        }
+
+        uint _uintFamine()
+        {
+            return uint(_famine());
+        }
+
+        uint uintFamine()
+        {
+            return _uintFamine();
+        }
+
+        uint _uintPestilence()
+        {
+            return uint(_pestilence());
+        }
+
+        uint uintPestilence()
+        {
+            return _uintPestilence();
+        }
+
+        uint _uintDeath()
+        {
+            return uint(_death());
+        }
+
+        uint uintDeath()
+        {
+            return _uintDeath();
+        }
+
+        bool uintWarBackwards()
+        {
+            return Foo(_uintWar()) == Foo.War;
+        }
+
+        bool uintFamineBackwards()
+        {
+            return Foo(_uintFamine()) == Foo.Famine;
+        }
+
+        bool uintPestilenceBackwards()
+        {
+            return Foo(_uintPestilence()) == Foo.Pestilence;
+        }
+
+        bool uintDeathBackwards()
+        {
+            return Foo(_uintDeath()) == Foo.Death;
+        }
+    `;
+
+    assert_equals(await callBoolFunction(program, "war", []), true);
+    assert_equals(await callBoolFunction(program, "famine", []), true);
+    assert_equals(await callBoolFunction(program, "pestilence", []), true);
+    assert_equals(await callBoolFunction(program, "death", []), true);
+
+    assert_equals(await callBoolFunction(program, "testSimpleEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testAnotherEqual", []), true);
+    assert_equals(await callBoolFunction(program, "testNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testSimpleNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testAnotherNotEqual", []), false);
+    assert_equals(await callBoolFunction(program, "testNotNotEqual", []), true);
+    assert_equals(await callUintFunction(program, "uintWar", []), 0);
+    assert_equals(await callUintFunction(program, "uintFamine", []), 1);
+    assert_equals(await callUintFunction(program, "uintPestilence", []), 2);
+    assert_equals(await callUintFunction(program, "uintDeath", []), 3);
+
+    assert_equals(await callBoolFunction(program, "uintWarBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "uintFamineBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "uintPestilenceBackwards", []), true);
+    assert_equals(await callBoolFunction(program, "uintDeathBackwards", []), true);
+};
+
+
+whlslTests.enumWithoutZero = async () => {
+    await checkFail(
+        `
+            enum Foo {
+                War = 72,
+                Famine = 64,
+                Pestilence = 23,
+                Death = -42
+            }
+        `);
+};
+
+whlslTests.enumNegativeUint = async () => {
+    await checkFail(
+        `
+            enum Foo : uint {
+                War,
+                Famine,
+                Pestilence,
+                Death = -42
+            }
+        `);
+};
+
+whlslTests.pastIntMax = async () => {
+    await checkFail(
+        `
+            enum Foo : int {
+                War = 2147483647,
+                Famine,
+                Pestilence = 0,
+                Death
+            }
+        `);
+};
+
+whlslTests.enumWithSomeManualValues = async () => {
+    let program = `
+        enum Foo {
+            War = 72,
+            Famine,
+            Pestilence = 0,
+            Death
+        }
+        int war()
+        {
+            return int(Foo.War);
+        }
+        int famine()
+        {
+            return int(Foo.Famine);
+        }
+        int pestilence()
+        {
+            return int(Foo.Pestilence);
+        }
+        int death()
+        {
+            return int(Foo.Death);
+        }
+    `;
+    assert_equals(await callIntFunction(program, "war", []), 72);
+    assert_equals(await callIntFunction(program, "famine", []), 73);
+    assert_equals(await callIntFunction(program, "pestilence", []), 0);
+    assert_equals(await callIntFunction(program, "death", []), 1);
+};
+
+
+whlslTests.enumSwitchBreakExhaustive = async () => {
+    let program = `
+        enum Foo {
+            A, B, C
+        }
+        int foo(int x)
+        {
+            int result = 0;
+            switch (Foo(x)) {
+            case Foo.A:
+                result += 27;
+                break;
+            case Foo.B:
+                result += 7624;
+                break;
+            case Foo.C:
+                result += 49;
+                break;
+            }
+            return result;
+        }
+    `;
+    assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 27);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(1)]), 7624);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(2)]), 49);
+}
+
+
+whlslTests.enumSwitchBreakNotQuiteExhaustiveWithDefault = async () => {
+    let program = `
+        enum Foo {
+            A, B, C
+        }
+        int foo(int x)
+        {
+            int result = 0;
+            switch (Foo(x)) {
+            case Foo.A:
+                result += 27;
+                break;
+            case Foo.B:
+                result += 7624;
+                break;
+            default:
+                result += 49;
+                break;
+            }
+            return result;
+        }
+    `;
+    assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 27);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(1)]), 7624);
+    assert_equals(await callIntFunction(program, "foo", [makeInt(2)]), 49);
+}
+
+whlslTests.enumWithManualValues = async () => {
+    let program = `
+        enum Foo {
+            War = 72,
+            Famine = 0,
+            Pestilence = 23,
+            Death = -42
+        }
+        int war()
+        {
+            return int(Foo.War);
+        }
+        int famine()
+        {
+            return int(Foo.Famine);
+        }
+        int pestilence()
+        {
+            return int(Foo.Pestilence);
+        }
+        int death()
+        {
+            return int(Foo.Death);
+        }
+    `;
+    assert_equals(await callIntFunction(program, "war", []), 72);
+    assert_equals(await callIntFunction(program, "famine", []), 0);
+    assert_equals(await callIntFunction(program, "pestilence", []), 23);
+    assert_equals(await callIntFunction(program, "death", []), -42);
+};
+
+whlslTests.enumSwitchBreakNotQuiteExhaustive = async () => {
+    await checkFail(
+        `
+            enum Foo {
+                A, B, C, D
+            }
+            int foo(Foo x)
+            {
+                int result = 0;
+                switch (x) {
+                case Foo.A:
+                    result += 27;
+                    break;
+                case Foo.B:
+                    result += 7624;
+                    break;
+                case Foo.C:
+                    result += 49;
+                    break;
+                }
+                return result;
+            }
+        `);
+};
+
+whlslTests.copyConstructors = async () => {
+    const program = `
+        enum Weekday {
+            Monday,
+            Tuesday,
+            Wednesday,
+            Thursday,
+            Pizzaday
+        }
+        int foo()
+        {
+            Weekday x = Weekday.Monday;
+            Weekday y = Weekday(x);
+            float4 z = float4(1, 2, 3, 4);
+            float4 w = float4(z);
+            float2x2 p;
+            float2x2 q = float2x2(p);
+            return 42;
+        }
+    `;
+    assert_equals(await callIntFunction(program, "foo", []), 42);
+};
+
+runTests(whlslTests);
+</script>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (247665 => 247666)


--- trunk/Source/WebCore/ChangeLog	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/ChangeLog	2019-07-20 12:40:22 UTC (rev 247666)
@@ -1,3 +1,52 @@
+2019-07-20  Saam Barati  <[email protected]>
+
+        [WHLSL] Make enums work
+        https://bugs.webkit.org/show_bug.cgi?id=199634
+
+        Reviewed by Robin Morisset.
+
+        This patch makes enums work. There were a couple changes needed to do this work:
+        1. In the metal codegen, we were emitting code like "EnumType.EnumValue"
+        instead of "EnumType::EnumValue".
+        
+        2. Inside the enumeration declaration AST node, we track enums in a HashMap
+        because we need to look up names of enum values in that map in parts of the
+        compiler. However, HashMap is not ordered but we were relying on the iteration
+        order of it to generate the default values of enums. E.g, in: "enum { Foo = 10, Bar }",
+        Bar should be 11. However, there was no guarantee that we iterated over Bar
+        after Foo. Instead, we just track the default values as we parse an enum
+        inside of the parser. This allows us to continue to keep using HashMap without
+        also adding an auxiliary data structure (or using more memory in some way) to
+        track the order of the enum elements.
+        
+        I've also filed a patch to do follow-up work on implementing the correct
+        operational behavior when constructing an enum from its underlying type when
+        the underlying value does not match any enum value:
+        https://bugs.webkit.org/show_bug.cgi?id=199853
+
+        Tests: webgpu/whlsl/enums-2.html
+               webgpu/whlsl/enums.html
+
+        * Modules/webgpu/WHLSL/AST/WHLSLEnumerationMember.h:
+        (WebCore::WHLSL::AST::EnumerationMember::EnumerationMember):
+        (WebCore::WHLSL::AST::EnumerationMember::value):
+        (WebCore::WHLSL::AST::EnumerationMember::setValue): Deleted.
+        * Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp:
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::visit):
+        (WebCore::WHLSL::Metal::FunctionDefinitionWriter::constantExpressionString):
+        * Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp:
+        (WebCore::WHLSL::Metal::TypeNamer::emitNamedTypeDefinition):
+        * Modules/webgpu/WHLSL/WHLSLASTDumper.cpp:
+        (WebCore::WHLSL::ASTDumper::visit):
+        * Modules/webgpu/WHLSL/WHLSLChecker.cpp:
+        (WebCore::WHLSL::Checker::visit):
+        * Modules/webgpu/WHLSL/WHLSLParser.cpp:
+        (WebCore::WHLSL::Parser::parseEnumerationDefinition):
+        (WebCore::WHLSL::Parser::parseEnumerationMember):
+        * Modules/webgpu/WHLSL/WHLSLParser.h:
+        * Modules/webgpu/WHLSL/WHLSLVisitor.cpp:
+        (WebCore::WHLSL::Visitor::visit):
+
 2019-07-19  Youenn Fablet  <[email protected]>
 
         Remote WebInspector should enable mock capture devices in UIProcess if doing it in WebProcess

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLEnumerationMember.h (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLEnumerationMember.h	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLEnumerationMember.h	2019-07-20 12:40:22 UTC (rev 247666)
@@ -41,10 +41,10 @@
 
 class EnumerationMember {
 public:
-    EnumerationMember(CodeLocation location, String&& name, Optional<ConstantExpression>&& value = WTF::nullopt)
+    EnumerationMember(CodeLocation location, String&& name, int64_t value)
         : m_codeLocation(location)
         , m_name(WTFMove(name))
-        , m_value(WTFMove(value))
+        , m_value(value)
     {
     }
 
@@ -55,18 +55,12 @@
 
     const CodeLocation& codeLocation() const { return m_codeLocation; }
     String name() { return m_name; }
-    Optional<ConstantExpression>& value() { return m_value; }
+    int64_t value() { return m_value; }
 
-    void setValue(ConstantExpression&& value)
-    {
-        ASSERT(!m_value);
-        m_value = WTFMove(value);
-    }
-
 private:
     CodeLocation m_codeLocation;
     String m_name;
-    Optional<ConstantExpression> m_value;
+    int64_t m_value;
 };
 
 using EnumerationMembers = Vector<EnumerationMember>;

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLFunctionWriter.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -475,7 +475,7 @@
     ASSERT(enumerationMemberLiteral.enumerationDefinition());
     auto variableName = generateNextVariableName();
     auto mangledTypeName = m_typeNamer.mangledNameForType(enumerationMemberLiteral.resolvedType());
-    m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = ", mangledTypeName, '.', m_typeNamer.mangledNameForEnumerationMember(*enumerationMemberLiteral.enumerationMember()), ";\n"));
+    m_stringBuilder.append(makeString(mangledTypeName, ' ', variableName, " = ", mangledTypeName, "::", m_typeNamer.mangledNameForEnumerationMember(*enumerationMemberLiteral.enumerationMember()), ";\n"));
     appendRightValue(enumerationMemberLiteral, variableName);
 }
 
@@ -700,7 +700,7 @@
     }, [&](AST::EnumerationMemberLiteral& enumerationMemberLiteral) -> String {
         ASSERT(enumerationMemberLiteral.enumerationDefinition());
         ASSERT(enumerationMemberLiteral.enumerationDefinition());
-        return makeString(m_typeNamer.mangledNameForType(*enumerationMemberLiteral.enumerationDefinition()), '.', m_typeNamer.mangledNameForEnumerationMember(*enumerationMemberLiteral.enumerationMember()));
+        return makeString(m_typeNamer.mangledNameForType(*enumerationMemberLiteral.enumerationDefinition()), "::", m_typeNamer.mangledNameForEnumerationMember(*enumerationMemberLiteral.enumerationMember()));
     }));
 }
 

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLTypeNamer.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -413,7 +413,7 @@
         auto& baseType = enumerationDefinition.type().unifyNode();
         stringBuilder.append(makeString("enum class ", mangledNameForType(enumerationDefinition), " : ", mangledNameForType(downcast<AST::NamedType>(baseType)), " {\n"));
         for (auto& enumerationMember : enumerationDefinition.enumerationMembers())
-            stringBuilder.append(makeString("    ", mangledNameForEnumerationMember(enumerationMember), ",\n"));
+            stringBuilder.append(makeString("    ", mangledNameForEnumerationMember(enumerationMember), " = ", enumerationMember.get().value(), ",\n"));
         stringBuilder.append("};\n");
     } else if (is<AST::NativeTypeDeclaration>(namedType)) {
         // Native types already have definitions. There's nothing to do.

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLASTDumper.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -134,11 +134,7 @@
 
 void ASTDumper::visit(AST::EnumerationMember& enumerationMember)
 {
-    m_out.print(enumerationMember.name());
-    if (enumerationMember.value()) {
-        m_out.print(" = ");
-        visit(*enumerationMember.value());
-    }
+    m_out.print(enumerationMember.name(), " = ", enumerationMember.value());
 }
 
 void ASTDumper::visit(AST::FunctionDefinition& functionDefinition)

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLChecker.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -627,17 +627,6 @@
     }));
 }
 
-static Optional<UniqueRef<AST::UnnamedType>> matchAndCommit(ResolvingType& resolvingType, AST::NamedType& namedType)
-{
-    return resolvingType.visit(WTF::makeVisitor([&](UniqueRef<AST::UnnamedType>& resolvingType) -> Optional<UniqueRef<AST::UnnamedType>> {
-        if (matches(resolvingType, namedType))
-            return resolvingType->clone();
-        return WTF::nullopt;
-    }, [&](RefPtr<ResolvableTypeReference>& resolvingType) -> Optional<UniqueRef<AST::UnnamedType>> {
-        return matchAndCommit(namedType, resolvingType->resolvableType());
-    }));
-}
-
 static Optional<UniqueRef<AST::UnnamedType>> commit(ResolvingType& resolvingType)
 {
     return resolvingType.visit(WTF::makeVisitor([&](UniqueRef<AST::UnnamedType>& unnamedType) -> Optional<UniqueRef<AST::UnnamedType>> {
@@ -651,6 +640,7 @@
 
 void Checker::visit(AST::EnumerationDefinition& enumerationDefinition)
 {
+    bool isSigned;
     auto* baseType = ([&]() -> AST::NativeTypeDeclaration* {
         checkErrorAndVisit(enumerationDefinition.type());
         auto& baseType = enumerationDefinition.type().unifyNode();
@@ -662,6 +652,7 @@
         auto& nativeTypeDeclaration = downcast<AST::NativeTypeDeclaration>(namedType);
         if (!nativeTypeDeclaration.isInt())
             return nullptr;
+        isSigned = nativeTypeDeclaration.isSigned();
         return &nativeTypeDeclaration;
     })();
     if (!baseType) {
@@ -671,71 +662,25 @@
 
     auto enumerationMembers = enumerationDefinition.enumerationMembers();
 
-    auto matchAndCommitMember = [&](AST::EnumerationMember& member) -> bool {
-        return member.value()->visit(WTF::makeVisitor([&](AST::_expression_& value) -> bool {
-            auto valueInfo = recurseAndGetInfo(value);
-            if (!valueInfo)
-                return false;
-            return static_cast<bool>(matchAndCommit(valueInfo->resolvingType, *baseType));
-        }));
-    };
-
     for (auto& member : enumerationMembers) {
-        if (!member.get().value())
-            continue;
-
-        if (!matchAndCommitMember(member)) {
-            setError();
-            return;
-        }
-    }
-
-    int64_t nextValue = 0;
-    for (auto& member : enumerationMembers) {
-        if (member.get().value()) {
-            auto value = member.get().value()->visit(WTF::makeVisitor([&](AST::IntegerLiteral& integerLiteral) -> int64_t {
-                return integerLiteral.valueForSelectedType();
-            }, [&](AST::UnsignedIntegerLiteral& unsignedIntegerLiteral) -> int64_t {
-                return unsignedIntegerLiteral.valueForSelectedType();
-            }, [&](auto&) -> int64_t {
-                ASSERT_NOT_REACHED();
-                return 0;
-            }));
-            nextValue = baseType->successor()(value);
+        int64_t value = member.get().value();
+        if (isSigned) {
+            if (static_cast<int64_t>(static_cast<int32_t>(value)) != value) {
+                setError();
+                return;
+            }
         } else {
-            if (nextValue > std::numeric_limits<int>::max()) {
-                ASSERT(nextValue <= std::numeric_limits<unsigned>::max());
-                member.get().setValue(AST::ConstantExpression(AST::UnsignedIntegerLiteral(member.get().codeLocation(), static_cast<unsigned>(nextValue))));
-            }
-            ASSERT(nextValue >= std::numeric_limits<int>::min());
-            member.get().setValue(AST::ConstantExpression(AST::IntegerLiteral(member.get().codeLocation(), static_cast<int>(nextValue))));
-
-            if (!matchAndCommitMember(member)) {
+            if (static_cast<int64_t>(static_cast<uint32_t>(value)) != value) {
                 setError();
                 return;
             }
-
-            nextValue = baseType->successor()(nextValue);
         }
     }
 
-    auto getValue = [&](AST::EnumerationMember& member) -> int64_t {
-        ASSERT(member.value());
-        auto value = member.value()->visit(WTF::makeVisitor([&](AST::IntegerLiteral& integerLiteral) -> int64_t {
-            return integerLiteral.value();
-        }, [&](AST::UnsignedIntegerLiteral& unsignedIntegerLiteral) -> int64_t {
-            return unsignedIntegerLiteral.value();
-        }, [&](auto&) -> int64_t {
-            ASSERT_NOT_REACHED();
-            return 0;
-        }));
-        return value;
-    };
-
     for (size_t i = 0; i < enumerationMembers.size(); ++i) {
-        auto value = getValue(enumerationMembers[i].get());
+        auto value = enumerationMembers[i].get().value();
         for (size_t j = i + 1; j < enumerationMembers.size(); ++j) {
-            auto otherValue = getValue(enumerationMembers[j].get());
+            auto otherValue = enumerationMembers[j].get().value();
             if (value == otherValue) {
                 setError();
                 return;
@@ -745,7 +690,7 @@
 
     bool foundZero = false;
     for (auto& member : enumerationMembers) {
-        if (!getValue(member.get())) {
+        if (!member.get().value()) {
             foundZero = true;
             break;
         }

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -826,7 +826,9 @@
 
     CONSUME_TYPE(leftCurlyBracket, LeftCurlyBracket);
 
-    PARSE(firstEnumerationMember, EnumerationMember);
+    int64_t nextValue = 0;
+    PARSE(firstEnumerationMember, EnumerationMember, nextValue);
+    nextValue = firstEnumerationMember->value() + 1;
 
     AST::EnumerationDefinition result({ }, name->stringView(m_lexer).toString(), WTFMove(*type));
     auto success = result.add(WTFMove(*firstEnumerationMember));
@@ -834,7 +836,8 @@
         return fail("Cannot add enumeration member"_str);
 
     while (tryType(Token::Type::Comma)) {
-        PARSE(member, EnumerationMember);
+        PARSE(member, EnumerationMember, nextValue);
+        nextValue = member->value() + 1;
         success = result.add(WTFMove(*member));
         if (!success)
             return fail("Cannot add enumeration member"_str);
@@ -846,7 +849,7 @@
     return WTFMove(result);
 }
 
-auto Parser::parseEnumerationMember() -> Expected<AST::EnumerationMember, Error>
+auto Parser::parseEnumerationMember(int64_t defaultValue) -> Expected<AST::EnumerationMember, Error>
 {
     CONSUME_TYPE(identifier, Identifier);
     auto name = identifier->stringView(m_lexer).toString();
@@ -853,9 +856,23 @@
 
     if (tryType(Token::Type::EqualsSign)) {
         PARSE(constantExpression, ConstantExpression);
-        return AST::EnumerationMember(*identifier, WTFMove(name), WTFMove(*constantExpression));
+
+        Optional<int64_t> value;
+        constantExpression->visit(WTF::makeVisitor([&](AST::IntegerLiteral& integerLiteral) {
+            value = integerLiteral.value();
+        }, [&](AST::UnsignedIntegerLiteral& unsignedIntegerLiteral) {
+            value = unsignedIntegerLiteral.value();
+        }, [&](AST::FloatLiteral&) {
+        }, [&](AST::NullLiteral&) {
+        }, [&](AST::BooleanLiteral&) {
+        }, [&](AST::EnumerationMemberLiteral&) {
+        }));
+
+        if (!value)
+            return Unexpected<Error>("enum initialization values can only be an int or uint constant.");
+        return AST::EnumerationMember(*identifier, WTFMove(name), *value);
     }
-    return AST::EnumerationMember(*identifier, WTFMove(name));
+    return AST::EnumerationMember(*identifier, WTFMove(name), defaultValue);
 }
 
 auto Parser::parseNativeTypeDeclaration() -> Expected<AST::NativeTypeDeclaration, Error>

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.h (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.h	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.h	2019-07-20 12:40:22 UTC (rev 247666)
@@ -183,7 +183,7 @@
     Expected<AST::StructureElement, Error> parseStructureElement();
     Expected<AST::StructureDefinition, Error> parseStructureDefinition();
     Expected<AST::EnumerationDefinition, Error> parseEnumerationDefinition();
-    Expected<AST::EnumerationMember, Error> parseEnumerationMember();
+    Expected<AST::EnumerationMember, Error> parseEnumerationMember(int64_t defaultValue);
     Expected<AST::NativeTypeDeclaration, Error> parseNativeTypeDeclaration();
     Expected<AST::NumThreadsFunctionAttribute, Error> parseNumThreadsFunctionAttribute();
     Expected<AST::AttributeBlock, Error> parseAttributeBlock();

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLVisitor.cpp (247665 => 247666)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLVisitor.cpp	2019-07-20 02:01:56 UTC (rev 247665)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLVisitor.cpp	2019-07-20 12:40:22 UTC (rev 247666)
@@ -143,10 +143,8 @@
         checkErrorAndVisit(*structureElement.semantic());
 }
 
-void Visitor::visit(AST::EnumerationMember& enumerationMember)
+void Visitor::visit(AST::EnumerationMember&)
 {
-    if (enumerationMember.value())
-        checkErrorAndVisit(*enumerationMember.value());
 }
 
 void Visitor::visit(AST::FunctionDeclaration& functionDeclaration)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to