diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h @@ -173,14 +173,11 @@ const char *Modifier = nullptr); void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O, bool processDemoted, const NVPTXSubtarget &STI); - void printParamName(Function::const_arg_iterator I, int paramIndex, - raw_ostream &O); void emitGlobals(const Module &M); void emitHeader(Module &M, raw_ostream &O, const NVPTXSubtarget &STI); void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const; void emitVirtualRegister(unsigned int vr, raw_ostream &); void emitFunctionParamList(const Function *, raw_ostream &O); - void emitFunctionParamList(const MachineFunction &MF, raw_ostream &O); void setAndEmitFunctionVirtualRegisters(const MachineFunction &MF); void printReturnValStr(const Function *, raw_ostream &O); void printReturnValStr(const MachineFunction &MF, raw_ostream &O); diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -466,7 +466,7 @@ CurrentFnSym->print(O, MAI); - emitFunctionParamList(*MF, O); + emitFunctionParamList(F, O); if (isKernelFunction(*F)) emitKernelFunctionDirectives(*F, O); @@ -1441,12 +1441,6 @@ } } -void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, - int paramIndex, raw_ostream &O) { - getSymbol(I->getParent())->print(O, MAI); - O << "_param_" << paramIndex; -} - void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) { const DataLayout &DL = getDataLayout(); const AttributeList &PAL = F->getAttributes(); @@ -1485,24 +1479,21 @@ O << "\t.param .u64 .ptr .surfref "; else O << "\t.param .surfref "; - CurrentFnSym->print(O, MAI); - O << "_param_" << paramIndex; + O << TLI->getParamName(F, paramIndex); } else { // Default image is read_only if (hasImageHandles) O << "\t.param .u64 .ptr .texref "; else O << "\t.param .texref "; - CurrentFnSym->print(O, MAI); - O << "_param_" << paramIndex; + O << TLI->getParamName(F, paramIndex); } } else { if (hasImageHandles) O << "\t.param .u64 .ptr .samplerref "; else O << "\t.param .samplerref "; - CurrentFnSym->print(O, MAI); - O << "_param_" << paramIndex; + O << TLI->getParamName(F, paramIndex); } continue; } @@ -1524,7 +1515,7 @@ Align OptimalAlign = getOptimalAlignForParam(Ty); O << "\t.param .align " << OptimalAlign.value() << " .b8 "; - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); O << "[" << DL.getTypeAllocSize(Ty) << "]"; continue; @@ -1563,7 +1554,7 @@ Align ParamAlign = I->getParamAlign().valueOrOne(); O << ".align " << ParamAlign.value() << " "; } - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); continue; } @@ -1575,7 +1566,7 @@ else O << getPTXFundamentalTypeStr(Ty); O << " "; - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); continue; } // Non-kernel function, just print .param .b for ABI @@ -1598,7 +1589,7 @@ O << "\t.param .b" << sz << " "; else O << "\t.reg .b" << sz << " "; - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); continue; } @@ -1619,7 +1610,7 @@ unsigned sz = DL.getTypeAllocSize(ETy); O << "\t.param .align " << OptimalAlign.value() << " .b8 "; - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); O << "[" << sz << "]"; continue; } else { @@ -1642,7 +1633,7 @@ if (elemtype.isInteger()) sz = promoteScalarArgumentSize(sz); O << "\t.reg .b" << sz << " "; - printParamName(I, paramIndex, O); + O << TLI->getParamName(F, paramIndex); if (j < je - 1) O << ",\n"; ++paramIndex; @@ -1660,19 +1651,12 @@ O << ",\n"; O << "\t.param .align " << STI.getMaxRequiredAlignment(); O << " .b8 "; - getSymbol(F)->print(O, MAI); - O << "_vararg[]"; + O << TLI->getParamName(F, /* vararg */ -1) << "[]"; } O << "\n)\n"; } -void NVPTXAsmPrinter::emitFunctionParamList(const MachineFunction &MF, - raw_ostream &O) { - const Function &F = MF.getFunction(); - emitFunctionParamList(&F, O); -} - void NVPTXAsmPrinter::setAndEmitFunctionVirtualRegisters( const MachineFunction &MF) { SmallString<128> Str; diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h --- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h +++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h @@ -466,6 +466,11 @@ Align InitialAlign, const DataLayout &DL) const; + // Helper for getting a function parameter name. Name is composed from + // its index and the function name. Negative index corresponds to special + // parameter (unsized array) used for passing variable arguments. + std::string getParamName(const Function *F, int Idx) const; + /// isLegalAddressingMode - Return true if the addressing mode represented /// by AM is legal for this target, for a load/store of the specified type /// Used to guide target specific optimizations, like loop strength diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp --- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp @@ -2614,18 +2614,8 @@ // passing variable arguments. SDValue NVPTXTargetLowering::getParamSymbol(SelectionDAG &DAG, int idx, EVT v) const { - std::string ParamSym; - raw_string_ostream ParamStr(ParamSym); - - ParamStr << DAG.getMachineFunction().getName(); - - if (idx < 0) - ParamStr << "_vararg"; - else - ParamStr << "_param_" << idx; - - StringRef SavedStr = - nvTM->getStrPool().save(ParamSym); + StringRef SavedStr = nvTM->getStrPool().save( + getParamName(&DAG.getMachineFunction().getFunction(), idx)); return DAG.getTargetExternalSymbol(SavedStr.data(), v); } @@ -4522,6 +4512,23 @@ return ArgAlign; } +// Helper for getting a function parameter name. Name is composed from +// its index and the function name. Negative index corresponds to special +// parameter (unsized array) used for passing variable arguments. +std::string NVPTXTargetLowering::getParamName(const Function *F, + int Idx) const { + std::string ParamName; + raw_string_ostream ParamStr(ParamName); + + ParamStr << getTargetMachine().getSymbol(F)->getName(); + if (Idx < 0) + ParamStr << "_vararg"; + else + ParamStr << "_param_" << Idx; + + return ParamName; +} + /// isLegalAddressingMode - Return true if the addressing mode represented /// by AM is legal for this target, for a load/store of the specified type. /// Used to guide target specific optimizations, like loop strength reduction diff --git a/llvm/test/CodeGen/NVPTX/anonymous-fn-param.ll b/llvm/test/CodeGen/NVPTX/anonymous-fn-param.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/NVPTX/anonymous-fn-param.ll @@ -0,0 +1,25 @@ +; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s +; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %} + +; Check that parameter names we generate in the function signature and the name +; we use when we refer to the parameter in the function body do match. + +; CHECK: .func (.param .b32 func_retval0) __unnamed_1( +; CHECK-NEXT: .param .b32 __unnamed_1_param_0 +; CHECK: ld.param.u32 {{%r[0-9]+}}, [__unnamed_1_param_0]; + +; CHECK: .func (.param .b32 func_retval0) __unnamed_2( +; CHECK-NEXT: .param .b32 __unnamed_2_param_0 +; CHECK: ld.param.u32 {{%r[0-9]+}}, [__unnamed_2_param_0]; + +define internal i32 @0(i32 %a) { +entry: + %r = add i32 %a, 1 + ret i32 %r +} + +define internal i32 @1(i32 %a) { +entry: + %r = add i32 %a, 1 + ret i32 %r +}