-
Notifications
You must be signed in to change notification settings - Fork 200
Route memory-builtin recognition through AllocCallInfo. NFC #1935
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
+70
−23
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -237,22 +237,78 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| return atomicAddCall; | ||
| } | ||
|
|
||
| namespace { | ||
| // Recognises a C heap-memory builtin call and centralises the invariants | ||
| // reverse mode must preserve for it, so all memory-op reasoning goes through | ||
| // one place instead of ad-hoc name checks scattered across the visitor. | ||
| class AllocCallInfo { | ||
| public: | ||
| enum class Kind { None, Malloc, Calloc, Realloc, Free }; | ||
|
|
||
| AllocCallInfo() = default; | ||
|
|
||
| // Recognise E as a memory builtin; Kind::None if it is not one. Strips the | ||
| // C-style cast that wraps the call (e.g. `(double*)realloc(...)`), so | ||
| // IgnoreParenCasts, not IgnoreParenImpCasts, is required here. | ||
| [[nodiscard]] static AllocCallInfo recognize(clang::Expr* E) { | ||
| auto* CE = llvm::dyn_cast_or_null<clang::CallExpr>( | ||
| E ? E->IgnoreParenCasts() : nullptr); | ||
| if (!CE) | ||
| return {}; | ||
| const clang::FunctionDecl* FD = CE->getDirectCallee(); | ||
| // getName() asserts on non-identifier names (operators, constructors), | ||
| // which vector/STL code produces; the builtins are plain identifiers. | ||
| if (!FD || !FD->getDeclName().isIdentifier()) | ||
| return {}; | ||
| Kind k = llvm::StringSwitch<Kind>(FD->getName()) | ||
|
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. warning: no header providing "llvm::StringSwitch" is directly included [misc-include-cleaner] lib/Differentiator/ReverseModeVisitor.cpp:68: - #include <memory>
+ #include <llvm/ADT/StringSwitch.h>
+ #include <memory> |
||
| .Case("malloc", Kind::Malloc) | ||
| .Case("calloc", Kind::Calloc) | ||
| .Case("realloc", Kind::Realloc) | ||
| .Case("free", Kind::Free) | ||
| .Default(Kind::None); | ||
| return AllocCallInfo(k, CE); | ||
| } | ||
|
|
||
| // The number-of-bytes operand that a following memset must zero: | ||
| // malloc(n) -> n, realloc(p, n) -> n. calloc self-zeroes and needs no | ||
| // memset, so it (and free/none) report null here. | ||
| [[nodiscard]] clang::Expr* memsetByteSize() const { | ||
| switch (m_Kind) { | ||
| case Kind::Malloc: | ||
| return m_Call->getArg(0); | ||
| case Kind::Realloc: | ||
| return m_Call->getArg(1); | ||
| default: | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| // True for an in-place `p = realloc(p, n)`: the LHS is realloc's own | ||
| // pointer argument. Only then may the reallocated pointer be kept across | ||
| // the call (realloc frees the old block, so a saved pointer would dangle). | ||
| [[nodiscard]] bool isInPlaceRealloc(const clang::Expr* LHS) const { | ||
| if (m_Kind != Kind::Realloc || m_Call->getNumArgs() == 0) | ||
| return false; | ||
| const auto* LDRE = | ||
| llvm::dyn_cast<clang::DeclRefExpr>(LHS->IgnoreParenCasts()); | ||
| const auto* ArgDRE = llvm::dyn_cast<clang::DeclRefExpr>( | ||
| m_Call->getArg(0)->IgnoreParenCasts()); | ||
| return LDRE && ArgDRE && LDRE->getDecl() == ArgDRE->getDecl(); | ||
| } | ||
|
|
||
| private: | ||
| AllocCallInfo(Kind k, clang::CallExpr* c) : m_Kind(k), m_Call(c) {} | ||
| Kind m_Kind = Kind::None; | ||
| clang::CallExpr* m_Call = nullptr; | ||
| }; | ||
| } // namespace | ||
|
|
||
| // Both LHS (the memset destination) and the size expression are cloned here: | ||
| // callers pass the derived pointer they also assign to and the malloc/realloc | ||
| // size they also pass to that call, so cloning keeps the memset a distinct | ||
| // subtree. | ||
| Expr* ReverseModeVisitor::CheckAndBuildCallToMemset(Expr* LHS, Expr* RHS) { | ||
| Expr* size = nullptr; | ||
| if (auto* callExpr = dyn_cast_or_null<CallExpr>(RHS)) | ||
| if (auto* declRef = | ||
| dyn_cast<DeclRefExpr>(callExpr->getCallee()->IgnoreImpCasts())) | ||
| if (auto* FD = dyn_cast<FunctionDecl>(declRef->getDecl())) { | ||
| if (FD->getNameAsString() == "malloc") | ||
| size = callExpr->getArg(0); | ||
| else if (FD->getNameAsString() == "realloc") | ||
| size = callExpr->getArg(1); | ||
| } | ||
|
|
||
| Expr* size = AllocCallInfo::recognize(RHS).memsetByteSize(); | ||
| if (size) { | ||
| llvm::SmallVector<Expr*, 3> args = { | ||
| CloneNode(LHS), getZeroInit(m_Context.IntTy), CloneNode(size)}; | ||
|
|
@@ -3174,18 +3230,9 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) { | |
| // in-place realloc is not handled -- its reverse sweep may read the | ||
| // freed tail out of bounds, as it did before this change; no test | ||
| // exercises it. | ||
| bool isReallocAssignment = false; | ||
| if (opCode == BO_Assign) | ||
| if (auto* RCall = dyn_cast<CallExpr>(R->IgnoreParenCasts())) | ||
| if (const FunctionDecl* RFD = RCall->getDirectCallee()) | ||
| if (RFD->getNameAsString() == "realloc" && RCall->getNumArgs()) { | ||
| // Only in-place: the LHS must be realloc's own pointer argument. | ||
| const auto* LDRE = dyn_cast<DeclRefExpr>(L->IgnoreParenCasts()); | ||
| const auto* ArgDRE = | ||
| dyn_cast<DeclRefExpr>(RCall->getArg(0)->IgnoreParenCasts()); | ||
| isReallocAssignment = | ||
| LDRE && ArgDRE && LDRE->getDecl() == ArgDRE->getDecl(); | ||
| } | ||
| bool isReallocAssignment = | ||
| opCode == BO_Assign && | ||
| AllocCallInfo::recognize(R).isInPlaceRealloc(L); | ||
|
|
||
| // Store the value of the LHS of the assignment in the forward pass | ||
| // and restore it in the reverse pass | ||
|
|
||
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.
warning: enum 'Kind' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size [performance-enum-size]