Skip to content

Commit 9f786fd

Browse files
authoredMay 17, 2024
capture/relay: Add capture interface classes (shader-slang#4177)
* capture/relay: Add capture interface classes Add `ModuleCapture` class for capturing `IModule` - The `IModule` can only be created from -- `ISession::loadModule` -- `ISession::loadModuleFromIRBlob` -- `ISession::loadModuleFromSource` -- `ISession::loadModuleFromSourceString` so, we create the `ModuleCapture` at those methods in `SessionCapture` class. We use a hash map to store a map from `IModule` to `ModuleCapture` to avoid creating new `ModuleCapture` when there is already an old one. - In `SessionCapture::getLoadedModule`, we will assert on not finding a `ModuleCapture` instance. Add `EntryPointCapture` class for capturing `IEntryPoint`. - The `IEntryPoint` can only be created from: -- `IModule::findEntryPointByName` -- `IModule::findAndCheckEntryPoint` so, we create the `EntryPointCapture` at those methods in `ModuleCapture`. Similarly, we use a hash map to store a map from `IEntryPoint` to `EntryPointCapture`. - In `IModule::getDefinedEntryPoint`, we will assert on not finding a `EntryPointCapture` instance. Add `CompositeComponentTypeCapture` class for capturing CompositeComponentType, but since user is only exposed to `IComponentType`, so `CompositeComponentTypeCapture` just inherits from `IComponentType`. - `CompositeComponentType` can only be created from: -- ISession::createCompositeComponentType so create it here. Add `TypeConformanceCapture` class for capturing `ITypeConformance`. - The `ITypeConformance` can only be created from: -- `ISession::createTypeConformanceComponentType` so create it here. In addition, because `EntryPointCapture` and `ModuleCapture` share a some base class `IComponentType`, we generate the COM GUID for those two classes to differentiate them. * Fix the build issue * Add nullptr check for output parameter * define the SLANG_CAPTURE_ASSERT macro used in both debug and release build
1 parent 000396c commit 9f786fd

17 files changed

+1163
-64
lines changed
 

‎build/visual-studio/slang/slang.vcxproj

+8
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,13 @@ IF EXIST ..\..\..\external\slang-glslang\bin\windows-aarch64\release\slang-glsla
309309
<ItemGroup>
310310
<ClInclude Include="..\..\..\slang.h" />
311311
<ClInclude Include="..\..\..\source\slang-capture-replay\capture_utility.h" />
312+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-composite-component-type.h" />
313+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-entrypoint.h" />
312314
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-filesystem.h" />
313315
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-global-session.h" />
316+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-module.h" />
314317
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-session.h" />
318+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-type-conformance.h" />
315319
<ClInclude Include="..\..\..\source\slang\slang-artifact-output-util.h" />
316320
<ClInclude Include="..\..\..\source\slang\slang-ast-all.h" />
317321
<ClInclude Include="..\..\..\source\slang\slang-ast-base.h" />
@@ -543,9 +547,13 @@ IF EXIST ..\..\..\external\slang-glslang\bin\windows-aarch64\release\slang-glsla
543547
<ClCompile Include="..\..\..\prelude\slang-hlsl-prelude.h.cpp" />
544548
<ClCompile Include="..\..\..\prelude\slang-torch-prelude.h.cpp" />
545549
<ClCompile Include="..\..\..\source\slang-capture-replay\capture_utility.cpp" />
550+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-composite-component-type.cpp" />
551+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-entrypoint.cpp" />
546552
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-filesystem.cpp" />
547553
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-global-session.cpp" />
554+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-module.cpp" />
548555
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-session.cpp" />
556+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-type-conformance.cpp" />
549557
<ClCompile Include="..\..\..\source\slang\slang-api.cpp" />
550558
<ClCompile Include="..\..\..\source\slang\slang-artifact-output-util.cpp" />
551559
<ClCompile Include="..\..\..\source\slang\slang-ast-base.cpp" />

‎build/visual-studio/slang/slang.vcxproj.filters

+24
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,27 @@
1515
<ClInclude Include="..\..\..\source\slang-capture-replay\capture_utility.h">
1616
<Filter>Header Files</Filter>
1717
</ClInclude>
18+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-composite-component-type.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-entrypoint.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
1824
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-filesystem.h">
1925
<Filter>Header Files</Filter>
2026
</ClInclude>
2127
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-global-session.h">
2228
<Filter>Header Files</Filter>
2329
</ClInclude>
30+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-module.h">
31+
<Filter>Header Files</Filter>
32+
</ClInclude>
2433
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-session.h">
2534
<Filter>Header Files</Filter>
2635
</ClInclude>
36+
<ClInclude Include="..\..\..\source\slang-capture-replay\slang-type-conformance.h">
37+
<Filter>Header Files</Filter>
38+
</ClInclude>
2739
<ClInclude Include="..\..\..\source\slang\slang-artifact-output-util.h">
2840
<Filter>Header Files</Filter>
2941
</ClInclude>
@@ -713,15 +725,27 @@
713725
<ClCompile Include="..\..\..\source\slang-capture-replay\capture_utility.cpp">
714726
<Filter>Source Files</Filter>
715727
</ClCompile>
728+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-composite-component-type.cpp">
729+
<Filter>Source Files</Filter>
730+
</ClCompile>
731+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-entrypoint.cpp">
732+
<Filter>Source Files</Filter>
733+
</ClCompile>
716734
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-filesystem.cpp">
717735
<Filter>Source Files</Filter>
718736
</ClCompile>
719737
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-global-session.cpp">
720738
<Filter>Source Files</Filter>
721739
</ClCompile>
740+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-module.cpp">
741+
<Filter>Source Files</Filter>
742+
</ClCompile>
722743
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-session.cpp">
723744
<Filter>Source Files</Filter>
724745
</ClCompile>
746+
<ClCompile Include="..\..\..\source\slang-capture-replay\slang-type-conformance.cpp">
747+
<Filter>Source Files</Filter>
748+
</ClCompile>
725749
<ClCompile Include="..\..\..\source\slang\slang-api.cpp">
726750
<Filter>Source Files</Filter>
727751
</ClCompile>

‎source/slang-capture-replay/capture_utility.h

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#ifndef CAPTURE_UTILITY_H
22
#define CAPTURE_UTILITY_H
33

4+
// in gcc and clang, __PRETTY_FUNCTION__ is the function signature,
5+
// while MSVC uses __FUNCSIG__
6+
#ifdef _MSC_VER
7+
#define __PRETTY_FUNCTION__ __FUNCSIG__
8+
#endif
9+
410
namespace SlangCapture
511
{
612
enum LogLevel: unsigned int
@@ -15,4 +21,13 @@ namespace SlangCapture
1521
void slangCaptureLog(LogLevel logLevel, const char* fmt, ...);
1622
void setLogLevel();
1723
}
24+
25+
#define SLANG_CAPTURE_ASSERT(VALUE) \
26+
do { \
27+
if (!(VALUE)) { \
28+
SlangCapture::slangCaptureLog(SlangCapture::LogLevel::Error, "Assertion failed: %s, %s, %d\n", #VALUE, __FILE__, __LINE__);\
29+
std::abort(); \
30+
} \
31+
} while(0)
32+
1833
#endif // CAPTURE_UTILITY_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "capture_utility.h"
2+
#include "slang-composite-component-type.h"
3+
4+
namespace SlangCapture
5+
{
6+
CompositeComponentTypeCapture::CompositeComponentTypeCapture(slang::IComponentType* componentType)
7+
: m_actualCompositeComponentType(componentType)
8+
{
9+
SLANG_CAPTURE_ASSERT(m_actualCompositeComponentType != nullptr);
10+
slangCaptureLog(LogLevel::Verbose, "%s: %p\n", __PRETTY_FUNCTION__, componentType);
11+
}
12+
13+
CompositeComponentTypeCapture::~CompositeComponentTypeCapture()
14+
{
15+
m_actualCompositeComponentType->release();
16+
}
17+
18+
ISlangUnknown* CompositeComponentTypeCapture::getInterface(const Guid& guid)
19+
{
20+
if (guid == IComponentType::getTypeGuid())
21+
{
22+
return static_cast<ISlangUnknown*>(this);
23+
}
24+
return nullptr;
25+
}
26+
27+
SLANG_NO_THROW slang::ISession* CompositeComponentTypeCapture::getSession()
28+
{
29+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
30+
slang::ISession* res = m_actualCompositeComponentType->getSession();
31+
return res;
32+
}
33+
34+
SLANG_NO_THROW slang::ProgramLayout* CompositeComponentTypeCapture::getLayout(
35+
SlangInt targetIndex,
36+
slang::IBlob** outDiagnostics)
37+
{
38+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
39+
slang::ProgramLayout* res = m_actualCompositeComponentType->getLayout(targetIndex, outDiagnostics);
40+
return res;
41+
}
42+
43+
SLANG_NO_THROW SlangInt CompositeComponentTypeCapture::getSpecializationParamCount()
44+
{
45+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
46+
SlangInt res = m_actualCompositeComponentType->getSpecializationParamCount();
47+
return res;
48+
}
49+
50+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::getEntryPointCode(
51+
SlangInt entryPointIndex,
52+
SlangInt targetIndex,
53+
slang::IBlob** outCode,
54+
slang::IBlob** outDiagnostics)
55+
{
56+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
57+
SlangResult res = m_actualCompositeComponentType->getEntryPointCode(entryPointIndex, targetIndex, outCode, outDiagnostics);
58+
return res;
59+
}
60+
61+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::getResultAsFileSystem(
62+
SlangInt entryPointIndex,
63+
SlangInt targetIndex,
64+
ISlangMutableFileSystem** outFileSystem)
65+
{
66+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
67+
SlangResult res = m_actualCompositeComponentType->getResultAsFileSystem(entryPointIndex, targetIndex, outFileSystem);
68+
return res;
69+
}
70+
71+
SLANG_NO_THROW void CompositeComponentTypeCapture::getEntryPointHash(
72+
SlangInt entryPointIndex,
73+
SlangInt targetIndex,
74+
slang::IBlob** outHash)
75+
{
76+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
77+
m_actualCompositeComponentType->getEntryPointHash(entryPointIndex, targetIndex, outHash);
78+
}
79+
80+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::specialize(
81+
slang::SpecializationArg const* specializationArgs,
82+
SlangInt specializationArgCount,
83+
slang::IComponentType** outSpecializedComponentType,
84+
ISlangBlob** outDiagnostics)
85+
{
86+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
87+
SlangResult res = m_actualCompositeComponentType->specialize(specializationArgs, specializationArgCount, outSpecializedComponentType, outDiagnostics);
88+
return res;
89+
}
90+
91+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::link(
92+
slang::IComponentType** outLinkedComponentType,
93+
ISlangBlob** outDiagnostics)
94+
{
95+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
96+
SlangResult res = m_actualCompositeComponentType->link(outLinkedComponentType, outDiagnostics);
97+
return res;
98+
}
99+
100+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::getEntryPointHostCallable(
101+
int entryPointIndex,
102+
int targetIndex,
103+
ISlangSharedLibrary** outSharedLibrary,
104+
slang::IBlob** outDiagnostics)
105+
{
106+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
107+
SlangResult res = m_actualCompositeComponentType->getEntryPointHostCallable(entryPointIndex, targetIndex, outSharedLibrary, outDiagnostics);
108+
return res;
109+
}
110+
111+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::renameEntryPoint(
112+
const char* newName, IComponentType** outEntryPoint)
113+
{
114+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
115+
SlangResult res = m_actualCompositeComponentType->renameEntryPoint(newName, outEntryPoint);
116+
return res;
117+
}
118+
119+
SLANG_NO_THROW SlangResult CompositeComponentTypeCapture::linkWithOptions(
120+
IComponentType** outLinkedComponentType,
121+
uint32_t compilerOptionEntryCount,
122+
slang::CompilerOptionEntry* compilerOptionEntries,
123+
ISlangBlob** outDiagnostics)
124+
{
125+
slangCaptureLog(LogLevel::Verbose, "%s\n", __PRETTY_FUNCTION__);
126+
SlangResult res = m_actualCompositeComponentType->linkWithOptions(outLinkedComponentType, compilerOptionEntryCount, compilerOptionEntries, outDiagnostics);
127+
return res;
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#ifndef SLANG_COMPOSITE_COMPONENT_TYPE_H
2+
#define SLANG_COMPOSITE_COMPONENT_TYPE_H
3+
4+
#include "../../slang-com-ptr.h"
5+
#include "../../slang.h"
6+
#include "../../slang-com-helper.h"
7+
#include "../core/slang-smart-pointer.h"
8+
#include "../core/slang-dictionary.h"
9+
#include "../slang/slang-compiler.h"
10+
11+
namespace SlangCapture
12+
{
13+
using namespace Slang;
14+
class CompositeComponentTypeCapture: public slang::IComponentType, public RefObject
15+
{
16+
public:
17+
SLANG_REF_OBJECT_IUNKNOWN_ALL
18+
ISlangUnknown* getInterface(const Guid& guid);
19+
20+
explicit CompositeComponentTypeCapture(slang::IComponentType* componentType);
21+
~CompositeComponentTypeCapture();
22+
23+
// Interfaces for `IComponentType`
24+
virtual SLANG_NO_THROW slang::ISession* SLANG_MCALL getSession() override;
25+
virtual SLANG_NO_THROW slang::ProgramLayout* SLANG_MCALL getLayout(
26+
SlangInt targetIndex = 0,
27+
slang::IBlob** outDiagnostics = nullptr) override;
28+
virtual SLANG_NO_THROW SlangInt SLANG_MCALL getSpecializationParamCount() override;
29+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointCode(
30+
SlangInt entryPointIndex,
31+
SlangInt targetIndex,
32+
slang::IBlob** outCode,
33+
slang::IBlob** outDiagnostics = nullptr) override;
34+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getResultAsFileSystem(
35+
SlangInt entryPointIndex,
36+
SlangInt targetIndex,
37+
ISlangMutableFileSystem** outFileSystem) override;
38+
virtual SLANG_NO_THROW void SLANG_MCALL getEntryPointHash(
39+
SlangInt entryPointIndex,
40+
SlangInt targetIndex,
41+
slang::IBlob** outHash) override;
42+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL specialize(
43+
slang::SpecializationArg const* specializationArgs,
44+
SlangInt specializationArgCount,
45+
slang::IComponentType** outSpecializedComponentType,
46+
ISlangBlob** outDiagnostics = nullptr) override;
47+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL link(
48+
slang::IComponentType** outLinkedComponentType,
49+
ISlangBlob** outDiagnostics = nullptr) override;
50+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointHostCallable(
51+
int entryPointIndex,
52+
int targetIndex,
53+
ISlangSharedLibrary** outSharedLibrary,
54+
slang::IBlob** outDiagnostics = 0) override;
55+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL renameEntryPoint(
56+
const char* newName, IComponentType** outEntryPoint) override;
57+
virtual SLANG_NO_THROW SlangResult SLANG_MCALL linkWithOptions(
58+
IComponentType** outLinkedComponentType,
59+
uint32_t compilerOptionEntryCount,
60+
slang::CompilerOptionEntry* compilerOptionEntries,
61+
ISlangBlob** outDiagnostics = nullptr) override;
62+
63+
slang::IComponentType* getActualCompositeComponentType() const { return m_actualCompositeComponentType; }
64+
private:
65+
Slang::ComPtr<slang::IComponentType> m_actualCompositeComponentType;
66+
};
67+
}
68+
#endif // SLANG_COMPOSITE_COMPONENT_TYPE_H

0 commit comments

Comments
 (0)