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

Erich Keane via flang-commits flang-commits at lists.llvm.org
Thu Nov 16 11:18:10 PST 2023


================
@@ -322,6 +322,71 @@ 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 < 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,
+                                            ArrayRef<const Attr *> Attrs) {
+  // Create a list of CodeAlign attributes only.
+  SmallVector<const CodeAlignAttr *, 8> OnlyCodeAlignAttrs;
----------------
erichkeane wrote:

> Because this is a statement attribute, I don't think checking for duplicates is necessary or warrants this much effort. We check for conflicting duplicates on declaration attributes because redeclarations can easily introduce additional attributes. But the same is not true for statements -- the only way to get a duplicate there is with significant effort and it seems like "first/last one wins" would be fine to document. WDYT?

Given how common it is to wrap attributes in macros, and how common it is to accidentally introduce duplicates that way, I still think there is value in diagnosing that a duplicate was ignored.

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


More information about the flang-commits mailing list