EricWF created this revision.
EricWF added reviewers: rsmith, MaskRay, bruno, void.
Herald added a subscriber: kristina.

This patch implements `__builtin_is_constant_evaluated` as specifier by P0595R2 
<https://wg21.link/p0595r2>. It is built on the back of Bill Wendling's work 
for `__builtin_constant_p()`.

More tests to come, but early feedback is appreciated.

I plan to implement warnings for common mis-usages like those belowe in a 
following patch:

  void foo(int x) {
    if constexpr (std::is_constant_evaluated())) { // condition is always 
`true`. Should use plain `if` instead.
     foo_constexpr(x);
    } else {
      foo_runtime(x);
    }
  }




Repository:
  rC Clang

https://reviews.llvm.org/D55500

Files:
  include/clang/Basic/Builtins.def
  lib/AST/ExprConstant.cpp
  lib/Basic/Builtins.cpp
  test/CodeGenCXX/builtin-is-constant-evaluated.cpp
  test/Sema/builtins.c
  test/SemaCXX/builtin-is-constant-evaluated.cpp

Index: test/SemaCXX/builtin-is-constant-evaluated.cpp
===================================================================
--- /dev/null
+++ test/SemaCXX/builtin-is-constant-evaluated.cpp
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -std=c++2a -verify %s -fcxx-exceptions -triple=x86_64-linux-gnu
+
+using size_t = decltype(sizeof(int));
+
+namespace std {
+inline constexpr bool is_constant_evaluated() noexcept {
+  return __builtin_is_constant_evaluated();
+}
+} // namespace std
+
+extern int dummy; // expected-note 1+ {{declared here}}
+
+static_assert(__builtin_is_constant_evaluated());
+
+constexpr bool b = __builtin_is_constant_evaluated();
+static_assert(b);
+
+const int n = __builtin_is_constant_evaluated() ? 4 : dummy;
+static_assert(n == 4);
+constexpr int cn = __builtin_is_constant_evaluated() ? 11 : dummy;
+static_assert(cn == 11);
+// expected-error@+1 {{'bn' must be initialized by a constant expression}}
+constexpr int bn = __builtin_is_constant_evaluated() ? dummy : 42; // expected-note {{non-const variable 'dummy' is not allowed}}
+
+
+const int n2 = __builtin_is_constant_evaluated() ? dummy : 42; // expected-note {{declared here}}
+static_assert(n2 == 42); // expected-error {{static_assert expression is not an integral constant}}
+// expected-note@-1 {{initializer of 'n2' is not a constant expression}}
+
+template<bool V> struct Templ { static_assert(V);};
+Templ<__builtin_is_constant_evaluated()> x; // type X<true>
+
+template <class T>
+void test_if_constexpr() {
+  if constexpr (__builtin_is_constant_evaluated()) {
+    static_assert(__is_same(T, int));
+  } else {
+    using Test = typename T::DOES_NOT_EXIST;
+  }
+}
+template void test_if_constexpr<int>();
+
+void test_array_decl() {
+  char x[__builtin_is_constant_evaluated() + std::is_constant_evaluated()];
+  static_assert(sizeof(x) == 2, "");
+}
+
+void test_case_stmt(int x) {
+  switch (x) {
+  case 0:                                                                // OK
+  case __builtin_is_constant_evaluated():                                // expected-note {{previous case}}
+  case std::is_constant_evaluated() + __builtin_is_constant_evaluated(): // expected-note {{previous case}}
+  case 1:                                                                // expected-error {{duplicate case value '1'}}
+  case 2:                                                                // expected-error {{duplicate case value '2'}}
+    break;
+  }
+}
+
+constexpr size_t good_array_size() {
+  return std::is_constant_evaluated() ? 42 : static_cast<size_t>(-1);
+}
+
+constexpr size_t bad_array_size() {
+  return std::is_constant_evaluated() ? static_cast<size_t>(-1) : 13;
+}
+
+template <class T>
+constexpr T require_constexpr(T v) {
+  if (!std::is_constant_evaluated())
+    throw "BOOM";
+  return v;
+}
+
+void test_new_expr() {
+  constexpr size_t TooLarge = -1;
+  auto *x = new int[std::is_constant_evaluated() ? 1 : TooLarge];      // expected-error {{array is too large}}
+  auto *x2 = new int[std::is_constant_evaluated() ? TooLarge : 1];     // OK
+  auto *y = new int[1][std::is_constant_evaluated() ? TooLarge : 1]{}; // expected-error {{array is too large}}
+  auto *y2 = new int[1][require_constexpr(42)];
+}
Index: test/Sema/builtins.c
===================================================================
--- test/Sema/builtins.c
+++ test/Sema/builtins.c
@@ -296,3 +296,9 @@
   memcpy(buf, src, 11); // expected-warning{{'memcpy' will always overflow; destination buffer has size 10, but size argument is 11}}
   my_memcpy(buf, src, 11); // expected-warning{{'__builtin___memcpy_chk' will always overflow; destination buffer has size 10, but size argument is 11}}
 }
+
+// Test that __builtin_is_constant_evaluated() is not allowed in C
+int test_cxx_builtin() {
+  // expected-error@+1 {{use of unknown builtin '__builtin_is_constant_evaluated'}}
+  return __builtin_is_constant_evaluated();
+}
Index: test/CodeGenCXX/builtin-is-constant-evaluated.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/builtin-is-constant-evaluated.cpp
@@ -0,0 +1,85 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm %s -std=c++2a -o %t.ll
+// RUN: FileCheck -check-prefix=CHECK-FN-CG -input-file=%t.ll %s
+// RUN: FileCheck -check-prefix=CHECK-STATIC -input-file=%t.ll %s
+// RUN: FileCheck -check-prefix=CHECK-DYN -input-file=%t.ll %s
+// RUN: FileCheck -check-prefix=CHECK-ARR -input-file=%t.ll %s
+
+using size_t = decltype(sizeof(int));
+
+#define CONSTINIT __attribute__((require_constant_initialization))
+
+extern "C" [[noreturn]] void BOOM();
+extern "C" size_t RANDU();
+
+namespace std {
+inline constexpr bool is_constant_evaluated() noexcept {
+  return __builtin_is_constant_evaluated();
+}
+} // namespace std
+
+// CHECK-FN-CG-LABEL: define zeroext i1 @_Z3foov()
+// CHECK-FN-CG: ret i1 false
+bool foo() {
+  return __builtin_is_constant_evaluated();
+}
+
+// CHECK-FN-CG-LABEL: define linkonce_odr i32 @_Z1fv()
+constexpr int f() {
+  // CHECK-FN-CG: store i32 17, i32* %n, align 4
+  // CHECK-FN-CG: store i32 17, i32* %m, align 4
+  // CHECK-FN-CG:  %1 = load i32, i32* %m, align 4
+  // CHECK-FN-CG: %add = add nsw i32 %1, 13
+  // CHECK-FN-CG: ret i32 %add
+  const int n = __builtin_is_constant_evaluated() ? 13 : 17; // n == 13
+  int m = __builtin_is_constant_evaluated() ? 13 : 17;       // m might be 13 or 17 (see below)
+  char arr[n] = {};                                          // char[13]
+  return m + int(sizeof(arr));
+}
+
+// CHECK-STATIC-DAG: @p = global i32 26,
+CONSTINIT int p = f(); // f().m == 13; initialized to 26
+
+// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init()
+// CHECK-DYN: %0 = load i32, i32* @p, align 4
+// CHECK-DYN-NEXT: %call = call i32 @_Z1fv()
+// CHECK-DYN-NEXT: %add = add nsw i32 %0, %call
+// CHECK-DYN-NEXT: store i32 %add, i32* @q, align 4
+// CHECK-DYN-NEXT: ret void
+int q = p + f(); // m == 17 for this call; initialized to 56
+
+int y;
+
+// CHECK-STATIC-DAG: @b = global i32 2,
+CONSTINIT int b = __builtin_is_constant_evaluated() ? 2 : y; // static initialization to 2
+
+// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init.1()
+// CHECK-DYN: %0 = load i32, i32* @y, align 4
+// CHECK-DYN: %1 = load i32, i32* @y, align 4
+// CHECK-DYN-NEXT: %add = add
+// CHECK-DYN-NEXT: store i32 %add, i32* @c,
+int c = y + (__builtin_is_constant_evaluated() ? 2 : y); // dynamic initialization to y+y
+
+// CHECK-DYN-LABEL: define internal void @__cxx_global_var_init.2()
+// CHECK-DYN: store i32 1, i32* @_ZL1a, align 4
+// CHECK-DYN-NEXT: ret void
+const int a = __builtin_is_constant_evaluated() ? y : 1; // dynamic initialization to 1
+const int *a_sink = &a;
+
+// CHECK-ARR-LABEL: define void @_Z13test_arr_exprv
+void test_arr_expr() {
+  // CHECK-ARR: %x1 = alloca [101 x i8],
+  char x1[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? 101 : 1];
+
+  // CHECK-ARR: %x2 = alloca [42 x i8],
+  char x2[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? 42 : RANDU()];
+
+  // CHECK-ARR: call i8* @llvm.stacksave()
+  // CHECK-ARR: %vla = alloca i8, i64 13,
+  char x3[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? RANDU() : 13];
+}
+
+// CHECK-ARR-LABEL: define void @_Z17test_new_arr_exprv
+void test_new_arr_expr() {
+  // CHECK-ARR: call i8* @_Znam(i64 17)
+  new char[std::is_constant_evaluated() || __builtin_is_constant_evaluated() ? 1: 17];
+}
Index: lib/Basic/Builtins.cpp
===================================================================
--- lib/Basic/Builtins.cpp
+++ lib/Basic/Builtins.cpp
@@ -76,9 +76,11 @@
   bool OclCUnsupported = !LangOpts.OpenCL &&
                          (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
   bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
+  bool CPlusPlusUnsupported = !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
   return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
          !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
-         !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported;
+         !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
+         !CPlusPlusUnsupported;
 }
 
 /// initializeBuiltins - Mark the identifiers for all the builtins with their
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -8204,6 +8204,9 @@
     return Success(false, E);
   }
 
+  case Builtin::BI__builtin_is_constant_evaluated:
+    return Success(Info.InConstantContext, E);
+
   case Builtin::BI__builtin_ctz:
   case Builtin::BI__builtin_ctzl:
   case Builtin::BI__builtin_ctzll:
@@ -10859,6 +10862,8 @@
                                   const ASTContext &Ctx) const {
   EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression;
   EvalInfo Info(Ctx, Result, EM);
+  Info.InConstantContext = true;
+
   if (!::Evaluate(Result.Val, Info, this))
     return false;
 
Index: include/clang/Basic/Builtins.def
===================================================================
--- include/clang/Basic/Builtins.def
+++ include/clang/Basic/Builtins.def
@@ -755,6 +755,9 @@
 BUILTIN(__builtin_index, "c*cC*i", "Fn")
 BUILTIN(__builtin_rindex, "c*cC*i", "Fn")
 
+// Random C++ builtins.
+LANGBUILTIN(__builtin_is_constant_evaluated, "b", "ncu", CXX_LANG)
+
 // Microsoft builtins.  These are only active with -fms-extensions.
 LANGBUILTIN(_alloca,          "v*z", "n", ALL_MS_LANGUAGES)
 LANGBUILTIN(__annotation,     "wC*.","n", ALL_MS_LANGUAGES)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to