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-target-program.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-target-program.cpp')
| -rw-r--r-- | source/slang/slang-target-program.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/source/slang/slang-target-program.cpp b/source/slang/slang-target-program.cpp new file mode 100644 index 000000000..ffb859b55 --- /dev/null +++ b/source/slang/slang-target-program.cpp @@ -0,0 +1,113 @@ +// slang-target-program.cpp +#include "slang-target-program.h" + +#include "slang-compiler.h" +#include "slang-type-layout.h" + +namespace Slang +{ + +// +// TargetProgram +// + +TargetProgram::TargetProgram(ComponentType* componentType, TargetRequest* targetReq) + : m_program(componentType), m_targetReq(targetReq) +{ + m_entryPointResults.setCount(componentType->getEntryPointCount()); + m_optionSet.overrideWith(m_program->getOptionSet()); + m_optionSet.inheritFrom(targetReq->getOptionSet()); +} + +IArtifact* TargetProgram::_createWholeProgramResult( + DiagnosticSink* sink, + EndToEndCompileRequest* endToEndReq) +{ + // We want to call `emitEntryPoints` function to generate code that contains + // all the entrypoints defined in `m_program`. + // The current logic of `emitEntryPoints` takes a list of entry-point indices to + // emit code for, so we construct such a list first. + List<Int> entryPointIndices; + + m_entryPointResults.setCount(m_program->getEntryPointCount()); + entryPointIndices.setCount(m_program->getEntryPointCount()); + for (Index i = 0; i < entryPointIndices.getCount(); i++) + entryPointIndices[i] = i; + + CodeGenContext::Shared sharedCodeGenContext(this, entryPointIndices, sink, endToEndReq); + CodeGenContext codeGenContext(&sharedCodeGenContext); + + if (SLANG_FAILED(codeGenContext.emitEntryPoints(m_wholeProgramResult))) + { + return nullptr; + } + + return m_wholeProgramResult; +} + +IArtifact* TargetProgram::_createEntryPointResult( + Int entryPointIndex, + DiagnosticSink* sink, + EndToEndCompileRequest* endToEndReq) +{ + // It is possible that entry points got added to the `Program` + // *after* we created this `TargetProgram`, so there might be + // a request for an entry point that we didn't allocate space for. + // + // TODO: Change the construction logic so that a `Program` is + // constructed all at once rather than incrementally, to avoid + // this problem. + // + if (entryPointIndex >= m_entryPointResults.getCount()) + m_entryPointResults.setCount(entryPointIndex + 1); + + + CodeGenContext::EntryPointIndices entryPointIndices; + entryPointIndices.add(entryPointIndex); + + CodeGenContext::Shared sharedCodeGenContext(this, entryPointIndices, sink, endToEndReq); + CodeGenContext codeGenContext(&sharedCodeGenContext); + + codeGenContext.emitEntryPoints(m_entryPointResults[entryPointIndex]); + + return m_entryPointResults[entryPointIndex]; +} + +IArtifact* TargetProgram::getOrCreateWholeProgramResult(DiagnosticSink* sink) +{ + if (m_wholeProgramResult) + return m_wholeProgramResult; + + // If we haven't yet computed a layout for this target + // program, we need to make sure that is done before + // code generation. + // + if (!getOrCreateIRModuleForLayout(sink)) + { + return nullptr; + } + + return _createWholeProgramResult(sink); +} + +IArtifact* TargetProgram::getOrCreateEntryPointResult(Int entryPointIndex, DiagnosticSink* sink) +{ + if (entryPointIndex >= m_entryPointResults.getCount()) + m_entryPointResults.setCount(entryPointIndex + 1); + + if (IArtifact* artifact = m_entryPointResults[entryPointIndex]) + return artifact; + + // If we haven't yet computed a layout for this target + // program, we need to make sure that is done before + // code generation. + // + if (!getOrCreateIRModuleForLayout(sink)) + { + return nullptr; + } + + return _createEntryPointResult(entryPointIndex, sink); +} + +} // namespace Slang |
