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

Multiversioning: support for aliases (from @ccallable) #37530

Merged
merged 1 commit into from
Nov 25, 2021

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented Sep 11, 2020

Fixes #34064, cc @KristofferC, verified with https://s3.amazonaws.com/julialangnightlies/assert_pretesting/linux/x64/1.6/julia-c2db4248a9-linux64.tar.gz (from #37510, which includes the change) where the MWE from #34064 works:

julia> app_content = """
       Base.@ccallable function julia_main()::Cint
           return 0
       end
       """
"Base.@ccallable function julia_main()::Cint\n    return 0\nend\n"

julia> # Create the sysimage
       cmd = `$(get_julia_cmd()) --sysimage=$tmp_sys_ji --cpu-target='generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)'
                                 --output-o=sys.o -e $app_content`
`/tmp/julia-c2db4248a9/bin/julia --color=yes --startup-file=no --sysimage=/tmp/julia-c2db4248a9/sys.ji '--cpu-target=generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)' --output-o=sys.o -e 'Base.@ccallable function julia_main()::Cint
    return 0
end
'`

julia> run(cmd)
Process(`/tmp/julia-c2db4248a9/bin/julia --color=yes --startup-file=no --sysimage=/tmp/julia-c2db4248a9/sys.ji '--cpu-target=generic;sandybridge,-xsaveopt,clone_all;haswell,-rdrnd,base(1)' --output-o=sys.o -e 'Base.@ccallable function julia_main()::Cint
    return 0
end
'`, ProcessExited(0))

@maleadt maleadt added the bugfix This change fixes an existing bug label Sep 11, 2020
@yuyichao
Copy link
Contributor

I don't think this is correct. If the function is cloned then all uses of it, including global aliases must be updated.

@maleadt
Copy link
Member Author

maleadt commented Sep 11, 2020

I don't think this is correct. If the function is cloned then all uses of it, including global aliases must be updated.

Yeah, this was a quick fix that happened to work 🙂 Don't we keep the original functions for aliases to point to? From my reading of fix_gv_uses, we save global variable addresses to relocate at run time, but with aliases we can't do that?

@yuyichao
Copy link
Contributor

Correct, it was not supported and doesn't have a trivial fix #21849 (review) .

There are basically two options,

  1. Do not clone a function if there's a global alias pointing to it. (will need to update the clone_all check). This will also need to add assertions to make sure the calling convention isn't broken. (no vector type allowed in the argument/return value)

  2. Emit a wrapper function for global alias. Two suboptions for this one.

    1. This can be either done via a normal function, which suffers from the same no vector register constraint.
    2. Or done with an module level assembly to emit an architecture depending tail call.

@yuyichao
Copy link
Contributor

Also ref #21849 (comment) which is essentially about the second option above.

@maleadt
Copy link
Member Author

maleadt commented Sep 13, 2020

I went with the custom trampoline + slot option, which seems to work nicely for the @ccallable stuff I'm introducing in #37510:

julia> Float16(1.)

Thread 1 "julia" hit Breakpoint 1, 0x00007fffecd6e250 in __truncdfhf2 () from usr/lib/julia/sys.so
(gdb) disassemble
Dump of assembler code for function __truncdfhf2:
=> 0x00007fffecd6e250 <+0>:     push   %rax
   0x00007fffecd6e251 <+1>:     mov    0x507bba0(%rip),%rax        # 0x7ffff1de9df8 <__truncdfhf2.reloc_slot>
   0x00007fffecd6e258 <+8>:     pop    %rcx
   0x00007fffecd6e259 <+9>:     jmpq   *%rax
End of assembler dump.
(gdb) ni 4
0x00007fffec565950 in jlcapi___truncdfhf2_17404 () from usr/lib/julia/sys.so

@maleadt maleadt changed the title Don't touch global aliases during multiversioning. Multiversioning: support for aliases (from @ccallable) Sep 13, 2020
src/llvm-multiversioning.cpp Outdated Show resolved Hide resolved
src/llvm-multiversioning.cpp Outdated Show resolved Hide resolved
src/llvm-multiversioning.cpp Outdated Show resolved Hide resolved
src/llvm-multiversioning.cpp Outdated Show resolved Hide resolved
src/llvm-multiversioning.cpp Outdated Show resolved Hide resolved
@yuyichao
Copy link
Contributor

The way the information is collected is more or less OK I guess. There are still a few other issues though. The module merger doesn't seem to handle use of @ccallable functions very well, the use of the alias should also be fixed, and there are still a few old changes here that should be removed. I'm testing locally.

@maleadt
Copy link
Member Author

maleadt commented Oct 2, 2020

Any update, @yuyichao? I had hoped to get this in 1.6.

@yuyichao
Copy link
Contributor

yuyichao commented Oct 2, 2020

Not before the build on master is unbroken...

@yuyichao

This comment has been minimized.

@maleadt

This comment has been minimized.

@yuyichao

This comment has been minimized.

@maleadt
Copy link
Member Author

maleadt commented Oct 5, 2021

Rebased.

@yuyichao, could you give this another look?

@KristofferC Verified that this still fixes the issue (the MWE from #34064 (comment)). I did run into an LLVM assertion failure in some of the code added here, but can't reproduce that anymore; could you give this a quick spin to see if you can?

@KristofferC
Copy link
Member

KristofferC commented Oct 5, 2021

I do in fact get an assertion with this PR:

❯ ./bin/MyApp
Assertion failed: (success), function jl_reinit_item, file /Users/kristoffercarlsson/julia/src/staticdata.c, line 1455.
[1]    63440 abort      ./bin/MyApp

Example repro (in PackageCompiler repo):

using PackageCompiler
create_app("examples/MyApp/", "/tmp/MyAppCompiled";force=true, incremental=true, cpu_target="native")
run(`/tmp/MyAppCompiled/bin/MyApp`)

@maleadt maleadt force-pushed the tb/multiversion_globalalias branch 3 times, most recently from 784e919 to c0af7e9 Compare October 11, 2021 16:27
@maleadt
Copy link
Member Author

maleadt commented Nov 25, 2021

Verified to fix the linked PackageCompiler issue, so let's go ahead and merge this.

@maleadt maleadt merged commit 4170090 into master Nov 25, 2021
@maleadt maleadt deleted the tb/multiversion_globalalias branch November 25, 2021 16:29
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
KristofferC pushed a commit that referenced this pull request Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug
Projects
None yet
3 participants