Skip to content

Commit 39aff78

Browse files
committed
Name LLVM function arguments
1 parent a134076 commit 39aff78

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

src/codegen.cpp

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ jl_aliasinfo_t jl_aliasinfo_t::fromTBAA(jl_codectx_t &ctx, MDNode *tbaa) {
17361736
}
17371737

17381738
static Type *julia_type_to_llvm(jl_codectx_t &ctx, jl_value_t *jt, bool *isboxed = NULL);
1739-
static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value *fval, StringRef name, jl_value_t *sig, jl_value_t *jlrettype, bool is_opaque_closure, bool gcstack_arg);
1739+
static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value *fval, StringRef name, jl_value_t *sig, jl_value_t *jlrettype, bool is_opaque_closure, bool gcstack_arg, BitVector *used_arguments=nullptr, size_t *args_begin=nullptr);
17401740
static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr, ssize_t ssaval = -1);
17411741
static Value *global_binding_pointer(jl_codectx_t &ctx, jl_module_t *m, jl_sym_t *s,
17421742
jl_binding_t **pbnd, bool assign);
@@ -2363,16 +2363,16 @@ std::unique_ptr<Module> jl_create_llvm_module(StringRef name, LLVMContext &conte
23632363

23642364
static void jl_name_jlfunc_args(jl_codegen_params_t &params, Function *F) {
23652365
assert(F->arg_size() == 3);
2366-
F->getArg(0)->setName("function");
2367-
F->getArg(1)->setName("args");
2368-
F->getArg(2)->setName("nargs");
2366+
F->getArg(0)->setName("function::Core.Function");
2367+
F->getArg(1)->setName("args::Any[]");
2368+
F->getArg(2)->setName("nargs::UInt");
23692369
}
23702370

23712371
static void jl_name_jlfuncparams_args(jl_codegen_params_t &params, Function *F) {
23722372
assert(F->arg_size() == 4);
2373-
F->getArg(0)->setName("function");
2374-
F->getArg(1)->setName("args");
2375-
F->getArg(2)->setName("nargs");
2373+
F->getArg(0)->setName("function::Core.Function");
2374+
F->getArg(1)->setName("args::Any[]");
2375+
F->getArg(2)->setName("nargs::UInt32");
23762376
F->getArg(3)->setName("sparams");
23772377
}
23782378

@@ -4359,7 +4359,7 @@ static jl_cgval_t emit_call_specfun_boxed(jl_codectx_t &ctx, jl_value_t *jlretty
43594359
}
43604360
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_const);
43614361
theFptr = ai.decorateInst(ctx.builder.CreateAlignedLoad(pfunc, GV, Align(sizeof(void*))));
4362-
setName(ctx.emission_context, theFptr, namep);
4362+
setName(ctx.emission_context, theFptr, specFunctionObject);
43634363
}
43644364
else {
43654365
theFptr = jl_Module->getOrInsertFunction(specFunctionObject, ctx.types().T_jlfunc).getCallee();
@@ -6935,10 +6935,11 @@ static Function *gen_invoke_wrapper(jl_method_instance_t *lam, jl_value_t *jlret
69356935
return w;
69366936
}
69376937

6938-
static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value *fval, StringRef name, jl_value_t *sig, jl_value_t *jlrettype, bool is_opaque_closure, bool gcstack_arg)
6938+
static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value *fval, StringRef name, jl_value_t *sig, jl_value_t *jlrettype, bool is_opaque_closure, bool gcstack_arg, BitVector *used_arguments, size_t *arg_offset)
69396939
{
69406940
jl_returninfo_t props = {};
69416941
SmallVector<Type*, 8> fsig;
6942+
SmallVector<std::string, 4> argnames;
69426943
Type *rt = NULL;
69436944
Type *srt = NULL;
69446945
if (jlrettype == (jl_value_t*)jl_bottom_type) {
@@ -6956,6 +6957,7 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
69566957
props.cc = jl_returninfo_t::Union;
69576958
Type *AT = ArrayType::get(getInt8Ty(ctx.builder.getContext()), props.union_bytes);
69586959
fsig.push_back(AT->getPointerTo());
6960+
argnames.push_back("union_bytes_return");
69596961
Type *pair[] = { ctx.types().T_prjlvalue, getInt8Ty(ctx.builder.getContext()) };
69606962
rt = StructType::get(ctx.builder.getContext(), makeArrayRef(pair));
69616963
}
@@ -6980,6 +6982,7 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
69806982
// sret is always passed from alloca
69816983
assert(M);
69826984
fsig.push_back(rt->getPointerTo(M->getDataLayout().getAllocaAddrSpace()));
6985+
argnames.push_back("sret_return");
69836986
srt = rt;
69846987
rt = getVoidTy(ctx.builder.getContext());
69856988
}
@@ -7018,6 +7021,7 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
70187021
param.addAttribute(Attribute::NoUndef);
70197022
attrs.push_back(AttributeSet::get(ctx.builder.getContext(), param));
70207023
fsig.push_back(get_returnroots_type(ctx, props.return_roots)->getPointerTo(0));
7024+
argnames.push_back("return_roots");
70217025
}
70227026

70237027
if (gcstack_arg){
@@ -7026,9 +7030,16 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
70267030
param.addAttribute(Attribute::NonNull);
70277031
attrs.push_back(AttributeSet::get(ctx.builder.getContext(), param));
70287032
fsig.push_back(PointerType::get(JuliaType::get_ppjlvalue_ty(ctx.builder.getContext()), 0));
7033+
argnames.push_back("pgcstack_arg");
70297034
}
70307035

7031-
for (size_t i = 0; i < jl_nparams(sig); i++) {
7036+
if (arg_offset)
7037+
*arg_offset = fsig.size();
7038+
size_t nparams = jl_nparams(sig);
7039+
if (used_arguments)
7040+
used_arguments->resize(nparams);
7041+
7042+
for (size_t i = 0; i < nparams; i++) {
70327043
jl_value_t *jt = jl_tparam(sig, i);
70337044
bool isboxed = false;
70347045
Type *ty = NULL;
@@ -7060,6 +7071,8 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
70607071
}
70617072
attrs.push_back(AttributeSet::get(ctx.builder.getContext(), param));
70627073
fsig.push_back(ty);
7074+
if (used_arguments)
7075+
used_arguments->set(i);
70637076
}
70647077

70657078
AttributeSet FnAttrs;
@@ -7089,8 +7102,14 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
70897102
else
70907103
fval = emit_bitcast(ctx, fval, ftype->getPointerTo());
70917104
}
7092-
if (gcstack_arg && isa<Function>(fval))
7093-
cast<Function>(fval)->setCallingConv(CallingConv::Swift);
7105+
if (auto F = dyn_cast<Function>(fval)) {
7106+
if (gcstack_arg)
7107+
F->setCallingConv(CallingConv::Swift);
7108+
assert(F->arg_size() >= argnames.size());
7109+
for (size_t i = 0; i < argnames.size(); i++) {
7110+
F->getArg(i)->setName(argnames[i]);
7111+
}
7112+
}
70947113
props.decl = FunctionCallee(ftype, fval);
70957114
props.attrs = attributes;
70967115
return props;
@@ -7316,11 +7335,43 @@ static jl_llvm_functions_t
73167335
Function *f = NULL;
73177336
bool has_sret = false;
73187337
if (specsig) { // assumes !va and !needsparams
7338+
BitVector used_args;
7339+
size_t args_begin;
73197340
returninfo = get_specsig_function(ctx, M, NULL, declarations.specFunctionObject, lam->specTypes,
7320-
jlrettype, ctx.is_opaque_closure, JL_FEAT_TEST(ctx,gcstack_arg));
7341+
jlrettype, ctx.is_opaque_closure, JL_FEAT_TEST(ctx,gcstack_arg), &used_args, &args_begin);
73217342
f = cast<Function>(returninfo.decl.getCallee());
73227343
has_sret = (returninfo.cc == jl_returninfo_t::SRet || returninfo.cc == jl_returninfo_t::Union);
73237344
jl_init_function(f, ctx.emission_context.TargetTriple);
7345+
auto arg_typename = [&](size_t i) JL_NOTSAFEPOINT { return jl_symbol_name(((jl_datatype_t*)jl_tparam(lam->specTypes, i))->name->name); };
7346+
size_t nreal = 0;
7347+
for (size_t i = 0; i < std::min(nreq, static_cast<size_t>(used_args.size())); i++) {
7348+
jl_sym_t *argname = slot_symbol(ctx, i);
7349+
if (argname == jl_unused_sym)
7350+
continue;
7351+
if (used_args.test(i)) {
7352+
auto &arg = *f->getArg(args_begin++);
7353+
nreal++;
7354+
auto name = jl_symbol_name(argname);
7355+
if (!name[0]) {
7356+
arg.setName(StringRef("#") + Twine(nreal) + StringRef("::") + arg_typename(i));
7357+
} else {
7358+
arg.setName(name + StringRef("::") + arg_typename(i));
7359+
}
7360+
}
7361+
}
7362+
if (va && ctx.vaSlot != -1) {
7363+
size_t vidx = 0;
7364+
for (size_t i = nreq; i < used_args.size(); i++) {
7365+
if (used_args.test(i)) {
7366+
auto &arg = *f->getArg(args_begin++);
7367+
auto type = arg_typename(i);
7368+
const char *name = jl_symbol_name(slot_symbol(ctx, ctx.vaSlot));
7369+
if (!name[0])
7370+
name = "...";
7371+
arg.setName(name + StringRef("[") + Twine(vidx++) + StringRef("]::") + type);
7372+
}
7373+
}
7374+
}
73247375

73257376
// common pattern: see if all return statements are an argument in that
73267377
// case the apply-generic call can re-use the original box for the return

0 commit comments

Comments
 (0)