Remove type param from Unary Complement

Remove the need to provide a `type` to the unary complement builder
method. Instead, infer it from the type of the parameter.

Change-Id: Id73a774ce29e1ff53373bc6da6cf17530b8e7d98
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/272914
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/core/ir/builder.h b/src/tint/lang/core/ir/builder.h
index e2aa2f5..6ec23f7 100644
--- a/src/tint/lang/core/ir/builder.h
+++ b/src/tint/lang/core/ir/builder.h
@@ -1010,32 +1010,21 @@
 
     /// Creates an op for `op val`
     /// @param op the unary operator
-    /// @tparam TYPE the result type of the binary expression
     /// @param val the value of the operation
     /// @returns the operation
-    template <typename TYPE, typename VAL>
+    template <typename VAL>
     ir::CoreUnary* Unary(UnaryOp op, VAL&& val) {
-        auto* type = ir.Types().Get<TYPE>();
-        return Unary(op, type, std::forward<VAL>(val));
+        auto* value = Value(std::forward<VAL>(val));
+        return Append(
+            ir.CreateInstruction<ir::CoreUnary>(InstructionResult(value->Type()), op, value));
     }
 
     /// Creates a Complement operation
-    /// @param type the result type of the expression
     /// @param val the value
     /// @returns the operation
     template <typename VAL>
-    ir::CoreUnary* Complement(const core::type::Type* type, VAL&& val) {
-        return Unary(UnaryOp::kComplement, type, std::forward<VAL>(val));
-    }
-
-    /// Creates a Complement operation
-    /// @tparam TYPE the result type of the expression
-    /// @param val the value
-    /// @returns the operation
-    template <typename TYPE, typename VAL>
     ir::CoreUnary* Complement(VAL&& val) {
-        auto* type = ir.Types().Get<TYPE>();
-        return Complement(type, std::forward<VAL>(val));
+        return Unary(UnaryOp::kComplement, std::forward<VAL>(val));
     }
 
     /// Creates a Negation operation
diff --git a/src/tint/lang/core/ir/core_unary_test.cc b/src/tint/lang/core/ir/core_unary_test.cc
index 8074ad6..ac9632d 100644
--- a/src/tint/lang/core/ir/core_unary_test.cc
+++ b/src/tint/lang/core/ir/core_unary_test.cc
@@ -41,7 +41,7 @@
 using IR_UnaryDeathTest = IR_UnaryTest;
 
 TEST_F(IR_UnaryTest, CreateComplement) {
-    auto* inst = b.Complement(mod.Types().i32(), 4_i);
+    auto* inst = b.Complement(4_i);
 
     ASSERT_TRUE(inst->Is<Unary>());
     EXPECT_EQ(inst->Op(), UnaryOp::kComplement);
@@ -103,7 +103,7 @@
 }
 
 TEST_F(IR_UnaryTest, Clone) {
-    auto* inst = b.Complement(mod.Types().i32(), 4_i);
+    auto* inst = b.Complement(4_i);
     auto* new_inst = clone_ctx.Clone(inst);
 
     EXPECT_NE(inst, new_inst);
diff --git a/src/tint/lang/core/ir/evaluator_test.cc b/src/tint/lang/core/ir/evaluator_test.cc
index c3b0a0d..4631036 100644
--- a/src/tint/lang/core/ir/evaluator_test.cc
+++ b/src/tint/lang/core/ir/evaluator_test.cc
@@ -72,7 +72,7 @@
 }
 
 TEST_F(IR_EvaluatorTest, Unary) {
-    auto* inst = b.Complement(mod.Types().i32(), 4_i);
+    auto* inst = b.Complement(4_i);
     auto res = Eval(b, inst);
     ASSERT_EQ(res, Success);
 
diff --git a/src/tint/lang/core/ir/transform/builtin_polyfill.cc b/src/tint/lang/core/ir/transform/builtin_polyfill.cc
index 7ce3628..30c1e92 100644
--- a/src/tint/lang/core/ir/transform/builtin_polyfill.cc
+++ b/src/tint/lang/core/ir/transform/builtin_polyfill.cc
@@ -702,7 +702,7 @@
             auto* x = input;
             if (result_ty->IsSignedIntegerScalarOrVector()) {
                 x = b.Bitcast(uint_ty, x)->Result();
-                auto* inverted = b.Complement(uint_ty, x);
+                auto* inverted = b.Complement(x);
                 x = b.Call(uint_ty, core::BuiltinFn::kSelect, inverted, x,
                            b.LessThan(bool_ty, x, V(0x80000000)))
                         ->Result();
@@ -870,8 +870,7 @@
                     auto* s3 = b.Call(result_ty, core::BuiltinFn::kSelect, f3, t3,
                                       b.LessThan<bool>(offset, 32_u));
                     auto* result_lhs = b.And(result_ty, s3, mask_as_result_type(mask));
-                    auto* result_rhs =
-                        b.And(result_ty, e, mask_as_result_type(b.Complement<u32>(mask)));
+                    auto* result_rhs = b.And(result_ty, e, mask_as_result_type(b.Complement(mask)));
                     auto* result = b.Or(result_ty, result_lhs, result_rhs);
                     result->SetResult(call->DetachResult());
                 });
diff --git a/src/tint/lang/core/ir/transform/signed_integer_polyfill.cc b/src/tint/lang/core/ir/transform/signed_integer_polyfill.cc
index 795e3ff..bfc7e39 100644
--- a/src/tint/lang/core/ir/transform/signed_integer_polyfill.cc
+++ b/src/tint/lang/core/ir/transform/signed_integer_polyfill.cc
@@ -94,7 +94,7 @@
         auto* unsigned_type = ty.MatchWidth(ty.u32(), signed_type);
         b.InsertBefore(unary, [&] {
             auto* unsigned_value = b.Bitcast(unsigned_type, unary->Val());
-            auto* complement = b.Complement(unsigned_type, unsigned_value);
+            auto* complement = b.Complement(unsigned_value);
             auto* plus_one = b.Add(unsigned_type, complement, b.MatchWidth(u32(1), unsigned_type));
             auto* result = b.Bitcast(signed_type, plus_one);
             unary->Result()->ReplaceAllUsesWith(result->Result());
diff --git a/src/tint/lang/core/ir/validator_test.cc b/src/tint/lang/core/ir/validator_test.cc
index 9f03755..9b71913 100644
--- a/src/tint/lang/core/ir/validator_test.cc
+++ b/src/tint/lang/core/ir/validator_test.cc
@@ -1416,7 +1416,8 @@
 }
 
 TEST_F(IR_ValidatorTest, Unary_ResultTypeNotMatchValueType) {
-    auto* bin = b.Complement(ty.f32(), 2_i);
+    auto* bin = b.Append(b.ir.CreateInstruction<ir::CoreUnary>(b.InstructionResult(ty.f32()),
+                                                               UnaryOp::kComplement, b.Value(2_i)));
 
     auto* f = b.Function("my_func", ty.void_());
 
diff --git a/src/tint/lang/glsl/writer/unary_test.cc b/src/tint/lang/glsl/writer/unary_test.cc
index b0461e1..37ff874 100644
--- a/src/tint/lang/glsl/writer/unary_test.cc
+++ b/src/tint/lang/glsl/writer/unary_test.cc
@@ -39,7 +39,7 @@
     auto* func = b.ComputeFunction("main");
     b.Append(func->Block(), [&] {
         auto* l = b.Let("left", b.Constant(1_u));
-        auto* op = b.Complement(ty.u32(), l);
+        auto* op = b.Complement(l);
         b.Let("val", op);
         b.Return(func);
     });
diff --git a/src/tint/lang/hlsl/writer/unary_test.cc b/src/tint/lang/hlsl/writer/unary_test.cc
index 16ed1bb..7cd3043 100644
--- a/src/tint/lang/hlsl/writer/unary_test.cc
+++ b/src/tint/lang/hlsl/writer/unary_test.cc
@@ -39,7 +39,7 @@
     auto* func = b.ComputeFunction("main");
     b.Append(func->Block(), [&] {
         auto* a = b.Var("a", b.Zero<i32>());
-        b.Var("b", b.Complement<i32>(b.Load(a)));
+        b.Var("b", b.Complement(b.Load(a)));
         b.Return(func);
     });
 
diff --git a/src/tint/lang/spirv/reader/lower/builtins.cc b/src/tint/lang/spirv/reader/lower/builtins.cc
index 44a3109..c63d889 100644
--- a/src/tint/lang/spirv/reader/lower/builtins.cc
+++ b/src/tint/lang/spirv/reader/lower/builtins.cc
@@ -481,7 +481,7 @@
         auto* val = call->Args()[0];
         auto* result_ty = call->Result()->Type();
         b.InsertBefore(call, [&] {
-            auto* complement = b.Complement(val->Type(), val)->Result();
+            auto* complement = b.Complement(val)->Result();
             if (val->Type() != result_ty) {
                 complement = b.Bitcast(result_ty, complement)->Result();
             }
diff --git a/src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.cc b/src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.cc
index 1214dcc..d0f89a3 100644
--- a/src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.cc
+++ b/src/tint/lang/wgsl/reader/program_to_ir/program_to_ir.cc
@@ -946,8 +946,7 @@
                         Bind(expr, val);
                         return;
                     case core::UnaryOp::kComplement: {
-                        auto* ty = sem->Type()->Clone(impl.clone_ctx_.type_ctx);
-                        inst = impl.builder_.Complement(ty, val);
+                        inst = impl.builder_.Complement(val);
                         break;
                     }
                     case core::UnaryOp::kNegation: {
diff --git a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
index 249a7a7..461847c 100644
--- a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
+++ b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program_test.cc
@@ -1037,7 +1037,7 @@
     auto* i = b.FunctionParam("i", ty.u32());
     fn->SetParams({i});
 
-    b.Append(fn->Block(), [&] { b.Return(fn, b.Complement(ty.u32(), i)); });
+    b.Append(fn->Block(), [&] { b.Return(fn, b.Complement(i)); });
 
     EXPECT_WGSL(R"(
 fn f(i : u32) -> u32 {
@@ -2025,7 +2025,7 @@
     fn->SetParams({i});
 
     b.Append(fn->Block(), [&] {
-        auto* v = b.Let("v", b.Complement(ty.u32(), i));
+        auto* v = b.Let("v", b.Complement(i));
         b.Return(fn, v);
     });