Răsfoiți Sursa

Don't emit built-in 'vk' namespace in DeclPrinter (#4803)

Laura Hermanns 2 ani în urmă
părinte
comite
6ef6e91b2a

+ 7 - 0
tools/clang/lib/AST/DeclPrinter.cpp

@@ -852,6 +852,13 @@ void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
 // C++ declarations
 //----------------------------------------------------------------------------
 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
+  // HLSL Change Begin - Don't emit built-in "vk" namespace, it's implicitly
+  // declared when compiling to SPIR-V and would otherwise cause parsing errors
+  // due to unsupported HLSL 2021 features.
+  if (D->getNameAsString() == "vk")
+    return;
+  // HLSL Change End
+
   if (D->isInline())
     Out << "inline ";
   Out << "namespace " << *D << " {\n";

+ 16 - 0
tools/clang/test/HLSL/rewriter/spirv.hlsl

@@ -0,0 +1,16 @@
+
+struct S
+{
+	int4 a;
+	int4 b;
+};
+
+RWStructuredBuffer<S> structuredUAV : register(u0);
+
+RWBuffer<int4> outBufferUAV : register(u1);
+
+[RootSignature("UAV(u0, numDescriptors=2, space=0, offset=DESCRIPTOR_RANGE_OFFSET_APPEND)")]
+[numthreads(1, 1, 1)]
+void main(uint id : SV_DispatchThreadID) {
+	outBufferUAV[id] = structuredUAV[id].a + structuredUAV[id].b;
+}

+ 26 - 0
tools/clang/unittests/HLSL/RewriterTest.cpp

@@ -73,6 +73,7 @@ public:
   TEST_METHOD(RunVectorSyntaxMix);
   TEST_METHOD(RunVectorSyntax);
   TEST_METHOD(RunIncludes);
+  TEST_METHOD(RunSpirv);
   TEST_METHOD(RunStructMethods);
   TEST_METHOD(RunPredefines);
   TEST_METHOD(RunWideOneByte);
@@ -398,6 +399,31 @@ TEST_F(RewriterTest, RunNoFunctionBodyInclude) {
       RewriterOptionMask::SkipFunctionBody));
 }
 
+TEST_F(RewriterTest, RunSpirv) {
+  CComPtr<IDxcRewriter> pRewriter;
+  CComPtr<IDxcRewriter2> pRewriter2;
+  VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
+  VERIFY_SUCCEEDED(pRewriter->QueryInterface(&pRewriter2));
+  CComPtr<IDxcOperationResult> pRewriteResult;
+
+  // Get the source text from a file
+  FileWithBlob source(m_dllSupport,
+                      GetPathToHlslDataFile(L"rewriter\\spirv.hlsl").c_str());
+
+  LPCWSTR compileOptions[] = {L"-E", L"main", L"-HV", L"2021", L"-spirv"};
+
+  // Run rewriter with HLSL 2021 specification and SPIR-V support
+  VERIFY_SUCCEEDED(pRewriter2->RewriteWithOptions(
+      source.BlobEncoding, L"spirv.hlsl", compileOptions,
+      _countof(compileOptions), nullptr, 0, nullptr, &pRewriteResult));
+
+  CComPtr<IDxcBlob> result;
+  VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
+  std::string strResult = BlobToUtf8(result);
+  // No built-in namespace "vk"
+  VERIFY_IS_TRUE(strResult.find("namespace vk") == std::string::npos);
+}
+
 TEST_F(RewriterTest, RunStructMethods) {
   CheckVerifiesHLSL(L"rewriter\\struct-methods.hlsl", L"rewriter\\correct_rewrites\\struct-methods_gold.hlsl");
 }