Skip to content

Commit 537697d

Browse files
authored
Inline global constants for shader style CPU targets (#8686)
On the shader-host-callable target, test `gh-4874.slang` generates IR that contains global constants referencing global params. These need to get inlined into functions, as otherwise `introduceExplicitGlobalContext()` will fail with "no outer func at use site for global", making the test crash the compiler.
1 parent 0257cb0 commit 537697d

5 files changed

Lines changed: 19 additions & 2 deletions

File tree

source/slang/slang-emit.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,13 @@ Result linkAndOptimizeIR(
12701270
// For CUDA targets, always inline global constants to avoid dynamic initialization
12711271
// of __device__ variables rejected by NVRTC. This runs independently of the broader
12721272
// resource/existential type legalization, which remains disabled for CUDA.
1273-
if (target == CodeGenTarget::CUDASource || options.shouldLegalizeExistentialAndResourceTypes)
1273+
//
1274+
// We also need this pass on the CPU targets in shader mode, as global
1275+
// constants may reference global parameters, which can't be emitted as
1276+
// constants.
1277+
if (target == CodeGenTarget::CUDASource ||
1278+
(isCPUTarget(targetRequest) && isKernelTarget(target)) ||
1279+
options.shouldLegalizeExistentialAndResourceTypes)
12741280
{
12751281
inlineGlobalConstantsForLegalization(irModule);
12761282
}

source/slang/slang-ir-legalize-global-values.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ bool GlobalInstInliningContextGeneric::isInlinableGlobalInst(IRInst* inst)
129129
case kIROp_Neq:
130130
case kIROp_Eql:
131131
case kIROp_Call:
132+
case kIROp_Load:
132133
return true;
133134
default:
134135
if (isInlinableGlobalInstForTarget(inst))

source/slang/slang-target.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ bool isCPUTarget(TargetRequest* targetReq);
8383
bool isWGPUTarget(TargetRequest* targetReq);
8484
bool isWGPUTarget(CodeGenTarget target);
8585

86+
// Are we generating code for a Kernel-style target (as opposed to host-style target)
87+
bool isKernelTarget(CodeGenTarget codeGenTarget);
88+
8689
/// A request to generate output in some target format.
8790
class TargetRequest : public RefObject
8891
{

source/slang/slang-type-layout.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,12 @@ bool isWGPUTarget(TargetRequest* targetReq)
27122712
return isWGPUTarget(targetReq->getTarget());
27132713
}
27142714

2715+
bool isKernelTarget(CodeGenTarget codeGenTarget)
2716+
{
2717+
return ArtifactDescUtil::makeDescForCompileTarget(asExternal(codeGenTarget)).style ==
2718+
ArtifactStyle::Kernel;
2719+
}
2720+
27152721
SourceLanguage getIntermediateSourceLanguageForTarget(TargetProgram* targetProgram)
27162722
{
27172723
// If we are emitting directly, there is no intermediate source language

tests/bugs/gh-4874.slang

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
88
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -vk
9+
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj -cpu
910
//
1011
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
1112
RWStructuredBuffer<uint> outputBuffer;
@@ -47,4 +48,4 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
4748
// CHECK: 432
4849
// CHECK: 543
4950
// CHECK: 654
50-
}
51+
}

0 commit comments

Comments
 (0)