Skip to content

Commit 5cd4383

Browse files
committed
[clang] Xteam reduction must filter out unhandled unary operators.
Change-Id: Iffd5f7a9dd1f30951d63a25b86ee7e41d34ed8b8
1 parent eae8647 commit 5cd4383

File tree

3 files changed

+1096
-18
lines changed

3 files changed

+1096
-18
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7776,14 +7776,27 @@ class NoLoopStepChecker final : public ConstStmtVisitor<NoLoopStepChecker> {
77767776
class XteamRedExprChecker final : public ConstStmtVisitor<XteamRedExprChecker> {
77777777
public:
77787778
XteamRedExprChecker(const CodeGenModule::XteamRedVarMap &RVM)
7779-
: RedMap{RVM}, IsSupported{true} {}
7779+
: RedMap(RVM), IsSupported(true) {}
77807780
XteamRedExprChecker() = delete;
77817781

77827782
bool isSupported() const { return IsSupported; }
77837783

77847784
void VisitStmt(const Stmt *S) {
77857785
if (!S)
77867786
return;
7787+
7788+
auto isExprXteamRedVar = [this](const Expr *E) {
7789+
if (!isa<DeclRefExpr>(E))
7790+
return false;
7791+
auto *Decl = cast<DeclRefExpr>(E)->getDecl();
7792+
if (!isa<VarDecl>(Decl))
7793+
return false;
7794+
auto *VD = cast<VarDecl>(Decl);
7795+
if (RedMap.find(VD) != RedMap.end())
7796+
return true;
7797+
return false;
7798+
};
7799+
77877800
if (isa<BinaryOperator>(S)) {
77887801
const BinaryOperator *BinOpExpr = cast<BinaryOperator>(S);
77897802
// Even though we filtered out everything except the sum reduction
@@ -7793,19 +7806,7 @@ class XteamRedExprChecker final : public ConstStmtVisitor<XteamRedExprChecker> {
77937806
// red-var = red-var + <expr> and red-var = <expr> + red-var.
77947807
// We punt on anything more complex.
77957808

7796-
auto isExprXteamRedVar = [this](Expr *E) {
7797-
if (!isa<DeclRefExpr>(E))
7798-
return false;
7799-
auto *Decl = cast<DeclRefExpr>(E)->getDecl();
7800-
if (!isa<VarDecl>(Decl))
7801-
return false;
7802-
auto *VD = cast<VarDecl>(Decl);
7803-
if (RedMap.find(VD) != RedMap.end())
7804-
return true;
7805-
return false;
7806-
};
7807-
7808-
Expr *LHS = BinOpExpr->getLHS()->IgnoreImpCasts();
7809+
const Expr *LHS = BinOpExpr->getLHS()->IgnoreImpCasts();
78097810
if (isExprXteamRedVar(LHS)) {
78107811
auto BinOpExprOp = BinOpExpr->getOpcode();
78117812
if (BinOpExprOp != BO_Assign && BinOpExprOp != BO_AddAssign &&
@@ -7816,33 +7817,44 @@ class XteamRedExprChecker final : public ConstStmtVisitor<XteamRedExprChecker> {
78167817
// We only need to further examine the assignment case.
78177818
// If += or +, Codegen will extract the rhs.
78187819
if (BinOpExpr->getOpcode() == BO_Assign) {
7819-
Expr *RHS = BinOpExpr->getRHS()->IgnoreImpCasts();
7820+
const Expr *RHS = BinOpExpr->getRHS()->IgnoreImpCasts();
78207821
if (!isa<BinaryOperator>(RHS)) {
78217822
IsSupported = false;
78227823
return;
78237824
}
7824-
BinaryOperator *BinOpRHS = cast<BinaryOperator>(RHS);
7825+
const BinaryOperator *BinOpRHS = cast<BinaryOperator>(RHS);
78257826
if (BinOpRHS->getOpcode() != BO_Add) {
78267827
IsSupported = false;
78277828
return;
78287829
}
7829-
Expr *LHSBinOpRHS = BinOpRHS->getLHS()->IgnoreImpCasts();
7830-
Expr *RHSBinOpRHS = BinOpRHS->getRHS()->IgnoreImpCasts();
7830+
const Expr *LHSBinOpRHS = BinOpRHS->getLHS()->IgnoreImpCasts();
7831+
const Expr *RHSBinOpRHS = BinOpRHS->getRHS()->IgnoreImpCasts();
78317832
if (!isExprXteamRedVar(LHSBinOpRHS) &&
78327833
!isExprXteamRedVar(RHSBinOpRHS)) {
78337834
IsSupported = false;
78347835
return;
78357836
}
78367837
}
78377838
}
7839+
} else if (isa<UnaryOperator>(S)) {
7840+
const Expr *UnaryOpExpr =
7841+
cast<UnaryOperator>(S)->getSubExpr()->IgnoreImpCasts();
7842+
// Xteam reduction does not handle unary operators currently.
7843+
if (isExprXteamRedVar(UnaryOpExpr)) {
7844+
IsSupported = false;
7845+
return;
7846+
}
78387847
}
7848+
78397849
for (const Stmt *Child : S->children())
78407850
if (Child)
78417851
Visit(Child);
78427852
}
78437853

78447854
private:
7855+
/// Map of reduction variables for this directive.
78457856
const CodeGenModule::XteamRedVarMap &RedMap;
7857+
/// Set to false if codegen does not support the reduction expression.
78467858
bool IsSupported;
78477859
};
78487860

0 commit comments

Comments
 (0)