Skip to content

[clang][bytecode] Don't set OnePastEnd bit for array elements #136422

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 19, 2025

Conversation

tbaederr
Copy link
Contributor

If we refer to arr[N], don't set the OnePastEnd bit of the APValue, since that is already encoded in the array index.

If we refer to arr[N], don't set the OnePastEnd bit of the APValue,
since that is already encoded in the array index.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Apr 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 19, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

If we refer to arr[N], don't set the OnePastEnd bit of the APValue, since that is already encoded in the array index.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Pointer.cpp (+5-4)
  • (modified) clang/test/AST/ByteCode/arrays.cpp (+10)
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 686ec381d232a..059503cae3561 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -223,6 +223,7 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
     UsePath = false;
 
   // Build the path into the object.
+  bool OnePastEnd = isOnePastEnd();
   Pointer Ptr = *this;
   while (Ptr.isField() || Ptr.isArrayElement()) {
 
@@ -251,9 +252,10 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
       Ptr = Ptr.expand();
       const Descriptor *Desc = Ptr.getFieldDesc();
       unsigned Index;
-      if (Ptr.isOnePastEnd())
+      if (Ptr.isOnePastEnd()) {
         Index = Ptr.getArray().getNumElems();
-      else
+        OnePastEnd = false;
+      } else
         Index = Ptr.getIndex();
 
       QualType ElemType = Desc->getElemQualType();
@@ -304,8 +306,7 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
   std::reverse(Path.begin(), Path.end());
 
   if (UsePath)
-    return APValue(Base, Offset, Path,
-                   /*IsOnePastEnd=*/!isElementPastEnd() && isOnePastEnd());
+    return APValue(Base, Offset, Path, OnePastEnd);
 
   return APValue(Base, Offset, APValue::NoLValuePath());
 }
diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp
index e50839e0f0877..934c3a3e7aff1 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -761,3 +761,13 @@ namespace PointerSubscript {
   struct S{};
   static_assert((foo<S>(), true));
 }
+
+namespace OnePastEndDiag {
+
+  constexpr int a(const int *b) {
+    return *b; // both-note {{read of dereferenced one-past-the-end pointer}}
+  }
+  constexpr int foo[] = {1,2};
+  constexpr int k = a(foo + 2); // both-error {{must be initialized by a constant expression}} \
+                                // both-note {{in call to 'a(&foo[2])'}}
+}

@tbaederr tbaederr merged commit 6196b4e into llvm:main Apr 19, 2025
15 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 19, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve2-vla-2stage running on linaro-g4-02 while building clang at step 12 "ninja check 2".

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

Here is the relevant piece of the build log for the reference
Step 12 (ninja check 2) failure: stage 2 checked (failure)
...
[922/1396] Linking CXX executable unittests/ExecutionEngine/ExecutionEngineTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[940/1396] Linking CXX executable unittests/Demangle/DemangleTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[942/1396] Linking CXX executable unittests/DebugInfo/DWARF/DebugInfoDWARFTests
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
[960/1396] Linking CXX executable unittests/InterfaceStub/InterfaceStubTests
FAILED: unittests/InterfaceStub/InterfaceStubTests 
: && /home/tcwg-buildbot/worker/clang-aarch64-sve2-vla-2stage/stage1.install/bin/clang++ -mcpu=neoverse-v2 -mllvm -scalable-vectorization=preferred -mllvm -treat-scalable-fixed-error-as-warning=false -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=lld -Wl,--color-diagnostics    -Wl,--gc-sections unittests/InterfaceStub/CMakeFiles/InterfaceStubTests.dir/ELFYAMLTest.cpp.o -o unittests/InterfaceStub/InterfaceStubTests  lib/libLLVMInterfaceStub.a  lib/libLLVMSupport.a  lib/libllvm_gtest_main.a  lib/libllvm_gtest.a  lib/libLLVMTestingSupport.a  lib/libLLVMObject.a  lib/libLLVMIRReader.a  lib/libLLVMBitReader.a  lib/libLLVMAsmParser.a  lib/libLLVMCore.a  lib/libLLVMRemarks.a  lib/libLLVMBitstreamReader.a  lib/libLLVMMCParser.a  lib/libLLVMMC.a  lib/libLLVMTextAPI.a  lib/libLLVMBinaryFormat.a  lib/libLLVMTargetParser.a  lib/libllvm_gtest.a  lib/libLLVMSupport.a  -lrt  -ldl  -lm  /usr/lib/aarch64-linux-gnu/libz.so  lib/libLLVMDemangle.a  -lpthread && :
clang++: warning: argument unused during compilation: '-mllvm -scalable-vectorization=preferred' [-Wunused-command-line-argument]
clang++: warning: argument unused during compilation: '-mllvm -treat-scalable-fixed-error-as-warning=false' [-Wunused-command-line-argument]
ld.lld: error: cannot open lib/libllvm_gtest.a: No such file or directory
ld.lld: error: cannot open lib/libllvm_gtest.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[963/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/ResourceTrackerTest.cpp.o
[965/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/RTDyldObjectLinkingLayerTest.cpp.o
[966/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/DemandedBitsTest.cpp.o
[967/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/ReOptimizeLayerTest.cpp.o
[968/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/JITLinkRedirectionManagerTest.cpp.o
[969/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ConstantRangeListTest.cpp.o
[970/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/AbstractCallSiteTest.cpp.o
[971/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/LazyCallThroughAndReexportsTest.cpp.o
[972/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/AttributesTest.cpp.o
[973/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/DebugTypeODRUniquingTest.cpp.o
[974/1396] Building CXX object unittests/FuzzMutate/CMakeFiles/FuzzMutateTests.dir/RandomIRBuilderTest.cpp.o
[975/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/CFGBuilder.cpp.o
[976/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/AsmWriterTest.cpp.o
[977/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ConstantFPRangeTest.cpp.o
[978/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ConstantsTest.cpp.o
[979/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/FunctionTest.cpp.o
[980/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/MapperJITLinkMemoryManagerTest.cpp.o
[981/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/BasicBlockTest.cpp.o
[982/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ManglerTest.cpp.o
[983/1396] Building CXX object unittests/ExecutionEngine/Orc/CMakeFiles/OrcJITTests.dir/SharedMemoryMapperTest.cpp.o
[984/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/MemoryModelRelaxationAnnotationsTest.cpp.o
[985/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/DominatorTreeBatchUpdatesTest.cpp.o
[986/1396] Building CXX object unittests/FuzzMutate/CMakeFiles/FuzzMutateTests.dir/OperationsTest.cpp.o
[987/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/MDBuilderTest.cpp.o
[988/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/BasicBlockDbgInfoTest.cpp.o
[989/1396] Building CXX object unittests/FuzzMutate/CMakeFiles/FuzzMutateTests.dir/StrategiesTest.cpp.o
[990/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ShuffleVectorInstTest.cpp.o
[991/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/ModuleSummaryIndexTest.cpp.o
[992/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/IntrinsicsTest.cpp.o
[993/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/DroppedVariableStatsIRTest.cpp.o
[994/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/LegacyPassManagerTest.cpp.o
[995/1396] Building CXX object unittests/IR/CMakeFiles/IRTests.dir/DominatorTreeTest.cpp.o

hnrklssn added a commit to hnrklssn/llvm-project that referenced this pull request Apr 24, 2025
Update clang/test/AST/ByteCode/cxx20.cpp to align with the new behaviour
in 6196b4e (llvm#136422).
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…36422)

If we refer to arr[N], don't set the OnePastEnd bit of the APValue,
since that is already encoded in the array index.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…36422)

If we refer to arr[N], don't set the OnePastEnd bit of the APValue,
since that is already encoded in the array index.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…36422)

If we refer to arr[N], don't set the OnePastEnd bit of the APValue,
since that is already encoded in the array index.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bytecode Issues for the clang bytecode constexpr interpreter 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.

4 participants