[PATCH] D125722: [Attribute] Add clang optsize attribute

Stephen Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 16 13:04:10 PDT 2022


steplong created this revision.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
steplong requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add the ability to put `__attribute__((optsize))` on functions. Depends on D125720 <https://reviews.llvm.org/D125720>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125722

Files:
  clang/include/clang/Basic/Attr.td
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Sema/SemaDeclAttr.cpp


Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -6763,6 +6763,19 @@
     checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+static void handleOptimizeSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
+  if (!AL.isArgIdent(0)) {
+    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
+        << AL << 1 << AANT_ArgumentIdentifier;
+    return;
+  }
+
+  OptimizeSizeAttr::Kind Kind;
+  IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
+  OptimizeSizeAttr::ConvertStrToKind(II->getName(), Kind);
+  D->addAttr(::new (S.Context) OptimizeSizeAttr(S.Context, AL, Kind));
+}
+
 static void handleFramePointer(Sema &S, Decl *D, const ParsedAttr &AL) {
   if (!AL.isArgIdent(0)) {
     S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
@@ -8534,6 +8547,9 @@
   case ParsedAttr::AT_MinSize:
     handleMinSizeAttr(S, D, AL);
     break;
+  case ParsedAttr::AT_OptimizeSize:
+    handleOptimizeSizeAttr(S, D, AL);
+    break;
   case ParsedAttr::AT_OptimizeNone:
     handleOptimizeNoneAttr(S, D, AL);
     break;
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1653,6 +1653,19 @@
   if (!GD.getDecl())
     return;
 
+  bool HasOptnone = GD.getDecl()->hasAttr<OptimizeNoneAttr>();
+  bool HasOptsizeOn = false;
+  bool HasOptsizeOff = false;
+  if (GD.getDecl()->hasAttr<OptimizeSizeAttr>()) {
+    OptimizeSizeAttr *Attr = GD.getDecl()->getAttr<OptimizeSizeAttr>();
+    HasOptsizeOn = Attr->getKind() == OptimizeSizeAttr::On;
+    HasOptsizeOff = Attr->getKind() == OptimizeSizeAttr::Off;
+  }
+  if (HasOptsizeOn && !HasOptnone)
+    F->addFnAttr(llvm::Attribute::OptimizeForSize);
+  if (HasOptsizeOff)
+    F->removeFnAttr(llvm::Attribute::OptimizeForSize);
+
   if (GD.getDecl()->hasAttr<FramePointerAttr>()) {
     F->removeFnAttr("frame-pointer");
     FramePointerAttr *Attr = GD.getDecl()->getAttr<FramePointerAttr>();
Index: clang/include/clang/Basic/Attr.td
===================================================================
--- clang/include/clang/Basic/Attr.td
+++ clang/include/clang/Basic/Attr.td
@@ -1451,6 +1451,16 @@
   let Documentation = [Undocumented];
 }
 
+def OptimizeSize: InheritableAttr {
+  // This attribute has no spellings as it is only ever created implicitly.
+  let Spellings = [];
+  let Subjects = SubjectList<[Function]>;
+  let Args = [EnumArgument<"Kind", "Kind",
+                ["off", "on"],
+                ["Off", "On"]>];
+  let Documentation = [Undocumented];
+}
+
 def FlagEnum : InheritableAttr {
   let Spellings = [Clang<"flag_enum">];
   let Subjects = SubjectList<[Enum]>;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125722.429825.patch
Type: text/x-patch
Size: 2827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220516/bb8059f5/attachment.bin>


More information about the cfe-commits mailing list