Skip to content

Commit

Permalink
Completely stabilize NVCC_DEVICE_LINK. CONTRIB-150
Browse files Browse the repository at this point in the history
Fix symbols that depend on mtime and cwd.

This ensures that nm contrib/libs/nvidia/nccl/src/collectives/device/devlink.o does not change after --rebuild.

ref:810eaf8dbc93b225494ac1d081f5021f065695d4
  • Loading branch information
orivej committed Aug 1, 2019
1 parent 7e9f8ee commit f39838c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 29 deletions.
24 changes: 14 additions & 10 deletions build/scripts/compile_cuda.py
Expand Up @@ -22,7 +22,7 @@ def main():
skip_nocxxinc = False

spl = sys.argv.index('--cflags')
getpid1 = sys.argv[1]
mtime0 = sys.argv[1]
command = sys.argv[2: spl]
cflags = sys.argv[spl + 1:]

Expand Down Expand Up @@ -124,19 +124,23 @@ def good(arg):
if compiler_args:
command += ['--compiler-options', ','.join(compiler_args)]

# nvcc generates symbols like this:
# __cudaRegisterLinkedBinary_{len}_tmpxft_{pid}_00000000_6_{src}1_ii_{hash}
# They embed nvcc pid. This is the only unstable part. We stabilize it by
# preloading getpid() that always returns 1.
os.environ['LD_PRELOAD'] = getpid1
# nvcc deletes all files in TMPDIR that match tmpxft_{pid}*, even not
# created by it, so we provide a fresh TMPDIR.
os.environ['TMPDIR'] = tempfile.mkdtemp(prefix='compile_cuda.py.')
# --keep is necessary to prevent nvcc from embedding nvcc pid in generated
# symbols. It makes nvcc use the original file name as the prefix in the
# generated files (otherwise it also prepends tmpxft_{pid}_00000000-5), and
# cicc derives the module name from its {input}.cpp1.ii file name.
command += ['--keep', '--keep-dir', tempfile.mkdtemp(prefix='compile_cuda.py.')]
# nvcc generates symbols like __fatbinwrap_{len}_{basename}_{hash} where
# {basename} is {input}.cpp1.ii with non-C chars translated to _, {len} is
# {basename} length, and {hash} is the hash of first exported symbol in
# {input}.cpp1.ii if there is one, otherwise it is based on its modification
# time and the current working directory. To stabilize the names of these
# symbols we need to fix mtime and cwd.
os.environ['LD_PRELOAD'] = mtime0

if dump_args:
sys.stdout.write('\n'.join(command))
else:
sys.exit(subprocess.Popen(command, stdout=sys.stderr, stderr=sys.stderr).wait())
sys.exit(subprocess.Popen(command, stdout=sys.stderr, stderr=sys.stderr, cwd='/').wait())


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion build/ymake_conf.py
Expand Up @@ -2414,7 +2414,7 @@ def print_macros(self):
}

if not self.cuda_use_clang.value:
cmd = '$YMAKE_PYTHON ${input:"build/scripts/compile_cuda.py"} ${tool:"tools/getpid1"} $NVCC $NVCC_FLAGS -c ${input:SRC} -o ${output;suf=${OBJ_SUF}${NVCC_OBJ_EXT}:SRC} %(skip_nocxxinc)s %(includes)s --cflags $C_FLAGS_PLATFORM $CFLAGS $SRCFLAGS $CUDA_HOST_COMPILER_ENV ${kv;hide:"p CC"} ${kv;hide:"pc light-green"}'
cmd = '$YMAKE_PYTHON ${input:"build/scripts/compile_cuda.py"} ${tool:"tools/mtime0"} $NVCC $NVCC_FLAGS -c ${input:SRC} -o ${output;suf=${OBJ_SUF}${NVCC_OBJ_EXT}:SRC} %(skip_nocxxinc)s %(includes)s --cflags $C_FLAGS_PLATFORM $CFLAGS $SRCFLAGS $CUDA_HOST_COMPILER_ENV ${kv;hide:"p CC"} ${kv;hide:"pc light-green"}'
else:
cmd = '$CXX_COMPILER --cuda-path=$CUDA_ROOT $C_FLAGS_PLATFORM -c ${input:SRC} -o ${output;suf=${OBJ_SUF}${NVCC_OBJ_EXT}:SRC} %(includes)s $CXXFLAGS $SRCFLAGS $TOOLCHAIN_ENV ${kv;hide:"p CU"} ${kv;hide:"pc green"}'

Expand Down
3 changes: 0 additions & 3 deletions tools/getpid1/getpid1.c

This file was deleted.

1 change: 0 additions & 1 deletion tools/getpid1/getpid1.exports

This file was deleted.

13 changes: 0 additions & 13 deletions tools/getpid1/ya.make

This file was deleted.

17 changes: 17 additions & 0 deletions tools/mtime0/mtime0.c
@@ -0,0 +1,17 @@
// Do not alias __xstat to __xstat64.
#undef _FILE_OFFSET_BITS

#include <dlfcn.h>
#include <sys/stat.h>

int __xstat(int ver, const char* path, struct stat* buf) {
static int (*xstat)(int, const char*, struct stat*) = 0;
if (!xstat) {
xstat = dlsym(RTLD_NEXT, "__xstat");
}
int rc = xstat(ver, path, buf);
if (rc == 0 && ver == _STAT_VER) {
buf->st_mtime = 0;
}
return rc;
}
1 change: 1 addition & 0 deletions tools/mtime0/mtime0.exports
@@ -0,0 +1 @@
C __xstat
19 changes: 19 additions & 0 deletions tools/mtime0/ya.make
@@ -0,0 +1,19 @@
DLL_TOOL(mtime0 PREFIX "")



EXPORTS_SCRIPT(mtime0.exports)

NO_RUNTIME()

CFLAGS(
-fPIC
)

IF (OS_LINUX)
SRCS(
mtime0.c
)
ENDIF()

END()
2 changes: 1 addition & 1 deletion tools/ya.make
@@ -1,7 +1,7 @@
RECURSE(
enum_parser
fix_elf
getpid1
mtime0
rescompiler
rescompressor
rorescompiler
Expand Down

0 comments on commit f39838c

Please sign in to comment.