Browse Source

Check method when remove unused globals in rewriter. (#2906)

Xiang Li 5 năm trước cách đây
mục cha
commit
59f9b01564

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

@@ -0,0 +1,16 @@
+RWBuffer<uint> u;
+
+struct ST
+{
+	void Store(uint i, uint v)
+	{
+	  u[i] = v;
+	}
+};
+
+[numthreads(8, 8, 1)]
+void main(uint2 id : SV_DispatchThreadID)
+{
+  ST s;
+  s.Store(id.x,id.y);
+}

+ 9 - 0
tools/clang/tools/libclang/dxcrewriteunused.cpp

@@ -95,6 +95,15 @@ public:
     }
     return true;
   }
+  bool VisitCXXMemberCallExpr(CXXMemberCallExpr *expr) {
+    if (FunctionDecl *fnDecl =
+            dyn_cast_or_null<FunctionDecl>(expr->getCalleeDecl())) {
+      if (!m_visitedFunctions.count(fnDecl)) {
+        m_pendingFunctions.push_back(fnDecl);
+      }
+    }
+    return true;
+  }
 };
 
 static void raw_string_ostream_to_CoString(raw_string_ostream &o, _Outptr_result_z_ LPSTR *pResult) {

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

@@ -87,6 +87,7 @@ public:
   TEST_METHOD(RunNoStatic);
   TEST_METHOD(RunKeepUserMacro);
   TEST_METHOD(RunExtractUniforms);
+  TEST_METHOD(RunGlobalsUsedInMethod);
   TEST_METHOD(RunRewriterFails)
 
   dxc::DxcDllSupport m_dllSupport;
@@ -566,6 +567,32 @@ TEST_F(RewriterTest, RunNoStatic) {
   VERIFY_IS_TRUE(strResult.find("static") == std::string::npos);
 }
 
+TEST_F(RewriterTest, RunGlobalsUsedInMethod) {
+  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\\not_remove_globals_used_in_methods.hlsl").c_str());
+
+  LPCWSTR compileOptions[] = {L"-E", L"main", L" -remove-unused-globals"};
+
+  // Run rewrite on the source code to move uniform params to globals
+  VERIFY_SUCCEEDED(pRewriter2->RewriteWithOptions(
+      source.BlobEncoding, L"rewrite-uniforms.hlsl", compileOptions,
+      _countof(compileOptions), nullptr, 0, nullptr, &pRewriteResult));
+
+  CComPtr<IDxcBlob> result;
+  VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
+  std::string strResult = BlobToUtf8(result);
+  // No static.
+  VERIFY_IS_TRUE(strResult.find("RWBuffer<uint> u;") != std::string::npos);
+}
+
 TEST_F(RewriterTest, RunForceExtern) {  CComPtr<IDxcRewriter> pRewriter;
   VERIFY_SUCCEEDED(CreateRewriter(&pRewriter));
   CComPtr<IDxcOperationResult> pRewriteResult;