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

Allow a slightly wider class of constants to be inlined #38732

Merged
merged 1 commit into from
Dec 7, 2020

Conversation

Keno
Copy link
Member

@Keno Keno commented Dec 6, 2020

Currently we only inline constants (i.e. replace Const() yielding
statements by the constants themselves) if the constant in question
is isbits. We do this to avoid blowing up the size of the IR by
inlining constants that are too large. However, there are some
useful small constants that this definition ignores. E.g. I
was working with structs of the form:

struct Dims
    d::NTuple{N, Int} where N
end

and the IR was littered with various pure statements that the
optimizer wouldn't inline because this struct isn't considered
isbits (side-note, I want to change that, but that's a separate
issue). Here I'm proposing changing the criteria for a struct
to be eligible for inlining slightly: Rather than just looking
for isbits, we recurse through any immutable, isbits fields and
as long as the size of all structs combined isn't too large, we
allow the inlining.

Currently we only inline constants (i.e. replace Const() yielding
statements by the constants themselves) if the constant in question
is isbits. We do this to avoid blowing up the size of the IR by
inlining constants that are too large. However, there are some
useful small constants that this definition ignores. E.g. I
was working with structs of the form:
```
struct Dims
    d::NTuple{N, Int} where N
end
```
and the IR was littered with various pure statements that the
optimizer wouldn't inline because this struct isn't considered
isbits (side-note, I want to change that, but that's a separate
issue). Here I'm proposing changing the criteria for a struct
to be eligible for inlining slightly: Rather than just looking
for isbits, we recurse through any immutable, isbits fields and
as long as the size of all structs combined isn't too large, we
allow the inlining.
@Keno Keno closed this Dec 7, 2020
@Keno Keno reopened this Dec 7, 2020
@Keno Keno closed this Dec 7, 2020
@Keno Keno reopened this Dec 7, 2020
@Keno Keno merged commit 18b7fee into master Dec 7, 2020
@Keno Keno deleted the kf/inlinedimsstruct branch December 7, 2020 19:25
dtfd = DataTypeFieldDesc(dt)
for i = 1:nfields(x)
dtfd[i].isptr || continue
sz += count_const_size(getfield(x, i))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Need to check if the x.i field isdefined before loading it

Copy link
Member Author

Choose a reason for hiding this comment

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

whoops, thanks

@maleadt
Copy link
Member

maleadt commented Dec 8, 2020

This broke the following code from LLVM.jl:

function callme(ir, fn, args::Expr=:())
    quote
        Base.@_inline_meta
        Base.llvmcall(($ir,$fn), Nothing, Tuple{}, $args...)
    end
end

@generated function main()
    ir = """
        define void @entry() #0 {
        entry:
          ret void
        }"""
    fn = "entry"

    return callme(ir, fn)
end

isinteractive() || main()
ERROR: LoadError: error statically evaluating llvm IR argument

I can work around it by doing $(args.args...), but it may be an unintended side-effect of this PR.

maleadt added a commit to maleadt/LLVM.jl that referenced this pull request Dec 8, 2020
maleadt added a commit to maleadt/LLVM.jl that referenced this pull request Dec 8, 2020
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
)

Currently we only inline constants (i.e. replace Const() yielding
statements by the constants themselves) if the constant in question
is isbits. We do this to avoid blowing up the size of the IR by
inlining constants that are too large. However, there are some
useful small constants that this definition ignores. E.g. I
was working with structs of the form:
```
struct Dims
    d::NTuple{N, Int} where N
end
```
and the IR was littered with various pure statements that the
optimizer wouldn't inline because this struct isn't considered
isbits (side-note, I want to change that, but that's a separate
issue). Here I'm proposing changing the criteria for a struct
to be eligible for inlining slightly: Rather than just looking
for isbits, we recurse through any immutable, isbits fields and
as long as the size of all structs combined isn't too large, we
allow the inlining.
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.

4 participants