Skip to content

Commit 1a4ff5c

Browse files
authored
Make DxilSignatureElement::m_SemanticName canonical for system values (microsoft#5889)
When DxilModule metadata is serialized, semantic strings for system values are canonicalized. If these strings are used anywhere, this can lead to a difference in behavior between an original DxilModule that has not undergone serialization and one that has been deserialized. This change canonicalizes the string as soon as a DxilSignatureElement is created, thereby removing the possibility of accidentally relying on the non-canonical string for a system value.
1 parent dc02828 commit 1a4ff5c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/DXIL/DxilSignatureElement.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ void DxilSignatureElement::Initialize(llvm::StringRef Name,
5151
m_SemanticStartIndex = IndexVector[0];
5252
// Find semantic in the table.
5353
m_pSemantic = Semantic::GetByName(m_SemanticName, m_sigPointKind);
54+
// Replace semantic name with canonical name if it's a system value.
55+
if (!m_pSemantic->IsInvalid() && !m_pSemantic->IsArbitrary())
56+
m_SemanticName = m_pSemantic->GetName();
5457
SetCompType(ElementType);
5558
m_InterpMode = InterpMode;
5659
m_SemanticIndex = IndexVector;

tools/clang/unittests/HLSL/DxilModuleTest.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class DxilModuleTest : public ::testing::Test {
7171

7272
TEST_METHOD(PayloadQualifier)
7373

74+
TEST_METHOD(CanonicalSystemValueSemantic)
75+
7476
void VerifyValidatorVersionFails(LPCWSTR shaderModel,
7577
const std::vector<LPCWSTR> &arguments,
7678
const std::vector<LPCSTR> &expectedErrors);
@@ -598,4 +600,24 @@ TEST_F(DxilModuleTest, PayloadQualifier) {
598600
DXIL::PayloadAccessShaderStage::Anyhit));
599601
}
600602
}
601-
}
603+
}
604+
605+
TEST_F(DxilModuleTest, CanonicalSystemValueSemantic) {
606+
// Verify that the semantic name is canonicalized.
607+
608+
// This is difficult to do from a FileCheck test because system value
609+
// semantics get canonicallized during the metadata emit. However, having a
610+
// non-canonical semantic name at earlier stages can lead to inconsistency
611+
// between the strings used earlier and the final canonicalized version. This
612+
// makes sure the string gets canonicalized when the signature elsement is
613+
// created.
614+
615+
std::unique_ptr<hlsl::DxilSignatureElement> newElt =
616+
std::make_unique<hlsl::DxilSignatureElement>(
617+
hlsl::DXIL::SigPointKind::VSOut);
618+
newElt->Initialize("sV_pOsItIoN", hlsl::CompType::getF32(),
619+
hlsl::InterpolationMode(
620+
hlsl::DXIL::InterpolationMode::LinearNoperspective),
621+
1, 4, 0, 0, 0, {0});
622+
VERIFY_ARE_EQUAL_STR("SV_Position", newElt->GetSemanticName().data());
623+
}

0 commit comments

Comments
 (0)