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

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 15 00:59:08 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-07-15T09:59:03+02:00
New Revision: b78a0acaa964804151dd3a25eaec360baacfd631

URL: https://github.com/llvm/llvm-project/commit/b78a0acaa964804151dd3a25eaec360baacfd631
DIFF: https://github.com/llvm/llvm-project/commit/b78a0acaa964804151dd3a25eaec360baacfd631.diff

LOG: [SPIR-V] Unify duplicated named MDNode lookup helper (#209376)

Added: 
    

Modified: 
    llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Removed: 
    


################################################################################
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)))


        


More information about the llvm-commits mailing list