[spirv-reader] Do not change access mode used for type map for handles

The access mode that we use to key into the type map needs to use the
actual access mode that we are going to use for the image type,
otherwise we will have a collision when the same SPIR-V type is used
in two different variables with two different access mode.

We already handle setting the access mode for image types when we
actually emit the type, so we don't need to change anything for the
type map key.

Fixed: 437093642
Change-Id: I90b6e383c6ad0bc563fcea43ee698f7a81723254
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/256517
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/tint/lang/spirv/reader/parser/parser.cc b/src/tint/lang/spirv/reader/parser/parser.cc
index 0961dfd..ded8bd6 100644
--- a/src/tint/lang/spirv/reader/parser/parser.cc
+++ b/src/tint/lang/spirv/reader/parser/parser.cc
@@ -634,19 +634,11 @@
     /// @returns a Tint type object
     const core::type::Type* Type(const spvtools::opt::analysis::Type* type,
                                  core::Access access_mode = core::Access::kUndefined) {
+        // Only use the access mode for the map key if it is used as part of the type in Tint IR.
         auto key_mode = core::Access::kUndefined;
-        if (type->kind() == spvtools::opt::analysis::Type::kImage) {
+        if (type->kind() == spvtools::opt::analysis::Type::kImage ||
+            type->kind() == spvtools::opt::analysis::Type::kPointer) {
             key_mode = access_mode;
-        } else if (type->kind() == spvtools::opt::analysis::Type::kPointer) {
-            // Pointers use the access mode, unless they're handle pointers in which case they get
-            // Read.
-            key_mode = access_mode;
-
-            auto* ptr = type->AsPointer();
-            if (ptr->pointee_type()->kind() == spvtools::opt::analysis::Type::kSampler ||
-                ptr->pointee_type()->kind() == spvtools::opt::analysis::Type::kImage) {
-                key_mode = core::Access::kRead;
-            }
         }
 
         return types_.GetOrAdd(TypeKey{type, key_mode}, [&]() -> const core::type::Type* {
diff --git a/src/tint/lang/spirv/reader/texture_test.cc b/src/tint/lang/spirv/reader/texture_test.cc
index 4a07d73..6bf5b8e 100644
--- a/src/tint/lang/spirv/reader/texture_test.cc
+++ b/src/tint/lang/spirv/reader/texture_test.cc
@@ -168,6 +168,48 @@
 )");
 }
 
+TEST_F(SpirvReaderTest, Handle_SameImageType_TwoVarsWithDifferentAccessModes) {
+    EXPECT_IR(R"(
+           OpCapability Shader
+           OpCapability Sampled1D
+           OpCapability StorageImageExtendedFormats
+           OpMemoryModel Logical Simple
+           OpEntryPoint Fragment %main "main"
+           OpExecutionMode %main OriginUpperLeft
+           OpName %write_only_image "write_only_image"
+           OpName %read_only_image "read_only_image"
+           OpDecorate %write_only_image DescriptorSet 0
+           OpDecorate %write_only_image Binding 0
+           OpDecorate %write_only_image NonReadable
+           OpDecorate %read_only_image DescriptorSet 0
+           OpDecorate %read_only_image Binding 1
+           OpDecorate %read_only_image NonWritable
+  %float = OpTypeFloat 32
+%storage = OpTypeImage %float 1D 0 0 0 2 Rg32f
+%ptr_storage = OpTypePointer UniformConstant %storage
+   %void = OpTypeVoid
+ %voidfn = OpTypeFunction %void
+%write_only_image = OpVariable %ptr_storage UniformConstant
+ %read_only_image = OpVariable %ptr_storage UniformConstant
+   %main = OpFunction %void None %voidfn
+  %entry = OpLabel
+           OpReturn
+           OpFunctionEnd
+        )",
+              R"(
+$B1: {  # root
+  %write_only_image:ptr<handle, texture_storage_1d<rg32float, write>, read> = var undef @binding_point(0, 0)
+  %read_only_image:ptr<handle, texture_storage_1d<rg32float, read>, read> = var undef @binding_point(0, 1)
+}
+
+%main = @fragment func():void {
+  $B2: {
+    ret
+  }
+}
+)");
+}
+
 TEST_F(SpirvReaderTest, Handle_MS_2D) {
     EXPECT_IR(R"(
            OpCapability Shader