From 3d936af43817dbd3f07f975b3a8767e2c0f43e44 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Fri, 8 Jul 2016 08:55:42 +0900 Subject: [PATCH 01/12] Configure LLVM to built compiler-rt without clang This allows us the more freely use llvm intrinsics and have fallbacks in place for systems where they don't map to instructions. Currently this requires building with a Make.user containing ``` override BUILD_COMPILER-RT = 1 override LLVM_USE_CMAKE = 1 override USE_LLVM_SHLIB = 0 ``` TODO: * Allow for a system installation * Figure out what is needed to make this work without LLVM_USE_CMAKE --- Make.inc | 3 +++ deps/llvm.mk | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Make.inc b/Make.inc index 8d34d4be08880..ad17bb1157453 100644 --- a/Make.inc +++ b/Make.inc @@ -42,6 +42,9 @@ USE_SYSTEM_PATCHELF:=0 # Link to the LLVM shared library USE_LLVM_SHLIB := 1 +# Use the builtins from compiler-rt +USE_COMPILER-RT :=0 + ## Settings for various Intel tools # Set to 1 to use MKL USE_INTEL_MKL ?= 0 diff --git a/deps/llvm.mk b/deps/llvm.mk index bf33bc4ca354b..cdb53cfe7398d 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -13,6 +13,11 @@ BUILD_LLVM_CLANG := 1 # because it's a build requirement endif +ifeq ($(BUILD_LLVM_CLANG), 1) +BUILD_COMPILER-RT := 1 +# build requirement +endif + ifeq ($(USE_POLLY),1) ifeq ($(USE_SYSTEM_LLVM),0) ifneq ($(LLVM_VER),svn) @@ -65,13 +70,17 @@ endif # BUILD_LLDB ifeq ($(BUILD_LLVM_CLANG),1) LLVM_CLANG_TAR:=$(SRCDIR)/srccache/cfe-$(LLVM_TAR_EXT) -LLVM_COMPILER_RT_TAR:=$(SRCDIR)/srccache/compiler-rt-$(LLVM_TAR_EXT) else LLVM_CLANG_TAR:= -LLVM_COMPILER_RT_TAR:= LLVM_LIBCXX_TAR:= endif # BUILD_LLVM_CLANG +ifeq ($(BUILD_COMPILER-RT), 1) +LLVM_COMPILER_RT_TAR:=$(SRCDIR)/srccache/compiler-rt-$(LLVM_TAR_EXT) +else +LLVM_COMPILER_RT_TAR:= +endif # BUILD_COMPILER-RT + ifeq ($(BUILD_CUSTOM_LIBCXX),1) LLVM_LIBCXX_TAR:=$(SRCDIR)/srccache/libcxx-$(LLVM_TAR_EXT) endif @@ -207,19 +216,29 @@ LLVM_FLAGS += LDFLAGS="$(LLVM_LDFLAGS)" LLVM_MFLAGS += LDFLAGS="$(LLVM_LDFLAGS)" endif +ifeq ($(BUILD_COMPILER-RT),1) +ifneq ($(BUILD_LLVM_CLANG),1) +# block default building of Clang +LLVM_MFLAGS += OPTIONAL_PARALLEL_DIRS=compiler-rt +endif +else +ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6 3.7)) +LLVM_CMAKE += -DLLVM_EXTERNAL_COMPILER_RT_BUILD=OFF +else +LLVM_CMAKE += -DLLVM_TOOL_COMPILER_RT_BUILD=OFF +endif +endif + ifeq ($(BUILD_LLVM_CLANG),1) LLVM_MFLAGS += OPTIONAL_PARALLEL_DIRS=clang else -# block default building of Clang -LLVM_MFLAGS += OPTIONAL_PARALLEL_DIRS= ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6 3.7)) LLVM_CMAKE += -DLLVM_EXTERNAL_CLANG_BUILD=OFF -LLVM_CMAKE += -DLLVM_EXTERNAL_COMPILER_RT_BUILD=OFF else LLVM_CMAKE += -DLLVM_TOOL_CLANG_BUILD=OFF -LLVM_CMAKE += -DLLVM_TOOL_COMPILER_RT_BUILD=OFF endif endif + ifeq ($(BUILD_LLDB),1) LLVM_MFLAGS += OPTIONAL_DIRS=lldb else @@ -370,15 +389,17 @@ ifeq ($(BUILD_LLVM_CLANG),1) git clone $(LLVM_GIT_URL_CLANG) $(LLVM_SRC_DIR)/tools/clang ) || \ (cd $(LLVM_SRC_DIR)/tools/clang && \ git pull --ff-only) - ([ ! -d $(LLVM_SRC_DIR)/projects/compiler-rt ] && \ - git clone $(LLVM_GIT_URL_COMPILER_RT) $(LLVM_SRC_DIR)/projects/compiler-rt ) || \ - (cd $(LLVM_SRC_DIR)/projects/compiler-rt && \ - git pull --ff-only) ifneq ($(LLVM_GIT_VER_CLANG),) (cd $(LLVM_SRC_DIR)/tools/clang && \ git checkout $(LLVM_GIT_VER_CLANG)) endif # LLVM_GIT_VER_CLANG endif # BUILD_LLVM_CLANG +ifeq ($(BUILD_COMPILER-RT),1) + ([ ! -d $(LLVM_SRC_DIR)/projects/compiler-rt ] && \ + git clone $(LLVM_GIT_URL_COMPILER_RT) $(LLVM_SRC_DIR)/projects/compiler-rt ) || \ + (cd $(LLVM_SRC_DIR)/projects/compiler-rt && \ + git pull --ff-only) +endif # BUILD_COMPILER-RT ifeq ($(BUILD_LLDB),1) ([ ! -d $(LLVM_SRC_DIR)/tools/lldb ] && \ git clone $(LLVM_GIT_URL_LLDB) $(LLVM_SRC_DIR)/tools/lldb ) || \ @@ -454,11 +475,11 @@ endif # LLVM_VER ifeq ($(LLVM_VER),3.7.1) ifeq ($(BUILD_LLDB),1) $(eval $(call LLVM_PATCH,lldb-3.7.1)) -endif -ifeq ($(BUILD_LLVM_CLANG),1) +ifeq ($(BUILD_COMPILER-RT),1) $(eval $(call LLVM_PATCH,compiler-rt-3.7.1)) endif endif +endif ifeq ($(LLVM_USE_CMAKE),1) From a3c651d759bcfbfcc9a63c5b60dd9f916df2d717 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 9 Jul 2016 04:22:16 +0900 Subject: [PATCH 02/12] teach llvm.mk to create compiler-rt --- deps/llvm.mk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/deps/llvm.mk b/deps/llvm.mk index cdb53cfe7398d..e96633c94bcc7 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -533,6 +533,9 @@ else $(call make-install,llvm-$(LLVM_VER)/build_$(LLVM_BUILDTYPE),\ $(LLVM_MFLAGS) PATH="$(llvm_python_workaround):$$PATH") endif # LLVM_USE_CMAKE +ifeq ($(BUILD_COMPILER-RT),1) + $(CC) $(LDFLAGS) -shared $(fPIC) -o $(build_libdir)/libcompiler-rt.$(SHLIB_EXT) -nostdlib -Wl,--whole-archive -L$(build_libdir)/clang/$(LLVM_VER)/lib/$(call patsubst,%inux,linux,$(OS)) -lclang_rt.builtins-$(call patsubst,i%86,i386,$(ARCH)) +endif touch -c $@ reinstall-llvm: From a34bb7f693c13f1c53136237ff5f67e203f7b72c Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 9 Jul 2016 05:16:11 +0900 Subject: [PATCH 03/12] link sys.so against compiler-rt --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9d7e02534915c..667976534f4b6 100644 --- a/Makefile +++ b/Makefile @@ -180,7 +180,8 @@ endif $(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o @$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ $< \ $(if $(findstring -debug.$(SHLIB_EXT),$(notdir $@)),-ljulia-debug,-ljulia) \ - $$([ $(OS) = WINNT ] && echo '' -lssp)) + $$([ $(OS) = WINNT ] && echo '' -lssp) \ + $$([ $(BUILD_COMPILER-RT) = 1 ] && echo '' -lcompiler-rt)) @$(INSTALL_NAME_CMD)$(notdir $@) $@ @$(DSYMUTIL) $@ From b504f99a481e15b7ac0c0fff6c209e7024be6121 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 9 Jul 2016 03:27:41 +0900 Subject: [PATCH 04/12] Load a shared version of compiler-rt in jitlayers LLVM intrinsics either map to instructions or to functions in compiler-rt. If we can't find a symbol look into a shared version of compiler-rt and resolve the functions there. --- src/jitlayers.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/jitlayers.cpp b/src/jitlayers.cpp index c0eeb33aabd72..5dfa1ac5841f6 100644 --- a/src/jitlayers.cpp +++ b/src/jitlayers.cpp @@ -198,6 +198,19 @@ void NotifyDebugger(jit_code_entry *JITCodeEntry) } // ------------------------ END OF TEMPORARY COPY FROM LLVM ----------------- +// Resolve compiler-rt functions in the shared library that we created from compiler-rt +static uint64_t resolve_compiler_rt(const char *name) +{ + static void *compiler_rt_hdl = jl_load_dynamic_library_e("libcompiler-rt", + JL_RTLD_LOCAL); + static const char *const prefix = "__"; + if (!compiler_rt_hdl) + return 0; + if (strncmp(name, prefix, strlen(prefix) != 0)) + return 0; + return (uintptr_t)jl_dlsym_e(compiler_rt_hdl, name); +} + #ifdef _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. @@ -428,6 +441,8 @@ class JuliaOJIT { if (uint64_t addr = resolve_atomic(Name.c_str())) return RuntimeDyld::SymbolInfo(addr, JITSymbolFlags::Exported); #endif + if (uint64_t addr = resolve_compiler_rt(Name.c_str())) + return RuntimeDyld::SymbolInfo(addr, JITSymbolFlags::Exported); // Return failure code return RuntimeDyld::SymbolInfo(nullptr); }, From b96355dcc0d874dfa8a126c9efab7e3482c46d64 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 10 Jul 2016 04:33:31 +0900 Subject: [PATCH 05/12] rename COMPILER-RT to COMPILER_RT --- Make.inc | 2 +- Makefile | 2 +- deps/llvm.mk | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Make.inc b/Make.inc index ad17bb1157453..85c532848de92 100644 --- a/Make.inc +++ b/Make.inc @@ -43,7 +43,7 @@ USE_SYSTEM_PATCHELF:=0 USE_LLVM_SHLIB := 1 # Use the builtins from compiler-rt -USE_COMPILER-RT :=0 +USE_COMPILER_RT :=0 ## Settings for various Intel tools # Set to 1 to use MKL diff --git a/Makefile b/Makefile index 667976534f4b6..68ef0e52ad785 100644 --- a/Makefile +++ b/Makefile @@ -181,7 +181,7 @@ $(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o @$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ $< \ $(if $(findstring -debug.$(SHLIB_EXT),$(notdir $@)),-ljulia-debug,-ljulia) \ $$([ $(OS) = WINNT ] && echo '' -lssp) \ - $$([ $(BUILD_COMPILER-RT) = 1 ] && echo '' -lcompiler-rt)) + $$([ $(BUILD_COMPILER_RT) = 1 ] && echo '' -lcompiler-rt)) @$(INSTALL_NAME_CMD)$(notdir $@) $@ @$(DSYMUTIL) $@ diff --git a/deps/llvm.mk b/deps/llvm.mk index e96633c94bcc7..7cf28ac6edeac 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -14,7 +14,7 @@ BUILD_LLVM_CLANG := 1 endif ifeq ($(BUILD_LLVM_CLANG), 1) -BUILD_COMPILER-RT := 1 +BUILD_COMPILER_RT := 1 # build requirement endif @@ -75,11 +75,11 @@ LLVM_CLANG_TAR:= LLVM_LIBCXX_TAR:= endif # BUILD_LLVM_CLANG -ifeq ($(BUILD_COMPILER-RT), 1) +ifeq ($(BUILD_COMPILER_RT), 1) LLVM_COMPILER_RT_TAR:=$(SRCDIR)/srccache/compiler-rt-$(LLVM_TAR_EXT) else LLVM_COMPILER_RT_TAR:= -endif # BUILD_COMPILER-RT +endif # BUILD_COMPILER_RT ifeq ($(BUILD_CUSTOM_LIBCXX),1) LLVM_LIBCXX_TAR:=$(SRCDIR)/srccache/libcxx-$(LLVM_TAR_EXT) @@ -216,7 +216,7 @@ LLVM_FLAGS += LDFLAGS="$(LLVM_LDFLAGS)" LLVM_MFLAGS += LDFLAGS="$(LLVM_LDFLAGS)" endif -ifeq ($(BUILD_COMPILER-RT),1) +ifeq ($(BUILD_COMPILER_RT),1) ifneq ($(BUILD_LLVM_CLANG),1) # block default building of Clang LLVM_MFLAGS += OPTIONAL_PARALLEL_DIRS=compiler-rt @@ -394,12 +394,12 @@ ifneq ($(LLVM_GIT_VER_CLANG),) git checkout $(LLVM_GIT_VER_CLANG)) endif # LLVM_GIT_VER_CLANG endif # BUILD_LLVM_CLANG -ifeq ($(BUILD_COMPILER-RT),1) +ifeq ($(BUILD_COMPILER_RT),1) ([ ! -d $(LLVM_SRC_DIR)/projects/compiler-rt ] && \ git clone $(LLVM_GIT_URL_COMPILER_RT) $(LLVM_SRC_DIR)/projects/compiler-rt ) || \ (cd $(LLVM_SRC_DIR)/projects/compiler-rt && \ git pull --ff-only) -endif # BUILD_COMPILER-RT +endif # BUILD_COMPILER_RT ifeq ($(BUILD_LLDB),1) ([ ! -d $(LLVM_SRC_DIR)/tools/lldb ] && \ git clone $(LLVM_GIT_URL_LLDB) $(LLVM_SRC_DIR)/tools/lldb ) || \ @@ -475,7 +475,7 @@ endif # LLVM_VER ifeq ($(LLVM_VER),3.7.1) ifeq ($(BUILD_LLDB),1) $(eval $(call LLVM_PATCH,lldb-3.7.1)) -ifeq ($(BUILD_COMPILER-RT),1) +ifeq ($(BUILD_COMPILER_RT),1) $(eval $(call LLVM_PATCH,compiler-rt-3.7.1)) endif endif @@ -533,7 +533,7 @@ else $(call make-install,llvm-$(LLVM_VER)/build_$(LLVM_BUILDTYPE),\ $(LLVM_MFLAGS) PATH="$(llvm_python_workaround):$$PATH") endif # LLVM_USE_CMAKE -ifeq ($(BUILD_COMPILER-RT),1) +ifeq ($(BUILD_COMPILER_RT),1) $(CC) $(LDFLAGS) -shared $(fPIC) -o $(build_libdir)/libcompiler-rt.$(SHLIB_EXT) -nostdlib -Wl,--whole-archive -L$(build_libdir)/clang/$(LLVM_VER)/lib/$(call patsubst,%inux,linux,$(OS)) -lclang_rt.builtins-$(call patsubst,i%86,i386,$(ARCH)) endif touch -c $@ From d8d95081d02cd8c001f5e1840e2ab6e2de73a731 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 10 Jul 2016 04:36:09 +0900 Subject: [PATCH 06/12] fix patch for compiler-rt --- deps/llvm.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/llvm.mk b/deps/llvm.mk index 7cf28ac6edeac..361a517f0e71b 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -475,11 +475,11 @@ endif # LLVM_VER ifeq ($(LLVM_VER),3.7.1) ifeq ($(BUILD_LLDB),1) $(eval $(call LLVM_PATCH,lldb-3.7.1)) +endif ifeq ($(BUILD_COMPILER_RT),1) $(eval $(call LLVM_PATCH,compiler-rt-3.7.1)) endif endif -endif ifeq ($(LLVM_USE_CMAKE),1) From 086c6f37b5f631353a64c299992fc5841fe0b7e9 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 10 Jul 2016 04:39:29 +0900 Subject: [PATCH 07/12] use build_shlibdir --- deps/llvm.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/llvm.mk b/deps/llvm.mk index 361a517f0e71b..641838bdf1b9b 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -534,7 +534,7 @@ else $(LLVM_MFLAGS) PATH="$(llvm_python_workaround):$$PATH") endif # LLVM_USE_CMAKE ifeq ($(BUILD_COMPILER_RT),1) - $(CC) $(LDFLAGS) -shared $(fPIC) -o $(build_libdir)/libcompiler-rt.$(SHLIB_EXT) -nostdlib -Wl,--whole-archive -L$(build_libdir)/clang/$(LLVM_VER)/lib/$(call patsubst,%inux,linux,$(OS)) -lclang_rt.builtins-$(call patsubst,i%86,i386,$(ARCH)) + $(CC) $(LDFLAGS) -shared $(fPIC) -o $(build_shlibdir)/libcompiler-rt.$(SHLIB_EXT) -nostdlib -Wl,--whole-archive -L$(build_libdir)/clang/$(LLVM_VER)/lib/$(call patsubst,%inux,linux,$(OS)) -lclang_rt.builtins-$(call patsubst,i%86,i386,$(ARCH)) endif touch -c $@ From e5f8f177d80c1ba91c9355e61a8a50a8c977c159 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 7 Jul 2016 07:09:27 +0900 Subject: [PATCH 08/12] use llvm intrinsics to convert from and to Float16 --- base/float.jl | 4 +- base/float16.jl | 123 ++++++++++-------------------------------------- 2 files changed, 27 insertions(+), 100 deletions(-) diff --git a/base/float.jl b/base/float.jl index 6d507f2d6b34b..103a56d430cc0 100644 --- a/base/float.jl +++ b/base/float.jl @@ -99,11 +99,11 @@ function convert(::Type{Float32}, x::Int128) end #convert(::Type{Float16}, x::Float32) = box(Float16,fptrunc(Float16,x)) -convert(::Type{Float16}, x::Float64) = convert(Float16, convert(Float32,x)) +#convert(::Type{Float16}, x::Float64) = convert(Float16, convert(Float32,x)) convert(::Type{Float32}, x::Float64) = box(Float32,fptrunc(Float32,unbox(Float64,x))) #convert(::Type{Float32}, x::Float16) = box(Float32,fpext(Float32,x)) -convert(::Type{Float64}, x::Float16) = convert(Float64, convert(Float32,x)) +#convert(::Type{Float64}, x::Float16) = convert(Float64, convert(Float32,x)) convert(::Type{Float64}, x::Float32) = box(Float64,fpext(Float64,unbox(Float32,x))) convert(::Type{AbstractFloat}, x::Bool) = convert(Float64, x) diff --git a/base/float16.jl b/base/float16.jl index 46dcb8bfaa0d7..b33239c0d660e 100644 --- a/base/float16.jl +++ b/base/float16.jl @@ -1,102 +1,29 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license - -function convert(::Type{Float32}, val::Float16) - ival::UInt32 = reinterpret(UInt16, val) - sign::UInt32 = (ival & 0x8000) >> 15 - exp::UInt32 = (ival & 0x7c00) >> 10 - sig::UInt32 = (ival & 0x3ff) >> 0 - ret::UInt32 - - if exp == 0 - if sig == 0 - sign = sign << 31 - ret = sign | exp | sig - else - n_bit = 1 - bit = 0x0200 - while (bit & sig) == 0 - n_bit = n_bit + 1 - bit = bit >> 1 - end - sign = sign << 31 - exp = (-14 - n_bit + 127) << 23 - sig = ((sig & (~bit)) << n_bit) << (23 - 10) - ret = sign | exp | sig - end - elseif exp == 0x1f - if sig == 0 # Inf - if sign == 0 - ret = 0x7f800000 - else - ret = 0xff800000 - end - else # NaN - ret = 0x7fc00000 | (sign<<31) - end - else - sign = sign << 31 - exp = (exp - 15 + 127) << 23 - sig = sig << (23 - 10) - ret = sign | exp | sig - end - return reinterpret(Float32, ret) -end - -# Float32 -> Float16 algorithm from: -# "Fast Half Float Conversion" by Jeroen van der Zijp -# ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf - -const basetable = Array{UInt16}(512) -const shifttable = Array{UInt8}(512) - -for i = 0:255 - e = i - 127 - if e < -24 # Very small numbers map to zero - basetable[i|0x000+1] = 0x0000 - basetable[i|0x100+1] = 0x8000 - shifttable[i|0x000+1] = 24 - shifttable[i|0x100+1] = 24 - elseif e < -14 # Small numbers map to denorms - basetable[i|0x000+1] = (0x0400>>(-e-14)) - basetable[i|0x100+1] = (0x0400>>(-e-14)) | 0x8000 - shifttable[i|0x000+1] = -e-1 - shifttable[i|0x100+1] = -e-1 - elseif e <= 15 # Normal numbers just lose precision - basetable[i|0x000+1] = ((e+15)<<10) - basetable[i|0x100+1] = ((e+15)<<10) | 0x8000 - shifttable[i|0x000+1] = 13 - shifttable[i|0x100+1] = 13 - elseif e < 128 # Large numbers map to Infinity - basetable[i|0x000+1] = 0x7C00 - basetable[i|0x100+1] = 0xFC00 - shifttable[i|0x000+1] = 24 - shifttable[i|0x100+1] = 24 - else # Infinity and NaN's stay Infinity and NaN's - basetable[i|0x000+1] = 0x7C00 - basetable[i|0x100+1] = 0xFC00 - shifttable[i|0x000+1] = 13 - shifttable[i|0x100+1] = 13 - end -end - -function convert(::Type{Float16}, val::Float32) - f = reinterpret(UInt32, val) - i = (f >> 23) & 0x1ff + 1 - sh = shifttable[i] - f &= 0x007fffff - h::UInt16 = basetable[i] + (f >> sh) - # round - # NOTE: we maybe should ignore NaNs here, but the payload is - # getting truncated anyway so "rounding" it might not matter - nextbit = (f >> (sh-1)) & 1 - if nextbit != 0 - if h&1 == 1 || # round halfway to even - (f & ((1<<(sh-1))-1)) != 0 # check lower bits - h += 1 - end - end - reinterpret(Float16, h) -end +import Base.llvmcall +# Implement conversion to and from Float16 with llvm intrinsics +convert(::Type{Float32}, val::Float16) = + llvmcall(("""declare float @llvm.convert.from.fp16.f32(i16)""", + """%2 = call float @llvm.convert.from.fp16.f32(i16 %0) + ret float %2"""), + Float32, Tuple{Float16}, val) + +convert(::Type{Float64}, val::Float16) = + llvmcall(("""declare double @llvm.convert.from.fp16.f64(i16)""", + """%2 = call double @llvm.convert.from.fp16.f64(i16 %0) + ret double %2"""), + Float64, Tuple{Float16}, val) + +convert(::Type{Float16}, val::Float32) = + llvmcall(("""declare i16 @llvm.convert.to.fp16.f32(float)""", + """%2 = call i16 @llvm.convert.to.fp16.f32(float %0) + ret i16 %2"""), + Float16, Tuple{Float32}, val) + +convert(::Type{Float16}, val::Float64) = + llvmcall(("""declare i16 @llvm.convert.to.fp16.f64(double)""", + """%2 = call i16 @llvm.convert.to.fp16.f64(double %0) + ret i16 %2"""), + Float16, Tuple{Float64}, val) convert(::Type{Bool}, x::Float16) = x==0 ? false : x==1 ? true : throw(InexactError()) convert(::Type{Int128}, x::Float16) = convert(Int128, Float32(x)) From 86da367305a53bd07947c0aff84f4b875a0ab66b Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 11 Jul 2016 23:48:44 +0900 Subject: [PATCH 09/12] cleanup makefile for compiler-rt --- Make.inc | 2 +- Makefile | 2 +- deps/llvm.mk | 21 ++++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Make.inc b/Make.inc index 85c532848de92..d7164b830194d 100644 --- a/Make.inc +++ b/Make.inc @@ -43,7 +43,7 @@ USE_SYSTEM_PATCHELF:=0 USE_LLVM_SHLIB := 1 # Use the builtins from compiler-rt -USE_COMPILER_RT :=0 +BUILD_COMPILER_RT := 0 ## Settings for various Intel tools # Set to 1 to use MKL diff --git a/Makefile b/Makefile index 68ef0e52ad785..d7d72e987d680 100644 --- a/Makefile +++ b/Makefile @@ -177,7 +177,7 @@ ifeq ($(OS), WINNT) $(build_sysconfdir)/julia/juliarc.jl: $(JULIAHOME)/contrib/windows/juliarc.jl endif -$(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o +$(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o $(wildcard $(CRT_OBJ_TARGET)) @$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ $< \ $(if $(findstring -debug.$(SHLIB_EXT),$(notdir $@)),-ljulia-debug,-ljulia) \ $$([ $(OS) = WINNT ] && echo '' -lssp) \ diff --git a/deps/llvm.mk b/deps/llvm.mk index 641838bdf1b9b..d02c0222555af 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -533,9 +533,6 @@ else $(call make-install,llvm-$(LLVM_VER)/build_$(LLVM_BUILDTYPE),\ $(LLVM_MFLAGS) PATH="$(llvm_python_workaround):$$PATH") endif # LLVM_USE_CMAKE -ifeq ($(BUILD_COMPILER_RT),1) - $(CC) $(LDFLAGS) -shared $(fPIC) -o $(build_shlibdir)/libcompiler-rt.$(SHLIB_EXT) -nostdlib -Wl,--whole-archive -L$(build_libdir)/clang/$(LLVM_VER)/lib/$(call patsubst,%inux,linux,$(OS)) -lclang_rt.builtins-$(call patsubst,i%86,i386,$(ARCH)) -endif touch -c $@ reinstall-llvm: @@ -550,6 +547,24 @@ distclean-llvm: $(LLVM_COMPILER_RT_TAR) $(LLVM_LIBCXX_TAR) $(LLVM_LLDB_TAR) \ $(LLVM_SRC_DIR) $(LLVM_BUILDDIR_withtype) +# COMPILER-RT +ifeq ($(BUILD_COMPILER_RT),1) +CRT_OS := $(call patsubst,%inux,linux,$(OS)) +CRT_ARCH := $(call patsubst,i%86,i386,$(ARCH)) +CRT_BUILD_DIR := $(LLVM_BUILDDIR_withtype)/lib/clang/$(LLVM_VER)/lib/$(CRT_OS) +CRT_STATIC_NAME := clang_rt.builtins-$(CRT_ARCH) +CRT_OBJ_TARGET := $(build_shlibdir)/libcompiler-rt.$(SHLIB_EXT) + +$(CRT_BUILD_DIR)/lib$(CRT_STATIC_NAME): | $(LLVM_OBJ_TARGET) + touch -c $@ + +$(CRT_OBJ_TARGET): $(CRT_BUILD_DIR)/lib$(CRT_STATIC_NAME) + $(CC) $(LDFLAGS) -shared $(fPIC) -o $@ -nostdlib -Wl,--whole-archive -L$(CRT_BUILD_DIR) -l$(CRT_STATIC_NAME) + touch -c $@ + +install-compiler_rt: $(CRT_OBJ_TARGET) +endif + ifneq ($(LLVM_VER),svn) get-llvm: $(LLVM_TAR) $(LLVM_CLANG_TAR) $(LLVM_COMPILER_RT_TAR) $(LLVM_LIBCXX_TAR) $(LLVM_LLDB_TAR) else From 956b4fc1ddf1c5268fd9ec5c52229c1903b934f5 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 12 Jul 2016 05:11:38 +0900 Subject: [PATCH 10/12] enable compiler_rt to be build for USE_SYSTEM_LLVM --- Make.inc | 2 +- Makefile | 2 +- deps/Makefile | 4 ++-- deps/llvm.mk | 9 ++++++--- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Make.inc b/Make.inc index d7164b830194d..e8d0976304e15 100644 --- a/Make.inc +++ b/Make.inc @@ -43,7 +43,7 @@ USE_SYSTEM_PATCHELF:=0 USE_LLVM_SHLIB := 1 # Use the builtins from compiler-rt -BUILD_COMPILER_RT := 0 +BUILD_COMPILER_RT := 1 ## Settings for various Intel tools # Set to 1 to use MKL diff --git a/Makefile b/Makefile index d7d72e987d680..68ef0e52ad785 100644 --- a/Makefile +++ b/Makefile @@ -177,7 +177,7 @@ ifeq ($(OS), WINNT) $(build_sysconfdir)/julia/juliarc.jl: $(JULIAHOME)/contrib/windows/juliarc.jl endif -$(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o $(wildcard $(CRT_OBJ_TARGET)) +$(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o @$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ $< \ $(if $(findstring -debug.$(SHLIB_EXT),$(notdir $@)),-ljulia-debug,-ljulia) \ $$([ $(OS) = WINNT ] && echo '' -lssp) \ diff --git a/deps/Makefile b/deps/Makefile index e376b07671922..f1c351adaacc3 100644 --- a/deps/Makefile +++ b/deps/Makefile @@ -209,7 +209,7 @@ get: $(addprefix get-, $(DEP_LIBS)) configure: $(addprefix configure-, $(DEP_LIBS)) compile: $(addprefix compile-, $(DEP_LIBS)) check: $(addprefix check-, $(DEP_LIBS)) -install: $(addprefix install-, $(DEP_LIBS)) +install: $(addprefix install-, $(DEP_LIBS)) install-compiler_rt cleanall: $(addprefix clean-, $(DEP_LIBS)) distcleanall: $(addprefix distclean-, $(DEP_LIBS)) rm -rf $(build_prefix) @@ -268,4 +268,4 @@ include $(SRCDIR)/virtualenv.mk .PHONY: default compile install cleanall distcleanall \ get-* configure-* compile-* check-* install-* \ - clean-* distclean-* reinstall-* update-llvm + clean-* distclean-* reinstall-* update-llvm install-compiler_rt diff --git a/deps/llvm.mk b/deps/llvm.mk index d02c0222555af..f7be6067599bd 100644 --- a/deps/llvm.mk +++ b/deps/llvm.mk @@ -548,22 +548,25 @@ distclean-llvm: $(LLVM_SRC_DIR) $(LLVM_BUILDDIR_withtype) # COMPILER-RT -ifeq ($(BUILD_COMPILER_RT),1) CRT_OS := $(call patsubst,%inux,linux,$(OS)) CRT_ARCH := $(call patsubst,i%86,i386,$(ARCH)) -CRT_BUILD_DIR := $(LLVM_BUILDDIR_withtype)/lib/clang/$(LLVM_VER)/lib/$(CRT_OS) CRT_STATIC_NAME := clang_rt.builtins-$(CRT_ARCH) CRT_OBJ_TARGET := $(build_shlibdir)/libcompiler-rt.$(SHLIB_EXT) +ifeq ($(USE_SYSTEM_LLVM),0) +CRT_BUILD_DIR := $(LLVM_BUILDDIR_withtype)/lib/clang/$(LLVM_VER)/lib/$(CRT_OS) $(CRT_BUILD_DIR)/lib$(CRT_STATIC_NAME): | $(LLVM_OBJ_TARGET) touch -c $@ +else +CRT_BUILD_DIR := $(shell llvm-config --libdir)/clang/$(shell llvm-config --version)/lib/$(CRT_OS) +$(CRT_BUILD_DIR)/lib$(CRT_STATIC_NAME): +endif $(CRT_OBJ_TARGET): $(CRT_BUILD_DIR)/lib$(CRT_STATIC_NAME) $(CC) $(LDFLAGS) -shared $(fPIC) -o $@ -nostdlib -Wl,--whole-archive -L$(CRT_BUILD_DIR) -l$(CRT_STATIC_NAME) touch -c $@ install-compiler_rt: $(CRT_OBJ_TARGET) -endif ifneq ($(LLVM_VER),svn) get-llvm: $(LLVM_TAR) $(LLVM_CLANG_TAR) $(LLVM_COMPILER_RT_TAR) $(LLVM_LIBCXX_TAR) $(LLVM_LLDB_TAR) From 999577f145c21b5f097f6fcfbd096bb07184ccb8 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 12 Jul 2016 06:00:54 +0900 Subject: [PATCH 11/12] use CMAKE for llvm from 3.7 upwards --- deps/llvm-ver.make | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/llvm-ver.make b/deps/llvm-ver.make index 323ac9423fb51..fe31dd59e3524 100644 --- a/deps/llvm-ver.make +++ b/deps/llvm-ver.make @@ -11,7 +11,7 @@ ifeq ($(LLVM_VER_PATCH),) LLVM_VER_PATCH := 0 endif -ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6 3.7 3.8)) +ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6)) LLVM_USE_CMAKE := 0 else LLVM_USE_CMAKE := 1 From 8bf61ad413e7e856f8f8112f45ae385586a8390d Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 12 Jul 2016 06:09:25 +0900 Subject: [PATCH 12/12] Bump llvm to 3.8 We need a cmake build that also works with SHLIB=1 for compiler-rt --- deps/Versions.make | 2 +- deps/llvm-ver.make | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/Versions.make b/deps/Versions.make index 3cf63f3c7eaf4..b83cb58ae09f4 100644 --- a/deps/Versions.make +++ b/deps/Versions.make @@ -1,4 +1,4 @@ -LLVM_VER = 3.7.1 +LLVM_VER = 3.8.0 LLVM_LIB_SUFFIX = PCRE_VER = 10.21 DSFMT_VER = 2.2.3 diff --git a/deps/llvm-ver.make b/deps/llvm-ver.make index fe31dd59e3524..c0f595722dec3 100644 --- a/deps/llvm-ver.make +++ b/deps/llvm-ver.make @@ -11,7 +11,7 @@ ifeq ($(LLVM_VER_PATCH),) LLVM_VER_PATCH := 0 endif -ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6)) +ifeq ($(LLVM_VER_SHORT),$(filter $(LLVM_VER_SHORT),3.3 3.4 3.5 3.6 3.7)) LLVM_USE_CMAKE := 0 else LLVM_USE_CMAKE := 1