[tint][ir] Serialize structs first

Change-Id: I5c53712519d8688637f8c5a58468e715a6d52ce8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/164890
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/lang/core/ir/binary/encode.cc b/src/tint/lang/core/ir/binary/encode.cc
index 9c213cc..ef64042 100644
--- a/src/tint/lang/core/ir/binary/encode.cc
+++ b/src/tint/lang/core/ir/binary/encode.cc
@@ -92,6 +92,13 @@
     Hashmap<const core::constant::Value*, uint32_t, 32> constant_values_{};
 
     void Encode() {
+        // Encode all user-declared structures first. This is to ensure that the IR disassembly
+        // (which prints structure types first) does not reorder after encoding and decoding.
+        for (auto* ty : mod_in_.Types()) {
+            if (auto* str = ty->As<core::type::Struct>()) {
+                Type(str);
+            }
+        }
         Vector<pb::Function*, 8> fns_out;
         for (auto& fn_in : mod_in_.functions) {
             uint32_t id = static_cast<uint32_t>(fns_out.Length() + 1);