Index: docs/LangRef.rst =================================================================== --- docs/LangRef.rst +++ docs/LangRef.rst @@ -698,7 +698,7 @@ :ref:`parameter attribute ` for the return type, a function name, a (possibly empty) argument list (each with optional :ref:`parameter attributes `), optional :ref:`function attributes `, -an optional section, an optional alignment, +an optional address space, an optional section, an optional alignment, an optional :ref:`comdat `, an optional :ref:`garbage collector name `, an optional :ref:`prefix `, an optional :ref:`prologue `, @@ -710,8 +710,8 @@ optional :ref:`linkage type `, an optional :ref:`visibility style `, an optional :ref:`DLL storage class `, an optional :ref:`calling convention `, an optional ``unnamed_addr`` -or ``local_unnamed_addr`` attribute, a return type, an optional :ref:`parameter -attribute ` for the return type, a function name, a possibly +or ``local_unnamed_addr`` attribute, an optional address space, a return type, +an optional :ref:`parameter attribute ` for the return type, a function name, a possibly empty list of arguments, an optional alignment, an optional :ref:`garbage collector name `, an optional :ref:`prefix `, and an optional :ref:`prologue `. @@ -748,13 +748,15 @@ If the ``local_unnamed_addr`` attribute is given, the address is known to not be significant within the module. +If an explicit address space is not given, it will default to zero. + Syntax:: define [linkage] [visibility] [DLLStorageClass] [cconv] [ret attrs] @ ([argument list]) - [(unnamed_addr|local_unnamed_addr)] [fn Attrs] [section "name"] - [comdat [($name)]] [align N] [gc] [prefix Constant] + [(unnamed_addr|local_unnamed_addr)] [AddrSpace] [fn Attrs] + [section "name"] [comdat [($name)]] [align N] [gc] [prefix Constant] [prologue Constant] [personality Constant] (!name !N)* { ... } The argument list is a comma separated sequence of arguments where each Index: include/llvm/IR/Function.h =================================================================== --- include/llvm/IR/Function.h +++ include/llvm/IR/Function.h @@ -117,7 +117,7 @@ /// function is automatically inserted into the end of the function list for /// the module. /// - Function(FunctionType *Ty, LinkageTypes Linkage, + Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N = "", Module *M = nullptr); public: @@ -126,10 +126,21 @@ ~Function(); static Function *Create(FunctionType *Ty, LinkageTypes Linkage, - const Twine &N = "", Module *M = nullptr) { - return new Function(Ty, Linkage, N, M); + unsigned AddrSpace, const Twine &N = "", + Module *M = nullptr) { + return new Function(Ty, Linkage, AddrSpace, N, M); } + /// Creates a new function and attaches it to a module. + /// + /// Places the function in the program address space as specified + /// by the module's data layout. + static Function *Create(FunctionType *Ty, LinkageTypes Linkage, + const Twine &N, Module &M); + + static Function *CreateBefore(Function &InsertBefore, FunctionType *Ty, + LinkageTypes Linkage, const Twine &N = ""); + // Provide fast operand accessors. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); Index: lib/AsmParser/LLParser.cpp =================================================================== --- lib/AsmParser/LLParser.cpp +++ lib/AsmParser/LLParser.cpp @@ -1162,7 +1162,8 @@ static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy, const std::string &Name) { if (auto *FT = dyn_cast(PTy->getElementType())) - return Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); + return Function::Create(FT, GlobalValue::ExternalWeakLinkage, + PTy->getAddressSpace(), Name, M); else return new GlobalVariable(*M, PTy->getElementType(), false, GlobalValue::ExternalWeakLinkage, nullptr, Name, @@ -4765,6 +4766,7 @@ std::string GC; GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; LocTy UnnamedAddrLoc; + unsigned AddrSpace = 0; Constant *Prefix = nullptr; Constant *Prologue = nullptr; Constant *PersonalityFn = nullptr; @@ -4772,6 +4774,7 @@ if (ParseArgumentList(ArgList, isVarArg) || ParseOptionalUnnamedAddr(UnnamedAddr) || + ParseOptionalAddrSpace(AddrSpace) || ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false, BuiltinLoc) || (EatIfPresent(lltok::kw_section) && @@ -4855,7 +4858,8 @@ } if (!Fn) - Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M); + Fn = Function::Create(FT, GlobalValue::ExternalLinkage, AddrSpace, + FunctionName, M); else // Move the forward-reference to the correct spot in the module. M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn); Index: lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- lib/Bitcode/Reader/BitcodeReader.cpp +++ lib/Bitcode/Reader/BitcodeReader.cpp @@ -2890,7 +2890,7 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef Record) { // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, - // prefixdata] (name in VST) + // prefixdata, personalityfn, addrspace] (name in VST) // v2: [strtab_offset, strtab_size, v1] StringRef Name; std::tie(Name, Record) = readNameFromStrtab(Record); @@ -2909,8 +2909,13 @@ if (CC & ~CallingConv::MaxID) return error("Invalid calling convention ID"); + unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); + if (Record.size() > 15) + AddrSpace = Record[15]; + Function *Func = - Function::Create(FTy, GlobalValue::ExternalLinkage, Name, TheModule); + Function::Create(FTy, GlobalValue::ExternalLinkage, AddrSpace, Name, + TheModule); Func->setCallingConv(CC); bool isProto = Record[2]; Index: lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- lib/Bitcode/Writer/BitcodeWriter.cpp +++ lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1193,7 +1193,7 @@ // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto, // linkage, paramattrs, alignment, section, visibility, gc, // unnamed_addr, prologuedata, dllstorageclass, comdat, - // prefixdata, personalityfn] + // prefixdata, personalityfn, addrspace] Vals.push_back(addToStrtab(F.getName())); Vals.push_back(F.getName().size()); Vals.push_back(VE.getTypeID(F.getFunctionType())); @@ -1214,6 +1214,7 @@ : 0); Vals.push_back( F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0); + Vals.push_back(F.getAddressSpace()); unsigned AbbrevToUse = 0; Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); Index: lib/IR/AsmWriter.cpp =================================================================== --- lib/IR/AsmWriter.cpp +++ lib/IR/AsmWriter.cpp @@ -2747,6 +2747,8 @@ StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr()); if (!UA.empty()) Out << ' ' << UA; + if (F->getAddressSpace() != 0) + Out << " addrspace(" << F->getAddressSpace() << ")"; if (Attrs.hasAttributes(AttributeList::FunctionIndex)) Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes()); if (F->hasSection()) { Index: lib/IR/AutoUpgrade.cpp =================================================================== --- lib/IR/AutoUpgrade.cpp +++ lib/IR/AutoUpgrade.cpp @@ -364,7 +364,7 @@ // the end of the name. Change name from llvm.arm.neon.vclz.* to // llvm.ctlz.* FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); - NewFn = Function::Create(fType, F->getLinkage(), + NewFn = Function::Create(fType, F->getLinkage(), F->getAddressSpace(), "llvm.ctlz." + Name.substr(14), F->getParent()); return true; } @@ -380,7 +380,7 @@ // Can't use Intrinsic::getDeclaration here as the return types might // then only be structurally equal. FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false); - NewFn = Function::Create(fType, F->getLinkage(), + NewFn = Function::Create(fType, F->getLinkage(), F->getAddressSpace(), "llvm." + Name + ".p0i8", F->getParent()); return true; } Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -1781,8 +1781,9 @@ LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy) { + auto &Mod = *unwrap(M); return wrap(Function::Create(unwrap(FunctionTy), - GlobalValue::ExternalLinkage, Name, unwrap(M))); + GlobalValue::ExternalLinkage, Name, Mod)); } LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { Index: lib/IR/Function.cpp =================================================================== --- lib/IR/Function.cpp +++ lib/IR/Function.cpp @@ -195,6 +195,22 @@ return getType()->getContext(); } +Function *Function::Create(FunctionType *Ty, LinkageTypes Linkage, + const Twine &N, Module &M) { + return Create(Ty, Linkage, M.getDataLayout().getProgramAddressSpace(), + N, &M); +} + +Function *Function::CreateBefore(Function &InsertBefore, FunctionType *Ty, + LinkageTypes Linkage, const Twine &N) { + auto &M = *InsertBefore.getParent(); + unsigned AddrSpace = M.getDataLayout().getProgramAddressSpace(); + Function *F = Create(Ty, Linkage, AddrSpace, N); + + M.getFunctionList().insert(InsertBefore.getIterator(), F); + return F; +} + void Function::removeFromParent() { getParent()->getFunctionList().remove(getIterator()); } @@ -207,10 +223,11 @@ // Function Implementation //===----------------------------------------------------------------------===// -Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, - Module *ParentModule) +Function::Function(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, + const Twine &name, Module *ParentModule) : GlobalObject(Ty, Value::FunctionVal, - OperandTraits::op_begin(this), 0, Linkage, name), + OperandTraits::op_begin(this), 0, Linkage, name, + AddrSpace), NumArgs(Ty->getNumParams()) { assert(FunctionType::isValidReturnType(getReturnType()) && "invalid return type"); Index: lib/IR/Module.cpp =================================================================== --- lib/IR/Module.cpp +++ lib/IR/Module.cpp @@ -145,7 +145,8 @@ GlobalValue *F = getNamedValue(Name); if (!F) { // Nope, add it - Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); + Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, + DL.getProgramAddressSpace(), Name); if (!New->isIntrinsic()) // Intrinsics get attrs set on construction New->setAttributes(AttributeList); FunctionList.push_back(New); @@ -154,8 +155,9 @@ // If the function exists but has the wrong type, return a bitcast to the // right type. - if (F->getType() != PointerType::getUnqual(Ty)) - return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); + auto *PTy = PointerType::get(Ty, F->getAddressSpace()); + if (F->getType() != PTy) + return ConstantExpr::getBitCast(F, PTy); // Otherwise, we just found the existing function or a prototype. return F; Index: lib/Linker/IRMover.cpp =================================================================== --- lib/Linker/IRMover.cpp +++ lib/Linker/IRMover.cpp @@ -613,7 +613,7 @@ // bring SF over. auto *F = Function::Create(TypeMap.get(SF->getFunctionType()), - GlobalValue::ExternalLinkage, SF->getName(), &DstM); + GlobalValue::ExternalLinkage, SF->getName(), DstM); F->copyAttributesFrom(SF); return F; } @@ -643,7 +643,7 @@ else if (SGV->getValueType()->isFunctionTy()) NewGV = Function::Create(cast(TypeMap.get(SGV->getValueType())), - GlobalValue::ExternalLinkage, SGV->getName(), &DstM); + GlobalValue::ExternalLinkage, SGV->getName(), DstM); else NewGV = new GlobalVariable( DstM, TypeMap.get(SGV->getValueType()), Index: lib/Linker/LinkModules.cpp =================================================================== --- lib/Linker/LinkModules.cpp +++ lib/Linker/LinkModules.cpp @@ -441,7 +441,7 @@ PointerType &Ty = *cast(Alias.getType()); GlobalValue *Declaration; if (auto *FTy = dyn_cast(Alias.getValueType())) { - Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, "", &M); + Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, "", M); } else { Declaration = new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, Index: lib/Target/AMDGPU/AMDGPUOpenCLImageTypeLoweringPass.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUOpenCLImageTypeLoweringPass.cpp +++ lib/Target/AMDGPU/AMDGPUOpenCLImageTypeLoweringPass.cpp @@ -304,7 +304,8 @@ // Create function with new signature and clone the old body into it. auto NewFT = FunctionType::get(FT->getReturnType(), ArgTypes, false); - auto NewF = Function::Create(NewFT, F->getLinkage(), F->getName()); + auto NewF = Function::Create(NewFT, F->getLinkage(), F->getAddressSpace(), + F->getName()); ValueToValueMapTy VMap; auto NewFArgIt = NewF->arg_begin(); for (auto &Arg: F->args()) { Index: lib/Target/AMDGPU/AMDGPURewriteOutArguments.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPURewriteOutArguments.cpp +++ lib/Target/AMDGPU/AMDGPURewriteOutArguments.cpp @@ -360,9 +360,9 @@ DEBUG(dbgs() << "Computed new return type: " << *NewRetTy << '\n'); - Function *NewFunc = Function::Create(NewFuncTy, Function::PrivateLinkage, - F.getName() + ".body"); - F.getParent()->getFunctionList().insert(F.getIterator(), NewFunc); + Function *NewFunc = Function::CreateBefore(F, NewFuncTy, + Function::PrivateLinkage, + F.getName() + ".body"); NewFunc->copyAttributesFrom(&F); NewFunc->setComdat(F.getComdat()); Index: lib/Target/Mips/Mips16HardFloat.cpp =================================================================== --- lib/Target/Mips/Mips16HardFloat.cpp +++ lib/Target/Mips/Mips16HardFloat.cpp @@ -268,7 +268,7 @@ Function *FStub = M->getFunction(StubName); if (FStub && !FStub->isDeclaration()) return; FStub = Function::Create(F.getFunctionType(), - Function::InternalLinkage, StubName, M); + Function::InternalLinkage, StubName, *M); FStub->addFnAttr("mips16_fp_stub"); FStub->addFnAttr(Attribute::Naked); FStub->addFnAttr(Attribute::NoInline); @@ -453,7 +453,7 @@ std::string LocalName = "$$__fn_local_" + Name; Function *FStub = Function::Create (F->getFunctionType(), - Function::InternalLinkage, StubName, M); + Function::InternalLinkage, StubName, *M); FStub->addFnAttr("mips16_fp_stub"); FStub->addFnAttr(Attribute::Naked); FStub->addFnAttr(Attribute::NoUnwind); Index: lib/Target/X86/X86WinEHState.cpp =================================================================== --- lib/Target/X86/X86WinEHState.cpp +++ lib/Target/X86/X86WinEHState.cpp @@ -400,7 +400,7 @@ Function::Create(TrampolineTy, GlobalValue::InternalLinkage, Twine("__ehhandler$") + GlobalValue::dropLLVMManglingEscape( ParentFunc->getName()), - TheModule); + *TheModule); BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline); IRBuilder<> Builder(EntryBB); Value *LSDA = emitEHLSDA(Builder, ParentFunc); Index: lib/Transforms/IPO/ArgumentPromotion.cpp =================================================================== --- lib/Transforms/IPO/ArgumentPromotion.cpp +++ lib/Transforms/IPO/ArgumentPromotion.cpp @@ -186,7 +186,8 @@ FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg()); // Create the new function body and insert it into the module. - Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName()); + Function *NF = Function::CreateBefore(*F, NFTy, F->getLinkage(), + F->getName()); NF->copyAttributesFrom(F); // Patch the pointer to LLVM function in debug info descriptor. @@ -202,7 +203,6 @@ PAL.getRetAttributes(), ArgAttrVec)); ArgAttrVec.clear(); - F->getParent()->getFunctionList().insert(F->getIterator(), NF); NF->takeName(F); // Loop over all of the callers of the function, transforming the call sites Index: lib/Transforms/IPO/DeadArgumentElimination.cpp =================================================================== --- lib/Transforms/IPO/DeadArgumentElimination.cpp +++ lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -146,10 +146,9 @@ unsigned NumArgs = Params.size(); // Create the new function body and insert it into the module... - Function *NF = Function::Create(NFTy, Fn.getLinkage()); + Function *NF = Function::CreateBefore(Fn, NFTy, Fn.getLinkage()); NF->copyAttributesFrom(&Fn); NF->setComdat(Fn.getComdat()); - Fn.getParent()->getFunctionList().insert(Fn.getIterator(), NF); NF->takeName(&Fn); // Loop over all of the callers of the function, transforming the call sites @@ -796,14 +795,13 @@ if (NFTy == FTy) return false; - // Create the new function body and insert it into the module... - Function *NF = Function::Create(NFTy, F->getLinkage()); + // Create the new function body. + // Insert the new function before the old function, so we won't be processing + // it again. + Function *NF = Function::CreateBefore(*F, NFTy, F->getLinkage()); NF->copyAttributesFrom(F); NF->setComdat(F->getComdat()); NF->setAttributes(NewPAL); - // Insert the new function before the old function, so we won't be processing - // it again. - F->getParent()->getFunctionList().insert(F->getIterator(), NF); NF->takeName(F); // Loop over all of the callers of the function, transforming the call sites Index: lib/Transforms/IPO/ExtractGV.cpp =================================================================== --- lib/Transforms/IPO/ExtractGV.cpp +++ lib/Transforms/IPO/ExtractGV.cpp @@ -137,7 +137,7 @@ llvm::Value *Declaration; if (FunctionType *FTy = dyn_cast(Ty)) { Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, - CurI->getName(), &M); + CurI->getName(), M); } else { Declaration = Index: lib/Transforms/IPO/LowerTypeTests.cpp =================================================================== --- lib/Transforms/IPO/LowerTypeTests.cpp +++ lib/Transforms/IPO/LowerTypeTests.cpp @@ -857,14 +857,14 @@ if (F->isDeclarationForLinker() && !isDefinition) { // Declaration of an external function. FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage, - Name + ".cfi_jt", &M); + Name + ".cfi_jt", M); FDecl->setVisibility(GlobalValue::HiddenVisibility); } else if (isDefinition) { F->setName(Name + ".cfi"); F->setLinkage(GlobalValue::ExternalLinkage); F->setVisibility(GlobalValue::HiddenVisibility); FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage, - Name, &M); + Name, M); FDecl->setVisibility(Visibility); } else { // Function definition without type metadata, where some other translation @@ -1024,7 +1024,7 @@ WeakInitializerFn = Function::Create( FunctionType::get(Type::getVoidTy(M.getContext()), /* IsVarArg */ false), - GlobalValue::InternalLinkage, "__cfi_global_var_init", &M); + GlobalValue::InternalLinkage, "__cfi_global_var_init", M); BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn); ReturnInst::Create(M.getContext(), BB); @@ -1067,7 +1067,7 @@ // placeholder first. Function *PlaceholderFn = Function::Create(cast(F->getValueType()), - GlobalValue::ExternalWeakLinkage, "", &M); + GlobalValue::ExternalWeakLinkage, "", M); F->replaceAllUsesWith(PlaceholderFn); Constant *Target = ConstantExpr::getSelect( @@ -1215,7 +1215,7 @@ Function *JumpTableFn = Function::Create(FunctionType::get(Type::getVoidTy(M.getContext()), /* IsVarArg */ false), - GlobalValue::PrivateLinkage, ".cfi.jumptable", &M); + GlobalValue::PrivateLinkage, ".cfi.jumptable", M); ArrayType *JumpTableType = ArrayType::get(getJumpTableEntryType(), Functions.size()); auto JumpTable = @@ -1514,7 +1514,7 @@ if (!F) F = Function::Create( FunctionType::get(Type::getVoidTy(M.getContext()), false), - GlobalVariable::ExternalLinkage, FunctionName, &M); + GlobalVariable::ExternalLinkage, FunctionName, M); // If the function is available_externally, remove its definition so // that it is handled the same way as a declaration. Later we will try Index: lib/Transforms/IPO/MergeFunctions.cpp =================================================================== --- lib/Transforms/IPO/MergeFunctions.cpp +++ lib/Transforms/IPO/MergeFunctions.cpp @@ -658,7 +658,7 @@ BB = GEntryBlock; } else { NewG = Function::Create(G->getFunctionType(), G->getLinkage(), "", - G->getParent()); + *G->getParent()); BB = BasicBlock::Create(F->getContext(), "", NewG); } @@ -717,7 +717,7 @@ // Make them both thunks to the same internal function. Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "", - F->getParent()); + *F->getParent()); H->copyAttributesFrom(F); H->takeName(F); removeUsers(F); Index: lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp =================================================================== --- lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp +++ lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp @@ -147,7 +147,7 @@ continue; Function *NewF = - Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M); + Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", M); NewF->setVisibility(F.getVisibility()); NewF->takeName(&F); F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType())); @@ -174,7 +174,7 @@ GlobalObject *GO; if (GA->getValueType()->isFunctionTy()) GO = Function::Create(cast(GA->getValueType()), - GlobalValue::ExternalLinkage, "", M); + GlobalValue::ExternalLinkage, "", *M); else GO = new GlobalVariable( *M, GA->getValueType(), false, GlobalValue::ExternalLinkage, Index: lib/Transforms/Instrumentation/AddressSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1711,7 +1711,7 @@ IRBuilder<> AddressSanitizerModule::CreateAsanModuleDtor(Module &M) { AsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*C), false), - GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); + GlobalValue::InternalLinkage, kAsanModuleDtorName, M); BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); return IRBuilder<>(ReturnInst::Create(*C, AsanDtorBB)); Index: lib/Transforms/Instrumentation/DataFlowSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/DataFlowSanitizer.cpp +++ lib/Transforms/Instrumentation/DataFlowSanitizer.cpp @@ -537,8 +537,8 @@ GlobalValue::LinkageTypes NewFLink, FunctionType *NewFT) { FunctionType *FT = F->getFunctionType(); - Function *NewF = Function::Create(NewFT, NewFLink, NewFName, - F->getParent()); + Function *NewF = Function::Create(NewFT, NewFLink, F->getAddressSpace(), + NewFName, F->getParent()); NewF->copyAttributesFrom(F); NewF->removeAttributes( AttributeList::ReturnIndex, @@ -711,7 +711,8 @@ // easily identify cases of mismatching ABIs. if (getInstrumentedABI() == IA_Args && !IsZeroArgsVoidRet) { FunctionType *NewFT = getArgsFunctionType(FT); - Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M); + Function *NewF = Function::Create(NewFT, F.getLinkage(), + F.getAddressSpace(), "", &M); NewF->copyAttributesFrom(&F); NewF->removeAttributes( AttributeList::ReturnIndex, Index: lib/Transforms/Instrumentation/EfficiencySanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/EfficiencySanitizer.cpp +++ lib/Transforms/Instrumentation/EfficiencySanitizer.cpp @@ -523,7 +523,7 @@ EsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*Ctx), false), GlobalValue::InternalLinkage, - EsanModuleDtorName, &M); + EsanModuleDtorName, M); ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction)); IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator()); Function *EsanExit = checkSanitizerInterfaceFunction( Index: lib/Transforms/Instrumentation/GCOVProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/GCOVProfiling.cpp +++ lib/Transforms/Instrumentation/GCOVProfiling.cpp @@ -691,7 +691,7 @@ // when "__gcov_flush" is called. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); Function *F = Function::Create(FTy, GlobalValue::InternalLinkage, - "__llvm_gcov_init", M); + "__llvm_gcov_init", *M); F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); F->setLinkage(GlobalValue::InternalLinkage); F->addFnAttr(Attribute::NoInline); @@ -842,7 +842,7 @@ Function *WriteoutF = M->getFunction("__llvm_gcov_writeout"); if (!WriteoutF) WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage, - "__llvm_gcov_writeout", M); + "__llvm_gcov_writeout", *M); WriteoutF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); WriteoutF->addFnAttr(Attribute::NoInline); if (Options.NoRedZone) @@ -957,7 +957,7 @@ Function *FlushF = M->getFunction("__llvm_gcov_flush"); if (!FlushF) FlushF = Function::Create(FTy, GlobalValue::InternalLinkage, - "__llvm_gcov_flush", M); + "__llvm_gcov_flush", *M); else FlushF->setLinkage(GlobalValue::InternalLinkage); FlushF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); Index: lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/InstrProfiling.cpp +++ lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -861,7 +861,7 @@ auto *Int64Ty = Type::getInt64Ty(M->getContext()); auto *RegisterFTy = FunctionType::get(VoidTy, false); auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, - getInstrProfRegFuncsName(), M); + getInstrProfRegFuncsName(), *M); RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); if (Options.NoRedZone) RegisterF->addFnAttr(Attribute::NoRedZone); @@ -869,7 +869,7 @@ auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); auto *RuntimeRegisterF = Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, - getInstrProfRegFuncName(), M); + getInstrProfRegFuncName(), *M); IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); for (Value *Data : UsedVars) @@ -882,7 +882,7 @@ FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false); auto *NamesRegisterF = Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage, - getInstrProfNamesRegFuncName(), M); + getInstrProfNamesRegFuncName(), *M); IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy), IRB.getInt64(NamesSize)}); } @@ -909,7 +909,7 @@ // Make a function that uses it. auto *User = Function::Create(FunctionType::get(Int32Ty, false), GlobalValue::LinkOnceODRLinkage, - getInstrProfRuntimeHookVarUseFuncName(), M); + getInstrProfRuntimeHookVarUseFuncName(), *M); User->addFnAttr(Attribute::NoInline); if (Options.NoRedZone) User->addFnAttr(Attribute::NoRedZone); @@ -955,7 +955,7 @@ auto *VoidTy = Type::getVoidTy(M->getContext()); auto *F = Function::Create(FunctionType::get(VoidTy, false), GlobalValue::InternalLinkage, - getInstrProfInitFuncName(), M); + getInstrProfInitFuncName(), *M); F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); F->addFnAttr(Attribute::NoInline); if (Options.NoRedZone) Index: lib/Transforms/Utils/CloneFunction.cpp =================================================================== --- lib/Transforms/Utils/CloneFunction.cpp +++ lib/Transforms/Utils/CloneFunction.cpp @@ -244,7 +244,8 @@ // Create the new function... Function *NewF = - Function::Create(FTy, F->getLinkage(), F->getName(), F->getParent()); + Function::Create(FTy, F->getLinkage(), F->getAddressSpace(), + F->getName(), F->getParent()); // Loop over the arguments, copying the names of the mapped arguments over... Function::arg_iterator DestI = NewF->arg_begin(); Index: lib/Transforms/Utils/CloneModule.cpp =================================================================== --- lib/Transforms/Utils/CloneModule.cpp +++ lib/Transforms/Utils/CloneModule.cpp @@ -75,7 +75,8 @@ // Loop over the functions in the module, making external functions as before for (const Function &I : *M) { Function *NF = Function::Create(cast(I.getValueType()), - I.getLinkage(), I.getName(), New.get()); + I.getLinkage(), I.getAddressSpace(), + I.getName(), New.get()); NF->copyAttributesFrom(&I); VMap[&I] = NF; } @@ -91,7 +92,8 @@ GlobalValue *GV; if (I->getValueType()->isFunctionTy()) GV = Function::Create(cast(I->getValueType()), - GlobalValue::ExternalLinkage, I->getName(), + GlobalValue::ExternalLinkage, + I->getAddressSpace(), I->getName(), New.get()); else GV = new GlobalVariable( Index: lib/Transforms/Utils/CodeExtractor.cpp =================================================================== --- lib/Transforms/Utils/CodeExtractor.cpp +++ lib/Transforms/Utils/CodeExtractor.cpp @@ -580,6 +580,7 @@ // Create the new function Function *newFunction = Function::Create(funcType, GlobalValue::InternalLinkage, + oldFunction->getAddressSpace(), oldFunction->getName() + "_" + header->getName(), M); // If the old function is no-throw, so is the new one. Index: lib/Transforms/Utils/ModuleUtils.cpp =================================================================== --- lib/Transforms/Utils/ModuleUtils.cpp +++ lib/Transforms/Utils/ModuleUtils.cpp @@ -160,7 +160,8 @@ declareSanitizerInitFunction(M, InitName, InitArgTypes); Function *Ctor = Function::Create( FunctionType::get(Type::getVoidTy(M.getContext()), false), - GlobalValue::InternalLinkage, CtorName, &M); + GlobalValue::InternalLinkage, + CtorName, M); BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor); IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB)); IRB.CreateCall(InitFunction, InitArgs); Index: lib/Transforms/Utils/SanitizerStats.cpp =================================================================== --- lib/Transforms/Utils/SanitizerStats.cpp +++ lib/Transforms/Utils/SanitizerStats.cpp @@ -93,7 +93,7 @@ // Create a global constructor to register NewModuleStatsGV. auto F = Function::Create(FunctionType::get(VoidTy, false), - GlobalValue::InternalLinkage, "", M); + GlobalValue::InternalLinkage, "", *M); auto BB = BasicBlock::Create(M->getContext(), "", F); IRBuilder<> B(BB); Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -4989,7 +4989,7 @@ // Generate a declaration FunctionType *FTy = FunctionType::get(RetTy, Tys, false); VectorF = - Function::Create(FTy, Function::ExternalLinkage, VFnName, M); + Function::Create(FTy, Function::ExternalLinkage, VFnName, *M); VectorF->copyAttributesFrom(F); } } Index: test/Bitcode/function-nonzero-address-spaces.ll =================================================================== --- /dev/null +++ test/Bitcode/function-nonzero-address-spaces.ll @@ -0,0 +1,7 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s + +; CHECK: define void @main() addrspace(3) +define void @main() addrspace(3) { + ret void +} +