Skip to content

Commit

Permalink
Clean up various naming aspects of codegen (#50638)
Browse files Browse the repository at this point in the history
Most of the changes are changing from `const std::string &` to `const
Twine &`, which lets us be lazier about computing string values
efficiently. We also make the plt function private linkage since it's
only referred to by the plt global.

---------

Co-authored-by: Jameson Nash <vtjnash@gmail.com>
  • Loading branch information
pchintalapudi and vtjnash committed Jul 26, 2023
1 parent 9c6efd6 commit 8c3452f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static GlobalVariable *emit_plt_thunk(
std::string fname;
raw_string_ostream(fname) << "jlplt_" << f_name << "_" << jl_atomic_fetch_add(&globalUniqueGeneratedNames, 1);
Function *plt = Function::Create(functype,
GlobalVariable::ExternalLinkage,
GlobalVariable::PrivateLinkage,
fname, M);
plt->setAttributes(attrs);
if (cc != CallingConv::C)
Expand Down
61 changes: 35 additions & 26 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ AtomicOrdering get_llvm_atomic_order(enum jl_memory_order order)
static Value *stringConstPtr(
jl_codegen_params_t &emission_context,
IRBuilder<> &irbuilder,
const std::string &txt)
const Twine &txt)
{
Module *M = jl_builderModule(irbuilder);
StringRef ctxt(txt.c_str(), txt.size() + 1);
Constant *Data = ConstantDataArray::get(irbuilder.getContext(), arrayRefFromStringRef(ctxt));
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str", *M);
SmallVector<char, 128> ctxt;
txt.toVector(ctxt);
// null-terminate the string
ctxt.push_back(0);
Constant *Data = ConstantDataArray::get(irbuilder.getContext(), ctxt);
ctxt.pop_back();
// We use this for the name of the gv, so cap its size to avoid memory blowout
if (ctxt.size() > 28) {
ctxt.resize(28);
ctxt[25] = ctxt[26] = ctxt[27] = '.';
}
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
Value *zero = ConstantInt::get(Type::getInt32Ty(irbuilder.getContext()), 0);
Value *Args[] = { zero, zero };
auto gep = irbuilder.CreateInBoundsGEP(gv->getValueType(),
Expand Down Expand Up @@ -1097,7 +1106,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
if (justtag && jt->smalltag) {
ptr = ConstantInt::get(expr_type, jt->smalltag << 4);
if (ctx.emission_context.imaging)
ptr = get_pointer_to_constant(ctx.emission_context, ptr, "_j_tag", *jl_Module);
ptr = get_pointer_to_constant(ctx.emission_context, ptr, StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
}
else if (ctx.emission_context.imaging)
ptr = ConstantExpr::getBitCast(literal_pointer_val_slot(ctx, (jl_value_t*)jt), datatype_or_p->getType());
Expand Down Expand Up @@ -1278,27 +1287,27 @@ static Value *emit_datatype_name(jl_codectx_t &ctx, Value *dt)
// the error is always thrown. This may cause non dominated use
// of SSA value error in the verifier.

static void just_emit_error(jl_codectx_t &ctx, Function *F, const std::string &txt)
static void just_emit_error(jl_codectx_t &ctx, Function *F, const Twine &txt)
{
++EmittedErrors;
ctx.builder.CreateCall(F, stringConstPtr(ctx.emission_context, ctx.builder, txt));
}

static void emit_error(jl_codectx_t &ctx, Function *F, const std::string &txt)
static void emit_error(jl_codectx_t &ctx, Function *F, const Twine &txt)
{
just_emit_error(ctx, F, txt);
ctx.builder.CreateUnreachable();
BasicBlock *cont = BasicBlock::Create(ctx.builder.getContext(), "after_error", ctx.f);
ctx.builder.SetInsertPoint(cont);
}

static void emit_error(jl_codectx_t &ctx, const std::string &txt)
static void emit_error(jl_codectx_t &ctx, const Twine &txt)
{
emit_error(ctx, prepare_call(jlerror_func), txt);
}

// DO NOT PASS IN A CONST CONDITION!
static void error_unless(jl_codectx_t &ctx, Value *cond, const std::string &msg)
static void error_unless(jl_codectx_t &ctx, Value *cond, const Twine &msg)
{
++EmittedConditionalErrors;
BasicBlock *failBB = BasicBlock::Create(ctx.builder.getContext(), "fail", ctx.f);
Expand Down Expand Up @@ -1451,14 +1460,14 @@ static Value *emit_typeof(jl_codectx_t &ctx, Value *v, bool maybenull, bool just

static Value *boxed(jl_codectx_t &ctx, const jl_cgval_t &v, bool is_promotable=false);

static void just_emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const std::string &msg)
static void just_emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const Twine &msg)
{
Value *msg_val = stringConstPtr(ctx.emission_context, ctx.builder, msg);
ctx.builder.CreateCall(prepare_call(jltypeerror_func),
{ msg_val, maybe_decay_untracked(ctx, type), mark_callee_rooted(ctx, boxed(ctx, x))});
}

static void emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const std::string &msg)
static void emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const Twine &msg)
{
just_emit_type_error(ctx, x, type, msg);
ctx.builder.CreateUnreachable();
Expand Down Expand Up @@ -1538,7 +1547,7 @@ static Value *emit_exactly_isa(jl_codectx_t &ctx, const jl_cgval_t &arg, jl_data
}

static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
jl_value_t *type, const std::string *msg);
jl_value_t *type, const Twine &msg);

static void emit_isa_union(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type,
SmallVectorImpl<std::pair<std::pair<BasicBlock*,BasicBlock*>,Value*>> &bbs)
Expand All @@ -1550,15 +1559,15 @@ static void emit_isa_union(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *t
return;
}
BasicBlock *enter = ctx.builder.GetInsertBlock();
Value *v = emit_isa(ctx, x, type, nullptr).first;
Value *v = emit_isa(ctx, x, type, Twine()).first;
BasicBlock *exit = ctx.builder.GetInsertBlock();
bbs.emplace_back(std::make_pair(enter, exit), v);
BasicBlock *isaBB = BasicBlock::Create(ctx.builder.getContext(), "isa", ctx.f);
ctx.builder.SetInsertPoint(isaBB);
}

// Should agree with `_can_optimize_isa` above
static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const std::string *msg)
static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const Twine &msg)
{
++EmittedIsa;
// TODO: The subtype check below suffers from incorrectness issues due to broken
Expand All @@ -1578,8 +1587,8 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
known_isa = false;
}
if (known_isa) {
if (!*known_isa && msg) {
emit_type_error(ctx, x, literal_pointer_val(ctx, type), *msg);
if (!*known_isa && !msg.isTriviallyEmpty()) {
emit_type_error(ctx, x, literal_pointer_val(ctx, type), msg);
}
return std::make_pair(ConstantInt::get(getInt1Ty(ctx.builder.getContext()), *known_isa), true);
}
Expand Down Expand Up @@ -1611,7 +1620,7 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
if (jl_has_intersect_type_not_kind(type) || jl_has_intersect_type_not_kind(intersected_type)) {
Value *vx = boxed(ctx, x);
Value *vtyp = track_pjlvalue(ctx, literal_pointer_val(ctx, type));
if (msg && *msg == "typeassert") {
if (msg.isSingleStringRef() && msg.getSingleStringRef() == "typeassert") {
ctx.builder.CreateCall(prepare_call(jltypeassert_func), { vx, vtyp });
return std::make_pair(ConstantInt::get(getInt1Ty(ctx.builder.getContext()), 1), true);
}
Expand Down Expand Up @@ -1672,16 +1681,16 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
static Value *emit_isa_and_defined(jl_codectx_t &ctx, const jl_cgval_t &val, jl_value_t *typ)
{
return emit_nullcheck_guard(ctx, val.ispointer() ? val.V : nullptr, [&] {
return emit_isa(ctx, val, typ, nullptr).first;
return emit_isa(ctx, val, typ, Twine()).first;
});
}


static void emit_typecheck(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const std::string &msg)
static void emit_typecheck(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const Twine &msg)
{
Value *istype;
bool handled_msg;
std::tie(istype, handled_msg) = emit_isa(ctx, x, type, &msg);
std::tie(istype, handled_msg) = emit_isa(ctx, x, type, msg);
if (!handled_msg) {
++EmittedTypechecks;
BasicBlock *failBB = BasicBlock::Create(ctx.builder.getContext(), "fail", ctx.f);
Expand Down Expand Up @@ -1709,7 +1718,7 @@ static Value *emit_isconcrete(jl_codectx_t &ctx, Value *typ)
return isconcrete;
}

static void emit_concretecheck(jl_codectx_t &ctx, Value *typ, const std::string &msg)
static void emit_concretecheck(jl_codectx_t &ctx, Value *typ, const Twine &msg)
{
++EmittedConcretechecks;
assert(typ->getType() == ctx.types().T_prjlvalue);
Expand Down Expand Up @@ -1930,7 +1939,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
Value *parent, // for the write barrier, NULL if no barrier needed
bool isboxed, AtomicOrdering Order, AtomicOrdering FailOrder, unsigned alignment,
bool needlock, bool issetfield, bool isreplacefield, bool isswapfield, bool ismodifyfield,
bool maybe_null_if_boxed, const jl_cgval_t *modifyop, const std::string &fname)
bool maybe_null_if_boxed, const jl_cgval_t *modifyop, const Twine &fname)
{
auto newval = [&](const jl_cgval_t &lhs) {
const jl_cgval_t argv[3] = { cmp, lhs, rhs };
Expand Down Expand Up @@ -2057,7 +2066,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
else if (!isboxed) {
assert(jl_is_concrete_type(jltype));
needloop = ((jl_datatype_t*)jltype)->layout->haspadding;
Value *SameType = emit_isa(ctx, cmp, jltype, nullptr).first;
Value *SameType = emit_isa(ctx, cmp, jltype, Twine()).first;
if (SameType != ConstantInt::getTrue(ctx.builder.getContext())) {
BasicBlock *SkipBB = BasicBlock::Create(ctx.builder.getContext(), "skip_xchg", ctx.f);
BasicBlock *BB = BasicBlock::Create(ctx.builder.getContext(), "ok_xchg", ctx.f);
Expand Down Expand Up @@ -2306,7 +2315,7 @@ static Value *julia_bool(jl_codectx_t &ctx, Value *cond)

// --- accessing the representations of built-in data types ---

static void emit_atomic_error(jl_codectx_t &ctx, const std::string &msg)
static void emit_atomic_error(jl_codectx_t &ctx, const Twine &msg)
{
emit_error(ctx, prepare_call(jlatomicerror_func), msg);
}
Expand Down Expand Up @@ -3668,7 +3677,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
}


static void emit_cpointercheck(jl_codectx_t &ctx, const jl_cgval_t &x, const std::string &msg)
static void emit_cpointercheck(jl_codectx_t &ctx, const jl_cgval_t &x, const Twine &msg)
{
++EmittedCPointerChecks;
Value *t = emit_typeof(ctx, x, false, false);
Expand Down Expand Up @@ -3776,7 +3785,7 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx,
jl_cgval_t rhs, jl_cgval_t cmp,
bool wb, AtomicOrdering Order, AtomicOrdering FailOrder,
bool needlock, bool issetfield, bool isreplacefield, bool isswapfield, bool ismodifyfield,
const jl_cgval_t *modifyop, const std::string &fname)
const jl_cgval_t *modifyop, const Twine &fname)
{
auto get_objname = [&]() {
return strct.V ? strct.V->getName() : StringRef("");
Expand Down
40 changes: 20 additions & 20 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ static Value *global_binding_pointer(jl_codectx_t &ctx, jl_module_t *m, jl_sym_t
jl_binding_t **pbnd, bool assign);
static jl_cgval_t emit_checked_var(jl_codectx_t &ctx, Value *bp, jl_sym_t *name, bool isvol, MDNode *tbaa);
static jl_cgval_t emit_sparam(jl_codectx_t &ctx, size_t i);
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const std::string &msg);
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const Twine &msg);
static Value *get_current_task(jl_codectx_t &ctx);
static Value *get_current_ptls(jl_codectx_t &ctx);
static Value *get_last_age_field(jl_codectx_t &ctx);
Expand Down Expand Up @@ -1811,31 +1811,31 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)

// --- convenience functions for tagging llvm values with julia types ---

static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, StringRef name, Module &M)
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, const Twine &name, Module &M)
{
GlobalVariable *&gv = emission_context.mergedConstants[val];
StringRef localname;
std::string ssno;
if (gv == nullptr) {
raw_string_ostream(ssno) << name << emission_context.mergedConstants.size();
localname = StringRef(ssno);
}
else {
localname = gv->getName();
if (gv->getParent() != &M)
gv = cast_or_null<GlobalVariable>(M.getNamedValue(localname));
}
if (gv == nullptr) {
gv = new GlobalVariable(
auto get_gv = [&](const Twine &name) {
auto gv = new GlobalVariable(
M,
val->getType(),
true,
GlobalVariable::PrivateLinkage,
val,
localname);
name);
gv->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
return gv;
};
if (gv == nullptr) {
gv = get_gv(name + "#" + Twine(emission_context.mergedConstants.size()));
}
else if (gv->getParent() != &M) {
StringRef gvname = gv->getName();
gv = M.getNamedGlobal(gvname);
if (!gv) {
gv = get_gv(gvname);
}
}
assert(localname == gv->getName());
assert(gv->getName().startswith(name.str()));
assert(val == gv->getInitializer());
return gv;
}
Expand Down Expand Up @@ -3393,7 +3393,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
const jl_cgval_t &ty = argv[2];
if (jl_is_type_type(ty.typ) && !jl_has_free_typevars(ty.typ)) {
jl_value_t *tp0 = jl_tparam0(ty.typ);
Value *isa_result = emit_isa(ctx, arg, tp0, NULL).first;
Value *isa_result = emit_isa(ctx, arg, tp0, Twine()).first;
if (isa_result->getType() == getInt1Ty(ctx.builder.getContext()))
isa_result = ctx.builder.CreateZExt(isa_result, getInt8Ty(ctx.builder.getContext()));
*ret = mark_julia_type(ctx, isa_result, false, jl_bool_type);
Expand Down Expand Up @@ -5278,7 +5278,7 @@ static void emit_upsilonnode(jl_codectx_t &ctx, ssize_t phic, jl_value_t *val)

static jl_cgval_t emit_cfunction(jl_codectx_t &ctx, jl_value_t *output_type, const jl_cgval_t &fexpr, jl_value_t *rt, jl_svec_t *argt);

static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const std::string &msg)
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const Twine &msg)
{
bool isbool = (condV.typ == (jl_value_t*)jl_bool_type);
if (!isbool) {
Expand All @@ -5301,7 +5301,7 @@ static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const s
return ConstantInt::get(getInt1Ty(ctx.builder.getContext()), 0); // TODO: replace with Undef
}

static Value *emit_condition(jl_codectx_t &ctx, jl_value_t *cond, const std::string &msg)
static Value *emit_condition(jl_codectx_t &ctx, jl_value_t *cond, const Twine &msg)
{
return emit_condition(ctx, emit_expr(ctx, cond), msg);
}
Expand Down

0 comments on commit 8c3452f

Please sign in to comment.