Skip to content

Commit a1827ee

Browse files
authored
SPIRV Fixes. (shader-slang#3622)
* Use SpvSourceLanguageSlang enum. * Fix spirv entrypoint interface. * Cleanup. * Add error on unknown spirv opcode. * Fix CI. * Fix.
1 parent 401d8cd commit a1827ee

10 files changed

+66
-25
lines changed

github_test.sh .github/github_test.sh

+1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ SLANG_TEST=${OUTPUTDIR}slang-test
3939
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OUTPUTDIR
4040
export PATH=$PATH:${OUTPUTDIR}
4141
export SLANG_RUN_SPIRV_VALIDATION=1
42+
export SLANG_USE_SPV_SOURCE_LANGUAGE_UNKNOWN=1
4243
${SLANG_TEST} -bindir ${OUTPUTDIR} -travis -category ${SLANG_TEST_CATEGORY} ${SLANG_TEST_FLAGS} -api all-vk -expected-failure-list tests/expected-failure-github.txt

.github/workflows/linux-arm64.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ jobs:
5656
CC=${{matrix.compiler}}
5757
ARCH=${{matrix.platform}}
5858
PATH="${PATH:+${PATH}:}$(pwd)/external/slang-binaries/spirv-tools/$(uname -m)-linux/bin"
59-
source ./github_test.sh
59+
source .github/github_test.sh

.github/workflows/linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ jobs:
7272
CC=${{matrix.compiler}}
7373
ARCH=${{matrix.platform}}
7474
PATH="${PATH:+${PATH}:}$(pwd)/external/slang-binaries/spirv-tools/$(uname -m)-linux/bin"
75-
source ./github_test.sh
75+
source .github/github_test.sh

.github/workflows/macos.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ jobs:
6969
CONFIGURATION=${{matrix.configuration}}
7070
CC=${{matrix.compiler}}
7171
ARCH=${{matrix.targetPlatform}}
72-
source ./github_test.sh
72+
source .github/github_test.sh

.github/workflows/windows.yml

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
$spirvToolsBinDir = ".\external\slang-binaries\spirv-tools\windows-${{matrix.testPlatform}}\bin\";
6969
$env:Path += ";$slangTestBinDir;$spirvToolsBinDir";
7070
$env:SLANG_RUN_SPIRV_VALIDATION='1';
71+
$env:SLANG_USE_SPV_SOURCE_LANGUAGE_UNKNOWN='1';
7172
Expand-Archive "vk_swiftshader_windows_${{matrix.testPlatform}}.zip" -DestinationPath $slangTestBinDir;
7273
& "$slangTestBinDir\slang-test.exe" -api all-dx12 -appveyor -bindir "$slangTestBinDir\" -platform ${{matrix.testPlatform}} -configuration ${{matrix.configuration}} -category ${{matrix.testCategory}} -expected-failure-list tests/expected-failure-github.txt 2>&1;
7374

source/slang/core.meta.slang

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ ${{{{
624624
case BaseType::Float:
625625
case BaseType::Double:
626626
}}}}
627-
[TreatAsDifferentiable]
627+
[Differentiable]
628628
static $(kBaseTypes[tt].name) getPi() { return $(kBaseTypes[tt].name)(3.14159265358979323846264338328); }
629629

630630
__intrinsic_op($(kIROp_Less)) bool lessThan(This other);

source/slang/slang-emit-spirv.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -2816,18 +2816,6 @@ struct SPIRVEmitContext
28162816
case kIROp_EntryPointDecoration:
28172817
{
28182818
auto section = getSection(SpvLogicalSectionID::EntryPoints);
2819-
2820-
// TODO: The `OpEntryPoint` is required to list an varying
2821-
// input or output parameters (by `<id>`) used by the entry point,
2822-
// although these are encoded as global variables in the IR.
2823-
//
2824-
// Currently we have a pass that moves entry-point varying
2825-
// parameters to global scope for the benefit of GLSL output,
2826-
// but we do not maintain a connection between those parameters
2827-
// and the original entry point. That pass should be updated
2828-
// to attach a decoration linking the original entry point
2829-
// to the new globals, which would be used in the SPIR-V emit case.
2830-
28312819
auto entryPointDecor = cast<IREntryPointDecoration>(decoration);
28322820
auto entryPoint = as<IRFunc>(decoration->getParent());
28332821
auto spvStage = mapStageToExecutionModel(entryPointDecor->getProfile().getStage());
@@ -2842,6 +2830,7 @@ struct SPIRVEmitContext
28422830
{
28432831
case kIROp_GlobalVar:
28442832
case kIROp_GlobalParam:
2833+
case kIROp_SPIRVAsmOperandBuiltinVar:
28452834
{
28462835
SpvInst* spvGlobalInst;
28472836
if (m_mapIRInstToSpvInst.tryGetValue(globalInst, spvGlobalInst))
@@ -2861,13 +2850,6 @@ struct SPIRVEmitContext
28612850
break;
28622851
}
28632852
}
2864-
// Add remaining builtin variables that does not have a corresponding IR global var/param.
2865-
// These variables could be added from SPIRV ASM blocks.
2866-
for (auto builtinVar : m_builtinGlobalVars)
2867-
{
2868-
if (paramsSet.add(builtinVar.second))
2869-
params.add(builtinVar.second);
2870-
}
28712853
emitOpEntryPoint(
28722854
section,
28732855
decoration,
@@ -3283,6 +3265,7 @@ struct SPIRVEmitContext
32833265
}
32843266

32853267
Dictionary<SpvBuiltIn, SpvInst*> m_builtinGlobalVars;
3268+
32863269
SpvInst* getBuiltinGlobalVar(IRType* type, SpvBuiltIn builtinVal)
32873270
{
32883271
SpvInst* result = nullptr;
@@ -5273,9 +5256,19 @@ SlangResult emitSPIRVFromIR(
52735256
}
52745257

52755258
// Emit source language info.
5259+
// By default we will use SpvSourceLanguageSlang.
5260+
// However this will cause problems when using swiftshader.
5261+
// To workaround this problem, we allow overriding this behavior with an
5262+
// environment variable that will be set in the software testing environment.
5263+
auto sourceLanguage = SpvSourceLanguageSlang;
5264+
StringBuilder noSlangEnv;
5265+
PlatformUtil::getEnvironmentVariable(toSlice("SLANG_USE_SPV_SOURCE_LANGUAGE_UNKNOWN"), noSlangEnv);
5266+
if (noSlangEnv.produceString() == "1")
5267+
{
5268+
sourceLanguage = SpvSourceLanguageUnknown;
5269+
}
52765270
context.emitInst(context.getSection(SpvLogicalSectionID::DebugStringsAndSource), nullptr, SpvOpSource,
5277-
// TODO: update this to SpvSourceLanguageSlang when a new release of spirv-tools is available.
5278-
SpvLiteralInteger::from32(0), // language identifier, should be SpvSourceLanguageSlang.
5271+
SpvLiteralInteger::from32(sourceLanguage), // language identifier, should be SpvSourceLanguageSlang.
52795272
SpvLiteralInteger::from32(1)); // language version.
52805273

52815274
for (auto irEntryPoint : irEntryPoints)

source/slang/slang-parser.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -7221,6 +7221,23 @@ namespace Slang
72217221
break;
72227222
}
72237223
}
7224+
7225+
if (ret.opcode.flavor == SPIRVAsmOperand::Flavor::NamedValue
7226+
&& ret.opcode.knownValue == SpvOp(0xffffffff))
7227+
{
7228+
if (ret.opcode.token.type == TokenType::IntegerLiteral)
7229+
{
7230+
Int intVal = -1;
7231+
StringUtil::parseInt(ret.opcode.token.getContent(), intVal);
7232+
ret.opcode.knownValue = (SpvWord)intVal;
7233+
}
7234+
else
7235+
{
7236+
parser->diagnose(ret.opcode.token, Diagnostics::unrecognizedSPIRVOpcode, ret.opcode.token);
7237+
return std::nullopt;
7238+
}
7239+
}
7240+
72247241
return ret;
72257242
}
72267243

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target spirv -fvk-use-entrypoint-name -emit-spirv-directly
2+
3+
// CHECK: OpEntryPoint
4+
5+
[shader("vertex")]
6+
float4 vmain(uint vertex_id : SV_VertexID) : SV_Position {
7+
return float4(vertex_id, 0, 0, 1);
8+
}
9+
10+
[shader("pixel")]
11+
float4 pmain(float4 position : SV_Position) : SV_Target {
12+
return position;
13+
}

tests/spirv/unknown-opcode.slang

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -stage compute -entry main -target spirv -emit-spirv-directly
3+
4+
void main()
5+
{
6+
spirv_asm
7+
{
8+
// CHECK: ([[#@LINE+1]]): error
9+
unknownOpCode
10+
};
11+
spirv_asm
12+
{
13+
// CHECK-NOT: ([[#@LINE+1]]): error
14+
5
15+
};
16+
}

0 commit comments

Comments
 (0)