-
Notifications
You must be signed in to change notification settings - Fork 7
[memory-analysis] Added a field to CPPMethod to store memory-ownershi… #35
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
keremsahn
wants to merge
1
commit into
compiler-research:main
Choose a base branch
from
keremsahn:bool-is-alloc
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
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
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,9 @@ | ||
| Name: MemOwnrship | ||
| Functions: | ||
| - Name: memOwnAllocGlobal | ||
| SwiftReturnOwnership: cppAllocNew | ||
| Tags: | ||
| - Name: memOwn | ||
| Methods: | ||
| - Name: memOwnAllocator | ||
| SwiftReturnOwnership: cppAllocNew |
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,10 @@ | ||
| #ifndef MEMORY_ANALYSIS_REDECL_H | ||
| #define MEMORY_ANALYSIS_REDECL_H | ||
| #include "../memory_analysis.h" | ||
|
|
||
| namespace memory { | ||
| [[clang::annotate("cppAllocNew")]] | ||
| memOwn* allocDefaultMemOwn(); | ||
| } | ||
|
|
||
| #endif |
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 @@ | ||
| module MemOwnrship { header "../memory_analysis.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,24 @@ | ||
| #include "memory_analysis.h" | ||
|
|
||
| namespace memory { | ||
|
|
||
| __attribute__((malloc)) memAnalysisKlass* mallocAttr() { | ||
| return new memAnalysisKlass; | ||
| } | ||
|
|
||
| __attribute__((ownership_returns(malloc))) memAnalysisKlass* | ||
| ownershipReturnsAttr() { | ||
| return new memAnalysisKlass; | ||
| } | ||
|
|
||
| // Expected to not return ownership when analysis is off, and there is just | ||
| // attr-check | ||
| memAnalysisKlass* noAttr() { return new memAnalysisKlass; } | ||
|
|
||
| memOwn* memOwnAllocGlobal() { return (memOwn*)malloc(sizeof(memOwn)); } | ||
|
|
||
| memOwn* allocDefaultMemOwn() { return new memOwn; } | ||
|
|
||
| memOwn* noAttrAlloc() { return new memOwn; } | ||
|
|
||
| } // namespace memory |
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 MEMORY_ANALYSIS_H | ||
| #define MEMORY_ANALYSIS_H | ||
|
|
||
| #include <new> | ||
| #include <stdlib.h> | ||
| namespace memory { | ||
|
|
||
| class memAnalysisKlass { | ||
| public: | ||
| int val; | ||
| }; | ||
| __attribute__((malloc)) memAnalysisKlass* mallocAttr(); | ||
| __attribute__((ownership_returns(malloc))) memAnalysisKlass* | ||
| ownershipReturnsAttr(); | ||
| memAnalysisKlass* noAttr(); | ||
|
|
||
| struct memOwn { | ||
| int val; | ||
| memOwn(int value) : val(value) {} | ||
| memOwn() { val = 0; } | ||
| // Attribute injected by APINotes | ||
| static memOwn* memOwnAllocator(int x) { return new memOwn(x); } | ||
| }; | ||
|
|
||
| // Attribute injected by APINotes | ||
| memOwn* memOwnAllocGlobal(); | ||
|
|
||
| // Attribute injected by redeclaration | ||
| memOwn* allocDefaultMemOwn(); | ||
|
|
||
| // No ownership attribute anywhere | ||
| memOwn* noAttrAlloc(); | ||
|
|
||
| } // namespace memory | ||
|
|
||
| #endif // MEMORY_ANALYSIS_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,102 @@ | ||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import py | ||
| from pytest import mark | ||
| from support import IS_CLING, setup_make | ||
|
|
||
| currpath = py.path.local(__file__).dirpath() | ||
| test_dct = str(currpath.join("cpp/memory_analysisDict")) | ||
|
|
||
| FLAGS = "-fmodules -fimplicit-module-maps -fapinotes-modules" | ||
| IN_CHILD = "-fapinotes-modules" in os.getenv("CPPINTEROP_EXTRA_INTERPRETER_ARGS", "") | ||
|
|
||
|
|
||
| def setup_module(mod): | ||
| setup_make("memory_analysis") | ||
|
|
||
|
|
||
| @mark.skipif( | ||
| IN_CHILD or IS_CLING, | ||
| reason="Cling asserts in collectModuleMaps when built with " + FLAGS, | ||
| ) | ||
| def test00_driver(): | ||
| env = os.environ.copy() | ||
| env["CPPINTEROP_EXTRA_INTERPRETER_ARGS"] = ( | ||
| env.get("CPPINTEROP_EXTRA_INTERPRETER_ARGS", "") + " " + FLAGS | ||
| ) | ||
| subprocess.check_call([sys.executable, "-m", "pytest", __file__], env=env) | ||
|
|
||
|
|
||
| class TestMEMORYANALYSIS: | ||
| def setup_class(cls): | ||
| cls.test_dct = test_dct | ||
| import cppjit | ||
|
|
||
| cppjit.add_include_path(str(currpath.join("cpp", "MemoryOwnership"))) | ||
| cppjit.include("../memory_analysis.h") | ||
| cppjit.include("memory_analysis_redecl.h") | ||
| cls.memory_analysis = cppjit.load_library(cls.test_dct + ".so") | ||
|
|
||
| def test01_malloc_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.mallocAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test02_ownership_returns_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.ownershipReturnsAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert obj.__python_owns__ | ||
|
Collaborator
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. We also need tests for assert not obj.__python__owns__ |
||
|
|
||
| def test03_no_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.noAttr() | ||
| assert type(obj) == cppjit.gbl.memory.memAnalysisKlass | ||
| assert not obj.__python_owns__ | ||
| obj.__python_owns__ = True | ||
|
|
||
| def test04_redecl_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.allocDefaultMemOwn() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test05_redecl_no_attr(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.noAttrAlloc() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert not obj.__python_owns__ | ||
| obj.__python_owns__ = True | ||
|
|
||
|
|
||
| @mark.skipif(not IN_CHILD, reason="needs " + FLAGS) | ||
| class TestMEMORYANALYSIS_APINOTES: | ||
| def setup_class(cls): | ||
| cls.test_dct = test_dct | ||
| import cppjit | ||
|
|
||
| cppjit.add_include_path(str(currpath.join("cpp", "MemoryOwnership"))) | ||
| cppjit.include("../memory_analysis.h") | ||
| cls.memory_analysis = cppjit.load_library(cls.test_dct + ".so") | ||
|
|
||
| def test01_apinotes_attr_method(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.memOwn.memOwnAllocator(5) | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
|
|
||
| def test02_apinotes_attr_func(self): | ||
| import cppjit | ||
|
|
||
| obj = cppjit.gbl.memory.memOwnAllocGlobal() | ||
| assert type(obj) == cppjit.gbl.memory.memOwn | ||
| assert obj.__python_owns__ | ||
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.
?
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.
Only class does not override GetAllocBehaviour is TPythonCallBack, and from my understanding this class is for some sort of python function, not C/C++, therefore I thought it would make sense to return None