[clang] [clang][bytecode] Implement __noop (PR #106714)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 30 16:52:37 PDT 2024


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/106714

>From b04dc6d69039eeabe177cc5d109970734e0c29ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 30 Aug 2024 13:45:37 +0200
Subject: [PATCH] [clang][bytecode] Implement __noop

This does nothing and returns 0.
---
 clang/lib/AST/ByteCode/Function.cpp      |  3 ++-
 clang/lib/AST/ByteCode/InterpBuiltin.cpp |  4 ++++
 clang/test/AST/ByteCode/ms.cpp           | 15 +++++++++++++--
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Function.cpp b/clang/lib/AST/ByteCode/Function.cpp
index 25da6ae1bc7b61..52d6d05211f642 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -61,7 +61,8 @@ bool Function::isVirtual() const {
 static bool isUnevaluatedBuiltin(unsigned BuiltinID) {
   return BuiltinID == Builtin::BI__builtin_classify_type ||
          BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size ||
-         BuiltinID == Builtin::BI__builtin_constant_p;
+         BuiltinID == Builtin::BI__builtin_constant_p ||
+         BuiltinID == Builtin::BI__noop;
 }
 
 bool Function::isUnevaluatedBuiltin() const {
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 1a71bff25d2540..81e49f203524b7 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1593,6 +1593,10 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
       return false;
     break;
 
+  case Builtin::BI__noop:
+    pushInteger(S, 0, Call->getType());
+    break;
+
   default:
     S.FFDiag(S.Current->getLocation(OpPC),
              diag::note_invalid_subexpr_in_const_expr)
diff --git a/clang/test/AST/ByteCode/ms.cpp b/clang/test/AST/ByteCode/ms.cpp
index fe5ed219946e76..35ffcbccae3b3a 100644
--- a/clang/test/AST/ByteCode/ms.cpp
+++ b/clang/test/AST/ByteCode/ms.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -verify=ref,both %s -fms-extensions
-// RUN: %clang_cc1 -verify=expected,both %s -fexperimental-new-constant-interpreter -fms-extensions
+// RUN: %clang_cc1 -verify=ref,both %s -fms-extensions -fcxx-exceptions
+// RUN: %clang_cc1 -verify=expected,both %s -fexperimental-new-constant-interpreter -fms-extensions -fcxx-exceptions
 
 // ref-no-diagnostics
 // expected-no-diagnostics
@@ -8,3 +8,14 @@
 static_assert(_rotl(0x01, 5) == 32);
 
 static_assert(alignof(__unaligned int) == 1, "");
+
+static_assert(__noop() == 0, "");
+
+constexpr int noopIsActuallyNoop() {
+    int a = 0;
+    __noop(throw);
+    __noop(++a);
+    __noop(a = 100);
+    return a;
+}
+static_assert(noopIsActuallyNoop() == 0);



More information about the cfe-commits mailing list