[msl] Add ModuleScopeVars transform

Pass a structure containing a pointer for each module-scope variable
down the call-stack to any functions that need it. The structure is
created by the entry point using entry-point parameters and
function-scope declarations.

Handles `private`, `storage`, and `uniform` address spaces. Other
address spaces will be added in future patches.

Bug: 42251016
Change-Id: I0f9ac2d292ab5a8e01049f425c5e124697f90c65
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/188342
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/tint/lang/core/ir/function_param.h b/src/tint/lang/core/ir/function_param.h
index f323914..14d8f92 100644
--- a/src/tint/lang/core/ir/function_param.h
+++ b/src/tint/lang/core/ir/function_param.h
@@ -110,6 +110,12 @@
     /// @param binding the binding
     void SetBindingPoint(uint32_t group, uint32_t binding) { binding_point_ = {group, binding}; }
 
+    /// Sets the binding point
+    /// @param binding_point the binding point
+    void SetBindingPoint(std::optional<struct BindingPoint> binding_point) {
+        binding_point_ = binding_point;
+    }
+
     /// @returns the binding points if `Attributes` contains `kBindingPoint`
     std::optional<struct BindingPoint> BindingPoint() const { return binding_point_; }
 
diff --git a/src/tint/lang/msl/writer/printer/printer.cc b/src/tint/lang/msl/writer/printer/printer.cc
index 5399e2d..02da70d 100644
--- a/src/tint/lang/msl/writer/printer/printer.cc
+++ b/src/tint/lang/msl/writer/printer/printer.cc
@@ -118,8 +118,8 @@
             Line() << "using namespace metal;";
         }
 
-        // Emit module-scope declarations.
-        EmitBlockInstructions(ir_.root_block);
+        // Module-scope declarations should have all been moved into the entry points.
+        TINT_ASSERT(ir_.root_block->IsEmpty());
 
         // Emit functions.
         for (auto* func : ir_.DependencyOrderedFunctions()) {
diff --git a/src/tint/lang/msl/writer/raise/BUILD.bazel b/src/tint/lang/msl/writer/raise/BUILD.bazel
index c906eaa..1c1d899 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.bazel
+++ b/src/tint/lang/msl/writer/raise/BUILD.bazel
@@ -40,10 +40,12 @@
   name = "raise",
   srcs = [
     "builtin_polyfill.cc",
+    "module_scope_vars.cc",
     "raise.cc",
   ],
   hdrs = [
     "builtin_polyfill.h",
+    "module_scope_vars.h",
     "raise.h",
   ],
   deps = [
@@ -54,6 +56,7 @@
     "//src/tint/lang/core/intrinsic",
     "//src/tint/lang/core/ir",
     "//src/tint/lang/core/ir/transform",
+    "//src/tint/lang/core/ir/transform/common",
     "//src/tint/lang/core/type",
     "//src/tint/lang/msl",
     "//src/tint/lang/msl/intrinsic",
@@ -85,6 +88,7 @@
   alwayslink = True,
   srcs = [
     "builtin_polyfill_test.cc",
+    "module_scope_vars_test.cc",
   ],
   deps = [
     "//src/tint/api/common",
diff --git a/src/tint/lang/msl/writer/raise/BUILD.cmake b/src/tint/lang/msl/writer/raise/BUILD.cmake
index 06e15b9..396344d 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.cmake
+++ b/src/tint/lang/msl/writer/raise/BUILD.cmake
@@ -43,6 +43,8 @@
 tint_add_target(tint_lang_msl_writer_raise lib
   lang/msl/writer/raise/builtin_polyfill.cc
   lang/msl/writer/raise/builtin_polyfill.h
+  lang/msl/writer/raise/module_scope_vars.cc
+  lang/msl/writer/raise/module_scope_vars.h
   lang/msl/writer/raise/raise.cc
   lang/msl/writer/raise/raise.h
 )
@@ -55,6 +57,7 @@
   tint_lang_core_intrinsic
   tint_lang_core_ir
   tint_lang_core_ir_transform
+  tint_lang_core_ir_transform_common
   tint_lang_core_type
   tint_lang_msl
   tint_lang_msl_intrinsic
@@ -89,6 +92,7 @@
 ################################################################################
 tint_add_target(tint_lang_msl_writer_raise_test test
   lang/msl/writer/raise/builtin_polyfill_test.cc
+  lang/msl/writer/raise/module_scope_vars_test.cc
 )
 
 tint_target_add_dependencies(tint_lang_msl_writer_raise_test test
diff --git a/src/tint/lang/msl/writer/raise/BUILD.gn b/src/tint/lang/msl/writer/raise/BUILD.gn
index d8e6ad5..afc543c 100644
--- a/src/tint/lang/msl/writer/raise/BUILD.gn
+++ b/src/tint/lang/msl/writer/raise/BUILD.gn
@@ -46,6 +46,8 @@
     sources = [
       "builtin_polyfill.cc",
       "builtin_polyfill.h",
+      "module_scope_vars.cc",
+      "module_scope_vars.h",
       "raise.cc",
       "raise.h",
     ]
@@ -57,6 +59,7 @@
       "${tint_src_dir}/lang/core/intrinsic",
       "${tint_src_dir}/lang/core/ir",
       "${tint_src_dir}/lang/core/ir/transform",
+      "${tint_src_dir}/lang/core/ir/transform/common",
       "${tint_src_dir}/lang/core/type",
       "${tint_src_dir}/lang/msl",
       "${tint_src_dir}/lang/msl/intrinsic",
@@ -84,7 +87,10 @@
 if (tint_build_unittests) {
   if (tint_build_msl_writer) {
     tint_unittests_source_set("unittests") {
-      sources = [ "builtin_polyfill_test.cc" ]
+      sources = [
+        "builtin_polyfill_test.cc",
+        "module_scope_vars_test.cc",
+      ]
       deps = [
         "${tint_src_dir}:gmock_and_gtest",
         "${tint_src_dir}/api/common",
diff --git a/src/tint/lang/msl/writer/raise/module_scope_vars.cc b/src/tint/lang/msl/writer/raise/module_scope_vars.cc
new file mode 100644
index 0000000..0cb07c1
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/module_scope_vars.cc
@@ -0,0 +1,260 @@
+// 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/msl/writer/raise/module_scope_vars.h"
+
+#include <utility>
+
+#include "src/tint/lang/core/ir/builder.h"
+#include "src/tint/lang/core/ir/transform/common/referenced_module_vars.h"
+#include "src/tint/lang/core/ir/validator.h"
+
+namespace tint::msl::writer::raise {
+namespace {
+
+using namespace tint::core::fluent_types;  // NOLINT
+
+/// PIMPL state for the transform.
+struct State {
+    /// The IR module.
+    core::ir::Module& ir;
+
+    /// The IR builder.
+    core::ir::Builder b{ir};
+
+    /// The type manager.
+    core::type::Manager& ty{ir.Types()};
+
+    /// The type of the structure that will contain all of the module-scope variables.
+    const core::type::Struct* struct_type = nullptr;
+
+    /// The list of module-scope variables.
+    Vector<core::ir::Var*, 8> module_vars{};
+
+    /// A map from a function to the value that contains the module-scope variable pointers.
+    Hashmap<core::ir::Function*, core::ir::Value*, 8> function_to_struct_value{};
+
+    /// A map from block to its containing function.
+    Hashmap<core::ir::Block*, core::ir::Function*, 64> block_to_function{};
+
+    /// The mapping from functions to their transitively referenced workgroup variables.
+    core::ir::ReferencedModuleVars referenced_module_vars{ir};
+
+    // The name of the module-scope variables structure.
+    static constexpr const char* kModuleVarsName = "tint_module_vars";
+
+    /// Process the module.
+    void Process() {
+        // Seed the block-to-function map with the function entry blocks.
+        // This is used to determine the owning function for any given instruction.
+        for (auto& func : ir.functions) {
+            block_to_function.Add(func->Block(), func);
+        }
+
+        // Create the structure to hold all module-scope variables.
+        // This includes all variables declared in the module, even those that are unused by one or
+        // more entry points.
+        CreateStruct();
+
+        // Process functions in reverse-dependency order (i.e. root to leaves).
+        // This is so that when we update the callsites for a function to add the new argument, we
+        // will have already added the necessary structure to the callers.
+        auto functions = ir.DependencyOrderedFunctions();
+        for (auto func = functions.rbegin(); func != functions.rend(); func++) {
+            ProcessFunction(*func);
+        }
+
+        // Replace uses of each module-scope variable with pointers extracted from the structure.
+        uint32_t index = 0;
+        for (auto& var : module_vars) {
+            var->Result(0)->ReplaceAllUsesWith([&](core::ir::Usage use) {  //
+                return GetPointerFromStruct(var, use.instruction, index);
+            });
+            var->Destroy();
+            index++;
+        }
+    }
+
+    /// Create the structure type to hold all of the module-scope variables.
+    void CreateStruct() {
+        // Collect a list of struct members for the variable declarations.
+        Vector<core::type::Manager::StructMemberDesc, 8> struct_members;
+        for (auto* global : *ir.root_block) {
+            if (auto* var = global->As<core::ir::Var>()) {
+                auto* type = var->Result(0)->Type();
+                auto name = ir.NameOf(var);
+                if (!name) {
+                    name = ir.symbols.New();
+                }
+                module_vars.Push(var);
+                struct_members.Push(core::type::Manager::StructMemberDesc{name, type});
+            }
+        }
+        if (struct_members.IsEmpty()) {
+            return;
+        }
+
+        // Create the structure.
+        auto name = ir.symbols.New("tint_module_vars_struct");
+        struct_type = ty.Struct(name, std::move(struct_members));
+    }
+
+    /// Process a function.
+    void ProcessFunction(core::ir::Function* func) {
+        auto& refs = referenced_module_vars.TransitiveReferences(func);
+        if (refs.IsEmpty()) {
+            // No module-scope variables are referenced from this function, so no changes needed.
+            return;
+        }
+
+        // Add the structure the holds the module-scope variable pointers to the function and record
+        // it in the map. Entry points will create the structure, other functions will declare it as
+        // a parameter.
+        if (func->Stage() != core::ir::Function::PipelineStage::kUndefined) {
+            function_to_struct_value.Add(func, AddModuleVarsToEntryPoint(func, refs));
+        } else {
+            function_to_struct_value.Add(func, AddModuleVarsToFunction(func));
+        }
+    }
+
+    /// Add a module-scope variables structure to an entry point function.
+    /// @param func the entry point function to modify
+    /// @param referenced_vars the set of variables transitively referenced by the entry point
+    /// @returns the structure that holds the module-scope variables
+    core::ir::Value* AddModuleVarsToEntryPoint(
+        core::ir::Function* func,
+        const core::ir::ReferencedModuleVars::VarSet& referenced_vars) {
+        core::ir::Value* module_var_struct = nullptr;
+        // Add parameters and insert instruction at the top of the entry point to set up the
+        // module-scope variables structure.
+        b.InsertBefore(func->Block()->Front(), [&] {  //
+            Vector<core::ir::Value*, 8> construct_args;
+            for (auto var : module_vars) {
+                if (!referenced_vars.Contains(var)) {
+                    // The variable isn't used by this entry point, so set the member to undef.
+                    construct_args.Push(nullptr);
+                    continue;
+                }
+
+                // Create a new declaration in the entry point to replace the module-scope variable.
+                // Use either a parameter or a local variable, depending on the address space.
+                core::ir::Value* decl = nullptr;
+                auto* ptr = var->Result(0)->Type()->As<core::type::Pointer>();
+                switch (ptr->AddressSpace()) {
+                    case core::AddressSpace::kPrivate: {
+                        // Private variables become function-scope variables.
+                        auto* local_var = b.Var(ptr);
+                        local_var->SetInitializer(var->Initializer());
+                        decl = local_var->Result(0);
+                        break;
+                    }
+                    case core::AddressSpace::kStorage:
+                    case core::AddressSpace::kUniform: {
+                        // Storage and uniform buffers become function parameters.
+                        auto* param = b.FunctionParam(ptr);
+                        param->SetBindingPoint(var->BindingPoint());
+                        func->AppendParam(param);
+                        decl = param;
+                        break;
+                    }
+                    default:
+                        TINT_UNREACHABLE() << "unhandled address space: " << ptr->AddressSpace();
+                }
+
+                // Copy an existing name over to the new declaration if present.
+                if (auto name = ir.NameOf(var)) {
+                    ir.SetName(decl, name);
+                }
+                construct_args.Push(decl);
+            }
+
+            // Construct the structure value and name it with a `let` instruction.
+            // The `let` prevents the printer from inlining the constructor, which aids readability.
+            auto* construct = b.Construct(struct_type, std::move(construct_args));
+            module_var_struct = b.Let(kModuleVarsName, construct)->Result(0);
+        });
+        return module_var_struct;
+    }
+
+    /// Add a module-scope variables structure to a non-entry-point function.
+    /// @param func the function to modify
+    /// @returns the parameter that holds the module-scope variables structure
+    core::ir::Value* AddModuleVarsToFunction(core::ir::Function* func) {
+        // Add a new parameter to receive the module-scope variables structure.
+        auto* param = b.FunctionParam(kModuleVarsName, struct_type);
+        func->AppendParam(param);
+
+        // Update all callsites to pass the module-scope variables structure as an argument.
+        func->ForEachUse([&](core::ir::Usage use) {
+            if (auto* call = use.instruction->As<core::ir::UserCall>()) {
+                call->AppendArg(*function_to_struct_value.Get(ContainingFunction(call)));
+            }
+        });
+
+        return param;
+    }
+
+    /// Get a pointer from the module-scope variable replacement structure, inserting new access
+    /// instructions before @p inst.
+    /// @param var the variable to get the replacement for
+    /// @param inst the instruction that uses the variable
+    /// @param index the index of the variable in the structure member list
+    /// @returns the pointer extracted from the structure
+    core::ir::Value* GetPointerFromStruct(core::ir::Var* var,
+                                          core::ir::Instruction* inst,
+                                          uint32_t index) {
+        auto* func = ContainingFunction(inst);
+        auto* struct_value = function_to_struct_value.GetOr(func, nullptr);
+        auto* access = b.Access(var->Result(0)->Type(), struct_value, u32(index));
+        access->InsertBefore(inst);
+        return access->Result(0);
+    }
+
+    /// Get the function that contains an instruction.
+    /// @param inst the instruction
+    /// @returns the function
+    core::ir::Function* ContainingFunction(core::ir::Instruction* inst) {
+        return block_to_function.GetOrAdd(inst->Block(), [&] {  //
+            return ContainingFunction(inst->Block()->Parent());
+        });
+    }
+};
+
+}  // namespace
+
+Result<SuccessType> ModuleScopeVars(core::ir::Module& ir) {
+    auto result = ValidateAndDumpIfNeeded(ir, "ModuleScopeVars transform");
+    if (result != Success) {
+        return result.Failure();
+    }
+
+    State{ir}.Process();
+
+    return Success;
+}
+
+}  // namespace tint::msl::writer::raise
diff --git a/src/tint/lang/msl/writer/raise/module_scope_vars.h b/src/tint/lang/msl/writer/raise/module_scope_vars.h
new file mode 100644
index 0000000..a3c69ee
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/module_scope_vars.h
@@ -0,0 +1,50 @@
+// 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.
+
+#ifndef SRC_TINT_LANG_MSL_WRITER_RAISE_MODULE_SCOPE_VARS_H_
+#define SRC_TINT_LANG_MSL_WRITER_RAISE_MODULE_SCOPE_VARS_H_
+
+#include <string>
+
+#include "src/tint/utils/result/result.h"
+
+// Forward declarations.
+namespace tint::core::ir {
+class Module;
+}  // namespace tint::core::ir
+
+namespace tint::msl::writer::raise {
+
+/// ModuleScopeVars is a transform that replaces module-scope variables with entry-point
+/// declarations that are wrapped in a structure and passed to functions that need them.
+/// @param module the module to transform
+/// @returns success or failure
+Result<SuccessType> ModuleScopeVars(core::ir::Module& module);
+
+}  // namespace tint::msl::writer::raise
+
+#endif  // SRC_TINT_LANG_MSL_WRITER_RAISE_MODULE_SCOPE_VARS_H_
diff --git a/src/tint/lang/msl/writer/raise/module_scope_vars_test.cc b/src/tint/lang/msl/writer/raise/module_scope_vars_test.cc
new file mode 100644
index 0000000..17ffdc1
--- /dev/null
+++ b/src/tint/lang/msl/writer/raise/module_scope_vars_test.cc
@@ -0,0 +1,1366 @@
+// 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/msl/writer/raise/module_scope_vars.h"
+
+#include <utility>
+
+#include "src/tint/lang/core/ir/transform/helper_test.h"
+
+using namespace tint::core::fluent_types;     // NOLINT
+using namespace tint::core::number_suffixes;  // NOLINT
+
+namespace tint::msl::writer::raise {
+namespace {
+
+using MslWriter_ModuleScopeVarsTest = core::ir::transform::TransformTest;
+
+TEST_F(MslWriter_ModuleScopeVarsTest, NoModuleScopeVars) {
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* var = b.Var<function, i32>("v");
+        b.Load(var);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+%foo = @fragment func():void {
+  $B1: {
+    %v:ptr<function, i32, read_write> = var
+    %3:i32 = load %v
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = src;
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, Private) {
+    auto* var_a = b.Var("a", ty.ptr<private_, i32>());
+    auto* var_b = b.Var("b", ty.ptr<private_, i32>());
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_a, b.Add<i32>(load_a, load_b));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<private, i32, read_write> = var
+  %b:ptr<private, i32, read_write> = var
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %4:i32 = load %a
+    %5:i32 = load %b
+    %6:i32 = add %4, %5
+    store %a, %6
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<private, i32, read_write> @offset(0)
+  b:ptr<private, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func():void {
+  $B1: {
+    %a:ptr<private, i32, read_write> = var
+    %b:ptr<private, i32, read_write> = var
+    %4:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:ptr<private, i32, read_write> = access %tint_module_vars, 0u
+    %7:i32 = load %6
+    %8:ptr<private, i32, read_write> = access %tint_module_vars, 1u
+    %9:i32 = load %8
+    %10:i32 = add %7, %9
+    %11:ptr<private, i32, read_write> = access %tint_module_vars, 0u
+    store %11, %10
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, Private_WithInitializers) {
+    auto* var_a = b.Var<private_>("a", 42_i);
+    auto* var_b = b.Var<private_>("b", -1_i);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_a, b.Add<i32>(load_a, load_b));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<private, i32, read_write> = var, 42i
+  %b:ptr<private, i32, read_write> = var, -1i
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %4:i32 = load %a
+    %5:i32 = load %b
+    %6:i32 = add %4, %5
+    store %a, %6
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<private, i32, read_write> @offset(0)
+  b:ptr<private, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func():void {
+  $B1: {
+    %a:ptr<private, i32, read_write> = var, 42i
+    %b:ptr<private, i32, read_write> = var, -1i
+    %4:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:ptr<private, i32, read_write> = access %tint_module_vars, 0u
+    %7:i32 = load %6
+    %8:ptr<private, i32, read_write> = access %tint_module_vars, 1u
+    %9:i32 = load %8
+    %10:i32 = add %7, %9
+    %11:ptr<private, i32, read_write> = access %tint_module_vars, 0u
+    store %11, %10
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, Storage) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, load_b));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %4:i32 = load %a
+    %5:i32 = load %b
+    %6:i32 = add %4, %5
+    store %b, %6
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %4:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %7:i32 = load %6
+    %8:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %9:i32 = load %8
+    %10:i32 = add %7, %9
+    %11:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %11, %10
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, Uniform) {
+    auto* var_a = b.Var("a", ty.ptr<uniform, i32>());
+    auto* var_b = b.Var("b", ty.ptr<uniform, i32>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Add<i32>(load_a, load_b);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %b:ptr<uniform, i32, read> = var @binding_point(3, 4)
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %4:i32 = load %a
+    %5:i32 = load %b
+    %6:i32 = add %4, %5
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<uniform, i32, read> @offset(0)
+  b:ptr<uniform, i32, read> @offset(0)
+}
+
+%foo = @fragment func(%a:ptr<uniform, i32, read> [@binding_point(1, 2)], %b:ptr<uniform, i32, read> [@binding_point(3, 4)]):void {
+  $B1: {
+    %4:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %7:i32 = load %6
+    %8:ptr<uniform, i32, read> = access %tint_module_vars, 1u
+    %9:i32 = load %8
+    %10:i32 = add %7, %9
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, MultipleAddressSpaces) {
+    auto* var_a = b.Var("a", ty.ptr<uniform, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    auto* var_c = b.Var("c", ty.ptr<private_, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+    mod.root_block->Append(var_c);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        auto* load_c = b.Load(var_c);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, load_c)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+  %c:ptr<private, i32, read_write> = var
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = load %c
+    %8:i32 = add %6, %7
+    %9:i32 = add %5, %8
+    store %b, %9
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<uniform, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+  c:ptr<private, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func(%a:ptr<uniform, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %c:ptr<private, i32, read_write> = var
+    %5:tint_module_vars_struct = construct %a, %b, %c
+    %tint_module_vars:tint_module_vars_struct = let %5
+    %7:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %8:i32 = load %7
+    %9:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %10:i32 = load %9
+    %11:ptr<private, i32, read_write> = access %tint_module_vars, 2u
+    %12:i32 = load %11
+    %13:i32 = add %10, %12
+    %14:i32 = add %8, %13
+    %15:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %15, %14
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, EntryPointHasExistingParameters) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    auto* param = b.FunctionParam<i32>("param");
+    param->SetLocation(
+        core::ir::Location{1_u, core::Interpolation{core::InterpolationType::kFlat}});
+    func->SetParams({param});
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, param)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = @fragment func(%param:i32 [@location(1), @interpolate(flat)]):void {
+  $B2: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = add %6, %param
+    %8:i32 = add %5, %7
+    store %b, %8
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func(%param:i32 [@location(1), @interpolate(flat)], %a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %5:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %5
+    %7:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %8:i32 = load %7
+    %9:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %10:i32 = load %9
+    %11:i32 = add %10, %param
+    %12:i32 = add %8, %11
+    %13:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %13, %12
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionThatUsesVars_NoArgs) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* foo = b.Function("foo", ty.void_());
+    b.Append(foo->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, load_b));
+        b.Return(foo);
+    });
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        b.Call(foo);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = func():void {
+  $B2: {
+    %4:i32 = load %a
+    %5:i32 = load %b
+    %6:i32 = add %4, %5
+    store %b, %6
+    ret
+  }
+}
+%main = @fragment func():void {
+  $B3: {
+    %8:void = call %foo
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = func(%tint_module_vars:tint_module_vars_struct):void {
+  $B1: {
+    %3:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %4:i32 = load %3
+    %5:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %6:i32 = load %5
+    %7:i32 = add %4, %6
+    %8:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %8, %7
+    ret
+  }
+}
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B2: {
+    %12:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars_1:tint_module_vars_struct = let %12  # %tint_module_vars_1: 'tint_module_vars'
+    %14:void = call %foo, %tint_module_vars_1
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionThatUsesVars_WithExistingParameters) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* foo = b.Function("foo", ty.void_());
+    auto* param = b.FunctionParam<i32>("param");
+    foo->SetParams({param});
+    b.Append(foo->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, param)));
+        b.Return(foo);
+    });
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        b.Call(foo, 42_i);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = func(%param:i32):void {
+  $B2: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = add %6, %param
+    %8:i32 = add %5, %7
+    store %b, %8
+    ret
+  }
+}
+%main = @fragment func():void {
+  $B3: {
+    %10:void = call %foo, 42i
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = func(%param:i32, %tint_module_vars:tint_module_vars_struct):void {
+  $B1: {
+    %4:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %5:i32 = load %4
+    %6:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %7:i32 = load %6
+    %8:i32 = add %7, %param
+    %9:i32 = add %5, %8
+    %10:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %10, %9
+    ret
+  }
+}
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B2: {
+    %14:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars_1:tint_module_vars_struct = let %14  # %tint_module_vars_1: 'tint_module_vars'
+    %16:void = call %foo, 42i, %tint_module_vars_1
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionThatUsesVars_OutOfOrder) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+
+    auto* foo = b.Function("foo", ty.void_());
+    b.Append(foo->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, load_b));
+        b.Return(foo);
+    });
+
+    b.Append(func->Block(), [&] {
+        b.Call(foo);
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%main = @fragment func():void {
+  $B2: {
+    %4:void = call %foo
+    ret
+  }
+}
+%foo = func():void {
+  $B3: {
+    %6:i32 = load %a
+    %7:i32 = load %b
+    %8:i32 = add %6, %7
+    store %b, %8
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %4:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:void = call %foo, %tint_module_vars
+    ret
+  }
+}
+%foo = func(%tint_module_vars_1:tint_module_vars_struct):void {  # %tint_module_vars_1: 'tint_module_vars'
+  $B2: {
+    %9:ptr<storage, i32, read> = access %tint_module_vars_1, 0u
+    %10:i32 = load %9
+    %11:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    %12:i32 = load %11
+    %13:i32 = add %10, %12
+    %14:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    store %14, %13
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+// Test that we do not add the structure to functions that do not need it.
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionThatDoesNotUseVars) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* foo = b.Function("foo", ty.i32());
+    b.Append(foo->Block(), [&] {  //
+        b.Return(foo, 42_i);
+    });
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, b.Call(foo))));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = func():i32 {
+  $B2: {
+    ret 42i
+  }
+}
+%main = @fragment func():void {
+  $B3: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = call %foo
+    %8:i32 = add %6, %7
+    %9:i32 = add %5, %8
+    store %b, %9
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = func():i32 {
+  $B1: {
+    ret 42i
+  }
+}
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B2: {
+    %5:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars:tint_module_vars_struct = let %5
+    %7:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %8:i32 = load %7
+    %9:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %10:i32 = load %9
+    %11:i32 = call %foo
+    %12:i32 = add %10, %11
+    %13:i32 = add %8, %12
+    %14:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %14, %13
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+// Test that we *do* add the structure to functions that only have transitive uses.
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionWithOnlyTransitiveUses) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* bar = b.Function("bar", ty.i32());
+    b.Append(bar->Block(), [&] {  //
+        b.Return(bar, b.Load(var_a));
+    });
+
+    auto* foo = b.Function("foo", ty.i32());
+    b.Append(foo->Block(), [&] {  //
+        b.Return(foo, b.Call(bar));
+    });
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, b.Call(foo))));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%bar = func():i32 {
+  $B2: {
+    %4:i32 = load %a
+    ret %4
+  }
+}
+%foo = func():i32 {
+  $B3: {
+    %6:i32 = call %bar
+    ret %6
+  }
+}
+%main = @fragment func():void {
+  $B4: {
+    %8:i32 = load %a
+    %9:i32 = load %b
+    %10:i32 = call %foo
+    %11:i32 = add %9, %10
+    %12:i32 = add %8, %11
+    store %b, %12
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%bar = func(%tint_module_vars:tint_module_vars_struct):i32 {
+  $B1: {
+    %3:ptr<storage, i32, read> = access %tint_module_vars, 0u
+    %4:i32 = load %3
+    ret %4
+  }
+}
+%foo = func(%tint_module_vars_1:tint_module_vars_struct):i32 {  # %tint_module_vars_1: 'tint_module_vars'
+  $B2: {
+    %7:i32 = call %bar, %tint_module_vars_1
+    ret %7
+  }
+}
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B3: {
+    %11:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars_2:tint_module_vars_struct = let %11  # %tint_module_vars_2: 'tint_module_vars'
+    %13:ptr<storage, i32, read> = access %tint_module_vars_2, 0u
+    %14:i32 = load %13
+    %15:ptr<storage, i32, read_write> = access %tint_module_vars_2, 1u
+    %16:i32 = load %15
+    %17:i32 = call %foo, %tint_module_vars_2
+    %18:i32 = add %16, %17
+    %19:i32 = add %14, %18
+    %20:ptr<storage, i32, read_write> = access %tint_module_vars_2, 1u
+    store %20, %19
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+// Test that we *do* add the structure to functions that only have transitive uses, where that
+// function is declared first.
+TEST_F(MslWriter_ModuleScopeVarsTest, CallFunctionWithOnlyTransitiveUses_OutOfOrder) {
+    auto* var_a = b.Var("a", ty.ptr<storage, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+
+    auto* foo = b.Function("foo", ty.i32());
+
+    auto* bar = b.Function("bar", ty.i32());
+    b.Append(bar->Block(), [&] {  //
+        b.Return(bar, b.Load(var_a));
+    });
+
+    b.Append(foo->Block(), [&] {  //
+        b.Return(foo, b.Call(bar));
+    });
+
+    auto* func = b.Function("main", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, b.Call(foo))));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<storage, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+}
+
+%foo = func():i32 {
+  $B2: {
+    %4:i32 = call %bar
+    ret %4
+  }
+}
+%bar = func():i32 {
+  $B3: {
+    %6:i32 = load %a
+    ret %6
+  }
+}
+%main = @fragment func():void {
+  $B4: {
+    %8:i32 = load %a
+    %9:i32 = load %b
+    %10:i32 = call %foo
+    %11:i32 = add %9, %10
+    %12:i32 = add %8, %11
+    store %b, %12
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<storage, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+}
+
+%foo = func(%tint_module_vars:tint_module_vars_struct):i32 {
+  $B1: {
+    %3:i32 = call %bar, %tint_module_vars
+    ret %3
+  }
+}
+%bar = func(%tint_module_vars_1:tint_module_vars_struct):i32 {  # %tint_module_vars_1: 'tint_module_vars'
+  $B2: {
+    %6:ptr<storage, i32, read> = access %tint_module_vars_1, 0u
+    %7:i32 = load %6
+    ret %7
+  }
+}
+%main = @fragment func(%a:ptr<storage, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B3: {
+    %11:tint_module_vars_struct = construct %a, %b
+    %tint_module_vars_2:tint_module_vars_struct = let %11  # %tint_module_vars_2: 'tint_module_vars'
+    %13:ptr<storage, i32, read> = access %tint_module_vars_2, 0u
+    %14:i32 = load %13
+    %15:ptr<storage, i32, read_write> = access %tint_module_vars_2, 1u
+    %16:i32 = load %15
+    %17:i32 = call %foo, %tint_module_vars_2
+    %18:i32 = add %16, %17
+    %19:i32 = add %14, %18
+    %20:ptr<storage, i32, read_write> = access %tint_module_vars_2, 1u
+    store %20, %19
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, MultipleEntryPoints) {
+    auto* var_a = b.Var("a", ty.ptr<uniform, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    auto* var_c = b.Var("c", ty.ptr<private_, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+    mod.root_block->Append(var_c);
+
+    auto* main_a = b.Function("main_a", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_a->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        auto* load_c = b.Load(var_c);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, load_c)));
+        b.Return(main_a);
+    });
+
+    auto* main_b = b.Function("main_b", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_b->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        auto* load_c = b.Load(var_c);
+        b.Store(var_b, b.Multiply<i32>(load_a, b.Multiply<i32>(load_b, load_c)));
+        b.Return(main_b);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+  %c:ptr<private, i32, read_write> = var
+}
+
+%main_a = @fragment func():void {
+  $B2: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = load %c
+    %8:i32 = add %6, %7
+    %9:i32 = add %5, %8
+    store %b, %9
+    ret
+  }
+}
+%main_b = @fragment func():void {
+  $B3: {
+    %11:i32 = load %a
+    %12:i32 = load %b
+    %13:i32 = load %c
+    %14:i32 = mul %12, %13
+    %15:i32 = mul %11, %14
+    store %b, %15
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<uniform, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+  c:ptr<private, i32, read_write> @offset(0)
+}
+
+%main_a = @fragment func(%a:ptr<uniform, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %c:ptr<private, i32, read_write> = var
+    %5:tint_module_vars_struct = construct %a, %b, %c
+    %tint_module_vars:tint_module_vars_struct = let %5
+    %7:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %8:i32 = load %7
+    %9:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %10:i32 = load %9
+    %11:ptr<private, i32, read_write> = access %tint_module_vars, 2u
+    %12:i32 = load %11
+    %13:i32 = add %10, %12
+    %14:i32 = add %8, %13
+    %15:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %15, %14
+    ret
+  }
+}
+%main_b = @fragment func(%a_1:ptr<uniform, i32, read> [@binding_point(1, 2)], %b_1:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {  # %a_1: 'a', %b_1: 'b'
+  $B2: {
+    %c_1:ptr<private, i32, read_write> = var  # %c_1: 'c'
+    %20:tint_module_vars_struct = construct %a_1, %b_1, %c_1
+    %tint_module_vars_1:tint_module_vars_struct = let %20  # %tint_module_vars_1: 'tint_module_vars'
+    %22:ptr<uniform, i32, read> = access %tint_module_vars_1, 0u
+    %23:i32 = load %22
+    %24:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    %25:i32 = load %24
+    %26:ptr<private, i32, read_write> = access %tint_module_vars_1, 2u
+    %27:i32 = load %26
+    %28:i32 = mul %25, %27
+    %29:i32 = mul %23, %28
+    %30:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    store %30, %29
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, MultipleEntryPoints_DifferentUsageSets) {
+    auto* var_a = b.Var("a", ty.ptr<uniform, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    auto* var_c = b.Var("c", ty.ptr<private_, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+    mod.root_block->Append(var_c);
+
+    auto* main_a = b.Function("main_a", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_a->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(load_a, load_b));
+        b.Return(main_a);
+    });
+
+    auto* main_b = b.Function("main_b", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_b->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_c = b.Load(var_c);
+        b.Store(var_c, b.Multiply<i32>(load_a, load_c));
+        b.Return(main_b);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+  %c:ptr<private, i32, read_write> = var
+}
+
+%main_a = @fragment func():void {
+  $B2: {
+    %5:i32 = load %a
+    %6:i32 = load %b
+    %7:i32 = add %5, %6
+    store %b, %7
+    ret
+  }
+}
+%main_b = @fragment func():void {
+  $B3: {
+    %9:i32 = load %a
+    %10:i32 = load %c
+    %11:i32 = mul %9, %10
+    store %c, %11
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<uniform, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+  c:ptr<private, i32, read_write> @offset(0)
+}
+
+%main_a = @fragment func(%a:ptr<uniform, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %4:tint_module_vars_struct = construct %a, %b, undef
+    %tint_module_vars:tint_module_vars_struct = let %4
+    %6:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %7:i32 = load %6
+    %8:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %9:i32 = load %8
+    %10:i32 = add %7, %9
+    %11:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %11, %10
+    ret
+  }
+}
+%main_b = @fragment func(%a_1:ptr<uniform, i32, read> [@binding_point(1, 2)]):void {  # %a_1: 'a'
+  $B2: {
+    %c:ptr<private, i32, read_write> = var
+    %15:tint_module_vars_struct = construct %a_1, undef, %c
+    %tint_module_vars_1:tint_module_vars_struct = let %15  # %tint_module_vars_1: 'tint_module_vars'
+    %17:ptr<uniform, i32, read> = access %tint_module_vars_1, 0u
+    %18:i32 = load %17
+    %19:ptr<private, i32, read_write> = access %tint_module_vars_1, 2u
+    %20:i32 = load %19
+    %21:i32 = mul %18, %20
+    %22:ptr<private, i32, read_write> = access %tint_module_vars_1, 2u
+    store %22, %21
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, MultipleEntryPoints_DifferentUsageSets_CommonHelper) {
+    auto* var_a = b.Var("a", ty.ptr<uniform, i32, core::Access::kRead>());
+    auto* var_b = b.Var("b", ty.ptr<storage, i32, core::Access::kReadWrite>());
+    auto* var_c = b.Var("c", ty.ptr<private_, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+    mod.root_block->Append(var_c);
+
+    auto* foo = b.Function("foo", ty.i32());
+    b.Append(foo->Block(), [&] {  //
+        b.Return(foo, b.Load(var_a));
+    });
+
+    auto* main_a = b.Function("main_a", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_a->Block(), [&] {
+        auto* load_b = b.Load(var_b);
+        b.Store(var_b, b.Add<i32>(b.Call(foo), load_b));
+        b.Return(main_a);
+    });
+
+    auto* main_b = b.Function("main_b", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(main_b->Block(), [&] {
+        auto* load_c = b.Load(var_c);
+        b.Store(var_c, b.Multiply<i32>(b.Call(foo), load_c));
+        b.Return(main_b);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %a:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %b:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+  %c:ptr<private, i32, read_write> = var
+}
+
+%foo = func():i32 {
+  $B2: {
+    %5:i32 = load %a
+    ret %5
+  }
+}
+%main_a = @fragment func():void {
+  $B3: {
+    %7:i32 = load %b
+    %8:i32 = call %foo
+    %9:i32 = add %8, %7
+    store %b, %9
+    ret
+  }
+}
+%main_b = @fragment func():void {
+  $B4: {
+    %11:i32 = load %c
+    %12:i32 = call %foo
+    %13:i32 = mul %12, %11
+    store %c, %13
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  a:ptr<uniform, i32, read> @offset(0)
+  b:ptr<storage, i32, read_write> @offset(0)
+  c:ptr<private, i32, read_write> @offset(0)
+}
+
+%foo = func(%tint_module_vars:tint_module_vars_struct):i32 {
+  $B1: {
+    %3:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %4:i32 = load %3
+    ret %4
+  }
+}
+%main_a = @fragment func(%a:ptr<uniform, i32, read> [@binding_point(1, 2)], %b:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B2: {
+    %8:tint_module_vars_struct = construct %a, %b, undef
+    %tint_module_vars_1:tint_module_vars_struct = let %8  # %tint_module_vars_1: 'tint_module_vars'
+    %10:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    %11:i32 = load %10
+    %12:i32 = call %foo, %tint_module_vars_1
+    %13:i32 = add %12, %11
+    %14:ptr<storage, i32, read_write> = access %tint_module_vars_1, 1u
+    store %14, %13
+    ret
+  }
+}
+%main_b = @fragment func(%a_1:ptr<uniform, i32, read> [@binding_point(1, 2)]):void {  # %a_1: 'a'
+  $B3: {
+    %c:ptr<private, i32, read_write> = var
+    %18:tint_module_vars_struct = construct %a_1, undef, %c
+    %tint_module_vars_2:tint_module_vars_struct = let %18  # %tint_module_vars_2: 'tint_module_vars'
+    %20:ptr<private, i32, read_write> = access %tint_module_vars_2, 2u
+    %21:i32 = load %20
+    %22:i32 = call %foo, %tint_module_vars_2
+    %23:i32 = mul %22, %21
+    %24:ptr<private, i32, read_write> = access %tint_module_vars_2, 2u
+    store %24, %23
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+TEST_F(MslWriter_ModuleScopeVarsTest, VarsWithNoNames) {
+    auto* var_a = b.Var(ty.ptr<uniform, i32, core::Access::kRead>());
+    auto* var_b = b.Var(ty.ptr<storage, i32, core::Access::kReadWrite>());
+    auto* var_c = b.Var(ty.ptr<private_, i32, core::Access::kReadWrite>());
+    var_a->SetBindingPoint(1, 2);
+    var_b->SetBindingPoint(3, 4);
+    mod.root_block->Append(var_a);
+    mod.root_block->Append(var_b);
+    mod.root_block->Append(var_c);
+
+    auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
+    b.Append(func->Block(), [&] {
+        auto* load_a = b.Load(var_a);
+        auto* load_b = b.Load(var_b);
+        auto* load_c = b.Load(var_c);
+        b.Store(var_b, b.Add<i32>(load_a, b.Add<i32>(load_b, load_c)));
+        b.Return(func);
+    });
+
+    auto* src = R"(
+$B1: {  # root
+  %1:ptr<uniform, i32, read> = var @binding_point(1, 2)
+  %2:ptr<storage, i32, read_write> = var @binding_point(3, 4)
+  %3:ptr<private, i32, read_write> = var
+}
+
+%foo = @fragment func():void {
+  $B2: {
+    %5:i32 = load %1
+    %6:i32 = load %2
+    %7:i32 = load %3
+    %8:i32 = add %6, %7
+    %9:i32 = add %5, %8
+    store %2, %9
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+tint_module_vars_struct = struct @align(1) {
+  tint_symbol:ptr<uniform, i32, read> @offset(0)
+  tint_symbol_1:ptr<storage, i32, read_write> @offset(0)
+  tint_symbol_2:ptr<private, i32, read_write> @offset(0)
+}
+
+%foo = @fragment func(%2:ptr<uniform, i32, read> [@binding_point(1, 2)], %3:ptr<storage, i32, read_write> [@binding_point(3, 4)]):void {
+  $B1: {
+    %4:ptr<private, i32, read_write> = var
+    %5:tint_module_vars_struct = construct %2, %3, %4
+    %tint_module_vars:tint_module_vars_struct = let %5
+    %7:ptr<uniform, i32, read> = access %tint_module_vars, 0u
+    %8:i32 = load %7
+    %9:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    %10:i32 = load %9
+    %11:ptr<private, i32, read_write> = access %tint_module_vars, 2u
+    %12:i32 = load %11
+    %13:i32 = add %10, %12
+    %14:i32 = add %8, %13
+    %15:ptr<storage, i32, read_write> = access %tint_module_vars, 1u
+    store %15, %14
+    ret
+  }
+}
+)";
+
+    Run(ModuleScopeVars);
+
+    EXPECT_EQ(expect, str());
+}
+
+}  // namespace
+}  // namespace tint::msl::writer::raise
diff --git a/src/tint/lang/msl/writer/raise/raise.cc b/src/tint/lang/msl/writer/raise/raise.cc
index fab831d..fea8afd 100644
--- a/src/tint/lang/msl/writer/raise/raise.cc
+++ b/src/tint/lang/msl/writer/raise/raise.cc
@@ -42,6 +42,7 @@
 #include "src/tint/lang/core/ir/transform/zero_init_workgroup_memory.h"
 #include "src/tint/lang/msl/writer/common/option_helpers.h"
 #include "src/tint/lang/msl/writer/raise/builtin_polyfill.h"
+#include "src/tint/lang/msl/writer/raise/module_scope_vars.h"
 
 namespace tint::msl::writer {
 
@@ -97,13 +98,13 @@
         RUN_TRANSFORM(core::ir::transform::ZeroInitWorkgroupMemory);
     }
 
-    // PreservePadding must come before DirectVariableAccess.
     RUN_TRANSFORM(core::ir::transform::PreservePadding);
     RUN_TRANSFORM(core::ir::transform::VectorizeScalarMatrixConstructors);
 
     // DemoteToHelper must come before any transform that introduces non-core instructions.
     RUN_TRANSFORM(core::ir::transform::DemoteToHelper);
 
+    RUN_TRANSFORM(raise::ModuleScopeVars);
     RUN_TRANSFORM(core::ir::transform::ValueToLet);
     RUN_TRANSFORM(raise::BuiltinPolyfill);