Skip to content

Commit c1f806d

Browse files
authored
compile: make more efficient by discarding internal names (#56702)
These are not user-visible, so this makes the compiler faster and more efficient with no effort on our part, and avoids duplicating the debug_level parameter.
1 parent 5053a17 commit c1f806d

File tree

7 files changed

+35
-45
lines changed

7 files changed

+35
-45
lines changed

src/aotcompile.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,10 @@ void *jl_create_native_impl(jl_array_t *methods, LLVMOrcThreadSafeModuleRef llvm
593593
});
594594
egal_set method_roots;
595595
jl_codegen_params_t params(ctxt, std::move(target_info.first), std::move(target_info.second));
596+
if (!llvmmod)
597+
params.getContext().setDiscardValueNames(true);
596598
params.params = cgparams;
597599
params.imaging_mode = imaging;
598-
params.debug_level = cgparams->debug_info_level;
599600
params.external_linkage = _external_linkage;
600601
params.temporary_roots = jl_alloc_array_1d(jl_array_any_type, 0);
601602
JL_GC_PUSH3(&params.temporary_roots, &method_roots.list, &method_roots.keyset);
@@ -1719,6 +1720,7 @@ static SmallVector<AOTOutputs, 16> add_output(Module &M, TargetMachine &TM, Stri
17191720
for (unsigned i = 0; i < threads; i++) {
17201721
std::function<void()> func = [&, i]() {
17211722
LLVMContext ctx;
1723+
ctx.setDiscardValueNames(true);
17221724
#if JL_LLVM_VERSION < 170000
17231725
SetOpaquePointer(ctx);
17241726
#endif
@@ -1930,6 +1932,7 @@ void jl_dump_native_impl(void *native_code,
19301932
if (z) {
19311933
JL_TIMING(NATIVE_AOT, NATIVE_Sysimg);
19321934
LLVMContext Context;
1935+
Context.setDiscardValueNames(true);
19331936
#if JL_LLVM_VERSION < 170000
19341937
SetOpaquePointer(Context);
19351938
#endif
@@ -2077,6 +2080,7 @@ void jl_dump_native_impl(void *native_code,
20772080
{
20782081
JL_TIMING(NATIVE_AOT, NATIVE_Metadata);
20792082
LLVMContext Context;
2083+
Context.setDiscardValueNames(true);
20802084
#if JL_LLVM_VERSION < 170000
20812085
SetOpaquePointer(Context);
20822086
#endif
@@ -2278,7 +2282,6 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, jl_
22782282
// output.imaging = true;
22792283
// This would also be nice, but it seems to cause OOMs on the windows32 builder
22802284
// To get correct names in the IR this needs to be at least 2
2281-
output.debug_level = params.debug_info_level;
22822285
output.temporary_roots = jl_alloc_array_1d(jl_array_any_type, 0);
22832286
JL_GC_PUSH1(&output.temporary_roots);
22842287
auto decls = jl_emit_code(m, mi, src, output);

src/ccall.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
851851

852852
// generate a temporary module that contains our IR
853853
std::unique_ptr<Module> Mod;
854+
bool shouldDiscardValueNames = ctx.builder.getContext().shouldDiscardValueNames();
854855
Function *f;
855856
if (entry == NULL) {
856857
// we only have function IR, which we should put in a function
@@ -878,7 +879,9 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
878879
<< jl_string_data(ir) << "\n}";
879880

880881
SMDiagnostic Err = SMDiagnostic();
882+
ctx.builder.getContext().setDiscardValueNames(false);
881883
Mod = parseAssemblyString(ir_stream.str(), Err, ctx.builder.getContext());
884+
ctx.builder.getContext().setDiscardValueNames(shouldDiscardValueNames);
882885

883886
// backwards compatibility: support for IR with integer pointers
884887
if (!Mod) {
@@ -911,8 +914,9 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
911914
<< jl_string_data(ir) << "\n}";
912915

913916
SMDiagnostic Err = SMDiagnostic();
914-
Mod =
915-
parseAssemblyString(compat_ir_stream.str(), Err, ctx.builder.getContext());
917+
ctx.builder.getContext().setDiscardValueNames(false);
918+
Mod = parseAssemblyString(compat_ir_stream.str(), Err, ctx.builder.getContext());
919+
ctx.builder.getContext().setDiscardValueNames(shouldDiscardValueNames);
916920
}
917921

918922
if (!Mod) {
@@ -932,7 +936,9 @@ static jl_cgval_t emit_llvmcall(jl_codectx_t &ctx, jl_value_t **args, size_t nar
932936

933937
if (jl_is_string(ir)) {
934938
SMDiagnostic Err = SMDiagnostic();
939+
ctx.builder.getContext().setDiscardValueNames(false);
935940
Mod = parseAssemblyString(jl_string_data(ir), Err, ctx.builder.getContext());
941+
ctx.builder.getContext().setDiscardValueNames(shouldDiscardValueNames);
936942
if (!Mod) {
937943
std::string message = "Failed to parse LLVM assembly: \n";
938944
raw_string_ostream stream(message);

src/codegen.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,31 +167,29 @@ void setName(jl_codegen_params_t &params, Value *V, const Twine &Name)
167167
// is not checking that setName is only called for non-folded instructions (e.g. folded bitcasts
168168
// and 0-byte geps), which can result in information loss on the renamed instruction.
169169
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
170-
if (params.debug_level >= 2 && !isa<Constant>(V)) {
170+
if (!isa<Constant>(V)) {
171171
V->setName(Name);
172172
}
173173
}
174174

175175
void maybeSetName(jl_codegen_params_t &params, Value *V, const Twine &Name)
176176
{
177177
// To be used when we may get an Instruction or something that is not an instruction i.e Constants/Arguments
178-
if (params.debug_level >= 2 && isa<Instruction>(V)) {
178+
if (isa<Instruction>(V))
179179
V->setName(Name);
180-
}
181180
}
182181

183182
void setName(jl_codegen_params_t &params, Value *V, std::function<std::string()> GetName)
184183
{
185184
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
186-
if (params.debug_level >= 2 && !isa<Constant>(V)) {
185+
if (!params.getContext().shouldDiscardValueNames() && !isa<Constant>(V))
187186
V->setName(Twine(GetName()));
188-
}
189187
}
190188

191189
void setNameWithField(jl_codegen_params_t &params, Value *V, std::function<StringRef()> GetObjName, jl_datatype_t *jt, unsigned idx, const Twine &suffix)
192190
{
193191
assert((isa<Constant>(V) || isa<Instruction>(V)) && "Should only set names on instructions!");
194-
if (params.debug_level >= 2 && !isa<Constant>(V)) {
192+
if (!params.getContext().shouldDiscardValueNames() && !isa<Constant>(V)) {
195193
if (jl_is_tuple_type(jt)){
196194
V->setName(Twine(GetObjName()) + "[" + Twine(idx + 1) + "]"+ suffix);
197195
return;
@@ -8327,7 +8325,7 @@ static jl_returninfo_t get_specsig_function(jl_codectx_t &ctx, Module *M, Value
83278325
if (f == NULL) {
83288326
f = Function::Create(ftype, GlobalVariable::ExternalLinkage, name, M);
83298327
jl_init_function(f, ctx.emission_context.TargetTriple);
8330-
if (ctx.emission_context.debug_level >= 2) {
8328+
if (ctx.emission_context.params->debug_info_level >= 2) {
83318329
ios_t sigbuf;
83328330
ios_mem(&sigbuf, 0);
83338331
jl_static_show_func_sig((JL_STREAM*) &sigbuf, sig);
@@ -8435,7 +8433,7 @@ static jl_llvm_functions_t
84358433
std::map<int, BasicBlock*> labels;
84368434
ctx.module = jl_is_method(lam->def.method) ? lam->def.method->module : lam->def.module;
84378435
ctx.linfo = lam;
8438-
ctx.name = TSM.getModuleUnlocked()->getModuleIdentifier().data();
8436+
ctx.name = name_from_method_instance(lam);
84398437
size_t nreq = src->nargs;
84408438
int va = src->isva;
84418439
ctx.nargs = nreq;
@@ -8488,7 +8486,7 @@ static jl_llvm_functions_t
84888486
// jl_printf(JL_STDERR, "\n*** compiling %s at %s:%d\n\n",
84898487
// jl_symbol_name(ctx.name), ctx.file.str().c_str(), toplineno);
84908488

8491-
bool debug_enabled = ctx.emission_context.debug_level != 0;
8489+
bool debug_enabled = ctx.emission_context.params->debug_info_level != 0;
84928490
if (dbgFuncName.empty()) // Should never happen anymore?
84938491
debug_enabled = false;
84948492

@@ -8564,15 +8562,14 @@ static jl_llvm_functions_t
85648562
// allocate Function declarations and wrapper objects
85658563
//Safe because params holds ctx lock
85668564
Module *M = TSM.getModuleUnlocked();
8567-
M->addModuleFlag(Module::Warning, "julia.debug_level", ctx.emission_context.debug_level);
85688565
jl_debugcache_t debugcache;
85698566
debugcache.initialize(M);
85708567
jl_returninfo_t returninfo = {};
85718568
Function *f = NULL;
85728569
bool has_sret = false;
85738570
if (specsig) { // assumes !va and !needsparams
85748571
SmallVector<const char*,0> ArgNames(0);
8575-
if (ctx.emission_context.debug_level >= 2) {
8572+
if (!M->getContext().shouldDiscardValueNames()) {
85768573
ArgNames.resize(ctx.nargs, "");
85778574
for (int i = 0; i < ctx.nargs; i++) {
85788575
jl_sym_t *argname = slot_symbol(ctx, i);
@@ -8639,7 +8636,7 @@ static jl_llvm_functions_t
86398636
declarations.functionObject = needsparams ? "jl_fptr_sparam" : "jl_fptr_args";
86408637
}
86418638

8642-
if (ctx.emission_context.debug_level >= 2 && lam->def.method && jl_is_method(lam->def.method) && lam->specTypes != (jl_value_t*)jl_emptytuple_type) {
8639+
if (!params.getContext().shouldDiscardValueNames() && ctx.emission_context.params->debug_info_level >= 2 && lam->def.method && jl_is_method(lam->def.method) && lam->specTypes != (jl_value_t*)jl_emptytuple_type) {
86438640
ios_t sigbuf;
86448641
ios_mem(&sigbuf, 0);
86458642
jl_static_show_func_sig((JL_STREAM*) &sigbuf, (jl_value_t*)lam->specTypes);
@@ -8694,7 +8691,7 @@ static jl_llvm_functions_t
86948691
if (debug_enabled) {
86958692
topfile = dbuilder.createFile(ctx.file, ".");
86968693
DISubroutineType *subrty;
8697-
if (ctx.emission_context.debug_level <= 1)
8694+
if (ctx.emission_context.params->debug_info_level <= 1)
86988695
subrty = debugcache.jl_di_func_null_sig;
86998696
else if (!specsig)
87008697
subrty = debugcache.jl_di_func_sig;
@@ -8715,7 +8712,7 @@ static jl_llvm_functions_t
87158712
);
87168713
topdebugloc = DILocation::get(ctx.builder.getContext(), toplineno, 0, SP, NULL);
87178714
f->setSubprogram(SP);
8718-
if (ctx.emission_context.debug_level >= 2) {
8715+
if (ctx.emission_context.params->debug_info_level >= 2) {
87198716
const bool AlwaysPreserve = true;
87208717
// Go over all arguments and local variables and initialize their debug information
87218718
for (i = 0; i < nreq; i++) {
@@ -10161,7 +10158,7 @@ jl_llvm_functions_t jl_emit_codeinst(
1016110158
if (// keep code when keeping everything
1016210159
!(JL_DELETE_NON_INLINEABLE) ||
1016310160
// aggressively keep code when debugging level >= 2
10164-
// note that this uses the global jl_options.debug_level, not the local emission_ctx.debug_level
10161+
// note that this uses the global jl_options.debug_level, not the local emission_ctx.debug_info_level
1016510162
jl_options.debug_level > 1) {
1016610163
// update the stored code
1016710164
if (inferred != (jl_value_t*)src) {

src/jitlayers.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,9 @@ static void jl_emit_codeinst_to_jit(
690690
JL_TIMING(CODEINST_COMPILE, CODEINST_COMPILE);
691691
// emit the code in LLVM IR form to the new context
692692
jl_codegen_params_t params(std::make_unique<LLVMContext>(), jl_ExecutionEngine->getDataLayout(), jl_ExecutionEngine->getTargetTriple()); // Locks the context
693+
params.getContext().setDiscardValueNames(true);
693694
params.cache = true;
694695
params.imaging_mode = imaging_default();
695-
params.debug_level = jl_options.debug_level;
696696
orc::ThreadSafeModule result_m =
697697
jl_create_ts_module(name_from_method_instance(codeinst->def), params.tsctx, params.DL, params.TargetTriple);
698698
params.temporary_roots = jl_alloc_array_1d(jl_array_any_type, 0);
@@ -795,9 +795,10 @@ int jl_compile_extern_c_impl(LLVMOrcThreadSafeModuleRef llvmmod, void *p, void *
795795
Module *M = into->getModuleUnlocked();
796796
jl_codegen_params_t params(into->getContext(), M->getDataLayout(), Triple(M->getTargetTriple()));
797797
params.imaging_mode = imaging_default();
798-
params.debug_level = jl_options.debug_level;
799-
if (pparams == NULL)
798+
if (pparams == NULL) {
799+
M->getContext().setDiscardValueNames(true);
800800
pparams = &params;
801+
}
801802
assert(pparams->tsctx.getContext() == into->getContext().getContext());
802803
const char *name = jl_generate_ccallable(wrap(into), sysimg, declrt, sigt, *pparams);
803804
if (!sysimg) {

src/jitlayers.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ struct jl_codegen_params_t {
259259
bool cache = false;
260260
bool external_linkage = false;
261261
bool imaging_mode;
262-
int debug_level;
263262
bool use_swiftcc = true;
264263
jl_codegen_params_t(orc::ThreadSafeContext ctx, DataLayout DL, Triple triple)
265264
: tsctx(std::move(ctx)),

src/llvm-late-gc-lowering.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,36 +1944,27 @@ void LateLowerGCFrame::CleanupWriteBarriers(Function &F, State *S, const SmallVe
19441944
if (CFGModified) {
19451945
*CFGModified = true;
19461946
}
1947-
auto DebugInfoMeta = F.getParent()->getModuleFlag("julia.debug_level");
1948-
int debug_info = 1;
1949-
if (DebugInfoMeta != nullptr) {
1950-
debug_info = cast<ConstantInt>(cast<ConstantAsMetadata>(DebugInfoMeta)->getValue())->getZExtValue();
1951-
}
19521947

19531948
IRBuilder<> builder(CI);
19541949
builder.SetCurrentDebugLocation(CI->getDebugLoc());
1955-
auto parBits = builder.CreateAnd(EmitLoadTag(builder, T_size, parent), GC_OLD_MARKED);
1956-
setName(parBits, "parent_bits", debug_info);
1957-
auto parOldMarked = builder.CreateICmpEQ(parBits, ConstantInt::get(T_size, GC_OLD_MARKED));
1958-
setName(parOldMarked, "parent_old_marked", debug_info);
1950+
auto parBits = builder.CreateAnd(EmitLoadTag(builder, T_size, parent), GC_OLD_MARKED, "parent_bits");
1951+
auto parOldMarked = builder.CreateICmpEQ(parBits, ConstantInt::get(T_size, GC_OLD_MARKED), "parent_old_marked");
19591952
auto mayTrigTerm = SplitBlockAndInsertIfThen(parOldMarked, CI, false);
19601953
builder.SetInsertPoint(mayTrigTerm);
1961-
setName(mayTrigTerm->getParent(), "may_trigger_wb", debug_info);
1954+
mayTrigTerm->getParent()->setName("may_trigger_wb");
19621955
Value *anyChldNotMarked = NULL;
19631956
for (unsigned i = 1; i < CI->arg_size(); i++) {
19641957
Value *child = CI->getArgOperand(i);
1965-
Value *chldBit = builder.CreateAnd(EmitLoadTag(builder, T_size, child), GC_MARKED);
1966-
setName(chldBit, "child_bit", debug_info);
1967-
Value *chldNotMarked = builder.CreateICmpEQ(chldBit, ConstantInt::get(T_size, 0),"child_not_marked");
1968-
setName(chldNotMarked, "child_not_marked", debug_info);
1958+
Value *chldBit = builder.CreateAnd(EmitLoadTag(builder, T_size, child), GC_MARKED, "child_bit");
1959+
Value *chldNotMarked = builder.CreateICmpEQ(chldBit, ConstantInt::get(T_size, 0), "child_not_marked");
19691960
anyChldNotMarked = anyChldNotMarked ? builder.CreateOr(anyChldNotMarked, chldNotMarked) : chldNotMarked;
19701961
}
19711962
assert(anyChldNotMarked); // handled by all_of test above
19721963
MDBuilder MDB(parent->getContext());
19731964
SmallVector<uint32_t, 2> Weights{1, 9};
19741965
auto trigTerm = SplitBlockAndInsertIfThen(anyChldNotMarked, mayTrigTerm, false,
19751966
MDB.createBranchWeights(Weights));
1976-
setName(trigTerm->getParent(), "trigger_wb", debug_info);
1967+
trigTerm->getParent()->setName("trigger_wb");
19771968
builder.SetInsertPoint(trigTerm);
19781969
if (CI->getCalledOperand() == write_barrier_func) {
19791970
builder.CreateCall(getOrDeclare(jl_intrinsics::queueGCRoot), parent);

src/llvm-pass-helpers.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,3 @@ namespace jl_well_known {
335335
return addGCAllocAttributes(allocTypedFunc);
336336
});
337337
}
338-
339-
void setName(llvm::Value *V, const llvm::Twine &Name, int debug_info)
340-
{
341-
if (debug_info >= 2 && !llvm::isa<llvm::Constant>(V)) {
342-
V->setName(Name);
343-
}
344-
}

0 commit comments

Comments
 (0)