Skip to content

[NFC][SPIR-V] Dedupe getConstInt metadata helper into SPIRVUtils#208194

Merged
aobolensk merged 1 commit into
llvm:mainfrom
aobolensk:llvm-spirv-const-int-md-helper
Jul 8, 2026
Merged

[NFC][SPIR-V] Dedupe getConstInt metadata helper into SPIRVUtils#208194
aobolensk merged 1 commit into
llvm:mainfrom
aobolensk:llvm-spirv-const-int-md-helper

Conversation

@aobolensk

Copy link
Copy Markdown
Contributor

No description provided.

@llvmorg-github-actions

Copy link
Copy Markdown

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

Author: Arseniy Obolenskiy (aobolensk)

Changes

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

3 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp (+2-11)
  • (modified) llvm/lib/Target/SPIRV/SPIRVUtils.cpp (+9-9)
  • (modified) llvm/lib/Target/SPIRV/SPIRVUtils.h (+4)
diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
index d177e934cdb83..8a7bc05d3fe39 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
@@ -86,15 +86,6 @@ static uint32_t getFunctionControl(const Function &F,
   return FuncControl;
 }
 
-static ConstantInt *getConstInt(MDNode *MD, unsigned NumOp) {
-  if (MD->getNumOperands() > NumOp) {
-    auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(NumOp));
-    if (CMeta)
-      return dyn_cast<ConstantInt>(CMeta->getValue());
-  }
-  return nullptr;
-}
-
 // If the function has pointer arguments, we are forced to re-create this
 // function type from the very beginning, changing PointerType by
 // TypedPointerType for each pointer argument. Otherwise, the same `Type*`
@@ -358,13 +349,13 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
         for (const MDOperand &MDOp : MD->operands()) {
           MDNode *MD2 = dyn_cast<MDNode>(MDOp);
           assert(MD2 && "Metadata operand is expected");
-          ConstantInt *Const = getConstInt(MD2, 0);
+          ConstantInt *Const = getMDOperandAsConstInt(MD2, 0);
           assert(Const && "MDOperand should be ConstantInt");
           auto Dec =
               static_cast<SPIRV::Decoration::Decoration>(Const->getZExtValue());
           std::vector<uint32_t> DecVec;
           for (unsigned j = 1; j < MD2->getNumOperands(); j++) {
-            ConstantInt *Const = getConstInt(MD2, j);
+            ConstantInt *Const = getMDOperandAsConstInt(MD2, j);
             assert(Const && "MDOperand should be ConstantInt");
             DecVec.push_back(static_cast<uint32_t>(Const->getZExtValue()));
           }
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
index fafc0c1c63177..748783490ef64 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.cpp
@@ -43,14 +43,6 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD,
   if (!NMD)
     return FTy;
 
-  constexpr auto getConstInt = [](MDNode *MD, unsigned OpId) -> ConstantInt * {
-    if (MD->getNumOperands() <= OpId)
-      return nullptr;
-    if (auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(OpId)))
-      return dyn_cast<ConstantInt>(CMeta->getValue());
-    return nullptr;
-  };
-
   auto It = find_if(NMD->operands(), [Name](MDNode *N) {
     if (auto *MDS = dyn_cast_or_null<MDString>(N->getOperand(0)))
       return MDS->getString() == Name;
@@ -67,7 +59,7 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD,
     MDNode *MD = dyn_cast<MDNode>((*It)->getOperand(I));
     assert(MD && "MDNode operand is expected");
 
-    if (auto *Const = getConstInt(MD, 0)) {
+    if (auto *Const = getMDOperandAsConstInt(MD, 0)) {
       auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
       assert(CMeta && "ConstantAsMetadata operand is expected");
       int64_t Idx = Const->getSExtValue();
@@ -508,6 +500,14 @@ Type *getMDOperandAsType(const MDNode *N, unsigned I) {
   return toTypedPointer(ElementTy);
 }
 
+ConstantInt *getMDOperandAsConstInt(const MDNode *N, unsigned I) {
+  if (N->getNumOperands() <= I)
+    return nullptr;
+  if (auto *CMeta = dyn_cast<ConstantAsMetadata>(N->getOperand(I)))
+    return dyn_cast<ConstantInt>(CMeta->getValue());
+  return nullptr;
+}
+
 static bool isEnqueueKernelBI(StringRef MangledName) {
   return MangledName == "__enqueue_kernel_basic" ||
          MangledName == "__enqueue_kernel_basic_events" ||
diff --git a/llvm/lib/Target/SPIRV/SPIRVUtils.h b/llvm/lib/Target/SPIRV/SPIRVUtils.h
index 24853aead48a0..64fecb37c880f 100644
--- a/llvm/lib/Target/SPIRV/SPIRVUtils.h
+++ b/llvm/lib/Target/SPIRV/SPIRVUtils.h
@@ -303,6 +303,10 @@ bool isSpvIntrinsic(const Value *Arg);
 // Get type of i-th operand of the metadata node.
 Type *getMDOperandAsType(const MDNode *N, unsigned I);
 
+// Get the i-th operand of the metadata node as a ConstantInt, or nullptr if it
+// is out of range or not a ConstantInt.
+ConstantInt *getMDOperandAsConstInt(const MDNode *N, unsigned I);
+
 // If OpenCL or SPIR-V builtin function name is recognized, return a demangled
 // name, otherwise return an empty string.
 std::string getOclOrSpirvBuiltinDemangledName(StringRef Name);

@aobolensk aobolensk merged commit 56f655f into llvm:main Jul 8, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants