Skip to content
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

[clang][Interp] Support AddOffset with 128bit offsets #68679

Merged
merged 1 commit into from
Oct 13, 2023

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Oct 10, 2023

We do a similar thing a few lines above for Index:

  // Get a version of the index comparable to the type.
  T Index = T::from(Ptr.getIndex(), Offset.bitWidth());

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

llvmbot commented Oct 10, 2023

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

We do a similar things a few lines above for Index:

  // Get a version of the index comparable to the type.
  T Index = T::from(Ptr.getIndex(), Offset.bitWidth());

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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/Interp.h (+1-1)
  • (modified) clang/test/AST/Interp/intap.cpp (+9)
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 47dc1d08c9c4d8b..620d797da27023f 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1437,7 +1437,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
     return false;
   };
 
-  unsigned MaxOffset = MaxIndex - Ptr.getIndex();
+  T MaxOffset = T::from(MaxIndex - Ptr.getIndex(), Offset.bitWidth());
   if constexpr (Op == ArithOp::Add) {
     // If the new offset would be negative, bail out.
     if (Offset.isNegative() && (Offset.isMin() || -Offset > Index))
diff --git a/clang/test/AST/Interp/intap.cpp b/clang/test/AST/Interp/intap.cpp
index b3f02d2b769531d..e9e68438bb597aa 100644
--- a/clang/test/AST/Interp/intap.cpp
+++ b/clang/test/AST/Interp/intap.cpp
@@ -76,6 +76,15 @@ namespace i128 {
                                            // expected-note {{is outside the range of representable values of type}}
 }
 
+namespace AddSubOffset {
+  constexpr __int128 A = 1;
+  constexpr int arr[] = {1,2,3};
+  constexpr const int *P = arr + A;
+  static_assert(*P == 2, "");
+  constexpr const int *P2 = P - A;
+  static_assert(*P2 == 1,"");
+}
+
 #else
 /// No int128 support, so no expected directives.
 

@@ -1437,7 +1437,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
return false;
};

unsigned MaxOffset = MaxIndex - Ptr.getIndex();
T MaxOffset = T::from(MaxIndex - Ptr.getIndex(), Offset.bitWidth());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should MaxIndex just be a T as well? I wonder if this function should just be 'fully dedicated' to being T instead of just this 1 variable here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno, We never compare MaxIndex with anything of type T. It's even only used twice and once is in the diagnostic output. The other one is line 1440 above.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the math operate on a T consistently, especially given that Offset and Index are already a T. The mixture of types is a bit of a code smell IMO.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tbaederr tbaederr merged commit 3efa479 into llvm:main Oct 13, 2023
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Oct 20, 2023
Local branch amd-gfx b77230b Revert "Revert "ConstantFolding: Constant fold denormal inputs to canonicalize for IEEE""
Remote branch main 3efa479 [clang][Interp] Support AddOffset with 128bit offsets (llvm#68679)
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.

4 participants