Skip to content

Commit

Permalink
aotcompile: add missing codegen support for OC
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Nov 12, 2024
1 parent fa9f0de commit 8f24144
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
// Const returns do not do codegen, but juliac inspects codegen results so make a dummy fvar entry to represent it
if (jl_options.trim != JL_TRIM_NO && jl_atomic_load_relaxed(&codeinst->invoke) == jl_fptr_const_return_addr) {
data->jl_fvar_map[codeinst] = std::make_tuple((uint32_t)-3, (uint32_t)-3);
} else {
}
else {
JL_GC_PROMISE_ROOTED(codeinst->rettype);
orc::ThreadSafeModule result_m = jl_create_ts_module(name_from_method_instance(codeinst->def),
params.tsctx, clone.getModuleUnlocked()->getDataLayout(),
Expand Down Expand Up @@ -609,6 +610,9 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
else if (func == "jl_fptr_sparam") {
func_id = -2;
}
else if (decls.functionObject == "jl_f_opaque_closure_call") {
func_id = -4;
}
else {
//Safe b/c context is locked by params
data->jl_sysimg_fvars.push_back(cast<Function>(clone.getModuleUnlocked()->getNamedValue(func)));
Expand Down Expand Up @@ -904,11 +908,13 @@ static bool canPartition(const GlobalValue &G) {
return true;
}

static inline bool verify_partitioning(const SmallVectorImpl<Partition> &partitions, const Module &M, size_t fvars_size, size_t gvars_size) {
static inline bool verify_partitioning(const SmallVectorImpl<Partition> &partitions, const Module &M, DenseMap<GlobalValue *, unsigned> &fvars, DenseMap<GlobalValue *, unsigned> &gvars) {
bool bad = false;
#ifndef JL_NDEBUG
SmallVector<uint32_t, 0> fvars(fvars_size);
SmallVector<uint32_t, 0> gvars(gvars_size);
size_t fvars_size = fvars.size();
size_t gvars_size = gvars.size();
SmallVector<uint32_t, 0> fvars_partition(fvars_size);
SmallVector<uint32_t, 0> gvars_partition(gvars_size);
StringMap<uint32_t> GVNames;
for (uint32_t i = 0; i < partitions.size(); i++) {
for (auto &name : partitions[i].globals) {
Expand All @@ -919,18 +925,18 @@ static inline bool verify_partitioning(const SmallVectorImpl<Partition> &partiti
GVNames[name.getKey()] = i;
}
for (auto &fvar : partitions[i].fvars) {
if (fvars[fvar.second] != 0) {
if (fvars_partition[fvar.second] != 0) {
bad = true;
dbgs() << "Duplicate fvar " << fvar.first() << " in partitions " << i << " and " << fvars[fvar.second] - 1 << "\n";
dbgs() << "Duplicate fvar " << fvar.first() << " in partitions " << i << " and " << fvars_partition[fvar.second] - 1 << "\n";
}
fvars[fvar.second] = i+1;
fvars_partition[fvar.second] = i+1;
}
for (auto &gvar : partitions[i].gvars) {
if (gvars[gvar.second] != 0) {
if (gvars_partition[gvar.second] != 0) {
bad = true;
dbgs() << "Duplicate gvar " << gvar.first() << " in partitions " << i << " and " << gvars[gvar.second] - 1 << "\n";
dbgs() << "Duplicate gvar " << gvar.first() << " in partitions " << i << " and " << gvars_partition[gvar.second] - 1 << "\n";
}
gvars[gvar.second] = i+1;
gvars_partition[gvar.second] = i+1;
}
}
for (auto &GV : M.global_values()) {
Expand Down Expand Up @@ -967,13 +973,14 @@ static inline bool verify_partitioning(const SmallVectorImpl<Partition> &partiti
}
}
for (uint32_t i = 0; i < fvars_size; i++) {
if (fvars[i] == 0) {
if (fvars_partition[i] == 0) {
auto gv = find_if(fvars.begin(), fvars.end(), [i](auto var) { return var.second == i; });
bad = true;
dbgs() << "fvar " << i << " not in any partition\n";
dbgs() << "fvar " << gv->first->getName() << " at " << i << " not in any partition\n";
}
}
for (uint32_t i = 0; i < gvars_size; i++) {
if (gvars[i] == 0) {
if (gvars_partition[i] == 0) {
bad = true;
dbgs() << "gvar " << i << " not in any partition\n";
}
Expand Down Expand Up @@ -1117,7 +1124,9 @@ static SmallVector<Partition, 32> partitionModule(Module &M, unsigned threads) {
}
}

bool verified = verify_partitioning(partitions, M, fvars.size(), gvars.size());
bool verified = verify_partitioning(partitions, M, fvars, gvars);
if (!verified)
M.dump();

This comment has been minimized.

Copy link
@yuyichao

yuyichao Nov 17, 2024

Contributor

This needs to be llvm_dump(&M) instead to support non-assert build of llvm.

assert(verified && "Partitioning failed to partition globals correctly");
(void) verified;

Expand Down
13 changes: 12 additions & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ typedef enum {
JL_API_BOXED,
JL_API_CONST,
JL_API_WITH_PARAMETERS,
JL_API_OC_CALL,
JL_API_INTERPRETED,
JL_API_BUILTIN,
JL_API_MAX
Expand Down Expand Up @@ -1797,6 +1798,12 @@ static void jl_write_values(jl_serializer_state *s) JL_GC_DISABLED
else if (invokeptr_id == -2) {
fptr_id = JL_API_WITH_PARAMETERS;
}
else if (invokeptr_id == -3) {
abort();
}
else if (invokeptr_id == -4) {
fptr_id = JL_API_OC_CALL;
}
else {
assert(invokeptr_id > 0);
ios_ensureroom(s->fptr_record, invokeptr_id * sizeof(void*));
Expand Down Expand Up @@ -2033,11 +2040,15 @@ static inline uintptr_t get_item_for_reloc(jl_serializer_state *s, uintptr_t bas
case JL_API_BOXED:
if (s->image->fptrs.nptrs)
return (uintptr_t)jl_fptr_args;
JL_FALLTHROUGH;
return (uintptr_t)NULL;
case JL_API_WITH_PARAMETERS:
if (s->image->fptrs.nptrs)
return (uintptr_t)jl_fptr_sparam;
return (uintptr_t)NULL;
case JL_API_OC_CALL:
if (s->image->fptrs.nptrs)
return (uintptr_t)jl_f_opaque_closure_call;
return (uintptr_t)NULL;
case JL_API_CONST:
return (uintptr_t)jl_fptr_const_return;
case JL_API_INTERPRETED:
Expand Down

0 comments on commit 8f24144

Please sign in to comment.