Skip to content

Commit

Permalink
[Clang][OpenMP] Rework recently moved getTargetEntryUniqueInfo to fix…
Browse files Browse the repository at this point in the history
… incorrect error breaking sanitizer

This move was done by https://reviews.llvm.org/rGcda46cc4f921f6b288c57a68b901ec2f57134606 and may be the issue causing the sanitizer to fail. But in either case, it is an invalid assert that required some changes to function correctly.
  • Loading branch information
agozillon committed Jun 7, 2023
1 parent 296d867 commit 3090232
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
48 changes: 23 additions & 25 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,25 @@ convertCaptureClause(const VarDecl *VD) {
}
}

static llvm::TargetRegionEntryInfo getEntryInfoFromPresumedLoc(
CodeGenModule &CGM, llvm::OpenMPIRBuilder &OMPBuilder,
SourceLocation BeginLoc, llvm::StringRef ParentName = "") {

auto FileInfoCallBack = [&]() {
SourceManager &SM = CGM.getContext().getSourceManager();
PresumedLoc PLoc = SM.getPresumedLoc(BeginLoc);

llvm::sys::fs::UniqueID ID;
if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) {
PLoc = SM.getPresumedLoc(BeginLoc, /*UseLineDirectives=*/false);
}

return std::pair<std::string, uint64_t>(PLoc.getFilename(), PLoc.getLine());
};

return OMPBuilder.getTargetEntryUniqueInfo(FileInfoCallBack, ParentName);
}

Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
auto AddrOfGlobal = [&VD, this]() { return CGM.GetAddrOfGlobal(VD); };

Expand All @@ -1654,19 +1673,15 @@ Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
};

std::vector<llvm::GlobalVariable *> GeneratedRefs;
auto PLoc = CGM.getContext().getSourceManager().getPresumedLoc(
VD->getCanonicalDecl()->getBeginLoc());
if (PLoc.isInvalid())
PLoc = CGM.getContext().getSourceManager().getPresumedLoc(
VD->getCanonicalDecl()->getBeginLoc(), /*UseLineDirectives=*/false);

llvm::Type *LlvmPtrTy = CGM.getTypes().ConvertTypeForMem(
CGM.getContext().getPointerType(VD->getType()));
llvm::Constant *addr = OMPBuilder.getAddrOfDeclareTargetVar(
convertCaptureClause(VD), convertDeviceClause(VD),
VD->hasDefinition(CGM.getContext()) == VarDecl::DeclarationOnly,
VD->isExternallyVisible(),
OMPBuilder.getTargetEntryUniqueInfo(PLoc.getFilename(), PLoc.getLine()),
getEntryInfoFromPresumedLoc(CGM, OMPBuilder,
VD->getCanonicalDecl()->getBeginLoc()),
CGM.getMangledName(VD), GeneratedRefs, CGM.getLangOpts().OpenMPSimd,
CGM.getLangOpts().OMPTargetTriples, LlvmPtrTy, AddrOfGlobal,
LinkageForVariable);
Expand Down Expand Up @@ -1849,19 +1864,6 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
return nullptr;
}

static llvm::TargetRegionEntryInfo getEntryInfoFromPresumedLoc(
CodeGenModule &CGM, llvm::OpenMPIRBuilder &OMPBuilder,
SourceLocation BeginLoc, llvm::StringRef ParentName) {

auto PLoc = CGM.getContext().getSourceManager().getPresumedLoc(BeginLoc);
if (PLoc.isInvalid())
PLoc = CGM.getContext().getSourceManager().getPresumedLoc(
BeginLoc, /*UseLineDirectives=*/false);

return OMPBuilder.getTargetEntryUniqueInfo(PLoc.getFilename(), PLoc.getLine(),
ParentName);
}

bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
llvm::GlobalVariable *Addr,
bool PerformInit) {
Expand Down Expand Up @@ -10349,16 +10351,12 @@ void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
};

std::vector<llvm::GlobalVariable *> GeneratedRefs;
auto PLoc = CGM.getContext().getSourceManager().getPresumedLoc(
VD->getCanonicalDecl()->getBeginLoc());
if (PLoc.isInvalid())
PLoc = CGM.getContext().getSourceManager().getPresumedLoc(
VD->getCanonicalDecl()->getBeginLoc(), /*UseLineDirectives=*/false);
OMPBuilder.registerTargetGlobalVariable(
convertCaptureClause(VD), convertDeviceClause(VD),
VD->hasDefinition(CGM.getContext()) == VarDecl::DeclarationOnly,
VD->isExternallyVisible(),
OMPBuilder.getTargetEntryUniqueInfo(PLoc.getFilename(), PLoc.getLine()),
getEntryInfoFromPresumedLoc(CGM, OMPBuilder,
VD->getCanonicalDecl()->getBeginLoc()),
CGM.getMangledName(VD), GeneratedRefs, CGM.getLangOpts().OpenMPSimd,
CGM.getLangOpts().OMPTargetTriples, AddrOfGlobal, LinkageForVariable,
CGM.getTypes().ConvertTypeForMem(
Expand Down
9 changes: 6 additions & 3 deletions llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1155,15 +1155,18 @@ class OpenMPIRBuilder {
InsertPointTy AllocaIP,
BodyGenCallbackTy BodyGenCB);


using FileIdentifierInfoCallbackTy = std::function<std::tuple<std::string, uint64_t>()>;

/// Creates a unique info for a target entry when provided a filename and
/// line number from.
///
/// \param FileName The name of the file the target entry resides in
/// \param Line The line number where the target entry resides
/// \param CallBack A callback function which should return filename the entry
/// resides in as well as the line number for the target entry
/// \param ParentName The name of the parent the target entry resides in, if
/// any.
static TargetRegionEntryInfo
getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
getTargetEntryUniqueInfo(FileIdentifierInfoCallbackTy CallBack,
StringRef ParentName = "");

/// Functions used to generate reductions. Such functions take two Values
Expand Down
13 changes: 8 additions & 5 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5195,14 +5195,17 @@ void OffloadEntriesInfoManager::getTargetRegionEntryFnName(
}

TargetRegionEntryInfo
OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
OpenMPIRBuilder::getTargetEntryUniqueInfo(FileIdentifierInfoCallbackTy CallBack,
StringRef ParentName) {
sys::fs::UniqueID ID;
if (auto EC = sys::fs::getUniqueID(FileName, ID)) {
assert(EC &&
"Unable to get unique ID for file, during getTargetEntryUniqueInfo");
auto FileIDInfo = CallBack();
if (auto EC = sys::fs::getUniqueID(std::get<0>(FileIDInfo), ID)) {
llvm_unreachable(
"Unable to get unique ID for file, during getTargetEntryUniqueInfo");
}
return TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(), Line);

return TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
std::get<1>(FileIDInfo));
}

Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
Expand Down

0 comments on commit 3090232

Please sign in to comment.