Add reverse-mode support for CUDA shared memory attributes - #1826
Add reverse-mode support for CUDA shared memory attributes#1826Vedant2005goyal wants to merge 1 commit into
Conversation
|
clang-tidy review says "All clean, LGTM! 👍" |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
b2dbf0d to
52bcea5
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
52bcea5 to
924c7c5
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
| if (VD->getType()->isIncompleteArrayType()) { | ||
| llvm::SmallVector<Expr*, 2> args = { | ||
| BuildDeclRef(VDDerived), | ||
| BuildDeclRef(m_SharedMemSizeParam) |
There was a problem hiding this comment.
warning: use of undeclared identifier 'm_SharedMemSizeParam' [clang-diagnostic-error]
BuildDeclRef(m_SharedMemSizeParam)
^| BuildDeclRef(VDDerived), | ||
| BuildDeclRef(m_SharedMemSizeParam) | ||
| }; | ||
| m_HasDynamicSharedMem = true; |
There was a problem hiding this comment.
warning: use of undeclared identifier 'm_HasDynamicSharedMem' [clang-diagnostic-error]
m_HasDynamicSharedMem = true;
^|
|
||
| params.push_back(dPVD); | ||
| } | ||
| if (FD->hasAttr<CUDAGlobalAttr>() && m_HasDynamicSharedMem) { |
There was a problem hiding this comment.
warning: use of undeclared identifier 'm_HasDynamicSharedMem' [clang-diagnostic-error]
if (FD->hasAttr<CUDAGlobalAttr>() && m_HasDynamicSharedMem) {
^| II, | ||
| m_Context.getSizeType()); | ||
| m_Sema.PushOnScopeChains( SharedBytesPVD, getCurrentScope(), /*AddToContext=*/false); | ||
| m_SharedMemSizeParam = SharedBytesPVD; |
There was a problem hiding this comment.
warning: use of undeclared identifier 'm_SharedMemSizeParam' [clang-diagnostic-error]
m_SharedMemSizeParam = SharedBytesPVD;
^9f8f95d to
0845ba4
Compare
b821744 to
f23b3d9
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
1 similar comment
|
clang-tidy review says "All clean, LGTM! 👍" |
| if (!isDynamicSharedMem && VD->hasAttr<clang::CUDASharedAttr>()) { | ||
| auto* SharedAttr = clang::CUDASharedAttr::CreateImplicit( | ||
| m_Context, VD->getAttr<clang::CUDASharedAttr>()->getRange()); | ||
| SharedAttr->setImplicit(false); |
There was a problem hiding this comment.
Why do we create implicit attribute and then change that -- can't we use the usual way of creating attributes?
There was a problem hiding this comment.
Thanks for the review. Older versions of Clang have a problem with the standard Create() method, so using an implicit attribute is a workaround for the problem
| VDClone->setStorageClass(VD->getStorageClass() == clang::SC_Extern | ||
| ? clang::SC_Extern | ||
| : clang::SC_Static); | ||
| } |
There was a problem hiding this comment.
I believe that code should go in its own function.
f23b3d9 to
7b32563
Compare
| } else { | ||
| // Dynamic Shared Memory: Automate zero-init purely via AST | ||
| Expr* derivedRef = BuildDeclRef(VDDerived); | ||
| Expr* zeroIdx = ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]
| Expr* zeroIdx = ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); | |
| Expr* zeroIdx = ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0); |
7b32563 to
aa379bb
Compare
| // Dynamic Shared Memory: Automate zero-init purely via AST | ||
| Expr* derivedRef = BuildDeclRef(VDDerived); | ||
| Expr* zeroIdx = | ||
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); | |
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0); |
| initDiff.updateStmtDx(finalInit); | ||
| } | ||
|
|
||
| CloneCUDASharedAttr(VD, VDClone); |
There was a problem hiding this comment.
That seems should be a part of BuildGlobalVarDecl.
| Stmt* ReverseResult = utils::unwrapIfSingleStmt(RCS); | ||
| return {StmtDiff(ForwardResult, ReverseResult), EDiff}; | ||
| } | ||
| void ReverseModeVisitor::CloneCUDASharedAttr(const clang::VarDecl* OriginalVD, |
There was a problem hiding this comment.
This does not need to be a member function, a file static if at all, should be enough.
| : clang::SC_Static); | ||
| } | ||
| } | ||
| void ReverseModeVisitor::HandleCUDASharedMemoryDecl( |
There was a problem hiding this comment.
This should be inside a new file called ReverseModeVisitorCUDA.cpp
429a5f0 to
303621d
Compare
| if (!isDynamicSharedMem) { | ||
| CloneCUDASharedAttr(VD, VDDerived); | ||
| VDDerived->setStorageClass(clang::SC_Static); | ||
|
|
There was a problem hiding this comment.
warning: no header providing "clang::SC_Static" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <clang/Basic/Specifiers.h>| VDForward->setStorageClass(clang::SC_Static); | ||
|
|
||
| llvm::SmallVector<Expr*, 1> args = {BuildDeclRef(VDDerived)}; | ||
| Stmt* initCall = GetCladZeroInit(args); |
There was a problem hiding this comment.
warning: no header providing "llvm::SmallVector" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <llvm/ADT/SmallVector.h>| Expr* derivedRef = BuildDeclRef(VDDerived); | ||
| Expr* zeroIdx = | ||
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); | ||
| Expr* arraySub = BuildArraySubscript(derivedRef, {zeroIdx}); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]
| Expr* arraySub = BuildArraySubscript(derivedRef, {zeroIdx}); | |
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0); |
| Expr* arraySub = BuildArraySubscript(derivedRef, {zeroIdx}); | ||
| QualType elemType = VDDerived->getType()->getPointeeType(); | ||
| Expr* assignZero = BuildOp(BO_Assign, arraySub, getZeroInit(elemType)); | ||
| memsetCalls.push_back(assignZero); |
There was a problem hiding this comment.
warning: no header providing "clang::BO_Assign" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <clang/AST/OperationKinds.h>303621d to
41d85b5
Compare
…e dynamic shared memory
41d85b5 to
f01ec62
Compare
|
|
||
| if (!isDynamicSharedMem) { | ||
| CloneCUDASharedAttr(VD, VDDerived); | ||
| VDDerived->setStorageClass(clang::SC_Static); |
There was a problem hiding this comment.
warning: no header providing "clang::SC_Static" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <clang/Basic/Specifiers.h>| CloneCUDASharedAttr(VD, VDForward); | ||
| VDForward->setStorageClass(clang::SC_Static); | ||
|
|
||
| llvm::SmallVector<Expr*, 1> args = {BuildDeclRef(VDDerived)}; |
There was a problem hiding this comment.
warning: no header providing "llvm::SmallVector" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <llvm/ADT/SmallVector.h>| CloneCUDASharedAttr(VD, VDForward); | ||
| Expr* derivedRef = BuildDeclRef(VDDerived); | ||
| Expr* zeroIdx = | ||
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); | |
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0); |
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); | ||
| Expr* arraySub = BuildArraySubscript(derivedRef, {zeroIdx}); | ||
| QualType elemType = VDDerived->getType()->getPointeeType(); | ||
| Expr* assignZero = BuildOp(BO_Assign, arraySub, getZeroInit(elemType)); |
There was a problem hiding this comment.
warning: no header providing "clang::BO_Assign" is directly included [misc-include-cleaner]
lib/Differentiator/ReverseModeVisitorCUDA.cpp:5:
+ #include <clang/AST/OperationKinds.h>
This PR enables differentiation of CUDA kernels having
__shared__attribute. It also enable differentiation support of dynamic shared memoryextern __shared__.__syncthreadsandatomicAddis also enabled during the reverse sweep of the reverse mode to ensure no race condition.