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(MD->getOperand(NumOp)); - if (CMeta) - return dyn_cast(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(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(Const->getZExtValue()); std::vector 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(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(MD->getOperand(OpId))) - return dyn_cast(CMeta->getValue()); - return nullptr; - }; - auto It = find_if(NMD->operands(), [Name](MDNode *N) { if (auto *MDS = dyn_cast_or_null(N->getOperand(0))) return MDS->getString() == Name; @@ -67,7 +59,7 @@ static FunctionType *extractFunctionTypeFromMetadata(NamedMDNode *NMD, MDNode *MD = dyn_cast((*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(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(N->getOperand(I))) + return dyn_cast(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);