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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12#[non_exhaustive]
13pub enum MacroDefinition<'a> {
14 Name(
16 &'a str,
18 ),
19 WithValue {
21 name: &'a str,
23 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
39#[non_exhaustive]
40pub enum CppDialect {
41 Cpp03,
43 Cpp11,
45 Cpp14,
47 Cpp17,
49 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
63#[non_exhaustive]
64pub enum FastCompileLevel {
65 Zero,
67 Min,
69 Mid,
71 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
84#[non_exhaustive]
85pub enum WarningAsErrorKind {
86 AllWarnings,
88 Reorder,
90 DeprecatedDeclarations,
92}
93
94impl_enum_display!(WarningAsErrorKind, {
95 Self::AllWarnings => "all-warnings",
96 Self::Reorder => "reorder",
97 Self::DeprecatedDeclarations => "deprecated-declarations",
98});
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102#[non_exhaustive]
103pub enum OptimizationInfoKind {
104 Inline,
106}
107
108impl_enum_display!(OptimizationInfoKind, {
109 Self::Inline => "inline",
110});
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub enum TileCompilation {
115 Enabled,
117 TileOnly,
119 SimtOnly,
121}
122
123#[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 pub gpu_architecture: Option<GpuArchitecture>,
135 pub relocatable_device_code: Option<bool>,
137 pub extensible_whole_program: bool,
139 pub device_debug: bool,
141 pub generate_line_info: bool,
143 pub device_optimization: Option<bool>,
146 pub fast_compile: Option<FastCompileLevel>,
148 pub ptxas_options: Vec<&'a str>,
150 pub max_register_count: Option<i32>,
152 pub flush_to_zero: Option<bool>,
154 pub precise_square_root: Option<bool>,
156 pub precise_division: Option<bool>,
158 pub fmad: Option<bool>,
160 pub use_fast_math: bool,
162 pub extra_device_vectorization: bool,
164 pub modify_stack_limit: Option<bool>,
166 pub dlink_time_optimization: bool,
168 pub generate_optimized_lto: bool,
170 pub optix_ir: bool,
172 pub tile_compilation: Option<TileCompilation>,
174 pub default_tile: bool,
176 pub diagnose_implicit_tile_variable: bool,
178 pub jump_table_density: Option<u8>,
180 pub no_cache: bool,
182 pub random_seed: Option<&'a str>,
184 pub define_macros: Vec<MacroDefinition<'a>>,
186 pub undefine_macros: Vec<&'a str>,
188 pub include_paths: Vec<&'a str>,
190 pub pre_include_headers: Vec<&'a str>,
192 pub no_source_include: bool,
194 pub cpp_dialect: Option<CppDialect>,
196 pub builtin_move_forward: Option<bool>,
198 pub builtin_initializer_list: Option<bool>,
200 pub pch: bool,
202 pub create_pch: Option<&'a str>,
204 pub use_pch: Option<&'a str>,
206 pub pch_dir: Option<&'a str>,
208 pub pch_verbose: Option<bool>,
210 pub pch_messages: Option<bool>,
212 pub instantiate_templates_in_pch: Option<bool>,
214 pub disable_warnings: bool,
216 pub warning_as_error: Vec<WarningAsErrorKind>,
218 pub restrict_pointers: bool,
220 pub device_as_default_execution_space: bool,
222 pub device_int128: bool,
224 pub device_float128: bool,
226 pub optimization_info: Vec<OptimizationInfoKind>,
228 pub display_error_number: Option<bool>,
230 pub diag_error: Vec<i32>,
232 pub diag_suppress: Vec<i32>,
234 pub diag_warn: Vec<i32>,
236 pub brief_diagnostics: Option<bool>,
238 pub time: Option<&'a str>,
240 pub split_compile: Option<i32>,
242 pub device_syntax_only: bool,
244 pub minimal: bool,
246 pub device_stack_protector: Option<bool>,
248 pub device_time_trace: Option<&'a str>,
250 pub raw_options: Vec<&'a str>,
252 pub cancel_flag: Option<Arc<AtomicBool>>,
255}
256
257impl<'a> CompileOptions<'a> {
258 #[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 #[must_use]
327 pub fn gpu_architecture(mut self, value: GpuArchitecture) -> Self {
328 self.gpu_architecture = Some(value);
329 self
330 }
331
332 #[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 #[must_use]
341 pub fn extensible_whole_program(mut self, value: bool) -> Self {
342 self.extensible_whole_program = value;
343 self
344 }
345
346 #[must_use]
348 pub fn device_debug(mut self, value: bool) -> Self {
349 self.device_debug = value;
350 self
351 }
352
353 #[must_use]
355 pub fn generate_line_info(mut self, value: bool) -> Self {
356 self.generate_line_info = value;
357 self
358 }
359
360 #[must_use]
363 pub fn device_optimization(mut self, value: bool) -> Self {
364 self.device_optimization = Some(value);
365 self
366 }
367
368 #[must_use]
370 pub fn fast_compile(mut self, value: FastCompileLevel) -> Self {
371 self.fast_compile = Some(value);
372 self
373 }
374
375 #[must_use]
377 pub fn ptxas_option(mut self, value: &'a str) -> Self {
378 self.ptxas_options.push(value);
379 self
380 }
381
382 #[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 #[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 #[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 #[must_use]
405 pub fn precise_division(mut self, value: bool) -> Self {
406 self.precise_division = Some(value);
407 self
408 }
409
410 #[must_use]
412 pub fn fmad(mut self, value: bool) -> Self {
413 self.fmad = Some(value);
414 self
415 }
416
417 #[must_use]
419 pub fn use_fast_math(mut self, value: bool) -> Self {
420 self.use_fast_math = value;
421 self
422 }
423
424 #[must_use]
426 pub fn extra_device_vectorization(mut self, value: bool) -> Self {
427 self.extra_device_vectorization = value;
428 self
429 }
430
431 #[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 #[must_use]
440 pub fn dlink_time_optimization(mut self, value: bool) -> Self {
441 self.dlink_time_optimization = value;
442 self
443 }
444
445 #[must_use]
447 pub fn generate_optimized_lto(mut self, value: bool) -> Self {
448 self.generate_optimized_lto = value;
449 self
450 }
451
452 #[must_use]
454 pub fn optix_ir(mut self, value: bool) -> Self {
455 self.optix_ir = value;
456 self
457 }
458
459 #[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 #[must_use]
468 pub fn no_cache(mut self, value: bool) -> Self {
469 self.no_cache = value;
470 self
471 }
472
473 #[must_use]
475 pub fn random_seed(mut self, value: &'a str) -> Self {
476 self.random_seed = Some(value);
477 self
478 }
479
480 #[must_use]
482 pub fn define_macro(mut self, value: MacroDefinition<'a>) -> Self {
483 self.define_macros.push(value);
484 self
485 }
486
487 #[must_use]
489 pub fn undefine_macro(mut self, value: &'a str) -> Self {
490 self.undefine_macros.push(value);
491 self
492 }
493
494 #[must_use]
496 pub fn include_path(mut self, value: &'a str) -> Self {
497 self.include_paths.push(value);
498 self
499 }
500
501 #[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 #[must_use]
510 pub fn no_source_include(mut self, value: bool) -> Self {
511 self.no_source_include = value;
512 self
513 }
514
515 #[must_use]
517 pub fn cpp_dialect(mut self, value: CppDialect) -> Self {
518 self.cpp_dialect = Some(value);
519 self
520 }
521
522 #[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 #[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 #[must_use]
538 pub fn pch(mut self, value: bool) -> Self {
539 self.pch = value;
540 self
541 }
542
543 #[must_use]
545 pub fn create_pch(mut self, value: &'a str) -> Self {
546 self.create_pch = Some(value);
547 self
548 }
549
550 #[must_use]
552 pub fn use_pch(mut self, value: &'a str) -> Self {
553 self.use_pch = Some(value);
554 self
555 }
556
557 #[must_use]
559 pub fn pch_dir(mut self, value: &'a str) -> Self {
560 self.pch_dir = Some(value);
561 self
562 }
563
564 #[must_use]
566 pub fn pch_verbose(mut self, value: bool) -> Self {
567 self.pch_verbose = Some(value);
568 self
569 }
570
571 #[must_use]
573 pub fn pch_messages(mut self, value: bool) -> Self {
574 self.pch_messages = Some(value);
575 self
576 }
577
578 #[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 #[must_use]
587 pub fn disable_warnings(mut self, value: bool) -> Self {
588 self.disable_warnings = value;
589 self
590 }
591
592 #[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 #[must_use]
601 pub fn restrict_pointers(mut self, value: bool) -> Self {
602 self.restrict_pointers = value;
603 self
604 }
605
606 #[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 #[must_use]
615 pub fn device_int128(mut self, value: bool) -> Self {
616 self.device_int128 = value;
617 self
618 }
619
620 #[must_use]
622 pub fn device_float128(mut self, value: bool) -> Self {
623 self.device_float128 = value;
624 self
625 }
626
627 #[must_use]
629 pub fn optimization_info(mut self, value: OptimizationInfoKind) -> Self {
630 self.optimization_info.push(value);
631 self
632 }
633
634 #[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 #[must_use]
643 pub fn diag_error(mut self, value: i32) -> Self {
644 self.diag_error.push(value);
645 self
646 }
647
648 #[must_use]
650 pub fn diag_suppress(mut self, value: i32) -> Self {
651 self.diag_suppress.push(value);
652 self
653 }
654
655 #[must_use]
657 pub fn diag_warn(mut self, value: i32) -> Self {
658 self.diag_warn.push(value);
659 self
660 }
661
662 #[must_use]
664 pub fn brief_diagnostics(mut self, value: bool) -> Self {
665 self.brief_diagnostics = Some(value);
666 self
667 }
668
669 #[must_use]
671 pub fn time(mut self, value: &'a str) -> Self {
672 self.time = Some(value);
673 self
674 }
675
676 #[must_use]
679 pub fn split_compile(mut self, value: i32) -> Self {
680 self.split_compile = Some(value);
681 self
682 }
683
684 #[must_use]
686 pub fn device_syntax_only(mut self, value: bool) -> Self {
687 self.device_syntax_only = value;
688 self
689 }
690
691 #[must_use]
693 pub fn minimal(mut self, value: bool) -> Self {
694 self.minimal = value;
695 self
696 }
697
698 #[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 #[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 #[must_use]
714 pub fn raw_option(mut self, value: &'a str) -> Self {
715 self.raw_options.push(value);
716 self
717 }
718
719 #[must_use]
721 pub fn cancel_flag(mut self, flag: Arc<AtomicBool>) -> Self {
722 self.cancel_flag = Some(flag);
723 self
724 }
725
726 #[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}