1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
|
// slang-nvrtc-compiler.cpp
#include "slang-nvrtc-compiler.h"
#include "../core/slang-common.h"
#include "../../slang-com-helper.h"
#include "../core/slang-blob.h"
#include "../core/slang-string-util.h"
#include "../core/slang-string-slice-pool.h"
#include "../core/slang-io.h"
#include "../core/slang-shared-library.h"
#include "../core/slang-semantic-version.h"
#include "../core/slang-shared-library.h"
namespace nvrtc
{
typedef enum {
NVRTC_SUCCESS = 0,
NVRTC_ERROR_OUT_OF_MEMORY = 1,
NVRTC_ERROR_PROGRAM_CREATION_FAILURE = 2,
NVRTC_ERROR_INVALID_INPUT = 3,
NVRTC_ERROR_INVALID_PROGRAM = 4,
NVRTC_ERROR_INVALID_OPTION = 5,
NVRTC_ERROR_COMPILATION = 6,
NVRTC_ERROR_BUILTIN_OPERATION_FAILURE = 7,
NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION = 8,
NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION = 9,
NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID = 10,
NVRTC_ERROR_INTERNAL_ERROR = 11
} nvrtcResult;
typedef struct _nvrtcProgram *nvrtcProgram;
#define SLANG_NVRTC_FUNCS(x) \
x(const char*, nvrtcGetErrorString, (nvrtcResult result)) \
x(nvrtcResult, nvrtcVersion, (int *major, int *minor)) \
x(nvrtcResult, nvrtcCreateProgram, (nvrtcProgram *prog, const char *src, const char *name, int numHeaders, const char * const *headers, const char * const *includeNames)) \
x(nvrtcResult, nvrtcDestroyProgram, (nvrtcProgram *prog)) \
x(nvrtcResult, nvrtcCompileProgram, (nvrtcProgram prog, int numOptions, const char * const *options)) \
x(nvrtcResult, nvrtcGetPTXSize, (nvrtcProgram prog, size_t *ptxSizeRet)) \
x(nvrtcResult, nvrtcGetPTX, (nvrtcProgram prog, char *ptx)) \
x(nvrtcResult, nvrtcGetProgramLogSize, (nvrtcProgram prog, size_t *logSizeRet)) \
x(nvrtcResult, nvrtcGetProgramLog, (nvrtcProgram prog, char *log))\
x(nvrtcResult, nvrtcAddNameExpression, (nvrtcProgram prog, const char * const name_expression)) \
x(nvrtcResult, nvrtcGetLoweredName, (nvrtcProgram prog, const char *const name_expression, const char** lowered_name))
} // namespace nvrtc
namespace Slang
{
using namespace nvrtc;
static SlangResult _asResult(nvrtcResult res)
{
switch (res)
{
case NVRTC_SUCCESS:
{
return SLANG_OK;
}
case NVRTC_ERROR_OUT_OF_MEMORY:
{
return SLANG_E_OUT_OF_MEMORY;
}
case NVRTC_ERROR_PROGRAM_CREATION_FAILURE:
case NVRTC_ERROR_INVALID_INPUT:
case NVRTC_ERROR_INVALID_PROGRAM:
{
return SLANG_FAIL;
}
case NVRTC_ERROR_INVALID_OPTION:
{
return SLANG_E_INVALID_ARG;
}
case NVRTC_ERROR_COMPILATION:
case NVRTC_ERROR_BUILTIN_OPERATION_FAILURE:
case NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION:
case NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION:
case NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID:
{
return SLANG_FAIL;
}
case NVRTC_ERROR_INTERNAL_ERROR:
{
return SLANG_E_INTERNAL_FAIL;
}
default: return SLANG_FAIL;
}
}
class NVRTCDownstreamCompiler : public DownstreamCompiler
{
public:
typedef DownstreamCompiler Super;
// DownstreamCompiler
virtual SlangResult compile(const CompileOptions& options, RefPtr<DownstreamCompileResult>& outResult) SLANG_OVERRIDE;
virtual bool isFileBased() SLANG_OVERRIDE { return false; }
/// Must be called before use
SlangResult init(ISlangSharedLibrary* library);
NVRTCDownstreamCompiler() {}
protected:
struct ScopeProgram
{
ScopeProgram(NVRTCDownstreamCompiler* compiler, nvrtcProgram program):
m_compiler(compiler),
m_program(program)
{
}
~ScopeProgram()
{
m_compiler->m_nvrtcDestroyProgram(&m_program);
}
NVRTCDownstreamCompiler* m_compiler;
nvrtcProgram m_program;
};
SlangResult _findIncludePath(String& outIncludePath);
SlangResult _getIncludePath(String& outIncludePath);
SlangResult _maybeAddHalfSupport(const CompileOptions& options, CommandLine& ioCmdLine);
#define SLANG_NVTRC_MEMBER_FUNCS(ret, name, params) \
ret (*m_##name) params;
SLANG_NVRTC_FUNCS(SLANG_NVTRC_MEMBER_FUNCS);
// Holds list of paths passed in where cuda_fp16.h is found. Does *NOT* include cuda_fp16.h.
List<String> m_cudaFp16FoundPaths;
bool m_includeSearched = false;
// Holds location of where include (for cuda_fp16.h) is found.
String m_includePath;
ComPtr<ISlangSharedLibrary> m_sharedLibrary;
};
#define SLANG_NVRTC_RETURN_ON_FAIL(x) { nvrtcResult _res = x; if (_res != NVRTC_SUCCESS) return _asResult(_res); }
SlangResult NVRTCDownstreamCompiler::init(ISlangSharedLibrary* library)
{
#define SLANG_NVTRC_GET_FUNC(ret, name, params) \
m_##name = (ret (*) params)library->findFuncByName(#name); \
if (m_##name == nullptr) return SLANG_FAIL;
SLANG_NVRTC_FUNCS(SLANG_NVTRC_GET_FUNC)
m_sharedLibrary = library;
m_desc.type = SLANG_PASS_THROUGH_NVRTC;
int major, minor;
m_nvrtcVersion(&major, &minor);
m_desc.majorVersion = major;
m_desc.minorVersion = minor;
return SLANG_OK;
}
static SlangResult _parseLocation(const UnownedStringSlice& in, DownstreamDiagnostic& outDiagnostic)
{
const Index startIndex = in.indexOf('(');
if (startIndex >= 0)
{
outDiagnostic.filePath = UnownedStringSlice(in.begin(), in.begin() + startIndex);
UnownedStringSlice remaining(in.begin() + startIndex + 1, in.end());
const Int endIndex = remaining.indexOf(')');
UnownedStringSlice lineText = UnownedStringSlice(remaining.begin(), remaining.begin() + endIndex);
Int line;
SLANG_RETURN_ON_FAIL(StringUtil::parseInt(lineText, line));
outDiagnostic.fileLine = line;
}
else
{
outDiagnostic.fileLine = 0;
outDiagnostic.filePath = in;
}
return SLANG_OK;
}
static bool _isDriveLetter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static bool _hasDriveLetter(const UnownedStringSlice& line)
{
return line.getLength() > 2 && line[1] == ':' && _isDriveLetter(line[0]);
}
static SlangResult _parseNVRTCLine(const UnownedStringSlice& line, DownstreamDiagnostic& outDiagnostic)
{
typedef DownstreamDiagnostic Diagnostic;
typedef Diagnostic::Severity Severity;
outDiagnostic.stage = Diagnostic::Stage::Compile;
List<UnownedStringSlice> split;
if (_hasDriveLetter(line))
{
// The drive letter has :, which confuses things, so skip that and then fix up first entry
UnownedStringSlice lineWithoutDrive(line.begin() + 2, line.end());
StringUtil::split(lineWithoutDrive, ':', split);
split[0] = UnownedStringSlice(line.begin(), split[0].end());
}
else
{
StringUtil::split(line, ':', split);
}
if (split.getCount() >= 3)
{
// tests/cuda/cuda-compile.cu(7): warning: variable "c" is used before its value is set
const auto split1 = split[1].trim();
if (split1 == "error")
{
outDiagnostic.severity = Severity::Error;
}
else if (split1 == "warning")
{
outDiagnostic.severity = Severity::Warning;
}
outDiagnostic.text = split[2].trim();
SLANG_RETURN_ON_FAIL(_parseLocation(split[0], outDiagnostic));
return SLANG_OK;
}
return SLANG_E_NOT_FOUND;
}
/* An implementation of Path::Visitor that can be used for finding NVRTC shared library installations. */
struct NVRTCPathVisitor : Path::Visitor
{
struct Candidate
{
typedef Candidate ThisType;
bool operator==(const ThisType& rhs) const { return path == rhs.path && version == rhs.version; }
bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
static Candidate make(const String& path, const SemanticVersion& version)
{
Candidate can;
can.version = version;
can.path = path;
return can;
}
String path;
SemanticVersion version;
};
Index findVersion(const SemanticVersion& version) const
{
const Index count = m_candidates.getCount();
for (Index i = 0; i < count; ++i)
{
if (m_candidates[i].version == version)
{
return i;
}
}
return -1;
}
static bool _orderCandiate(const Candidate& a, const Candidate& b) { return a.version < b.version; }
void sortCandidates() { m_candidates.sort(_orderCandiate); }
#if SLANG_WINDOWS_FAMILY
SlangResult getVersion(const UnownedStringSlice& filename, SemanticVersion& outVersion)
{
// Versions on windows of the form
// nvrtc64_110_2.dll
// 11 - Major
// 0 Minor
// 2 Patch
Index endIndex = filename.indexOf('.');
endIndex = (endIndex < 0) ? filename.getLength() : endIndex;
// If we have a version slice, split it
UnownedStringSlice versionSlice = UnownedStringSlice(filename.begin() + m_prefix.getLength(), filename.begin() + endIndex);
if (versionSlice.getLength() <= 0)
{
return SLANG_E_NOT_FOUND;
}
Int patch = 0;
UnownedStringSlice majorMinorSlice;
{
List<UnownedStringSlice> slices;
StringUtil::split(versionSlice, '_', slices);
if (slices.getCount() >= 2)
{
// We don't bother checking for error here, if it's not parsable, it will be 0
StringUtil::parseInt(slices[1], patch);
}
majorMinorSlice = slices[0];
}
if (majorMinorSlice.getLength() < 2)
{
// Must be a major and minor
return SLANG_FAIL;
}
UnownedStringSlice majorSlice = majorMinorSlice.head(majorMinorSlice.getLength() - 1);
UnownedStringSlice minorSlice = majorMinorSlice.subString(majorMinorSlice.getLength() - 1, 1);
Int major;
Int minor;
SLANG_RETURN_ON_FAIL(StringUtil::parseInt(majorSlice, major));
SLANG_RETURN_ON_FAIL(StringUtil::parseInt(minorSlice, minor));
outVersion = SemanticVersion(int(major), int(minor), int(patch));
return SLANG_OK;
}
#else
// How the path is constructed depends on platform
// https://docs.nvidia.com/cuda/nvrtc/index.html
// TODO(JS): Handle version number depending on the platform - it's different for Windows/OSX/Linux
SlangResult getVersion(const UnownedStringSlice& filename, SemanticVersion& outVersion)
{
SLANG_UNUSED(filename);
SLANG_UNUSED(outVersion);
return SLANG_E_NOT_IMPLEMENTED;
}
#endif
void accept(Path::Type type, const UnownedStringSlice& filename) SLANG_OVERRIDE
{
// Lets make sure it start's with nvrtc, but not worry about case
if (type == Path::Type::File)
{
// If there is a defined extension, make sure it has it
if (m_postfix.getLength() && filename.getLength() >= m_postfix.getLength())
{
// We test without case - really for windows
UnownedStringSlice filenamePostfix = filename.tail(filename.getLength() - m_postfix.getLength());
if (!filenamePostfix.caseInsensitiveEquals(m_postfix.getUnownedSlice()))
{
return;
}
}
if (filename.getLength() >= m_prefix.getLength() &&
filename.subString(0, m_prefix.getLength()).caseInsensitiveEquals(m_prefix.getUnownedSlice()))
{
SemanticVersion version;
// If it produces an error, just use 0.0.0
if (SLANG_FAILED(getVersion(filename, version)))
{
version = SemanticVersion();
}
// We may want to add multiple versions, if they are in different locations - as there may be multiple entries
// in the PATH, and only one works. We'll only know which works by loading
#if 0
// We already found this version, so let's not add it again
if (findVersion(version) >= 0)
{
return;
}
#endif
// Strip to make a shared library name
UnownedStringSlice sharedLibraryName = filename.tail(m_prefix.getLength() - m_sharedLibraryStem.getLength());
sharedLibraryName = filename.head(filename.getLength() - m_postfix.getLength());
auto candidate = Candidate::make(Path::combine(m_basePath, sharedLibraryName), version);
// If we already have this candidate, then skip
if (m_candidates.indexOf(candidate) >= 0)
{
return;
}
// Add to the list of candidates
m_candidates.add(candidate);
}
}
}
SlangResult findInDirectory(const String& path)
{
m_basePath = path;
return Path::find(path, nullptr, this);
}
bool hasCandidates() const { return m_candidates.getCount() > 0; }
NVRTCPathVisitor(const UnownedStringSlice& sharedLibraryStem) :
m_sharedLibraryStem(sharedLibraryStem)
{
// Work out the prefix and postfix of the shader
StringBuilder buf;
SharedLibrary::appendPlatformFileName(sharedLibraryStem, buf);
const Index index = buf.indexOf(sharedLibraryStem);
SLANG_ASSERT(index >= 0);
m_prefix = buf.getUnownedSlice().head(index + sharedLibraryStem.getLength());
m_postfix = buf.getUnownedSlice().tail(index + sharedLibraryStem.getLength());
}
String m_prefix;
String m_postfix;
String m_basePath;
String m_sharedLibraryStem;
List<Candidate> m_candidates;
};
template <typename T>
SLANG_FORCE_INLINE static void _unusedFunction(const T& func)
{
SLANG_UNUSED(func);
}
#define SLANG_UNUSED_FUNCTION(x) _unusedFunction(x)
static UnownedStringSlice _getNVRTCBaseName()
{
#if SLANG_WINDOWS_FAMILY && SLANG_PTR_IS_64
return UnownedStringSlice::fromLiteral("nvrtc64_");
#else
return UnownedStringSlice::fromLiteral("nvrtc");
#endif
}
// Candidates are in m_candidates list. Will be ordered from the oldest to newest (in version number)
static SlangResult _findNVRTC(NVRTCPathVisitor& visitor)
{
// First try the instance path (if supported on platform)
{
StringBuilder instancePath;
if (SLANG_SUCCEEDED(PlatformUtil::getInstancePath(instancePath)))
{
visitor.findInDirectory(instancePath);
}
}
// If we don't have a candidate try CUDA_PATH
if (!visitor.hasCandidates())
{
StringBuilder buf;
if (!SLANG_SUCCEEDED(PlatformUtil::getEnvironmentVariable(UnownedStringSlice::fromLiteral("CUDA_PATH"), buf)))
{
// Look for candidates in the directory
visitor.findInDirectory(Path::combine(buf, "bin"));
}
}
// If we haven't we go searching through PATH
if (!visitor.hasCandidates())
{
List<UnownedStringSlice> splitPath;
StringBuilder buf;
if (SLANG_SUCCEEDED(PlatformUtil::getEnvironmentVariable(UnownedStringSlice::fromLiteral("PATH"), buf)))
{
// Split so we get individual paths
List<UnownedStringSlice> paths;
StringUtil::split(buf.getUnownedSlice(), ';', paths);
// We use a pool to make sure we only check each path once
StringSlicePool pool(StringSlicePool::Style::Empty);
// We are going to search the paths in order
for (const auto& path : paths)
{
// PATH can have the same path multiple times. If we have already searched this path, we don't need to again
if (!pool.has(path))
{
pool.add(path);
Path::split(path, splitPath);
// We could search every path, but here we restrict to paths that look like CUDA installations.
// It's a path that contains a CUDA directory and has bin
if (splitPath.indexOf("CUDA") >= 0 && splitPath[splitPath.getCount() - 1].caseInsensitiveEquals(UnownedStringSlice::fromLiteral("bin")))
{
// Okay lets search it
visitor.findInDirectory(path);
}
}
}
}
}
// Put into version order with oldest first.
visitor.sortCandidates();
return SLANG_OK;
}
static const UnownedStringSlice g_fp16HeaderName = UnownedStringSlice::fromLiteral("cuda_fp16.h");
SlangResult NVRTCDownstreamCompiler::_getIncludePath(String& outPath)
{
if (!m_includeSearched)
{
m_includeSearched = true;
SLANG_ASSERT(m_includePath.getLength() == 0);
_findIncludePath(m_includePath);
}
outPath = m_includePath;
return m_includePath.getLength() ? SLANG_OK : SLANG_E_NOT_FOUND;
}
SlangResult _findFileInIncludePath(const String& path, const UnownedStringSlice& filename, String& outPath)
{
if (File::exists(Path::combine(path, filename)))
{
outPath = path;
return SLANG_OK;
}
{
String includePath = Path::combine(path, "include");
if (File::exists(Path::combine(includePath, filename)))
{
outPath = includePath;
return SLANG_OK;
}
}
{
String cudaIncludePath = Path::combine(path, "CUDA/include");
if (File::exists(Path::combine(cudaIncludePath, filename)))
{
outPath = cudaIncludePath;
return SLANG_OK;
}
}
return SLANG_E_NOT_FOUND;
}
SlangResult NVRTCDownstreamCompiler::_findIncludePath(String& outPath)
{
outPath = String();
// Try looking up from a symbol. This will work as long as the nvrtc is loaded somehow from a dll/sharedlibrary
// And the header is included from there
{
String libPath = SharedLibraryUtils::getSharedLibraryFileName((void*)m_nvrtcCreateProgram);
if (libPath.getLength())
{
const String parentPath = Path::getParentDirectory(libPath);
if (SLANG_SUCCEEDED(_findFileInIncludePath(parentPath, g_fp16HeaderName, outPath)))
{
return SLANG_OK;
}
}
}
// Try CUDA_PATH environment variable
{
StringBuilder buf;
if (SLANG_SUCCEEDED(PlatformUtil::getEnvironmentVariable(UnownedStringSlice::fromLiteral("CUDA_PATH"), buf)))
{
String includePath = Path::combine(buf, "include");
if (File::exists(Path::combine(includePath, g_fp16HeaderName)))
{
outPath = includePath;
return SLANG_OK;
}
}
}
return SLANG_E_NOT_FOUND;
}
SlangResult NVRTCDownstreamCompiler::_maybeAddHalfSupport(const CompileOptions& options, CommandLine& ioCmdLine)
{
if ((options.flags & CompileOptions::Flag::EnableFloat16) == 0)
{
return SLANG_OK;
}
// First check if we know if one of the include paths contains cuda_fp16.h
for (const auto& includePath : options.includePaths)
{
if (m_cudaFp16FoundPaths.indexOf(includePath) >= 0)
{
// Okay we have an include path that we know works.
// Just need to enable HALF in prelude
ioCmdLine.addArg("-DSLANG_CUDA_ENABLE_HALF");
return SLANG_OK;
}
}
// Let's see if one of the paths finds cuda_fp16.h
for (const auto& includePath : options.includePaths)
{
const String checkPath = Path::combine(includePath, g_fp16HeaderName);
if (File::exists(checkPath))
{
m_cudaFp16FoundPaths.add(includePath);
// Just need to enable HALF in prelude
ioCmdLine.addArg("-DSLANG_CUDA_ENABLE_HALF");
return SLANG_OK;
}
}
String includePath;
SLANG_RETURN_ON_FAIL(_getIncludePath(includePath));
// Add the found include path
ioCmdLine.addArg("-I");
ioCmdLine.addArg(includePath);
ioCmdLine.addArg("-DSLANG_CUDA_ENABLE_HALF");
return SLANG_OK;
}
SlangResult NVRTCDownstreamCompiler::compile(const CompileOptions& options, RefPtr<DownstreamCompileResult>& outResult)
{
// This compiler doesn't read files, they should be read externally and stored in sourceContents/sourceContentsPath
if (options.sourceFiles.getCount() > 0)
{
return SLANG_FAIL;
}
CommandLine cmdLine;
switch (options.debugInfoType)
{
case DebugInfoType::None:
{
break;
}
default:
{
cmdLine.addArg("--device-debug");
break;
}
case DebugInfoType::Maximal:
{
cmdLine.addArg("--device-debug");
cmdLine.addArg("--generate-line-info");
break;
}
}
// Don't seem to have such a control, so ignore for now
//switch (options.optimizationLevel)
//{
// default: break;
//}
switch (options.floatingPointMode)
{
case FloatingPointMode::Default: break;
case FloatingPointMode::Precise:
{
break;
}
case FloatingPointMode::Fast:
{
cmdLine.addArg("--use_fast_math");
break;
}
}
// Add defines
for (const auto& define : options.defines)
{
StringBuilder builder;
builder << "-D";
builder << define.nameWithSig;
if (define.value.getLength())
{
builder << "=" << define.value;
}
cmdLine.addArg(builder);
}
// Add includes
for (const auto& include : options.includePaths)
{
cmdLine.addArg("-I");
cmdLine.addArg(include);
}
SLANG_RETURN_ON_FAIL(_maybeAddHalfSupport(options, cmdLine));
// Neither of these options are strictly required, for general use of nvrtc,
// but are enabled to make use withing Slang work more smoothly
{
// Require c++14, as makes initialization construction with {} available and so simplifies code generation
cmdLine.addArg("-std=c++14");
// Disable all warnings
// This is arguably too much - but nvrtc does not appear to have a mechanism to switch off individual warnings.
// I tried the -Xcudafe mechanism but that does not appear to work for nvrtc
cmdLine.addArg("-w");
}
{
// The lowest supported CUDA architecture version supported
// by NVRTC is `compute_30`.
//
SemanticVersion version(3);
// Newer releases of NVRTC only support `compute_35` and up
// (with everything before `compute_52` being deprecated).
//
if( m_desc.majorVersion >= 11 )
{
version = SemanticVersion(3, 5);
}
// If constructs used in the code to be compield require
// a higher architecture version than the minimum, then
// we will set the version to the highest version listed
// among the requirements.
//
for (const auto& capabilityVersion : options.requiredCapabilityVersions)
{
if (capabilityVersion.kind == DownstreamCompiler::CapabilityVersion::Kind::CUDASM)
{
if (capabilityVersion.version > version)
{
version = capabilityVersion.version;
}
}
}
StringBuilder builder;
builder << "-arch=compute_";
builder << version.m_major;
SLANG_ASSERT(version.m_minor >= 0 && version.m_minor <= 9);
builder << char('0' + version.m_minor);
cmdLine.addArg(builder);
}
List<const char*> headers;
List<const char*> headerIncludeNames;
// If compiling for OptiX, we need to add the appropriate search paths to the command line.
//
if(options.pipelineType == PipelineType::RayTracing)
{
// The device-side OptiX API is accessed through a constellation
// of headers provided by the OptiX SDK, so we need to set an
// include path for the compile that makes those visible.
//
// TODO: The OptiX SDK installer doesn't set any kind of environment
// variable to indicate where the SDK was installed, so we seemingly
// need to probe paths instead. The form of the path will differ
// betwene Windows and Unix-y platforms, and we will need some kind
// of approach to probe multiple versiosn and use the latest.
//
// HACK: For now I'm using the fixed path for the most recent SDK
// release on Windows. This means that OptiX cross-compilation will
// only "work" on a subset of platforms, but that doesn't matter
// for now since it doesn't really "work" at all.
//
cmdLine.addArg("-I");
cmdLine.addArg("C:/ProgramData/NVIDIA Corporation/OptiX SDK 7.0.0/include/");
// The OptiX headers in turn `#include <stddef.h>` and expect that
// to work. We could try to also add in an include path from the CUDA
// SDK (which seems to provide a `stddef.h` in the most recent version),
// but using that version doesn't seem to work (and also bakes in a
// requirement that the user have the CUDA SDK installed in addition
// to the OptiX SDK).
//
// Instead, we will rely on the NVRTC feature that lets us set up
// memory buffers to be used as include files by the we compile.
// We will define a dummy `stddef.h` that includes the bare minimum
// lines required to get the OptiX headers to compile without complaint.
//
// TODO: Confirm that the `LP64` definition here is actually needed.
//
headerIncludeNames.add("stddef.h");
headers.add("#pragma once\n" "#define LP64\n");
// Finally, we want the CUDA prelude to be able to react to whether
// or not OptiX is required (most notably by `#include`ing the appropriate
// header(s)), so we will insert a preprocessor define to indicate
// the requirement.
//
cmdLine.addArg("-DSLANG_CUDA_ENABLE_OPTIX");
}
SLANG_ASSERT(headers.getCount() == headerIncludeNames.getCount());
nvrtcProgram program = nullptr;
nvrtcResult res = m_nvrtcCreateProgram(&program, options.sourceContents.getBuffer(), options.sourceContentsPath.getBuffer(),
(int) headers.getCount(),
headers.getBuffer(),
headerIncludeNames.getBuffer());
if (res != NVRTC_SUCCESS)
{
return _asResult(res);
}
ScopeProgram scope(this, program);
List<const char*> dstOptions;
dstOptions.setCount(cmdLine.m_args.getCount());
for (Index i = 0; i < cmdLine.m_args.getCount(); ++i)
{
dstOptions[i] = cmdLine.m_args[i].getBuffer();
}
res = m_nvrtcCompileProgram(program, int(dstOptions.getCount()), dstOptions.getBuffer());
RefPtr<ListBlob> blob;
DownstreamDiagnostics diagnostics;
diagnostics.result = _asResult(res);
{
String rawDiagnostics;
size_t logSize = 0;
SLANG_NVRTC_RETURN_ON_FAIL(m_nvrtcGetProgramLogSize(program, &logSize));
if (logSize)
{
char* dst = rawDiagnostics.prepareForAppend(Index(logSize));
SLANG_NVRTC_RETURN_ON_FAIL(m_nvrtcGetProgramLog(program, dst));
rawDiagnostics.appendInPlace(dst, Index(logSize));
diagnostics.rawDiagnostics = rawDiagnostics;
}
// Parse the diagnostics here
for (auto line : LineParser(diagnostics.rawDiagnostics.getUnownedSlice()))
{
DownstreamDiagnostic diagnostic;
SlangResult lineRes = _parseNVRTCLine(line, diagnostic);
if (SLANG_SUCCEEDED(lineRes))
{
diagnostics.diagnostics.add(diagnostic);
}
else if (lineRes != SLANG_E_NOT_FOUND)
{
return lineRes;
}
}
// if it has a compilation error.. set on output
if (diagnostics.has(DownstreamDiagnostic::Severity::Error))
{
diagnostics.result = SLANG_FAIL;
}
}
if (res == nvrtc::NVRTC_SUCCESS)
{
// We should parse the log to set up the diagnostics
size_t ptxSize;
SLANG_NVRTC_RETURN_ON_FAIL(m_nvrtcGetPTXSize(program, &ptxSize));
List<uint8_t> ptx;
ptx.setCount(Index(ptxSize));
SLANG_NVRTC_RETURN_ON_FAIL(m_nvrtcGetPTX(program, (char*)ptx.getBuffer()));
blob = ListBlob::moveCreate(ptx);
}
outResult = new BlobDownstreamCompileResult(diagnostics, blob);
return SLANG_OK;
}
static SlangResult _findAndLoadNVRTC(ISlangSharedLibraryLoader* loader, ComPtr<ISlangSharedLibrary>& outLibrary)
{
#if SLANG_WINDOWS_FAMILY && SLANG_PTR_IS_64
// We only need to search 64 bit versions on windows
NVRTCPathVisitor visitor(_getNVRTCBaseName());
SLANG_RETURN_ON_FAIL(_findNVRTC(visitor));
// We want to start with the newest version...
for (Index i = visitor.m_candidates.getCount() - 1; i >= 0; --i)
{
const auto& candidate = visitor.m_candidates[i];
if (SLANG_SUCCEEDED(loader->loadSharedLibrary(candidate.path.getBuffer(), outLibrary.writeRef())))
{
return SLANG_OK;
}
}
#else
SLANG_UNUSED(loader);
SLANG_UNUSED(outLibrary);
SLANG_UNUSED_FUNCTION(_getNVRTCBaseName);
SLANG_UNUSED_FUNCTION(_findNVRTC);
#endif
// This is an official-ish list of versions is here:
// https://developer.nvidia.com/cuda-toolkit-archive
// Filenames for NVRTC
// https://docs.nvidia.com/cuda/nvrtc/index.html
//
// From this it appears on platforms other than windows the SharedLibrary name
// should be nvrtc which is already tried, so we can give up now.
return SLANG_E_NOT_FOUND;
}
/* static */SlangResult NVRTCDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
{
ComPtr<ISlangSharedLibrary> library;
// If the user supplies a path to their preferred version of NVRTC,
// we just use this.
if (path.getLength() != 0)
{
SLANG_RETURN_ON_FAIL(loader->loadSharedLibrary(path.getBuffer(), library.writeRef()));
}
else
{
// As a catch-all for non-Windows platforms, we search for
// a library simply named `nvrtc` (well, `libnvrtc`) which
// is expected to match whatever the user has installed.
//
// On Windows an installation could place the version of nvrtc it uses in the same directory
// as the slang binary, such that it's loaded.
// Using this name also allows a ISlangSharedLibraryLoader to easily identify what is required
// and perhaps load a specific version
if (SLANG_FAILED(loader->loadSharedLibrary("nvrtc", library.writeRef())))
{
// Try something more sophisticated to locate NVRTC
SLANG_RETURN_ON_FAIL(_findAndLoadNVRTC(loader, library));
}
}
SLANG_ASSERT(library);
if (!library)
{
return SLANG_FAIL;
}
RefPtr<NVRTCDownstreamCompiler> compiler(new NVRTCDownstreamCompiler);
SLANG_RETURN_ON_FAIL(compiler->init(library));
set->addCompiler(compiler);
return SLANG_OK;
}
}
|