-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Draft] Summary Based Analysis Prototype #144224
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
Draft
isuckatcs
wants to merge
49
commits into
llvm:main
Choose a base branch
from
isuckatcs:summaries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
7fba0ad
[clang][Driver] Add option to emit summaries
isuckatcs 3d7d23a
[clang][Summary] add the summarizer skeleton
isuckatcs 2592f08
[clang][Summary] implement summary base prototype
isuckatcs 43c1b90
[clang][Summary] implement summary inference prototype
isuckatcs f1ea491
[clang][Summary] summary printing prototype
isuckatcs 6fb6404
[clang][Driver][Summary] add a flag to specify the directory to parse…
isuckatcs a45d2c0
[clang][Summary] implement parsing the summaries
isuckatcs e6b2107
[clang][Summary] implement reduction prototype
isuckatcs da80bdb
[clang][Summary] make `SummaryManager` own the attributes
isuckatcs 4964c96
[clang][Summary] only keep one constructor for `FunctionSummary`
isuckatcs d8c26d3
[clang][Summary] the summary manager shouldn't be reading the JSON file
isuckatcs b5465c1
[clang][Summary] the summary manager shouldn't contain the JSON summa…
isuckatcs 1754b30
[clang][Summary] rename description to attr
isuckatcs 7e27c32
[clang][Summary] more flexible merge logic
isuckatcs 4f8acb9
[clang][Summary] refactor summaries in the compiler instance and sema
isuckatcs d635e32
[clang][Summary] give the summary consumer a default value to keep th…
isuckatcs 757c0d6
[clang][Summary] change frontend action and summary interaction
isuckatcs f511c22
[clang][Summary] explicitly flush summary
isuckatcs 6964f2c
[clang][analyzer][Summary] pass summaries to the analyzer
isuckatcs 81b039f
[analyzer] don't invalidate global regions if a function doesn't writ…
isuckatcs 016dcdb
[clang][Summary] refactor summary attributes
isuckatcs 1155844
[Summary] move the ast matcher callback out of the attribute declaration
isuckatcs 49937a6
[clang] don't crash if there is no summary consumer
isuckatcs 2499ff2
[Driver][Summary] implement emitting summary next to the object file
isuckatcs d44d9d0
[Summary] move summary related logic into a separate library
isuckatcs 9e94174
link clangSummary against clangIndex
isuckatcs 41c286b
format
isuckatcs 103956b
make the summary context in the expression engine nullptr by default
isuckatcs 8a5a967
initialize SummaryCtx to nullptr in AnalysisConsumer
isuckatcs f71dd84
handle when call event doesn't have a decl
isuckatcs daccc50
[Summary] flag functions that call virtual funtions and non-functions
isuckatcs 0daa24f
fix crashes
isuckatcs e08fac7
a few experimental changes
isuckatcs 8739704
add new attribute that checks if a function modifies a pointer argument
isuckatcs 8dfc609
store string in function summary
isuckatcs da96155
yaml serialization... complete mess
isuckatcs 03fc645
flexible serailization
isuckatcs b125329
add support for summary format selection
isuckatcs cfd792c
format
isuckatcs a2b8e2d
some cleanup
isuckatcs d3923ce
don't generate summary format argument if not needed
isuckatcs 449adeb
emit summaries even if obj path has no base dir path
isuckatcs 8449340
binary serialization prototype
isuckatcs 767e3f9
unify summary layout accross the different formats
isuckatcs 0e88fa8
remove nowriteptrparameter
isuckatcs 5d72937
make file processing consistent
isuckatcs bbfa385
fix yamls parsing performance issues
isuckatcs cc3e507
format
isuckatcs 6451e5b
path
isuckatcs 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
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #ifndef LLVM_CLANG_SUMMARY_SUMMARYATTRIBUTE_H | ||
| #define LLVM_CLANG_SUMMARY_SUMMARYATTRIBUTE_H | ||
|
|
||
| #include "clang/AST/Decl.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
|
||
| namespace clang { | ||
| enum SummaryAttrKind { NO_WRITE_GLOBAL, NO_WRITE_PTR_PARAMETER }; | ||
|
|
||
| class FunctionSummary; | ||
| class SummaryContext; | ||
|
|
||
| class SummaryAttr { | ||
| const SummaryAttrKind Kind; | ||
| const char *Spelling; | ||
|
|
||
| protected: | ||
| SummaryAttr(SummaryAttrKind Kind, const char *Spelling) | ||
| : Kind(Kind), Spelling(Spelling) {}; | ||
|
|
||
| public: | ||
| virtual ~SummaryAttr() = default; | ||
|
|
||
| SummaryAttrKind getKind() const { return Kind; } | ||
| const char *getSpelling() const { return Spelling; } | ||
|
|
||
| virtual bool infer(const FunctionDecl *FD) const = 0; | ||
| virtual bool merge(const FunctionSummary &Caller, | ||
| const FunctionSummary *Callee) const = 0; | ||
|
|
||
| virtual std::string serialize() const { return std::string(Spelling); }; | ||
| virtual bool parse(std::string_view input) const { | ||
| return input == Spelling; | ||
| }; | ||
| }; | ||
|
|
||
| class NoWriteGlobalAttr : public SummaryAttr { | ||
| NoWriteGlobalAttr() : SummaryAttr(NO_WRITE_GLOBAL, "no_write_global") {} | ||
|
|
||
| public: | ||
| bool infer(const FunctionDecl *FD) const override final; | ||
| bool merge(const FunctionSummary &Caller, | ||
| const FunctionSummary *Callee) const override final; | ||
|
|
||
| static bool classof(const SummaryAttr *A) { | ||
| return A->getKind() == NO_WRITE_GLOBAL; | ||
| } | ||
| friend class SummaryContext; | ||
| }; | ||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_SUMMARY_SUMMARYATTRIBUTEH |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #ifndef LLVM_CLANG_SUMMARY_SUMMARYCONSUMER_H | ||
| #define LLVM_CLANG_SUMMARY_SUMMARYCONSUMER_H | ||
|
|
||
| #include "llvm/Support/raw_ostream.h" | ||
| namespace clang { | ||
| class FunctionSummary; | ||
| class SummaryContext; | ||
| class SummarySerializer; | ||
|
|
||
| class SummaryConsumer { | ||
| protected: | ||
| const SummaryContext *SummaryCtx; | ||
|
|
||
| public: | ||
| SummaryConsumer(const SummaryContext &SummaryCtx) : SummaryCtx(&SummaryCtx) {} | ||
| virtual ~SummaryConsumer() = default; | ||
|
|
||
| virtual void ProcessStartOfSourceFile() {}; | ||
| virtual void ProcessFunctionSummary(const FunctionSummary &) {}; | ||
| virtual void ProcessEndOfSourceFile() {}; | ||
| }; | ||
|
|
||
| class SerializingSummaryConsumer : public SummaryConsumer { | ||
| llvm::raw_ostream &OS; | ||
| SummarySerializer *Serializer; | ||
|
|
||
| public: | ||
| SerializingSummaryConsumer(SummarySerializer &Serializer, | ||
| llvm::raw_ostream &OS); | ||
|
|
||
| void ProcessEndOfSourceFile() override; | ||
| }; | ||
|
|
||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_SUMMARY_SUMMARYCONSUMER_H |
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #ifndef LLVM_CLANG_SUMMARY_SUMMARYCONTEXT_H | ||
| #define LLVM_CLANG_SUMMARY_SUMMARYCONTEXT_H | ||
|
|
||
| #include "clang/Summary/SummaryAttribute.h" | ||
| #include "clang/Summary/SummaryConsumer.h" | ||
| #include "llvm/Support/YAMLTraits.h" | ||
| #include <set> | ||
|
|
||
| namespace clang { | ||
| class FunctionSummary { | ||
| size_t ID; | ||
| std::set<const SummaryAttr *> Attrs; | ||
| std::set<size_t> Calls; | ||
| bool CallsOpaque; | ||
|
|
||
| public: | ||
| FunctionSummary(size_t ID, std::set<const SummaryAttr *> Attrs, | ||
| std::set<size_t> Calls, bool CallsOpaque); | ||
|
|
||
| size_t getID() const { return ID; } | ||
| const std::set<const SummaryAttr *> &getAttributes() const { return Attrs; } | ||
| const std::set<size_t> &getCalls() const { return Calls; } | ||
| bool callsOpaqueObject() const { return CallsOpaque; } | ||
|
|
||
| template <typename T> bool hasAttribute() const { | ||
| for (auto &&attr : Attrs) { | ||
| if (llvm::isa<T>(attr)) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| void replaceAttributes(std::set<const SummaryAttr *> Attrs) { | ||
| this->Attrs = std::move(Attrs); | ||
| } | ||
| }; | ||
|
|
||
| class SummaryContext { | ||
| std::map<std::string, size_t> IdentifierToID; | ||
| std::vector<StringRef> Identifiers; | ||
|
|
||
| std::map<size_t, const FunctionSummary *> IDToSummary; | ||
| std::vector<std::unique_ptr<FunctionSummary>> FunctionSummaries; | ||
|
|
||
| std::map<SummaryAttrKind, const SummaryAttr *> KindToAttribute; | ||
| std::vector<std::unique_ptr<SummaryAttr>> Attributes; | ||
|
|
||
| template <typename T> void RegisterAttr(); | ||
|
|
||
| public: | ||
| SummaryContext(); | ||
|
|
||
| size_t GetOrInsertStoredIdentifierIdx(StringRef ID); | ||
| std::optional<size_t> GetStoredIdentifierIdx(StringRef ID) const; | ||
|
|
||
| void CreateSummary(size_t ID, std::set<const SummaryAttr *> Attrs, | ||
| std::set<size_t> Calls, bool CallsOpaque); | ||
| bool ReduceFunctionSummary(FunctionSummary &FunctionSummary); | ||
|
|
||
| const std::vector<std::unique_ptr<FunctionSummary>> &GetSummaries() const { | ||
| return FunctionSummaries; | ||
| }; | ||
| const std::vector<std::unique_ptr<SummaryAttr>> &GetAttributes() const { | ||
| return Attributes; | ||
| }; | ||
| const std::vector<StringRef> &GetIdentifiers() const { return Identifiers; }; | ||
|
|
||
| const FunctionSummary *GetSummary(const FunctionDecl *FD) const; | ||
| void SummarizeFunctionBody(const FunctionDecl *FD); | ||
| void ReduceSummaries(); | ||
| }; | ||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_SUMMARY_SUMMARYCONTEXTH |
Oops, something went wrong.
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.
Should we assert here that we don't have a summary context yet?
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.
I'm not sure tbh. I just mirrored what other create functions such as
CompilerInstance::createSema()do. Those don't assert the existence, so maybe the consumers of the class expect this kind of behaviour.