-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang][bytecode] Start implementing __builtin_{,dynamic}_object_size #136478
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
Conversation
This file contains hidden or 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/136478.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Descriptor.h b/clang/lib/AST/ByteCode/Descriptor.h
index a0705cc8c377f..532b407c2c788 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -265,7 +265,7 @@ struct Descriptor final {
bool isUnknownSizeArray() const { return Size == UnknownSizeMark; }
/// Checks if the descriptor is of a primitive.
- bool isPrimitive() const { return !IsArray && !ElemRecord && !IsDummy; }
+ bool isPrimitive() const { return !IsArray && !ElemRecord && PrimT; }
/// Checks if the descriptor is of an array.
bool isArray() const { return IsArray; }
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 34553301ef630..1eb210c0cac4f 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -2179,6 +2179,52 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
return true;
}
+static unsigned computeFullDescSize(const ASTContext &ASTCtx,
+ const Descriptor *Desc) {
+
+ if (Desc->isPrimitive())
+ return ASTCtx.getTypeSizeInChars(Desc->getType()).getQuantity();
+
+ if (Desc->isArray())
+ return ASTCtx.getTypeSizeInChars(Desc->getElemQualType()).getQuantity() *
+ Desc->getNumElems();
+
+ if (Desc->isRecord())
+ return ASTCtx.getTypeSizeInChars(Desc->getType()).getQuantity();
+
+ llvm_unreachable("Unhandled descriptor type");
+ return 0;
+}
+
+static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
+ const InterpFrame *Frame,
+ const Function *Func,
+ const CallExpr *Call) {
+ PrimType KindT = *S.getContext().classify(Call->getArg(1));
+ unsigned Kind = peekToAPSInt(S.Stk, KindT).getZExtValue();
+
+ assert(Kind <= 3 && "unexpected kind");
+
+ const Pointer &Ptr =
+ S.Stk.peek<Pointer>(align(primSize(KindT)) + align(primSize(PT_Ptr)));
+
+ if (Ptr.isZero())
+ return false;
+
+ const Descriptor *DeclDesc = Ptr.getDeclDesc();
+ if (!DeclDesc)
+ return false;
+
+ const ASTContext &ASTCtx = S.getASTContext();
+
+ unsigned ByteOffset = 0;
+ unsigned FullSize = computeFullDescSize(ASTCtx, DeclDesc);
+
+ pushInteger(S, FullSize - ByteOffset, Call->getType());
+
+ return true;
+}
+
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
const CallExpr *Call, uint32_t BuiltinID) {
if (!S.getASTContext().BuiltinInfo.isConstantEvaluated(BuiltinID))
@@ -2681,6 +2727,12 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
return false;
break;
+ case Builtin::BI__builtin_object_size:
+ case Builtin::BI__builtin_dynamic_object_size:
+ if (!interp__builtin_object_size(S, OpPC, Frame, F, Call))
+ return false;
+ break;
+
default:
S.FFDiag(S.Current->getLocation(OpPC),
diag::note_invalid_subexpr_in_const_expr)
diff --git a/clang/test/AST/ByteCode/builtin-object-size.cpp b/clang/test/AST/ByteCode/builtin-object-size.cpp
new file mode 100644
index 0000000000000..62bb1642c5301
--- /dev/null
+++ b/clang/test/AST/ByteCode/builtin-object-size.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected %s
+// RUN: %clang_cc1 -verify=both,ref %s
+
+// both-no-diagnostics
+
+int a;
+static_assert(__builtin_object_size(&a, 0) == sizeof(int), "");
+float f;
+static_assert(__builtin_object_size(&f, 0) == sizeof(float), "");
+int arr[2];
+static_assert(__builtin_object_size(&arr, 0) == (sizeof(int)*2), "");
+
+float arrf[2];
+static_assert(__builtin_object_size(&arrf, 0) == (sizeof(float)*2), "");
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/13415 Here is the relevant piece of the build log for the reference
|
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
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.