10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
20 const auto ThreadID = expr(hasDescendant(callExpr(callee(functionDecl(
21 anyOf(hasName(
"get_global_id"), hasName(
"get_local_id")))))));
23 const auto RefVarOrField = forEachDescendant(
24 stmt(anyOf(declRefExpr(to(varDecl())).bind(
"assign_ref_var"),
25 memberExpr(member(fieldDecl())).bind(
"assign_ref_field"))));
32 anyOf(declStmt(hasDescendant(varDecl(hasInitializer(ThreadID))
33 .bind(
"tid_dep_var"))),
35 isAssignmentOperator(), hasRHS(ThreadID),
37 declRefExpr(to(varDecl().bind(
"tid_dep_var"))),
39 fieldDecl().bind(
"tid_dep_field"))))))))
40 .bind(
"straight_assignment"))),
46 stmt(forEachDescendant(
47 varDecl(hasInitializer(RefVarOrField)).bind(
"pot_tid_var"))),
53 stmt(forEachDescendant(binaryOperator(
54 allOf(isAssignmentOperator(), hasRHS(RefVarOrField),
56 declRefExpr(to(varDecl().bind(
"pot_tid_var"))),
57 memberExpr(member(fieldDecl().bind(
"pot_tid_field"))))))))),
65 expr(anyOf(hasDescendant(callExpr(callee(functionDecl(
66 anyOf(hasName(
"get_global_id"),
67 hasName(
"get_local_id")))))
69 hasDescendant(stmt(anyOf(declRefExpr(to(varDecl())),
70 memberExpr(member(fieldDecl())))))))
72 Finder->addMatcher(stmt(anyOf(forStmt(hasCondition(CondExpr)),
73 doStmt(hasCondition(CondExpr)),
74 whileStmt(hasCondition(CondExpr))))
75 .bind(
"backward_branch"),
79IdDependentBackwardBranchCheck::IdDependencyRecord *
80IdDependentBackwardBranchCheck::hasIdDepVar(
const Expr *Expression) {
81 if (
const auto *Declaration = dyn_cast<DeclRefExpr>(Expression)) {
83 const auto *CheckVariable = dyn_cast<VarDecl>(Declaration->getDecl());
84 auto FoundVariable = IdDepVarsMap.find(CheckVariable);
85 if (FoundVariable == IdDepVarsMap.end())
87 return &(FoundVariable->second);
89 for (
const auto *Child : Expression->children())
90 if (
const auto *ChildExpression = dyn_cast<Expr>(Child))
91 if (IdDependencyRecord *Result = hasIdDepVar(ChildExpression))
96IdDependentBackwardBranchCheck::IdDependencyRecord *
97IdDependentBackwardBranchCheck::hasIdDepField(
const Expr *Expression) {
98 if (
const auto *MemberExpression = dyn_cast<MemberExpr>(Expression)) {
99 const auto *CheckField =
100 dyn_cast<FieldDecl>(MemberExpression->getMemberDecl());
101 auto FoundField = IdDepFieldsMap.find(CheckField);
102 if (FoundField == IdDepFieldsMap.end())
104 return &(FoundField->second);
106 for (
const auto *Child : Expression->children())
107 if (
const auto *ChildExpression = dyn_cast<Expr>(Child))
108 if (IdDependencyRecord *Result = hasIdDepField(ChildExpression))
113void IdDependentBackwardBranchCheck::saveIdDepVar(
const Stmt *Statement,
114 const VarDecl *Variable) {
117 IdDependencyRecord(Variable,
Variable->getBeginLoc(),
118 Twine(
"assignment of ID-dependent variable ") +
122void IdDependentBackwardBranchCheck::saveIdDepField(
const Stmt *Statement,
123 const FieldDecl *Field) {
125 IdDepFieldsMap[
Field] = IdDependencyRecord(
126 Field, Statement->getBeginLoc(),
127 Twine(
"assignment of ID-dependent field ") +
Field->getNameAsString());
130void IdDependentBackwardBranchCheck::saveIdDepVarFromReference(
131 const DeclRefExpr *RefExpr,
const MemberExpr *MemExpr,
132 const VarDecl *PotentialVar) {
134 if (IdDepVarsMap.find(PotentialVar) != IdDepVarsMap.end())
137 llvm::raw_string_ostream StringStream(Message);
138 StringStream <<
"inferred assignment of ID-dependent value from "
141 const auto *RefVar = dyn_cast<VarDecl>(RefExpr->getDecl());
143 if (IdDepVarsMap.find(RefVar) != IdDepVarsMap.end())
144 StringStream <<
"variable " << RefVar->getNameAsString();
147 const auto *RefField = dyn_cast<FieldDecl>(MemExpr->getMemberDecl());
149 if (IdDepFieldsMap.find(RefField) != IdDepFieldsMap.end())
150 StringStream <<
"member " << RefField->getNameAsString();
152 IdDepVarsMap[PotentialVar] =
153 IdDependencyRecord(PotentialVar, PotentialVar->getBeginLoc(), Message);
156void IdDependentBackwardBranchCheck::saveIdDepFieldFromReference(
157 const DeclRefExpr *RefExpr,
const MemberExpr *MemExpr,
158 const FieldDecl *PotentialField) {
160 if (IdDepFieldsMap.find(PotentialField) != IdDepFieldsMap.end())
163 llvm::raw_string_ostream StringStream(Message);
164 StringStream <<
"inferred assignment of ID-dependent member from "
167 const auto *RefVar = dyn_cast<VarDecl>(RefExpr->getDecl());
169 if (IdDepVarsMap.find(RefVar) != IdDepVarsMap.end())
170 StringStream <<
"variable " << RefVar->getNameAsString();
173 const auto *RefField = dyn_cast<FieldDecl>(MemExpr->getMemberDecl());
174 if (IdDepFieldsMap.find(RefField) != IdDepFieldsMap.end())
175 StringStream <<
"member " << RefField->getNameAsString();
177 IdDepFieldsMap[PotentialField] = IdDependencyRecord(
178 PotentialField, PotentialField->getBeginLoc(), Message);
181IdDependentBackwardBranchCheck::LoopType
182IdDependentBackwardBranchCheck::getLoopType(
const Stmt *Loop) {
183 switch (Loop->getStmtClass()) {
184 case Stmt::DoStmtClass:
186 case Stmt::WhileStmtClass:
188 case Stmt::ForStmtClass:
196 const MatchFinder::MatchResult &Result) {
199 const auto *Variable = Result.Nodes.getNodeAs<VarDecl>(
"tid_dep_var");
200 const auto *
Field = Result.Nodes.getNodeAs<FieldDecl>(
"tid_dep_field");
201 const auto *Statement = Result.Nodes.getNodeAs<Stmt>(
"straight_assignment");
202 const auto *RefExpr = Result.Nodes.getNodeAs<DeclRefExpr>(
"assign_ref_var");
203 const auto *MemExpr = Result.Nodes.getNodeAs<MemberExpr>(
"assign_ref_field");
204 const auto *PotentialVar = Result.Nodes.getNodeAs<VarDecl>(
"pot_tid_var");
205 const auto *PotentialField =
206 Result.Nodes.getNodeAs<FieldDecl>(
"pot_tid_field");
209 if (Statement && (Variable ||
Field)) {
211 saveIdDepVar(Statement, Variable);
213 saveIdDepField(Statement,
Field);
217 if ((RefExpr || MemExpr) && PotentialVar)
218 saveIdDepVarFromReference(RefExpr, MemExpr, PotentialVar);
221 if ((RefExpr || MemExpr) && PotentialField)
222 saveIdDepFieldFromReference(RefExpr, MemExpr, PotentialField);
226 const auto *CondExpr = Result.Nodes.getNodeAs<Expr>(
"cond_expr");
227 const auto *IDCall = Result.Nodes.getNodeAs<CallExpr>(
"id_call");
228 const auto *Loop = Result.Nodes.getNodeAs<Stmt>(
"backward_branch");
231 LoopType
Type = getLoopType(Loop);
234 diag(CondExpr->getBeginLoc(),
235 "backward branch (%select{do|while|for}0 loop) is ID-dependent due "
236 "to ID function call and may cause performance degradation")
241 IdDependencyRecord *IdDepVar = hasIdDepVar(CondExpr);
242 IdDependencyRecord *IdDepField = hasIdDepField(CondExpr);
244 diag(CondExpr->getBeginLoc(),
245 "backward branch (%select{do|while|for}0 loop) is ID-dependent due "
246 "to variable reference to %1 and may cause performance degradation")
247 <<
Type << IdDepVar->VariableDeclaration;
248 diag(IdDepVar->Location, IdDepVar->Message, DiagnosticIDs::Note);
249 }
else if (IdDepField) {
250 diag(CondExpr->getBeginLoc(),
251 "backward branch (%select{do|while|for}0 loop) is ID-dependent due "
252 "to member reference to %1 and may cause performance degradation")
253 <<
Type << IdDepField->FieldDeclaration;
254 diag(IdDepField->Location, IdDepField->Message, DiagnosticIDs::Note);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
constexpr llvm::StringLiteral Message