Skip to content

Commit 3cd3157

Browse files
Updated based on feedback
1 parent 22f6af4 commit 3cd3157

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,63 +1829,60 @@ void CGOpenMPRuntime::registerVTableOffloadEntry(llvm::GlobalVariable *VTable,
18291829
llvm::GlobalValue::WeakODRLinkage);
18301830
}
18311831

1832-
// Register VTable by scanning through the map clause of OpenMP target region.
1833-
void CGOpenMPRuntime::registerVTable(const OMPExecutableDirective &D) {
1834-
// Get CXXRecordDecl and VarDecl from Expr.
1835-
auto getVTableDecl = [](const Expr *E) {
1836-
QualType VDTy = E->getType();
1837-
CXXRecordDecl *CXXRecord = nullptr;
1838-
if (const auto *RefType = VDTy->getAs<LValueReferenceType>())
1839-
VDTy = RefType->getPointeeType();
1840-
if (VDTy->isPointerType())
1841-
CXXRecord = VDTy->getPointeeType()->getAsCXXRecordDecl();
1842-
else
1843-
CXXRecord = VDTy->getAsCXXRecordDecl();
1844-
1845-
const VarDecl *VD = nullptr;
1846-
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
1847-
VD = cast<VarDecl>(DRE->getDecl());
1848-
return std::pair<CXXRecordDecl *, const VarDecl *>(CXXRecord, VD);
1849-
};
1850-
1851-
// Emit VTable and register the VTable to OpenMP offload entry recursively.
1852-
std::function<void(CodeGenModule &, CXXRecordDecl *, const VarDecl *)>
1853-
emitAndRegisterVTable = [&emitAndRegisterVTable](CodeGenModule &CGM,
1854-
CXXRecordDecl *CXXRecord,
1855-
const VarDecl *VD) {
1832+
void CGOpenMPRuntime::emitAndRegisterVTable(CodeGenModule &CGM,
1833+
CXXRecordDecl *CXXRecord,
1834+
const VarDecl *VD) {
18561835
// Register C++ VTable to OpenMP Offload Entry if it's a new
18571836
// CXXRecordDecl.
18581837
if (CXXRecord && CXXRecord->isDynamicClass() &&
18591838
CGM.getOpenMPRuntime().VTableDeclMap.find(CXXRecord) ==
18601839
CGM.getOpenMPRuntime().VTableDeclMap.end()) {
18611840
CGM.getOpenMPRuntime().VTableDeclMap.try_emplace(CXXRecord, VD);
18621841
CGM.EmitVTable(CXXRecord);
1863-
auto VTables = CGM.getVTables();
1864-
auto *VTablesAddr = VTables.GetAddrOfVTable(CXXRecord);
1865-
if (VTablesAddr) {
1842+
CodeGenVTables VTables = CGM.getVTables();
1843+
llvm::GlobalVariable *VTablesAddr = VTables.GetAddrOfVTable(CXXRecord);
1844+
if (VTablesAddr)
18661845
CGM.getOpenMPRuntime().registerVTableOffloadEntry(VTablesAddr, VD);
1867-
}
18681846
// Emit VTable for all the fields containing dynamic CXXRecord
18691847
for (const FieldDecl *Field : CXXRecord->fields()) {
18701848
if (CXXRecordDecl *RecordDecl =
1871-
Field->getType()->getAsCXXRecordDecl()) {
1849+
Field->getType()->getAsCXXRecordDecl())
18721850
emitAndRegisterVTable(CGM, RecordDecl, VD);
1873-
}
1851+
18741852
}
18751853
// Emit VTable for all dynamic parent class
18761854
for (CXXBaseSpecifier &Base : CXXRecord->bases()) {
18771855
if (CXXRecordDecl *BaseDecl =
1878-
Base.getType()->getAsCXXRecordDecl()) {
1856+
Base.getType()->getAsCXXRecordDecl())
18791857
emitAndRegisterVTable(CGM, BaseDecl, VD);
1880-
}
1858+
18811859
}
18821860
}
18831861
};
18841862

1863+
1864+
void CGOpenMPRuntime::registerVTable(const OMPExecutableDirective &D) {
1865+
// Register VTable by scanning through the map clause of OpenMP target region.
1866+
// Get CXXRecordDecl and VarDecl from Expr.
1867+
auto GetVTableDecl = [](const Expr *E) {
1868+
QualType VDTy = E->getType();
1869+
CXXRecordDecl *CXXRecord = nullptr;
1870+
if (const auto *RefType = VDTy->getAs<LValueReferenceType>())
1871+
VDTy = RefType->getPointeeType();
1872+
if (VDTy->isPointerType())
1873+
CXXRecord = VDTy->getPointeeType()->getAsCXXRecordDecl();
1874+
else
1875+
CXXRecord = VDTy->getAsCXXRecordDecl();
1876+
1877+
const VarDecl *VD = nullptr;
1878+
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
1879+
VD = cast<VarDecl>(DRE->getDecl());
1880+
return std::pair<CXXRecordDecl *, const VarDecl *>(CXXRecord, VD);
1881+
};
18851882
// Collect VTable from OpenMP map clause.
18861883
for (const auto *C : D.getClausesOfKind<OMPMapClause>()) {
18871884
for (const auto *E : C->varlist()) {
1888-
auto DeclPair = getVTableDecl(E);
1885+
auto DeclPair = GetVTableDecl(E);
18891886
emitAndRegisterVTable(CGM, DeclPair.first, DeclPair.second);
18901887
}
18911888
}
@@ -10075,8 +10072,8 @@ void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
1007510072
// target data by checking if S is a executable directive (target).
1007610073
if (isa<OMPExecutableDirective>(S) &&
1007710074
isOpenMPTargetDataManagementDirective(
10078-
cast<OMPExecutableDirective>(S)->getDirectiveKind())) {
10079-
auto &E = *cast<OMPExecutableDirective>(S);
10075+
dyn_cast<OMPExecutableDirective>(S)->getDirectiveKind())) {
10076+
auto &E = *dyn_cast<OMPExecutableDirective>(S);
1008010077
// Don't need to check if it's device compile
1008110078
// since scanForTargetRegionsFunctions currently only called
1008210079
// in device compilation.

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ class CGOpenMPRuntime {
606606
Address DependenciesArray);
607607

608608
/// Keep track of VTable Declarations so we don't register duplicate VTable.
609-
llvm::DenseMap<CXXRecordDecl*, const VarDecl*> VTableDeclMap;
609+
llvm::SmallDenseMap<CXXRecordDecl *, const VarDecl *> VTableDeclMap;
610610

611611
public:
612612
explicit CGOpenMPRuntime(CodeGenModule &CGM);
@@ -1124,6 +1124,13 @@ class CGOpenMPRuntime {
11241124
/// \param D OpenMP target directive.
11251125
virtual void registerVTable(const OMPExecutableDirective &D);
11261126

1127+
/// Emit and register VTable for the C++ class in OpenMP offload entry.
1128+
/// \param CXXRecord C++ class decl.
1129+
/// \param VD Variable decl which holds VTable.
1130+
virtual void emitAndRegisterVTable(CodeGenModule &CGM,
1131+
CXXRecordDecl *CXXRecord,
1132+
const VarDecl *VD);
1133+
11271134
/// Creates artificial threadprivate variable with name \p Name and type \p
11281135
/// VarType.
11291136
/// \param VarType Type of the artificial threadprivate variable.

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7618,9 +7618,9 @@ void CodeGenFunction::EmitOMPUseDeviceAddrClause(
76187618
void CodeGenFunction::EmitOMPTargetDataDirective(
76197619
const OMPTargetDataDirective &S) {
76207620
// Emit vtable only from host for target data directive.
7621-
if (!CGM.getLangOpts().OpenMPIsTargetDevice) {
7621+
if (!CGM.getLangOpts().OpenMPIsTargetDevice)
76227622
CGM.getOpenMPRuntime().registerVTable(S);
7623-
}
7623+
76247624
CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true,
76257625
/*SeparateBeginEndCalls=*/true);
76267626

0 commit comments

Comments
 (0)