[PATCH] D78098: [CGExprAgg] Fix infinite loop in `findPeephole`

Ehud Katz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 15 04:20:36 PDT 2020


ekatz updated this revision to Diff 257665.
ekatz edited the summary of this revision.
ekatz added a comment.

Simplify function using IgnoreParenNoopCasts.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78098/new/

https://reviews.llvm.org/D78098

Files:
  clang/lib/CodeGen/CGExprAgg.cpp
  clang/test/CodeGen/atomic-struct-bug.cpp


Index: clang/test/CodeGen/atomic-struct-bug.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/atomic-struct-bug.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
+// PR45476
+
+// This test used to get into an infinite loop,
+// which, in turn, caused clang to never finish execution.
+
+struct s3 {
+  char a, b, c;
+};
+
+_Atomic struct s3 a;
+
+extern "C" void foo() {
+  // CHECK-LABEL: @foo
+  // CHECK: store atomic i32
+
+  a = s3{1, 2, 3};
+}
+
Index: clang/lib/CodeGen/CGExprAgg.cpp
===================================================================
--- clang/lib/CodeGen/CGExprAgg.cpp
+++ clang/lib/CodeGen/CGExprAgg.cpp
@@ -677,17 +677,13 @@
 
 /// Attempt to look through various unimportant expressions to find a
 /// cast of the given kind.
-static Expr *findPeephole(Expr *op, CastKind kind) {
-  while (true) {
-    op = op->IgnoreParens();
-    if (CastExpr *castE = dyn_cast<CastExpr>(op)) {
-      if (castE->getCastKind() == kind)
-        return castE->getSubExpr();
-      if (castE->getCastKind() == CK_NoOp)
-        continue;
-    }
-    return nullptr;
+static Expr *findPeephole(Expr *op, CastKind kind, const ASTContext &ctx) {
+  op = op->IgnoreParenNoopCasts(ctx);
+  if (auto castE = dyn_cast<CastExpr>(op)) {
+    if (castE->getCastKind() == kind)
+      return castE->getSubExpr();
   }
+  return nullptr;
 }
 
 void AggExprEmitter::VisitCastExpr(CastExpr *E) {
@@ -776,7 +772,8 @@
       (isToAtomic ? CK_AtomicToNonAtomic : CK_NonAtomicToAtomic);
 
     // These two cases are reverses of each other; try to peephole them.
-    if (Expr *op = findPeephole(E->getSubExpr(), peepholeTarget)) {
+    if (Expr *op =
+            findPeephole(E->getSubExpr(), peepholeTarget, CGF.getContext())) {
       assert(CGF.getContext().hasSameUnqualifiedType(op->getType(),
                                                      E->getType()) &&
            "peephole significantly changed types?");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78098.257665.patch
Type: text/x-patch
Size: 2006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200415/3980c271/attachment.bin>


More information about the cfe-commits mailing list