Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
3404d138
Commit
3404d138
authored
Feb 17, 2016
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handling some more of llvm refcounting
parent
b92d42d8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
43 deletions
+108
-43
src/codegen/irgen/refcounts.cpp
src/codegen/irgen/refcounts.cpp
+108
-43
No files found.
src/codegen/irgen/refcounts.cpp
View file @
3404d138
...
@@ -23,6 +23,7 @@
...
@@ -23,6 +23,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "codegen/codegen.h"
#include "codegen/codegen.h"
#include "codegen/gcbuilder.h"
#include "codegen/gcbuilder.h"
...
@@ -58,6 +59,90 @@ void RefcountTracker::refConsumed(llvm::Value* v, llvm::Instruction* inst) {
...
@@ -58,6 +59,90 @@ void RefcountTracker::refConsumed(llvm::Value* v, llvm::Instruction* inst) {
assert
(
std
::
find
(
inst
->
op_begin
(),
inst
->
op_end
(),
v
)
!=
inst
->
op_end
());
assert
(
std
::
find
(
inst
->
op_begin
(),
inst
->
op_end
(),
v
)
!=
inst
->
op_end
());
}
}
llvm
::
Instruction
*
findIncrefPt
(
llvm
::
BasicBlock
*
BB
)
{
llvm
::
Instruction
*
incref_pt
;
// = BB->getFirstInsertionPt();
if
(
llvm
::
isa
<
llvm
::
LandingPadInst
>
(
*
BB
->
begin
()))
{
// Don't split up the landingpad+extract+cxa_begin_catch
auto
it
=
BB
->
begin
();
++
it
;
++
it
;
++
it
;
incref_pt
=
&*
it
;
}
else
{
incref_pt
=
BB
->
getFirstInsertionPt
();
}
return
incref_pt
;
}
void
addIncrefs
(
llvm
::
Value
*
v
,
int
num_refs
,
llvm
::
Instruction
*
incref_pt
)
{
assert
(
num_refs
>
0
);
#ifdef Py_REF_DEBUG
auto
reftotal_gv
=
g
.
cur_module
->
getOrInsertGlobal
(
"_Py_RefTotal"
,
g
.
i64
);
auto
reftotal
=
new
llvm
::
LoadInst
(
reftotal_gv
,
""
,
incref_pt
);
auto
new_reftotal
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Add
,
reftotal
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
incref_pt
);
new
llvm
::
StoreInst
(
new_reftotal
,
reftotal_gv
,
incref_pt
);
#endif
llvm
::
ArrayRef
<
llvm
::
Value
*>
idxs
({
getConstantInt
(
0
,
g
.
i32
),
getConstantInt
(
0
,
g
.
i32
)
});
auto
refcount_ptr
=
llvm
::
GetElementPtrInst
::
CreateInBounds
(
v
,
idxs
,
""
,
incref_pt
);
auto
refcount
=
new
llvm
::
LoadInst
(
refcount_ptr
,
""
,
incref_pt
);
auto
new_refcount
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Add
,
refcount
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
incref_pt
);
new
llvm
::
StoreInst
(
new_refcount
,
refcount_ptr
,
incref_pt
);
}
void
addDecrefs
(
llvm
::
Value
*
v
,
int
num_refs
,
llvm
::
Instruction
*
decref_pt
)
{
assert
(
num_refs
>
0
);
llvm
::
IRBuilder
<
true
>
builder
(
decref_pt
);
#ifdef Py_REF_DEBUG
auto
reftotal_gv
=
g
.
cur_module
->
getOrInsertGlobal
(
"_Py_RefTotal"
,
g
.
i64
);
auto
reftotal
=
new
llvm
::
LoadInst
(
reftotal_gv
,
""
,
decref_pt
);
auto
new_reftotal
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Sub
,
reftotal
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
decref_pt
);
new
llvm
::
StoreInst
(
new_reftotal
,
reftotal_gv
,
decref_pt
);
#endif
llvm
::
ArrayRef
<
llvm
::
Value
*>
idxs
({
getConstantInt
(
0
,
g
.
i32
),
getConstantInt
(
0
,
g
.
i32
)
});
auto
refcount_ptr
=
llvm
::
GetElementPtrInst
::
CreateInBounds
(
v
,
idxs
,
""
,
decref_pt
);
auto
refcount
=
new
llvm
::
LoadInst
(
refcount_ptr
,
""
,
decref_pt
);
auto
new_refcount
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Sub
,
refcount
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
decref_pt
);
new
llvm
::
StoreInst
(
new_refcount
,
refcount_ptr
,
decref_pt
);
llvm
::
BasicBlock
*
cur_block
=
decref_pt
->
getParent
();
llvm
::
BasicBlock
*
dealloc_block
=
llvm
::
BasicBlock
::
Create
(
g
.
context
,
""
,
decref_pt
->
getParent
()
->
getParent
());
llvm
::
BasicBlock
*
continue_block
=
cur_block
->
splitBasicBlock
(
decref_pt
);
assert
(
llvm
::
isa
<
llvm
::
BranchInst
>
(
cur_block
->
getTerminator
()));
cur_block
->
getTerminator
()
->
eraseFromParent
();
builder
.
SetInsertPoint
(
cur_block
);
auto
iszero
=
builder
.
CreateICmpEQ
(
new_refcount
,
getConstantInt
(
0
,
g
.
i64
));
builder
.
CreateCondBr
(
iszero
,
dealloc_block
,
continue_block
);
builder
.
SetInsertPoint
(
dealloc_block
);
auto
cls_ptr
=
builder
.
CreateConstInBoundsGEP2_32
(
v
,
0
,
1
);
auto
cls
=
builder
.
CreateLoad
(
cls_ptr
);
auto
dtor_ptr
=
builder
.
CreateConstInBoundsGEP2_32
(
cls
,
0
,
4
);
#ifndef NDEBUG
llvm
::
APInt
offset
(
64
,
0
,
true
);
assert
(
llvm
::
cast
<
llvm
::
GetElementPtrInst
>
(
dtor_ptr
)
->
accumulateConstantOffset
(
*
g
.
tm
->
getDataLayout
(),
offset
));
assert
(
offset
.
getZExtValue
()
==
offsetof
(
BoxedClass
,
tp_dealloc
));
#endif
auto
dtor
=
builder
.
CreateLoad
(
dtor_ptr
);
builder
.
CreateCall
(
dtor
,
v
);
builder
.
CreateBr
(
continue_block
);
builder
.
SetInsertPoint
(
continue_block
);
}
void
RefcountTracker
::
addRefcounts
(
IRGenState
*
irstate
)
{
void
RefcountTracker
::
addRefcounts
(
IRGenState
*
irstate
)
{
llvm
::
Function
*
f
=
irstate
->
getLLVMFunction
();
llvm
::
Function
*
f
=
irstate
->
getLLVMFunction
();
RefcountTracker
*
rt
=
irstate
->
getRefcounts
();
RefcountTracker
*
rt
=
irstate
->
getRefcounts
();
...
@@ -160,37 +245,6 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -160,37 +245,6 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
}
}
}
}
auto
addRefcountsToBlock
=
[](
llvm
::
BasicBlock
*
BB
,
llvm
::
Value
*
v
,
int
num_refs
)
{
llvm
::
Instruction
*
incref_pt
;
// = BB->getFirstInsertionPt();
if
(
llvm
::
isa
<
llvm
::
LandingPadInst
>
(
*
BB
->
begin
()))
{
// Don't split up the landingpad+extract+cxa_begin_catch
auto
it
=
BB
->
begin
();
++
it
;
++
it
;
++
it
;
incref_pt
=
&*
it
;
}
else
{
incref_pt
=
BB
->
getFirstInsertionPt
();
}
#ifdef Py_REF_DEBUG
auto
reftotal_gv
=
g
.
cur_module
->
getOrInsertGlobal
(
"_Py_RefTotal"
,
g
.
i64
);
auto
reftotal
=
new
llvm
::
LoadInst
(
reftotal_gv
,
""
,
incref_pt
);
auto
new_reftotal
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Add
,
reftotal
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
incref_pt
);
new
llvm
::
StoreInst
(
new_reftotal
,
reftotal_gv
,
""
,
incref_pt
);
#endif
llvm
::
ArrayRef
<
llvm
::
Value
*>
idxs
({
getConstantInt
(
0
,
g
.
i32
),
getConstantInt
(
0
,
g
.
i32
)
});
auto
refcount_ptr
=
llvm
::
GetElementPtrInst
::
CreateInBounds
(
v
,
idxs
,
""
,
incref_pt
);
auto
refcount
=
new
llvm
::
LoadInst
(
refcount_ptr
,
""
,
incref_pt
);
auto
new_refcount
=
llvm
::
BinaryOperator
::
Create
(
llvm
::
BinaryOperator
::
BinaryOps
::
Add
,
refcount
,
getConstantInt
(
num_refs
,
g
.
i64
),
""
,
incref_pt
);
new
llvm
::
StoreInst
(
new_refcount
,
refcount_ptr
,
""
,
incref_pt
);
};
while
(
!
block_queue
.
empty
())
{
while
(
!
block_queue
.
empty
())
{
llvm
::
BasicBlock
&
BB
=
*
block_queue
.
front
();
llvm
::
BasicBlock
&
BB
=
*
block_queue
.
front
();
block_queue
.
pop_front
();
block_queue
.
pop_front
();
...
@@ -241,13 +295,16 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -241,13 +295,16 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
this_refs
=
it
->
second
;
this_refs
=
it
->
second
;
}
}
if
(
this_refs
>
min_refs
)
{
if
(
this_refs
>
min_refs
)
{
add
RefcountsToBlock
(
SBB
,
v
,
this_refs
-
min_refs
);
add
Increfs
(
v
,
this_refs
-
min_refs
,
findIncrefPt
(
SBB
)
);
//llvm::outs() << *incref_pt << '\n';
//llvm::outs() << *incref_pt << '\n';
//llvm::outs() << "Need to incref " << *v << " at beginning of " << SBB->getName() << "\n";
//llvm::outs() << "Need to incref " << *v << " at beginning of " << SBB->getName() << "\n";
}
}
}
}
state
.
refs
[
v
]
=
min_refs
;
if
(
min_refs
)
state
.
refs
[
v
]
=
min_refs
;
else
assert
(
state
.
refs
.
count
(
v
)
==
0
);
}
else
{
}
else
{
assert
(
0
&&
"finish implementing"
);
assert
(
0
&&
"finish implementing"
);
}
}
...
@@ -287,7 +344,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -287,7 +344,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
if
(
num_times_as_op
[
op
]
>
num_consumed
)
{
if
(
num_times_as_op
[
op
]
>
num_consumed
)
{
if
(
rt
->
vars
[
op
].
reftype
==
RefType
::
BORROWED
)
{
if
(
rt
->
vars
[
op
].
reftype
==
RefType
::
BORROWED
)
{
}
else
{
}
else
{
assert
(
0
&&
"handle this case
"
);
assert
(
state
.
refs
.
count
(
op
)
&&
"handle this case (last reference of an owned var)
"
);
}
}
}
}
...
@@ -297,14 +354,21 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -297,14 +354,21 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
}
}
// Handle variables that were defined in this BB:
// Handle variables that were defined in this BB:
for
(
auto
it
=
state
.
refs
.
begin
(),
end
=
state
.
refs
.
end
();
it
!=
end
;
)
{
for
(
auto
&&
p
:
rt
->
vars
)
{
llvm
::
Instruction
*
inst
=
llvm
::
dyn_cast
<
llvm
::
Instruction
>
(
it
->
first
);
llvm
::
Instruction
*
inst
=
llvm
::
dyn_cast
<
llvm
::
Instruction
>
(
p
.
first
);
if
(
inst
&&
inst
->
getParent
()
==
&
BB
)
{
if
(
inst
&&
inst
->
getParent
()
==
&
BB
)
{
assert
(
it
->
second
==
1
&&
rt
->
vars
[
it
->
first
].
reftype
==
RefType
::
OWNED
);
int
starting_refs
=
(
p
.
second
.
reftype
==
RefType
::
OWNED
?
1
:
0
);
state
.
refs
.
erase
(
it
);
if
(
state
.
refs
[
inst
]
!=
starting_refs
)
{
++
it
;
llvm
::
Instruction
*
insertion_pt
=
inst
->
getNextNode
();
}
else
{
assert
(
insertion_pt
);
++
it
;
if
(
state
.
refs
[
inst
]
<
starting_refs
)
{
assert
(
p
.
second
.
reftype
==
RefType
::
OWNED
);
addDecrefs
(
inst
,
starting_refs
-
state
.
refs
[
inst
],
insertion_pt
);
}
else
{
addIncrefs
(
inst
,
state
.
refs
[
inst
]
-
starting_refs
,
insertion_pt
);
}
}
state
.
refs
.
erase
(
inst
);
}
}
}
}
...
@@ -313,7 +377,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -313,7 +377,7 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
llvm
::
outs
()
<<
*
p
.
first
<<
" "
<<
p
.
second
<<
'\n'
;
llvm
::
outs
()
<<
*
p
.
first
<<
" "
<<
p
.
second
<<
'\n'
;
assert
(
llvm
::
isa
<
llvm
::
GlobalVariable
>
(
p
.
first
));
assert
(
llvm
::
isa
<
llvm
::
GlobalVariable
>
(
p
.
first
));
assert
(
rt
->
vars
[
p
.
first
].
reftype
==
RefType
::
BORROWED
);
assert
(
rt
->
vars
[
p
.
first
].
reftype
==
RefType
::
BORROWED
);
add
RefcountsToBlock
(
&
BB
,
p
.
first
,
p
.
second
);
add
Increfs
(
p
.
first
,
p
.
second
,
findIncrefPt
(
&
BB
)
);
}
}
state
.
refs
.
clear
();
state
.
refs
.
clear
();
}
}
...
@@ -342,7 +406,8 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
...
@@ -342,7 +406,8 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
fprintf
(
stderr
,
"After refcounts:
\n
"
);
fprintf
(
stderr
,
"After refcounts:
\n
"
);
fprintf
(
stderr
,
"
\033
[35m"
);
fprintf
(
stderr
,
"
\033
[35m"
);
dumpPrettyIR
(
f
);
f
->
dump
();
//dumpPrettyIR(f);
fprintf
(
stderr
,
"
\033
[0m"
);
fprintf
(
stderr
,
"
\033
[0m"
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment