Skip to content
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
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[alias]
xtask = "run -p xtask --bin xtask --"
compiletest = "run --release -p compiletests --"
15 changes: 15 additions & 0 deletions .github/workflows/ci_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,18 @@ jobs:
MODAL_TOKEN_SECRET: ${{ secrets.MODAL_TOKEN_SECRET }}
run: |
echo "Stubbed out"
compiletest:
name: Compile tests
runs-on: ubuntu-latest
container:
image: "ghcr.io/rust-gpu/rust-cuda-ubuntu24-cuda12:latest"
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run cargo version
run: cargo --version
- name: Rustfmt compiletests
shell: bash
run: shopt -s globstar && rustfmt --check tests/compiletests/ui/**/*.rs
- name: Compiletest
run: cargo run -p compiletests --release --no-default-features -- --target-arch compute_61,compute_70,compute_90
18 changes: 16 additions & 2 deletions .github/workflows/ci_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ jobs:
target: x86_64-pc-windows-msvc
cuda: "12.8.1"
linux-local-args: []
sub-packages: ["nvcc", "nvrtc", "nvrtc_dev", "cuda_profiler_api", "cudart", "cublas", "cublas_dev", "curand", "curand_dev"]
sub-packages:
[
"nvcc",
"nvrtc",
"nvrtc_dev",
"cuda_profiler_api",
"cudart",
"cublas",
"cublas_dev",
"curand",
"curand_dev",
]

steps:
- name: Checkout repository
Expand All @@ -41,7 +52,7 @@ jobs:
linux-local-args: ${{ toJson(matrix.linux-local-args) }}
use-local-cache: false
sub-packages: ${{ toJson(matrix.sub-packages) }}
log-file-suffix: '${{matrix.os}}-${{matrix.cuda}}'
log-file-suffix: "${{matrix.os}}-${{matrix.cuda}}"

- name: Verify CUDA installation
run: nvcc --version
Expand Down Expand Up @@ -76,3 +87,6 @@ jobs:
env:
RUSTDOCFLAGS: -Dwarnings
run: cargo doc --workspace --all-features --document-private-items --no-deps --exclude "optix*" --exclude "path-tracer" --exclude "denoiser" --exclude "vecadd*" --exclude "gemm*" --exclude "ex*" --exclude "cudnn*" --exclude "cust_raw"
# Disabled due to dll issues, someone with Windows knowledge needed
# - name: Compiletest
# run: cargo run -p compiletests --release --no-default-features -- --target-arch compute_61,compute_70,compute_90
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"examples/cuda/path_tracer/kernels",

"examples/optix/*",
"tests/compiletests",
"tests/compiletests/deps-helper",
]

exclude = [
Expand All @@ -24,3 +26,7 @@ exclude = [

[profile.dev.package.rustc_codegen_nvvm]
opt-level = 3

[workspace.dependencies]
cuda_std = { path = "crates/cuda_std" }
cuda_builder = { path = "crates/cuda_builder" }
56 changes: 51 additions & 5 deletions crates/rustc_codegen_nvvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,35 +551,81 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
}
}

#[derive(Clone)]
pub enum DisassembleMode {
All,
Function(String),
Entry(String),
Globals,
}

#[derive(Default, Clone)]
pub struct CodegenArgs {
pub nvvm_options: Vec<NvvmOption>,
pub override_libm: bool,
pub use_constant_memory_space: bool,
pub final_module_path: Option<PathBuf>,
pub disassemble: Option<DisassembleMode>,
}

impl CodegenArgs {
pub fn from_session(sess: &Session) -> Self {
Self::parse(&sess.opts.cg.llvm_args)
Self::parse(&sess.opts.cg.llvm_args, sess)
}

// we may want to use rustc's own option parsing facilities to have better errors in the future.
pub fn parse(args: &[String]) -> Self {
pub fn parse(args: &[String], sess: &Session) -> Self {
// TODO: replace this with a "proper" arg parser.
let mut cg_args = Self::default();

let mut skip_next = false;
for (idx, arg) in args.iter().enumerate() {
if skip_next {
skip_next = false;
continue;
}

if let Ok(flag) = NvvmOption::from_str(arg) {
cg_args.nvvm_options.push(flag);
} else if arg == "--override-libm" {
cg_args.override_libm = true;
} else if arg == "--use-constant-memory-space" {
cg_args.use_constant_memory_space = true;
} else if arg == "--final-module-path" {
cg_args.final_module_path = Some(PathBuf::from(
args.get(idx + 1).expect("No path for --final-module-path"),
));
let path = match args.get(idx + 1) {
Some(p) => p,
None => sess
.dcx()
.fatal("--final-module-path requires a path argument"),
};
cg_args.final_module_path = Some(PathBuf::from(path));
skip_next = true;
} else if arg == "--disassemble" {
cg_args.disassemble = Some(DisassembleMode::All);
} else if arg == "--disassemble-globals" {
cg_args.disassemble = Some(DisassembleMode::Globals);
} else if arg == "--disassemble-fn" {
let func_name = match args.get(idx + 1) {
Some(name) => name.clone(),
None => sess
.dcx()
.fatal("--disassemble-fn requires a function name argument"),
};
cg_args.disassemble = Some(DisassembleMode::Function(func_name));
skip_next = true;
} else if let Some(func) = arg.strip_prefix("--disassemble-fn=") {
cg_args.disassemble = Some(DisassembleMode::Function(func.to_string()));
} else if arg == "--disassemble-entry" {
let entry_name = match args.get(idx + 1) {
Some(name) => name.clone(),
None => sess
.dcx()
.fatal("--disassemble-entry requires an entry name argument"),
};
cg_args.disassemble = Some(DisassembleMode::Entry(entry_name));
skip_next = true;
} else if let Some(entry) = arg.strip_prefix("--disassemble-entry=") {
cg_args.disassemble = Some(DisassembleMode::Entry(entry.to_string()));
}
}

Expand Down
19 changes: 18 additions & 1 deletion crates/rustc_codegen_nvvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod lto;
mod mono_item;
mod nvvm;
mod override_fns;
mod ptx_filter;
mod target;
mod ty;

Expand Down Expand Up @@ -216,14 +217,30 @@ impl CodegenBackend for NvvmCodegenBackend {
let cmdline = sess.opts.cg.target_feature.split(',');
let cfg = sess.target.options.features.split(',');

let target_features: Vec<_> = cfg
let mut target_features: Vec<_> = cfg
.chain(cmdline)
.filter(|l| l.starts_with('+'))
.map(|l| &l[1..])
.filter(|l| !l.is_empty())
.map(rustc_span::Symbol::intern)
.collect();

// Add backend-synthesized features (e.g., hierarchical compute capabilities)
// Parse CodegenArgs to get the architecture from llvm-args
let args = context::CodegenArgs::from_session(sess);
for opt in &args.nvvm_options {
if let ::nvvm::NvvmOption::Arch(arch) = opt {
// Add all features up to and including the current architecture
let backend_features = arch.all_target_features();
target_features.extend(
backend_features
.iter()
.map(|f| rustc_span::Symbol::intern(f)),
);
break;
}
}

// For NVPTX, all target features are stable
let unstable_target_features = target_features.clone();

Expand Down
26 changes: 26 additions & 0 deletions crates/rustc_codegen_nvvm/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use tracing::{debug, trace};

use crate::LlvmMod;
use crate::context::CodegenArgs;
use crate::ptx_filter::{PtxFilter, PtxFilterConfig};

pub(crate) struct NvvmMetadataLoader;

Expand Down Expand Up @@ -305,6 +306,31 @@ fn codegen_into_ptx_file(
}
};

// If disassembly is requested, print PTX to stderr
if args.disassemble.is_some()
&& let Ok(ptx_str) = std::str::from_utf8(&ptx_bytes)
{
let config = PtxFilterConfig::from_codegen_args(&args);
let filter = PtxFilter::new(config);
let output = filter.filter(ptx_str);
if !output.is_empty() {
// Check if we're in JSON mode by checking the error format
use rustc_session::config::ErrorOutputType;
match sess.opts.error_format {
ErrorOutputType::Json { .. } => {
sess.dcx()
.err("PTX disassembly output in JSON mode is not supported");
}
_ => {
// In normal mode, just print to stderr
// Replace tabs with spaces for cleaner output
let output = output.replace('\t', " ");
eprintln!("{output}");
}
}
}
}

std::fs::write(out_filename, ptx_bytes)
}

Expand Down
Loading
Loading