Skip to content

Commit 442f6f7

Browse files
committed
Add an option to add CPUID tag to sysimg and .ji files
1 parent c38a5a3 commit 442f6f7

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

Make.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ HAVE_SSP := 0
8383
WITH_GC_VERIFY := 0
8484
WITH_GC_DEBUG_ENV := 0
8585

86+
# When set, give julia binaries CPUID specific names. This is useful in cluster environments
87+
# with heterogeneous architectures. N.B.: will not be automatically rebuilt for all
88+
# architectures if julia is updated.
89+
CPUID_SPECIFIC_BINARIES ?= 0
90+
8691
# Prevent picking up $ARCH from the environment variables
8792
ARCH:=
8893

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ julia-ui-release julia-ui-debug : julia-ui-% : julia-src-%
9595
julia-inference : julia-base julia-ui-$(JULIA_BUILD_MODE) $(build_prefix)/.examples
9696
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT) $(build_private_libdir)/inference.ji JULIA_BUILD_MODE=$(JULIA_BUILD_MODE)
9797

98+
ifneq ($(CPUID_SPECIFIC_BINARIES), 0)
99+
CPUID_TAG = _$(call spawn,$(JULIA_EXECUTABLE) --cpuid)
100+
else
101+
CPUID_TAG =
102+
endif
103+
98104
julia-sysimg-release : julia-inference julia-ui-release
99-
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT) $(build_private_libdir)/sys.$(SHLIB_EXT) JULIA_BUILD_MODE=release
105+
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT) $(build_private_libdir)/sys$(CPUID_TAG).$(SHLIB_EXT) JULIA_BUILD_MODE=release
100106

101107
julia-sysimg-debug : julia-inference julia-ui-debug
102-
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT) $(build_private_libdir)/sys-debug.$(SHLIB_EXT) JULIA_BUILD_MODE=debug
108+
@$(MAKE) $(QUIET_MAKE) -C $(BUILDROOT) $(build_private_libdir)/sys-debug$(CPUID_TAG).$(SHLIB_EXT) JULIA_BUILD_MODE=debug
103109

104110
julia-debug julia-release : julia-% : julia-ui-% julia-sysimg-% julia-symlink julia-libccalltest
105111

@@ -169,7 +175,7 @@ $(build_datarootdir)/julia/julia-config.jl : $(JULIAHOME)/contrib/julia-config.j
169175

170176
$(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o
171177
@$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ $< \
172-
$(if $(findstring -debug.$(SHLIB_EXT),$(notdir $@)),-ljulia-debug,-ljulia) \
178+
$(if $(findstring -debug,$(notdir $@)),-ljulia-debug,-ljulia) \
173179
$$([ $(OS) = WINNT ] && echo '' -lssp))
174180
@$(INSTALL_NAME_CMD)$(notdir $@) $@
175181
@$(DSYMUTIL) $@
@@ -215,8 +221,13 @@ $$(build_private_libdir)/sys$1.o: $$(build_private_libdir)/inference.ji $$(JULIA
215221
|| { echo '*** This error is usually fixed by running `make clean`. If the error persists$$(COMMA) try `make cleanall`. ***' && false; } )
216222
.SECONDARY: $(build_private_libdir)/sys$1.o
217223
endef
224+
ifneq ($(CPUID_SPECIFIC_BINARIES),0)
225+
$(eval $(call sysimg_builder,_%,-O3,$(JULIA_EXECUTABLE_release)))
226+
$(eval $(call sysimg_builder,-debug_%,-O0,$(JULIA_EXECUTABLE_debug)))
227+
else
218228
$(eval $(call sysimg_builder,,-O3,$(JULIA_EXECUTABLE_release)))
219229
$(eval $(call sysimg_builder,-debug,-O0,$(JULIA_EXECUTABLE_debug)))
230+
endif
220231

221232
$(build_depsbindir)/stringreplace: $(JULIAHOME)/contrib/stringreplace.c | $(build_depsbindir)
222233
@$(call PRINT_CC, $(HOSTCC) -o $(build_depsbindir)/stringreplace $(JULIAHOME)/contrib/stringreplace.c)

base/pkg/pkg.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ init(meta::AbstractString=DEFAULT_META, branch::AbstractString=META_BRANCH) = Di
7171

7272
function __init__()
7373
vers = "v$(VERSION.major).$(VERSION.minor)"
74+
vers = ccall(:jl_uses_cpuid_tag, Cint, ()) == 0 ? vers :
75+
joinpath(vers,hex(ccall(:jl_cpuid_tag, UInt64, ()), 2*sizeof(UInt64)))
7476
unshift!(Base.LOAD_CACHE_PATH, abspath(Dir._pkgroot(), "lib", vers))
7577
end
7678

src/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ DEBUGFLAGS += $(FLAGS)
103103
SHIPFLAGS += $(FLAGS)
104104

105105
# if not absolute, then relative to the directory of the julia executable
106-
SHIPFLAGS += "-DJL_SYSTEM_IMAGE_PATH=\"$(build_private_libdir_rel)/sys.$(SHLIB_EXT)\""
107-
DEBUGFLAGS += "-DJL_SYSTEM_IMAGE_PATH=\"$(build_private_libdir_rel)/sys-debug.$(SHLIB_EXT)\""
106+
SHIPFLAGS += "-DJL_SYSTEM_IMAGE_PATH=\"$(build_private_libdir_rel)/sys\""
107+
DEBUGFLAGS += "-DJL_SYSTEM_IMAGE_PATH=\"$(build_private_libdir_rel)/sys-debug\""
108+
109+
ifneq ($(CPUID_SPECIFIC_BINARIES), 0)
110+
override CPPFLAGS += "-DCPUID_SPECIFIC_BINARIES=1"
111+
endif
108112

109113
FLISP_EXECUTABLE_debug := $(BUILDDIR)/flisp/flisp-debug
110114
FLISP_EXECUTABLE_release := $(BUILDDIR)/flisp/flisp

src/jloptions.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@
1111
#include "getopt.h"
1212
#endif
1313

14-
static const char system_image_path[256] = "\0" JL_SYSTEM_IMAGE_PATH;
14+
#ifdef _OS_WINDOWS_
15+
char *shlib_ext = ".dll";
16+
#elif defined(_OS_DARWIN_)
17+
char *shlib_ext = ".dylib";
18+
#else
19+
char *shlib_ext = ".so";
20+
#endif
21+
22+
static char default_sysimg_path[512];
23+
static const char *get_default_sysimg_path()
24+
{
25+
#ifndef CPUID_SPECIFIC_BINARIES
26+
snprintf(default_sysimg_path, 512, "%s%s", JL_SYSTEM_IMAGE_PATH, shlib_ext);
27+
#else
28+
snprintf(default_sysimg_path, 512, "%s_%llx%s", JL_SYSTEM_IMAGE_PATH, jl_cpuid_tag(), shlib_ext);
29+
#endif
30+
return default_sysimg_path;
31+
}
32+
1533

1634
jl_options_t jl_options = { 0, // quiet
1735
NULL, // julia_home
@@ -20,7 +38,7 @@ jl_options_t jl_options = { 0, // quiet
2038
NULL, // print
2139
NULL, // post-boot
2240
NULL, // load
23-
&system_image_path[1], // image_file
41+
NULL, // image_file (will be filled in below)
2442
NULL, // cpu_target ("native", "core2", etc...)
2543
0, // nprocs
2644
NULL, // machinefile
@@ -192,6 +210,10 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
192210
{ 0, 0, 0, 0 }
193211
};
194212

213+
// If CPUID specific binaries are enabled, this varies between runs, so initialize
214+
// it here, rather than as part of the static initialization above.
215+
jl_options.image_file = get_default_sysimg_path();
216+
195217
int codecov = JL_LOG_NONE;
196218
int malloclog= JL_LOG_NONE;
197219
// getopt handles argument parsing up to -- delineator

src/julia.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,8 @@ JL_DLLEXPORT long jl_getallocationgranularity(void);
12341234
JL_DLLEXPORT int jl_is_debugbuild(void);
12351235
JL_DLLEXPORT jl_sym_t *jl_get_UNAME(void);
12361236
JL_DLLEXPORT jl_sym_t *jl_get_ARCH(void);
1237+
JL_DLLEXPORT uint64_t jl_cpuid_tag(void);
1238+
JL_DLLEXPORT int jl_uses_cpuid_tag(void);
12371239

12381240
// environment entries
12391241
JL_DLLEXPORT jl_value_t *jl_environ(int i);

src/sys.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ JL_DLLEXPORT int jl_cpu_cores(void)
366366
#endif
367367
}
368368

369+
369370
// -- high resolution timers --
370371
// Returns time in nanosec
371372
JL_DLLEXPORT uint64_t jl_hrtime(void)
@@ -451,7 +452,24 @@ JL_DLLEXPORT void jl_cpuid(int32_t CPUInfo[4], int32_t InfoType)
451452
);
452453
#endif
453454
}
455+
JL_DLLEXPORT uint64_t jl_cpuid_tag(void)
456+
{
457+
uint32_t info[4];
458+
jl_cpuid((int32_t *)info, 1);
459+
return (((uint64_t)info[2]) | (((uint64_t)info[3]) << 32));
460+
}
461+
#elif defined(CPUID_SPECIFIC_BINARIES)
462+
#error "CPUID not available on this CPU. Turn off CPUID_SPECIFIC_BINARIES"
463+
#endif
464+
465+
JL_DLLEXPORT int jl_uses_cpuid_tag()
466+
{
467+
#ifdef CPUID_SPECIFIC_BINARIES
468+
return 1;
469+
#else
470+
return 0;
454471
#endif
472+
}
455473

456474
// -- set/clear the FZ/DAZ flags on x86 & x86-64 --
457475
#ifdef __SSE__

ui/repl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ static NOINLINE int true_main(int argc, char *argv[])
177177
return 0;
178178
}
179179

180+
extern uint64_t jl_cpuid_tag();
181+
180182
#ifndef _OS_WINDOWS_
181183
int main(int argc, char *argv[])
182184
{
@@ -241,6 +243,11 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
241243
argv[i] = (wchar_t*)arg;
242244
}
243245
#endif
246+
if (argc >= 2 && strcmp((char *)argv[1], "--cpuid") == 0) {
247+
/* Used by the build system to name CPUID-specific binaries */
248+
printf("%llx", jl_cpuid_tag());
249+
return 0;
250+
}
244251
libsupport_init();
245252
int lisp_prompt = (argc >= 2 && strcmp((char*)argv[1],"--lisp") == 0);
246253
if (lisp_prompt) {

0 commit comments

Comments
 (0)