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

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


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

>From 0423c99a3b539e20c807ae4a773521a5ad5063c6 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/ByteCodeEmitter.cpp |  4 +++-
 clang/lib/AST/ByteCode/InterpBuiltin.cpp   |  4 ++++
 clang/test/AST/ByteCode/ms.cpp             | 15 +++++++++++++--
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 35ae1547939fdd..6985ed2aceb502 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -29,7 +29,9 @@ using namespace clang::interp;
 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;
+
 }
 
 Function *ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
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