Skip to content

Commit 7dd458d

Browse files
authored
[SYCLomatic] Enable analysis mode to summary the migration effect required with option "--analysis-mode" (#1443)
Signed-off-by: Ziran Zhang <ziran.zhang@intel.com>
1 parent 094bb88 commit 7dd458d

11 files changed

Lines changed: 278 additions & 30 deletions

File tree

clang/include/clang/DPCT/DPCTOptions.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,9 @@ DPCT_ENUM_OPTION(
477477
llvm::cl::CommaSeparated, llvm::cl::value_desc("value"),
478478
llvm::cl::cat(DPCTCat), llvm::cl::ZeroOrMore)
479479

480+
DPCT_NON_ENUM_OPTION(
481+
DPCT_OPT_TYPE(static llvm::cl::opt<bool>), AnalysisMode, "analysis-mode",
482+
llvm::cl::desc("Only generate report for porting effort. Default: off."),
483+
llvm::cl::cat(DPCTCat), llvm::cl::init(false))
484+
480485
#endif // !DPCT_OPTIONS_IN_CLANG_DPCT

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12483,6 +12483,7 @@ void RecognizeAPINameRule::processFuncCall(const CallExpr *CE) {
1248312483
return;
1248412484
}
1248512485

12486+
recordRecognizedAPI(CE);
1248612487
auto *NSD = dyn_cast<NamespaceDecl>(ND->getDeclContext());
1248712488
Namespace = getNameSpace(NSD);
1248812489
APIName = CE->getCalleeDecl()->getAsFunction()->getNameAsString();
@@ -12526,6 +12527,7 @@ void RecognizeTypeRule::registerMatcher(ast_matchers::MatchFinder &MF) {
1252612527
auto TypeTable = MigrationStatistics::GetTypeTable();
1252712528
std::vector<std::string> UnsupportedType;
1252812529
std::vector<std::string> UnsupportedPointerType;
12530+
std::vector<std::string> AllTypes;
1252912531
for (auto &Type : TypeTable) {
1253012532
if (!Type.second) {
1253112533
if (Type.first.find("*") != std::string::npos) {
@@ -12535,6 +12537,9 @@ void RecognizeTypeRule::registerMatcher(ast_matchers::MatchFinder &MF) {
1253512537
UnsupportedType.push_back(Type.first);
1253612538
}
1253712539
}
12540+
if (DpctGlobalInfo::isAnalysisModeEnabled()) {
12541+
AllTypes.push_back(Type.first);
12542+
}
1253812543
}
1253912544
MF.addMatcher(
1254012545
typeLoc(
@@ -12544,31 +12549,42 @@ void RecognizeTypeRule::registerMatcher(ast_matchers::MatchFinder &MF) {
1254412549
loc(pointerType(pointee(qualType(hasDeclaration(namedDecl(
1254512550
internal::Matcher<NamedDecl>(new internal::HasNameMatcher(
1254612551
UnsupportedPointerType))))))))))
12547-
.bind("typeloc"),
12552+
.bind("unsupportedtypeloc"),
1254812553
this);
12554+
12555+
if (DpctGlobalInfo::isAnalysisModeEnabled()) {
12556+
MF.addMatcher(typeLoc(loc(qualType(hasDeclaration(
12557+
namedDecl(internal::Matcher<NamedDecl>(
12558+
new internal::HasNameMatcher(AllTypes)))))))
12559+
.bind("alltypeloc"),
12560+
this);
12561+
}
1254912562
}
1255012563

1255112564
void RecognizeTypeRule::runRule(
1255212565
const ast_matchers::MatchFinder::MatchResult &Result) {
12553-
const TypeLoc *TL = getNodeAsType<TypeLoc>(Result, "typeloc");
12554-
if (!TL)
12555-
return;
12556-
auto &Context = DpctGlobalInfo::getContext();
12557-
QualType QTy = TL->getType();
12558-
if (QTy.isCanonical())
12559-
return;
12560-
std::string TypeName =
12566+
if (const TypeLoc* TL = getNodeAsType<TypeLoc>(Result, "unsupportedtypeloc")) {
12567+
auto& Context = DpctGlobalInfo::getContext();
12568+
QualType QTy = TL->getType();
12569+
if (QTy.isCanonical())
12570+
return;
12571+
std::string TypeName =
1256112572
DpctGlobalInfo::getTypeName(QTy.getUnqualifiedType(), Context);
12562-
// process pointer type
12563-
if (!QTy->isTypedefNameType() && QTy->isPointerType()) {
12564-
std::string PointeeTy = DpctGlobalInfo::getTypeName(
12573+
// process pointer type
12574+
if (!QTy->isTypedefNameType() && QTy->isPointerType()) {
12575+
std::string PointeeTy = DpctGlobalInfo::getTypeName(
1256512576
QTy->getPointeeType().getUnqualifiedType(), Context);
12577+
report(TL->getBeginLoc(), Diagnostics::KNOWN_UNSUPPORTED_TYPE, false,
12578+
PointeeTy + " *");
12579+
return;
12580+
}
1256612581
report(TL->getBeginLoc(), Diagnostics::KNOWN_UNSUPPORTED_TYPE, false,
12567-
PointeeTy + " *");
12582+
TypeName);
1256812583
return;
1256912584
}
12570-
report(TL->getBeginLoc(), Diagnostics::KNOWN_UNSUPPORTED_TYPE, false,
12571-
TypeName);
12585+
if (const TypeLoc *TL = getNodeAsType<TypeLoc>(Result, "alltypeloc")) {
12586+
recordRecognizedType(*TL);
12587+
}
1257212588
}
1257312589

1257412590
REGISTER_RULE(RecognizeTypeRule, PassKind::PK_Migration)

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ unsigned DpctGlobalInfo::ExtensionDEFlag = static_cast<unsigned>(-1);
153153
unsigned DpctGlobalInfo::ExtensionDDFlag = 0;
154154
unsigned DpctGlobalInfo::ExperimentalFlag = 0;
155155
unsigned DpctGlobalInfo::HelperFuncPreferenceFlag = 0;
156+
bool DpctGlobalInfo::AnalysisModeFlag = false;
156157
unsigned int DpctGlobalInfo::ColorOption = 1;
157158
std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
158159
DpctGlobalInfo::CubPlaceholderIndexMap;

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,8 @@ class DpctGlobalInfo {
10121012
static unsigned getHelperFuncPreferenceFlag() {
10131013
return HelperFuncPreferenceFlag;
10141014
}
1015+
static bool isAnalysisModeEnabled() { return AnalysisModeFlag; }
1016+
static void enableAnalysisMode() { AnalysisModeFlag = true; }
10151017

10161018
inline static format::FormatRange getFormatRange() { return FmtRng; }
10171019
inline static void setFormatRange(format::FormatRange FR) { FmtRng = FR; }
@@ -2168,6 +2170,7 @@ class DpctGlobalInfo {
21682170
static unsigned ExtensionDDFlag;
21692171
static unsigned ExperimentalFlag;
21702172
static unsigned HelperFuncPreferenceFlag;
2173+
static bool AnalysisModeFlag;
21712174
static unsigned int ColorOption;
21722175
static std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
21732176
CubPlaceholderIndexMap;

clang/lib/DPCT/DPCT.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ int runDPCT(int argc, const char **argv) {
606606
clang::tooling::SetDiagnosticOutput(DpctTerm());
607607
}
608608

609+
if (AnalysisMode) {
610+
DpctGlobalInfo::enableAnalysisMode();
611+
SuppressWarningsAllFlag = true;
612+
}
609613
initWarningIDs();
610614

611615
DpctInstallPath = getInstallPath(argv[0]);
@@ -1282,6 +1286,11 @@ int runDPCT(int argc, const char **argv) {
12821286
}
12831287
}
12841288

1289+
if (DpctGlobalInfo::isAnalysisModeEnabled()) {
1290+
dumpAnalysisModeStatics(llvm::outs());
1291+
return MigrationSucceeded;
1292+
}
1293+
12851294
// if run was successful
12861295
int Status = saveNewFiles(Tool, InRoot, OutRoot);
12871296
ShowStatus(Status);

clang/lib/DPCT/Diagnostics.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ bool checkDuplicated(const std::string &FileAndLine,
3434
std::unordered_map<int, DiagnosticsMessage> DiagnosticIDTable;
3535
std::unordered_map<int, DiagnosticsMessage> CommentIDTable;
3636

37+
#define HIGH_LEVEL EffortLevel::EL_High
38+
#define MEDIUM_LEVEL EffortLevel::EL_Medium
39+
#define LOW_LEVEL EffortLevel::EL_Low
3740

38-
#define DEF_WARNING(NAME, ID, LEVEL, MSG) \
41+
#define DEF_WARNING(NAME, ID, LEVEL, MSG) \
3942
DiagnosticsMessage wg_##NAME(DiagnosticIDTable, ID, \
40-
clang::DiagnosticIDs::Warning, MSG);
43+
clang::DiagnosticIDs::Warning, LEVEL, MSG);
4144

42-
#define DEF_COMMENT(NAME, ID, LEVEL, MSG) \
45+
#define DEF_COMMENT(NAME, ID, LEVEL, MSG) \
4346
DiagnosticsMessage cg_##NAME(CommentIDTable, ID, clang::DiagnosticIDs::Note, \
44-
MSG);
47+
LEVEL, MSG);
4548

4649
#include "Diagnostics.inc"
4750

@@ -58,7 +61,8 @@ std::unordered_set<int> APIQueryNeedReportWarningIDSet = {
5861

5962
std::unordered_map<int, DiagnosticsMessage> MsgIDTable;
6063
#define DEF_COMMENT(NAME, ID, MSG) \
61-
DiagnosticsMessage cg_##NAME(MsgIDTable, ID, clang::DiagnosticIDs::Note, MSG);
64+
DiagnosticsMessage cg_##NAME(MsgIDTable, ID, clang::DiagnosticIDs::Note, \
65+
EffortLevel::EL_Low, MSG);
6266
#include "DiagnosticsBuildScript.inc"
6367
#undef DEF_COMMENT
6468

clang/lib/DPCT/Diagnostics.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern std::set<int> WarningIDs;
4343
struct DiagnosticsMessage {
4444
int ID;
4545
int Category;
46+
EffortLevel EL;
4647
const char *Msg;
4748

4849
#define DEF_WARNING(NAME, ID, LEVEL, MSG) ID,
@@ -58,8 +59,8 @@ struct DiagnosticsMessage {
5859
#undef DEF_COMMENT
5960
DiagnosticsMessage() = default;
6061
DiagnosticsMessage(std::unordered_map<int, DiagnosticsMessage> &Table, int ID,
61-
int Category, const char *Msg)
62-
: ID(ID), Category(Category), Msg(Msg) {
62+
int Category, EffortLevel EL, const char *Msg)
63+
: ID(ID), Category(Category), EL(EL), Msg(Msg) {
6364
assert(Table.find(ID) == Table.end() && "[DPCT Internal error] Two "
6465
"messages with the same ID "
6566
"are being registered");
@@ -317,12 +318,17 @@ inline bool report(SourceLocation SL, IDTy MsgID,
317318
if (checkDuplicated(FileAndLine, WarningIDAndMsg))
318319
return false;
319320

321+
auto Diag = DiagnosticIDTable.find((int)MsgID);
322+
if (Diag == DiagnosticIDTable.end())
323+
return true;
324+
if (DpctGlobalInfo::isAnalysisModeEnabled()) {
325+
recordAnalysisModeEffort(SL, Diag->second.EL);
326+
return true;
327+
}
320328
if (!SuppressWarningsAllFlag) {
321329
// Only report warnings that are not suppressed
322-
if (WarningIDs.find((int)MsgID) == WarningIDs.end() &&
323-
DiagnosticIDTable.find((int)MsgID) != DiagnosticIDTable.end()) {
324-
reportWarning(SL, DiagnosticIDTable[(int)MsgID], SM.getDiagnostics(),
325-
Vals...);
330+
if (WarningIDs.find((int)MsgID) == WarningIDs.end()) {
331+
reportWarning(SL, Diag->second, SM.getDiagnostics(), Vals...);
326332
}
327333
}
328334
if (TS && CommentIDTable.find((int)MsgID) != CommentIDTable.end()) {
@@ -402,12 +408,17 @@ bool report(const clang::tooling::UnifiedPath &FileAbsPath, unsigned int Offset,
402408
unsigned int ColNum = Offset - Fileinfo->getLineInfo(LineNum).Offset + 1;
403409
SourceLocation SL = SM.translateLineCol(FID, LineNum, ColNum);
404410

411+
auto Diag = DiagnosticIDTable.find((int)MsgID);
412+
if (Diag == DiagnosticIDTable.end())
413+
return true;
414+
if (DpctGlobalInfo::isAnalysisModeEnabled()) {
415+
recordAnalysisModeEffort(FileAbsPath, Offset, Diag->second.EL);
416+
return true;
417+
}
405418
if (!SuppressWarningsAllFlag) {
406419
// Only report warnings that are not suppressed
407-
if (WarningIDs.find((int)MsgID) == WarningIDs.end() &&
408-
DiagnosticIDTable.find((int)MsgID) != DiagnosticIDTable.end()) {
409-
reportWarning(SL, DiagnosticIDTable[(int)MsgID], SM.getDiagnostics(),
410-
Vals...);
420+
if (WarningIDs.find((int)MsgID) == WarningIDs.end()) {
421+
reportWarning(SL, Diag->second, SM.getDiagnostics(), Vals...);
411422
}
412423
}
413424

clang/lib/DPCT/MigrationAction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ std::shared_ptr<TranslationUnitInfo> DpctToolAction::createTranslationUnitInfo(
174174

175175
std::shared_ptr<TranslationUnitInfo> DpctToolAction::createTranslationUnitInfoImpl(
176176
std::shared_ptr<CompilerInvocation> Invocation, bool &Success) {
177+
Invocation->getDiagnosticOpts().IgnoreWarnings = true;
177178
auto DiagConsumer = new TextDiagnosticPrinter(
178179
DiagnosticStream, &Invocation->getDiagnosticOpts());
179180
auto Info = std::make_shared<TranslationUnitInfo>();

0 commit comments

Comments
 (0)