Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions contrib/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 9 additions & 0 deletions doc/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
98 changes: 64 additions & 34 deletions src/llvm-multiversioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ struct CloneCtx {
void clone_partial(Group &grp, Target &tgt);
uint32_t get_func_id(Function *F) const;
std::pair<uint32_t,GlobalVariable*> 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;
Expand Down Expand Up @@ -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<Value *, 0> Args;
for (auto &arg : trampoline->args())
Args.push_back(&arg);
auto call = irbuilder.CreateCall(F->getFunctionType(), ptr, ArrayRef<Value *>(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) {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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<Value *, 0> Args;
for (auto &arg : trampoline->args())
Args.push_back(&arg);
auto call = irbuilder.CreateCall(F->getFunctionType(), ptr, ArrayRef<Value *>(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()
Expand Down
20 changes: 15 additions & 5 deletions test/llvmpasses/multiversioning-clone-only.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion test/llvmpasses/multiversioning-x86.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 10 additions & 1 deletion test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions test/project/deps/BadStdlibDeps2/Manifest.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions test/project/deps/BadStdlibDeps2/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"