[PATCH] D76525: Expose cache line size in __builtin_get_cpu_cache_line_size

Zoe Carver via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 20 14:07:15 PDT 2020


zoecarver created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
zoecarver added reviewers: jfb, EricWF, efriedma, jyknight, echristo, __simt__.
Herald added a subscriber: dexonsmith.

This patch creates the __builtin_get_cpu_cache_line_size to wrap the cpu cache line size API from D74918 <https://reviews.llvm.org/D74918>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76525

Files:
  clang/include/clang/Basic/Builtins.def
  clang/lib/AST/ExprConstant.cpp
  clang/test/SemaCXX/builtin-cache-line-size.cpp


Index: clang/test/SemaCXX/builtin-cache-line-size.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/builtin-cache-line-size.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang -mcpu=i368 %s
+// RUN: ./a.out
+
+#include <cassert>
+
+size_t a() {
+  return __builtin_get_cpu_cache_line_size();
+}
+
+size_t b(bool x) {
+  if (x) {
+    return __builtin_get_cpu_cache_line_size();
+  }
+  return 0;
+}
+
+int main() {
+  assert(a() == 64);
+  assert(b(true) == 64);
+  assert(b(false) == 0);
+}
+
+// This is based on the value for i368.
+static_assert(__builtin_get_cpu_cache_line_size() == 64, "");
+
+struct keep_apart {
+  alignas(__builtin_get_cpu_cache_line_size()) int cat;
+  alignas(__builtin_get_cpu_cache_line_size()) int dog;
+};
+
+static_assert(sizeof(keep_apart) == __builtin_get_cpu_cache_line_size() * 2 ||
+              __builtin_get_cpu_cache_line_size() == 0, "");
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -10910,6 +10910,13 @@
     return Success(Info.InConstantContext, E);
   }
 
+  case Builtin::BI__builtin_get_cpu_cache_line_size: {
+    Optional<unsigned> cacheLineSize = Info.Ctx.getTargetInfo().getCPUCacheLineSize();
+    if (cacheLineSize.hasValue())
+      return Success(cacheLineSize.getValue(), E);
+    return Success(0, E);
+  }
+
   case Builtin::BI__builtin_ctz:
   case Builtin::BI__builtin_ctzl:
   case Builtin::BI__builtin_ctzll:
Index: clang/include/clang/Basic/Builtins.def
===================================================================
--- clang/include/clang/Basic/Builtins.def
+++ clang/include/clang/Basic/Builtins.def
@@ -1480,6 +1480,7 @@
 BUILTIN(__builtin_char_memchr, "c*cC*iz", "n")
 BUILTIN(__builtin_dump_struct, "ivC*v*", "tn")
 BUILTIN(__builtin_preserve_access_index, "v.", "t")
+BUILTIN(__builtin_get_cpu_cache_line_size, "z", "nctu")
 
 // Alignment builtins (uses custom parsing to support pointers and integers)
 BUILTIN(__builtin_is_aligned, "bvC*z", "nct")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76525.251753.patch
Type: text/x-patch
Size: 2108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200320/e3e67b3e/attachment.bin>


More information about the cfe-commits mailing list