Skip to content

Commit 950c271

Browse files
authored
[SYCLomatic] Ignore a YMAL rule and Emit warning if Out field is required but not specified (#2766)
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
1 parent b88844d commit 950c271

File tree

14 files changed

+124
-118
lines changed

14 files changed

+124
-118
lines changed

clang/lib/DPCT/MigrateScript/MigrateCmakeScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ void doCmakeScriptMigration(const clang::tooling::UnifiedPath &InRoot,
599599
}
600600

601601
void registerCmakeMigrationRule(MetaRuleObject &R) {
602-
if (!R.Out.has_value())
602+
if (!validateOutFieldAndWarn(R))
603603
return;
604604
auto PR = MetaRuleObject::PatternRewriter(R.In, R.Out.value(), R.Subrules,
605605
R.MatchMode, R.Warning, R.RuleId,

clang/lib/DPCT/MigrateScript/MigratePythonBuildScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void doPythonBuildScriptMigration(const clang::tooling::UnifiedPath &InRoot,
9191
}
9292

9393
void registerPythonMigrationRule(MetaRuleObject &R) {
94-
if (!R.Out.has_value())
94+
if (!validateOutFieldAndWarn(R))
9595
return;
9696
auto PR = MetaRuleObject::PatternRewriter(R.In, R.Out.value(), R.Subrules,
9797
R.MatchMode, R.Warning, R.RuleId,

clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ getUserDefinedHeader(const std::string &FileName) {
5555
for (auto &Header : Rule.Includes) {
5656
PrintHeader(Header);
5757
}
58-
if (Rule.Out.has_value() && !Rule.Out.value().empty())
58+
if (validateOutFieldAndWarn(Rule) && !Rule.Out.value().empty())
5959
PrintHeader(Rule.Out.value());
6060
OS << Rule.Postfix;
6161
return std::make_pair(ReplHeaderStr, Rule.Priority);

clang/lib/DPCT/UserDefinedRules/UserDefinedRules.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void registerMigrationRule(const std::string &Name, Functor &&F) {
4848
}
4949

5050
void registerMacroRule(MetaRuleObject &R) {
51-
if (!R.Out.has_value())
51+
if (!validateOutFieldAndWarn(R))
5252
return;
5353
auto It = MapNames::MacroRuleMap.find(R.In);
5454
if (It != MapNames::MacroRuleMap.end()) {
@@ -70,7 +70,7 @@ void registerMacroRule(MetaRuleObject &R) {
7070
}
7171

7272
void registerAPIRule(MetaRuleObject &R) {
73-
if (!R.Out.has_value())
73+
if (!validateOutFieldAndWarn(R))
7474
return;
7575
using namespace clang::dpct;
7676
// register rule
@@ -122,7 +122,7 @@ void registerAPIRule(MetaRuleObject &R) {
122122
}
123123

124124
void registerHeaderRule(MetaRuleObject &R) {
125-
if (!R.Out.has_value())
125+
if (!validateOutFieldAndWarn(R))
126126
return;
127127
auto It = MapNames::HeaderRuleMap.find(R.In);
128128
if (It != MapNames::HeaderRuleMap.end()) {
@@ -135,7 +135,7 @@ void registerHeaderRule(MetaRuleObject &R) {
135135
}
136136

137137
void registerTypeRule(MetaRuleObject &R) {
138-
if (!R.Out.has_value())
138+
if (!validateOutFieldAndWarn(R))
139139
return;
140140
std::shared_ptr TOB = std::make_shared<TypeOutputBuilder>();
141141
TOB->Kind = TypeOutputBuilder::Kind::Top;
@@ -185,6 +185,9 @@ void registerClassRule(MetaRuleObject &R) {
185185
// register class name migration rule
186186
if (R.Out.has_value())
187187
registerTypeRule(R);
188+
if (R.Fields.empty() && R.Methods.empty()) {
189+
(void)validateOutFieldAndWarn(R);
190+
}
188191
// register all field rules
189192
for (auto ItField = R.Fields.begin(); ItField != R.Fields.end(); ItField++) {
190193
std::string BaseAndFieldName = R.In + "." + (*ItField)->In;
@@ -258,7 +261,7 @@ void registerClassRule(MetaRuleObject &R) {
258261
}
259262

260263
void registerEnumRule(MetaRuleObject &R) {
261-
if (!R.Out.has_value())
264+
if (!validateOutFieldAndWarn(R))
262265
return;
263266
auto It = MapNames::EnumNamesMap.find(R.In);
264267
if (It != MapNames::EnumNamesMap.end()) {
@@ -287,22 +290,22 @@ void registerEnumRule(MetaRuleObject &R) {
287290
}
288291

289292
void deregisterAPIRule(MetaRuleObject &R) {
290-
if (!R.Out.has_value())
293+
if (!validateOutFieldAndWarn(R))
291294
return;
292295
using namespace clang::dpct;
293296
CallExprRewriterFactoryBase::RewriterMap->erase(R.In);
294297
}
295298

296299
void registerPatternRewriterRule(MetaRuleObject &R) {
297-
if (!R.Out.has_value())
300+
if (!validateOutFieldAndWarn(R))
298301
return;
299302
MapNames::PatternRewriters.emplace_back(MetaRuleObject::PatternRewriter(
300303
R.In, R.Out.value(), R.Subrules, R.MatchMode, R.Warning, R.RuleId,
301304
R.BuildScriptSyntax, R.Priority));
302305
}
303306

304307
void registerHelperFunctionRule(MetaRuleObject &R) {
305-
if (!R.Out.has_value())
308+
if (!validateOutFieldAndWarn(R))
306309
return;
307310
static const std::unordered_map<std::string, dpct::HelperFuncCatalog>
308311
String2HelperFuncCatalogMap{
@@ -562,6 +565,14 @@ void importRules(std::vector<clang::tooling::UnifiedPath> &RuleFiles) {
562565
}
563566
}
564567

568+
bool validateOutFieldAndWarn(const MetaRuleObject &R) {
569+
if (R.Out.has_value())
570+
return true;
571+
llvm::errs() << "warning: The \"Out\" field of rule " << R.RuleId << " (in "
572+
<< R.RuleFile << ") is not specified. This rule is ignored.\n";
573+
return false;
574+
}
575+
565576
// /RuleOutputString is the string specified in rule's "Out" session
566577
void OutputBuilder::parse(std::string &RuleOutputString) {
567578
size_t i, StrStartIdx = 0;

clang/lib/DPCT/UserDefinedRules/UserDefinedRules.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class TypeOutputBuilder : public OutputBuilder {
236236

237237
void importRules(std::vector<clang::tooling::UnifiedPath> &RuleFiles);
238238

239+
bool validateOutFieldAndWarn(const MetaRuleObject &R);
240+
239241
} // namespace dpct
240242
} // namespace clang
241243

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: dpct --out-root %T %s --cuda-include-path="%cuda-path/include" --rule-file %S/rule1.yaml 2>&1 | tee %T/output1.txt
2+
// RUN: FileCheck --input-file %T/ignore_rule.dp.cpp --match-full-lines %s
3+
// RUN: FileCheck --input-file %T/output1.txt --match-full-lines %S/output1_ref.txt
4+
// RUN: %if build_lit %{icpx -c -fsycl %T/ignore_rule.dp.cpp -o %T/ignore_rule.dp.o %}
5+
6+
// CHECK: #define MACRO_A 1
7+
// CHECK-NEXT: void foo() {
8+
// CHECK-NEXT: sycl::int2 a;
9+
// CHECK-NEXT: a.x() = MACRO_A;
10+
// CHECK-NEXT: }
11+
#define MACRO_A 1
12+
void foo() {
13+
int2 a;
14+
a.x = MACRO_A;
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// CHECK: warning: The "Out" field of rule rule_MACRO_A (in {{.*}}rule1.yaml) is not specified. This rule is ignored.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Rule: rule_MACRO_A
2+
Kind: Macro
3+
Priority: Takeover
4+
In: MACRO_A
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Tensor {
2+
public:
3+
bool is_cuda();
4+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: dpct --out-root %T %s --cuda-include-path="%cuda-path/include" --rule-file %S/rule2.yaml 2>&1 | tee %T/output2.txt
2+
// RUN: FileCheck --input-file %T/not_ignore_rule.dp.cpp --match-full-lines %s
3+
// RUN: FileCheck --input-file %T/output2.txt --match-full-lines %S/output2_ref.txt
4+
5+
#include "../not_ignore_rule.h"
6+
7+
// CHECK: void foo(Tensor a) {
8+
// CHECK-NEXT: a.is_xpu();
9+
// CHECK-NEXT: }
10+
void foo(Tensor a) {
11+
a.is_cuda();
12+
}

0 commit comments

Comments
 (0)