[glsl][ir] Support `CoreUnary`.

This CL adds support for the `CoreUnary` instruction to the GLSL IR
backend.

Bug: 42251044
Change-Id: I25e7ab748a560c9ab2b6818dd39e215c78f2a757
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/204495
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/glsl/writer/BUILD.bazel b/src/tint/lang/glsl/writer/BUILD.bazel
index 776be58..833b2ae 100644
--- a/src/tint/lang/glsl/writer/BUILD.bazel
+++ b/src/tint/lang/glsl/writer/BUILD.bazel
@@ -94,6 +94,7 @@
     "constant_test.cc",
     "function_test.cc",
     "type_test.cc",
+    "unary_test.cc",
     "var_and_let_test.cc",
   ] + select({
     ":tint_build_glsl_validator": [
diff --git a/src/tint/lang/glsl/writer/BUILD.cmake b/src/tint/lang/glsl/writer/BUILD.cmake
index e3d52c8..9d8ff03 100644
--- a/src/tint/lang/glsl/writer/BUILD.cmake
+++ b/src/tint/lang/glsl/writer/BUILD.cmake
@@ -108,6 +108,7 @@
   lang/glsl/writer/constant_test.cc
   lang/glsl/writer/function_test.cc
   lang/glsl/writer/type_test.cc
+  lang/glsl/writer/unary_test.cc
   lang/glsl/writer/var_and_let_test.cc
 )
 
diff --git a/src/tint/lang/glsl/writer/BUILD.gn b/src/tint/lang/glsl/writer/BUILD.gn
index 1d387bf..bef103e 100644
--- a/src/tint/lang/glsl/writer/BUILD.gn
+++ b/src/tint/lang/glsl/writer/BUILD.gn
@@ -98,6 +98,7 @@
         "constant_test.cc",
         "function_test.cc",
         "type_test.cc",
+        "unary_test.cc",
         "var_and_let_test.cc",
       ]
       deps = [
diff --git a/src/tint/lang/glsl/writer/printer/printer.cc b/src/tint/lang/glsl/writer/printer/printer.cc
index 22fb742..84fe10a 100644
--- a/src/tint/lang/glsl/writer/printer/printer.cc
+++ b/src/tint/lang/glsl/writer/printer/printer.cc
@@ -561,9 +561,10 @@
             [&](const core::ir::Constant* c) { EmitConstant(out, c); },
             [&](const core::ir::InstructionResult* r) {
                 tint::Switch(
-                    r->Instruction(),                                            //
-                    [&](const core::ir::CoreBinary* b) { EmitBinary(out, b); },  //
+                    r->Instruction(),  //
+                    [&](const core::ir::CoreBinary* b) { EmitBinary(out, b); },
                     [&](const core::ir::CoreBuiltinCall* c) { EmitCoreBuiltinCall(out, c); },
+                    [&](const core::ir::CoreUnary* u) { EmitUnary(out, u); },
                     [&](const core::ir::Let* l) { out << NameOf(l->Result(0)); },
                     [&](const core::ir::UserCall* c) { EmitUserCall(out, c); },
                     [&](const core::ir::Var* var) { out << NameOf(var->Result(0)); },
@@ -575,6 +576,29 @@
             TINT_ICE_ON_NO_MATCH);
     }
 
+    void EmitUnary(StringStream& out, const core::ir::CoreUnary* u) {
+        switch (u->Op()) {
+            case core::UnaryOp::kNegation:
+                out << "-";
+                break;
+            case core::UnaryOp::kComplement:
+                out << "~";
+                break;
+            case core::UnaryOp::kNot:
+                if (u->Val()->Type()->Is<core::type::Scalar>()) {
+                    out << "!";
+                } else {
+                    out << "not";
+                }
+                break;
+            default:
+                TINT_UNIMPLEMENTED() << u->Op();
+        }
+        out << "(";
+        EmitValue(out, u->Val());
+        out << ")";
+    }
+
     /// Emit a binary instruction
     /// @param b the binary instruction
     void EmitBinary(StringStream& out, const core::ir::CoreBinary* b) {
diff --git a/src/tint/lang/glsl/writer/unary_test.cc b/src/tint/lang/glsl/writer/unary_test.cc
new file mode 100644
index 0000000..e70110b
--- /dev/null
+++ b/src/tint/lang/glsl/writer/unary_test.cc
@@ -0,0 +1,125 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// 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.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+//    contributors may be used to endorse or promote products derived from
+//    this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT HOLDER OR 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 "src/tint/lang/core/fluent_types.h"
+#include "src/tint/lang/glsl/writer/helper_test.h"
+#include "src/tint/utils/text/string_stream.h"
+
+using namespace tint::core::number_suffixes;  // NOLINT
+using namespace tint::core::fluent_types;     // NOLINT
+
+namespace tint::glsl::writer {
+namespace {
+
+// TODO(dsinclair): Test address of is gone
+TEST_F(GlslWriterTest, DISABLED_AddressOf) {}
+
+// TODO(dsinclair): Test indirection is gone
+TEST_F(GlslWriterTest, Indirection) {}
+
+TEST_F(GlslWriterTest, Complement) {
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+    func->SetWorkgroupSize(1, 1, 1);
+    b.Append(func->Block(), [&] {
+        auto* l = b.Let("left", b.Constant(1_u));
+        auto* op = b.Complement(ty.u32(), l);
+        b.Let("val", op);
+        b.Return(func);
+    });
+
+    ASSERT_TRUE(Generate()) << err_ << output_.glsl;
+    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+  uint left = 1u;
+  uint val = ~(left);
+}
+)");
+}
+
+TEST_F(GlslWriterTest, Not_Scalar) {
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+    func->SetWorkgroupSize(1, 1, 1);
+    b.Append(func->Block(), [&] {
+        auto* l = b.Let("left", b.Constant(false));
+        auto* op = b.Not(ty.bool_(), l);
+        b.Let("val", op);
+        b.Return(func);
+    });
+
+    ASSERT_TRUE(Generate()) << err_ << output_.glsl;
+    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+  bool left = false;
+  bool val = !(left);
+}
+)");
+}
+
+TEST_F(GlslWriterTest, Not_Vector) {
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+    func->SetWorkgroupSize(1, 1, 1);
+    b.Append(func->Block(), [&] {
+        auto* l = b.Let("left", b.Splat(ty.vec3<bool>(), false));
+        auto* op = b.Not(ty.vec3<bool>(), l);
+        b.Let("val", op);
+        b.Return(func);
+    });
+
+    ASSERT_TRUE(Generate()) << err_ << output_.glsl;
+    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+  bvec3 left = bvec3(false);
+  bvec3 val = not(left);
+}
+)");
+}
+
+TEST_F(GlslWriterTest, Negation) {
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kCompute);
+    func->SetWorkgroupSize(1, 1, 1);
+    b.Append(func->Block(), [&] {
+        auto* l = b.Let("left", b.Constant(1_i));
+        auto* op = b.Negation(ty.i32(), l);
+        b.Let("val", op);
+        b.Return(func);
+    });
+
+    ASSERT_TRUE(Generate()) << err_ << output_.glsl;
+    EXPECT_EQ(output_.glsl, GlslHeader() + R"(
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+  int left = 1;
+  int val = -(left);
+}
+)");
+}
+
+}  // namespace
+}  // namespace tint::glsl::writer
diff --git a/test/tint/expressions/unary/complement/complement.wgsl.expected.ir.glsl b/test/tint/expressions/unary/complement/complement.wgsl.expected.ir.glsl
index cb792a4..67fa5de 100644
--- a/test/tint/expressions/unary/complement/complement.wgsl.expected.ir.glsl
+++ b/test/tint/expressions/unary/complement/complement.wgsl.expected.ir.glsl
@@ -1,11 +1,17 @@
-SKIP: FAILED
+#version 310 es
 
-<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::CoreUnary
-********************************************************************
-*  The tint shader compiler has encountered an unexpected error.   *
-*                                                                  *
-*  Please help us fix this issue by submitting a bug report at     *
-*  crbug.com/tint with the source program that triggered the bug.  *
-********************************************************************
-
-tint executable returned error: signal: illegal instruction
+int i(int x) {
+  return ~(x);
+}
+uint u(uint x) {
+  return ~(x);
+}
+ivec4 vi(ivec4 x) {
+  return ~(x);
+}
+uvec4 vu(uvec4 x) {
+  return ~(x);
+}
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+}
diff --git a/test/tint/expressions/unary/negate/negate.wgsl.expected.ir.glsl b/test/tint/expressions/unary/negate/negate.wgsl.expected.ir.glsl
index cb792a4..1048469 100644
--- a/test/tint/expressions/unary/negate/negate.wgsl.expected.ir.glsl
+++ b/test/tint/expressions/unary/negate/negate.wgsl.expected.ir.glsl
@@ -1,11 +1,11 @@
-SKIP: FAILED
+#version 310 es
 
-<dawn>/src/tint/lang/glsl/writer/printer/printer.cc:482 internal compiler error: Switch() matched no cases. Type: tint::core::ir::CoreUnary
-********************************************************************
-*  The tint shader compiler has encountered an unexpected error.   *
-*                                                                  *
-*  Please help us fix this issue by submitting a bug report at     *
-*  crbug.com/tint with the source program that triggered the bug.  *
-********************************************************************
-
-tint executable returned error: signal: illegal instruction
+int i(int x) {
+  return -(x);
+}
+ivec4 vi(ivec4 x) {
+  return -(x);
+}
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+}