Skip to content

Commit

Permalink
SPIRV Fixes. (#3622)
Browse files Browse the repository at this point in the history
* Use SpvSourceLanguageSlang enum.

* Fix spirv entrypoint interface.

* Cleanup.

* Add error on unknown spirv opcode.

* Fix CI.

* Fix.
  • Loading branch information
csyonghe authored Feb 24, 2024
1 parent 401d8cd commit a1827ee
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 25 deletions.
1 change: 1 addition & 0 deletions github_test.sh → .github/github_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ SLANG_TEST=${OUTPUTDIR}slang-test
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OUTPUTDIR
export PATH=$PATH:${OUTPUTDIR}
export SLANG_RUN_SPIRV_VALIDATION=1
export SLANG_USE_SPV_SOURCE_LANGUAGE_UNKNOWN=1
${SLANG_TEST} -bindir ${OUTPUTDIR} -travis -category ${SLANG_TEST_CATEGORY} ${SLANG_TEST_FLAGS} -api all-vk -expected-failure-list tests/expected-failure-github.txt
2 changes: 1 addition & 1 deletion .github/workflows/linux-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:
CC=${{matrix.compiler}}
ARCH=${{matrix.platform}}
PATH="${PATH:+${PATH}:}$(pwd)/external/slang-binaries/spirv-tools/$(uname -m)-linux/bin"
source ./github_test.sh
source .github/github_test.sh
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ jobs:
CC=${{matrix.compiler}}
ARCH=${{matrix.platform}}
PATH="${PATH:+${PATH}:}$(pwd)/external/slang-binaries/spirv-tools/$(uname -m)-linux/bin"
source ./github_test.sh
source .github/github_test.sh
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ jobs:
CONFIGURATION=${{matrix.configuration}}
CC=${{matrix.compiler}}
ARCH=${{matrix.targetPlatform}}
source ./github_test.sh
source .github/github_test.sh
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
$spirvToolsBinDir = ".\external\slang-binaries\spirv-tools\windows-${{matrix.testPlatform}}\bin\";
$env:Path += ";$slangTestBinDir;$spirvToolsBinDir";
$env:SLANG_RUN_SPIRV_VALIDATION='1';
$env:SLANG_USE_SPV_SOURCE_LANGUAGE_UNKNOWN='1';
Expand-Archive "vk_swiftshader_windows_${{matrix.testPlatform}}.zip" -DestinationPath $slangTestBinDir;
& "$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;
2 changes: 1 addition & 1 deletion source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ ${{{{
case BaseType::Float:
case BaseType::Double:
}}}}
[TreatAsDifferentiable]
[Differentiable]
static $(kBaseTypes[tt].name) getPi() { return $(kBaseTypes[tt].name)(3.14159265358979323846264338328); }

__intrinsic_op($(kIROp_Less)) bool lessThan(This other);
Expand Down
35 changes: 14 additions & 21 deletions source/slang/slang-emit-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2816,18 +2816,6 @@ struct SPIRVEmitContext
case kIROp_EntryPointDecoration:
{
auto section = getSection(SpvLogicalSectionID::EntryPoints);

// TODO: The `OpEntryPoint` is required to list an varying
// input or output parameters (by `<id>`) used by the entry point,
// although these are encoded as global variables in the IR.
//
// Currently we have a pass that moves entry-point varying
// parameters to global scope for the benefit of GLSL output,
// but we do not maintain a connection between those parameters
// and the original entry point. That pass should be updated
// to attach a decoration linking the original entry point
// to the new globals, which would be used in the SPIR-V emit case.

auto entryPointDecor = cast<IREntryPointDecoration>(decoration);
auto entryPoint = as<IRFunc>(decoration->getParent());
auto spvStage = mapStageToExecutionModel(entryPointDecor->getProfile().getStage());
Expand All @@ -2842,6 +2830,7 @@ struct SPIRVEmitContext
{
case kIROp_GlobalVar:
case kIROp_GlobalParam:
case kIROp_SPIRVAsmOperandBuiltinVar:
{
SpvInst* spvGlobalInst;
if (m_mapIRInstToSpvInst.tryGetValue(globalInst, spvGlobalInst))
Expand All @@ -2861,13 +2850,6 @@ struct SPIRVEmitContext
break;
}
}
// Add remaining builtin variables that does not have a corresponding IR global var/param.
// These variables could be added from SPIRV ASM blocks.
for (auto builtinVar : m_builtinGlobalVars)
{
if (paramsSet.add(builtinVar.second))
params.add(builtinVar.second);
}
emitOpEntryPoint(
section,
decoration,
Expand Down Expand Up @@ -3283,6 +3265,7 @@ struct SPIRVEmitContext
}

Dictionary<SpvBuiltIn, SpvInst*> m_builtinGlobalVars;

SpvInst* getBuiltinGlobalVar(IRType* type, SpvBuiltIn builtinVal)
{
SpvInst* result = nullptr;
Expand Down Expand Up @@ -5273,9 +5256,19 @@ SlangResult emitSPIRVFromIR(
}

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

for (auto irEntryPoint : irEntryPoints)
Expand Down
17 changes: 17 additions & 0 deletions source/slang/slang-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7221,6 +7221,23 @@ namespace Slang
break;
}
}

if (ret.opcode.flavor == SPIRVAsmOperand::Flavor::NamedValue
&& ret.opcode.knownValue == SpvOp(0xffffffff))
{
if (ret.opcode.token.type == TokenType::IntegerLiteral)
{
Int intVal = -1;
StringUtil::parseInt(ret.opcode.token.getContent(), intVal);
ret.opcode.knownValue = (SpvWord)intVal;
}
else
{
parser->diagnose(ret.opcode.token, Diagnostics::unrecognizedSPIRVOpcode, ret.opcode.token);
return std::nullopt;
}
}

return ret;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/spirv/multi-entry-point-input-var.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//TEST:SIMPLE(filecheck=CHECK): -target spirv -fvk-use-entrypoint-name -emit-spirv-directly

// CHECK: OpEntryPoint

[shader("vertex")]
float4 vmain(uint vertex_id : SV_VertexID) : SV_Position {
return float4(vertex_id, 0, 0, 1);
}

[shader("pixel")]
float4 pmain(float4 position : SV_Position) : SV_Target {
return position;
}
16 changes: 16 additions & 0 deletions tests/spirv/unknown-opcode.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -stage compute -entry main -target spirv -emit-spirv-directly

void main()
{
spirv_asm
{
// CHECK: ([[#@LINE+1]]): error
unknownOpCode
};
spirv_asm
{
// CHECK-NOT: ([[#@LINE+1]]): error
5
};
}

0 comments on commit a1827ee

Please sign in to comment.