IntrinsicTable: remove double underscores

'__' is reserved in C++, and the 'match__' and 'build__' functions are causing OSS-fuzz builds to fail.

Add the change in tint behavior to the OT notes.

Add end to end tests for underscores. While the GLSL and MSL compilers seem to accept leading and double underscores in identifiers, the tint build failure has highlighted we have more work to do here (crbug.com/tint/1319)

Fixed: oss-fuzz:41214
Bug: tint:1292
Bug: tint:1319
Change-Id: I32b7bf4e0cff26e678b788457f90452c2503da50
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/70480
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
diff --git a/docs/origin-trial-changes.md b/docs/origin-trial-changes.md
index 38f9279..f8612ad 100644
--- a/docs/origin-trial-changes.md
+++ b/docs/origin-trial-changes.md
@@ -9,6 +9,7 @@
 ### New Features
 
 * The `dot()` builtin now supports integer vector types.
+* Identifiers can now start with a single leading underscore.  [tint:1292](https://crbug.com/tint/1292)
 
 ## Changes for M97
 
@@ -45,4 +46,4 @@
 
 * Hex floats: now correctly errors when the magnitude is non-zero, and the exponent would cause overflow. [tint:1150](https://crbug.com/tint/1150), [tint:1166](https://crbug.com/tint/1166)
 * Identifiers beginning with an underscore are now correctly rejected.  [tint:1179](https://crbug.com/tint/1179)
-* `abs()` fixed for unsigned integers on SPIR-V backend   [tint:1179](https://crbug.com/tint/1194)
+* `abs()` fixed for unsigned integers on SPIR-V backend
diff --git a/src/intrinsic_table.cc b/src/intrinsic_table.cc
index dcc2ae5..cb7c02f 100644
--- a/src/intrinsic_table.cc
+++ b/src/intrinsic_table.cc
@@ -657,20 +657,20 @@
 // Builtin types starting with a _ prefix cannot be declared in WGSL, so they
 // can only be used as return types. Because of this, they must only match Any,
 // which is used as the return type matcher.
-bool match__modf_result(const sem::Type* ty) {
+bool match_modf_result(const sem::Type* ty) {
   return ty->Is<Any>();
 }
-bool match__modf_result_vec(const sem::Type* ty, Number& N) {
+bool match_modf_result_vec(const sem::Type* ty, Number& N) {
   if (!ty->Is<Any>()) {
     return false;
   }
   N = Number::any;
   return true;
 }
-bool match__frexp_result(const sem::Type* ty) {
+bool match_frexp_result(const sem::Type* ty) {
   return ty->Is<Any>();
 }
-bool match__frexp_result_vec(const sem::Type* ty, Number& N) {
+bool match_frexp_result_vec(const sem::Type* ty, Number& N) {
   if (!ty->Is<Any>()) {
     return false;
   }
@@ -715,22 +715,22 @@
       /* size_no_padding */ size_without_padding);
 }
 
-const sem::Struct* build__modf_result(MatchState& state) {
+const sem::Struct* build_modf_result(MatchState& state) {
   auto* f32 = state.builder.create<sem::F32>();
   return build_struct(state, "__modf_result", {{"fract", f32}, {"whole", f32}});
 }
-const sem::Struct* build__modf_result_vec(MatchState& state, Number& n) {
+const sem::Struct* build_modf_result_vec(MatchState& state, Number& n) {
   auto* vec_f32 = state.builder.create<sem::Vector>(
       state.builder.create<sem::F32>(), n.Value());
   return build_struct(state, "__modf_result_vec" + std::to_string(n.Value()),
                       {{"fract", vec_f32}, {"whole", vec_f32}});
 }
-const sem::Struct* build__frexp_result(MatchState& state) {
+const sem::Struct* build_frexp_result(MatchState& state) {
   auto* f32 = state.builder.create<sem::F32>();
   auto* i32 = state.builder.create<sem::I32>();
   return build_struct(state, "__frexp_result", {{"sig", f32}, {"exp", i32}});
 }
-const sem::Struct* build__frexp_result_vec(MatchState& state, Number& n) {
+const sem::Struct* build_frexp_result_vec(MatchState& state, Number& n) {
   auto* vec_f32 = state.builder.create<sem::Vector>(
       state.builder.create<sem::F32>(), n.Value());
   auto* vec_i32 = state.builder.create<sem::Vector>(
diff --git a/src/intrinsic_table.inl b/src/intrinsic_table.inl
index 69e7a12..73a0966 100644
--- a/src/intrinsic_table.inl
+++ b/src/intrinsic_table.inl
@@ -1050,10 +1050,10 @@
 };
 
 const sem::Type* ModfResult::Match(MatchState& state, const sem::Type* ty) const {
-  if (!match__modf_result(ty)) {
+  if (!match_modf_result(ty)) {
     return nullptr;
   }
-  return build__modf_result(state);
+  return build_modf_result(state);
 }
 
 std::string ModfResult::String(MatchState&) const {
@@ -1078,14 +1078,14 @@
 
 const sem::Type* ModfResultVec::Match(MatchState& state, const sem::Type* ty) const {
   Number N = Number::invalid;
-  if (!match__modf_result_vec(ty, N)) {
+  if (!match_modf_result_vec(ty, N)) {
     return nullptr;
   }
   N = state.Num(N);
   if (!N.IsValid()) {
     return nullptr;
   }
-  return build__modf_result_vec(state, N);
+  return build_modf_result_vec(state, N);
 }
 
 std::string ModfResultVec::String(MatchState& state) const {
@@ -1112,10 +1112,10 @@
 };
 
 const sem::Type* FrexpResult::Match(MatchState& state, const sem::Type* ty) const {
-  if (!match__frexp_result(ty)) {
+  if (!match_frexp_result(ty)) {
     return nullptr;
   }
-  return build__frexp_result(state);
+  return build_frexp_result(state);
 }
 
 std::string FrexpResult::String(MatchState&) const {
@@ -1140,14 +1140,14 @@
 
 const sem::Type* FrexpResultVec::Match(MatchState& state, const sem::Type* ty) const {
   Number N = Number::invalid;
-  if (!match__frexp_result_vec(ty, N)) {
+  if (!match_frexp_result_vec(ty, N)) {
     return nullptr;
   }
   N = state.Num(N);
   if (!N.IsValid()) {
     return nullptr;
   }
-  return build__frexp_result_vec(state, N);
+  return build_frexp_result_vec(state, N);
 }
 
 std::string FrexpResultVec::String(MatchState& state) const {
diff --git a/src/intrinsic_table.inl.tmpl b/src/intrinsic_table.inl.tmpl
index c64bf7d..a112159 100644
--- a/src/intrinsic_table.inl.tmpl
+++ b/src/intrinsic_table.inl.tmpl
@@ -148,7 +148,7 @@
 {{- range .TemplateParams }}
 {{-   template "DeclareLocalTemplateParam" . }}
 {{- end  }}
-  if (!match_{{TrimPrefix .Name "_"}}(ty{{range .TemplateParams}}, {{.GetName}}{{end}})) {
+  if (!match_{{TrimLeft .Name "_"}}(ty{{range .TemplateParams}}, {{.GetName}}{{end}})) {
     return nullptr;
   }
 {{- range .TemplateParams }}
@@ -157,7 +157,7 @@
     return nullptr;
   }
 {{- end  }}
-  return build_{{TrimPrefix .Name "_"}}(state{{range .TemplateParams}}, {{.GetName}}{{end}});
+  return build_{{TrimLeft .Name "_"}}(state{{range .TemplateParams}}, {{.GetName}}{{end}});
 }
 
 std::string {{$class}}::String(MatchState&{{if .TemplateParams}} state{{end}}) const {
diff --git a/test/identifiers/underscore/double/alias.wgsl b/test/identifiers/underscore/double/alias.wgsl
new file mode 100644
index 0000000..51d4dad
--- /dev/null
+++ b/test/identifiers/underscore/double/alias.wgsl
@@ -0,0 +1,9 @@
+type a = i32;
+type a__ = i32;
+type b = a;
+type b__ = a__;
+
+fn f() {
+    var c : b;
+    var d : b__;
+}
diff --git a/test/identifiers/underscore/double/alias.wgsl.expected.hlsl b/test/identifiers/underscore/double/alias.wgsl.expected.hlsl
new file mode 100644
index 0000000..0e84791
--- /dev/null
+++ b/test/identifiers/underscore/double/alias.wgsl.expected.hlsl
@@ -0,0 +1,9 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
diff --git a/test/identifiers/underscore/double/alias.wgsl.expected.msl b/test/identifiers/underscore/double/alias.wgsl.expected.msl
new file mode 100644
index 0000000..7e91cf7
--- /dev/null
+++ b/test/identifiers/underscore/double/alias.wgsl.expected.msl
@@ -0,0 +1,9 @@
+#include <metal_stdlib>
+
+using namespace metal;
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
+
diff --git a/test/identifiers/underscore/double/alias.wgsl.expected.spvasm b/test/identifiers/underscore/double/alias.wgsl.expected.spvasm
new file mode 100644
index 0000000..b510533
--- /dev/null
+++ b/test/identifiers/underscore/double/alias.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 12
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %c "c"
+               OpName %d "d"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+         %10 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+          %c = OpVariable %_ptr_Function_int Function %10
+          %d = OpVariable %_ptr_Function_int Function %10
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/alias.wgsl.expected.wgsl b/test/identifiers/underscore/double/alias.wgsl.expected.wgsl
new file mode 100644
index 0000000..d60b6b4
--- /dev/null
+++ b/test/identifiers/underscore/double/alias.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+type a = i32;
+
+type a__ = i32;
+
+type b = a;
+
+type b__ = a__;
+
+fn f() {
+  var c : b;
+  var d : b__;
+}
diff --git a/test/identifiers/underscore/double/fn.wgsl b/test/identifiers/underscore/double/fn.wgsl
new file mode 100644
index 0000000..3cd2de5
--- /dev/null
+++ b/test/identifiers/underscore/double/fn.wgsl
@@ -0,0 +1,5 @@
+fn a() {}
+fn a__() {}
+
+fn b() { a(); }
+fn b__() { a__(); }
diff --git a/test/identifiers/underscore/double/fn.wgsl.expected.hlsl b/test/identifiers/underscore/double/fn.wgsl.expected.hlsl
new file mode 100644
index 0000000..aa8d1a9
--- /dev/null
+++ b/test/identifiers/underscore/double/fn.wgsl.expected.hlsl
@@ -0,0 +1,18 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void a() {
+}
+
+void a__() {
+}
+
+void b() {
+  a();
+}
+
+void b__() {
+  a__();
+}
diff --git a/test/identifiers/underscore/double/fn.wgsl.expected.msl b/test/identifiers/underscore/double/fn.wgsl.expected.msl
new file mode 100644
index 0000000..aa446cf
--- /dev/null
+++ b/test/identifiers/underscore/double/fn.wgsl.expected.msl
@@ -0,0 +1,17 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void a() {
+}
+
+void a__() {
+}
+
+void b() {
+  a();
+}
+
+void b__() {
+  a__();
+}
+
diff --git a/test/identifiers/underscore/double/fn.wgsl.expected.spvasm b/test/identifiers/underscore/double/fn.wgsl.expected.spvasm
new file mode 100644
index 0000000..1fb5c95
--- /dev/null
+++ b/test/identifiers/underscore/double/fn.wgsl.expected.spvasm
@@ -0,0 +1,38 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 15
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %a "a"
+               OpName %a__ "a__"
+               OpName %b "b"
+               OpName %b__ "b__"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %a = OpFunction %void None %1
+          %6 = OpLabel
+               OpReturn
+               OpFunctionEnd
+        %a__ = OpFunction %void None %1
+          %8 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %b = OpFunction %void None %1
+         %10 = OpLabel
+         %11 = OpFunctionCall %void %a
+               OpReturn
+               OpFunctionEnd
+        %b__ = OpFunction %void None %1
+         %13 = OpLabel
+         %14 = OpFunctionCall %void %a__
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/fn.wgsl.expected.wgsl b/test/identifiers/underscore/double/fn.wgsl.expected.wgsl
new file mode 100644
index 0000000..382fdd5
--- /dev/null
+++ b/test/identifiers/underscore/double/fn.wgsl.expected.wgsl
@@ -0,0 +1,13 @@
+fn a() {
+}
+
+fn a__() {
+}
+
+fn b() {
+  a();
+}
+
+fn b__() {
+  a__();
+}
diff --git a/test/identifiers/underscore/double/let.wgsl b/test/identifiers/underscore/double/let.wgsl
new file mode 100644
index 0000000..a6f5ba9
--- /dev/null
+++ b/test/identifiers/underscore/double/let.wgsl
@@ -0,0 +1,7 @@
+let a : i32 = 1;
+let a__ : i32 = 2;
+
+fn f() {
+    let b = a;
+    let b__ = a__;
+}
diff --git a/test/identifiers/underscore/double/let.wgsl.expected.hlsl b/test/identifiers/underscore/double/let.wgsl.expected.hlsl
new file mode 100644
index 0000000..8f66e9d
--- /dev/null
+++ b/test/identifiers/underscore/double/let.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static const int a = 1;
+static const int a__ = 2;
+
+void f() {
+  const int b = a;
+  const int b__ = a__;
+}
diff --git a/test/identifiers/underscore/double/let.wgsl.expected.msl b/test/identifiers/underscore/double/let.wgsl.expected.msl
new file mode 100644
index 0000000..af5a236
--- /dev/null
+++ b/test/identifiers/underscore/double/let.wgsl.expected.msl
@@ -0,0 +1,10 @@
+#include <metal_stdlib>
+
+using namespace metal;
+constant int a = 1;
+constant int a__ = 2;
+void f() {
+  int const b = a;
+  int const b__ = a__;
+}
+
diff --git a/test/identifiers/underscore/double/let.wgsl.expected.spvasm b/test/identifiers/underscore/double/let.wgsl.expected.spvasm
new file mode 100644
index 0000000..f51d6d6
--- /dev/null
+++ b/test/identifiers/underscore/double/let.wgsl.expected.spvasm
@@ -0,0 +1,26 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %a "a"
+               OpName %a__ "a__"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+        %int = OpTypeInt 32 1
+          %a = OpConstant %int 1
+        %a__ = OpConstant %int 2
+       %void = OpTypeVoid
+          %4 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %4
+          %7 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %4
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/let.wgsl.expected.wgsl b/test/identifiers/underscore/double/let.wgsl.expected.wgsl
new file mode 100644
index 0000000..a8db78c
--- /dev/null
+++ b/test/identifiers/underscore/double/let.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+let a : i32 = 1;
+
+let a__ : i32 = 2;
+
+fn f() {
+  let b = a;
+  let b__ = a__;
+}
diff --git a/test/identifiers/underscore/double/parameter.wgsl b/test/identifiers/underscore/double/parameter.wgsl
new file mode 100644
index 0000000..ec3a8c3
--- /dev/null
+++ b/test/identifiers/underscore/double/parameter.wgsl
@@ -0,0 +1,3 @@
+fn f(a__ : i32) {
+  let b = a__;
+}
diff --git a/test/identifiers/underscore/double/parameter.wgsl.expected.hlsl b/test/identifiers/underscore/double/parameter.wgsl.expected.hlsl
new file mode 100644
index 0000000..6926de2
--- /dev/null
+++ b/test/identifiers/underscore/double/parameter.wgsl.expected.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f(int a__) {
+  const int b = a__;
+}
diff --git a/test/identifiers/underscore/double/parameter.wgsl.expected.msl b/test/identifiers/underscore/double/parameter.wgsl.expected.msl
new file mode 100644
index 0000000..737b2cc
--- /dev/null
+++ b/test/identifiers/underscore/double/parameter.wgsl.expected.msl
@@ -0,0 +1,7 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(int a__) {
+  int const b = a__;
+}
+
diff --git a/test/identifiers/underscore/double/parameter.wgsl.expected.spvasm b/test/identifiers/underscore/double/parameter.wgsl.expected.spvasm
new file mode 100644
index 0000000..6c817c9
--- /dev/null
+++ b/test/identifiers/underscore/double/parameter.wgsl.expected.spvasm
@@ -0,0 +1,25 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %a__ "a__"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+          %5 = OpTypeFunction %void %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %5
+        %a__ = OpFunctionParameter %int
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/parameter.wgsl.expected.wgsl b/test/identifiers/underscore/double/parameter.wgsl.expected.wgsl
new file mode 100644
index 0000000..ec3a8c3
--- /dev/null
+++ b/test/identifiers/underscore/double/parameter.wgsl.expected.wgsl
@@ -0,0 +1,3 @@
+fn f(a__ : i32) {
+  let b = a__;
+}
diff --git a/test/identifiers/underscore/double/struct.wgsl b/test/identifiers/underscore/double/struct.wgsl
new file mode 100644
index 0000000..5f03712
--- /dev/null
+++ b/test/identifiers/underscore/double/struct.wgsl
@@ -0,0 +1,10 @@
+struct a {
+  b : i32;
+};
+struct a__ {
+  b__ : i32;
+};
+fn f() {
+  let c = a__();
+  let d = c.b__;
+}
diff --git a/test/identifiers/underscore/double/struct.wgsl.expected.hlsl b/test/identifiers/underscore/double/struct.wgsl.expected.hlsl
new file mode 100644
index 0000000..8c04697
--- /dev/null
+++ b/test/identifiers/underscore/double/struct.wgsl.expected.hlsl
@@ -0,0 +1,13 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+struct a__ {
+  int b__;
+};
+
+void f() {
+  const a__ c = (a__)0;
+  const int d = c.b__;
+}
diff --git a/test/identifiers/underscore/double/struct.wgsl.expected.msl b/test/identifiers/underscore/double/struct.wgsl.expected.msl
new file mode 100644
index 0000000..2389d0e
--- /dev/null
+++ b/test/identifiers/underscore/double/struct.wgsl.expected.msl
@@ -0,0 +1,15 @@
+#include <metal_stdlib>
+
+using namespace metal;
+struct a {
+  int b;
+};
+struct a__ {
+  int b__;
+};
+
+void f() {
+  a__ const c = {};
+  int const d = c.b__;
+}
+
diff --git a/test/identifiers/underscore/double/struct.wgsl.expected.spvasm b/test/identifiers/underscore/double/struct.wgsl.expected.spvasm
new file mode 100644
index 0000000..9faa08b
--- /dev/null
+++ b/test/identifiers/underscore/double/struct.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 11
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %a__ "a__"
+               OpMemberName %a__ 0 "b__"
+               OpMemberDecorate %a__ 0 Offset 0
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+        %a__ = OpTypeStruct %int
+          %9 = OpConstantNull %a__
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+         %10 = OpCompositeExtract %int %9 0
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/struct.wgsl.expected.wgsl b/test/identifiers/underscore/double/struct.wgsl.expected.wgsl
new file mode 100644
index 0000000..2a008b6
--- /dev/null
+++ b/test/identifiers/underscore/double/struct.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+struct a {
+  b : i32;
+};
+
+struct a__ {
+  b__ : i32;
+};
+
+fn f() {
+  let c = a__();
+  let d = c.b__;
+}
diff --git a/test/identifiers/underscore/double/var.wgsl b/test/identifiers/underscore/double/var.wgsl
new file mode 100644
index 0000000..b727c79
--- /dev/null
+++ b/test/identifiers/underscore/double/var.wgsl
@@ -0,0 +1,7 @@
+var<private> a : i32 = 1;
+var<private> a__ : i32 = 2;
+
+fn f() {
+  var b : i32 = a;
+  var b__ : i32 = a__;
+}
diff --git a/test/identifiers/underscore/double/var.wgsl.expected.hlsl b/test/identifiers/underscore/double/var.wgsl.expected.hlsl
new file mode 100644
index 0000000..3b20c1d
--- /dev/null
+++ b/test/identifiers/underscore/double/var.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static int a = 1;
+static int a__ = 2;
+
+void f() {
+  int b = a;
+  int b__ = a__;
+}
diff --git a/test/identifiers/underscore/double/var.wgsl.expected.msl b/test/identifiers/underscore/double/var.wgsl.expected.msl
new file mode 100644
index 0000000..3feee1f
--- /dev/null
+++ b/test/identifiers/underscore/double/var.wgsl.expected.msl
@@ -0,0 +1,8 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(thread int* const tint_symbol, thread int* const tint_symbol_1) {
+  int b = *(tint_symbol);
+  int b__ = *(tint_symbol_1);
+}
+
diff --git a/test/identifiers/underscore/double/var.wgsl.expected.spvasm b/test/identifiers/underscore/double/var.wgsl.expected.spvasm
new file mode 100644
index 0000000..c706274
--- /dev/null
+++ b/test/identifiers/underscore/double/var.wgsl.expected.spvasm
@@ -0,0 +1,39 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 19
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %a "a"
+               OpName %a__ "a__"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %b "b"
+               OpName %b__ "b__"
+        %int = OpTypeInt 32 1
+      %int_1 = OpConstant %int 1
+%_ptr_Private_int = OpTypePointer Private %int
+          %a = OpVariable %_ptr_Private_int Private %int_1
+      %int_2 = OpConstant %int 2
+        %a__ = OpVariable %_ptr_Private_int Private %int_2
+       %void = OpTypeVoid
+          %7 = OpTypeFunction %void
+%_ptr_Function_int = OpTypePointer Function %int
+         %16 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %7
+         %10 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %7
+         %12 = OpLabel
+          %b = OpVariable %_ptr_Function_int Function %16
+        %b__ = OpVariable %_ptr_Function_int Function %16
+         %13 = OpLoad %int %a
+               OpStore %b %13
+         %17 = OpLoad %int %a__
+               OpStore %b__ %17
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/double/var.wgsl.expected.wgsl b/test/identifiers/underscore/double/var.wgsl.expected.wgsl
new file mode 100644
index 0000000..e225819
--- /dev/null
+++ b/test/identifiers/underscore/double/var.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+var<private> a : i32 = 1;
+
+var<private> a__ : i32 = 2;
+
+fn f() {
+  var b : i32 = a;
+  var b__ : i32 = a__;
+}
diff --git a/test/identifiers/underscore/prefix/lower/alias.wgsl b/test/identifiers/underscore/prefix/lower/alias.wgsl
new file mode 100644
index 0000000..bb06471
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/alias.wgsl
@@ -0,0 +1,9 @@
+type a = i32;
+type _a = i32;
+type b = a;
+type _b = _a;
+
+fn f() {
+    var c : b;
+    var d : _b;
+}
diff --git a/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.hlsl
new file mode 100644
index 0000000..0e84791
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.hlsl
@@ -0,0 +1,9 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
diff --git a/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.msl
new file mode 100644
index 0000000..7e91cf7
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.msl
@@ -0,0 +1,9 @@
+#include <metal_stdlib>
+
+using namespace metal;
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.spvasm
new file mode 100644
index 0000000..b510533
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 12
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %c "c"
+               OpName %d "d"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+         %10 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+          %c = OpVariable %_ptr_Function_int Function %10
+          %d = OpVariable %_ptr_Function_int Function %10
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.wgsl
new file mode 100644
index 0000000..da69752
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/alias.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+type a = i32;
+
+type _a = i32;
+
+type b = a;
+
+type _b = _a;
+
+fn f() {
+  var c : b;
+  var d : _b;
+}
diff --git a/test/identifiers/underscore/prefix/lower/fn.wgsl b/test/identifiers/underscore/prefix/lower/fn.wgsl
new file mode 100644
index 0000000..f5d5221
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/fn.wgsl
@@ -0,0 +1,5 @@
+fn a() {}
+fn _a() {}
+
+fn b() { a(); }
+fn _b() { _a(); }
diff --git a/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.hlsl
new file mode 100644
index 0000000..328adcf
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.hlsl
@@ -0,0 +1,18 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void a() {
+}
+
+void _a() {
+}
+
+void b() {
+  a();
+}
+
+void _b() {
+  _a();
+}
diff --git a/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.msl
new file mode 100644
index 0000000..b889289
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.msl
@@ -0,0 +1,17 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void a() {
+}
+
+void _a() {
+}
+
+void b() {
+  a();
+}
+
+void _b() {
+  _a();
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.spvasm
new file mode 100644
index 0000000..fc7a96b
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.spvasm
@@ -0,0 +1,38 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 15
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %a "a"
+               OpName %_a "_a"
+               OpName %b "b"
+               OpName %_b "_b"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %a = OpFunction %void None %1
+          %6 = OpLabel
+               OpReturn
+               OpFunctionEnd
+         %_a = OpFunction %void None %1
+          %8 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %b = OpFunction %void None %1
+         %10 = OpLabel
+         %11 = OpFunctionCall %void %a
+               OpReturn
+               OpFunctionEnd
+         %_b = OpFunction %void None %1
+         %13 = OpLabel
+         %14 = OpFunctionCall %void %_a
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.wgsl
new file mode 100644
index 0000000..255471e
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/fn.wgsl.expected.wgsl
@@ -0,0 +1,13 @@
+fn a() {
+}
+
+fn _a() {
+}
+
+fn b() {
+  a();
+}
+
+fn _b() {
+  _a();
+}
diff --git a/test/identifiers/underscore/prefix/lower/let.wgsl b/test/identifiers/underscore/prefix/lower/let.wgsl
new file mode 100644
index 0000000..6ded620
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/let.wgsl
@@ -0,0 +1,7 @@
+let a : i32 = 1;
+let _a : i32 = 2;
+
+fn f() {
+    let b = a;
+    let _b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/let.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.hlsl
new file mode 100644
index 0000000..4e65f71
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static const int a = 1;
+static const int _a = 2;
+
+void f() {
+  const int b = a;
+  const int _b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/let.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.msl
new file mode 100644
index 0000000..555cfd1
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.msl
@@ -0,0 +1,10 @@
+#include <metal_stdlib>
+
+using namespace metal;
+constant int a = 1;
+constant int _a = 2;
+void f() {
+  int const b = a;
+  int const _b = _a;
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/let.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.spvasm
new file mode 100644
index 0000000..df61dfe
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.spvasm
@@ -0,0 +1,26 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %a "a"
+               OpName %_a "_a"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+        %int = OpTypeInt 32 1
+          %a = OpConstant %int 1
+         %_a = OpConstant %int 2
+       %void = OpTypeVoid
+          %4 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %4
+          %7 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %4
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/let.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.wgsl
new file mode 100644
index 0000000..9f75219
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/let.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+let a : i32 = 1;
+
+let _a : i32 = 2;
+
+fn f() {
+  let b = a;
+  let _b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/parameter.wgsl b/test/identifiers/underscore/prefix/lower/parameter.wgsl
new file mode 100644
index 0000000..2b3af33
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/parameter.wgsl
@@ -0,0 +1,3 @@
+fn f(_a : i32) {
+  let b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.hlsl
new file mode 100644
index 0000000..246087c
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f(int _a) {
+  const int b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.msl
new file mode 100644
index 0000000..a57966c
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.msl
@@ -0,0 +1,7 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(int _a) {
+  int const b = _a;
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.spvasm
new file mode 100644
index 0000000..b291923
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.spvasm
@@ -0,0 +1,25 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %_a "_a"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+          %5 = OpTypeFunction %void %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %5
+         %_a = OpFunctionParameter %int
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.wgsl
new file mode 100644
index 0000000..2b3af33
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/parameter.wgsl.expected.wgsl
@@ -0,0 +1,3 @@
+fn f(_a : i32) {
+  let b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/struct.wgsl b/test/identifiers/underscore/prefix/lower/struct.wgsl
new file mode 100644
index 0000000..5b03cd7
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/struct.wgsl
@@ -0,0 +1,10 @@
+struct a {
+  b : i32;
+};
+struct _a {
+  _b : i32;
+};
+fn f() {
+  let c = _a();
+  let d = c._b;
+}
diff --git a/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.hlsl
new file mode 100644
index 0000000..31e48b6
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.hlsl
@@ -0,0 +1,13 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+struct _a {
+  int _b;
+};
+
+void f() {
+  const _a c = (_a)0;
+  const int d = c._b;
+}
diff --git a/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.msl
new file mode 100644
index 0000000..32bffa1
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.msl
@@ -0,0 +1,15 @@
+#include <metal_stdlib>
+
+using namespace metal;
+struct a {
+  int b;
+};
+struct _a {
+  int _b;
+};
+
+void f() {
+  _a const c = {};
+  int const d = c._b;
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.spvasm
new file mode 100644
index 0000000..f08ac81
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 11
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %_a "_a"
+               OpMemberName %_a 0 "_b"
+               OpMemberDecorate %_a 0 Offset 0
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+         %_a = OpTypeStruct %int
+          %9 = OpConstantNull %_a
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+         %10 = OpCompositeExtract %int %9 0
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.wgsl
new file mode 100644
index 0000000..6565fc1
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/struct.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+struct a {
+  b : i32;
+};
+
+struct _a {
+  _b : i32;
+};
+
+fn f() {
+  let c = _a();
+  let d = c._b;
+}
diff --git a/test/identifiers/underscore/prefix/lower/var.wgsl b/test/identifiers/underscore/prefix/lower/var.wgsl
new file mode 100644
index 0000000..02973dc
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/var.wgsl
@@ -0,0 +1,7 @@
+var<private> a : i32 = 1;
+var<private> _a : i32 = 2;
+
+fn f() {
+  var b : i32 = a;
+  var _b : i32 = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/var.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.hlsl
new file mode 100644
index 0000000..ac75353
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static int a = 1;
+static int _a = 2;
+
+void f() {
+  int b = a;
+  int _b = _a;
+}
diff --git a/test/identifiers/underscore/prefix/lower/var.wgsl.expected.msl b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.msl
new file mode 100644
index 0000000..4b5a7c8
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.msl
@@ -0,0 +1,8 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(thread int* const tint_symbol, thread int* const tint_symbol_1) {
+  int b = *(tint_symbol);
+  int _b = *(tint_symbol_1);
+}
+
diff --git a/test/identifiers/underscore/prefix/lower/var.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.spvasm
new file mode 100644
index 0000000..b8f18f2
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.spvasm
@@ -0,0 +1,39 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 19
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %a "a"
+               OpName %_a "_a"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %b "b"
+               OpName %_b "_b"
+        %int = OpTypeInt 32 1
+      %int_1 = OpConstant %int 1
+%_ptr_Private_int = OpTypePointer Private %int
+          %a = OpVariable %_ptr_Private_int Private %int_1
+      %int_2 = OpConstant %int 2
+         %_a = OpVariable %_ptr_Private_int Private %int_2
+       %void = OpTypeVoid
+          %7 = OpTypeFunction %void
+%_ptr_Function_int = OpTypePointer Function %int
+         %16 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %7
+         %10 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %7
+         %12 = OpLabel
+          %b = OpVariable %_ptr_Function_int Function %16
+         %_b = OpVariable %_ptr_Function_int Function %16
+         %13 = OpLoad %int %a
+               OpStore %b %13
+         %17 = OpLoad %int %_a
+               OpStore %_b %17
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/lower/var.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.wgsl
new file mode 100644
index 0000000..4b2f858
--- /dev/null
+++ b/test/identifiers/underscore/prefix/lower/var.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+var<private> a : i32 = 1;
+
+var<private> _a : i32 = 2;
+
+fn f() {
+  var b : i32 = a;
+  var _b : i32 = _a;
+}
diff --git a/test/identifiers/underscore/prefix/upper/alias.wgsl b/test/identifiers/underscore/prefix/upper/alias.wgsl
new file mode 100644
index 0000000..d6fcd94
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/alias.wgsl
@@ -0,0 +1,9 @@
+type A = i32;
+type _A = i32;
+type B = A;
+type _B = _A;
+
+fn f() {
+    var c : B;
+    var d : _B;
+}
diff --git a/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.hlsl
new file mode 100644
index 0000000..0e84791
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.hlsl
@@ -0,0 +1,9 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
diff --git a/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.msl
new file mode 100644
index 0000000..7e91cf7
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.msl
@@ -0,0 +1,9 @@
+#include <metal_stdlib>
+
+using namespace metal;
+
+void f() {
+  int c = 0;
+  int d = 0;
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.spvasm
new file mode 100644
index 0000000..b510533
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 12
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %c "c"
+               OpName %d "d"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+%_ptr_Function_int = OpTypePointer Function %int
+         %10 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+          %c = OpVariable %_ptr_Function_int Function %10
+          %d = OpVariable %_ptr_Function_int Function %10
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.wgsl
new file mode 100644
index 0000000..e84f521
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/alias.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+type A = i32;
+
+type _A = i32;
+
+type B = A;
+
+type _B = _A;
+
+fn f() {
+  var c : B;
+  var d : _B;
+}
diff --git a/test/identifiers/underscore/prefix/upper/fn.wgsl b/test/identifiers/underscore/prefix/upper/fn.wgsl
new file mode 100644
index 0000000..9bc9625
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/fn.wgsl
@@ -0,0 +1,5 @@
+fn A() {}
+fn _A() {}
+
+fn B() { A(); }
+fn _B() { _A(); }
diff --git a/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.hlsl
new file mode 100644
index 0000000..6baa073
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.hlsl
@@ -0,0 +1,18 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void A() {
+}
+
+void _A() {
+}
+
+void B() {
+  A();
+}
+
+void _B() {
+  _A();
+}
diff --git a/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.msl
new file mode 100644
index 0000000..d1f2ad1
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.msl
@@ -0,0 +1,17 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void A() {
+}
+
+void _A() {
+}
+
+void B() {
+  A();
+}
+
+void _B() {
+  _A();
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.spvasm
new file mode 100644
index 0000000..fb80947
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.spvasm
@@ -0,0 +1,38 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 15
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %A "A"
+               OpName %_A "_A"
+               OpName %B "B"
+               OpName %_B "_B"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %A = OpFunction %void None %1
+          %6 = OpLabel
+               OpReturn
+               OpFunctionEnd
+         %_A = OpFunction %void None %1
+          %8 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %B = OpFunction %void None %1
+         %10 = OpLabel
+         %11 = OpFunctionCall %void %A
+               OpReturn
+               OpFunctionEnd
+         %_B = OpFunction %void None %1
+         %13 = OpLabel
+         %14 = OpFunctionCall %void %_A
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.wgsl
new file mode 100644
index 0000000..3a3345c
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/fn.wgsl.expected.wgsl
@@ -0,0 +1,13 @@
+fn A() {
+}
+
+fn _A() {
+}
+
+fn B() {
+  A();
+}
+
+fn _B() {
+  _A();
+}
diff --git a/test/identifiers/underscore/prefix/upper/let.wgsl b/test/identifiers/underscore/prefix/upper/let.wgsl
new file mode 100644
index 0000000..06c3aae
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/let.wgsl
@@ -0,0 +1,7 @@
+let A : i32 = 1;
+let _A : i32 = 2;
+
+fn f() {
+    let B = A;
+    let _B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/let.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.hlsl
new file mode 100644
index 0000000..fa81c83
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static const int A = 1;
+static const int _A = 2;
+
+void f() {
+  const int B = A;
+  const int _B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/let.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.msl
new file mode 100644
index 0000000..e1af5ac
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.msl
@@ -0,0 +1,10 @@
+#include <metal_stdlib>
+
+using namespace metal;
+constant int A = 1;
+constant int _A = 2;
+void f() {
+  int const B = A;
+  int const _B = _A;
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/let.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.spvasm
new file mode 100644
index 0000000..8683520
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.spvasm
@@ -0,0 +1,26 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %A "A"
+               OpName %_A "_A"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+        %int = OpTypeInt 32 1
+          %A = OpConstant %int 1
+         %_A = OpConstant %int 2
+       %void = OpTypeVoid
+          %4 = OpTypeFunction %void
+%unused_entry_point = OpFunction %void None %4
+          %7 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %4
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/let.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.wgsl
new file mode 100644
index 0000000..5c9a0fb
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/let.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+let A : i32 = 1;
+
+let _A : i32 = 2;
+
+fn f() {
+  let B = A;
+  let _B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/parameter.wgsl b/test/identifiers/underscore/prefix/upper/parameter.wgsl
new file mode 100644
index 0000000..494f4d2
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/parameter.wgsl
@@ -0,0 +1,3 @@
+fn f(_A : i32) {
+  let B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.hlsl
new file mode 100644
index 0000000..c8ca5f6
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f(int _A) {
+  const int B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.msl
new file mode 100644
index 0000000..75986e9
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.msl
@@ -0,0 +1,7 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(int _A) {
+  int const B = _A;
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.spvasm
new file mode 100644
index 0000000..751d391
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.spvasm
@@ -0,0 +1,25 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 10
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %_A "_A"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+          %5 = OpTypeFunction %void %int
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %5
+         %_A = OpFunctionParameter %int
+          %9 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.wgsl
new file mode 100644
index 0000000..494f4d2
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/parameter.wgsl.expected.wgsl
@@ -0,0 +1,3 @@
+fn f(_A : i32) {
+  let B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/struct.wgsl b/test/identifiers/underscore/prefix/upper/struct.wgsl
new file mode 100644
index 0000000..0afc824
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/struct.wgsl
@@ -0,0 +1,10 @@
+struct A {
+  B : i32;
+};
+struct _A {
+  _B : i32;
+};
+fn f() {
+  let c = _A();
+  let d = c._B;
+}
diff --git a/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.hlsl
new file mode 100644
index 0000000..2fa6f90
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.hlsl
@@ -0,0 +1,13 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+struct _A {
+  int _B;
+};
+
+void f() {
+  const _A c = (_A)0;
+  const int d = c._B;
+}
diff --git a/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.msl
new file mode 100644
index 0000000..bafed70
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.msl
@@ -0,0 +1,15 @@
+#include <metal_stdlib>
+
+using namespace metal;
+struct A {
+  int B;
+};
+struct _A {
+  int _B;
+};
+
+void f() {
+  _A const c = {};
+  int const d = c._B;
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.spvasm
new file mode 100644
index 0000000..75cbcd2
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.spvasm
@@ -0,0 +1,28 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 11
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %_A "_A"
+               OpMemberName %_A 0 "_B"
+               OpMemberDecorate %_A 0 Offset 0
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+         %_A = OpTypeStruct %int
+          %9 = OpConstantNull %_A
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+         %10 = OpCompositeExtract %int %9 0
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.wgsl
new file mode 100644
index 0000000..243f368
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/struct.wgsl.expected.wgsl
@@ -0,0 +1,12 @@
+struct A {
+  B : i32;
+};
+
+struct _A {
+  _B : i32;
+};
+
+fn f() {
+  let c = _A();
+  let d = c._B;
+}
diff --git a/test/identifiers/underscore/prefix/upper/var.wgsl b/test/identifiers/underscore/prefix/upper/var.wgsl
new file mode 100644
index 0000000..22399b6
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/var.wgsl
@@ -0,0 +1,7 @@
+var<private> A : i32 = 1;
+var<private> _A : i32 = 2;
+
+fn f() {
+  var B : i32 = A;
+  var _B : i32 = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/var.wgsl.expected.hlsl b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.hlsl
new file mode 100644
index 0000000..3b733cb
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.hlsl
@@ -0,0 +1,12 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+static int A = 1;
+static int _A = 2;
+
+void f() {
+  int B = A;
+  int _B = _A;
+}
diff --git a/test/identifiers/underscore/prefix/upper/var.wgsl.expected.msl b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.msl
new file mode 100644
index 0000000..f889d4d
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.msl
@@ -0,0 +1,8 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f(thread int* const tint_symbol, thread int* const tint_symbol_1) {
+  int B = *(tint_symbol);
+  int _B = *(tint_symbol_1);
+}
+
diff --git a/test/identifiers/underscore/prefix/upper/var.wgsl.expected.spvasm b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.spvasm
new file mode 100644
index 0000000..84442c5
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.spvasm
@@ -0,0 +1,39 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 19
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %A "A"
+               OpName %_A "_A"
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+               OpName %B "B"
+               OpName %_B "_B"
+        %int = OpTypeInt 32 1
+      %int_1 = OpConstant %int 1
+%_ptr_Private_int = OpTypePointer Private %int
+          %A = OpVariable %_ptr_Private_int Private %int_1
+      %int_2 = OpConstant %int 2
+         %_A = OpVariable %_ptr_Private_int Private %int_2
+       %void = OpTypeVoid
+          %7 = OpTypeFunction %void
+%_ptr_Function_int = OpTypePointer Function %int
+         %16 = OpConstantNull %int
+%unused_entry_point = OpFunction %void None %7
+         %10 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %7
+         %12 = OpLabel
+          %B = OpVariable %_ptr_Function_int Function %16
+         %_B = OpVariable %_ptr_Function_int Function %16
+         %13 = OpLoad %int %A
+               OpStore %B %13
+         %17 = OpLoad %int %_A
+               OpStore %_B %17
+               OpReturn
+               OpFunctionEnd
diff --git a/test/identifiers/underscore/prefix/upper/var.wgsl.expected.wgsl b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.wgsl
new file mode 100644
index 0000000..90e3142
--- /dev/null
+++ b/test/identifiers/underscore/prefix/upper/var.wgsl.expected.wgsl
@@ -0,0 +1,8 @@
+var<private> A : i32 = 1;
+
+var<private> _A : i32 = 2;
+
+fn f() {
+  var B : i32 = A;
+  var _B : i32 = _A;
+}
diff --git a/tools/src/cmd/intrinsic-gen/gen/generate.go b/tools/src/cmd/intrinsic-gen/gen/generate.go
index 6972841..34a8257 100644
--- a/tools/src/cmd/intrinsic-gen/gen/generate.go
+++ b/tools/src/cmd/intrinsic-gen/gen/generate.go
@@ -60,6 +60,8 @@
 		"HasSuffix":             strings.HasSuffix,
 		"TrimPrefix":            strings.TrimPrefix,
 		"TrimSuffix":            strings.TrimSuffix,
+		"TrimLeft":              strings.TrimLeft,
+		"TrimRight":             strings.TrimRight,
 		"IsEnumEntry":           is(sem.EnumEntry{}),
 		"IsEnumMatcher":         is(sem.EnumMatcher{}),
 		"IsFQN":                 is(sem.FullyQualifiedName{}),