Skip to content

Commit

Permalink
Concrete type assertion (#1890)
Browse files Browse the repository at this point in the history
* Concrete type assertion

* fix

* fix

* fix
  • Loading branch information
wsmoses authored Sep 26, 2024
1 parent 519b693 commit a72efb1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
72 changes: 45 additions & 27 deletions src/absint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ function actual_size(@nospecialize(typ2))
end
end

@inline function first_non_ghost(@nospecialize(typ2))
fc = fieldcount(typ2)
for i in 1:fc
if i == fc
return (i, sizeof(typ2))
else
fo = fieldoffset(typ2, i+1)
if fo != 0
return (i, fo)
end
end
end
return (-1, 0)
end

function should_recurse(@nospecialize(typ2), arg_t, byref, dl)
sz = sizeof(dl, arg_t)
if byref != GPUCompiler.BITS_VALUE
@assert sz == sizeof(Int)
return false
else
if actual_size(typ2) != sz
return true
else
if Base.isconcretetype(typ2)
idx, sz2 = first_non_ghost(typ2)
if idx != -1
if sz2 == sz
return true
end
end
end
return false
end
end
end

function abs_typeof(
arg::LLVM.Value,
partial::Bool = false,
Expand Down Expand Up @@ -346,7 +383,7 @@ function abs_typeof(

if !error
legal, typ, byref = abs_typeof(larg)
if legal && (byref == GPUCompiler.MUT_REF || byref == GPUCompiler.BITS_REF)
if legal && (byref == GPUCompiler.MUT_REF || byref == GPUCompiler.BITS_REF) && Base.isconcretetype(typ)
@static if VERSION < v"1.11-"
if typ <: Array && Base.isconcretetype(typ)
T = eltype(typ)
Expand All @@ -359,31 +396,11 @@ function abs_typeof(
end
if byref == GPUCompiler.BITS_REF || byref == GPUCompiler.MUT_REF
dl = LLVM.datalayout(LLVM.parent(LLVM.parent(LLVM.parent(arg))))
function should_recurse(typ2, arg_t, byref)
sz = sizeof(dl, arg_t)
if byref != GPUCompiler.BITS_VALUE
@assert sz == sizeof(Int)
return false
else
if actual_size(typ2) != sz
return true
else
if Base.isconcretetype(typ2)
if fieldcount(typ2) > 0
if actual_size(fieldtype(typ2,1)) == sz
return true
end
end
end
return false
end
end
end

byref = GPUCompiler.BITS_VALUE
legal = true

while offset !== nothing && legal
while (offset !== nothing && offset != 0) && legal
@assert Base.isconcretetype(typ)
seen = false
lasti = 1
Expand All @@ -403,6 +420,7 @@ function abs_typeof(
elseif fieldoffset(typ, i) > offset
offset = offset - fieldoffset(typ, lasti)
typ = fieldtype(typ, lasti)
@assert Base.isconcretetype(typ)
if !Base.allocatedinline(typ)
legal = false
end
Expand All @@ -420,9 +438,10 @@ function abs_typeof(
end

typ2 = typ
while should_recurse(typ2, value_type(arg), byref)
if fieldcount(typ2) > 0
typ2 = fieldtype(typ2, 1)
while should_recurse(typ2, value_type(arg), byref, dl)
idx, _ = first_non_ghost(typ2)
if idx != -1
typ2 = fieldtype(typ2, idx)
if !Base.allocatedinline(typ2)
if byref != GPUCompiler.BITS_VALUE
legal = false
Expand All @@ -439,10 +458,9 @@ function abs_typeof(
return (true, typ2, byref)
end
end
elseif legal && if typ <: Ptr && Base.isconcretetype(typ)
elseif legal && typ <: Ptr && Base.isconcretetype(typ)
return (true, eltype(typ), GPUCompiler.BITS_VALUE)
end
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7936,8 +7936,10 @@ function GPUCompiler.codegen(
elseif byref == GPUCompiler.MUT_REF || byref == GPUCompiler.BITS_REF
Ptr{source_typ}
else
println(string(mod))
# println(string(mod))
println(string(f))
@show legal, source_typ, byref, llvm_source_typ, codegen_typ, string(inst)
@show enzyme_custom_extract_mi(f)
@assert false
end
else
Expand Down

2 comments on commit a72efb1

@wsmoses
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request updated: JuliaRegistries/General/115805

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.3 -m "<description of version>" a72efb1ba163158c199a4811d4839b2640e4dd91
git push origin v0.13.3

Please sign in to comment.