Skip to content

Commit 6ef6e91

Browse files
authored
Don't emit built-in 'vk' namespace in DeclPrinter (#4803)
1 parent 54c709f commit 6ef6e91

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

tools/clang/lib/AST/DeclPrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,13 @@ void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
852852
// C++ declarations
853853
//----------------------------------------------------------------------------
854854
void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
855+
// HLSL Change Begin - Don't emit built-in "vk" namespace, it's implicitly
856+
// declared when compiling to SPIR-V and would otherwise cause parsing errors
857+
// due to unsupported HLSL 2021 features.
858+
if (D->getNameAsString() == "vk")
859+
return;
860+
// HLSL Change End
861+
855862
if (D->isInline())
856863
Out << "inline ";
857864
Out << "namespace " << *D << " {\n";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
struct S
3+
{
4+
int4 a;
5+
int4 b;
6+
};
7+
8+
RWStructuredBuffer<S> structuredUAV : register(u0);
9+
10+
RWBuffer<int4> outBufferUAV : register(u1);
11+
12+
[RootSignature("UAV(u0, numDescriptors=2, space=0, offset=DESCRIPTOR_RANGE_OFFSET_APPEND)")]
13+
[numthreads(1, 1, 1)]
14+
void main(uint id : SV_DispatchThreadID) {
15+
outBufferUAV[id] = structuredUAV[id].a + structuredUAV[id].b;
16+
}

tools/clang/unittests/HLSL/RewriterTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class RewriterTest {
7373
TEST_METHOD(RunVectorSyntaxMix);
7474
TEST_METHOD(RunVectorSyntax);
7575
TEST_METHOD(RunIncludes);
76+
TEST_METHOD(RunSpirv);
7677
TEST_METHOD(RunStructMethods);
7778
TEST_METHOD(RunPredefines);
7879
TEST_METHOD(RunWideOneByte);
@@ -398,6 +399,31 @@ TEST_F(RewriterTest, RunNoFunctionBodyInclude) {
398399
RewriterOptionMask::SkipFunctionBody));
399400
}
400401

402+
TEST_F(RewriterTest, RunSpirv) {
403+
CComPtr<IDxcRewriter> pRewriter;
404+
CComPtr<IDxcRewriter2> pRewriter2;
405+
VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
406+
VERIFY_SUCCEEDED(pRewriter->QueryInterface(&pRewriter2));
407+
CComPtr<IDxcOperationResult> pRewriteResult;
408+
409+
// Get the source text from a file
410+
FileWithBlob source(m_dllSupport,
411+
GetPathToHlslDataFile(L"rewriter\\spirv.hlsl").c_str());
412+
413+
LPCWSTR compileOptions[] = {L"-E", L"main", L"-HV", L"2021", L"-spirv"};
414+
415+
// Run rewriter with HLSL 2021 specification and SPIR-V support
416+
VERIFY_SUCCEEDED(pRewriter2->RewriteWithOptions(
417+
source.BlobEncoding, L"spirv.hlsl", compileOptions,
418+
_countof(compileOptions), nullptr, 0, nullptr, &pRewriteResult));
419+
420+
CComPtr<IDxcBlob> result;
421+
VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
422+
std::string strResult = BlobToUtf8(result);
423+
// No built-in namespace "vk"
424+
VERIFY_IS_TRUE(strResult.find("namespace vk") == std::string::npos);
425+
}
426+
401427
TEST_F(RewriterTest, RunStructMethods) {
402428
CheckVerifiesHLSL(L"rewriter\\struct-methods.hlsl", L"rewriter\\correct_rewrites\\struct-methods_gold.hlsl");
403429
}

0 commit comments

Comments
 (0)