Skip to content

Commit 79d106f

Browse files
authored
Fix for KernelContext threading issue for C++ targets (shader-slang#1843)
* #include an absolute path didn't work - because paths were taken to always be relative. * Fix for issue where threading KernelContext was not working on C++ test when there were multiple invocations. * Improve test for context threading.
1 parent 12bcc03 commit 79d106f

4 files changed

+81
-45
lines changed

source/slang/slang-ir-explicit-global-context.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ struct IntroduceExplicitGlobalContextPass
467467
addKernelContextNameHint(contextParam);
468468
contextParam->insertBefore(firstBlock->getFirstOrdinaryInst());
469469

470-
// The new parameter can be registerd as the context value
470+
// The new parameter can be registered as the context value
471471
// to be used for `func` right away.
472472
//
473473
// Note: we register the value *before* modifying locations
@@ -482,8 +482,12 @@ struct IntroduceExplicitGlobalContextPass
482482
// TODO: There is an issue here if `func` might be called
483483
// dynamically, through something like a witness table.
484484
//
485-
List<IRUse*> uses;
486-
for( auto use = func->firstUse; use; use = use->nextUse )
485+
// We collect all the uses first which are in calls.
486+
// NOTE! That we collect all calls and then process (and don't iterate
487+
// using the linked list), because when a replacement is made the func usage
488+
// linked list will no longer hold all of the use sites.
489+
List<IRCall*> callUses;
490+
for (auto use = func->firstUse; use; use = use->nextUse)
487491
{
488492
// We will only fix up calls to `func`, and ignore
489493
// other operations that might refer to it.
@@ -495,9 +499,15 @@ struct IntroduceExplicitGlobalContextPass
495499
// to a higher-order function.
496500
//
497501
auto call = as<IRCall>(use->getUser());
498-
if(!call)
499-
continue;
502+
if (call)
503+
{
504+
callUses.add(call);
505+
}
506+
}
500507

508+
// Fix up all of the call uses
509+
for( auto call : callUses)
510+
{
501511
// We are going to construct a new call to `func`
502512
// that has all of the arguments of the original call...
503513
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// kernel-context-threading.slang
2+
3+
// This test tests out the slang-ir-explicit-global-context functionality for C++ like targets.
4+
// In particular these require that the KernelContext is threaded through functions that access globals.
5+
// Currently this is only really applicable to C++, but for completeness all targets are tested.
6+
7+
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -compile-arg -O3 -xslang -matrix-layout-row-major -shaderobj
8+
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -output-using-type -compile-arg -O3 -xslang -matrix-layout-row-major -shaderobj
9+
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -output-using-type -compile-arg -O3 -xslang -matrix-layout-row-major -shaderobj
10+
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -output-using-type -xslang -matrix-layout-row-major -shaderobj
11+
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -output-using-type -dx12 -xslang -matrix-layout-row-major -shaderobj
12+
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -output-using-type -xslang -matrix-layout-row-major -shaderobj
13+
14+
//TEST_INPUT:cbuffer(data=[1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 10.0 20.0 30.0 1.0]):name matrixBuffer
15+
ConstantBuffer<float4x4> matrixBuffer;
16+
17+
//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name rowOrderMatrixOutput
18+
RWStructuredBuffer<float> rowOrderMatrixOutput;
19+
20+
void writeRow2(float4 v, int rowIndex)
21+
{
22+
int baseIndex = rowIndex * 4;
23+
24+
rowOrderMatrixOutput[baseIndex + 0] = v.x;
25+
rowOrderMatrixOutput[baseIndex + 1] = v.y;
26+
rowOrderMatrixOutput[baseIndex + 2] = v.z;
27+
rowOrderMatrixOutput[baseIndex + 3] = v.w;
28+
}
29+
30+
// Just to test threading works through multiple levels of functions.
31+
void writeRow(float4 v, int rowIndex)
32+
{
33+
writeRow2(v, rowIndex);
34+
}
35+
36+
[numthreads(1, 1, 1)]
37+
void computeMain(uint3 tid : SV_DispatchThreadID)
38+
{
39+
float4 v = float4(1, 2, 3, 1);
40+
41+
float4x4 M = matrixBuffer;
42+
43+
float4 r = mul(v, M);
44+
45+
writeRow(M[0], 0);
46+
writeRow(M[1], 1);
47+
writeRow(M[2], 2);
48+
writeRow(M[3], 3);
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: float
2+
1.000000
3+
0.000000
4+
0.000000
5+
0.000000
6+
0.000000
7+
1.000000
8+
0.000000
9+
0.000000
10+
0.000000
11+
0.000000
12+
1.000000
13+
0.000000
14+
10.000000
15+
20.000000
16+
30.000000
17+
1.000000

tests/current-bugs/cpp-resource-issue.slang

-40
This file was deleted.

0 commit comments

Comments
 (0)