Skip to content

Commit

Permalink
Improve remarks of the alloc opt pass slightly. (#55995)
Browse files Browse the repository at this point in the history
The Value printer LLVM uses just prints the kind of instruction so it
just shows call.

---------

Co-authored-by: Oscar Smith <[email protected]>
  • Loading branch information
gbaraldi and oscardssmith authored Oct 14, 2024
1 parent a19569d commit 159adbf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
28 changes: 22 additions & 6 deletions src/llvm-alloc-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ JL_USED_FUNC void AllocUseInfo::dump(llvm::raw_ostream &OS)
OS << " zeroed";
OS << '\n';
OS << "Uses: " << uses.size() << '\n';
for (auto inst: uses)
for (auto inst: uses) {
inst->print(OS);
OS << '\n';
}
if (!preserves.empty()) {
OS << "Preserves: " << preserves.size() << '\n';
for (auto inst: preserves)
for (auto inst: preserves) {
inst->print(OS);
OS << '\n';
}
}
OS << "MemOps: " << memops.size() << '\n';
for (auto &field: memops) {
Expand Down Expand Up @@ -268,9 +272,12 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
}
LLVM_DEBUG(dbgs() << "Unknown call, marking escape\n");
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
inst->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "UnknownCall",
inst)
<< "Unknown call, marking escape (" << ore::NV("Call", inst) << ")";
<< "Unknown call, marking escape (" << ore::NV("Call", StringRef(str)) << ")";
});
required.use_info.escaped = true;
return false;
Expand All @@ -284,9 +291,12 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
if (use->getOperandNo() != StoreInst::getPointerOperandIndex()) {
LLVM_DEBUG(dbgs() << "Object address is stored somewhere, marking escape\n");
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
inst->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "StoreObjAddr",
inst)
<< "Object address is stored somewhere, marking escape (" << ore::NV("Store", inst) << ")";
<< "Object address is stored somewhere, marking escape (" << ore::NV("Store", StringRef(str)) << ")";
});
required.use_info.escaped = true;
return false;
Expand All @@ -310,9 +320,12 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
if (use->getOperandNo() != isa<AtomicCmpXchgInst>(inst) ? AtomicCmpXchgInst::getPointerOperandIndex() : AtomicRMWInst::getPointerOperandIndex()) {
LLVM_DEBUG(dbgs() << "Object address is cmpxchg/rmw-ed somewhere, marking escape\n");
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
inst->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "StoreObjAddr",
inst)
<< "Object address is cmpxchg/rmw-ed somewhere, marking escape (" << ore::NV("Store", inst) << ")";
<< "Object address is cmpxchg/rmw-ed somewhere, marking escape (" << ore::NV("Store", StringRef(str)) << ")";
});
required.use_info.escaped = true;
return false;
Expand Down Expand Up @@ -363,9 +376,12 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
}
LLVM_DEBUG(dbgs() << "Unknown instruction, marking escape\n");
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
inst->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "UnknownInst",
inst)
<< "Unknown instruction, marking escape (" << ore::NV("Inst", inst) << ")";
<< "Unknown instruction, marking escape (" << ore::NV("Inst", StringRef(str)) << ")";
});
required.use_info.escaped = true;
return false;
Expand Down
45 changes: 36 additions & 9 deletions src/llvm-alloc-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,23 @@ void Optimizer::optimizeAll()
checkInst(orig);
if (use_info.escaped) {
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "Escaped", orig)
<< "GC allocation escaped " << ore::NV("GC Allocation", orig);
<< "GC allocation escaped " << ore::NV("GC Allocation", StringRef(str));
});
if (use_info.hastypeof)
optimizeTag(orig);
continue;
}
if (use_info.haserror || use_info.returned) {
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "Escaped", orig)
<< "GC allocation has error or was returned " << ore::NV("GC Allocation", orig);
<< "GC allocation has error or was returned " << ore::NV("GC Allocation", StringRef(str));
});
if (use_info.hastypeof)
optimizeTag(orig);
Expand All @@ -243,8 +249,11 @@ void Optimizer::optimizeAll()
if (!use_info.addrescaped && !use_info.hasload && (!use_info.haspreserve ||
!use_info.refstore)) {
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemark(DEBUG_TYPE, "Dead Allocation", orig)
<< "GC allocation removed " << ore::NV("GC Allocation", orig);
<< "GC allocation removed " << ore::NV("GC Allocation", StringRef(str));
});
// No one took the address, no one reads anything and there's no meaningful
// preserve of fields (either no preserve/ccall or no object reference fields)
Expand All @@ -270,17 +279,23 @@ void Optimizer::optimizeAll()
}
if (has_refaggr) {
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "Escaped", orig)
<< "GC allocation has unusual object reference, unable to move to stack " << ore::NV("GC Allocation", orig);
<< "GC allocation has unusual object reference, unable to move to stack " << ore::NV("GC Allocation", StringRef(str));
});
if (use_info.hastypeof)
optimizeTag(orig);
continue;
}
if (!use_info.hasunknownmem && !use_info.addrescaped) {
REMARK([&](){
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemark(DEBUG_TYPE, "Stack Split Allocation", orig)
<< "GC allocation split on stack " << ore::NV("GC Allocation", orig);
<< "GC allocation split on stack " << ore::NV("GC Allocation", StringRef(str));
});
// No one actually care about the memory layout of this object, split it.
splitOnStack(orig);
Expand All @@ -292,16 +307,22 @@ void Optimizer::optimizeAll()
// This later causes the GC rooting pass, to miss-characterize the float as a pointer to a GC value
if (has_unboxed && has_ref) {
REMARK([&]() {
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemarkMissed(DEBUG_TYPE, "Escaped", orig)
<< "GC allocation could not be split since it contains both boxed and unboxed values, unable to move to stack " << ore::NV("GC Allocation", orig);
<< "GC allocation could not be split since it contains both boxed and unboxed values, unable to move to stack " << ore::NV("GC Allocation", StringRef(str));
});
if (use_info.hastypeof)
optimizeTag(orig);
continue;
}
REMARK([&](){
std::string str;
llvm::raw_string_ostream rso(str);
orig->print(rso);
return OptimizationRemark(DEBUG_TYPE, "Stack Move Allocation", orig)
<< "GC allocation moved to stack " << ore::NV("GC Allocation", orig);
<< "GC allocation moved to stack " << ore::NV("GC Allocation", StringRef(str));
});
// The object has no fields with mix reference access
moveToStack(orig, sz, has_ref, use_info.allockind);
Expand Down Expand Up @@ -380,7 +401,10 @@ void Optimizer::checkInst(CallInst *I)
std::string suse_info;
llvm::raw_string_ostream osuse_info(suse_info);
use_info.dump(osuse_info);
return OptimizationRemarkAnalysis(DEBUG_TYPE, "EscapeAnalysis", I) << "escape analysis for " << ore::NV("GC Allocation", I) << "\n" << ore::NV("UseInfo", osuse_info.str());
std::string str;
llvm::raw_string_ostream rso(str);
I->print(rso);
return OptimizationRemarkAnalysis(DEBUG_TYPE, "EscapeAnalysis", I) << "escape analysis for " << ore::NV("GC Allocation", StringRef(str)) << "\n" << ore::NV("UseInfo", osuse_info.str());
});
}

Expand Down Expand Up @@ -905,8 +929,11 @@ void Optimizer::optimizeTag(CallInst *orig_inst)
if (pass.typeof_func == callee) {
++RemovedTypeofs;
REMARK([&](){
std::string str;
llvm::raw_string_ostream rso(str);
orig_inst->print(rso);
return OptimizationRemark(DEBUG_TYPE, "typeof", call)
<< "removed typeof call for GC allocation " << ore::NV("Alloc", orig_inst);
<< "removed typeof call for GC allocation " << ore::NV("Alloc", StringRef(str));
});
call->replaceAllUsesWith(tag);
// Push to the removed instructions to trigger `finalize` to
Expand Down

0 comments on commit 159adbf

Please sign in to comment.