-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[OpenMP][clang] Register Vtables on device for indirect calls #159856
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
Open
Jason-VanBeusekom
wants to merge
5
commits into
llvm:main
Choose a base branch
from
Jason-VanBeusekom:Register-Vtables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7aafa4
[OpenMP][clang] Register Vtables on device for indirect calls
Jason-VanBeusekom 22f6af4
Review feedback
Jason-VanBeusekom 3cd3157
Updated based on feedback
Jason-VanBeusekom d86188b
split codegen tests based on feedback
Jason-VanBeusekom 0dc410c
format fix
Jason-VanBeusekom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1771,12 +1771,118 @@ void CGOpenMPRuntime::emitDeclareTargetFunction(const FunctionDecl *FD, | |||||||||||||
| Addr->setVisibility(llvm::GlobalValue::ProtectedVisibility); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Register the indirect Vtable: | ||||||||||||||
| // This is similar to OMPTargetGlobalVarEntryIndirect, except that the | ||||||||||||||
| // size field refers to the size of memory pointed to, not the size of | ||||||||||||||
| // the pointer symbol itself (which is implicitly the size of a pointer). | ||||||||||||||
| OMPBuilder.OffloadInfoManager.registerDeviceGlobalVarEntryInfo( | ||||||||||||||
| Name, Addr, CGM.GetTargetTypeStoreSize(CGM.VoidPtrTy).getQuantity(), | ||||||||||||||
| llvm::OffloadEntriesInfoManager::OMPTargetGlobalVarEntryIndirect, | ||||||||||||||
| llvm::GlobalValue::WeakODRLinkage); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| void CGOpenMPRuntime::registerVTableOffloadEntry(llvm::GlobalVariable *VTable, | ||||||||||||||
| const VarDecl *VD) { | ||||||||||||||
| // TODO: add logic to avoid duplicate vtable registrations per | ||||||||||||||
| // translation unit; though for external linkage, this should no | ||||||||||||||
| // longer be an issue - or at least we can avoid the issue by | ||||||||||||||
| // checking for an existing offloading entry. But, perhaps the | ||||||||||||||
| // better approach is to defer emission of the vtables and offload | ||||||||||||||
| // entries until later (by tracking a list of items that need to be | ||||||||||||||
| // emitted). | ||||||||||||||
|
|
||||||||||||||
| llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder(); | ||||||||||||||
|
|
||||||||||||||
| // Generate a new externally visible global to point to the | ||||||||||||||
| // internally visible vtable. Doing this allows us to keep the | ||||||||||||||
| // visibility and linkage of the associated vtable unchanged while | ||||||||||||||
| // allowing the runtime to access its value. The externally | ||||||||||||||
| // visible global var needs to be emitted with a unique mangled | ||||||||||||||
| // name that won't conflict with similarly named (internal) | ||||||||||||||
| // vtables in other translation units. | ||||||||||||||
|
|
||||||||||||||
| // Register vtable with source location of dynamic object in map | ||||||||||||||
| // clause. | ||||||||||||||
| llvm::TargetRegionEntryInfo EntryInfo = getEntryInfoFromPresumedLoc( | ||||||||||||||
| CGM, OMPBuilder, VD->getCanonicalDecl()->getBeginLoc(), | ||||||||||||||
| VTable->getName()); | ||||||||||||||
|
|
||||||||||||||
| llvm::GlobalVariable *Addr = VTable; | ||||||||||||||
| size_t PointerSize = CGM.getDataLayout().getPointerSize(); | ||||||||||||||
| SmallString<128> AddrName; | ||||||||||||||
| OMPBuilder.OffloadInfoManager.getTargetRegionEntryFnName(AddrName, EntryInfo); | ||||||||||||||
| AddrName.append("addr"); | ||||||||||||||
|
|
||||||||||||||
| if (CGM.getLangOpts().OpenMPIsTargetDevice) { | ||||||||||||||
| Addr = new llvm::GlobalVariable( | ||||||||||||||
| CGM.getModule(), VTable->getType(), | ||||||||||||||
| /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, VTable, | ||||||||||||||
| AddrName, | ||||||||||||||
| /*InsertBefore*/ nullptr, llvm::GlobalValue::NotThreadLocal, | ||||||||||||||
| CGM.getModule().getDataLayout().getDefaultGlobalsAddressSpace()); | ||||||||||||||
| Addr->setVisibility(llvm::GlobalValue::ProtectedVisibility); | ||||||||||||||
| } | ||||||||||||||
| OMPBuilder.OffloadInfoManager.registerDeviceGlobalVarEntryInfo( | ||||||||||||||
| AddrName, VTable, | ||||||||||||||
| CGM.getDataLayout().getTypeAllocSize(VTable->getInitializer()->getType()), | ||||||||||||||
| llvm::OffloadEntriesInfoManager::OMPTargetGlobalVarEntryIndirectVTable, | ||||||||||||||
| llvm::GlobalValue::WeakODRLinkage); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| void CGOpenMPRuntime::emitAndRegisterVTable(CodeGenModule &CGM, | ||||||||||||||
| CXXRecordDecl *CXXRecord, | ||||||||||||||
| const VarDecl *VD) { | ||||||||||||||
| // Register C++ VTable to OpenMP Offload Entry if it's a new | ||||||||||||||
| // CXXRecordDecl. | ||||||||||||||
| if (CXXRecord && CXXRecord->isDynamicClass() && | ||||||||||||||
| CGM.getOpenMPRuntime().VTableDeclMap.find(CXXRecord) == | ||||||||||||||
| CGM.getOpenMPRuntime().VTableDeclMap.end()) { | ||||||||||||||
| CGM.getOpenMPRuntime().VTableDeclMap.try_emplace(CXXRecord, VD); | ||||||||||||||
| CGM.EmitVTable(CXXRecord); | ||||||||||||||
| CodeGenVTables VTables = CGM.getVTables(); | ||||||||||||||
| llvm::GlobalVariable *VTablesAddr = VTables.GetAddrOfVTable(CXXRecord); | ||||||||||||||
| if (VTablesAddr) | ||||||||||||||
| CGM.getOpenMPRuntime().registerVTableOffloadEntry(VTablesAddr, VD); | ||||||||||||||
| // Emit VTable for all the fields containing dynamic CXXRecord | ||||||||||||||
| for (const FieldDecl *Field : CXXRecord->fields()) { | ||||||||||||||
| if (CXXRecordDecl *RecordDecl = Field->getType()->getAsCXXRecordDecl()) | ||||||||||||||
| emitAndRegisterVTable(CGM, RecordDecl, VD); | ||||||||||||||
| } | ||||||||||||||
| // Emit VTable for all dynamic parent class | ||||||||||||||
| for (CXXBaseSpecifier &Base : CXXRecord->bases()) { | ||||||||||||||
| if (CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl()) | ||||||||||||||
| emitAndRegisterVTable(CGM, BaseDecl, VD); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| void CGOpenMPRuntime::registerVTable(const OMPExecutableDirective &D) { | ||||||||||||||
| // Register VTable by scanning through the map clause of OpenMP target region. | ||||||||||||||
| // Get CXXRecordDecl and VarDecl from Expr. | ||||||||||||||
| auto GetVTableDecl = [](const Expr *E) { | ||||||||||||||
| QualType VDTy = E->getType(); | ||||||||||||||
| CXXRecordDecl *CXXRecord = nullptr; | ||||||||||||||
| if (const auto *RefType = VDTy->getAs<LValueReferenceType>()) | ||||||||||||||
| VDTy = RefType->getPointeeType(); | ||||||||||||||
| if (VDTy->isPointerType()) | ||||||||||||||
| CXXRecord = VDTy->getPointeeType()->getAsCXXRecordDecl(); | ||||||||||||||
| else | ||||||||||||||
| CXXRecord = VDTy->getAsCXXRecordDecl(); | ||||||||||||||
|
|
||||||||||||||
| const VarDecl *VD = nullptr; | ||||||||||||||
| if (auto *DRE = dyn_cast<DeclRefExpr>(E)) | ||||||||||||||
| VD = cast<VarDecl>(DRE->getDecl()); | ||||||||||||||
| return std::pair<CXXRecordDecl *, const VarDecl *>(CXXRecord, VD); | ||||||||||||||
| }; | ||||||||||||||
| // Collect VTable from OpenMP map clause. | ||||||||||||||
| for (const auto *C : D.getClausesOfKind<OMPMapClause>()) { | ||||||||||||||
| for (const auto *E : C->varlist()) { | ||||||||||||||
| auto DeclPair = GetVTableDecl(E); | ||||||||||||||
| emitAndRegisterVTable(CGM, DeclPair.first, DeclPair.second); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF, | ||||||||||||||
| QualType VarType, | ||||||||||||||
| StringRef Name) { | ||||||||||||||
|
|
@@ -6249,6 +6355,7 @@ void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper( | |||||||||||||
| CGM.handleAMDGPUWavesPerEUAttr(OutlinedFn, Attr); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| registerVTable(D); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Checks if the expression is constant or does not have non-trivial function | ||||||||||||||
|
|
@@ -9955,6 +10062,19 @@ void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S, | |||||||||||||
| if (!S) | ||||||||||||||
| return; | ||||||||||||||
|
|
||||||||||||||
| // Register vtable from device for target data and target directives. | ||||||||||||||
| // Add this block here since scanForTargetRegionsFunctions ignores | ||||||||||||||
| // target data by checking if S is a executable directive (target). | ||||||||||||||
| if (isa<OMPExecutableDirective>(S) && | ||||||||||||||
| isOpenMPTargetDataManagementDirective( | ||||||||||||||
| dyn_cast<OMPExecutableDirective>(S)->getDirectiveKind())) { | ||||||||||||||
|
Comment on lines
+10068
to
+10070
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| auto &E = *dyn_cast<OMPExecutableDirective>(S); | ||||||||||||||
| // Don't need to check if it's device compile | ||||||||||||||
| // since scanForTargetRegionsFunctions currently only called | ||||||||||||||
| // in device compilation. | ||||||||||||||
| registerVTable(E); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Codegen OMP target directives that offload compute to the device. | ||||||||||||||
| bool RequiresDeviceCodegen = | ||||||||||||||
| isa<OMPExecutableDirective>(S) && | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm-bc %s -o %t-ppc-host.bc -fopenmp-version=52 -stdlib=libc++ | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple nvptx64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - -debug-info-kind=limited -fopenmp-version=52 -stdlib=libc++ | FileCheck %s | ||
| // expected-no-diagnostics | ||
|
|
||
| // CHECK-DAG: @_ZTV7Derived | ||
| // CHECK-DAG: @_ZTV4Base | ||
| template <typename T> | ||
| class Container { | ||
| private: | ||
| T value; | ||
| public: | ||
| Container() : value() {} | ||
| Container(T val) : value(val) {} | ||
|
|
||
| T getValue() const { return value; } | ||
|
|
||
| void setValue(T val) { value = val; } | ||
| }; | ||
|
|
||
| class Base { | ||
| public: | ||
| virtual void foo() {} | ||
| }; | ||
| class Derived : public Base {}; | ||
|
|
||
| class Test { | ||
| public: | ||
| Container<Derived> v; | ||
| }; | ||
|
|
||
| int main() { | ||
| Test test; | ||
| Derived d; | ||
| test.v.setValue(d); | ||
|
|
||
| // Make sure we emit VTable for type indirectly (template specialized type) | ||
| #pragma omp target map(test) | ||
| { | ||
| test.v.getValue().foo(); | ||
| } | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm-bc %s -o %t-ppc-host.bc -fopenmp-version=52 | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple nvptx64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - -debug-info-kind=limited -fopenmp-version=52 | FileCheck %s | ||
| // expected-no-diagnostics | ||
|
|
||
| // Make sure both host and device compilation emit vtable for Dervied | ||
| // CHECK-DAG: $_ZN7DerivedD1Ev = comdat any | ||
| // CHECK-DAG: $_ZN7DerivedD0Ev = comdat any | ||
| // CHECK-DAG: $_ZN7Derived5BaseAEi = comdat any | ||
| // CHECK-DAG: $_ZN7Derived8DerivedBEv = comdat any | ||
| // CHECK-DAG: $_ZN7DerivedD2Ev = comdat any | ||
| // CHECK-DAG: $_ZN4BaseD2Ev = comdat any | ||
| // CHECK-DAG: $_ZTV7Derived = comdat any | ||
| class Base { | ||
| public: | ||
|
|
||
| virtual ~Base() = default; | ||
|
|
||
| virtual void BaseA(int a) { } | ||
| }; | ||
|
|
||
| // CHECK: @_ZTV7Derived = linkonce_odr unnamed_addr constant { [6 x ptr] } | ||
| class Derived : public Base { | ||
| public: | ||
|
|
||
| ~Derived() override = default; | ||
|
|
||
| void BaseA(int a) override { x = a; } | ||
|
|
||
| virtual void DerivedB() { } | ||
| private: | ||
| int x; | ||
| }; | ||
|
|
||
| int main() { | ||
|
|
||
| Derived d; | ||
| Base& c = d; | ||
| int a = 50; | ||
| // Should emit vtable for Derived since d is added to map clause | ||
| #pragma omp target data map (to: d, a) | ||
| { | ||
| #pragma omp target map(d) | ||
| { | ||
| c.BaseA(a); | ||
| } | ||
| } | ||
| return 0; | ||
| } |
43 changes: 43 additions & 0 deletions
43
clang/test/OpenMP/target_vtable_codegen_implicit_namespace.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm-bc %s -o %t-ppc-host.bc -fopenmp-version=52 | ||
| // RUN: %clang_cc1 -verify -fopenmp -Wno-openmp-mapping -x c++ -triple nvptx64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-cuda-mode -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - -debug-info-kind=limited -fopenmp-version=52 | FileCheck %s | ||
| // expected-no-diagnostics | ||
|
|
||
| namespace { | ||
|
|
||
| // Make sure both host and device compilation emit vtable for Dervied | ||
| // CHECK-DAG: @_ZTVN12_GLOBAL__N_17DerivedE | ||
| // CHECK-DAG: @_ZN12_GLOBAL__N_17DerivedD1Ev | ||
| // CHECK-DAG: @_ZN12_GLOBAL__N_17DerivedD0Ev | ||
| // CHECK-DAG: @_ZN12_GLOBAL__N_17Derived5BaseAEi | ||
| // CHECK-DAG: @_ZN12_GLOBAL__N_17Derived8DerivedBEv | ||
| class Base { | ||
| public: | ||
| virtual ~Base() = default; | ||
| virtual void BaseA(int a) { } | ||
| }; | ||
|
|
||
| class Derived : public Base { | ||
| public: | ||
| ~Derived() override = default; | ||
| void BaseA(int a) override { x = a; } | ||
| virtual void DerivedB() { } | ||
| private: | ||
| int x; | ||
| }; | ||
|
|
||
| }; | ||
|
|
||
| int main() { | ||
|
|
||
| Derived d; | ||
| Base& c = d; | ||
| int a = 50; | ||
| #pragma omp target data map (to: d, a) | ||
| { | ||
| #pragma omp target | ||
| { | ||
| c.BaseA(a); | ||
| } | ||
| } | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it is a MemberExprRef?