← Back to Pull requests

JIT: link ccalls directly (removes DLSymOptimizer*) #61363

Open
xal-0 opened this pull request with 917 lines diff across 8 files
View on GitHub
xal-0 xal-0 commented on Mar 18, 2026 23:03 UTC
Author

In the interest of making the generated LLVM IR process-independent, we should find an alternative to DLSymOptimizer. For reference, right now we emit LLVM IR that calls through the Julia PLT, like this:

@jl_RTLD_DEFAULT_handle = external constant ptr
@ccall_foo_216 = global ptr null
@jlplt_foo_217_got = global ptr @jlplt_foo_217 #0
@"_j_str_foo#1" = private unnamed_addr constant [4 x i8] c"foo\00", align 1

define i64 @julia_example_0(i64 signext %"x::Int64") #1 {
top:
  %pgcstack = call ptr @julia.get_pgcstack()
  %foo = load atomic ptr, ptr @jlplt_foo_217_got unordered, align 8
  %0 = call i64 %foo(i64 %"x::Int64")
  ret i64 %0
}

define private i64 @jlplt_foo_217(i64 %0) {
top:
  %1 = load atomic ptr, ptr @ccall_foo_216 unordered, align 8
  %is_cached = icmp ne ptr %1, null
  br i1 %is_cached, label %ccall, label %dlsym

dlsym:                                            ; preds = %top
  %foo.found = call ptr @ijl_load_and_lookup(ptr null, ptr @"_j_str_foo#1", ptr @jl_RTLD_DEFAULT_handle)
  store atomic ptr %foo.found, ptr @ccall_foo_216 release, align 8
  br label %ccall

ccall:                                            ; preds = %dlsym, %top
  %2 = phi ptr [ %1, %top ], [ %foo.found, %dlsym ]
  store atomic ptr %2, ptr @jlplt_foo_217_got release, align 8
  %3 = call i64 %2(i64 %0)
  ret i64 %3
}

DLSymOptimizer initializes GOT with a constant:

@jl_RTLD_DEFAULT_handle = external constant ptr
@ccall_foo_216 = global ptr null
@jlplt_foo_217_got = unnamed_addr constant ptr @jlplt_foo_217_got.jit #0
@"_j_str_foo#1" = private unnamed_addr constant [4 x i8] c"foo\00", align 1

@jlplt_foo_217_got.jit = private alias ptr, inttoptr (i64 7008263280 to ptr)

define i64 @julia_example_0(i64 signext %"x::Int64") #1 {
top:
  %pgcstack = call ptr @julia.get_pgcstack()
  %foo = load atomic ptr, ptr @jlplt_foo_217_got unordered, align 8
  %0 = call i64 %foo(i64 %"x::Int64")
  ret i64 %0
}

Where 7008263280 is the address of foo that was resolved in the current process. This causes the IR hash to depend on the address space layout, making it impossible to cache LLVM compilation across sessions.

This PR implements one approach to making the IR predictable, on suppported platforms (currently, only aarch64). In the JIT, we emit direct ccalls, without loading from a Julia GOT, and track them in a table on jl_codegen_output_t. When linking, we resolve these ccall symbols to their targets directly if possible, falling back to lazy symbol that will try resolving the symbol again when it's first called.

We use vendored versions of LazyReexportsManager and RedirectableSymbolManager that support our cooperative task scheduler. The LazyReexportsManager is also modified so we can throw a Julia exception instead of materializing the destination symbol, which is required to preserve our ccall semantics (preserving the illusion that the dlsym happens at runtime).

On this branch, the foo example looks like this:

define i64 @julia_example_0(i64 signext %"x::Int64") #0 {
top:
  %pgcstack = call ptr @julia.get_pgcstack()
  %0 = call i64 @foo_0(i64 %"x::Int64")
  ret i64 %0
}

Diff: 917 lines (+869 / -48) across 8 files Reviewer: topolarity Review duration: 341265m
All Vouch review confidence checks passed
Residual risk 0.08 = Change Risk (0.60) × (1 − Review Confidence (0.87)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.60 · 917 lines diff across 8 files
0.60
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.94 · 4 substantive comment(s)
0.94
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
86%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Real PR Conversation Timeline
5 items
vchuravy vchuravy commented on Mar 19, 2026 07:33 UTC
Contributor

I assume this is aarch64 only because one would need to write the assembly for the other platforms?

I am okay with vendoring code from LLVM, but I would like to have a clear strategy on up streaming these changes. That helps us update to new LLVM versions and avoids us missing biu fixes.

Enzyme currently relies in being able to decode the ccall emissions we perform, to assign semantic meaning to those calls. Similarly AllocCheck and other GPUCompiler based tooling rely on being able to roundtrip LLVM modules without having a side table somewhere else.

So we would need a way to access that side table or serialize it into the LLVM module.

xal-0 xal-0 commented on Mar 19, 2026 18:12 UTC
Author

I assume this is aarch64 only because one would need to write the assembly for the other platforms?

Yep. In reality, I'm not so interested in lazy ORC materialization for this specific use, and wanted to get a version working so I can experiment with speculatively compiling on idle threads.

I am okay with vendoring code from LLVM, but I would like to have a clear strategy on up streaming these changes. That helps us update to new LLVM versions and avoids us missing biu fixes.

I've started upstreaming some LLVM changes (places where we need to be using asynchronous ORC lookups), but the modified LazyReexportsManager that can fail materialization is not something I expect they'd want. I'm also not sure I want it, now that I have the prototype in front of me. Expect another draft PR with a nicer approach sometime soon.

Enzyme currently relies in being able to decode the ccall emissions we perform, to assign semantic meaning to those calls. Similarly AllocCheck and other GPUCompiler based tooling rely on being able to roundtrip LLVM modules without having a side table somewhere else.

> So we would need a way to access that side table or serialize it into the LLVM module.

Agreed. I like the side tables in jl_linker_info_t more than the previous approach for tracking invoke names with global tables in jitlayers.cpp, but it's not ideal for stuff that doesn't depend on the running session. For a future version I'll try repurposing the julia.fname/julia.libname/julia.libidx attributes we already put on the julia GOT variables.

topolarity topolarity commented on code on Mar 26, 2026 15:18 UTC
Code Review
src/jitlayers.cpp:1
@@ -1575,45 +1589,6 @@ struct JuliaOJIT::DLSymOptimizer {
     }
 
     void operator()(Module &M) JL_NOTSAFEPOINT_LEAVE JL_NOTSAFEPOINT_ENTER {
-        for (auto &GV : M.globals()) {
-            auto Name = GV.getName();
-            if (Name.starts_with("jlplt") && Name.ends_with("got")) {
-                auto fname = GV.getAttribute("julia.fname").getValueAsString().str();
-                void *addr;
-                if (GV.hasAttribute("julia.libname")) {
-                    auto libname = GV.getAttribute("julia.libname").getValueAsString().str();
-                    addr = lookup(libname.data(), fname.data());
-                } else {
-                    assert(GV.hasAttribute("julia.libidx") && "PLT entry should have either libname or libidx attribute!");
-                    auto libidx = (uintptr_t)std::stoull(GV.getAttribute("julia.libidx").getValueAsString().str());
-                    addr = lookup(libidx, fname.data());
-                }
-                if (addr) {
-                    Function *Thunk = nullptr;
-                    if (!GV.isDeclaration()) {
-                        Thunk = cast<Function>(GV.getInitializer()->stripPointerCasts());
-                        assert(++Thunk->uses().begin() == Thunk->uses().end() && "Thunk should only have one use in PLT initializer!");
-                        assert(Thunk->hasLocalLinkage() && "Thunk should not have non-local linkage!");
-                    }
-                    else {
-                        GV.setLinkage(GlobalValue::PrivateLinkage);
-                    }
-                    auto init = ConstantExpr::getIntToPtr(ConstantInt::get(M.getDataLayout().getIntPtrType(M.getContext()), (uintptr_t)addr), GV.getValueType());
-                    if (named) {
-                        auto T = GV.getValueType();
-                        assert(T->isPointerTy());
-                        init = GlobalAlias::create(T, 0, GlobalValue::PrivateLinkage, GV.getName() + ".jit", init, &M);
-                    }
-                    GV.setInitializer(init);
-                    GV.setConstant(true);
-                    GV.setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
-                    if (Thunk) {
-                        Thunk->eraseFromParent();
-                    }
-                }
-            }
-        }
-
         for (auto &F : M) {

Why is this part of DLSymOptimizer kept? The lookup is just as problematic, isn't it?

xal-0 xal-0 commented on code on Mar 26, 2026 16:59 UTC
Code Review
src/jitlayers.cpp:1
@@ -1575,45 +1589,6 @@ struct JuliaOJIT::DLSymOptimizer {
     }
 
     void operator()(Module &M) JL_NOTSAFEPOINT_LEAVE JL_NOTSAFEPOINT_ENTER {
-        for (auto &GV : M.globals()) {
-            auto Name = GV.getName();
-            if (Name.starts_with("jlplt") && Name.ends_with("got")) {
-                auto fname = GV.getAttribute("julia.fname").getValueAsString().str();
-                void *addr;
-                if (GV.hasAttribute("julia.libname")) {
-                    auto libname = GV.getAttribute("julia.libname").getValueAsString().str();
-                    addr = lookup(libname.data(), fname.data());
-                } else {
-                    assert(GV.hasAttribute("julia.libidx") && "PLT entry should have either libname or libidx attribute!");
-                    auto libidx = (uintptr_t)std::stoull(GV.getAttribute("julia.libidx").getValueAsString().str());
-                    addr = lookup(libidx, fname.data());
-                }
-                if (addr) {
-                    Function *Thunk = nullptr;
-                    if (!GV.isDeclaration()) {
-                        Thunk = cast<Function>(GV.getInitializer()->stripPointerCasts());
-                        assert(++Thunk->uses().begin() == Thunk->uses().end() && "Thunk should only have one use in PLT initializer!");
-                        assert(Thunk->hasLocalLinkage() && "Thunk should not have non-local linkage!");
-                    }
-                    else {
-                        GV.setLinkage(GlobalValue::PrivateLinkage);
-                    }
-                    auto init = ConstantExpr::getIntToPtr(ConstantInt::get(M.getDataLayout().getIntPtrType(M.getContext()), (uintptr_t)addr), GV.getValueType());
-                    if (named) {
-                        auto T = GV.getValueType();
-                        assert(T->isPointerTy());
-                        init = GlobalAlias::create(T, 0, GlobalValue::PrivateLinkage, GV.getName() + ".jit", init, &M);
-                    }
-                    GV.setInitializer(init);
-                    GV.setConstant(true);
-                    GV.setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
-                    if (Thunk) {
-                        Thunk->eraseFromParent();
-                    }
-                }
-            }
-        }
-
         for (auto &F : M) {

It is, but this branch doesn't provide an alternative to DLSymOptimizer for cglobal.

Reviewers
Assignees
xal-0 xal-0
Labels
risk: low confidence: 86.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 60.2% (MEDIUM) · Higher = larger blast radius
60.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 86.7% · Lower = rushed or shallow review
86.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 8% · Safe to merge
8%