Index: llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp +++ llvm/lib/Target/AMDGPU/AMDGPULibCalls.cpp @@ -601,7 +601,8 @@ return false; // Further check the number of arguments to see if they match. - if (CI->arg_size() != FInfo.getNumArgs()) + // TODO: Check calling convention matches too + if (!FInfo.isCompatibleSignature(CI->getFunctionType())) return false; LLVM_DEBUG(dbgs() << "AMDIC: try folding " << *CI << '\n'); Index: llvm/lib/Target/AMDGPU/AMDGPULibFunc.h =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULibFunc.h +++ llvm/lib/Target/AMDGPU/AMDGPULibFunc.h @@ -383,6 +383,9 @@ return Impl->parseFuncName(MangledName); } + // Validate the call type matches the expected libfunc type. + bool isCompatibleSignature(const FunctionType *FuncTy) const; + /// \return The mangled function name for mangled library functions /// and unmangled function name for unmangled library functions. std::string mangle() const { return Impl->mangle(); } Index: llvm/lib/Target/AMDGPU/AMDGPULibFunc.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULibFunc.cpp +++ llvm/lib/Target/AMDGPU/AMDGPULibFunc.cpp @@ -945,17 +945,21 @@ return std::string(OS.str()); } +bool AMDGPULibFunc::isCompatibleSignature(const FunctionType *FuncTy) const { + // TODO: Validate types make sense + return !FuncTy->isVarArg() && FuncTy->getNumParams() == getNumArgs(); +} + Function *AMDGPULibFunc::getFunction(Module *M, const AMDGPULibFunc &fInfo) { std::string FuncName = fInfo.mangle(); Function *F = dyn_cast_or_null( M->getValueSymbolTable().lookup(FuncName)); // check formal with actual types conformance - if (F && !F->isDeclaration() - && !F->isVarArg() - && F->arg_size() == fInfo.getNumArgs()) { + if (F && !F->isDeclaration() && + fInfo.isCompatibleSignature(F->getFunctionType())) return F; - } + return nullptr; } @@ -966,11 +970,9 @@ M->getValueSymbolTable().lookup(FuncName)); // check formal with actual types conformance - if (F && !F->isDeclaration() - && !F->isVarArg() - && F->arg_size() == fInfo.getNumArgs()) { + if (F && !F->isDeclaration() && + fInfo.isCompatibleSignature(F->getFunctionType())) return F; - } FunctionType *FuncTy = fInfo.getFunctionType(*M);