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

Correct alignment of atomic load and stores #19482

Merged
merged 1 commit into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions base/atomics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ inttype(::Type{Float16}) = Int16
inttype(::Type{Float32}) = Int32
inttype(::Type{Float64}) = Int64


alignment{T}(::Type{T}) = ccall(:jl_alignment, Cint, (Csize_t,), sizeof(T))

# All atomic operations have acquire and/or release semantics, depending on
# whether the load or store values. Most of the time, this is what one wants
# anyway, and it's only moderately expensive on most hardware.
Expand All @@ -213,39 +216,39 @@ for typ in atomictypes
if VersionNumber(Base.libllvm_version) >= v"3.8"
@eval getindex(x::Atomic{$typ}) =
llvmcall($"""
%rv = load atomic $rt %0 acquire, align $(WORD_SIZE ÷ 8)
%rv = load atomic $rt %0 acquire, align $(alignment(typ))
ret $lt %rv
""", $typ, Tuple{Ptr{$typ}}, unsafe_convert(Ptr{$typ}, x))
@eval setindex!(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
store atomic $lt %1, $lt* %0 release, align $(WORD_SIZE ÷ 8)
store atomic $lt %1, $lt* %0 release, align $(alignment(typ))
ret void
""", Void, Tuple{Ptr{$typ},$typ}, unsafe_convert(Ptr{$typ}, x), v)
else
if typ <: Integer
@eval getindex(x::Atomic{$typ}) =
llvmcall($"""
%rv = load atomic $rt %0 acquire, align $(WORD_SIZE ÷ 8)
%rv = load atomic $rt %0 acquire, align $(alignment(typ))
ret $lt %rv
""", $typ, Tuple{Ptr{$typ}}, unsafe_convert(Ptr{$typ}, x))
@eval setindex!(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
store atomic $lt %1, $lt* %0 release, align $(WORD_SIZE ÷ 8)
store atomic $lt %1, $lt* %0 release, align $(alignment(typ))
ret void
""", Void, Tuple{Ptr{$typ},$typ}, unsafe_convert(Ptr{$typ}, x), v)
else
@eval getindex(x::Atomic{$typ}) =
llvmcall($"""
%iptr = bitcast $lt* %0 to $ilt*
%irv = load atomic $irt %iptr acquire, align $(WORD_SIZE ÷ 8)
%irv = load atomic $irt %iptr acquire, align $(alignment(typ))
%rv = bitcast $ilt %irv to $lt
ret $lt %rv
""", $typ, Tuple{Ptr{$typ}}, unsafe_convert(Ptr{$typ}, x))
@eval setindex!(x::Atomic{$typ}, v::$typ) =
llvmcall($"""
%iptr = bitcast $lt* %0 to $ilt*
%ival = bitcast $lt %1 to $ilt
store atomic $ilt %ival, $ilt* %iptr release, align $(WORD_SIZE ÷ 8)
store atomic $ilt %ival, $ilt* %iptr release, align $(alignment(typ))
ret void
""", Void, Tuple{Ptr{$typ},$typ}, unsafe_convert(Ptr{$typ}, x), v)
end
Expand Down
4 changes: 2 additions & 2 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void NotifyDebugger(jit_code_entry *JITCodeEntry)
}
// ------------------------ END OF TEMPORARY COPY FROM LLVM -----------------

#ifdef _OS_LINUX_
#if defined(_OS_LINUX_)
// Resolve non-lock free atomic functions in the libatomic library.
// This is the library that provides support for c11/c++11 atomic operations.
static uint64_t resolve_atomic(const char *name)
Expand Down Expand Up @@ -542,7 +542,7 @@ void JuliaOJIT::addModule(std::unique_ptr<Module> M)
// Step 2: Search the program symbols
if (uint64_t addr = SectionMemoryManager::getSymbolAddressInProcess(Name))
return JL_SymbolInfo(addr, JITSymbolFlags::Exported);
#ifdef _OS_LINUX_
#if defined(_OS_LINUX_)
if (uint64_t addr = resolve_atomic(Name.c_str()))
return JL_SymbolInfo(addr, JITSymbolFlags::Exported);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ STATIC_INLINE int jl_gc_alignment(size_t sz)
return 16;
#endif
}
JL_DLLEXPORT int jl_alignment(size_t sz);

STATIC_INLINE int JL_CONST_FUNC jl_gc_szclass(size_t sz)
{
Expand Down
7 changes: 7 additions & 0 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,13 @@ void jl_start_threads(void) { }

#endif // !JULIA_ENABLE_THREADING

// Make gc alignment available for threading
// see threads.jl alignment
JL_DLLEXPORT int jl_alignment(size_t sz)
{
return jl_gc_alignment(sz);
}

#ifdef __cplusplus
}
#endif
3 changes: 2 additions & 1 deletion test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ let atomic_types = [Int8, Int16, Int32, Int64, Int128,
Float16, Float32, Float64]
# Temporarily omit 128-bit types on 32bit x86
# 128-bit atomics do not exist on AArch32.
# And we don't support them yet on power.
# And we don't support them yet on power, because they are lowered
# to `__sync_lock_test_and_set_16`.
if Sys.ARCH === :i686 || startswith(string(Sys.ARCH), "arm") ||
Sys.ARCH === :powerpc64le || Sys.ARCH === :ppc64le
filter!(T -> sizeof(T)<=8, atomic_types)
Expand Down