Skip to content
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

Add more types to the returned allocation #19

Merged
merged 9 commits into from
Nov 6, 2023
Merged

Add more types to the returned allocation #19

merged 9 commits into from
Nov 6, 2023

Conversation

gbaraldi
Copy link
Member

This needs tests, but should bring the tpyes of allocations in 1.10/9 should be more correct.

@topolarity topolarity added this to the Public MVP milestone Oct 23, 2023
@topolarity topolarity linked an issue Oct 23, 2023 that may be closed by this pull request
@@ -19,6 +19,8 @@ const known_nonalloc_funcs = (
"jl_pop_handler", "ijl_pop_handler",
"jl_f_typeof", "ijl_f_typeof",
"jl_clock_now", "ijl_clock_now",
"jl_throw", "ijl_throw", #= the allocation of the error is separate =#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good test for this is to check that foo() = throw(EOFError()) is considered non-allocating

if isa(fn, LLVM.Function) && in(LLVM.name(fn), ("ijl_alloc_string", "jl_alloc_string"))
return String
end

if isa(fn, LLVM.Function) && in(LLVM.name(fn), ("ijl_copy_array", "jl_copy_array"))
return Array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way for us to know the type of the array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, grepping for this function in the runtime brings up nothing for me. Do you know what's up?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is typo of mine, it should be jl_array_copy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still needs fixing?

src/allocfunc.jl Outdated
continue
end
@label gep_handling
if convert(Int,operands(istag::LLVM.GetElementPtrInst)[2]) == -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if convert(Int,operands(istag::LLVM.GetElementPtrInst)[2]) == -1
offset = operands(istag::LLVM.GetElementPtrInst)[2]
if convert(Int, offset) == -1

Also, ideally this would also inspect the type of the GEP so that we know exactly what byte offset this is in memory (and the size of the store).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think this needs to check isa(offset, LLVM.ConstantInt) otherwise the convert will fail for anything with a runtime offset

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are lacking the accessor functions :(. I need to add some getSrcType/getDestType calls to LLVM.jl

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I don't think we do a -1 gep on anything except the tag.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it looks like LLVM.LLVMType(LLVM.API.LLVMGetGEPSourceElementType(gepinst))) is implemented

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, great we do wrap it. We don't have most of these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's only in LLVM 15 :(

src/allocfunc.jl Outdated
for gepuse in uses(istag)
isstore = user(gepuse)
if isa(isstore, LLVM.StoreInst)
return guess_julia_type(operands(isstore)[1], true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return guess_julia_type(operands(isstore)[1], true)
type_tag = operands(isstore)[1]
@assert type_tag isa LLVM.ConstantInt
return guess_julia_type(type_tag, true)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess_julia_type dispatches on the type, so this is a bit unnecessary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All asserts are unnecessary - this is to make explicit our assumptions on the IR.

Do we expect this to not be constant sometimes? If so, can guess_julia_type still give us a reasonable result?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it costs us nothing to do it.

if isa(istag, LLVM.BitCastInst)
for bituse in uses(istag)
isgep = user(bituse)
if isa(isgep, LLVM.GetElementPtrInst)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this logic is correct, since it will, e.g., ignore a bitcast whose second usage is the -1 GEP offset but whose first usage is a different GEP

It might be simpler to introduce something like transitive_uses(val; unwrap = (x)->isa(x, LLVM.BitcastInst))

Copy link
Member

@topolarity topolarity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me! Thanks for the iteration here @gbaraldi

Only last thought is that it'd be good to assert that we were able to find a type successfully, since it sounds like we expect these calls to always be lowered to LLVM with compile-time constant type-tag parameters.

test/runtests.jl Outdated Show resolved Hide resolved
@gbaraldi gbaraldi merged commit a8cbbba into main Nov 6, 2023
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allocation types are sometimes bad on Julia 1.10
2 participants