diff --git a/base/loading.jl b/base/loading.jl index 316f652c78fa3..40db7df679b60 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -462,6 +462,8 @@ function locate_package_env(pkg::PkgId, stopenv::Union{String, Nothing}=nothing) path = manifest_uuid_path(env, pkg) # missing is used as a sentinel to stop looking further down in envs if path === missing + # Before stopping, try stdlib fallback + is_stdlib(pkg) && @goto stdlib_fallback path = nothing @goto done end @@ -473,6 +475,7 @@ function locate_package_env(pkg::PkgId, stopenv::Union{String, Nothing}=nothing) stopenv == env && break end end + @label stdlib_fallback # Allow loading of stdlibs if the name/uuid are given # e.g. if they have been explicitly added to the project/manifest mbypath = manifest_uuid_path(Sys.STDLIB, pkg) diff --git a/contrib/generate_precompile.jl b/contrib/generate_precompile.jl index 88972be56ab4d..998a77e31452a 100644 --- a/contrib/generate_precompile.jl +++ b/contrib/generate_precompile.jl @@ -348,8 +348,8 @@ generate_precompile_statements() = try # Make sure `ansi_enablecursor` is printe uuid = "$pkguuid" """) touch(joinpath(pkgpath, "Manifest.toml")) - tmp_prec = tempname(prec_path) - tmp_proc = tempname(prec_path) + tmp_prec = tempname(prec_path; cleanup=false) + tmp_proc = tempname(prec_path; cleanup=false) s = """ pushfirst!(DEPOT_PATH, $(repr(joinpath(prec_path,"depot")))); Base.PRECOMPILE_TRACE_COMPILE[] = $(repr(tmp_prec)); diff --git a/doc/make.jl b/doc/make.jl index 6379bf8dae7bf..dd847b834aa8e 100644 --- a/doc/make.jl +++ b/doc/make.jl @@ -202,6 +202,15 @@ BaseDocs = [ StdlibDocs = [stdlib.targetfile for stdlib in STDLIB_DOCS] +# HACK: get nicer sorting here, even though we don't have the header +# of the .md files at hand. +sort!(StdlibDocs, by=function(x) + x = replace(x, "stdlib/" => "") + startswith(x, "Libdl") && return lowercase("Dynamic Linker") + startswith(x, "Test") && return lowercase("Unit Testing") + return lowercase(x) +end) + DevDocs = [ "Documentation of Julia's Internals" => [ "devdocs/init.md", diff --git a/src/llvm-multiversioning.cpp b/src/llvm-multiversioning.cpp index 02f77298513ee..dc63cad14a1cb 100644 --- a/src/llvm-multiversioning.cpp +++ b/src/llvm-multiversioning.cpp @@ -378,6 +378,8 @@ struct CloneCtx { void clone_partial(Group &grp, Target &tgt); uint32_t get_func_id(Function *F) const; std::pair get_reloc_slot(Function *F) const; + + Function *create_trampoline(Function *F, GlobalVariable *slot, bool autoinit=false); void rewrite_alias(GlobalAlias *alias, Function* F); MDNode *tbaa_const; @@ -492,6 +494,53 @@ void CloneCtx::prepare_vmap(ValueToValueMapTy &vmap) } } +Function *CloneCtx::create_trampoline(Function *F, GlobalVariable *slot, bool autoinit) +{ + Function *trampoline = + Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage, "", &M); + + trampoline->copyAttributesFrom(F); + trampoline->setVisibility(GlobalValue::HiddenVisibility); + trampoline->setDSOLocal(true); + + // drop multiversioning attributes + trampoline->removeFnAttr("julia.mv.reloc"); + trampoline->removeFnAttr("julia.mv.clones"); + + auto BB = BasicBlock::Create(F->getContext(), "top", trampoline); + IRBuilder<> irbuilder(BB); + + if (autoinit) { + irbuilder.CreateCall(F->getParent()->getOrInsertFunction( + XSTR(jl_autoinit_and_adopt_thread), + PointerType::get(F->getContext(), 0) + )); + } + + auto ptr = irbuilder.CreateLoad(F->getType(), slot); + ptr->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_const); + ptr->setMetadata(llvm::LLVMContext::MD_invariant_load, MDNode::get(F->getContext(), None)); + + SmallVector Args; + for (auto &arg : trampoline->args()) + Args.push_back(&arg); + auto call = irbuilder.CreateCall(F->getFunctionType(), ptr, ArrayRef(Args)); + if (F->isVarArg()) { + assert(!TT.isARM() && !TT.isPPC() && "musttail not supported on ARM/PPC!"); + call->setTailCallKind(CallInst::TCK_MustTail); + } else { + call->setTailCallKind(CallInst::TCK_Tail); + + } + + if (F->getReturnType() == Type::getVoidTy(F->getContext())) + irbuilder.CreateRetVoid(); + else + irbuilder.CreateRet(call); + + return trampoline; +} + void CloneCtx::prepare_slots() { for (auto &F : orig_funcs) { @@ -506,7 +555,12 @@ void CloneCtx::prepare_slots() else { auto id = get_func_id(F); const_relocs[id] = GV; - GV->setInitializer(Constant::getNullValue(F->getType())); + + // Initialize with a single-use trampoline that calls `jl_autoinit_and_adopt_thread`, + // so that auto-initialization works with multi-versioned entrypoints. + Function *trampoline = create_trampoline(F, GV, /* autoinit */ true); + trampoline->setName(F->getName() + ".autoinit_trampoline"); + GV->setInitializer(trampoline); } } } @@ -664,45 +718,21 @@ void CloneCtx::rewrite_alias(GlobalAlias *alias, Function *F) { assert(!is_vector(F->getFunctionType())); - Function *trampoline = - Function::Create(F->getFunctionType(), alias->getLinkage(), "", &M); - trampoline->copyAttributesFrom(F); - trampoline->takeName(alias); - trampoline->setVisibility(alias->getVisibility()); - trampoline->setDSOLocal(alias->isDSOLocal()); - // drop multiversioning attributes, add alias attribute for testing purposes - trampoline->removeFnAttr("julia.mv.reloc"); - trampoline->removeFnAttr("julia.mv.clones"); - trampoline->addFnAttr("julia.mv.alias"); - trampoline->setDLLStorageClass(alias->getDLLStorageClass()); - alias->eraseFromParent(); - uint32_t id; GlobalVariable *slot; std::tie(id, slot) = get_reloc_slot(F); + assert(slot); - auto BB = BasicBlock::Create(F->getContext(), "top", trampoline); - IRBuilder<> irbuilder(BB); + Function *trampoline = create_trampoline(F, slot, /* autoinit */ false); + trampoline->addFnAttr("julia.mv.alias"); // add alias attribute for testing purposes - auto ptr = irbuilder.CreateLoad(F->getType(), slot); - ptr->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_const); - ptr->setMetadata(llvm::LLVMContext::MD_invariant_load, MDNode::get(F->getContext(), None)); - - SmallVector Args; - for (auto &arg : trampoline->args()) - Args.push_back(&arg); - auto call = irbuilder.CreateCall(F->getFunctionType(), ptr, ArrayRef(Args)); - if (F->isVarArg()) { - assert(!TT.isARM() && !TT.isPPC() && "musttail not supported on ARM/PPC!"); - call->setTailCallKind(CallInst::TCK_MustTail); - } else { - call->setTailCallKind(CallInst::TCK_Tail); - } + trampoline->takeName(alias); + trampoline->setLinkage(alias->getLinkage()); + trampoline->setVisibility(alias->getVisibility()); + trampoline->setDSOLocal(alias->isDSOLocal()); + trampoline->setDLLStorageClass(alias->getDLLStorageClass()); - if (F->getReturnType() == Type::getVoidTy(F->getContext())) - irbuilder.CreateRetVoid(); - else - irbuilder.CreateRet(call); + alias->eraseFromParent(); } void CloneCtx::fix_gv_uses() diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 2ea2fb3a684df..6c8fc684d8e53 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -287,7 +287,7 @@ function random_seed() # almost surely always getting distinct seeds, while having them printed reasonably tersely return rand(RandomDevice(), UInt128) catch ex - ex isa IOError || rethrow() + ex isa Base.IOError || rethrow() @warn "Entropy pool not available to seed RNG; using ad-hoc entropy sources." return Libc.rand() end diff --git a/test/llvmpasses/multiversioning-clone-only.ll b/test/llvmpasses/multiversioning-clone-only.ll index 00f0db0aa1e91..c4f5257a59988 100644 --- a/test/llvmpasses/multiversioning-clone-only.ll +++ b/test/llvmpasses/multiversioning-clone-only.ll @@ -7,7 +7,7 @@ ; CHECK: @jl_fvar_idxs = hidden constant [1 x i32] zeroinitializer ; CHECK: @jl_gvar_idxs = hidden constant [0 x i32] zeroinitializer ; OPAQUE: @subtarget_cloned_gv = hidden global ptr null -; OPAQUE: @subtarget_cloned.reloc_slot = hidden global ptr null +; OPAQUE: @subtarget_cloned.reloc_slot = hidden global ptr @subtarget_cloned.autoinit_trampoline ; CHECK: @jl_fvar_count = hidden constant i64 1 ; OPAQUE: @jl_fvar_ptrs = hidden global [1 x ptr] [ptr @subtarget_cloned] ; CHECK: @jl_clone_slots = hidden constant [5 x i32] @@ -57,7 +57,7 @@ define noundef i32 @subtarget_cloned(i32 noundef %0) #2 { ; COM: should fixup this callsite since 2 is cloned for a subtarget ; CHECK: define{{.*}}@call_subtarget_cloned({{.*}}#[[CALL_SUBTARGET_CLONED_DEFAULT_ATTRS:[0-9]+]] ; CHECK-NEXT: [[FUNC_PTR:%[0-9]+]] = load{{.*}}@subtarget_cloned.reloc_slot{{.*}}!tbaa ![[TBAA_CONST_METADATA:[0-9]+]], !invariant.load -; CHECK-NEXT: call{{.*}}[[FUNC_PTR]] +; CHECK-NEXT: call{{.*}}[[FUNC_PTR]]({{.*}}) ; CHECK: ret i32 define noundef i32 @call_subtarget_cloned(i32 noundef %0) #3 { %2 = call noundef i32 @subtarget_cloned(i32 noundef %0) @@ -66,13 +66,23 @@ define noundef i32 @call_subtarget_cloned(i32 noundef %0) #3 { ; CHECK: define{{.*}}@call_subtarget_cloned_but_not_cloned({{.*}}#[[BORING_DEFAULT_ATTRS]] ; CHECK-NEXT: [[FUNC_PTR:%[0-9]+]] = load{{.*}}@subtarget_cloned.reloc_slot{{.*}}!tbaa ![[TBAA_CONST_METADATA]], !invariant.load -; CHECK-NEXT: call{{.*}}[[FUNC_PTR]] +; CHECK-NEXT: call{{.*}}[[FUNC_PTR]]({{.*}}) ; CHECK: ret i32 define noundef i32 @call_subtarget_cloned_but_not_cloned(i32 noundef %0) #0 { %2 = call noundef i32 @subtarget_cloned(i32 noundef %0) ret i32 %2 } +; COM: check that the autoinit trampoline is generated correctly +; CHECK: define{{.*}}@subtarget_cloned.autoinit_trampoline({{.*}} +; CHECK-NEXT: top: +; CHECK-NEXT: call ptr @ijl_autoinit_and_adopt_thread() +; CHECK-NEXT: [[FUNC_PTR:%[0-9]+]] = load ptr, ptr @subtarget_cloned.reloc_slot{{.*}}!tbaa ![[TBAA_CONST_METADATA]], !invariant.load +; CHECK-NEXT: call{{.*}}[[FUNC_PTR]]({{.*}}) +; CHECK: ret i32 + +declare ptr @ijl_autoinit_and_adopt_thread() + ; CHECK: define{{.*}}@boring.1({{.*}}#[[BORING_CLONEALL_ATTRS:[0-9]+]] ; CHECK-NEXT: ret i32 %0 @@ -106,10 +116,10 @@ define noundef i32 @call_subtarget_cloned_but_not_cloned(i32 noundef %0) #0 { ; CHECK-NOT: @subtarget_cloned_but_not_cloned.2 ; COM: check for alias being rewritten to a function trampoline -; CHECK: define{{.*}}@subtarget_cloned_aliased{{.*}}#[[SUBTARGET_ALIASED_ATTRS:[0-9]+]] +; CHECK: define{{.*}}@subtarget_cloned_aliased{{[^.]*}}#[[SUBTARGET_ALIASED_ATTRS:[0-9]+]] ; CHECK-NOT: } ; CHECK: [[FUNC_PTR:%[0-9]+]] = load{{.*}}@subtarget_cloned.reloc_slot{{.*}}!tbaa ![[TBAA_CONST_METADATA]], !invariant.load -; CHECK-NEXT: call{{.*}}[[FUNC_PTR]] +; CHECK-NEXT: call{{.*}}[[FUNC_PTR]]({{.*}}) ; CHECK: ret i32 ; CHECK: attributes #[[BORING_DEFAULT_ATTRS]] diff --git a/test/llvmpasses/multiversioning-x86.ll b/test/llvmpasses/multiversioning-x86.ll index ff4a8abba5252..e2918d0c20eec 100644 --- a/test/llvmpasses/multiversioning-x86.ll +++ b/test/llvmpasses/multiversioning-x86.ll @@ -11,7 +11,7 @@ ; OPAQUE: @jl_gvar_ptrs = global [0 x ptr] zeroinitializer, align 8 ; CHECK: @jl_fvar_idxs = hidden constant [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4], align 8 ; CHECK: @jl_gvar_idxs = hidden constant [0 x i32] zeroinitializer, align 8 -; OPAQUE: @simd_test.reloc_slot = hidden global ptr null +; OPAQUE: @simd_test.reloc_slot = hidden global ptr @simd_test.autoinit_trampoline ; OPAQUE: @jl_fvar_ptrs = hidden global [5 x ptr] [ptr @boring, ptr @fastmath_test, ptr @loop_test, ptr @simd_test, ptr @simd_test_call] ; OPAQUE: @jl_clone_slots = hidden constant [3 x i32] [i32 1, i32 3, i32 trunc (i64 sub (i64 ptrtoint (ptr @simd_test.reloc_slot to i64), i64 ptrtoint (ptr @jl_clone_slots to i64)) to i32)] ; CHECK: @jl_clone_idxs = hidden constant [10 x i32] [i32 -2147483647, i32 3, i32 -2147483647, i32 3, i32 4, i32 1, i32 1, i32 2, i32 -2147483645, i32 4] diff --git a/test/loading.jl b/test/loading.jl index 1a0a723eee533..a6fb433be52df 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -1414,12 +1414,21 @@ end mktempdir() do depot # This manifest has a LibGit2 entry that is missing LibGit2_jll, which should be # handled by falling back to the stdlib Project.toml for dependency truth. - badmanifest_test_dir = joinpath(@__DIR__, "project", "deps", "BadStdlibDeps.jl") + badmanifest_test_dir = joinpath(@__DIR__, "project", "deps", "BadStdlibDeps") @test success(addenv( `$(Base.julia_cmd()) --project=$badmanifest_test_dir --startup-file=no -e 'using LibGit2'`, "JULIA_DEPOT_PATH" => depot * Base.Filesystem.pathsep(), )) end + mktempdir() do depot + # This manifest has a LibGit2 entry that has a LibGit2_jll with a git-tree-hash1 + # which simulates an old manifest where LibGit2_jll was not a stdlib + badmanifest_test_dir2 = joinpath(@__DIR__, "project", "deps", "BadStdlibDeps2") + @test success(addenv( + `$(Base.julia_cmd()) --project=$badmanifest_test_dir2 --startup-file=no -e 'using LibGit2'`, + "JULIA_DEPOT_PATH" => depot * Base.Filesystem.pathsep(), + )) + end end @testset "code coverage disabled during precompilation" begin diff --git a/test/project/deps/BadStdlibDeps2/Manifest.toml b/test/project/deps/BadStdlibDeps2/Manifest.toml new file mode 100644 index 0000000000000..988efc8da56f3 --- /dev/null +++ b/test/project/deps/BadStdlibDeps2/Manifest.toml @@ -0,0 +1,54 @@ +# This file is machine-generated - editing it directly is not advised + +julia_version = "1.12.0-DEV" +manifest_format = "2.0" +project_hash = "dc9d33b0ee13d9466bdb75b8d375808a534a79ec" + +[[deps.Artifacts]] +uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" +version = "1.11.0" + +[[deps.LibGit2]] +deps = ["NetworkOptions", "Printf", "SHA", "LibGit2_jll"] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" +version = "1.11.0" + +# This is an stdlib but intentionally has a git-tree-sha1 because +# we are emulating that the manifest comes from a version where +# LibGit2_jll was not an stdlib +[[deps.LibGit2_jll]] +deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"] +git-tree-sha1 = "1111111111111111111111111111111111111111" +uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5" +version = "1.8.0+0" + +[[deps.LibSSH2_jll]] +deps = ["Artifacts", "Libdl", "MbedTLS_jll"] +uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" +version = "1.11.0+1" + +[[deps.Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" +version = "1.11.0" + +[[deps.MbedTLS_jll]] +deps = ["Artifacts", "Libdl"] +uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" +version = "2.28.6+1" + +[[deps.NetworkOptions]] +uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" +version = "1.2.0" + +[[deps.Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" +version = "1.11.0" + +[[deps.SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" +version = "0.7.0" + +[[deps.Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" +version = "1.11.0" diff --git a/test/project/deps/BadStdlibDeps2/Project.toml b/test/project/deps/BadStdlibDeps2/Project.toml new file mode 100644 index 0000000000000..223889185ea15 --- /dev/null +++ b/test/project/deps/BadStdlibDeps2/Project.toml @@ -0,0 +1,2 @@ +[deps] +LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"