Skip to content

[PGO][Offload] Fix offload coverage mapping #143490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CoverageMappingGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2622,8 +2622,9 @@ void CoverageMappingModuleGen::emit() {
CGM.addUsedGlobal(CovData);
// Create the deferred function records array
if (!FunctionNames.empty()) {
auto NamesArrTy = llvm::ArrayType::get(llvm::PointerType::getUnqual(Ctx),
FunctionNames.size());
auto AddrSpace = FunctionNames.front()->getType()->getPointerAddressSpace();
auto NamesArrTy = llvm::ArrayType::get(
llvm::PointerType::get(Ctx, AddrSpace), FunctionNames.size());
auto NamesArrVal = llvm::ConstantArray::get(NamesArrTy, FunctionNames);
// This variable will *NOT* be emitted to the object file. It is used
// to pass the list of names referenced to codegen.
Expand Down
6 changes: 0 additions & 6 deletions llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,12 +1954,6 @@ void InstrLowerer::emitNameData() {
GlobalValue::PrivateLinkage, NamesVal,
getInstrProfNamesVarName());

// Make names variable public if current target is a GPU
if (isGPUProfTarget(M)) {
NamesVar->setLinkage(GlobalValue::ExternalLinkage);
NamesVar->setVisibility(GlobalValue::VisibilityTypes::ProtectedVisibility);
}

NamesSize = CompressedNameStr.size();
setGlobalVariableLargeSection(TT, *NamesVar);
NamesVar->setSection(
Expand Down
4 changes: 1 addition & 3 deletions offload/plugins-nextgen/common/include/GlobalHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct GPUProfGlobals {

void dump() const;
Error write() const;
bool empty() const;
};

/// Subclass of GlobalTy that holds the memory for a global of \p Ty.
Expand Down Expand Up @@ -192,9 +193,6 @@ class GenericGlobalHandlerTy {
/*D2H=*/false);
}

/// Checks whether a given image contains profiling globals.
bool hasProfilingGlobals(GenericDeviceTy &Device, DeviceImageTy &Image);

/// Reads profiling data from a GPU image to supplied profdata struct.
/// Iterates through the image symbol table and stores global values
/// with profiling prefixes.
Expand Down
31 changes: 15 additions & 16 deletions offload/plugins-nextgen/common/src/GlobalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ Error GenericGlobalHandlerTy::readGlobalFromImage(GenericDeviceTy &Device,
return Plugin::success();
}

bool GenericGlobalHandlerTy::hasProfilingGlobals(GenericDeviceTy &Device,
DeviceImageTy &Image) {
GlobalTy global(getInstrProfNamesVarName().str(), 0);
if (auto Err = getGlobalMetadataFromImage(Device, Image, global)) {
consumeError(std::move(Err));
return false;
}
return true;
}

Expected<GPUProfGlobals>
GenericGlobalHandlerTy::readProfilingGlobals(GenericDeviceTy &Device,
DeviceImageTy &Image) {
Expand All @@ -195,12 +185,17 @@ GenericGlobalHandlerTy::readProfilingGlobals(GenericDeviceTy &Device,
// Check if given current global is a profiling global based
// on name
if (*NameOrErr == getInstrProfNamesVarName()) {
// Read in profiled function names
DeviceProfileData.NamesData = SmallVector<uint8_t>(Sym.getSize(), 0);
GlobalTy NamesGlobal(NameOrErr->str(), Sym.getSize(),
DeviceProfileData.NamesData.data());
if (auto Err = readGlobalFromDevice(Device, Image, NamesGlobal))
return Err;
// Read in profiled function names from ELF
auto SectionOrErr = Sym.getSection();
if (!SectionOrErr)
return SectionOrErr.takeError();

auto ContentsOrErr = (*SectionOrErr)->getContents();
if (!ContentsOrErr)
return ContentsOrErr.takeError();

SmallVector<uint8_t> NameBytes(ContentsOrErr->bytes());
DeviceProfileData.NamesData = NameBytes;
} else if (NameOrErr->starts_with(getInstrProfCountersVarPrefix())) {
// Read global variable profiling counts
SmallVector<int64_t> Counts(Sym.getSize() / sizeof(int64_t), 0);
Expand Down Expand Up @@ -311,3 +306,7 @@ Error GPUProfGlobals::write() const {

return Plugin::success();
}

bool GPUProfGlobals::empty() const {
return Counts.empty() && Data.empty() && NamesData.empty();
}
7 changes: 3 additions & 4 deletions offload/plugins-nextgen/common/src/PluginInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,13 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {

for (auto *Image : LoadedImages) {
GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler();
if (!Handler.hasProfilingGlobals(*this, *Image))
continue;

GPUProfGlobals profdata;
auto ProfOrErr = Handler.readProfilingGlobals(*this, *Image);
if (!ProfOrErr)
return ProfOrErr.takeError();

if (ProfOrErr->empty())
continue;

// Dump out profdata
if ((OMPX_DebugKind.get() & uint32_t(DeviceDebugKind::PGODump)) ==
uint32_t(DeviceDebugKind::PGODump))
Expand Down
Loading