[llvm] [SPIR-V] Unify duplicated named MDNode lookup helper (PR #209376)
Arseniy Obolenskiy via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 13 22:19:22 PDT 2026
https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/209376
None
>From 09f29b33840956e0fba4fe9016a1a38fb6434081 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 14 Jul 2026 07:18:23 +0200
Subject: [PATCH] [SPIR-V] Unify duplicated named MDNode lookup helper
---
llvm/lib/Target/SPIRV/SPIRVUtils.cpp | 34 +++++++++++++---------------
1 file changed, 16 insertions(+), 18 deletions(-)
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