Skip to content

[clang][bytecode] Reject constexpr-unknown pointers from Inc ops #135548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025

Conversation

tbaederr
Copy link
Contributor

We used to accept c++ as a known value here, causing wrong codegen.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 13, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

We used to accept c++ as a known value here, causing wrong codegen.


Full diff: https://github.com/llvm/llvm-project/pull/135548.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.h (+5)
  • (modified) clang/test/AST/ByteCode/codegen.cpp (+10-1)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 4e84dcc8d551d..b4e15b3ffbe68 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -771,6 +771,11 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
                   bool CanOverflow) {
   assert(!Ptr.isDummy());
 
+  if (!S.inConstantContext()) {
+    if (isConstexprUnknown(Ptr))
+      return false;
+  }
+
   if constexpr (std::is_same_v<T, Boolean>) {
     if (!S.getLangOpts().CPlusPlus14)
       return Invalid(S, OpPC);
diff --git a/clang/test/AST/ByteCode/codegen.cpp b/clang/test/AST/ByteCode/codegen.cpp
index ea2c812f30f6f..7c853a20362b8 100644
--- a/clang/test/AST/ByteCode/codegen.cpp
+++ b/clang/test/AST/ByteCode/codegen.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s                                         | FileCheck %s
 // RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s
 
 #ifdef __SIZEOF_INT128__
@@ -95,3 +95,12 @@ void f(A *a) {
   // CHECK: call void @_ZN1AD1Ev(
   A::E e3 = A().Foo;
 }
+
+int notdead() {
+  auto l = [c=0]() mutable {
+    return  c++ < 5 ? 10 : 12;
+  };
+  return l();
+}
+// CHECK: _ZZ7notdeadvEN3$_0clEv
+// CHECK: ret i32 %cond

We used to accept c++ as a known value here, causing wrong codegen.
@tbaederr tbaederr merged commit 974bda8 into llvm:main Apr 13, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 13, 2025

LLVM Buildbot has detected a new failure on builder flang-x86_64-windows running on minipc-ryzen-win while building clang at step 6 "build-unified-tree".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/166/builds/952

Here is the relevant piece of the build log for the reference
Step 6 (build-unified-tree) failure: build (failure)
...
39.742 [116/12/22] Linking CXX executable bin\sancov.exe
39.778 [115/12/23] Linking CXX executable bin\sanstats.exe
39.819 [114/12/24] Linking CXX executable bin\llvm-xray.exe
39.821 [113/12/25] Linking CXX executable bin\obj2yaml.exe
39.841 [112/12/26] Generating ../../bin/llvm-dlltool.exe
41.429 [111/12/27] Linking CXX executable bin\llvm-objcopy.exe
41.938 [110/12/28] Linking CXX executable bin\llvm-dwarfdump.exe
43.870 [109/12/29] Linking CXX executable bin\llvm-readobj.exe
44.419 [108/12/30] Linking CXX executable bin\llvm-ctxprof-util.exe
45.339 [107/12/31] Linking CXX executable bin\mlir-opt.exe
FAILED: bin/mlir-opt.exe 
C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --msvc-ver=1943 --intdir=tools\mlir\tools\mlir-opt\CMakeFiles\mlir-opt.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100220~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\mlir-opt.rsp  /out:bin\mlir-opt.exe /implib:lib\mlir-opt.lib /pdb:bin\mlir-opt.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console && cd ."
LINK: command "C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\mlir-opt.rsp /out:bin\mlir-opt.exe /implib:lib\mlir-opt.lib /pdb:bin\mlir-opt.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console /MANIFEST:EMBED,ID=1" failed (exit code 0) with the following output:

  Version 14.43.34809.0

  ExceptionCode            = C0000005
  ExceptionFlags           = 00000000
  ExceptionAddress         = 00007FF658EE36A2 (00007FF658EC0000) "C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1443~1.348\bin\Hostx64\x64\link.exe"
  NumberParameters         = 00000002
  ExceptionInformation[ 0] = 0
  ExceptionInformation[ 1] = FFFFFFFFFFFFFFFF

CONTEXT:
  Rax    = 2A6DC610340  R8     = 4000000000000
  Rbx    = AAAAAAAAAAAAAAAB  R9     = 0
  Rcx    = 2C9  R10    = 2A6DD160138
  Rdx    = 2A6D6909630  R11    = C1
  Rsp    = DDD3BFBF40  R12    = 0
  Rbp    = DDD3BFC040  E13    = 2A6DECFAB00

  Rsi    = A64E  R14    = 3A64

  Rdi    = 2000  R15    = 2A6DFD9F478

  Rip    = 7FF658EE36A2  EFlags = 10206

  SegCs  = 33  SegDs  = 2B

  SegSs  = 2B  SegEs  = 2B

  SegFs  = 53  SegGs  = 2B

  Dr0    = 0  Dr3    = 0

  Dr1    = 0  Dr6    = 0

  Dr2    = 0  Dr7    = 0


var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…m#135548)

We used to accept c++ as a known value here, causing wrong codegen.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants