[llvm] [SPIR-V] Unify duplicated named MDNode lookup helper (PR #209376)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 22:19:53 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-spir-v

Author: Arseniy Obolenskiy (aobolensk)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/209376.diff


1 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVUtils.cpp (+16-18) 


``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index c6f53609a2b3d..802053d6cd54b 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -32,6 +32,15 @@
 
 namespace llvm {
 namespace SPIRV {
+static MDNode *findNamedMDOperand(NamedMDNode *NMD, StringRef Name) {
+  auto It = find_if(NMD->operands(), [Name](MDNode *N) {
+    if (auto *MDS = dyn_cast_or_null<MDString>(N->getOperand(0)))
+      return MDS->getString() == Name;
+    return false;
+  });
+  return It == NMD->op_end() ? nullptr : *It;
+}
+
 // This code restores function args/retvalue types for composite cases
 // because the final types should still be aggregate whereas they're i32
 // during the translation to cope with aggregate flattening etc.
@@ -42,20 +51,15 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD,
   if (!NMD)
     return FTy;
 
-  auto It = find_if(NMD->operands(), [Name](MDNode *N) {
-    if (auto *MDS = dyn_cast_or_null<MDString>(N->getOperand(0)))
-      return MDS->getString() == Name;
-    return false;
-  });
-
-  if (It == NMD->op_end())
+  MDNode *Match = findNamedMDOperand(NMD, Name);
+  if (!Match)
     return FTy;
 
   Type *RetTy = FTy->getReturnType();
   SmallVector<Type *, 4> PTys(FTy->params());
 
-  for (unsigned I = 1; I != (*It)->getNumOperands(); ++I) {
-    MDNode *MD = dyn_cast<MDNode>((*It)->getOperand(I));
+  for (unsigned I = 1; I != Match->getNumOperands(); ++I) {
+    MDNode *MD = dyn_cast<MDNode>(Match->getOperand(I));
     assert(MD && "MDNode operand is expected");
 
     if (auto *Const = getMDOperandAsConstInt(MD, 0)) {
@@ -82,21 +86,15 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD,
 static StringRef extractAsmConstraintsFromMetadata(NamedMDNode *NMD,
                                                    StringRef Constraints,
                                                    StringRef Name) {
-  // TODO: unify the extractors.
   if (!NMD)
     return Constraints;
 
-  auto It = find_if(NMD->operands(), [Name](MDNode *N) {
-    if (auto *MDS = dyn_cast_or_null<MDString>(N->getOperand(0)))
-      return MDS->getString() == Name;
-    return false;
-  });
-
-  if (It == NMD->op_end())
+  MDNode *Match = findNamedMDOperand(NMD, Name);
+  if (!Match)
     return Constraints;
 
   // By convention, the constraints string is stored in the final MD operand.
-  MDNode *MD = dyn_cast<MDNode>((*It)->getOperand((*It)->getNumOperands() - 1));
+  MDNode *MD = dyn_cast<MDNode>(Match->getOperand(Match->getNumOperands() - 1));
   assert(MD && "MDNode operand is expected");
 
   if (auto *MDS = dyn_cast<MDString>(MD->getOperand(0)))

``````````

</details>


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


More information about the llvm-commits mailing list