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

Marcos Maronas via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 5 07:18:47 PDT 2026


================
@@ -6923,11 +7031,32 @@ bool SPIRVInstructionSelector::selectFrameIndex(Register ResVReg,
   // Change order of instructions if needed: all OpVariable instructions in a
   // function must be the first instructions in the first block
   auto It = getOpVariableMBBIt(*I.getMF());
-  BuildMI(*It->getParent(), It, It->getDebugLoc(), TII.get(SPIRV::OpVariable))
-      .addDef(ResVReg)
-      .addUse(GR.getSPIRVTypeID(ResType))
-      .addImm(static_cast<uint32_t>(SPIRV::StorageClass::Function))
-      .constrainAllUses(TII, TRI, RBI);
+
+  // 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.
+  bool UseUntypedPointers =
+      ResType->getOpcode() == SPIRV::OpTypeUntypedPointerKHR;
+  unsigned Opcode =
+      UseUntypedPointers ? SPIRV::OpUntypedVariableKHR : SPIRV::OpVariable;
+
+  auto MIB = BuildMI(*It->getParent(), It, It->getDebugLoc(), TII.get(Opcode))
+                 .addDef(ResVReg)
+                 .addUse(GR.getSPIRVTypeID(ResType))
+                 .addImm(static_cast<uint32_t>(SPIRV::StorageClass::Function));
+
+  // OpUntypedVariableKHR takes an extra Data Type operand right after the
+  // storage class.
+  if (UseUntypedPointers) {
+    // Get the element type that was stored when processing spv_assign_ptr_type.
+    SPIRVTypeInst DataType = GR.getUntypedPtrElementType(ResVReg);
+    if (!DataType) {
+      // Use i8 as a last resort.
----------------
maarquitos14 wrote:

Same question as in the previous "Use i8 as a last resort."

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


More information about the cfe-commits mailing list