Skip to content

Commit cdc37ce

Browse files
authored
rewrite DW_TAG_compile_unit during aotcompile (JuliaLang#49183)
The macOS linker requires these to be unique due to a bug, and we do not care about this value at all as it does nothing, so just make it something unique. However, keep the directory as ".", since it uses that to compose the DW_AT_decl_file values for its subprograms. Also try to save a little memory, since we have to leak all of these objects (per LLVMContext), and we would like to avoid that. Fix JuliaLang#49152 Fix JuliaLang#49151 Fix JuliaLang#49153
1 parent 8f78a94 commit cdc37ce

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

src/aotcompile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ static void add_output(Module &M, TargetMachine &TM, std::vector<std::string> &o
13411341
timers[i].construct.startTimer();
13421342
construct_vars(*M, partitions[i]);
13431343
M->setModuleFlag(Module::Error, "julia.mv.suffix", MDString::get(M->getContext(), "_" + std::to_string(i)));
1344+
// The DICompileUnit file is not used for anything, but ld64 requires it be a unique string per object file
1345+
// or it may skip emitting debug info for that file. Here set it to ./julia#N
1346+
DIFile *topfile = DIFile::get(M->getContext(), "julia#" + std::to_string(i), ".");
1347+
for (DICompileUnit *CU : M->debug_compile_units())
1348+
CU->replaceOperandWith(0, topfile);
13441349
timers[i].construct.stopTimer();
13451350

13461351
timers[i].deletion.startTimer();

src/cgutils.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,39 @@ Metadata *to_md_tree(jl_value_t *val, LLVMContext &ctxt) {
153153

154154
// --- Debug info ---
155155

156+
static DICompileUnit *getOrCreateJuliaCU(Module &M,
157+
DICompileUnit::DebugEmissionKind emissionKind,
158+
DICompileUnit::DebugNameTableKind tableKind)
159+
{
160+
// TODO: share debug objects globally in the context, instead of allocating a new one every time
161+
// or figure out how to delete them afterwards?
162+
// But at least share them a little bit here
163+
auto CUs = M.debug_compile_units();
164+
for (DICompileUnit *CU : CUs) {
165+
if (CU->getEmissionKind() == emissionKind &&
166+
CU->getNameTableKind() == tableKind)
167+
return CU;
168+
}
169+
DIFile *topfile = DIFile::get(M.getContext(), "julia", ".");
170+
DIBuilder dbuilder(M);
171+
DICompileUnit *CU =
172+
dbuilder.createCompileUnit(llvm::dwarf::DW_LANG_Julia
173+
,topfile // File
174+
,"julia" // Producer
175+
,true // isOptimized
176+
,"" // Flags
177+
,0 // RuntimeVersion
178+
,"" // SplitName
179+
,emissionKind // Kind
180+
,0 // DWOId
181+
,true // SplitDebugInlining
182+
,false // DebugInfoForProfiling
183+
,tableKind // NameTableKind
184+
);
185+
dbuilder.finalize();
186+
return CU;
187+
}
188+
156189
static DIType *_julia_type_to_di(jl_codegen_params_t *ctx, jl_debugcache_t &debuginfo, jl_value_t *jt, DIBuilder *dbuilder, bool isboxed)
157190
{
158191
jl_datatype_t *jdt = (jl_datatype_t*)jt;

src/codegen.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7149,47 +7149,26 @@ static jl_llvm_functions_t
71497149
ctx.f = f;
71507150

71517151
// Step 4b. determine debug info signature and other type info for locals
7152-
DIBuilder dbuilder(*M);
7152+
DICompileUnit::DebugEmissionKind emissionKind = (DICompileUnit::DebugEmissionKind) ctx.params->debug_info_kind;
7153+
DICompileUnit::DebugNameTableKind tableKind;
7154+
if (JL_FEAT_TEST(ctx, gnu_pubnames))
7155+
tableKind = DICompileUnit::DebugNameTableKind::GNU;
7156+
else
7157+
tableKind = DICompileUnit::DebugNameTableKind::None;
7158+
DIBuilder dbuilder(*M, true, ctx.debug_enabled ? getOrCreateJuliaCU(*M, emissionKind, tableKind) : NULL);
71537159
DIFile *topfile = NULL;
71547160
DISubprogram *SP = NULL;
71557161
DebugLoc noDbg, topdebugloc;
71567162
if (ctx.debug_enabled) {
7157-
DICompileUnit::DebugEmissionKind emissionKind = (DICompileUnit::DebugEmissionKind) ctx.params->debug_info_kind;
7158-
DICompileUnit::DebugNameTableKind tableKind;
7159-
7160-
if (JL_FEAT_TEST(ctx, gnu_pubnames)) {
7161-
tableKind = DICompileUnit::DebugNameTableKind::GNU;
7162-
}
7163-
else {
7164-
tableKind = DICompileUnit::DebugNameTableKind::None;
7165-
}
71667163
topfile = dbuilder.createFile(ctx.file, ".");
7167-
DICompileUnit *CU =
7168-
dbuilder.createCompileUnit(llvm::dwarf::DW_LANG_Julia
7169-
,topfile // File
7170-
,"julia" // Producer
7171-
,true // isOptimized
7172-
,"" // Flags
7173-
,0 // RuntimeVersion
7174-
,"" // SplitName
7175-
,emissionKind // Kind
7176-
,0 // DWOId
7177-
,true // SplitDebugInlining
7178-
,false // DebugInfoForProfiling
7179-
,tableKind // NameTableKind
7180-
);
7181-
71827164
DISubroutineType *subrty;
7183-
if (jl_options.debug_level <= 1) {
7165+
if (jl_options.debug_level <= 1)
71847166
subrty = debuginfo.jl_di_func_null_sig;
7185-
}
7186-
else if (!specsig) {
7167+
else if (!specsig)
71877168
subrty = debuginfo.jl_di_func_sig;
7188-
}
7189-
else {
7169+
else
71907170
subrty = get_specsig_di(ctx, debuginfo, jlrettype, lam->specTypes, dbuilder);
7191-
}
7192-
SP = dbuilder.createFunction(CU
7171+
SP = dbuilder.createFunction(nullptr
71937172
,dbgFuncName // Name
71947173
,f->getName() // LinkageName
71957174
,topfile // File

src/jitlayers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,8 +1694,8 @@ void jl_merge_module(orc::ThreadSafeModule &destTSM, orc::ThreadSafeModule srcTS
16941694
NamedMDNode *sNMD = src.getNamedMetadata("llvm.dbg.cu");
16951695
if (sNMD) {
16961696
NamedMDNode *dNMD = dest.getOrInsertNamedMetadata("llvm.dbg.cu");
1697-
for (NamedMDNode::op_iterator I = sNMD->op_begin(), E = sNMD->op_end(); I != E; ++I) {
1698-
dNMD->addOperand(*I);
1697+
for (MDNode *I : sNMD->operands()) {
1698+
dNMD->addOperand(I);
16991699
}
17001700
}
17011701
});

0 commit comments

Comments
 (0)