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

Running code_typed changes result of code_llvm #53718

Closed
topolarity opened this issue Mar 12, 2024 · 4 comments
Closed

Running code_typed changes result of code_llvm #53718

topolarity opened this issue Mar 12, 2024 · 4 comments

Comments

@topolarity
Copy link
Member

topolarity commented Mar 12, 2024

julia> using NetworkOptions

julia> code_llvm(NetworkOptions.__init__, (); raw=true)
; Function Signature: __init__()
;  @ /build/julia/usr/share/julia/stdlib/v1.11/NetworkOptions/src/ssh_options.jl:163 within `__init__`
define swiftcc void @julia___init___1092(ptr nonnull swiftself %pgcstack) #0 !dbg !5 {
top:
  %jlcallframe1 = alloca [2 x ptr], align 8
  %ptls_field = getelementptr inbounds ptr, ptr %pgcstack, i64 2
  %ptls_load = load ptr, ptr %ptls_field, align 8, !tbaa !14
  %0 = getelementptr inbounds ptr, ptr %ptls_load, i64 2
  %safepoint = load ptr, ptr %0, align 8, !tbaa !18
  fence syncscope("singlethread") seq_cst
  %1 = load volatile i64, ptr %safepoint, align 8, !dbg !20
  fence syncscope("singlethread") seq_cst
;  @ /build/julia/usr/share/julia/stdlib/v1.11/NetworkOptions/src/ssh_options.jl:165 within `__init__`
  store ptr @"jl_global#1098.jit", ptr %jlcallframe1, align 8, !dbg !21
  %2 = getelementptr inbounds ptr, ptr %jlcallframe1, i64 1, !dbg !21
  store ptr @"jl_global#1099.jit", ptr %2, align 8, !dbg !21
  %3 = call nonnull ptr @ijl_invoke(ptr nonnull @"jl_global#1097.jit", ptr nonnull %jlcallframe1, i32 2, ptr nonnull @"-Main.Base.lock#1096.jit"), !dbg !21
  ret void, !dbg !21
}

versus if you call code_typed first:

julia> using NetworkOptions

julia> code_typed(NetworkOptions.__init__, ())
1-element Vector{Any}:
 CodeInfo(
1 ─     invoke NetworkOptions.lock(NetworkOptions.var"#7#8"()::NetworkOptions.var"#7#8", NetworkOptions.BUNDLED_KNOWN_HOSTS_LOCK::ReentrantLock)::Nothing
└──     return nothing
) => Nothing

julia> code_llvm(NetworkOptions.__init__, (); raw=true)
; Function Signature: __init__()
;  @ /build/julia/usr/share/julia/stdlib/v1.11/NetworkOptions/src/ssh_options.jl:163 within `__init__`
define swiftcc void @julia___init___2493(ptr nonnull swiftself %pgcstack) #0 !dbg !5 {
top:
  %ptls_field = getelementptr inbounds ptr, ptr %pgcstack, i64 2
  %ptls_load = load ptr, ptr %ptls_field, align 8, !tbaa !14
  %0 = getelementptr inbounds ptr, ptr %ptls_load, i64 2
  %safepoint = load ptr, ptr %0, align 8, !tbaa !18
  fence syncscope("singlethread") seq_cst
  %1 = load volatile i64, ptr %safepoint, align 8, !dbg !20
  fence syncscope("singlethread") seq_cst
;  @ /build/julia/usr/share/julia/stdlib/v1.11/NetworkOptions/src/ssh_options.jl:165 within `__init__`
  call swiftcc void @j_lock_2497(ptr nonnull swiftself %pgcstack, ptr nonnull @"jl_global#2498.jit"), !dbg !21
  ret void, !dbg !21
}

Notice the jl_invoke that became a proper static call.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Mar 13, 2024

I think this is the same inference bug as I ran into here: #52069

@topolarity
Copy link
Member Author

I believe @gbaraldi pointed out another unresolved mystery here, which is why we end up with this "constant invoke" in the first place

@gbaraldi
Copy link
Member

This is mostly an artifact of the fact that the first time we compile that code the global doesn't exist, but if we recompile it we know it exists and assume it to always exist (potentially incorrectly)

@vtjnash
Copy link
Sponsor Member

vtjnash commented Mar 15, 2024

Looking closer, this is not a bug at all, just a suboptimal inference result (which is never a bug)

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

No branches or pull requests

3 participants