[flang-commits] [compiler-rt] [llvm] [flang] [clang] [clang-tools-extra] [clang] Add support for new loop attribute [[clang::code_align()]] (PR #70762)

Aaron Ballman via flang-commits flang-commits at lists.llvm.org
Thu Nov 16 12:35:08 PST 2023


================
@@ -322,6 +322,59 @@ static Attr *handleUnlikely(Sema &S, Stmt *St, const ParsedAttr &A,
   return ::new (S.Context) UnlikelyAttr(S.Context, A);
 }
 
+CodeAlignAttr *Sema::BuildCodeAlignAttr(const AttributeCommonInfo &CI,
+                                        Expr *E) {
+  if (!E->isValueDependent()) {
+    llvm::APSInt ArgVal;
+    ExprResult Res = VerifyIntegerConstantExpression(E, &ArgVal);
+    if (Res.isInvalid())
+      return nullptr;
+    E = Res.get();
+
+    // This attribute requires an integer argument which is a constant power of
+    // two between 1 and 4096 inclusive.
+    if (ArgVal < CodeAlignAttr::MinimumAlignment ||
+        ArgVal > CodeAlignAttr::MaximumAlignment || !ArgVal.isPowerOf2()) {
+      if (std::optional<int64_t> Value = ArgVal.trySExtValue())
+        Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
+            << CI << CodeAlignAttr::MinimumAlignment
+            << CodeAlignAttr::MaximumAlignment << Value.value();
+      else
+        Diag(CI.getLoc(), diag::err_attribute_power_of_two_in_range)
+            << CI << CodeAlignAttr::MinimumAlignment
+            << CodeAlignAttr::MaximumAlignment << E;
+      return nullptr;
+    }
+  }
+  return new (Context) CodeAlignAttr(Context, CI, E);
+}
+
+static Attr *handleCodeAlignAttr(Sema &S, Stmt *St, const ParsedAttr &A) {
+
+  Expr *E = A.getArgAsExpr(0);
+  return S.BuildCodeAlignAttr(A, E);
+}
+
+// Emit duplicate error for [[clang::code_align()]] attribute.
+static void
+CheckForDuplicateCodeAlignAttrs(Sema &S,
+                                const SmallVectorImpl<const Attr *> &Attrs) {
+  auto FindFunc = [](const Attr *A) { return isa<const CodeAlignAttr>(A); };
+  const auto *FirstItr = std::find_if(Attrs.begin(), Attrs.end(), FindFunc);
+
+  if (FirstItr == Attrs.end()) // no attributes found
+    return;
+
+  const auto *LastFoundItr = FirstItr;
+
+  while (Attrs.end() != (LastFoundItr = std::find_if(LastFoundItr + 1,
+                                                     Attrs.end(), FindFunc))) {
+    S.Diag((*LastFoundItr)->getLocation(), diag::err_loop_attr_duplication)
+        << *FirstItr;
+    S.Diag((*FirstItr)->getLocation(), diag::note_previous_attribute);
+  }
----------------
AaronBallman wrote:

I think we could be lazier here perhaps (and tackle the suggestion from @erichkeane at the same time):
```
llvm::SmallVector<const Attr *, 2> Seen;
std::optional<llvm::APSInt> PreviouslySeen;
llvm::copy_if(Attrs, std::back_inserter(Seen), [&PreviouslySeen](const Attr *A) {
  if (const auto *CCA = dyn_cast<CodeAlignAttr>(A)) {
    const auto *CE = cast<ConstantExpr>(CCA->getAlignment());
    llvm::APSInt ArgVal = CE->getResultAsAPSInt();
    bool Ret = PreviouslySeen ? *PreviouslySeen != ArgVal : true;
    PreviouslySeen = ArgVal;
    return Ret;
  }
});
```
Then we know to diagnose if `Seen.size() > 1`.

https://github.com/llvm/llvm-project/pull/70762


More information about the flang-commits mailing list