Skip to content

Commit 1ac1583

Browse files
committed
cancellation: match GC intrinsics by identity, not by name
CancellationLowering matched `julia.gc_alloc_obj`, the write barriers, `julia.pointer_from_objref`, and `julia.gc_loaded` by scanning callee names, including a substring match that any function containing "write_barrier" would satisfy. Inherit JuliaPassContext and compare against its function pointers through the family predicates instead. The setjmp/safepoint skip keeps its name match: it pairs a C symbol with calls this pass itself creates mid-run, before any declaration necessarily exists at context-initialization time. Assisted-by: Claude Code (Fable 5)
1 parent 5d476f4 commit 1ac1583

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

src/llvm-cancellation-lowering.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ static bool isImplicitRuntimeCall(CallInst *CI) {
9898

9999
namespace {
100100

101-
struct CancellationLowering {
102-
Function *cancel_point_func;
101+
struct CancellationLowering : public JuliaPassContext {
103102
Value *pgcstack;
104103
Value *reset_ctx_ptr; // Computed once in entry block, dominates all uses
105104
Value *ptls_field_ptr; // Pointer to task->ptls, computed alongside reset_ctx_ptr
106105
Value *eh_field_ptr; // Pointer to task->eh, computed alongside reset_ctx_ptr
107106

108-
CancellationLowering(Module &M) : cancel_point_func(nullptr), pgcstack(nullptr), reset_ctx_ptr(nullptr), ptls_field_ptr(nullptr), eh_field_ptr(nullptr) {
109-
cancel_point_func = M.getFunction("julia.cancellation_point");
107+
CancellationLowering(Module &M) : pgcstack(nullptr), reset_ctx_ptr(nullptr), ptls_field_ptr(nullptr), eh_field_ptr(nullptr) {
108+
initFunctions(M);
110109
}
111110

112111
bool runOnFunction(Function &F);
@@ -172,8 +171,6 @@ bool CancellationLowering::runOnFunction(Function &F) {
172171
reset_ctx_ptr = nullptr;
173172
eh_field_ptr = nullptr;
174173
Instruction *pgcstack_inst = nullptr; // Only set if pgcstack is from a call, not an argument
175-
Function *pgcstack_getter = F.getParent()->getFunction("julia.get_pgcstack");
176-
Function *adoptthread_func = F.getParent()->getFunction("julia.get_pgcstack_or_new");
177174
if (pgcstack_getter || adoptthread_func) {
178175
for (auto &I : F.getEntryBlock()) {
179176
if (CallInst *callInst = dyn_cast<CallInst>(&I)) {
@@ -574,18 +571,15 @@ bool CancellationLowering::runOnFunction(Function &F) {
574571
// argument-conversion glue (unsafe_convert of a mutable
575572
// object), and must not invalidate a reset region
576573
// published across an adjacent reset-safe foreign call.
577-
if (Callee && (Callee->getName() == "julia.pointer_from_objref" ||
578-
Callee->getName() == "julia.gc_loaded"))
574+
if (Callee && (Callee == pointer_from_objref_func || Callee == gc_loaded_func))
579575
continue;
580576
// Allocations and write barriers are safe to span:
581577
// FinalLowerGC (stock and MMTk) lowers annotated sites
582578
// to the *_reset_safe runtime entry points, which
583579
// unpublish the region around the operation and
584580
// republish it on the way out (so the region even
585581
// survives the operation).
586-
if (Callee && (Callee->getName() == "julia.gc_alloc_obj" ||
587-
Callee->getName() == "julia.object_write_barrier" ||
588-
Callee->getName().starts_with("julia.field_write_barrier")))
582+
if (Callee && (Callee == alloc_obj_func || isWriteBarrierFunc(Callee)))
589583
continue;
590584
UnsafePoints.push_back(CI);
591585
}
@@ -689,10 +683,7 @@ bool CancellationLowering::runOnFunction(Function &F) {
689683
Function *Callee = CI->getCalledFunction();
690684
if (!Callee)
691685
continue;
692-
StringRef Name = Callee->getName();
693-
if (region_open && (Name == "julia.gc_alloc_obj" ||
694-
Name == "julia.object_write_barrier" ||
695-
Name.starts_with("julia.field_write_barrier"))) {
686+
if (region_open && (Callee == alloc_obj_func || isWriteBarrierFunc(Callee))) {
696687
CI->setMetadata("julia.reset_region", MDNode::get(F.getContext(), {}));
697688
Changed = true;
698689
}

0 commit comments

Comments
 (0)