Skip to content

Commit 3d0e9ed

Browse files
committed
[OpenMP] [OMPIRBuilder] Create a new datatype to hold the unique target region info
This patch puts the individual target region information attributes into a struct so that the nested mappings are not needed and passing the information around is simplified. Reviewed By: jdoerfert, mikerice Differential Revision: https://reviews.llvm.org/D136601
1 parent cb088e8 commit 3d0e9ed

File tree

6 files changed

+124
-118
lines changed

6 files changed

+124
-118
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,9 +1595,9 @@ CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) {
15951595
/// Obtain information that uniquely identifies a target entry. This
15961596
/// consists of the file and device IDs as well as line number associated with
15971597
/// the relevant entry source location.
1598-
static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
1599-
unsigned &DeviceID, unsigned &FileID,
1600-
unsigned &LineNum) {
1598+
static llvm::TargetRegionEntryInfo
1599+
getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
1600+
StringRef ParentName = "") {
16011601
SourceManager &SM = C.getSourceManager();
16021602

16031603
// The loc should be always valid and have a file ID (the user cannot use
@@ -1617,9 +1617,8 @@ static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
16171617
<< PLoc.getFilename() << EC.message();
16181618
}
16191619

1620-
DeviceID = ID.getDevice();
1621-
FileID = ID.getFile();
1622-
LineNum = PLoc.getLine();
1620+
return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
1621+
PLoc.getLine());
16231622
}
16241623

16251624
Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
@@ -1635,11 +1634,9 @@ Address CGOpenMPRuntime::getAddrOfDeclareTargetVar(const VarDecl *VD) {
16351634
llvm::raw_svector_ostream OS(PtrName);
16361635
OS << CGM.getMangledName(GlobalDecl(VD));
16371636
if (!VD->isExternallyVisible()) {
1638-
unsigned DeviceID, FileID, Line;
1639-
getTargetEntryUniqueInfo(CGM.getContext(),
1640-
VD->getCanonicalDecl()->getBeginLoc(),
1641-
DeviceID, FileID, Line);
1642-
OS << llvm::format("_%x", FileID);
1637+
auto EntryInfo = getTargetEntryUniqueInfo(
1638+
CGM.getContext(), VD->getCanonicalDecl()->getBeginLoc());
1639+
OS << llvm::format("_%x", EntryInfo.FileID);
16431640
}
16441641
OS << "_decl_tgt_ref_ptr";
16451642
}
@@ -1858,16 +1855,10 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
18581855
// Produce the unique prefix to identify the new target regions. We use
18591856
// the source location of the variable declaration which we know to not
18601857
// conflict with any target region.
1861-
unsigned DeviceID;
1862-
unsigned FileID;
1863-
unsigned Line;
1864-
getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line);
1858+
auto EntryInfo =
1859+
getTargetEntryUniqueInfo(CGM.getContext(), Loc, VD->getName());
18651860
SmallString<128> Buffer, Out;
1866-
{
1867-
llvm::raw_svector_ostream OS(Buffer);
1868-
OS << "__omp_offloading_" << llvm::format("_%x", DeviceID)
1869-
<< llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line;
1870-
}
1861+
EntryInfo.getTargetRegionEntryFnName(Buffer);
18711862

18721863
const Expr *Init = VD->getAnyInitializer();
18731864
if (CGM.getLangOpts().CPlusPlus && PerformInit) {
@@ -1913,9 +1904,12 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
19131904

19141905
// Register the information for the entry associated with the constructor.
19151906
Out.clear();
1907+
auto CtorEntryInfo = EntryInfo;
1908+
CtorEntryInfo.ParentName = Twine(Buffer, "_ctor").toStringRef(Out);
1909+
llvm::errs() << "Registering var ctor: " << Twine(Buffer, "_ctor") << "\n";
19161910
OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
1917-
DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor,
1918-
ID, llvm::OffloadEntriesInfoManager::OMPTargetRegionEntryCtor,
1911+
CtorEntryInfo, Ctor, ID,
1912+
llvm::OffloadEntriesInfoManager::OMPTargetRegionEntryCtor,
19191913
CGM.getLangOpts().OpenMPIsDevice);
19201914
}
19211915
if (VD->getType().isDestructedType() != QualType::DK_none) {
@@ -1961,9 +1955,11 @@ bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
19611955
}
19621956
// Register the information for the entry associated with the destructor.
19631957
Out.clear();
1958+
auto DtorEntryInfo = EntryInfo;
1959+
DtorEntryInfo.ParentName = Twine(Buffer, "_dtor").toStringRef(Out);
19641960
OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
1965-
DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor,
1966-
ID, llvm::OffloadEntriesInfoManager::OMPTargetRegionEntryDtor,
1961+
DtorEntryInfo, Dtor, ID,
1962+
llvm::OffloadEntriesInfoManager::OMPTargetRegionEntryDtor,
19671963
CGM.getLangOpts().OpenMPIsDevice);
19681964
}
19691965
return CGM.getLangOpts().OpenMPIsDevice;
@@ -3001,8 +2997,7 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
30012997
auto &&TargetRegionMetadataEmitter =
30022998
[this, &C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt,
30032999
&GetMDString](
3004-
unsigned DeviceID, unsigned FileID, StringRef ParentName,
3005-
unsigned Line,
3000+
llvm::TargetRegionEntryInfo EntryInfo,
30063001
const llvm::OffloadEntriesInfoManager::OffloadEntryInfoTargetRegion
30073002
&E) {
30083003
// Generate metadata for target regions. Each entry of this metadata
@@ -3015,24 +3010,26 @@ void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
30153010
// - Entry 4 -> Line in the file where the entry was identified.
30163011
// - Entry 5 -> Order the entry was created.
30173012
// The first element of the metadata node is the kind.
3018-
llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID),
3019-
GetMDInt(FileID), GetMDString(ParentName),
3020-
GetMDInt(Line), GetMDInt(E.getOrder())};
3013+
llvm::Metadata *Ops[] = {
3014+
GetMDInt(E.getKind()), GetMDInt(EntryInfo.DeviceID),
3015+
GetMDInt(EntryInfo.FileID), GetMDString(EntryInfo.ParentName),
3016+
GetMDInt(EntryInfo.Line), GetMDInt(E.getOrder())};
30213017

30223018
SourceLocation Loc;
30233019
for (auto I = CGM.getContext().getSourceManager().fileinfo_begin(),
30243020
E = CGM.getContext().getSourceManager().fileinfo_end();
30253021
I != E; ++I) {
3026-
if (I->getFirst()->getUniqueID().getDevice() == DeviceID &&
3027-
I->getFirst()->getUniqueID().getFile() == FileID) {
3022+
if (I->getFirst()->getUniqueID().getDevice() == EntryInfo.DeviceID &&
3023+
I->getFirst()->getUniqueID().getFile() == EntryInfo.FileID) {
30283024
Loc = CGM.getContext().getSourceManager().translateFileLineCol(
3029-
I->getFirst(), Line, 1);
3025+
I->getFirst(), EntryInfo.Line, 1);
30303026
break;
30313027
}
30323028
}
30333029
// Save this entry in the right position of the ordered entries array.
3034-
OrderedEntries[E.getOrder()] = std::make_tuple(&E, Loc, ParentName);
3035-
ParentFunctions[E.getOrder()] = ParentName;
3030+
OrderedEntries[E.getOrder()] =
3031+
std::make_tuple(&E, Loc, StringRef(EntryInfo.ParentName));
3032+
ParentFunctions[E.getOrder()] = StringRef(EntryInfo.ParentName);
30363033

30373034
// Add metadata to the named metadata node.
30383035
MD->addOperand(llvm::MDNode::get(C, Ops));
@@ -3195,15 +3192,18 @@ void CGOpenMPRuntime::loadOffloadInfoMetadata() {
31953192
llvm_unreachable("Unexpected metadata!");
31963193
break;
31973194
case llvm::OffloadEntriesInfoManager::OffloadEntryInfo::
3198-
OffloadingEntryInfoTargetRegion:
3195+
OffloadingEntryInfoTargetRegion: {
31993196
assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
32003197
"only required for the "
32013198
"device code generation.");
3199+
llvm::TargetRegionEntryInfo EntryInfo(/*ParentName=*/GetMDString(3),
3200+
/*DeviceID=*/GetMDInt(1),
3201+
/*FileID=*/GetMDInt(2),
3202+
/*Line=*/GetMDInt(4));
32023203
OffloadEntriesInfoManager.initializeTargetRegionEntryInfo(
3203-
/*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2),
3204-
/*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4),
3205-
/*Order=*/GetMDInt(5));
3204+
EntryInfo, /*Order=*/GetMDInt(5));
32063205
break;
3206+
}
32073207
case llvm::OffloadEntriesInfoManager::OffloadEntryInfo::
32083208
OffloadingEntryInfoDeviceGlobalVar:
32093209
assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
@@ -6209,7 +6209,7 @@ void CGOpenMPRuntime::emitTargetOutlinedFunction(
62096209
const OMPExecutableDirective &D, StringRef ParentName,
62106210
llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
62116211
bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
6212-
assert(!ParentName.empty() && "Invalid target region parent name!");
6212+
assert(!ParentName.empty() && "Invalid target entry parent name!");
62136213
HasEmittedTargetRegion = true;
62146214
SmallVector<std::pair<const Expr *, const Expr *>, 4> Allocators;
62156215
for (const auto *C : D.getClausesOfKind<OMPUsesAllocatorsClause>()) {
@@ -6292,17 +6292,10 @@ void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
62926292

62936293
const bool BuildOutlinedFn = CGM.getLangOpts().OpenMPIsDevice ||
62946294
!CGM.getLangOpts().OpenMPOffloadMandatory;
6295-
unsigned DeviceID;
6296-
unsigned FileID;
6297-
unsigned Line;
6298-
getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID,
6299-
Line);
6295+
auto EntryInfo =
6296+
getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), ParentName);
63006297
SmallString<64> EntryFnName;
6301-
{
6302-
llvm::raw_svector_ostream OS(EntryFnName);
6303-
OS << "__omp_offloading" << llvm::format("_%x", DeviceID)
6304-
<< llvm::format("_%x_", FileID) << ParentName << "_l" << Line;
6305-
}
6298+
EntryInfo.getTargetRegionEntryFnName(EntryFnName);
63066299

63076300
const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
63086301

@@ -6357,7 +6350,7 @@ void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
63576350

63586351
// Register the information for the entry associated with this target region.
63596352
OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
6360-
DeviceID, FileID, ParentName, Line, TargetRegionEntryAddr, OutlinedFnID,
6353+
EntryInfo, TargetRegionEntryAddr, OutlinedFnID,
63616354
llvm::OffloadEntriesInfoManager::OMPTargetRegionEntryTargetRegion,
63626355
CGM.getLangOpts().OpenMPIsDevice);
63636356

@@ -10352,16 +10345,12 @@ void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
1035210345

1035310346
if (RequiresDeviceCodegen) {
1035410347
const auto &E = *cast<OMPExecutableDirective>(S);
10355-
unsigned DeviceID;
10356-
unsigned FileID;
10357-
unsigned Line;
10358-
getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID,
10359-
FileID, Line);
10348+
auto EntryInfo =
10349+
getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), ParentName);
1036010350

1036110351
// Is this a target region that should not be emitted as an entry point? If
1036210352
// so just signal we are done with this target region.
10363-
if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID,
10364-
ParentName, Line))
10353+
if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(EntryInfo))
1036510354
return;
1036610355

1036710356
switch (E.getDirectiveKind()) {

clang/test/OpenMP/declare_target_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
// CHECK-DAG: define {{.*}}i32 @{{.*}}{{foo|bar|baz2|baz3|FA|f_method}}{{.*}}()
5252
// CHECK-DAG: define {{.*}}void @{{.*}}TemplateClass{{.*}}(ptr {{[^,]*}} %{{.*}})
5353
// CHECK-DAG: define {{.*}}i32 @{{.*}}TemplateClass{{.*}}f_method{{.*}}(ptr {{[^,]*}} %{{.*}})
54-
// CHECK-DAG: define {{.*}}void @__omp_offloading__{{.*}}_globals_l[[@LINE+78]]_ctor()
54+
// CHECK-DAG: define {{.*}}void @__omp_offloading_{{.*}}_globals_l[[@LINE+78]]_ctor()
5555

5656
#ifndef HEADER
5757
#define HEADER

clang/test/OpenMP/nvptx_declare_target_var_ctor_dtor_codegen.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ int caz() { return 0; }
4343

4444
static int c = foo() + bar() + baz();
4545
#pragma omp declare target (c)
46-
// HOST-DAG: @[[C_CTOR:__omp_offloading__.+_c_l44_ctor]] = private constant i8 0
47-
// DEVICE-DAG: define weak_odr protected void [[C_CTOR:@__omp_offloading__.+_c_l44_ctor]]()
46+
// HOST-DAG: @[[C_CTOR:__omp_offloading_.+_c_l44_ctor]] = private constant i8 0
47+
// DEVICE-DAG: define weak_odr protected void [[C_CTOR:@__omp_offloading_.+_c_l44_ctor]]()
4848
// DEVICE-DAG: call noundef i32 [[FOO]]()
4949
// DEVICE-DAG: call noundef i32 [[BAR]]()
5050
// DEVICE-DAG: call noundef i32 [[BAZ]]()
@@ -60,15 +60,15 @@ struct S {
6060
#pragma omp declare target
6161
S cd = doo() + car() + caz() + baz();
6262
#pragma omp end declare target
63-
// HOST-DAG: @[[CD_CTOR:__omp_offloading__.+_cd_l61_ctor]] = private constant i8 0
64-
// DEVICE-DAG: define weak_odr protected void [[CD_CTOR:@__omp_offloading__.+_cd_l61_ctor]]()
63+
// HOST-DAG: @[[CD_CTOR:__omp_offloading_.+_cd_l61_ctor]] = private constant i8 0
64+
// DEVICE-DAG: define weak_odr protected void [[CD_CTOR:@__omp_offloading_.+_cd_l61_ctor]]()
6565
// DEVICE-DAG: call noundef i32 [[DOO]]()
6666
// DEVICE-DAG: call noundef i32 [[CAR]]()
6767
// DEVICE-DAG: call noundef i32 [[CAZ]]()
6868
// DEVICE-DAG: ret void
6969

70-
// HOST-DAG: @[[CD_DTOR:__omp_offloading__.+_cd_l61_dtor]] = private constant i8 0
71-
// DEVICE-DAG: define weak_odr protected void [[CD_DTOR:@__omp_offloading__.+_cd_l61_dtor]]()
70+
// HOST-DAG: @[[CD_DTOR:__omp_offloading_.+_cd_l61_dtor]] = private constant i8 0
71+
// DEVICE-DAG: define weak_odr protected void [[CD_DTOR:@__omp_offloading_.+_cd_l61_dtor]]()
7272
// DEVICE-DAG: call void
7373
// DEVICE-DAG: ret void
7474

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#include "llvm/IR/IRBuilder.h"
2121
#include "llvm/Support/Allocator.h"
2222
#include <forward_list>
23+
#include <map>
2324

2425
namespace llvm {
2526
class CanonicalLoopInfo;
27+
class OffloadEntriesInfoManager;
2628

2729
/// Move the instruction after an InsertPoint to the beginning of another
2830
/// BasicBlock.
@@ -1681,6 +1683,33 @@ class OpenMPIRBuilder {
16811683
const Twine &Name = {});
16821684
};
16831685

1686+
/// Data structure to contain the information needed to uniquely identify
1687+
/// a target entry.
1688+
struct TargetRegionEntryInfo {
1689+
std::string ParentName;
1690+
unsigned DeviceID;
1691+
unsigned FileID;
1692+
unsigned Line;
1693+
1694+
TargetRegionEntryInfo() : ParentName(""), DeviceID(0), FileID(0), Line(0) {}
1695+
TargetRegionEntryInfo(StringRef ParentName, unsigned DeviceID,
1696+
unsigned FileID, unsigned Line)
1697+
: ParentName(ParentName), DeviceID(DeviceID), FileID(FileID), Line(Line) {
1698+
}
1699+
1700+
static void getTargetRegionEntryFnName(SmallVectorImpl<char> &Name,
1701+
StringRef ParentName,
1702+
unsigned DeviceID, unsigned FileID,
1703+
unsigned Line);
1704+
1705+
void getTargetRegionEntryFnName(SmallVectorImpl<char> &Name);
1706+
1707+
bool operator<(const TargetRegionEntryInfo RHS) const {
1708+
return std::make_tuple(ParentName, DeviceID, FileID, Line) <
1709+
std::make_tuple(RHS.ParentName, RHS.DeviceID, RHS.FileID, RHS.Line);
1710+
}
1711+
};
1712+
16841713
/// Class that manages information about offload code regions and data
16851714
class OffloadEntriesInfoManager {
16861715
/// Number of entries registered so far.
@@ -1782,22 +1811,19 @@ class OffloadEntriesInfoManager {
17821811

17831812
/// Initialize target region entry.
17841813
/// This is ONLY needed for DEVICE compilation.
1785-
void initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
1786-
StringRef ParentName, unsigned LineNum,
1814+
void initializeTargetRegionEntryInfo(const TargetRegionEntryInfo &EntryInfo,
17871815
unsigned Order);
17881816
/// Register target region entry.
1789-
void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
1790-
StringRef ParentName, unsigned LineNum,
1817+
void registerTargetRegionEntryInfo(const TargetRegionEntryInfo &EntryInfo,
17911818
Constant *Addr, Constant *ID,
17921819
OMPTargetRegionEntryKind Flags,
17931820
bool IsDevice);
17941821
/// Return true if a target region entry with the provided information
17951822
/// exists.
1796-
bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
1797-
StringRef ParentName, unsigned LineNum,
1823+
bool hasTargetRegionEntryInfo(const TargetRegionEntryInfo &EntryInfo,
17981824
bool IgnoreAddressId = false) const;
17991825
/// brief Applies action \a Action on all registered entries.
1800-
typedef function_ref<void(unsigned, unsigned, StringRef, unsigned,
1826+
typedef function_ref<void(const TargetRegionEntryInfo &EntryInfo,
18011827
const OffloadEntryInfoTargetRegion &)>
18021828
OffloadTargetRegionEntryInfoActTy;
18031829
void
@@ -1868,17 +1894,9 @@ class OffloadEntriesInfoManager {
18681894
const OffloadDeviceGlobalVarEntryInfoActTy &Action);
18691895

18701896
private:
1871-
// Storage for target region entries kind. The storage is to be indexed by
1872-
// file ID, device ID, parent function name and line number.
1873-
typedef DenseMap<unsigned, OffloadEntryInfoTargetRegion>
1874-
OffloadEntriesTargetRegionPerLine;
1875-
typedef StringMap<OffloadEntriesTargetRegionPerLine>
1876-
OffloadEntriesTargetRegionPerParentName;
1877-
typedef DenseMap<unsigned, OffloadEntriesTargetRegionPerParentName>
1878-
OffloadEntriesTargetRegionPerFile;
1879-
typedef DenseMap<unsigned, OffloadEntriesTargetRegionPerFile>
1880-
OffloadEntriesTargetRegionPerDevice;
1881-
typedef OffloadEntriesTargetRegionPerDevice OffloadEntriesTargetRegionTy;
1897+
// Storage for target region entries kind.
1898+
typedef std::map<TargetRegionEntryInfo, OffloadEntryInfoTargetRegion>
1899+
OffloadEntriesTargetRegionTy;
18821900
OffloadEntriesTargetRegionTy OffloadEntriesTargetRegion;
18831901
/// Storage for device global variable entries kind. The storage is to be
18841902
/// indexed by mangled name.

0 commit comments

Comments
 (0)