[clang] [llvm] [SPIR-V] Implement SPV_KHR_untyped_pointers extension (PR #201233)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 5 07:31:21 PDT 2026


================
@@ -843,10 +844,40 @@ Register SPIRVGlobalRegistry::buildGlobalVariable(
   if (&GVBuilder.getMBB() != &EntryBB)
     GVBuilder.setInsertPt(EntryBB, EntryBB.getFirstTerminator());
 
-  auto MIB = GVBuilder.buildInstr(SPIRV::OpVariable)
+  // Pointers to opaque types stay typed even with the extension on, so emit the
+  // untyped variant only when the result is actually an untyped pointer.
+  const SPIRVSubtarget &Subtarget =
+      cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
+  const bool UseUntypedPointers =
+      BaseType->getOpcode() == SPIRV::OpTypeUntypedPointerKHR;
+  const unsigned VariableOpcode =
+      UseUntypedPointers ? SPIRV::OpUntypedVariableKHR : SPIRV::OpVariable;
+
+  auto MIB = GVBuilder.buildInstr(VariableOpcode)
                  .addDef(ResVReg)
-                 .addUse(getSPIRVTypeID(BaseType))
-                 .addImm(static_cast<uint32_t>(Storage));
+                 .addUse(getSPIRVTypeID(BaseType));
+
+  // Add storage class (comes before DataType for OpUntypedVariableKHR).
+  MIB.addImm(static_cast<uint32_t>(Storage));
+
+  // For OpUntypedVariableKHR, add the Data Type operand after the storage
+  // class.
+  if (UseUntypedPointers) {
+    // Data Type = the global's value type.
+    SPIRVTypeInst DataType = getPointeeType(BaseType);
+    if (!DataType && GVar)
----------------
MrSidims wrote:

indeed, applied

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


More information about the llvm-commits mailing list