Skip to content

Commit

Permalink
transform (gc): track phi nodes in stack slots
Browse files Browse the repository at this point in the history
  • Loading branch information
niaow authored and aykevl committed Apr 2, 2020
1 parent 6647c43 commit ae16b2c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
18 changes: 16 additions & 2 deletions transform/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ func MakeGCStackSlots(mod llvm.Module) bool {
continue
}
switch ptr.InstructionOpcode() {
case llvm.PHI, llvm.GetElementPtr:
case llvm.GetElementPtr:
// These values do not create new values: the values already
// existed locally in this function so must have been tracked
// already.
continue
case llvm.PHI:
// While the value may have already been tracked, it may be overwritten in a loop.
// Therefore, a second copy must be created to ensure that it is tracked over the entirety of its lifetime.
case llvm.ExtractValue, llvm.BitCast:
// These instructions do not create new values, but their
// original value may not be tracked. So keep tracking them for
Expand Down Expand Up @@ -228,11 +231,22 @@ func MakeGCStackSlots(mod llvm.Module) bool {

// Do a store to the stack object after each new pointer that is created.
for i, ptr := range pointers {
builder.SetInsertPointBefore(llvm.NextInstruction(ptr))
// Insert the store after the pointer value is created.
insertionPoint := llvm.NextInstruction(ptr)
for !insertionPoint.IsAPHINode().IsNil() {
// PHI nodes are required to be at the start of the block.
// Insert after the last PHI node.
insertionPoint = llvm.NextInstruction(insertionPoint)
}
builder.SetInsertPointBefore(insertionPoint)

// Extract a pointer to the appropriate section of the stack object.
gep := builder.CreateGEP(stackObject, []llvm.Value{
llvm.ConstInt(ctx.Int32Type(), 0, false),
llvm.ConstInt(ctx.Int32Type(), uint64(2+len(allocas)+i), false),
}, "")

// Store the pointer into the stack slot.
builder.CreateStore(ptr, gep)
}

Expand Down
34 changes: 34 additions & 0 deletions transform/testdata/gc-stackslots.ll
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,37 @@ define i8* @noAllocatingFunction() {
call void @runtime.trackPointer(i8* %ptr)
ret i8* %ptr
}

define i8* @fibNext(i8* %x, i8* %y) {
%x.val = load i8, i8* %x
%y.val = load i8, i8* %y
%out.val = add i8 %x.val, %y.val
%out.alloc = call i8* @runtime.alloc(i32 1)
call void @runtime.trackPointer(i8* %out.alloc)
store i8 %out.val, i8* %out.alloc
ret i8* %out.alloc
}

define i8* @allocLoop() {
entry:
%entry.x = call i8* @runtime.alloc(i32 1)
call void @runtime.trackPointer(i8* %entry.x)
%entry.y = call i8* @runtime.alloc(i32 1)
call void @runtime.trackPointer(i8* %entry.y)
store i8 1, i8* %entry.y
br label %loop

loop:
%prev.y = phi i8* [ %entry.y, %entry ], [ %prev.x, %loop ]
%prev.x = phi i8* [ %entry.x, %entry ], [ %next.x, %loop ]
call void @runtime.trackPointer(i8* %prev.x)
call void @runtime.trackPointer(i8* %prev.y)
%next.x = call i8* @fibNext(i8* %prev.x, i8* %prev.y)
call void @runtime.trackPointer(i8* %next.x)
%next.x.val = load i8, i8* %next.x
%loop.done = icmp ult i8 40, %next.x.val
br i1 %loop.done, label %end, label %loop

end:
ret i8* %next.x
}
56 changes: 56 additions & 0 deletions transform/testdata/gc-stackslots.out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,59 @@ define i8* @noAllocatingFunction() {
%ptr = call i8* @getPointer()
ret i8* %ptr
}

define i8* @fibNext(i8* %x, i8* %y) {
%gc.stackobject = alloca { %runtime.stackChainObject*, i32, i8* }
store { %runtime.stackChainObject*, i32, i8* } { %runtime.stackChainObject* null, i32 1, i8* null }, { %runtime.stackChainObject*, i32, i8* }* %gc.stackobject
%1 = load %runtime.stackChainObject*, %runtime.stackChainObject** @runtime.stackChainStart
%2 = getelementptr { %runtime.stackChainObject*, i32, i8* }, { %runtime.stackChainObject*, i32, i8* }* %gc.stackobject, i32 0, i32 0
store %runtime.stackChainObject* %1, %runtime.stackChainObject** %2
%3 = bitcast { %runtime.stackChainObject*, i32, i8* }* %gc.stackobject to %runtime.stackChainObject*
store %runtime.stackChainObject* %3, %runtime.stackChainObject** @runtime.stackChainStart
%x.val = load i8, i8* %x
%y.val = load i8, i8* %y
%out.val = add i8 %x.val, %y.val
%out.alloc = call i8* @runtime.alloc(i32 1)
%4 = getelementptr { %runtime.stackChainObject*, i32, i8* }, { %runtime.stackChainObject*, i32, i8* }* %gc.stackobject, i32 0, i32 2
store i8* %out.alloc, i8** %4
store i8 %out.val, i8* %out.alloc
store %runtime.stackChainObject* %1, %runtime.stackChainObject** @runtime.stackChainStart
ret i8* %out.alloc
}

define i8* @allocLoop() {
entry:
%gc.stackobject = alloca { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }
store { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* } { %runtime.stackChainObject* null, i32 5, i8* null, i8* null, i8* null, i8* null, i8* null }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject
%0 = load %runtime.stackChainObject*, %runtime.stackChainObject** @runtime.stackChainStart
%1 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 0
store %runtime.stackChainObject* %0, %runtime.stackChainObject** %1
%2 = bitcast { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject to %runtime.stackChainObject*
store %runtime.stackChainObject* %2, %runtime.stackChainObject** @runtime.stackChainStart
%entry.x = call i8* @runtime.alloc(i32 1)
%3 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 2
store i8* %entry.x, i8** %3
%entry.y = call i8* @runtime.alloc(i32 1)
%4 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 3
store i8* %entry.y, i8** %4
store i8 1, i8* %entry.y
br label %loop

loop:
%prev.y = phi i8* [ %entry.y, %entry ], [ %prev.x, %loop ]
%prev.x = phi i8* [ %entry.x, %entry ], [ %next.x, %loop ]
%5 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 5
store i8* %prev.y, i8** %5
%6 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 4
store i8* %prev.x, i8** %6
%next.x = call i8* @fibNext(i8* %prev.x, i8* %prev.y)
%7 = getelementptr { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }, { %runtime.stackChainObject*, i32, i8*, i8*, i8*, i8*, i8* }* %gc.stackobject, i32 0, i32 6
store i8* %next.x, i8** %7
%next.x.val = load i8, i8* %next.x
%loop.done = icmp ult i8 40, %next.x.val
br i1 %loop.done, label %end, label %loop

end:
store %runtime.stackChainObject* %0, %runtime.stackChainObject** @runtime.stackChainStart
ret i8* %next.x
}

0 comments on commit ae16b2c

Please sign in to comment.