[llvm-bugs] [Bug 35928] New: [WebAssembly] When compiling a fairly large C++ project, clang generates invalid precompiled module file

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Jan 12 08:39:43 PST 2018


https://bugs.llvm.org/show_bug.cgi?id=35928

            Bug ID: 35928
           Summary: [WebAssembly] When compiling a fairly large C++
                    project, clang generates invalid precompiled module
                    file
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: WebAssembly
          Assignee: unassignedbugs at nondot.org
          Reporter: patrickyccheng at gmail.com
                CC: llvm-bugs at lists.llvm.org

Created attachment 19667
  --> https://bugs.llvm.org/attachment.cgi?id=19667&action=edit
An example of the generated .pcm file

The existing C++ project uses -module-cache-path flag when compiling.

When porting to WebAssembly with -target wasm32-unknown-unknown-wasm

I am getting:
error: file 'XXXXX.pcm' is not a valid precompiled module file

Looks like in this case, the PCM file is generated in WASM Object Format.

And when trying to read the generated file, clang was not able to find the
'__clangast' section.

Here is the file that contains the code that read and write the __clangast
section:

https://reviews.llvm.org/source/clang/browse/cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp

Write:
    auto *ASTSym = new llvm::GlobalVariable(
        *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
        "__clang_ast");
    // The on-disk hashtable needs to be aligned.
    ASTSym->setAlignment(8);
...
      ASTSym->setSection("__CLANG,__clangast");

Read:
    // OF is a llvm::ObjectFile
      for (auto &Section : OF->sections()) {
        StringRef Name;
        Section.getName(Name);
        if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name ==
"clangast")) {

Specifically, ObjectFilePCHContainerReader::ExtractPCH was not able to find the
'__clangast' section.

Here is my "fix" in clang, but expose some of internals of WasmObjectFile, so I
am not sure if this is correct.

I attached one of the generated PCM file for reference.

---

>From 05a85d9c22d2024d6e5d4dcb044690774e658feb Mon Sep 17 00:00:00 2001
From: Patrick Cheng <patrickyccheng at gmail.com>
Date: Fri, 12 Jan 2018 08:27:23 -0800
Subject: [PATCH] Hack to fix "error: file 'XXXXX.pcm' is not a valid
 precompiled module file" when using -target=wasm32-none-none-wasm

---
 lib/CodeGen/ObjectFilePCHContainerOperations.cpp | 32 ++++++++++++++++++------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
index 150b64f..f4ac8e9 100644
--- a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
@@ -31,6 +31,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Object/COFF.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/Wasm.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TargetRegistry.h"
 #include <memory>
@@ -331,14 +332,29 @@
ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
   auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
   if (OFOrErr) {
     auto &OF = OFOrErr.get();
-    bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
-    // Find the clang AST section in the container.
-    for (auto &Section : OF->sections()) {
-      StringRef Name;
-      Section.getName(Name);
-      if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast"))
{
-        Section.getContents(PCH);
-        return PCH;
+    bool IsWASM = isa<llvm::object::WasmObjectFile>(*OF);
+    if (IsWASM) {
+      auto &WF = (std::unique_ptr<llvm::object::WasmObjectFile> &)(OF);
+      const auto &dataSegments = WF->dataSegments();
+      uint32_t count = dataSegments.size();
+      for (uint32_t i = 0; i < count; i++) {
+        auto wasmDataSegment = dataSegments[i].Data;
+        if (wasmDataSegment.Name == "__clangast") {
+          PCH = StringRef(reinterpret_cast<const char
*>(wasmDataSegment.Content.data()),
+                          wasmDataSegment.Content.size());
+          return PCH;
+        }
+      }
+    } else {
+      bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
+      // Find the clang AST section in the container.
+      for (auto &Section : OF->sections()) {
+        StringRef Name;
+        Section.getName(Name);
+        if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name ==
"clangast")) {
+          Section.getContents(PCH);
+          return PCH;
+        }
       }
     }
   }
-- 
2.10.1

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180112/1de8563f/attachment-0001.html>


More information about the llvm-bugs mailing list