-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Implement __builtin_object_size and __builtin_dynamic_object_size #166191
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30c9a5c
[CIR] Implement __builtin_object_size and __builtin_dynamic_object_size
mmha 4d4fe2a
clang-format, comment updates
mmha 2dc7585
more clang-format
mmha 6627147
Apply suggestions from code review
mmha bdf053c
Add nullunknown attribute
mmha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -459,6 +459,19 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, | |
| return emitCall(e->getCallee()->getType(), CIRGenCallee::forDirect(fnOp), e, | ||
| returnValue); | ||
| } | ||
| case Builtin::BI__builtin_dynamic_object_size: | ||
| case Builtin::BI__builtin_object_size: { | ||
| unsigned type = | ||
| e->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue(); | ||
| auto resType = mlir::cast<cir::IntType>(convertType(e->getType())); | ||
|
|
||
| // We pass this builtin onto the optimizer so that it can figure out the | ||
| // object size in more complex cases. | ||
| bool isDynamic = builtinID == Builtin::BI__builtin_dynamic_object_size; | ||
| return RValue::get(emitBuiltinObjectSize(e->getArg(0), type, resType, | ||
| /*EmittedE=*/nullptr, isDynamic)); | ||
| } | ||
|
|
||
| case Builtin::BI__builtin_prefetch: { | ||
| auto evaluateOperandAsInt = [&](const Expr *arg) { | ||
| Expr::EvalResult res; | ||
|
|
@@ -641,3 +654,42 @@ mlir::Value CIRGenFunction::emitVAArg(VAArgExpr *ve) { | |
| mlir::Value vaList = emitVAListRef(ve->getSubExpr()).getPointer(); | ||
| return cir::VAArgOp::create(builder, loc, type, vaList); | ||
| } | ||
|
|
||
| mlir::Value CIRGenFunction::emitBuiltinObjectSize(const Expr *e, unsigned type, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment explaining what |
||
| cir::IntType resType, | ||
| mlir::Value emittedE, | ||
| bool isDynamic) { | ||
| assert(!cir::MissingFeatures::opCallImplicitObjectSizeArgs()); | ||
|
|
||
| // LLVM can't handle type=3 appropriately, and __builtin_object_size shouldn't | ||
| // evaluate e for side-effects. In either case, just like original LLVM | ||
| // lowering, we shouldn't lower to `cir.objsize` but to a constant instead. | ||
| if (type == 3 || (!emittedE && e->HasSideEffects(getContext()))) | ||
| return builder.getConstInt(getLoc(e->getSourceRange()), resType, | ||
| (type & 2) ? 0 : -1); | ||
|
|
||
| mlir::Value ptr = emittedE ? emittedE : emitScalarExpr(e); | ||
| assert(mlir::isa<cir::PointerType>(ptr.getType()) && | ||
| "Non-pointer passed to __builtin_object_size?"); | ||
|
|
||
| assert(!cir::MissingFeatures::countedBySize()); | ||
|
|
||
| // Extract the min/max mode from type. CIR only supports type 0 | ||
| // (max, whole object) and type 2 (min, whole object), not type 1 or 3 | ||
| // (closest subobject variants). | ||
| const bool min = ((type & 2) != 0); | ||
| // For GCC compatibility, __builtin_object_size treats NULL as unknown size. | ||
| auto op = | ||
| cir::ObjSizeOp::create(builder, getLoc(e->getSourceRange()), resType, ptr, | ||
| min, /*nullUnknown=*/true, isDynamic); | ||
| return op.getResult(); | ||
| } | ||
|
|
||
| mlir::Value CIRGenFunction::evaluateOrEmitBuiltinObjectSize( | ||
| const Expr *e, unsigned type, cir::IntType resType, mlir::Value emittedE, | ||
| bool isDynamic) { | ||
| uint64_t objectSize; | ||
| if (!e->tryEvaluateObjectSize(objectSize, getContext(), type)) | ||
| return emitBuiltinObjectSize(e, type, resType, emittedE, isDynamic); | ||
| return builder.getConstInt(getLoc(e->getSourceRange()), resType, objectSize); | ||
mmha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we have an attribute corresponding to the
nullunknownargument tollvm.objectsize?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinions here, but since clang hardcodes this to
trueI don't see the need to expose this parameter. If we need it in the future it's easy to add it as an optional attribute later onUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clang hard codes it to
truewhen handling the builtin, but there's another place (EmitTypeCheck) where it sets it tofalse.But you're right that we can add it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I didn't spot that. This is just for the sanitizer and it will take us some time until this gets relevant, but I added that attribute now regardless.