diff --git a/compiler/codegen/cg-CForLoop.cpp b/compiler/codegen/cg-CForLoop.cpp index 2cfea6e80fc8..ca5cc7741ddb 100644 --- a/compiler/codegen/cg-CForLoop.cpp +++ b/compiler/codegen/cg-CForLoop.cpp @@ -206,7 +206,7 @@ static llvm::MDNode* generateLoopMetadata(LoopStmt* loop, std::vector args; // Resolve operand 0 for the loop id self reference - auto tmpNode = llvm::MDNode::getTemporary(ctx, chpl::empty); + auto tmpNode = llvm::MDNode::getTemporary(ctx, {}); args.push_back(tmpNode.get()); if(fNoVectorize == false && loop->isVectorizable()) { diff --git a/compiler/codegen/cg-expr.cpp b/compiler/codegen/cg-expr.cpp index 033651e24490..668cbd788839 100644 --- a/compiler/codegen/cg-expr.cpp +++ b/compiler/codegen/cg-expr.cpp @@ -530,11 +530,11 @@ GenRet codegenWideAddr(GenRet locale, GenRet raddr, Type* wideType = NULL) llvm::Type* locAddrType = nullptr; if (isOpaquePointer(addrType)) { - locAddrType = llvm::PointerType::getUnqual(gContext->llvmContext()); + locAddrType = getPointerType(gContext->llvmContext()); } else { #ifdef HAVE_LLVM_TYPED_POINTERS locAddrType = - llvm::PointerType::getUnqual(addrType->getPointerElementType()); + getPointerType(addrType->getPointerElementType()); #endif } INT_ASSERT(locAddrType); @@ -1304,7 +1304,7 @@ GenRet doCodegenFieldPtr( baseTy, baseValue, cBaseType->getMemberGEP("_u", unused)); trackLLVMValue(ret.val); auto addrSpace = baseValue->getType()->getPointerAddressSpace(); - llvm::PointerType* ty = llvm::PointerType::get(retType.type, addrSpace); + auto ty = getPointerType(retType.type, addrSpace); // Now cast it to the right type. ret.val = convertValueToType(ret.val, ty, false); INT_ASSERT(ret.val); @@ -1342,8 +1342,7 @@ GenRet doCodegenFieldPtr( (ret.chplType == dtCFnPtr)) { // cast the returned pointer to the right type auto addrSpace = baseValue->getType()->getPointerAddressSpace(); - llvm::PointerType* ty = - llvm::PointerType::get(retType.type, addrSpace); + auto ty = getPointerType(retType.type, addrSpace); ret.val = convertValueToType(ret.val, ty, false); } @@ -2201,6 +2200,20 @@ GenRet codegenMod(GenRet a, GenRet b) return ret; } +#ifdef HAVE_LLVM +static llvm::CallInst* CreateIntrinsic(llvm::Intrinsic::ID id, + llvm::ArrayRef tys, + llvm::ArrayRef args) { +#if LLVM_VERSION_MAJOR >= 21 + auto call = gGenInfo->irBuilder->CreateIntrinsic(id, tys, args, {}); +#else + auto call = gGenInfo->irBuilder->CreateIntrinsic(id, tys, args); +#endif + trackLLVMValue(call); + return call; +} +#endif + // TODO: We could call the C 'fma' function from 'math.h' here. static GenRet emitFmaForC(GenRet av, GenRet bv, GenRet cv) { INT_FATAL("Should not reach here, user facing functions should call the " @@ -2212,7 +2225,6 @@ static GenRet emitFmaForC(GenRet av, GenRet bv, GenRet cv) { static GenRet emitFmaForLlvm(GenRet av, GenRet bv, GenRet cv) { GenRet ret; #ifdef HAVE_LLVM - GenInfo* info = gGenInfo; INT_ASSERT(av.chplType == bv.chplType && bv.chplType == cv.chplType); INT_ASSERT(av.chplType == dtReal[FLOAT_SIZE_64] || av.chplType == dtReal[FLOAT_SIZE_32]); @@ -2229,7 +2241,7 @@ static GenRet emitFmaForLlvm(GenRet av, GenRet bv, GenRet cv) { auto id = llvm::Intrinsic::fma; std::vector tys = { ty }; std::vector args = { av.val, bv.val, cv.val }; - ret.val = info->irBuilder->CreateIntrinsic(id, tys, args); + ret.val = CreateIntrinsic(id, tys, args); trackLLVMValue(ret.val); #endif @@ -2276,7 +2288,6 @@ static GenRet emitSqrtCMath(GenRet av) { static GenRet emitSqrtLLVMIntrinsic(GenRet av) { GenRet ret; #ifdef HAVE_LLVM - GenInfo* info = gGenInfo; INT_ASSERT(av.chplType == dtReal[FLOAT_SIZE_64] || av.chplType == dtReal[FLOAT_SIZE_32]); auto ty = av.val->getType(); @@ -2288,7 +2299,7 @@ static GenRet emitSqrtLLVMIntrinsic(GenRet av) { auto id = llvm::Intrinsic::sqrt; std::vector tys = { ty }; std::vector args = { av.val }; - ret.val = info->irBuilder->CreateIntrinsic(id, tys, args); + ret.val = CreateIntrinsic(id, tys, args); trackLLVMValue(ret.val); #endif @@ -2323,7 +2334,6 @@ static GenRet emitAbsCMath(GenRet av) { static GenRet emitAbsLLVMIntrinsic(GenRet av) { GenRet ret; #ifdef HAVE_LLVM - GenInfo* info = gGenInfo; INT_ASSERT(av.chplType == dtReal[FLOAT_SIZE_64] || av.chplType == dtReal[FLOAT_SIZE_32]); auto ty = av.val->getType(); @@ -2335,7 +2345,7 @@ static GenRet emitAbsLLVMIntrinsic(GenRet av) { auto id = llvm::Intrinsic::fabs; std::vector tys = { ty }; std::vector args = { av.val }; - ret.val = info->irBuilder->CreateIntrinsic(id, tys, args); + ret.val = CreateIntrinsic(id, tys, args); trackLLVMValue(ret.val); #endif @@ -2993,8 +3003,8 @@ static GenRet codegenCallExprInner(GenRet function, tmp = createTempVarWith(args[i]); llvm::Value* ptr = tmp.val; - llvm::Type* sTyPtrTy = llvm::PointerType::get(sTy, stackSpace); - llvm::Type* i8PtrTy = getPointerType(irBuilder); + auto sTyPtrTy = getPointerType(sTy, stackSpace); + auto i8PtrTy = getPointerType(irBuilder); // handle offset if (unsigned offset = argInfo->getDirectOffset()) { @@ -3040,7 +3050,7 @@ static GenRet codegenCallExprInner(GenRet function, // Create a temp variable to load from tmp = createTempVarWith(args[i]); - llvm::Type* sTyPtrTy = llvm::PointerType::get(sTy, stackSpace); + auto sTyPtrTy = getPointerType(sTy, stackSpace); llvm::Value* ptr = irBuilder->CreatePointerCast(tmp.val, sTyPtrTy); trackLLVMValue(ptr); @@ -3105,7 +3115,7 @@ static GenRet codegenCallExprInner(GenRet function, // If we are using typed pointers, the pointer type must match the // call type or else instruction verification will fail. If using // opaque pointers, it is fine if the call pointer type is 'void*'. - auto fnPtrType = llvm::PointerType::getUnqual(fnType); + auto fnPtrType = getPointerType(fnType); val = info->irBuilder->CreateBitCast(val, fnPtrType); #endif @@ -3466,7 +3476,7 @@ GenRet codegenNullPointer() ret.c = "NULL"; } else { #ifdef HAVE_LLVM - llvm::Type* ptrType = getPointerType(info->irBuilder); + auto ptrType = getPointerType(info->irBuilder); ret.val = llvm::Constant::getNullValue(ptrType); #endif } @@ -3503,8 +3513,8 @@ void codegenCallMemcpy(GenRet dest, GenRet src, GenRet size, llvm::Type *types[3]; unsigned addrSpaceDest = llvm::cast(dest.val->getType())->getAddressSpace(); unsigned addrSpaceSrc = llvm::cast(src.val->getType())->getAddressSpace(); - types[0] = llvm::PointerType::get(int8Ty, addrSpaceDest); - types[1] = llvm::PointerType::get(int8Ty, addrSpaceSrc); + types[0] = getPointerType(int8Ty, addrSpaceDest); + types[1] = getPointerType(int8Ty, addrSpaceSrc); types[2] = llvm::Type::getInt64Ty(gContext->llvmContext()); //types[3] = llvm::Type::getInt32Ty(info->llvmContext); //types[4] = llvm::Type::getInt1Ty(info->llvmContext); @@ -3796,7 +3806,7 @@ GenRet codegenCastToVoidStar(GenRet value) ret.c += "))"; } else { #ifdef HAVE_LLVM - llvm::Type* castType = getPointerType(info->irBuilder); + auto castType = getPointerType(info->irBuilder); ret.val = convertValueToType(value.val, castType, !value.isUnsigned); INT_ASSERT(ret.val); #endif @@ -5586,7 +5596,7 @@ DEFINE_PRIM(GPU_ALLOC_SHARED) { trackLLVMValue(sharedArray); // Get a void* pointer to the shared array. - llvm::Type* voidPtrType = getPointerType(gContext->llvmContext(), 3); + auto voidPtrType = getPointerType(gContext->llvmContext(), 3); llvm::Value* sharedArrayPtr = gGenInfo->irBuilder->CreateBitCast(sharedArray, voidPtrType, "sharedArrayPtr"); trackLLVMValue(sharedArrayPtr); @@ -6294,14 +6304,14 @@ DEFINE_PRIM(FTABLE_CALL) { argt = call->get(2)->typeInfo()->codegen().type; if (argMustUseCPtr(call->get(2)->typeInfo())) - argt = llvm::PointerType::getUnqual(argt); + argt = getPointerType(argt); argumentTypes.push_back(argt); argt = call->get(3)->typeInfo()->codegen().type; if (argMustUseCPtr(call->get(3)->typeInfo())) - argt = llvm::PointerType::getUnqual(argt); + argt = getPointerType(argt); argumentTypes.push_back(argt); @@ -6311,7 +6321,7 @@ DEFINE_PRIM(FTABLE_CALL) { // OK, now cast to the fnTy. fngen.val = gGenInfo->irBuilder->CreateBitCast(fnPtr, - llvm::PointerType::getUnqual(fnTy)); + getPointerType(fnTy)); trackLLVMValue(fngen.val); #endif } @@ -6413,7 +6423,7 @@ llvm::MDNode* createMetadataScope(llvm::LLVMContext& ctx, const char* name) { auto scopeName = llvm::MDString::get(ctx, name); - auto dummy = llvm::MDNode::getTemporary(ctx, chpl::empty); + auto dummy = llvm::MDNode::getTemporary(ctx, {}); llvm::Metadata* Args[] = {dummy.get(), domain, scopeName}; auto scope = llvm::MDNode::get(ctx, Args); // Remove the dummy and replace it with a self-reference. @@ -6437,7 +6447,7 @@ DEFINE_PRIM(NO_ALIAS_SET) { if (info->noAliasDomain == NULL) { auto domainName = llvm::MDString::get(ctx, "Chapel no-alias"); - auto dummy = llvm::MDNode::getTemporary(ctx, chpl::empty); + auto dummy = llvm::MDNode::getTemporary(ctx, {}); llvm::Metadata* Args[] = {dummy.get(), domainName}; info->noAliasDomain = llvm::MDNode::get(ctx, Args); // Remove the dummy and replace it with a self-reference. @@ -6509,7 +6519,7 @@ DEFINE_PRIM(DEBUG_TRAP) { } else { #ifdef HAVE_LLVM - ret.val = info->irBuilder->CreateIntrinsic(llvm::Intrinsic::debugtrap, {}, {}); + ret.val = CreateIntrinsic(llvm::Intrinsic::debugtrap, {}, {}); trackLLVMValue(ret.val); #endif } diff --git a/compiler/codegen/cg-symbol.cpp b/compiler/codegen/cg-symbol.cpp index 0047e2a76473..8d486440f728 100644 --- a/compiler/codegen/cg-symbol.cpp +++ b/compiler/codegen/cg-symbol.cpp @@ -80,7 +80,11 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" +#if LLVM_VERSION_MAJOR >= 21 +#include "llvm/IR/Intrinsics.h" +#else #include "llvm/Target/TargetIntrinsicInfo.h" +#endif #include "clang/CodeGen/CGFunctionInfo.h" #endif @@ -1913,7 +1917,7 @@ llvm::Type* TypeSymbol::getLLVMType() { if (auto* stype = llvm::dyn_cast_or_null(llvmImplType)) { if (auto* aggType = toAggregateType(this->type)) { if (aggType->isClass()) { - return llvm::PointerType::getUnqual(stype); + return getPointerType(stype); } } } @@ -2053,7 +2057,7 @@ llvmAttachReturnInfo(llvm::LLVMContext& ctx, case clang::CodeGen::ABIArgInfo::Kind::InAlloca: { if (returnInfo.getInAllocaSRet()) { - returnTy = llvm::PointerType::get(returnTy, stackSpace); + returnTy = getPointerType(returnTy, stackSpace); } else { returnTy = llvm::Type::getVoidTy(ctx); } @@ -2079,7 +2083,7 @@ llvmAttachReturnInfo(llvm::LLVMContext& ctx, } // returnTy is void, so use chapelReturnTy - argTys.push_back(llvm::PointerType::get(chapelReturnTy, stackSpace)); + argTys.push_back(getPointerType(chapelReturnTy, stackSpace)); argNames.push_back("indirect_return"); // Adjust attributes for sret argument @@ -2198,7 +2202,7 @@ codegenFunctionTypeLLVMImpl( // Add type for inalloca argument if (CGI->usesInAlloca()) { auto argStruct = CGI->getArgStruct(); - argTys.push_back(llvm::PointerType::getUnqual(argStruct)); + argTys.push_back(getPointerType(argStruct)); outArgNames.push_back("inalloca_arg"); // Adjust attributes for inalloca argument @@ -2257,7 +2261,7 @@ codegenFunctionTypeLLVMImpl( case clang::CodeGen::ABIArgInfo::Kind::Indirect: { // Emit indirect argument - argTys.push_back(llvm::PointerType::get(argTy, stackSpace)); + argTys.push_back(getPointerType(argTy, stackSpace)); outArgNames.push_back(astr(cname, ".indirect")); // Adjust attributes for indirect argument @@ -2473,7 +2477,7 @@ GenRet FnSymbol::codegenCast(GenRet fnPtr) { #ifdef HAVE_LLVM // now cast to correct function type llvm::FunctionType* fnType = llvm::cast(t.type); - llvm::PointerType *ptrToFnType = llvm::PointerType::getUnqual(fnType); + auto ptrToFnType = getPointerType(fnType); fngen.val = info->irBuilder->CreateBitCast(fnPtr.val, ptrToFnType); trackLLVMValue(fngen.val); #endif @@ -3036,9 +3040,9 @@ void FnSymbol::codegenDef() { // Create a temp variable to store into GenRet tmp = createTempVar(argType); llvm::Value* ptr = tmp.val; - llvm::Type* ptrEltTy = chapelArgTy; - llvm::Type* i8PtrTy = getPointerType(irBuilder); - llvm::Type* coercePtrTy = llvm::PointerType::get(sTy, stackSpace); + auto ptrEltTy = chapelArgTy; + auto i8PtrTy = getPointerType(irBuilder); + auto coercePtrTy = getPointerType(sTy, stackSpace); // handle offset if (unsigned offset = argInfo->getDirectOffset()) { @@ -3276,14 +3280,22 @@ GenRet FnSymbol::codegenAsCallBaseExpr() { INT_ASSERT(ty.type); llvm::Type* Types[] = {ty.type}; +#if LLVM_VERSION_MAJOR < 21 + // TargetIntrinsics where removed in LLVM 21 + // we just access them like normal intrinsics const llvm::TargetIntrinsicInfo *TII = info->targetMachine->getIntrinsicInfo(); +#endif #if LLVM_VERSION_MAJOR >= 20 llvm::Intrinsic::ID ID = llvm::Intrinsic::lookupIntrinsicID(cname); #else llvm::Intrinsic::ID ID = llvm::Function::lookupIntrinsicID(cname); #endif +#if LLVM_VERSION_MAJOR < 21 if (ID == llvm::Intrinsic::not_intrinsic && TII) ID = static_cast(TII->lookupName(cname)); +#endif + if (ID == llvm::Intrinsic::not_intrinsic) + USR_FATAL("Could not find LLVM intrinsic %s", cname); #if LLVM_VERSION_MAJOR >= 20 ret.val = llvm::Intrinsic::getOrInsertDeclaration(info->module, ID, Types); #else diff --git a/compiler/codegen/cg-type.cpp b/compiler/codegen/cg-type.cpp index b888d22056c8..47251ffe2494 100644 --- a/compiler/codegen/cg-type.cpp +++ b/compiler/codegen/cg-type.cpp @@ -126,7 +126,7 @@ static void codegenFunctionTypeLocal(FunctionType* ft) { // The final type is a pointer to the underlying function type. auto& layout = gGenInfo->module->getDataLayout(); auto addrSpace = layout.getAllocaAddrSpace(); - llvm::Type* t = llvm::PointerType::get(baseType, addrSpace); + llvm::Type* t = getPointerType(baseType, addrSpace); if (!ft->symbol->hasLLVMType()) { gGenInfo->lvt->addGlobalType(ft->symbol->cname, t, false); @@ -541,7 +541,7 @@ void AggregateType::codegenDef() { } else { #ifdef HAVE_LLVM llvm::Type* llBaseType = baseForLLVMPointer(base)->getLLVMType(); - type = llvm::PointerType::getUnqual(llBaseType); + type = getPointerType(llBaseType); structAlignment = ALIGNMENT_DEFER; // or pointer alignment #endif } @@ -556,7 +556,7 @@ void AggregateType::codegenDef() { } else { #ifdef HAVE_LLVM llvm::Type* llBaseType = baseForLLVMPointer(base)->getLLVMType(); - type = llvm::PointerType::getUnqual(llBaseType); + type = getPointerType(llBaseType); structAlignment = ALIGNMENT_DEFER; // or pointer alignment #endif } @@ -628,7 +628,7 @@ void AggregateType::codegenDef() { if (isOpaquePointer(llBaseType)) { // No need to compute the element type for an opaque pointer - globalPtrTy = llvm::PointerType::get(gContext->llvmContext(), + globalPtrTy = getPointerType(gContext->llvmContext(), globalAddressSpace); } else { #ifdef HAVE_LLVM_TYPED_POINTERS @@ -636,7 +636,7 @@ void AggregateType::codegenDef() { // of a wide pointer is always a local address. llvm::Type* eltType = llBaseType->getPointerElementType(); INT_ASSERT(eltType); - globalPtrTy = llvm::PointerType::get(eltType, globalAddressSpace); + globalPtrTy = getPointerType(eltType, globalAddressSpace); #endif } @@ -719,7 +719,7 @@ void AggregateType::codegenPrototype() { st = llvm::StructType::create(info->module->getContext(), struct_name); info->lvt->addGlobalType(struct_name, st, false); - llvm::PointerType* pt = llvm::PointerType::getUnqual(st); + auto pt = getPointerType(st); info->lvt->addGlobalType(symbol->cname, pt, false); symbol->llvmImplType = st; #endif diff --git a/compiler/codegen/codegen.cpp b/compiler/codegen/codegen.cpp index b8029721b63e..113526af27e3 100644 --- a/compiler/codegen/codegen.cpp +++ b/compiler/codegen/codegen.cpp @@ -299,7 +299,7 @@ static void genGlobalRawString(const char *cname, std::string &value, size_t len static void genGlobalVoidPtr(const char* cname, bool isHeader, bool isConstant=true) { GenInfo* info = gGenInfo; - llvm::Type* voidPtrTy = getPointerType(info->module->getContext(), 1); + auto voidPtrTy = getPointerType(info->module->getContext(), 1); llvm::GlobalVariable *global = llvm::cast( info->module->getOrInsertGlobal(cname, voidPtrTy)); global->setInitializer(llvm::Constant::getNullValue(voidPtrTy)); @@ -1499,7 +1499,7 @@ static void genGlobalSerializeTable(GenInfo* info) { fprintf(hdrfile, "\n};\n"); } else if (!gCodegenGPU) { #ifdef HAVE_LLVM - llvm::Type *global_serializeTableEntryType = + auto global_serializeTableEntryType = getPointerType(info->module->getContext()); std::vector global_serializeTable; @@ -2122,7 +2122,7 @@ static void codegen_header(std::set & cnames, fprintf(hdrfile, "\nextern void* const chpl_private_broadcast_table[];\n"); } else if(!gCodegenGPU) { #ifdef HAVE_LLVM - llvm::Type *private_broadcastTableEntryType = + auto private_broadcastTableEntryType = getPointerType(info->module->getContext()); std::vector private_broadcastTable; diff --git a/compiler/include/llvmUtil.h b/compiler/include/llvmUtil.h index b190b1fc940a..8fb118cd03a6 100644 --- a/compiler/include/llvmUtil.h +++ b/compiler/include/llvmUtil.h @@ -127,6 +127,8 @@ llvm::Type* tryComputingPointerElementType(llvm::Value* ptr); // Newer LLVMs don't distinguish between pointer type, these return an opaque `ptr` llvm::Type* getPointerType(llvm::LLVMContext& ctx, unsigned AS=0); llvm::Type* getPointerType(llvm::IRBuilder<>* irBuilder, unsigned AS=0); +// although this takes a type, it returns an opaque `ptr` on newer LLVMs +llvm::Type* getPointerType(llvm::Type* eltType, unsigned AS=0); #endif //HAVE_LLVM #endif //LLVMUTIL_H diff --git a/compiler/llvm/clangUtil.cpp b/compiler/llvm/clangUtil.cpp index ab27ad460995..e458a9edc562 100644 --- a/compiler/llvm/clangUtil.cpp +++ b/compiler/llvm/clangUtil.cpp @@ -1685,11 +1685,20 @@ void setupClang(GenInfo* info, std::string mainFile) CompilerInstance* Clang = new CompilerInstance(); auto diagOptions = clang::CreateAndPopulateDiagOpts(clangInfo->driverArgsCStrings); auto diagClient = new clang::TextDiagnosticPrinter(llvm::errs(), - &*diagOptions); +#if LLVM_VERSION_MAJOR >= 21 + *diagOptions +#else + diagOptions.get() +#endif + ); #if LLVM_VERSION_MAJOR >= 20 auto clangDiags = clang::CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(), +#if LLVM_VERSION_MAJOR >= 21 + *diagOptions, +#else diagOptions.release(), +#endif diagClient, /* owned */ true); #else @@ -2103,7 +2112,11 @@ static void setupModule() // Set the target triple. const llvm::Triple &Triple = clangInfo->Clang->getTarget().getTriple(); +#if LLVM_VERSION_MAJOR >= 21 + info->module->setTargetTriple(Triple); +#else info->module->setTargetTriple(Triple.getTriple()); +#endif // Always set the module layout. This works around an apparent bug in // clang or LLVM (trivial/deitz/test_array_low.chpl would print out the @@ -2171,13 +2184,23 @@ static void setupModule() #endif // Create the target machine. - info->targetMachine = Target->createTargetMachine(Triple.str(), +#if LLVM_VERSION_MAJOR >= 21 + info->targetMachine = Target->createTargetMachine(Triple, + cpu, + featuresString, + Options, + relocModel, + codeModel, + optLevel); +#else + info->targetMachine = Target->createTargetMachine(Triple.getTriple(), cpu, featuresString, Options, relocModel, codeModel, optLevel); +#endif @@ -4521,7 +4544,11 @@ static void linkBitCodeFile(const char *bitCodeFilePath) { // adjust it const llvm::Triple &Triple = info->clangInfo->Clang->getTarget().getTriple(); +#if LLVM_VERSION_MAJOR >= 21 + bcLib->setTargetTriple(Triple); +#else bcLib->setTargetTriple(Triple.getTriple()); +#endif bcLib->setDataLayout(info->clangInfo->asmTargetLayoutStr); // link @@ -4556,15 +4583,15 @@ static void linkGpuDeviceLibraries() { GenInfo* info = gGenInfo; // save external functions - std::set externals; - for (auto it = info->module->begin() ; it!= info->module->end() ; ++it) { - if (it->hasExternalLinkage()) { - externals.insert(it->getGlobalIdentifier()); + std::unordered_set externals; + for (const auto& f: info->module->functions()) { + if (f.hasExternalLinkage()) { + externals.insert(f.getGUID()); } } - for (auto it = info->module->global_begin() ; it!= info->module->global_end() ; ++it) { - if (it->hasExternalLinkage()) { - externals.insert(it->getGlobalIdentifier()); + for (const auto& g: info->module->globals()) { + if (g.hasExternalLinkage()) { + externals.insert(g.getGUID()); } } @@ -4602,7 +4629,7 @@ static void linkGpuDeviceLibraries() { // internalize all functions that are not in `externals` llvm::InternalizePass iPass([&externals](const llvm::GlobalValue& gv) { - return externals.count(gv.getGlobalIdentifier()) > 0; + return externals.count(gv.getGUID()) > 0; }); iPass.internalizeModule(*info->module); } @@ -4653,8 +4680,8 @@ void setupForGlobalToWide(void) { const char* dummy = "chpl_wide_opt_dummy"; if( getFunctionLLVM(dummy) ) INT_FATAL("dummy function already exists"); - llvm::Type* retType = getPointerType(ginfo->module->getContext()); - llvm::Type* argType = llvm::Type::getInt64Ty(ginfo->module->getContext()); + auto retType = getPointerType(ginfo->module->getContext()); + auto argType = llvm::Type::getInt64Ty(ginfo->module->getContext()); llvm::Value* fval = ginfo->module->getOrInsertFunction( dummy, retType, argType).getCallee(); llvm::Function* fn = llvm::dyn_cast(fval); @@ -4748,7 +4775,7 @@ void checkAdjustedDataLayout() { // Check that the data layout setting worked const llvm::DataLayout& dl = info->module->getDataLayout(); - llvm::Type* testTy = getPointerType(info->module->getContext(), GLOBAL_PTR_SPACE); + auto testTy = getPointerType(info->module->getContext(), GLOBAL_PTR_SPACE); INT_ASSERT(dl.getTypeSizeInBits(testTy) == GLOBAL_PTR_SIZE); } diff --git a/compiler/llvm/llvmAggregateGlobalOps.cpp b/compiler/llvm/llvmAggregateGlobalOps.cpp index 4059df22e5bf..a2883337c252 100644 --- a/compiler/llvm/llvmAggregateGlobalOps.cpp +++ b/compiler/llvm/llvmAggregateGlobalOps.cpp @@ -431,9 +431,9 @@ Instruction *AggregateGlobalOpsOpt::tryAggregating(Instruction *StartInst, Value Module* M = StartInst->getParent()->getParent()->getParent(); LLVMContext& Context = StartInst->getContext(); - Type* int8Ty = Type::getInt8Ty(Context); - Type* sizeTy = DL->getIntPtrType(Context, 0); - Type* globalInt8PtrTy = llvm::PointerType::get(int8Ty, globalSpace); + auto int8Ty = Type::getInt8Ty(Context); + auto sizeTy = DL->getIntPtrType(Context, 0); + auto globalInt8PtrTy = getPointerType(int8Ty, globalSpace); bool isLoad = isa(StartInst); bool isStore = isa(StartInst); Instruction *lastAddedInsn = NULL; @@ -647,9 +647,9 @@ Instruction *AggregateGlobalOpsOpt::tryAggregating(Instruction *StartInst, Value offsets); trackLLVMValue(i8Dst); - Type* StoreType = oldStore->getValueOperand()->getType(); - Type* PtrType = llvm::PointerType::getUnqual(StoreType); - Value* Dst = irBuilder.CreatePointerCast(i8Dst, PtrType); + auto StoreType = oldStore->getValueOperand()->getType(); + auto PtrType = getPointerType(StoreType); + auto* Dst = irBuilder.CreatePointerCast(i8Dst, PtrType); trackLLVMValue(Dst); StoreInst* newStore = @@ -674,8 +674,8 @@ Instruction *AggregateGlobalOpsOpt::tryAggregating(Instruction *StartInst, Value if( isLoad ) addrSpaceSrc = globalSpace; Type *types[3]; - types[0] = llvm::PointerType::get(int8Ty, addrSpaceDst); - types[1] = llvm::PointerType::get(int8Ty, addrSpaceSrc); + types[0] = getPointerType(int8Ty, addrSpaceDst); + types[1] = getPointerType(int8Ty, addrSpaceSrc); types[2] = sizeTy; #if LLVM_VERSION_MAJOR >= 20 @@ -733,8 +733,8 @@ Instruction *AggregateGlobalOpsOpt::tryAggregating(Instruction *StartInst, Value offsets); trackLLVMValue(i8Src); - Type* LoadType = oldLoad->getType(); - Type* PtrType = llvm::PointerType::getUnqual(LoadType); + auto LoadType = oldLoad->getType(); + auto PtrType = getPointerType(LoadType); Value* Src = irBuilder.CreatePointerCast(i8Src, PtrType); trackLLVMValue(Src); diff --git a/compiler/llvm/llvmGlobalToWide.cpp b/compiler/llvm/llvmGlobalToWide.cpp index f73f4347f2b1..536cfecc3a19 100644 --- a/compiler/llvm/llvmGlobalToWide.cpp +++ b/compiler/llvm/llvmGlobalToWide.cpp @@ -323,8 +323,8 @@ namespace { Value* alloc = makeAlloca(allocType, "widecast", insertBefore); - Type* fromPtrType = llvm::PointerType::getUnqual(fromValue->getType()); - Type* newPtrType = llvm::PointerType::getUnqual(toType); + auto fromPtrType = getPointerType(fromValue->getType()); + auto newPtrType = getPointerType(toType); Value* allocAsFrom = alloc; if (allocAsFrom->getType() != fromPtrType) { @@ -401,7 +401,7 @@ namespace { voidPtrTy = getPointerType(M.getContext(), 0); glVoidPtrTy = getPointerType(M.getContext(), info->globalSpace); wideVoidPtrTy = convertTypeGlobalToWide(&M, info, glVoidPtrTy); - ptrLocTy = llvm::PointerType::getUnqual(info->localeIdType); + ptrLocTy = getPointerType(info->localeIdType); i64Ty = llvm::Type::getInt64Ty(M.getContext()); i8Ty = llvm::Type::getInt8Ty(M.getContext()); @@ -1304,7 +1304,11 @@ bool GlobalToWide::run(Module &M) { if( debugThisFn[0] || debugAllPassOne || debugAllPassTwo ) { dbgs() << "GlobalToWide: "; dbgs().write_escaped(M.getModuleIdentifier()) << '\n'; +#if LLVM_VERSION_MAJOR >= 21 + dbgs().write_escaped(M.getTargetTriple().str()) << '\n'; +#else dbgs().write_escaped(M.getTargetTriple()) << '\n'; +#endif } // Normally we expect a user of this optimization to have @@ -1312,12 +1316,12 @@ bool GlobalToWide::run(Module &M) { // information, but if not we set some defaults here so // that tests can be created and bugpoint can be run. if( !info ) { - Type* voidTy = llvm::Type::getVoidTy(M.getContext()); - Type* voidPtrTy = getPointerType(M.getContext(), 0); - Type* i64Ty = llvm::Type::getInt64Ty(M.getContext()); - Type* i8Ty = llvm::Type::getInt8Ty(M.getContext()); + auto voidTy = llvm::Type::getVoidTy(M.getContext()); + auto voidPtrTy = getPointerType(M.getContext(), 0); + auto i64Ty = llvm::Type::getInt64Ty(M.getContext()); + auto i8Ty = llvm::Type::getInt8Ty(M.getContext()); const DataLayout& DL = M.getDataLayout(); - Type* sizeTy = DL.getIntPtrType(M.getContext(), 0); + auto sizeTy = DL.getIntPtrType(M.getContext(), 0); errs() << "Warning: GlobalToWide using default configuration\n"; info = new GlobalToWideInfo(); @@ -1826,7 +1830,7 @@ bool GlobalToWide::run(Module &M) { Constant *init = ConstantExpr::getPointerCast(gv, new_type); GlobalAlias *new_alias = GlobalAlias::create( - llvm::PointerType::getUnqual(new_type), + getPointerType(new_type), 0, /* addr space */ ga->getLinkage(), "", init, &M); @@ -2152,10 +2156,10 @@ void populateFunctionsForGlobalType(Module *module, GlobalToWideInfo* info, Type GlobalPointerInfo & r = info->gTypes[globalPtrTy]; if (isOpaquePointer(globalPtrTy)) { - ptrTy = llvm::PointerType::getUnqual(module->getContext()); + ptrTy = getPointerType(module->getContext()); } else { #ifdef HAVE_LLVM_TYPED_POINTERS - ptrTy = llvm::PointerType::getUnqual(globalPtrTy->getPointerElementType()); + ptrTy = getPointerType(globalPtrTy->getPointerElementType()); #else assert(false && "Should not be reachable"); #endif @@ -2314,15 +2318,15 @@ Type* createWidePointerToType(Module* module, GlobalToWideInfo* i, Type* eltTy) // Get the wide pointer struct containing {locale, address} Type* fields[2]; fields[0] = i->localeIdType; - llvm::PointerType* ptrTy = nullptr; + llvm::Type* ptrTy = nullptr; if (eltTy) { #ifdef HAVE_LLVM_TYPED_POINTERS - ptrTy = llvm::PointerType::getUnqual(eltTy); + ptrTy = getPointerType(eltTy); #else assert(false && "Should not be reachable"); #endif } else { - ptrTy = llvm::PointerType::getUnqual(context); + ptrTy = getPointerType(context); } assert(ptrTy); fields[1] = ptrTy; @@ -2415,7 +2419,7 @@ Type* convertTypeGlobalToWide(Module* module, GlobalToWideInfo* info, Type* t) // Replace the pointer with a struct containing {locale, address} return createWidePointerToType(module, info, nullptr); } else { - return llvm::PointerType::get(context, t->getPointerAddressSpace()); + return getPointerType(context, t->getPointerAddressSpace()); } } else { #ifdef HAVE_LLVM_TYPED_POINTERS @@ -2427,7 +2431,7 @@ Type* convertTypeGlobalToWide(Module* module, GlobalToWideInfo* info, Type* t) // Replace the pointer with a struct containing {locale, address} return createWidePointerToType(module, info, wideEltType); } else { - return llvm::PointerType::get(wideEltType, t->getPointerAddressSpace()); + return getPointerType(wideEltType, t->getPointerAddressSpace()); } #else assert(false && "Should not be reachable"); diff --git a/compiler/llvm/llvmUtil.cpp b/compiler/llvm/llvmUtil.cpp index a37be457f229..5909f2ab73cc 100644 --- a/compiler/llvm/llvmUtil.cpp +++ b/compiler/llvm/llvmUtil.cpp @@ -508,8 +508,8 @@ llvm::Value *convertValueToType(llvm::IRBuilder<>* irBuilder, // todo: setValueAlignment(tmp_alloc, ???, ???); *alloca = tmp_alloc; // Now cast the allocation to both fromType and toType. - llvm::Type* curPtrType = llvm::PointerType::getUnqual(curType); - llvm::Type* newPtrType = llvm::PointerType::getUnqual(newType); + auto curPtrType = getPointerType(curType); + auto newPtrType = getPointerType(newType); // Now get cast pointers llvm::Value* tmp_cur = irBuilder->CreatePointerCast(tmp_alloc, curPtrType); trackLLVMValue(tmp_cur); @@ -851,5 +851,12 @@ llvm::Type* getPointerType(llvm::IRBuilder<>* irBuilder, unsigned AS) { return irBuilder->getInt8PtrTy(AS); #endif } +llvm::Type* getPointerType(llvm::Type* eltType, unsigned AS) { +#if LLVM_VERSION_MAJOR < 21 + return llvm::PointerType::get(eltType, AS); +#else + return llvm::PointerType::get(eltType->getContext(), AS); +#endif +} #endif diff --git a/frontend/lib/util/clang-integration.cpp b/frontend/lib/util/clang-integration.cpp index 196c78e9c5f7..79e6c1b8ce90 100644 --- a/frontend/lib/util/clang-integration.cpp +++ b/frontend/lib/util/clang-integration.cpp @@ -160,9 +160,21 @@ const std::vector& getCC1Arguments(Context* context, // Create a compiler instance to handle the actual work. auto diagOptions = new clang::DiagnosticOptions(); auto diagClient = new clang::TextDiagnosticPrinter(llvm::errs(), - &*diagOptions); +#if LLVM_VERSION_MAJOR >= 21 + *diagOptions +#else + &*diagOptions +#endif + ); auto diagID = new clang::DiagnosticIDs(); - auto diags = new clang::DiagnosticsEngine(diagID, &*diagOptions, diagClient); + auto diags = new clang::DiagnosticsEngine( + diagID, +#if LLVM_VERSION_MAJOR >= 21 + *diagOptions, +#else + &*diagOptions, +#endif + diagClient); // takes ownership of all of the above clang::driver::Driver D(argsCstrs[0], triple, *diags); @@ -296,8 +308,15 @@ createClangPrecompiledHeader(Context* context, ID externBlockId) { auto diagOptions = clang::CreateAndPopulateDiagOpts(cc1argsCstrs); auto diagClient = new clang::TextDiagnosticBuffer(); +#if LLVM_VERSION_MAJOR >= 21 + auto clangDiags = + clang::CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(), + *diagOptions, + diagClient, + /* owned */ true); +#else #if LLVM_VERSION_MAJOR >= 20 - auto clangDiags = + auto clangDiags = clang::CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(), diagOptions.release(), diagClient, @@ -307,6 +326,7 @@ createClangPrecompiledHeader(Context* context, ID externBlockId) { clang::CompilerInstance::createDiagnostics(diagOptions.release(), diagClient, /* owned */ true); +#endif #endif Clang->setDiagnostics(&*clangDiags); @@ -456,7 +476,20 @@ static owned getCompilerInstanceForReadingPch( clang::CompilerInstance* Clang = new clang::CompilerInstance(); auto diagOptions = clang::CreateAndPopulateDiagOpts(cc1argsCstrs); auto diagClient = new clang::TextDiagnosticPrinter(llvm::errs(), - &*diagOptions); +#if LLVM_VERSION_MAJOR >= 21 + *diagOptions +#else + &*diagOptions +#endif + ); + +#if LLVM_VERSION_MAJOR >= 21 + auto clangDiags = + clang::CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(), + *diagOptions, + diagClient, + /* owned */ true); +#else #if LLVM_VERSION_MAJOR >= 20 auto clangDiags = clang::CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(), @@ -468,6 +501,7 @@ static owned getCompilerInstanceForReadingPch( clang::CompilerInstance::createDiagnostics(diagOptions.release(), diagClient, /* owned */ true); +#endif #endif Clang->setDiagnostics(&*clangDiags); @@ -476,7 +510,16 @@ static owned getCompilerInstanceForReadingPch( cc1argsCstrs, *clangDiags); CHPL_ASSERT(success); - Clang->setTarget(clang::TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); + Clang->setTarget( + clang::TargetInfo::CreateTargetInfo( + Clang->getDiagnostics(), +#if LLVM_VERSION_MAJOR >= 21 + Clang->getInvocation().getTargetOpts() +#else + Clang->getInvocation().TargetOpts +#endif + ) + ); Clang->createFileManager(); Clang->createSourceManager(Clang->getFileManager()); Clang->createPreprocessor(clang::TU_Complete); diff --git a/test/llvm/abi/aarch64-darwin/export-vs-c-llvm21.good b/test/llvm/abi/aarch64-darwin/export-vs-c-llvm21.good new file mode 120000 index 000000000000..13b5c7fe463e --- /dev/null +++ b/test/llvm/abi/aarch64-darwin/export-vs-c-llvm21.good @@ -0,0 +1 @@ +export-vs-c-llvm18.good \ No newline at end of file diff --git a/test/llvm/abi/x86-64/export-vs-c-llvm21.good b/test/llvm/abi/x86-64/export-vs-c-llvm21.good new file mode 120000 index 000000000000..13b5c7fe463e --- /dev/null +++ b/test/llvm/abi/x86-64/export-vs-c-llvm21.good @@ -0,0 +1 @@ +export-vs-c-llvm18.good \ No newline at end of file diff --git a/util/chplenv/chpl_llvm.py b/util/chplenv/chpl_llvm.py index 9ec2f9a42df0..b364c44fee37 100755 --- a/util/chplenv/chpl_llvm.py +++ b/util/chplenv/chpl_llvm.py @@ -17,7 +17,7 @@ def llvm_versions(): # Which major release - only need one number for that with current # llvm (since LLVM 4.0). # These will be tried in order. - return ('20','19','18','17','16','15','14',) + return ('21','20','19','18','17','16','15','14',) @memoize def get_uniq_cfg_path_for(llvm_val, llvm_support_val):