DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

[MacOS only] dxc dump nothing when -fcgl with root signature

Open python3kgae opened this issue 2 years ago • 3 comments

Description dxc dump nothing when -fcgl with root signature on macOS.

Steps to Reproduce compile with dxc -Tcs_6_0 -fcgl

StructuredBuffer<matrix> buf1;
RWBuffer<float4> buf2;

[RootSignature("DescriptorTable(SRV(t0), UAV(u0))")]
[numthreads(8, 8, 1)]
void main(uint3 tid : SV_DispatchThreadID) {
  buf2[tid.x] = buf1[tid.x][tid.y];
}

Actual Behavior Nothing dumped in the console. UTF8BufferToWideBuffer failed caused by the serialized root signature.

Environment

  • Host Operating System MacOS

python3kgae avatar Jul 31 '23 18:07 python3kgae

This patch works around the issue by not converting UTF-8 to UTF-32 then back to UTF-8 on Linux and macOS. There is clearly a bug somewhere in our UTF-8->UTF-32 code, but we do so many unnecessary character conversions that we can minimize the impact of the bug by just not converting needlessly.

diff --git a/lib/DxcSupport/dxcapi.use.cpp b/lib/DxcSupport/dxcapi.use.cpp
index 5731db6078f..2a6d229e115 100644
--- a/lib/DxcSupport/dxcapi.use.cpp
+++ b/lib/DxcSupport/dxcapi.use.cpp
@@ -124,6 +124,31 @@ static void WriteWideNullTermToConsole(_In_opt_count_(charCount) const wchar_t *
   }
 }
 
+static void WriteUTF8NullTermToConsole(_In_opt_count_(charCount)
+                                           const char *pText,
+                                       DWORD streamType) {
+  if (pText == nullptr) {
+    return;
+  }
+
+#ifdef _WIN32
+  bool lossy; // Note: even if there was loss,  print anyway
+  std::string consoleMessage;
+  Unicode::UTF8ToConsoleString(pText, &consoleMessage, &lossy);
+  const char *messageCStr = consoleMessage.c_str();
+#else
+  const char *messageCStr = pText;
+#endif
+
+  if (streamType == STD_OUTPUT_HANDLE) {
+    fprintf(stdout, "%s\n", messageCStr);
+  } else if (streamType == STD_ERROR_HANDLE) {
+    fprintf(stderr, "%s\n", messageCStr);
+  } else {
+    throw hlsl::Exception(E_INVALIDARG);
+  }
+}
+
 static HRESULT BlobToUtf8IfText(_In_opt_ IDxcBlob *pBlob, IDxcBlobUtf8 **ppBlobUtf8) {
   CComPtr<IDxcBlobEncoding> pBlobEncoding;
   if (SUCCEEDED(pBlob->QueryInterface(&pBlobEncoding))) {
@@ -236,15 +261,8 @@ void WriteUtf8ToConsole(_In_opt_count_(charCount) const char *pText,
     return;
   }
 
-  std::string resultToPrint;
-  wchar_t *wideMessage = nullptr;
-  size_t wideMessageLen;
-  Unicode::UTF8BufferToWideBuffer(pText, charCount, &wideMessage,
-                                   &wideMessageLen);
-
-  WriteWideNullTermToConsole(wideMessage, streamType);
-
-  delete[] wideMessage;
+  std::string resultToPrint (pText, charCount);
+  WriteUTF8NullTermToConsole(resultToPrint.c_str(), streamType);
 }
 
 void WriteUtf8ToConsoleSizeT(_In_opt_count_(charCount) const char *pText,

llvm-beanz avatar Aug 01 '23 15:08 llvm-beanz

@llvm-beanz - will check to see if this still reproduces after some of the work we did recently to fix locale issues.

damyanp avatar Oct 08 '24 17:10 damyanp

This does still reproduce for me today.

llvm-beanz avatar Oct 09 '24 00:10 llvm-beanz