Skip to content

Commit 5341638

Browse files
Merge branch 'SYCLomatic' into ptx
2 parents 909a832 + f762d91 commit 5341638

148 files changed

Lines changed: 3859 additions & 566 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ASTTraversal.h"
10-
#include "RulesLang/RulesLang.h"
1110
#include "AnalysisInfo.h"
11+
#include "CodePin/GenCodePinHeader.h"
12+
#include "MigrationRuleManager.h"
1213
#include "RulesAsm/AsmMigration.h"
14+
#include "RulesCCL/NCCLAPIMigration.h"
1315
#include "RulesDNN/DNNAPIMigration.h"
16+
#include "RulesLang/OptimizeMigration.h"
17+
#include "RulesLang/RulesLang.h"
18+
#include "RulesLang/WMMAAPIMigration.h"
19+
#include "RulesLangLib/LIBCUAPIMigration.h"
20+
#include "RulesLangLib/NvtxAPIMigration.h"
21+
#include "RulesLangLib/ThrustAPIMigration.h"
22+
#include "RulesMathLib/BLASAPIMigration.h"
1423
#include "RulesMathLib/FFTAPIMigration.h"
1524
#include "RulesMathLib/RandomAPIMigration.h"
1625
#include "RulesMathLib/SolverAPIMigration.h"
17-
#include "RulesMathLib/BLASAPIMigration.h"
18-
#include "CodePin/GenCodePinHeader.h"
26+
#include "RulesMathLib/SpBLASAPIMigration.h"
27+
#include "RulesSHMEM/NVSHMEMAPIMigration.h"
1928
#include "RulesSecurity/Homoglyph.h"
20-
#include "RulesLangLib/LIBCUAPIMigration.h"
21-
#include "RulesLangLib/NvtxAPIMigration.h"
22-
#include "MigrationRuleManager.h"
2329
#include "RulesSecurity/MisleadingBidirectional.h"
24-
#include "RulesCCL/NCCLAPIMigration.h"
25-
#include "RulesLang/OptimizeMigration.h"
26-
#include "RulesMathLib/SpBLASAPIMigration.h"
2730
#include "TextModification.h"
28-
#include "RulesLangLib/ThrustAPIMigration.h"
2931
#include "Utility.h"
30-
#include "RulesLang/WMMAAPIMigration.h"
3132

3233
#include <string>
3334
#include <unordered_map>
@@ -160,32 +161,41 @@ REGISTER_RULE(GraphicsInteropRule, PassKind::PK_Migration)
160161
REGISTER_RULE(RulesLangAddrSpaceConvRule, PassKind::PK_Migration)
161162

162163
REGISTER_RULE(BLASEnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_BLas)
163-
REGISTER_RULE(BLASFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_BLas)
164+
REGISTER_RULE(BLASFunctionCallRule, PassKind::PK_Migration,
165+
RuleGroupKind::RK_BLas)
164166

165167
REGISTER_RULE(SPBLASEnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_Sparse)
166-
REGISTER_RULE(SPBLASFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_Sparse)
168+
REGISTER_RULE(SPBLASFunctionCallRule, PassKind::PK_Migration,
169+
RuleGroupKind::RK_Sparse)
167170

168171
REGISTER_RULE(RandomEnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_Rng)
169-
REGISTER_RULE(RandomFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_Rng)
170-
REGISTER_RULE(DeviceRandomFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_Rng)
172+
REGISTER_RULE(RandomFunctionCallRule, PassKind::PK_Migration,
173+
RuleGroupKind::RK_Rng)
174+
REGISTER_RULE(DeviceRandomFunctionCallRule, PassKind::PK_Migration,
175+
RuleGroupKind::RK_Rng)
171176

172177
REGISTER_RULE(SOLVEREnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_Solver)
173-
REGISTER_RULE(SOLVERFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_Solver)
178+
REGISTER_RULE(SOLVERFunctionCallRule, PassKind::PK_Migration,
179+
RuleGroupKind::RK_Solver)
174180

175181
REGISTER_RULE(LIBCURule, PassKind::PK_Migration, RuleGroupKind::RK_Libcu)
176182
REGISTER_RULE(NvtxRule, PassKind::PK_Migration)
177183

178184
REGISTER_RULE(ThrustAPIRule, PassKind::PK_Migration, RuleGroupKind::RK_Thrust)
179185
REGISTER_RULE(ThrustTypeRule, PassKind::PK_Migration, RuleGroupKind::RK_Thrust)
180186

181-
REGISTER_RULE(ManualMigrateEnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_NCCL)
187+
REGISTER_RULE(ManualMigrateEnumsRule, PassKind::PK_Migration,
188+
RuleGroupKind::RK_NCCL)
182189
REGISTER_RULE(NCCLRule, PassKind::PK_Migration, RuleGroupKind::RK_NCCL)
183190

184191
REGISTER_RULE(FFTEnumsRule, PassKind::PK_Migration, RuleGroupKind::RK_FFT)
185-
REGISTER_RULE(FFTFunctionCallRule, PassKind::PK_Migration,RuleGroupKind::RK_FFT)
192+
REGISTER_RULE(FFTFunctionCallRule, PassKind::PK_Migration,
193+
RuleGroupKind::RK_FFT)
186194

187195
REGISTER_RULE(CuDNNTypeRule, PassKind::PK_Migration, RuleGroupKind::RK_DNN)
188196
REGISTER_RULE(CuDNNAPIRule, PassKind::PK_Migration, RuleGroupKind::RK_DNN)
189197

198+
REGISTER_RULE(NVSHMEMRule, PassKind::PK_Migration, RuleGroupKind::RK_NVSHMEM)
199+
190200
} // namespace dpct
191-
} // namespace clang
201+
} // namespace clang

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/AST/ExprCXX.h"
2121
#include "clang/AST/OperationKinds.h"
2222
#include "clang/ASTMatchers/ASTMatchers.h"
23+
#include "clang/Tooling/Core/UnifiedPath.h"
2324
#include "clang/Tooling/Tooling.h"
2425
#include <algorithm>
2526
#include <deque>
@@ -829,6 +830,19 @@ void DpctFileInfo::setFirstIncludeOffset(unsigned Offset) {
829830
HasInclusionDirectiveSet.insert(std::move(MF));
830831
}
831832
}
833+
void DpctFileInfo::setHeaderInserted(HeaderType Header) {
834+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][Header] = true;
835+
}
836+
void DpctFileInfo::setMathHeaderInserted(bool B) {
837+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][HeaderType::HT_Math] = B;
838+
}
839+
void DpctFileInfo::setAlgorithmHeaderInserted(bool B) {
840+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath]
841+
[HeaderType::HT_Algorithm] = B;
842+
}
843+
void DpctFileInfo::setTimeHeaderInserted(bool B) {
844+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][HeaderType::HT_Time] = B;
845+
}
832846
void DpctFileInfo::concatHeader(llvm::raw_string_ostream &OS) {}
833847
template <class FirstT, class... Args>
834848
void DpctFileInfo::concatHeader(llvm::raw_string_ostream &OS, FirstT &&First,
@@ -862,9 +876,9 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
862876
Type, FirstIncludeOffset.at(MF));
863877
}
864878
}
865-
if (HeaderInsertedBitMap[Type])
879+
if (DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][Type])
866880
return;
867-
HeaderInsertedBitMap[Type] = true;
881+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][Type] = true;
868882
std::string ReplStr;
869883
llvm::raw_string_ostream OS(ReplStr);
870884
std::string MigratedMacroDefinitionStr;
@@ -898,10 +912,11 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
898912
concatHeader(OS, getHeaderSpelling(Type));
899913
if (DpctGlobalInfo::useSYCLCompat()) {
900914
concatHeader(OS, getHeaderSpelling(HT_COMPAT_SYCLcompat));
901-
HeaderInsertedBitMap[HT_COMPAT_SYCLcompat] = true;
915+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath]
916+
[HT_COMPAT_SYCLcompat] = true;
902917
} else {
903918
concatHeader(OS, getHeaderSpelling(HT_DPCT_Dpct));
904-
HeaderInsertedBitMap[HT_DPCT_Dpct] = true;
919+
DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][HT_DPCT_Dpct] = true;
905920
}
906921
DpctGlobalInfo::printUsingNamespace(OS);
907922
if (DpctGlobalInfo::useNoQueueDevice()) {
@@ -1456,8 +1471,8 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) {
14561471
return getStringForRegexDefaultQueueAndDevice(
14571472
HelperFuncType::HFT_DefaultQueuePtr, Index);
14581473
case 'E': {
1459-
auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileInfo();
1460-
return Vec[Index]->hasCUDASyntax()
1474+
auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList();
1475+
return DpctGlobalInfo::hasCUDASyntax(Vec[Index])
14611476
? ("c" + DpctGlobalInfo::getSYCLSourceExtension())
14621477
: "c";
14631478
}
@@ -2493,7 +2508,7 @@ bool DpctGlobalInfo::CVersionCUDALaunchUsedFlag = false;
24932508
unsigned int DpctGlobalInfo::ColorOption = 1;
24942509
std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
24952510
DpctGlobalInfo::CubPlaceholderIndexMap;
2496-
std::vector<std::shared_ptr<DpctFileInfo>> DpctGlobalInfo::CSourceFileInfo;
2511+
std::vector<tooling::UnifiedPath> DpctGlobalInfo::CSourceFileList;
24972512
bool DpctGlobalInfo::OptimizeMigrationFlag = false;
24982513
std::unordered_map<std::string, std::shared_ptr<PriorityReplInfo>>
24992514
DpctGlobalInfo::PriorityReplInfoMap;
@@ -2520,6 +2535,8 @@ std::vector<std::pair<std::string, std::vector<std::string>>>
25202535
std::unordered_set<std::string> DpctGlobalInfo::NeedParenAPISet = {};
25212536
std::unordered_set<std::string>
25222537
DpctGlobalInfo::CustomHelperFunctionAddtionalIncludes = {};
2538+
std::unordered_map<clang::tooling::UnifiedPath, std::bitset<32>>
2539+
DpctGlobalInfo::HeaderInsertedBitMap = {};
25232540
///// class DpctNameGenerator /////
25242541
void DpctNameGenerator::printName(const FunctionDecl *FD,
25252542
llvm::raw_ostream &OS) {
@@ -2608,6 +2625,9 @@ CtTypeInfo::CtTypeInfo(const VarDecl *D, bool NeedSizeFold) : CtTypeInfo() {
26082625
auto TL = D->getTypeSourceInfo()->getTypeLoc();
26092626
IsConstantQualified = D->hasAttr<CUDAConstantAttr>();
26102627
setTypeInfo(TL, NeedSizeFold);
2628+
if (IsAutoSpecified) {
2629+
DeducedTypeStr = DpctGlobalInfo::getReplacedTypeName(D->getType());
2630+
}
26112631
if (TL.getTypeLocClass() == TypeLoc::IncompleteArray) {
26122632
if (auto CAT = dyn_cast<ConstantArrayType>(D->getType())) {
26132633
Range[0] = std::to_string(CAT->getSize().getZExtValue());
@@ -2805,6 +2825,12 @@ void CtTypeInfo::setArrayInfo(const IncompleteArrayTypeLoc &TL,
28052825
}
28062826
void CtTypeInfo::setName(const TypeLoc &TL) {
28072827
ExprAnalysis EA;
2828+
QualType QT = TL.getType();
2829+
if (const Type *TP = QT.getTypePtr()) {
2830+
if (isa<AutoType>(TP)) {
2831+
IsAutoSpecified = true;
2832+
}
2833+
}
28082834
EA.analyze(TL);
28092835
TDSI = EA.getTemplateDependentStringInfo();
28102836
auto SetFromTL = EA.getHelperFeatureSet();
@@ -2921,7 +2947,7 @@ void MemVarInfo::migrateToDeviceGlobal(const VarDecl *MemVar) {
29212947
auto &Ctx = DpctGlobalInfo::getContext();
29222948
auto &MacroArgMap = DpctGlobalInfo::getMacroArgRecordMap();
29232949
auto TSI = MemVar->getTypeSourceInfo();
2924-
auto OriginTL = TSI->getTypeLoc();
2950+
auto OriginTL = TSI->getTypeLoc().getUnqualifiedLoc().getAs<TypeLoc>();
29252951
auto TL = OriginTL;
29262952
auto BegLoc = MemVar->getBeginLoc();
29272953
if (BegLoc.isMacroID()) {
@@ -3002,7 +3028,11 @@ void MemVarInfo::migrateToDeviceGlobal(const VarDecl *MemVar) {
30023028
}
30033029
}
30043030
if (BaseTypeStr.empty()) {
3005-
BaseTypeStr = getType()->getBaseNameWithoutQualifiers();
3031+
if (getType()->isAutoSpecified()) {
3032+
BaseTypeStr = getType()->getDeducedTypeStr();
3033+
} else {
3034+
BaseTypeStr = getType()->getBaseNameWithoutQualifiers();
3035+
}
30063036
TypeReplLoc = TL.getBeginLoc();
30073037
TypeReplLen =
30083038
SM.getFileOffset(TL.getEndLoc()) - SM.getFileOffset(TL.getBeginLoc()) +
@@ -4806,6 +4836,8 @@ DeviceFunctionDecl::DeviceFunctionDecl(
48064836
FuncInfo(getFuncInfo(FD)) {
48074837
if (FD->isFunctionTemplateSpecialization()) {
48084838
SourceRange ReturnTypeRange = FD->getReturnTypeSourceRange();
4839+
ReturnTypeRange = clang::dpct::getDefinitionRange(
4840+
ReturnTypeRange.getBegin(), ReturnTypeRange.getEnd());
48094841
OffsetForAttr =
48104842
DpctGlobalInfo::getLocInfo(ReturnTypeRange.getBegin()).second;
48114843
}
@@ -4842,6 +4874,11 @@ DeviceFunctionDecl::DeviceFunctionDecl(
48424874
FuncInfo(getFuncInfo(Specialization)) {
48434875
IsDefFilePathNeeded = false;
48444876

4877+
auto ReturnLoc = FTL.getReturnLoc();
4878+
auto ReturnRange = clang::dpct::getDefinitionRange(ReturnLoc.getBeginLoc(),
4879+
ReturnLoc.getEndLoc());
4880+
OffsetForAttr = DpctGlobalInfo::getLocInfo(ReturnRange.getBegin()).second;
4881+
48454882
buildReplaceLocInfo(FTL, Attrs);
48464883
buildTextureObjectParamsInfo(FTL.getParams());
48474884
if (Specialization->hasAttr<CUDAGlobalAttr>()) {
@@ -5232,7 +5269,9 @@ void DeviceFunctionDecl::insertWrapper() {
52325269
Printer << ">";
52335270
Printer.newLine();
52345271
}
5235-
Printer << "void " << FuncName << "_wrapper(";
5272+
Printer << (IsStaticSpecified ? "static " : "")
5273+
<< (IsInlineSpecified ? "inline " : "") << "void " << FuncName
5274+
<< "_wrapper(";
52365275
for (size_t i = 0; i < ParamsInfo.size(); i++) {
52375276
Printer << (i == 0 ? "" : " ,") << ParamsInfo[i].first << " "
52385277
<< ParamsInfo[i].second << ParameterDefaultValueMap[i];
@@ -5312,7 +5351,8 @@ void DeviceFunctionDecl::collectInfoForWrapper(const FunctionDecl *FD) {
53125351
if (HasBody && FD != Def) {
53135352
HasBody = false;
53145353
}
5315-
5354+
IsInlineSpecified = FD->isInlineSpecified();
5355+
IsStaticSpecified = FD->isStatic();
53165356
if (auto FTD = FD->getDescribedFunctionTemplate()) {
53175357
if (auto TemplateParmsList = FTD->getTemplateParameters()) {
53185358
for (size_t i = 0; i < TemplateParmsList->size(); ++i) {
@@ -6073,9 +6113,14 @@ void KernelCallExpr::addAccessorDecl() {
60736113
Tex.second->addDecl(OuterStmts.InitList, SubmitStmts.TextureList,
60746114
SubmitStmts.SamplerList, getQueueStr());
60756115
}
6076-
for (auto &Tmp : VM.getTempStorageMap()) {
6077-
Tmp.second->addAccessorDecl(SubmitStmts.AccessorList,
6078-
ExecutionConfig.LocalSize);
6116+
if (!VM.getTempStorageMap().empty()) {
6117+
DpctGlobalInfo::getInstance()
6118+
.insertFile(getFilePath())
6119+
->insertHeader(HT_DPCT_GROUP_Utils, RT_ForSYCLMigration);
6120+
for (auto &Tmp : VM.getTempStorageMap()) {
6121+
Tmp.second->addAccessorDecl(SubmitStmts.AccessorList,
6122+
ExecutionConfig.LocalSize);
6123+
}
60796124
}
60806125
}
60816126
void KernelCallExpr::buildInfo() {

0 commit comments

Comments
 (0)