[llvm] [SPIR-V] Consistent handling of TargetExtTypes in emit-intrinsics (PR #135682)

Steven Perron via llvm-commits llvm-commits at lists.llvm.org
Thu May 29 11:15:23 PDT 2025


https://github.com/s-perron updated https://github.com/llvm/llvm-project/pull/135682

>From e5b9e1d5ffe7a0dd2b5873c5d094175fd8c9d964 Mon Sep 17 00:00:00 2001
From: Cassandra Beckley <cbeckley at google.com>
Date: Mon, 14 Apr 2025 13:37:13 -0700
Subject: [PATCH 1/3] [SPIR-V] Consistent handling of TargetExtTypes in
 emit-intrinsics

TargetExtType values are replaced with calls to
`llvm.spv.track.constant`, with a `poison` value, but
`llvm.spv.assign.type` was called with their original value. This PR
updates the `assign.type` call to be consistent with the
`track.constant` call.

Fixes #134417.
---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 20 +++++++------------
 llvm/test/CodeGen/SPIRV/inline/type.undef.ll  | 20 +++++++++++++++++++
 2 files changed, 27 insertions(+), 13 deletions(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/inline/type.undef.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 0067d2400529a..b72de93d041f7 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -1947,10 +1947,14 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
           GR->buildAssignPtr(B, ElemTy ? ElemTy : deduceElementType(Op, true),
                              Op);
         } else {
+          Value *OpTyVal = Op;
+          if (OpTy->isTargetExtTy()) {
+            OpTyVal = getNormalizedPoisonValue(OpTy);
+          }
           CallInst *AssignCI =
               buildIntrWithMD(Intrinsic::spv_assign_type, {OpTy},
-                              getNormalizedPoisonValue(OpTy), Op, {}, B);
-          GR->addAssignPtrTypeInstr(Op, AssignCI);
+                              getNormalizedPoisonValue(OpTy), OpTyVal, {}, B);
+          GR->addAssignPtrTypeInstr(OpTyVal, AssignCI);
         }
       }
     }
@@ -2061,22 +2065,12 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
       BPrepared = true;
     }
     Type *OpTy = Op->getType();
-    Value *OpTyVal = Op;
-    if (OpTy->isTargetExtTy())
-      OpTyVal = getNormalizedPoisonValue(OpTy);
     Type *OpElemTy = GR->findDeducedElementType(Op);
     Value *NewOp = Op;
     if (OpTy->isTargetExtTy()) {
+      Value *OpTyVal = getNormalizedPoisonValue(OpTy);
       NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
                               {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);
-      if (isPointerTy(OpTy)) {
-        if (OpElemTy) {
-          GR->buildAssignPtr(B, OpElemTy, NewOp);
-        } else {
-          insertTodoType(NewOp);
-          GR->buildAssignPtr(B, OpTy, NewOp);
-        }
-      }
     }
     if (!IsConstComposite && isPointerTy(OpTy) && OpElemTy != nullptr &&
         OpElemTy != IntegerType::getInt8Ty(I->getContext())) {
diff --git a/llvm/test/CodeGen/SPIRV/inline/type.undef.ll b/llvm/test/CodeGen/SPIRV/inline/type.undef.ll
new file mode 100644
index 0000000000000..8cb90007c9aba
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/inline/type.undef.ll
@@ -0,0 +1,20 @@
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - | spirv-as - -o - | spirv-val %}
+
+%literal_32 = type target("spirv.Literal", 32)
+%literal_true = type target("spirv.Literal", 1)
+
+; CHECK-DAG: OpUnknown(21, 4) [[int_t:%[0-9]+]] 32 1
+%int_t = type target("spirv.Type", %literal_32, %literal_true, 21, 4, 32)
+
+; CHECK-DAG: {{%[0-9]+}} = OpTypeFunction [[int_t]]
+define %int_t @foo() {
+entry:
+  %v = alloca %int_t
+  %i = load %int_t, ptr %v
+
+; CHECK-DAG: [[i:%[0-9]+]] = OpUndef [[int_t]]
+; CHECK-DAG: OpReturnValue [[i]]
+  ret %int_t %i
+}

>From ef15dffb996615cc4ab7afb0db1ee9e36982e100 Mon Sep 17 00:00:00 2001
From: Cassandra Beckley <cbeckley at google.com>
Date: Fri, 23 May 2025 14:01:10 -0700
Subject: [PATCH 2/3] Add comments

---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index dfea0e3802878..92ceda6601f6c 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -1958,6 +1958,7 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
         } else {
           Value *OpTyVal = Op;
           if (OpTy->isTargetExtTy()) {
+            // We need to do this in order to be consistent with how target ext types are handled in `processInstrAfterVisit`
             OpTyVal = getNormalizedPoisonValue(OpTy);
           }
           CallInst *AssignCI =
@@ -2077,6 +2078,7 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
     Type *OpElemTy = GR->findDeducedElementType(Op);
     Value *NewOp = Op;
     if (OpTy->isTargetExtTy()) {
+      // Since this value is replaced by poison, we need to do the same in `insertAssignTypeIntrs`.
       Value *OpTyVal = getNormalizedPoisonValue(OpTy);
       NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
                               {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);

>From 737c66f60716c17edbfd56933a4ae6c6d66b8df5 Mon Sep 17 00:00:00 2001
From: Steven Perron <stevenperron at google.com>
Date: Thu, 29 May 2025 14:15:10 -0400
Subject: [PATCH 3/3] Fix format

---
 llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
index 92ceda6601f6c..50c93d8f0a4ec 100644
--- a/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
@@ -1958,7 +1958,8 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
         } else {
           Value *OpTyVal = Op;
           if (OpTy->isTargetExtTy()) {
-            // We need to do this in order to be consistent with how target ext types are handled in `processInstrAfterVisit`
+            // We need to do this in order to be consistent with how target ext
+            // types are handled in `processInstrAfterVisit`
             OpTyVal = getNormalizedPoisonValue(OpTy);
           }
           CallInst *AssignCI =
@@ -2078,7 +2079,8 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
     Type *OpElemTy = GR->findDeducedElementType(Op);
     Value *NewOp = Op;
     if (OpTy->isTargetExtTy()) {
-      // Since this value is replaced by poison, we need to do the same in `insertAssignTypeIntrs`.
+      // Since this value is replaced by poison, we need to do the same in
+      // `insertAssignTypeIntrs`.
       Value *OpTyVal = getNormalizedPoisonValue(OpTy);
       NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
                               {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);



More information about the llvm-commits mailing list