Skip to content

Commit 8c3452f

Browse files
Clean up various naming aspects of codegen (#50638)
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>
1 parent 9c6efd6 commit 8c3452f

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

src/ccall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static GlobalVariable *emit_plt_thunk(
245245
std::string fname;
246246
raw_string_ostream(fname) << "jlplt_" << f_name << "_" << jl_atomic_fetch_add(&globalUniqueGeneratedNames, 1);
247247
Function *plt = Function::Create(functype,
248-
GlobalVariable::ExternalLinkage,
248+
GlobalVariable::PrivateLinkage,
249249
fname, M);
250250
plt->setAttributes(attrs);
251251
if (cc != CallingConv::C)

src/cgutils.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,21 @@ AtomicOrdering get_llvm_atomic_order(enum jl_memory_order order)
111111
static Value *stringConstPtr(
112112
jl_codegen_params_t &emission_context,
113113
IRBuilder<> &irbuilder,
114-
const std::string &txt)
114+
const Twine &txt)
115115
{
116116
Module *M = jl_builderModule(irbuilder);
117-
StringRef ctxt(txt.c_str(), txt.size() + 1);
118-
Constant *Data = ConstantDataArray::get(irbuilder.getContext(), arrayRefFromStringRef(ctxt));
119-
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str", *M);
117+
SmallVector<char, 128> ctxt;
118+
txt.toVector(ctxt);
119+
// null-terminate the string
120+
ctxt.push_back(0);
121+
Constant *Data = ConstantDataArray::get(irbuilder.getContext(), ctxt);
122+
ctxt.pop_back();
123+
// We use this for the name of the gv, so cap its size to avoid memory blowout
124+
if (ctxt.size() > 28) {
125+
ctxt.resize(28);
126+
ctxt[25] = ctxt[26] = ctxt[27] = '.';
127+
}
128+
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
120129
Value *zero = ConstantInt::get(Type::getInt32Ty(irbuilder.getContext()), 0);
121130
Value *Args[] = { zero, zero };
122131
auto gep = irbuilder.CreateInBoundsGEP(gv->getValueType(),
@@ -1097,7 +1106,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
10971106
if (justtag && jt->smalltag) {
10981107
ptr = ConstantInt::get(expr_type, jt->smalltag << 4);
10991108
if (ctx.emission_context.imaging)
1100-
ptr = get_pointer_to_constant(ctx.emission_context, ptr, "_j_tag", *jl_Module);
1109+
ptr = get_pointer_to_constant(ctx.emission_context, ptr, StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
11011110
}
11021111
else if (ctx.emission_context.imaging)
11031112
ptr = ConstantExpr::getBitCast(literal_pointer_val_slot(ctx, (jl_value_t*)jt), datatype_or_p->getType());
@@ -1278,27 +1287,27 @@ static Value *emit_datatype_name(jl_codectx_t &ctx, Value *dt)
12781287
// the error is always thrown. This may cause non dominated use
12791288
// of SSA value error in the verifier.
12801289

1281-
static void just_emit_error(jl_codectx_t &ctx, Function *F, const std::string &txt)
1290+
static void just_emit_error(jl_codectx_t &ctx, Function *F, const Twine &txt)
12821291
{
12831292
++EmittedErrors;
12841293
ctx.builder.CreateCall(F, stringConstPtr(ctx.emission_context, ctx.builder, txt));
12851294
}
12861295

1287-
static void emit_error(jl_codectx_t &ctx, Function *F, const std::string &txt)
1296+
static void emit_error(jl_codectx_t &ctx, Function *F, const Twine &txt)
12881297
{
12891298
just_emit_error(ctx, F, txt);
12901299
ctx.builder.CreateUnreachable();
12911300
BasicBlock *cont = BasicBlock::Create(ctx.builder.getContext(), "after_error", ctx.f);
12921301
ctx.builder.SetInsertPoint(cont);
12931302
}
12941303

1295-
static void emit_error(jl_codectx_t &ctx, const std::string &txt)
1304+
static void emit_error(jl_codectx_t &ctx, const Twine &txt)
12961305
{
12971306
emit_error(ctx, prepare_call(jlerror_func), txt);
12981307
}
12991308

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

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

1454-
static void just_emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const std::string &msg)
1463+
static void just_emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const Twine &msg)
14551464
{
14561465
Value *msg_val = stringConstPtr(ctx.emission_context, ctx.builder, msg);
14571466
ctx.builder.CreateCall(prepare_call(jltypeerror_func),
14581467
{ msg_val, maybe_decay_untracked(ctx, type), mark_callee_rooted(ctx, boxed(ctx, x))});
14591468
}
14601469

1461-
static void emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const std::string &msg)
1470+
static void emit_type_error(jl_codectx_t &ctx, const jl_cgval_t &x, Value *type, const Twine &msg)
14621471
{
14631472
just_emit_type_error(ctx, x, type, msg);
14641473
ctx.builder.CreateUnreachable();
@@ -1538,7 +1547,7 @@ static Value *emit_exactly_isa(jl_codectx_t &ctx, const jl_cgval_t &arg, jl_data
15381547
}
15391548

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

15431552
static void emit_isa_union(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type,
15441553
SmallVectorImpl<std::pair<std::pair<BasicBlock*,BasicBlock*>,Value*>> &bbs)
@@ -1550,15 +1559,15 @@ static void emit_isa_union(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *t
15501559
return;
15511560
}
15521561
BasicBlock *enter = ctx.builder.GetInsertBlock();
1553-
Value *v = emit_isa(ctx, x, type, nullptr).first;
1562+
Value *v = emit_isa(ctx, x, type, Twine()).first;
15541563
BasicBlock *exit = ctx.builder.GetInsertBlock();
15551564
bbs.emplace_back(std::make_pair(enter, exit), v);
15561565
BasicBlock *isaBB = BasicBlock::Create(ctx.builder.getContext(), "isa", ctx.f);
15571566
ctx.builder.SetInsertPoint(isaBB);
15581567
}
15591568

15601569
// Should agree with `_can_optimize_isa` above
1561-
static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const std::string *msg)
1570+
static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const Twine &msg)
15621571
{
15631572
++EmittedIsa;
15641573
// TODO: The subtype check below suffers from incorrectness issues due to broken
@@ -1578,8 +1587,8 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
15781587
known_isa = false;
15791588
}
15801589
if (known_isa) {
1581-
if (!*known_isa && msg) {
1582-
emit_type_error(ctx, x, literal_pointer_val(ctx, type), *msg);
1590+
if (!*known_isa && !msg.isTriviallyEmpty()) {
1591+
emit_type_error(ctx, x, literal_pointer_val(ctx, type), msg);
15831592
}
15841593
return std::make_pair(ConstantInt::get(getInt1Ty(ctx.builder.getContext()), *known_isa), true);
15851594
}
@@ -1611,7 +1620,7 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
16111620
if (jl_has_intersect_type_not_kind(type) || jl_has_intersect_type_not_kind(intersected_type)) {
16121621
Value *vx = boxed(ctx, x);
16131622
Value *vtyp = track_pjlvalue(ctx, literal_pointer_val(ctx, type));
1614-
if (msg && *msg == "typeassert") {
1623+
if (msg.isSingleStringRef() && msg.getSingleStringRef() == "typeassert") {
16151624
ctx.builder.CreateCall(prepare_call(jltypeassert_func), { vx, vtyp });
16161625
return std::make_pair(ConstantInt::get(getInt1Ty(ctx.builder.getContext()), 1), true);
16171626
}
@@ -1672,16 +1681,16 @@ static std::pair<Value*, bool> emit_isa(jl_codectx_t &ctx, const jl_cgval_t &x,
16721681
static Value *emit_isa_and_defined(jl_codectx_t &ctx, const jl_cgval_t &val, jl_value_t *typ)
16731682
{
16741683
return emit_nullcheck_guard(ctx, val.ispointer() ? val.V : nullptr, [&] {
1675-
return emit_isa(ctx, val, typ, nullptr).first;
1684+
return emit_isa(ctx, val, typ, Twine()).first;
16761685
});
16771686
}
16781687

16791688

1680-
static void emit_typecheck(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const std::string &msg)
1689+
static void emit_typecheck(jl_codectx_t &ctx, const jl_cgval_t &x, jl_value_t *type, const Twine &msg)
16811690
{
16821691
Value *istype;
16831692
bool handled_msg;
1684-
std::tie(istype, handled_msg) = emit_isa(ctx, x, type, &msg);
1693+
std::tie(istype, handled_msg) = emit_isa(ctx, x, type, msg);
16851694
if (!handled_msg) {
16861695
++EmittedTypechecks;
16871696
BasicBlock *failBB = BasicBlock::Create(ctx.builder.getContext(), "fail", ctx.f);
@@ -1709,7 +1718,7 @@ static Value *emit_isconcrete(jl_codectx_t &ctx, Value *typ)
17091718
return isconcrete;
17101719
}
17111720

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

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

2309-
static void emit_atomic_error(jl_codectx_t &ctx, const std::string &msg)
2318+
static void emit_atomic_error(jl_codectx_t &ctx, const Twine &msg)
23102319
{
23112320
emit_error(ctx, prepare_call(jlatomicerror_func), msg);
23122321
}
@@ -3668,7 +3677,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
36683677
}
36693678

36703679

3671-
static void emit_cpointercheck(jl_codectx_t &ctx, const jl_cgval_t &x, const std::string &msg)
3680+
static void emit_cpointercheck(jl_codectx_t &ctx, const jl_cgval_t &x, const Twine &msg)
36723681
{
36733682
++EmittedCPointerChecks;
36743683
Value *t = emit_typeof(ctx, x, false, false);
@@ -3776,7 +3785,7 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx,
37763785
jl_cgval_t rhs, jl_cgval_t cmp,
37773786
bool wb, AtomicOrdering Order, AtomicOrdering FailOrder,
37783787
bool needlock, bool issetfield, bool isreplacefield, bool isswapfield, bool ismodifyfield,
3779-
const jl_cgval_t *modifyop, const std::string &fname)
3788+
const jl_cgval_t *modifyop, const Twine &fname)
37803789
{
37813790
auto get_objname = [&]() {
37823791
return strct.V ? strct.V->getName() : StringRef("");

src/codegen.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ static Value *global_binding_pointer(jl_codectx_t &ctx, jl_module_t *m, jl_sym_t
17611761
jl_binding_t **pbnd, bool assign);
17621762
static jl_cgval_t emit_checked_var(jl_codectx_t &ctx, Value *bp, jl_sym_t *name, bool isvol, MDNode *tbaa);
17631763
static jl_cgval_t emit_sparam(jl_codectx_t &ctx, size_t i);
1764-
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const std::string &msg);
1764+
static Value *emit_condition(jl_codectx_t &ctx, const jl_cgval_t &condV, const Twine &msg);
17651765
static Value *get_current_task(jl_codectx_t &ctx);
17661766
static Value *get_current_ptls(jl_codectx_t &ctx);
17671767
static Value *get_last_age_field(jl_codectx_t &ctx);
@@ -1811,31 +1811,31 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
18111811

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

1814-
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, StringRef name, Module &M)
1814+
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, const Twine &name, Module &M)
18151815
{
18161816
GlobalVariable *&gv = emission_context.mergedConstants[val];
1817-
StringRef localname;
1818-
std::string ssno;
1819-
if (gv == nullptr) {
1820-
raw_string_ostream(ssno) << name << emission_context.mergedConstants.size();
1821-
localname = StringRef(ssno);
1822-
}
1823-
else {
1824-
localname = gv->getName();
1825-
if (gv->getParent() != &M)
1826-
gv = cast_or_null<GlobalVariable>(M.getNamedValue(localname));
1827-
}
1828-
if (gv == nullptr) {
1829-
gv = new GlobalVariable(
1817+
auto get_gv = [&](const Twine &name) {
1818+
auto gv = new GlobalVariable(
18301819
M,
18311820
val->getType(),
18321821
true,
18331822
GlobalVariable::PrivateLinkage,
18341823
val,
1835-
localname);
1824+
name);
18361825
gv->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1826+
return gv;
1827+
};
1828+
if (gv == nullptr) {
1829+
gv = get_gv(name + "#" + Twine(emission_context.mergedConstants.size()));
1830+
}
1831+
else if (gv->getParent() != &M) {
1832+
StringRef gvname = gv->getName();
1833+
gv = M.getNamedGlobal(gvname);
1834+
if (!gv) {
1835+
gv = get_gv(gvname);
1836+
}
18371837
}
1838-
assert(localname == gv->getName());
1838+
assert(gv->getName().startswith(name.str()));
18391839
assert(val == gv->getInitializer());
18401840
return gv;
18411841
}
@@ -3393,7 +3393,7 @@ static bool emit_builtin_call(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f,
33933393
const jl_cgval_t &ty = argv[2];
33943394
if (jl_is_type_type(ty.typ) && !jl_has_free_typevars(ty.typ)) {
33953395
jl_value_t *tp0 = jl_tparam0(ty.typ);
3396-
Value *isa_result = emit_isa(ctx, arg, tp0, NULL).first;
3396+
Value *isa_result = emit_isa(ctx, arg, tp0, Twine()).first;
33973397
if (isa_result->getType() == getInt1Ty(ctx.builder.getContext()))
33983398
isa_result = ctx.builder.CreateZExt(isa_result, getInt8Ty(ctx.builder.getContext()));
33993399
*ret = mark_julia_type(ctx, isa_result, false, jl_bool_type);
@@ -5278,7 +5278,7 @@ static void emit_upsilonnode(jl_codectx_t &ctx, ssize_t phic, jl_value_t *val)
52785278

52795279
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);
52805280

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

5304-
static Value *emit_condition(jl_codectx_t &ctx, jl_value_t *cond, const std::string &msg)
5304+
static Value *emit_condition(jl_codectx_t &ctx, jl_value_t *cond, const Twine &msg)
53055305
{
53065306
return emit_condition(ctx, emit_expr(ctx, cond), msg);
53075307
}

0 commit comments

Comments
 (0)