[Mlir-commits] [mlir] [mlir][spirv] Add SPIR-V NonSemantic.Graph.DebugInfo (PR #199519)

Igor Wodiany llvmlistbot at llvm.org
Tue May 26 02:10:55 PDT 2026


================
@@ -302,8 +362,154 @@ bool allInstructionsWithinWordLimit(SmallVectorImpl<uint32_t> &binary) {
   return true;
 }
 
+using InstructionCallback =
+    function_ref<void(size_t, spirv::Opcode, ArrayRef<uint32_t>)>;
+
+void walkInstructions(SmallVectorImpl<uint32_t> &binary,
+                      InstructionCallback callback) {
+  size_t offset = spirv::kHeaderWordCount;
+  while (offset < binary.size()) {
+    uint32_t wordCount = binary[offset] >> 16;
+    if (!wordCount || offset + wordCount > binary.size())
+      return;
+
+    auto opcode = static_cast<spirv::Opcode>(binary[offset] & 0xffff);
+    ArrayRef<uint32_t> operands(binary.begin() + offset + 1,
+                                binary.begin() + offset + wordCount);
+    callback(offset, opcode, operands);
+    offset += wordCount;
+  }
+}
+
+std::optional<uint32_t> getExtInstSetID(SmallVectorImpl<uint32_t> &binary,
+                                        StringRef extInstSetName) {
+  std::optional<uint32_t> extInstSetID;
+  walkInstructions(binary, [&](size_t, spirv::Opcode opcode,
+                               ArrayRef<uint32_t> operands) {
+    if (opcode == spirv::Opcode::OpExtInstImport && operands.size() >= 2) {
+      unsigned stringIndex = 1;
+      if (spirv::decodeStringLiteral(operands, stringIndex) == extInstSetName) {
+        extInstSetID = operands[0];
+        return;
+      }
+    }
+  });
+  return extInstSetID;
+}
+
+struct ExtInstRecord {
+  size_t offset;
+  uint32_t resultID;
+  uint32_t setID;
+  uint32_t instruction;
+  SmallVector<uint32_t, 4> arguments;
+};
+
+SmallVector<ExtInstRecord> getExtInstRecords(SmallVectorImpl<uint32_t> &binary,
+                                             uint32_t extInstSetID) {
+  SmallVector<ExtInstRecord> records;
+  walkInstructions(binary, [&](size_t offset, spirv::Opcode opcode,
+                               ArrayRef<uint32_t> operands) {
+    if (opcode == spirv::Opcode::OpExtInst && operands.size() >= 4 &&
+        operands[2] == extInstSetID) {
+      ExtInstRecord record{offset, operands[1], operands[2], operands[3], {}};
+      record.arguments.append(operands.begin() + 4, operands.end());
+      records.push_back(std::move(record));
+    }
+  });
+
+  return records;
+}
+
 } // namespace
 
+TEST_F(SerializationTest, GraphDebugInfoReferencesSerializedObjects) {
----------------
IgWod wrote:

What exactly is being tested here and why cannot it be a lit test?

https://github.com/llvm/llvm-project/pull/199519


More information about the Mlir-commits mailing list