Skip to main content

singe_cuda/nvrtc/
options.rs

1use std::{
2    fmt::Display,
3    sync::{Arc, atomic::AtomicBool},
4};
5
6use singe_core::impl_enum_display;
7
8use crate::architecture::GpuArchitecture;
9
10/// Preprocessor macro definition emitted as an NVRTC `--define-macro` option.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum MacroDefinition<'a> {
14    /// Defines a macro without an explicit replacement value.
15    Name(
16        /// Preprocessor macro name.
17        &'a str,
18    ),
19    /// Defines a macro with an explicit replacement value.
20    WithValue {
21        /// Preprocessor macro name.
22        name: &'a str,
23        /// Macro replacement text.
24        value: &'a str,
25    },
26}
27
28impl MacroDefinition<'_> {
29    fn format(self) -> String {
30        match self {
31            Self::Name(name) => name.to_string(),
32            Self::WithValue { name, value } => format!("{name}={value}"),
33        }
34    }
35}
36
37/// C++ language standard passed to NVRTC.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39#[non_exhaustive]
40pub enum CppDialect {
41    /// Selects the C++03 language standard.
42    Cpp03,
43    /// Selects the C++11 language standard.
44    Cpp11,
45    /// Selects the C++14 language standard.
46    Cpp14,
47    /// Selects the C++17 language standard.
48    Cpp17,
49    /// Selects the C++20 language standard.
50    Cpp20,
51}
52
53impl_enum_display!(CppDialect, {
54    Self::Cpp03 => "c++03",
55    Self::Cpp11 => "c++11",
56    Self::Cpp14 => "c++14",
57    Self::Cpp17 => "c++17",
58    Self::Cpp20 => "c++20",
59});
60
61/// Amount of optimization disabled to improve device compilation speed.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
63#[non_exhaustive]
64pub enum FastCompileLevel {
65    /// Disables fast-compile mode.
66    Zero,
67    /// Disables a limited set of expensive optimizations to reduce compilation time.
68    Min,
69    /// Disables more expensive optimizations to balance compilation time and runtime performance.
70    Mid,
71    /// Prioritizes compilation speed by disabling many optimizations.
72    Max,
73}
74
75impl_enum_display!(FastCompileLevel, {
76    Self::Zero => "0",
77    Self::Min => "min",
78    Self::Mid => "mid",
79    Self::Max => "max",
80});
81
82/// Native warning category promoted to a compilation error.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
84#[non_exhaustive]
85pub enum WarningAsErrorKind {
86    /// Promotes all warnings to errors.
87    AllWarnings,
88    /// Promotes constructor initialization-order warnings to errors.
89    Reorder,
90    /// Promotes deprecated-declaration warnings to errors.
91    DeprecatedDeclarations,
92}
93
94impl_enum_display!(WarningAsErrorKind, {
95    Self::AllWarnings => "all-warnings",
96    Self::Reorder => "reorder",
97    Self::DeprecatedDeclarations => "deprecated-declarations",
98});
99
100/// Category of native compiler optimization reports.
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102#[non_exhaustive]
103pub enum OptimizationInfoKind {
104    /// Requests inlining optimization reports.
105    Inline,
106}
107
108impl_enum_display!(OptimizationInfoKind, {
109    Self::Inline => "inline",
110});
111
112/// Controls compilation of Tile constructs such as `__tile__` and `__tile_global__`.
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub enum TileCompilation {
115    /// Enable support for Tile constructs and define the macro `__CUDACC_TILE__`.
116    Enabled,
117    /// Enable Tile parsing, but omit code generation for non-Tile code.
118    TileOnly,
119    /// Enable Tile parsing, but omit code generation for Tile code.
120    SimtOnly,
121}
122
123/// Typed NVRTC compilation options.
124/// Unset optional values and disabled flag switches leave the corresponding native option unspecified.
125/// String lists preserve insertion order; `raw_options` are appended last.
126/// Validation occurs when the program is materialized or compiled.
127#[derive(Debug, Clone, Default)]
128#[expect(
129    clippy::struct_excessive_bools,
130    reason = "independent capabilities and option switches can be combined freely"
131)]
132pub struct CompileOptions<'a> {
133    /// GPU architecture target used for PTX or machine-code generation.
134    pub gpu_architecture: Option<GpuArchitecture>,
135    /// Whether to generate relocatable device code for subsequent linking.
136    pub relocatable_device_code: Option<bool>,
137    /// Enables extensible whole-program compilation.
138    pub extensible_whole_program: bool,
139    /// Generates device debug information.
140    pub device_debug: bool,
141    /// Generates source line information for device code.
142    pub generate_line_info: bool,
143    /// Emits `--dopt=on` only for `Some(true)`.
144    /// `None` and `Some(false)` leave the native default in effect.
145    pub device_optimization: Option<bool>,
146    /// Compilation-speed setting that trades generated-code optimization for shorter compilation.
147    pub fast_compile: Option<FastCompileLevel>,
148    /// Additional PTX assembler option strings, each emitted as a separate `--ptxas-options` argument.
149    pub ptxas_options: Vec<&'a str>,
150    /// Requested maximum register count per GPU thread, subject to native ABI minimums.
151    pub max_register_count: Option<i32>,
152    /// Whether single-precision denormal results are flushed to zero.
153    pub flush_to_zero: Option<bool>,
154    /// Whether single-precision square roots use the precise native implementation.
155    pub precise_square_root: Option<bool>,
156    /// Whether single-precision division uses the precise native implementation.
157    pub precise_division: Option<bool>,
158    /// Whether floating-point multiply and add operations may be contracted.
159    pub fmad: Option<bool>,
160    /// Enables the native fast-math preset.
161    pub use_fast_math: bool,
162    /// Enables additional device-code vectorization.
163    pub extra_device_vectorization: bool,
164    /// Whether NVRTC may temporarily raise the process-wide stack limit during compilation on Linux.
165    pub modify_stack_limit: Option<bool>,
166    /// Generates intermediate code for device link-time optimization.
167    pub dlink_time_optimization: bool,
168    /// Requests optimized link-time intermediate code.
169    pub generate_optimized_lto: bool,
170    /// Requests `OptiX` intermediate representation output.
171    pub optix_ir: bool,
172    /// Enable support for Tile constructs and control which code is generated.
173    pub tile_compilation: Option<TileCompilation>,
174    /// Consider unannotated functions and static-storage variables implicitly `__tile__`.
175    pub default_tile: bool,
176    /// Diagnose variables that cannot be implicitly annotated with `__tile__`.
177    pub diagnose_implicit_tile_variable: bool,
178    /// Case-density percentage threshold for generating a switch jump table.
179    pub jump_table_density: Option<u8>,
180    /// Disables native caching for both PTX and cubin generation.
181    pub no_cache: bool,
182    /// Seed string used for compiler-generated symbol and variable names.
183    pub random_seed: Option<&'a str>,
184    /// Preprocessor macro definitions emitted in insertion order.
185    pub define_macros: Vec<MacroDefinition<'a>>,
186    /// Preprocessor macro names to undefine.
187    pub undefine_macros: Vec<&'a str>,
188    /// Additional header search directories.
189    pub include_paths: Vec<&'a str>,
190    /// Headers included before processing the source.
191    pub pre_include_headers: Vec<&'a str>,
192    /// Prevents adding the source file directory to the header search path.
193    pub no_source_include: bool,
194    /// C++ language standard used for compilation.
195    pub cpp_dialect: Option<CppDialect>,
196    /// Whether NVRTC supplies built-in definitions of `std::move` and `std::forward`.
197    pub builtin_move_forward: Option<bool>,
198    /// Whether NVRTC supplies a built-in definition of `std::initializer_list`.
199    pub builtin_initializer_list: Option<bool>,
200    /// Enables automatic precompiled-header processing.
201    pub pch: bool,
202    /// Path of an explicitly created precompiled-header file.
203    pub create_pch: Option<&'a str>,
204    /// Path of an existing precompiled-header file to use.
205    pub use_pch: Option<&'a str>,
206    /// Directory used for automatic precompiled-header files.
207    pub pch_dir: Option<&'a str>,
208    /// Whether to emit verbose precompiled-header diagnostics.
209    pub pch_verbose: Option<bool>,
210    /// Whether to emit precompiled-header processing messages.
211    pub pch_messages: Option<bool>,
212    /// Whether to instantiate templates while creating a precompiled header.
213    pub instantiate_templates_in_pch: Option<bool>,
214    /// Suppresses native compiler warnings.
215    pub disable_warnings: bool,
216    /// Warning categories promoted to compilation errors.
217    pub warning_as_error: Vec<WarningAsErrorKind>,
218    /// Treats kernel pointer parameters as restricted pointers.
219    pub restrict_pointers: bool,
220    /// Treats unannotated functions as device functions.
221    pub device_as_default_execution_space: bool,
222    /// Enables device support for 128-bit integers.
223    pub device_int128: bool,
224    /// Enables device support for 128-bit floating-point types.
225    pub device_float128: bool,
226    /// Categories of optimization reports to request.
227    pub optimization_info: Vec<OptimizationInfoKind>,
228    /// Whether diagnostics display their numeric identifiers.
229    pub display_error_number: Option<bool>,
230    /// Diagnostic numbers promoted to errors.
231    pub diag_error: Vec<i32>,
232    /// Diagnostic numbers to suppress.
233    pub diag_suppress: Vec<i32>,
234    /// Diagnostic numbers demoted to warnings.
235    pub diag_warn: Vec<i32>,
236    /// Whether to use brief diagnostic output.
237    pub brief_diagnostics: Option<bool>,
238    /// File for compiler phase timing output.
239    pub time: Option<&'a str>,
240    /// Maximum number of threads used by parallel compilation.
241    pub split_compile: Option<i32>,
242    /// Checks device syntax without generating output code.
243    pub device_syntax_only: bool,
244    /// Omits texture, surface, device-runtime, device-launch, and runtime/driver type features to reduce compilation time.
245    pub minimal: bool,
246    /// Whether to enable device stack protection.
247    pub device_stack_protector: Option<bool>,
248    /// File for the device compilation time trace.
249    pub device_time_trace: Option<&'a str>,
250    /// Additional native option strings appended after the typed options.
251    pub raw_options: Vec<&'a str>,
252    /// Flag another thread sets to true to cancel compilation; it must stay true until compilation returns.
253    /// It is not rendered as an argument.
254    pub cancel_flag: Option<Arc<AtomicBool>>,
255}
256
257impl<'a> CompileOptions<'a> {
258    /// Creates an empty option set that leaves native defaults in effect.
259    #[must_use]
260    pub const fn new() -> Self {
261        Self {
262            gpu_architecture: None,
263            relocatable_device_code: None,
264            extensible_whole_program: false,
265            device_debug: false,
266            generate_line_info: false,
267            device_optimization: None,
268            fast_compile: None,
269            ptxas_options: Vec::new(),
270            max_register_count: None,
271            flush_to_zero: None,
272            precise_square_root: None,
273            precise_division: None,
274            fmad: None,
275            use_fast_math: false,
276            extra_device_vectorization: false,
277            modify_stack_limit: None,
278            dlink_time_optimization: false,
279            generate_optimized_lto: false,
280            optix_ir: false,
281            tile_compilation: None,
282            default_tile: false,
283            diagnose_implicit_tile_variable: false,
284            jump_table_density: None,
285            no_cache: false,
286            random_seed: None,
287            define_macros: Vec::new(),
288            undefine_macros: Vec::new(),
289            include_paths: Vec::new(),
290            pre_include_headers: Vec::new(),
291            no_source_include: false,
292            cpp_dialect: None,
293            builtin_move_forward: None,
294            builtin_initializer_list: None,
295            pch: false,
296            create_pch: None,
297            use_pch: None,
298            pch_dir: None,
299            pch_verbose: None,
300            pch_messages: None,
301            instantiate_templates_in_pch: None,
302            disable_warnings: false,
303            warning_as_error: Vec::new(),
304            restrict_pointers: false,
305            device_as_default_execution_space: false,
306            device_int128: false,
307            device_float128: false,
308            optimization_info: Vec::new(),
309            display_error_number: None,
310            diag_error: Vec::new(),
311            diag_suppress: Vec::new(),
312            diag_warn: Vec::new(),
313            brief_diagnostics: None,
314            time: None,
315            split_compile: None,
316            device_syntax_only: false,
317            minimal: false,
318            device_stack_protector: None,
319            device_time_trace: None,
320            raw_options: Vec::new(),
321            cancel_flag: None,
322        }
323    }
324
325    /// Sets the GPU architecture target for PTX or machine-code generation.
326    #[must_use]
327    pub fn gpu_architecture(mut self, value: GpuArchitecture) -> Self {
328        self.gpu_architecture = Some(value);
329        self
330    }
331
332    /// Enables or disables relocatable device code for subsequent linking.
333    #[must_use]
334    pub fn relocatable_device_code(mut self, value: bool) -> Self {
335        self.relocatable_device_code = Some(value);
336        self
337    }
338
339    /// Enables or disables extensible whole-program compilation.
340    #[must_use]
341    pub fn extensible_whole_program(mut self, value: bool) -> Self {
342        self.extensible_whole_program = value;
343        self
344    }
345
346    /// Enables or disables device debug information.
347    #[must_use]
348    pub fn device_debug(mut self, value: bool) -> Self {
349        self.device_debug = value;
350        self
351    }
352
353    /// Enables or disables device source line information.
354    #[must_use]
355    pub fn generate_line_info(mut self, value: bool) -> Self {
356        self.generate_line_info = value;
357        self
358    }
359
360    /// Requests `--dopt=on` when `value` is true.
361    /// False leaves the native optimization default in effect.
362    #[must_use]
363    pub fn device_optimization(mut self, value: bool) -> Self {
364        self.device_optimization = Some(value);
365        self
366    }
367
368    /// Sets the compilation-speed tradeoff between optimization effort and generated-code performance.
369    #[must_use]
370    pub fn fast_compile(mut self, value: FastCompileLevel) -> Self {
371        self.fast_compile = Some(value);
372        self
373    }
374
375    /// Appends a PTX assembler option string as a separate `--ptxas-options` argument.
376    #[must_use]
377    pub fn ptxas_option(mut self, value: &'a str) -> Self {
378        self.ptxas_options.push(value);
379        self
380    }
381
382    /// Sets the requested maximum register count per GPU thread, subject to native ABI minimums.
383    #[must_use]
384    pub fn max_register_count(mut self, value: i32) -> Self {
385        self.max_register_count = Some(value);
386        self
387    }
388
389    /// Controls whether single-precision denormal values are flushed to zero.
390    #[must_use]
391    pub fn flush_to_zero(mut self, value: bool) -> Self {
392        self.flush_to_zero = Some(value);
393        self
394    }
395
396    /// Selects precise or approximate single-precision square roots.
397    #[must_use]
398    pub fn precise_square_root(mut self, value: bool) -> Self {
399        self.precise_square_root = Some(value);
400        self
401    }
402
403    /// Selects precise or approximate single-precision division.
404    #[must_use]
405    pub fn precise_division(mut self, value: bool) -> Self {
406        self.precise_division = Some(value);
407        self
408    }
409
410    /// Controls contraction of floating-point multiply and add operations.
411    #[must_use]
412    pub fn fmad(mut self, value: bool) -> Self {
413        self.fmad = Some(value);
414        self
415    }
416
417    /// Enables or disables the native fast-math preset.
418    #[must_use]
419    pub fn use_fast_math(mut self, value: bool) -> Self {
420        self.use_fast_math = value;
421        self
422    }
423
424    /// Enables or disables additional device-code vectorization.
425    #[must_use]
426    pub fn extra_device_vectorization(mut self, value: bool) -> Self {
427        self.extra_device_vectorization = value;
428        self
429    }
430
431    /// Controls whether NVRTC may temporarily raise the process-wide stack limit during compilation on Linux.
432    #[must_use]
433    pub fn modify_stack_limit(mut self, value: bool) -> Self {
434        self.modify_stack_limit = Some(value);
435        self
436    }
437
438    /// Enables or disables device link-time intermediate-code generation.
439    #[must_use]
440    pub fn dlink_time_optimization(mut self, value: bool) -> Self {
441        self.dlink_time_optimization = value;
442        self
443    }
444
445    /// Enables or disables generation of optimized link-time intermediate code.
446    #[must_use]
447    pub fn generate_optimized_lto(mut self, value: bool) -> Self {
448        self.generate_optimized_lto = value;
449        self
450    }
451
452    /// Enables or disables `OptiX` intermediate representation output.
453    #[must_use]
454    pub fn optix_ir(mut self, value: bool) -> Self {
455        self.optix_ir = value;
456        self
457    }
458
459    /// Sets the case-density percentage threshold for generating a switch jump table.
460    #[must_use]
461    pub fn jump_table_density(mut self, value: u8) -> Self {
462        self.jump_table_density = Some(value.min(101));
463        self
464    }
465
466    /// Controls whether native PTX and cubin generation caches are disabled.
467    #[must_use]
468    pub fn no_cache(mut self, value: bool) -> Self {
469        self.no_cache = value;
470        self
471    }
472
473    /// Sets the seed string used for compiler-generated symbol and variable names.
474    #[must_use]
475    pub fn random_seed(mut self, value: &'a str) -> Self {
476        self.random_seed = Some(value);
477        self
478    }
479
480    /// Appends a preprocessor macro definition.
481    #[must_use]
482    pub fn define_macro(mut self, value: MacroDefinition<'a>) -> Self {
483        self.define_macros.push(value);
484        self
485    }
486
487    /// Appends a preprocessor macro name to undefine.
488    #[must_use]
489    pub fn undefine_macro(mut self, value: &'a str) -> Self {
490        self.undefine_macros.push(value);
491        self
492    }
493
494    /// Appends a header search directory.
495    #[must_use]
496    pub fn include_path(mut self, value: &'a str) -> Self {
497        self.include_paths.push(value);
498        self
499    }
500
501    /// Appends a header to include before processing the source.
502    #[must_use]
503    pub fn pre_include_header(mut self, value: &'a str) -> Self {
504        self.pre_include_headers.push(value);
505        self
506    }
507
508    /// Controls whether the source file directory is omitted from the header search path.
509    #[must_use]
510    pub fn no_source_include(mut self, value: bool) -> Self {
511        self.no_source_include = value;
512        self
513    }
514
515    /// Sets the C++ language standard.
516    #[must_use]
517    pub fn cpp_dialect(mut self, value: CppDialect) -> Self {
518        self.cpp_dialect = Some(value);
519        self
520    }
521
522    /// Controls whether NVRTC supplies built-in `std::move` and `std::forward` definitions.
523    #[must_use]
524    pub fn builtin_move_forward(mut self, value: bool) -> Self {
525        self.builtin_move_forward = Some(value);
526        self
527    }
528
529    /// Controls whether NVRTC supplies a built-in `std::initializer_list` definition.
530    #[must_use]
531    pub fn builtin_initializer_list(mut self, value: bool) -> Self {
532        self.builtin_initializer_list = Some(value);
533        self
534    }
535
536    /// Enables or disables automatic precompiled-header processing.
537    #[must_use]
538    pub fn pch(mut self, value: bool) -> Self {
539        self.pch = value;
540        self
541    }
542
543    /// Sets the path of a precompiled-header file to create explicitly.
544    #[must_use]
545    pub fn create_pch(mut self, value: &'a str) -> Self {
546        self.create_pch = Some(value);
547        self
548    }
549
550    /// Sets the path of an existing precompiled-header file to use.
551    #[must_use]
552    pub fn use_pch(mut self, value: &'a str) -> Self {
553        self.use_pch = Some(value);
554        self
555    }
556
557    /// Sets the directory used for automatic precompiled-header files.
558    #[must_use]
559    pub fn pch_dir(mut self, value: &'a str) -> Self {
560        self.pch_dir = Some(value);
561        self
562    }
563
564    /// Controls whether the log explains why candidate automatic precompiled headers could not be used.
565    #[must_use]
566    pub fn pch_verbose(mut self, value: bool) -> Self {
567        self.pch_verbose = Some(value);
568        self
569    }
570
571    /// Controls whether the log reports creating or using a precompiled header.
572    #[must_use]
573    pub fn pch_messages(mut self, value: bool) -> Self {
574        self.pch_messages = Some(value);
575        self
576    }
577
578    /// Controls template instantiation while creating a precompiled header.
579    #[must_use]
580    pub fn instantiate_templates_in_pch(mut self, value: bool) -> Self {
581        self.instantiate_templates_in_pch = Some(value);
582        self
583    }
584
585    /// Controls suppression of native compiler warnings.
586    #[must_use]
587    pub fn disable_warnings(mut self, value: bool) -> Self {
588        self.disable_warnings = value;
589        self
590    }
591
592    /// Appends a warning category to promote to compilation errors.
593    #[must_use]
594    pub fn warning_as_error(mut self, value: WarningAsErrorKind) -> Self {
595        self.warning_as_error.push(value);
596        self
597    }
598
599    /// Controls whether kernel pointer parameters are treated as restricted pointers.
600    #[must_use]
601    pub fn restrict_pointers(mut self, value: bool) -> Self {
602        self.restrict_pointers = value;
603        self
604    }
605
606    /// Controls whether unannotated functions are treated as device functions.
607    #[must_use]
608    pub fn device_as_default_execution_space(mut self, value: bool) -> Self {
609        self.device_as_default_execution_space = value;
610        self
611    }
612
613    /// Enables or disables device support for 128-bit integer types.
614    #[must_use]
615    pub fn device_int128(mut self, value: bool) -> Self {
616        self.device_int128 = value;
617        self
618    }
619
620    /// Enables or disables device support for 128-bit floating-point types.
621    #[must_use]
622    pub fn device_float128(mut self, value: bool) -> Self {
623        self.device_float128 = value;
624        self
625    }
626
627    /// Appends a category of optimization reports to request.
628    #[must_use]
629    pub fn optimization_info(mut self, value: OptimizationInfoKind) -> Self {
630        self.optimization_info.push(value);
631        self
632    }
633
634    /// Controls whether diagnostics display their numeric identifiers.
635    #[must_use]
636    pub fn display_error_number(mut self, value: bool) -> Self {
637        self.display_error_number = Some(value);
638        self
639    }
640
641    /// Appends a diagnostic number to promote to an error.
642    #[must_use]
643    pub fn diag_error(mut self, value: i32) -> Self {
644        self.diag_error.push(value);
645        self
646    }
647
648    /// Appends a diagnostic number to suppress.
649    #[must_use]
650    pub fn diag_suppress(mut self, value: i32) -> Self {
651        self.diag_suppress.push(value);
652        self
653    }
654
655    /// Appends a diagnostic number to demote to a warning.
656    #[must_use]
657    pub fn diag_warn(mut self, value: i32) -> Self {
658        self.diag_warn.push(value);
659        self
660    }
661
662    /// Controls whether to use brief diagnostic output.
663    #[must_use]
664    pub fn brief_diagnostics(mut self, value: bool) -> Self {
665        self.brief_diagnostics = Some(value);
666        self
667    }
668
669    /// Sets the file for compiler phase timing output.
670    #[must_use]
671    pub fn time(mut self, value: &'a str) -> Self {
672        self.time = Some(value);
673        self
674    }
675
676    /// Sets the maximum number of parallel optimization threads.
677    /// Zero uses the available system threads; one disables splitting.
678    #[must_use]
679    pub fn split_compile(mut self, value: i32) -> Self {
680        self.split_compile = Some(value);
681        self
682    }
683
684    /// Enables or disables syntax-only device compilation without valid output code.
685    #[must_use]
686    pub fn device_syntax_only(mut self, value: bool) -> Self {
687        self.device_syntax_only = value;
688        self
689    }
690
691    /// Enables or disables minimal compilation, which omits texture, surface, device-runtime, device-launch, and runtime/driver type features.
692    #[must_use]
693    pub fn minimal(mut self, value: bool) -> Self {
694        self.minimal = value;
695        self
696    }
697
698    /// Enables or disables device stack protection.
699    #[must_use]
700    pub fn device_stack_protector(mut self, value: bool) -> Self {
701        self.device_stack_protector = Some(value);
702        self
703    }
704
705    /// Sets the file for the device compilation time trace.
706    #[must_use]
707    pub fn device_time_trace(mut self, value: &'a str) -> Self {
708        self.device_time_trace = Some(value);
709        self
710    }
711
712    /// Appends a native option string after the typed options.
713    #[must_use]
714    pub fn raw_option(mut self, value: &'a str) -> Self {
715        self.raw_options.push(value);
716        self
717    }
718
719    /// Sets the flag another thread sets to true to cancel compilation.
720    #[must_use]
721    pub fn cancel_flag(mut self, flag: Arc<AtomicBool>) -> Self {
722        self.cancel_flag = Some(flag);
723        self
724    }
725
726    /// Renders configured options as owned NVRTC command-line argument strings.
727    /// This performs no native compilation or option validation.
728    #[must_use]
729    pub fn as_arguments(&self) -> Vec<String> {
730        let mut arguments = Vec::new();
731
732        if let Some(value) = self.gpu_architecture {
733            arguments.push(format!("--gpu-architecture={value}"));
734        }
735
736        if let Some(value) = self.relocatable_device_code {
737            arguments.push(format!("--relocatable-device-code={}", bool_flag(value)));
738        }
739
740        if self.extensible_whole_program {
741            arguments.push(String::from("--extensible-whole-program"));
742        }
743
744        if self.device_debug {
745            arguments.push(String::from("--device-debug"));
746        }
747
748        if self.generate_line_info {
749            arguments.push(String::from("--generate-line-info"));
750        }
751
752        if let Some(value) = self.device_optimization
753            && value
754        {
755            arguments.push(String::from("--dopt=on"));
756        }
757
758        if let Some(value) = self.fast_compile {
759            arguments.push(format!("--Ofast-compile={value}"));
760        }
761
762        arguments.extend(
763            self.ptxas_options
764                .iter()
765                .map(|value| format!("--ptxas-options={value}")),
766        );
767        if let Some(value) = self.max_register_count {
768            arguments.push(format!("--maxrregcount={value}"));
769        }
770
771        if let Some(value) = self.flush_to_zero {
772            arguments.push(format!("--ftz={}", bool_flag(value)));
773        }
774
775        if let Some(value) = self.precise_square_root {
776            arguments.push(format!("--prec-sqrt={}", bool_flag(value)));
777        }
778
779        if let Some(value) = self.precise_division {
780            arguments.push(format!("--prec-div={}", bool_flag(value)));
781        }
782
783        if let Some(value) = self.fmad {
784            arguments.push(format!("--fmad={}", bool_flag(value)));
785        }
786
787        if self.use_fast_math {
788            arguments.push(String::from("--use_fast_math"));
789        }
790
791        if self.extra_device_vectorization {
792            arguments.push(String::from("--extra-device-vectorization"));
793        }
794
795        if let Some(value) = self.modify_stack_limit {
796            arguments.push(format!("--modify-stack-limit={}", bool_flag(value)));
797        }
798
799        if self.dlink_time_optimization {
800            arguments.push(String::from("--dlink-time-opt"));
801        }
802
803        if self.generate_optimized_lto {
804            arguments.push(String::from("--gen-opt-lto"));
805        }
806
807        if self.optix_ir {
808            arguments.push(String::from("--optix-ir"));
809        }
810
811        if let Some(value) = self.jump_table_density {
812            arguments.push(format!("--jump-table-density={value}"));
813        }
814
815        if self.no_cache {
816            arguments.push(String::from("--no-cache"));
817        }
818
819        if let Some(value) = self.random_seed {
820            arguments.push(format!("--frandom-seed={value}"));
821        }
822
823        arguments.extend(
824            self.define_macros
825                .iter()
826                .copied()
827                .map(|value| format!("--define-macro={}", value.format())),
828        );
829        arguments.extend(
830            self.undefine_macros
831                .iter()
832                .map(|value| format!("--undefine-macro={value}")),
833        );
834        arguments.extend(
835            self.include_paths
836                .iter()
837                .map(|value| format!("--include-path={value}")),
838        );
839        arguments.extend(
840            self.pre_include_headers
841                .iter()
842                .map(|value| format!("--pre-include={value}")),
843        );
844        if self.no_source_include {
845            arguments.push(String::from("--no-source-include"));
846        }
847
848        if let Some(value) = self.cpp_dialect {
849            arguments.push(format!("--std={value}"));
850        }
851
852        if let Some(value) = self.builtin_move_forward {
853            arguments.push(format!("--builtin-move-forward={}", bool_flag(value)));
854        }
855
856        if let Some(value) = self.builtin_initializer_list {
857            arguments.push(format!("--builtin-initializer-list={}", bool_flag(value)));
858        }
859
860        if self.pch {
861            arguments.push(String::from("--pch"));
862        }
863
864        if let Some(value) = self.create_pch {
865            arguments.push(format!("--create-pch={value}"));
866        }
867
868        if let Some(value) = self.use_pch {
869            arguments.push(format!("--use-pch={value}"));
870        }
871
872        if let Some(value) = self.pch_dir {
873            arguments.push(format!("--pch-dir={value}"));
874        }
875
876        if let Some(value) = self.pch_verbose {
877            arguments.push(format!("--pch-verbose={}", bool_flag(value)));
878        }
879
880        if let Some(value) = self.pch_messages {
881            arguments.push(format!("--pch-messages={}", bool_flag(value)));
882        }
883
884        if let Some(value) = self.instantiate_templates_in_pch {
885            arguments.push(format!(
886                "--instantiate-templates-in-pch={}",
887                bool_flag(value)
888            ));
889        }
890
891        if self.disable_warnings {
892            arguments.push(String::from("--disable-warnings"));
893        }
894
895        if !self.warning_as_error.is_empty() {
896            arguments.push(format!(
897                "--warning-as-error={}",
898                join_display(&self.warning_as_error)
899            ));
900        }
901
902        if self.restrict_pointers {
903            arguments.push(String::from("--restrict"));
904        }
905
906        if self.device_as_default_execution_space {
907            arguments.push(String::from("--device-as-default-execution-space"));
908        }
909
910        if self.device_int128 {
911            arguments.push(String::from("--device-int128"));
912        }
913
914        if self.device_float128 {
915            arguments.push(String::from("--device-float128"));
916        }
917
918        arguments.extend(
919            self.optimization_info
920                .iter()
921                .map(|value| format!("--optimization-info={value}")),
922        );
923        if let Some(value) = self.display_error_number {
924            arguments.push(if value {
925                String::from("--display-error-number")
926            } else {
927                String::from("--no-display-error-number")
928            });
929        }
930
931        if !self.diag_error.is_empty() {
932            arguments.push(format!("--diag-error={}", join_display(&self.diag_error)));
933        }
934
935        if !self.diag_suppress.is_empty() {
936            arguments.push(format!(
937                "--diag-suppress={}",
938                join_display(&self.diag_suppress)
939            ));
940        }
941
942        if !self.diag_warn.is_empty() {
943            arguments.push(format!("--diag-warn={}", join_display(&self.diag_warn)));
944        }
945
946        if let Some(value) = self.brief_diagnostics {
947            arguments.push(format!("--brief-diagnostics={}", bool_flag(value)));
948        }
949
950        if let Some(value) = self.time {
951            arguments.push(format!("--time={value}"));
952        }
953
954        if let Some(value) = self.split_compile {
955            arguments.push(format!("--split-compile={value}"));
956        }
957
958        if self.device_syntax_only {
959            arguments.push(String::from("--fdevice-syntax-only"));
960        }
961
962        if self.minimal {
963            arguments.push(String::from("--minimal"));
964        }
965
966        if let Some(value) = self.device_stack_protector {
967            arguments.push(format!("--device-stack-protector={}", bool_flag(value)));
968        }
969
970        if let Some(value) = self.device_time_trace {
971            arguments.push(format!("--fdevice-time-trace={value}"));
972        }
973
974        if let Some(mode) = self.tile_compilation {
975            arguments.push(
976                match mode {
977                    TileCompilation::Enabled => "--enable-tile",
978                    TileCompilation::TileOnly => "--tile-only",
979                    TileCompilation::SimtOnly => "--simt-only",
980                }
981                .into(),
982            );
983        }
984
985        if self.default_tile {
986            arguments.push("--default-tile".into());
987        }
988
989        if self.diagnose_implicit_tile_variable {
990            arguments.push("-diagnose-implicit-tile-var".into());
991        }
992
993        arguments.extend(self.raw_options.iter().map(|value| (*value).to_string()));
994        arguments
995    }
996}
997
998fn bool_flag(value: bool) -> &'static str {
999    if value { "true" } else { "false" }
1000}
1001
1002fn join_display(values: &[impl Display]) -> String {
1003    values
1004        .iter()
1005        .map(ToString::to_string)
1006        .collect::<Vec<_>>()
1007        .join(",")
1008}