[NFC][SPIR-V] Dedupe getConstInt metadata helper into SPIRVUtils#208194
Merged
Conversation
|
@llvm/pr-subscribers-backend-spir-v Author: Arseniy Obolenskiy (aobolensk) ChangesFull diff: https://github.com/llvm/llvm-project/pull/208194.diff 3 Files Affected:
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);
|
jmmartinez
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.