-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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][bytecode] Handle __builtin_memcmp #119544
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesFull diff: https://github.com/llvm/llvm-project/pull/119544.diff 2 Files Affected:
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index a0de193ec32a2f..a567a955bb536f 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -190,6 +190,12 @@ static bool interp__builtin_is_constant_evaluated(InterpState &S, CodePtr OpPC,
return true;
}
+/// Determine if T is a character type for which we guarantee that
+/// sizeof(T) == 1.
+static bool isOneByteCharacterType(QualType T) {
+ return T->isCharType() || T->isChar8Type();
+}
+
static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const Function *Func, const CallExpr *Call) {
@@ -197,11 +203,13 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
const Pointer &A = getParam<Pointer>(Frame, 0);
const Pointer &B = getParam<Pointer>(Frame, 1);
- if (ID == Builtin::BIstrcmp || ID == Builtin::BIstrncmp)
+ if (ID == Builtin::BIstrcmp || ID == Builtin::BIstrncmp ||
+ ID == Builtin::BImemcmp)
diagnoseNonConstexprBuiltin(S, OpPC, ID);
uint64_t Limit = ~static_cast<uint64_t>(0);
- if (ID == Builtin::BIstrncmp || ID == Builtin::BI__builtin_strncmp)
+ if (ID == Builtin::BIstrncmp || ID == Builtin::BI__builtin_strncmp ||
+ ID == Builtin::BI__builtin_memcmp || ID == Builtin::BImemcmp)
Limit = peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)))
.getZExtValue();
@@ -219,6 +227,23 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
assert(A.getFieldDesc()->isPrimitiveArray());
assert(B.getFieldDesc()->isPrimitiveArray());
+ bool StopAtZero =
+ (ID == Builtin::BIstrcmp || ID == Builtin::BI__builtin_strcmp ||
+ ID == Builtin::BIstrncmp || ID == Builtin::BI__builtin_strncmp);
+ bool IsRawByte = ID == Builtin::BImemcmp || ID == Builtin::BI__builtin_memcmp;
+
+ if (IsRawByte &&
+ (!isOneByteCharacterType(A.getFieldDesc()->getElemQualType()) ||
+ !isOneByteCharacterType(B.getFieldDesc()->getElemQualType()))) {
+ QualType CharTy1 = A.getFieldDesc()->getElemQualType();
+ QualType CharTy2 = B.getFieldDesc()->getElemQualType();
+ S.FFDiag(S.Current->getSource(OpPC),
+ diag::note_constexpr_memcmp_unsupported)
+ << ("'" + S.getASTContext().BuiltinInfo.getName(ID) + "'").str()
+ << CharTy1 << CharTy2;
+ return false;
+ }
+
unsigned IndexA = A.getIndex();
unsigned IndexB = B.getIndex();
int32_t Result = 0;
@@ -243,7 +268,7 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
Result = -1;
break;
}
- if (CA == 0 || CB == 0)
+ if (StopAtZero && (CA == 0 || CB == 0))
break;
}
@@ -1904,6 +1929,8 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
case Builtin::BIstrcmp:
case Builtin::BI__builtin_strncmp:
case Builtin::BIstrncmp:
+ case Builtin::BI__builtin_memcmp:
+ case Builtin::BImemcmp:
if (!interp__builtin_strcmp(S, OpPC, Frame, F, Call))
return false;
break;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 7dd08cb5fa1c35..485652867a44ec 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1208,3 +1208,24 @@ namespace BuiltinMemcpy {
static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
}
+
+namespace Memcmp {
+ constexpr unsigned char ku00fe00[] = {0x00, 0xfe, 0x00};
+ constexpr unsigned char ku00feff[] = {0x00, 0xfe, 0xff};
+ constexpr signed char ks00fe00[] = {0, -2, 0};
+ constexpr signed char ks00feff[] = {0, -2, -1};
+ static_assert(__builtin_memcmp(ku00feff, ks00fe00, 2) == 0);
+ static_assert(__builtin_memcmp(ku00feff, ks00fe00, 99) == 1);
+ static_assert(__builtin_memcmp(ku00fe00, ks00feff, 99) == -1);
+ static_assert(__builtin_memcmp(ks00feff, ku00fe00, 2) == 0);
+ static_assert(__builtin_memcmp(ks00feff, ku00fe00, 99) == 1);
+ static_assert(__builtin_memcmp(ks00fe00, ku00feff, 99) == -1);
+ static_assert(__builtin_memcmp(ks00fe00, ks00feff, 2) == 0);
+ static_assert(__builtin_memcmp(ks00feff, ks00fe00, 99) == 1);
+ static_assert(__builtin_memcmp(ks00fe00, ks00feff, 99) == -1);
+
+ struct Bool3Tuple { bool bb[3]; };
+ constexpr Bool3Tuple kb000100 = {{false, true, false}};
+ static_assert(sizeof(bool) != 1u || __builtin_memcmp(ks00fe00, kb000100.bb, 1) == 0); // both-error {{constant}} \
+ // both-note {{not supported}}
+}
|
d4518ea
to
3e5ca2b
Compare
3e5ca2b
to
060f96d
Compare
Check out https://lab.llvm.org/buildbot/#/builders/13/builds/4041:
|
We're seeing the same issue on s390x. Big endian not handled correctly? |
tbaederr
added a commit
to tbaederr/llvm-project
that referenced
this pull request
Dec 13, 2024
See the discussion in llvm#119678 (comment) and llvm#119544 (comment)
Yup, see #119851 |
tbaederr
added a commit
that referenced
this pull request
Dec 13, 2024
See the discussion in #119678 (comment) and #119544 (comment)
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.