From d3903d5f9c4e58cc3fa256ec5be52717b84e6308 Mon Sep 17 00:00:00 2001 From: Denys Zariaiev Date: Sat, 19 Jan 2019 21:59:34 +0100 Subject: Create `nvptx64-nvidia-cuda` target specification --- src/librustc_codegen_ssa/back/link.rs | 1 + src/librustc_codegen_ssa/back/linker.rs | 125 ++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) (limited to 'src/librustc_codegen_ssa') diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index d03bb0a3d73..2a5ecf9a059 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -149,6 +149,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { LinkerFlavor::Ld => "ld", LinkerFlavor::Msvc => "link.exe", LinkerFlavor::Lld(_) => "lld", + LinkerFlavor::PtxLinker => "rust-ptx-linker", }), flavor)), (Some(linker), None) => { let stem = if linker.extension().and_then(|ext| ext.to_str()) == Some("exe") { diff --git a/src/librustc_codegen_ssa/back/linker.rs b/src/librustc_codegen_ssa/back/linker.rs index ad61f8f01d8..5e9aeed7107 100644 --- a/src/librustc_codegen_ssa/back/linker.rs +++ b/src/librustc_codegen_ssa/back/linker.rs @@ -83,6 +83,10 @@ impl LinkerInfo { LinkerFlavor::Lld(LldFlavor::Wasm) => { Box::new(WasmLd::new(cmd, sess, self)) as Box } + + LinkerFlavor::PtxLinker => { + Box::new(PtxLinker { cmd, sess }) as Box + } } } } @@ -1080,3 +1084,124 @@ fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec { symbols } + +/// Much simplified and explicit CLI for the NVPTX linker. The linker operates +/// with bitcode and uses LLVM backend to generate a PTX assembly. +pub struct PtxLinker<'a> { + cmd: Command, + sess: &'a Session, +} + +impl<'a> Linker for PtxLinker<'a> { + fn link_rlib(&mut self, path: &Path) { + self.cmd.arg("--rlib").arg(path); + } + + fn link_whole_rlib(&mut self, path: &Path) { + self.cmd.arg("--rlib").arg(path); + } + + fn include_path(&mut self, path: &Path) { + self.cmd.arg("-L").arg(path); + } + + fn debuginfo(&mut self) { + self.cmd.arg("--debug"); + } + + fn add_object(&mut self, path: &Path) { + self.cmd.arg("--bitcode").arg(path); + } + + fn args(&mut self, args: &[String]) { + self.cmd.args(args); + } + + fn optimize(&mut self) { + self.cmd.arg(match self.sess.opts.optimize { + OptLevel::No => "-O0", + OptLevel::Less => "-O1", + OptLevel::Default => "-O2", + OptLevel::Aggressive => "-O3", + OptLevel::Size => "-Os", + OptLevel::SizeMin => "-Os" + }); + } + + fn output_filename(&mut self, path: &Path) { + self.cmd.arg("-o").arg(path); + } + + fn finalize(&mut self) -> Command { + ::std::mem::replace(&mut self.cmd, Command::new("")) + } + + fn link_dylib(&mut self, _lib: &str) { + panic!("external dylibs not supported") + } + + fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) { + panic!("external dylibs not supported") + } + + fn link_staticlib(&mut self, _lib: &str) { + panic!("staticlibs not supported") + } + + fn link_whole_staticlib(&mut self, _lib: &str, _search_path: &[PathBuf]) { + panic!("staticlibs not supported") + } + + fn framework_path(&mut self, _path: &Path) { + panic!("frameworks not supported") + } + + fn link_framework(&mut self, _framework: &str) { + panic!("frameworks not supported") + } + + fn position_independent_executable(&mut self) { + } + + fn full_relro(&mut self) { + } + + fn partial_relro(&mut self) { + } + + fn no_relro(&mut self) { + } + + fn build_static_executable(&mut self) { + } + + fn gc_sections(&mut self, _keep_metadata: bool) { + } + + fn pgo_gen(&mut self) { + } + + fn no_default_libraries(&mut self) { + } + + fn build_dylib(&mut self, _out_filename: &Path) { + } + + fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) { + } + + fn subsystem(&mut self, _subsystem: &str) { + } + + fn no_position_independent_executable(&mut self) { + } + + fn group_start(&mut self) { + } + + fn group_end(&mut self) { + } + + fn cross_lang_lto(&mut self) { + } +} -- cgit 1.4.1-3-g733a5 From 6f86a70ea13cbe8b32c6e6ed76b8f57be4d70c68 Mon Sep 17 00:00:00 2001 From: Denys Zariaiev Date: Mon, 28 Jan 2019 23:56:37 +0100 Subject: Adjust PTXLinker LTO logic and CLI --- src/ci/docker/nvptx-cuda/Dockerfile | 2 +- src/librustc_codegen_ssa/back/linker.rs | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) (limited to 'src/librustc_codegen_ssa') diff --git a/src/ci/docker/nvptx-cuda/Dockerfile b/src/ci/docker/nvptx-cuda/Dockerfile index b52865ced3e..c7c3ca6bc54 100644 --- a/src/ci/docker/nvptx-cuda/Dockerfile +++ b/src/ci/docker/nvptx-cuda/Dockerfile @@ -6,7 +6,7 @@ RUN apt-get install -y --no-install-recommends \ cmake sudo gdb # FIXME: setup `ptx-linker` CI for automatic binary releases. -RUN curl -sL https://github.com/denzp/rust-ptx-linker/releases/download/v0.9.0-alpha/rust-ptx-linker.linux64.tar.gz | \ +RUN curl -sL https://github.com/denzp/rust-ptx-linker/releases/download/v0.9.0-alpha.1/rust-ptx-linker.linux64.tar.gz | \ tar -xzvC /usr/bin COPY scripts/sccache.sh /scripts/ diff --git a/src/librustc_codegen_ssa/back/linker.rs b/src/librustc_codegen_ssa/back/linker.rs index 5e9aeed7107..55b02ebe6c4 100644 --- a/src/librustc_codegen_ssa/back/linker.rs +++ b/src/librustc_codegen_ssa/back/linker.rs @@ -13,7 +13,7 @@ use rustc::hir::def_id::{LOCAL_CRATE, CrateNum}; use rustc::middle::dependency_format::Linkage; use rustc::session::Session; use rustc::session::config::{self, CrateType, OptLevel, DebugInfo, - CrossLangLto}; + CrossLangLto, Lto}; use rustc::ty::TyCtxt; use rustc_target::spec::{LinkerFlavor, LldFlavor}; use serialize::{json, Encoder}; @@ -1118,14 +1118,13 @@ impl<'a> Linker for PtxLinker<'a> { } fn optimize(&mut self) { - self.cmd.arg(match self.sess.opts.optimize { - OptLevel::No => "-O0", - OptLevel::Less => "-O1", - OptLevel::Default => "-O2", - OptLevel::Aggressive => "-O3", - OptLevel::Size => "-Os", - OptLevel::SizeMin => "-Os" - }); + match self.sess.lto() { + Lto::Thin | Lto::Fat | Lto::ThinLocal => { + self.cmd.arg("-Olto"); + }, + + Lto::No => { }, + }; } fn output_filename(&mut self, path: &Path) { -- cgit 1.4.1-3-g733a5 From 3f624456e6e284f918d4e189e766e357911b49e2 Mon Sep 17 00:00:00 2001 From: Denys Zariaiev Date: Tue, 29 Jan 2019 20:54:23 +0100 Subject: Provide PTXLinker with fallback to internal `target-cpu` --- src/ci/docker/test-various/Dockerfile | 2 +- src/librustc_codegen_ssa/back/linker.rs | 6 ++++++ src/test/run-make/nvptx-binary-crate/Makefile | 7 +++++-- src/test/run-make/nvptx-dylib-crate/kernel.rs | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src/librustc_codegen_ssa') diff --git a/src/ci/docker/test-various/Dockerfile b/src/ci/docker/test-various/Dockerfile index a5ae94262c1..6c419e13c9f 100644 --- a/src/ci/docker/test-various/Dockerfile +++ b/src/ci/docker/test-various/Dockerfile @@ -14,7 +14,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ xz-utils # FIXME: build the `ptx-linker` instead. -RUN curl -sL https://github.com/denzp/rust-ptx-linker/releases/download/v0.9.0-alpha.1/rust-ptx-linker.linux64.tar.gz | \ +RUN curl -sL https://github.com/denzp/rust-ptx-linker/releases/download/v0.9.0-alpha.2/rust-ptx-linker.linux64.tar.gz | \ tar -xzvC /usr/bin RUN curl -sL https://nodejs.org/dist/v9.2.0/node-v9.2.0-linux-x64.tar.xz | \ diff --git a/src/librustc_codegen_ssa/back/linker.rs b/src/librustc_codegen_ssa/back/linker.rs index 55b02ebe6c4..249715a7b6e 100644 --- a/src/librustc_codegen_ssa/back/linker.rs +++ b/src/librustc_codegen_ssa/back/linker.rs @@ -1132,6 +1132,12 @@ impl<'a> Linker for PtxLinker<'a> { } fn finalize(&mut self) -> Command { + // Provide the linker with fallback to internal `target-cpu`. + self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu { + Some(ref s) => s, + None => &self.sess.target.target.options.cpu + }); + ::std::mem::replace(&mut self.cmd, Command::new("")) } diff --git a/src/test/run-make/nvptx-binary-crate/Makefile b/src/test/run-make/nvptx-binary-crate/Makefile index 4c22dae265c..2c211b5c785 100644 --- a/src/test/run-make/nvptx-binary-crate/Makefile +++ b/src/test/run-make/nvptx-binary-crate/Makefile @@ -2,8 +2,11 @@ ifeq ($(TARGET),nvptx64-nvidia-cuda) all: - $(RUSTC) main.rs -Clink-arg=--arch=sm_60 --crate-type="bin" -O --target $(TARGET) - FileCheck main.rs --input-file $(TMPDIR)/main.ptx + $(RUSTC) main.rs --crate-type="bin" --target $(TARGET) -O -C link-arg=--arch=sm_60 -o $(TMPDIR)/main.link_arg.ptx + $(RUSTC) main.rs --crate-type="bin" --target $(TARGET) -O -C target-cpu=sm_60 -o $(TMPDIR)/main.target_cpu.ptx + + FileCheck main.rs --input-file $(TMPDIR)/main.link_arg.ptx + FileCheck main.rs --input-file $(TMPDIR)/main.target_cpu.ptx else all: endif diff --git a/src/test/run-make/nvptx-dylib-crate/kernel.rs b/src/test/run-make/nvptx-dylib-crate/kernel.rs index 5e65cca9140..63fd6b063dd 100644 --- a/src/test/run-make/nvptx-dylib-crate/kernel.rs +++ b/src/test/run-make/nvptx-dylib-crate/kernel.rs @@ -5,7 +5,7 @@ extern crate dep; // Verify the default CUDA arch. -// CHECK: .target sm_20 +// CHECK: .target sm_30 // CHECK: .address_size 64 // Make sure declarations are there. -- cgit 1.4.1-3-g733a5