diff options
| author | Theresa Foley <10618364+tangent-vector@users.noreply.github.com> | 2025-07-24 12:59:58 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-24 19:59:58 +0000 |
| commit | 8ccd495d5eaa82cb831378c28dd190e657b6c999 (patch) | |
| tree | c39dc364d95f78984fd4ba2776d259ff944d2596 /source/slang/slang-pass-through.cpp | |
| parent | 2d23a962766a97cbb11bcee5483a66aec923da49 (diff) | |
Organize code better by splitting some big files (#7890)
* Organize code better by splitting some big files
The basic change here is that the majority of the declarations in `slang-compiler.h` have been split out into a set of smaller and more focused files.
As a result, the implement of those declarations have been moved from `slang-compiler.cpp` and `slang.cpp` over to those new files when the proper home for code is obvious.
I have tried as much as possible to *not* make any edits to the code along the way, and just copy-paste declarations from one place to another as-is.
The exceptions I am aware of are:
* In some cases a function that used to be file-scope `static` was used by code that landed in two or more different `.cpp` files. In these cases, I changed the function to be non-`static` (removing the `_` prefix from its name, if it had one, per our naming conventions), and put a declaration for the function into the most appropriate header I could identify.
* I added a few comments in places where I saw ugly or unfortunate things in the code I was moving, and wanted to tag them with `TODO`s so we can hopefully get to them in the fullness of time.
* I added top-level comments to each of the new `.h` files that was introduced to try to explain the logic for what goes into that file.
* In cases where one of the new header files mostly existed to declare a single type, I sometimes added more detail to the doc comment on that type, to better explain the type and its role in the compiler (this is text that otherwise might have gone into the comment at the top leve lof the file, but I figured that the doc comment would have higher discoverability).
I expect that the most contentious choice here is that the `Session` class lands in `slang-global-session.h` while `slang-session.h` holds the `Linkage` class.
The names used in this change are consistent with how the relevant concepts in the public Slang API are named, and are consistent with how we *intend* to rename the classes themselves in time.
* format code
* fixup
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'source/slang/slang-pass-through.cpp')
| -rw-r--r-- | source/slang/slang-pass-through.cpp | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/source/slang/slang-pass-through.cpp b/source/slang/slang-pass-through.cpp new file mode 100644 index 000000000..268cbc98f --- /dev/null +++ b/source/slang/slang-pass-through.cpp @@ -0,0 +1,275 @@ +// slang-pass-through.cpp +#include "slang-pass-through.h" + +#include "../core/slang-type-text-util.h" +#include "compiler-core/slang-slice-allocator.h" +#include "slang-compiler.h" + +namespace Slang +{ + +void printDiagnosticArg(StringBuilder& sb, PassThroughMode val) +{ + sb << TypeTextUtil::getPassThroughName(SlangPassThrough(val)); +} + +SourceLanguage getDefaultSourceLanguageForDownstreamCompiler(PassThroughMode compiler) +{ + switch (compiler) + { + case PassThroughMode::None: + { + return SourceLanguage::Unknown; + } + case PassThroughMode::Fxc: + case PassThroughMode::Dxc: + { + return SourceLanguage::HLSL; + } + case PassThroughMode::Glslang: + { + return SourceLanguage::GLSL; + } + case PassThroughMode::LLVM: + case PassThroughMode::Clang: + case PassThroughMode::VisualStudio: + case PassThroughMode::Gcc: + case PassThroughMode::GenericCCpp: + { + // These could ingest C, but we only have this function to work out a + // 'default' language to ingest. + return SourceLanguage::CPP; + } + case PassThroughMode::NVRTC: + { + return SourceLanguage::CUDA; + } + case PassThroughMode::Tint: + { + return SourceLanguage::WGSL; + } + case PassThroughMode::SpirvDis: + { + return SourceLanguage::SPIRV; + } + case PassThroughMode::MetalC: + { + return SourceLanguage::Metal; + } + default: + break; + } + SLANG_ASSERT(!"Unknown compiler"); + return SourceLanguage::Unknown; +} + +PassThroughMode getDownstreamCompilerRequiredForTarget(CodeGenTarget target) +{ + switch (target) + { + // Don't *require* a downstream compiler for source output + case CodeGenTarget::GLSL: + case CodeGenTarget::HLSL: + case CodeGenTarget::CUDASource: + case CodeGenTarget::CPPSource: + case CodeGenTarget::HostCPPSource: + case CodeGenTarget::PyTorchCppBinding: + case CodeGenTarget::CSource: + case CodeGenTarget::Metal: + case CodeGenTarget::WGSL: + { + return PassThroughMode::None; + } + case CodeGenTarget::None: + { + return PassThroughMode::None; + } + case CodeGenTarget::WGSLSPIRVAssembly: + case CodeGenTarget::SPIRVAssembly: + case CodeGenTarget::SPIRV: + { + return PassThroughMode::SpirvDis; + } + case CodeGenTarget::DXBytecode: + case CodeGenTarget::DXBytecodeAssembly: + { + return PassThroughMode::Fxc; + } + case CodeGenTarget::DXIL: + case CodeGenTarget::DXILAssembly: + { + return PassThroughMode::Dxc; + } + case CodeGenTarget::MetalLib: + case CodeGenTarget::MetalLibAssembly: + { + return PassThroughMode::MetalC; + } + case CodeGenTarget::ShaderHostCallable: + case CodeGenTarget::ShaderSharedLibrary: + case CodeGenTarget::HostExecutable: + case CodeGenTarget::HostHostCallable: + case CodeGenTarget::HostSharedLibrary: + { + // We need some C/C++ compiler + return PassThroughMode::GenericCCpp; + } + case CodeGenTarget::PTX: + { + return PassThroughMode::NVRTC; + } + case CodeGenTarget::WGSLSPIRV: + { + return PassThroughMode::Tint; + } + default: + break; + } + + SLANG_ASSERT(!"Unhandled target"); + return PassThroughMode::None; +} + + +void reportExternalCompileError( + const char* compilerName, + Severity severity, + SlangResult res, + const UnownedStringSlice& diagnostic, + DiagnosticSink* sink) +{ + StringBuilder builder; + if (compilerName) + { + builder << compilerName << ": "; + } + + if (SLANG_FAILED(res) && res != SLANG_FAIL) + { + { + char tmp[17]; + sprintf_s(tmp, SLANG_COUNT_OF(tmp), "0x%08x", uint32_t(res)); + builder << "Result(" << tmp << ") "; + } + + PlatformUtil::appendResult(res, builder); + } + + if (diagnostic.getLength() > 0) + { + builder.append(diagnostic); + if (!diagnostic.endsWith("\n")) + { + builder.append("\n"); + } + } + + sink->diagnoseRaw(severity, builder.getUnownedSlice()); +} + +void reportExternalCompileError( + const char* compilerName, + SlangResult res, + const UnownedStringSlice& diagnostic, + DiagnosticSink* sink) +{ + // TODO(tfoley): need a better policy for how we translate diagnostics + // back into the Slang world (although we should always try to generate + // HLSL that doesn't produce any diagnostics...) + reportExternalCompileError( + compilerName, + SLANG_FAILED(res) ? Severity::Error : Severity::Warning, + res, + diagnostic, + sink); +} + +static Severity _getDiagnosticSeverity(ArtifactDiagnostic::Severity severity) +{ + switch (severity) + { + case ArtifactDiagnostic::Severity::Warning: + return Severity::Warning; + case ArtifactDiagnostic::Severity::Info: + return Severity::Note; + default: + return Severity::Error; + } +} + +SlangResult passthroughDownstreamDiagnostics( + DiagnosticSink* sink, + IDownstreamCompiler* compiler, + IArtifact* artifact) +{ + auto diagnostics = findAssociatedRepresentation<IArtifactDiagnostics>(artifact); + + if (!diagnostics) + return SLANG_OK; + + if (diagnostics->getCount()) + { + StringBuilder compilerText; + DownstreamCompilerUtil::appendAsText(compiler->getDesc(), compilerText); + + StringBuilder builder; + + auto const diagnosticCount = diagnostics->getCount(); + for (Index i = 0; i < diagnosticCount; ++i) + { + const auto& diagnostic = *diagnostics->getAt(i); + + builder.clear(); + + const Severity severity = _getDiagnosticSeverity(diagnostic.severity); + + if (diagnostic.filePath.count == 0 && diagnostic.location.line == 0 && + severity == Severity::Note) + { + // If theres no filePath line number and it's info, output severity and text alone + builder << getSeverityName(severity) << " : "; + } + else + { + if (diagnostic.filePath.count) + { + builder << asStringSlice(diagnostic.filePath); + } + + if (diagnostic.location.line) + { + builder << "(" << diagnostic.location.line << ")"; + } + + builder << ": "; + + if (diagnostic.stage == ArtifactDiagnostic::Stage::Link) + { + builder << "link "; + } + + builder << getSeverityName(severity); + builder << " " << asStringSlice(diagnostic.code) << ": "; + } + + builder << asStringSlice(diagnostic.text); + reportExternalCompileError( + compilerText.getBuffer(), + severity, + SLANG_OK, + builder.getUnownedSlice(), + sink); + } + } + + // If any errors are emitted, then we are done + if (diagnostics->hasOfAtLeastSeverity(ArtifactDiagnostic::Severity::Error)) + { + return SLANG_FAIL; + } + + return SLANG_OK; +} + + +} // namespace Slang |
