[PATCH] D43933: [WebAssembly] Pass ownership of body to SyntheticFunction . NFC

Nicholas Wilson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 1 09:09:14 PST 2018


ncw updated this revision to Diff 136548.
ncw marked 4 inline comments as done.
ncw retitled this revision from "[WebAssembly] Pass ownership of body to SyntheticFunction and assert type. NFC" to "[WebAssembly] Pass ownership of body to SyntheticFunction . NFC".
ncw edited the summary of this revision.
ncw added a comment.

Updated with review comments. Thanks!

Assertion pulled out into another review, you're right it's separate.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D43933

Files:
  wasm/InputChunks.h
  wasm/Writer.cpp


Index: wasm/Writer.cpp
===================================================================
--- wasm/Writer.cpp
+++ wasm/Writer.cpp
@@ -17,7 +17,6 @@
 #include "WriterUtils.h"
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Memory.h"
-#include "lld/Common/Strings.h"
 #include "lld/Common/Threads.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/BinaryFormat/Wasm.h"
@@ -137,7 +136,6 @@
   std::vector<OutputSection *> OutputSections;
 
   std::unique_ptr<FileOutputBuffer> Buffer;
-  std::string CtorFunctionBody;
 
   std::vector<OutputSegment *> Segments;
   llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
@@ -859,10 +857,10 @@
   uint32_t FunctionIndex = NumImportedFunctions + InputFunctions.size();
   WasmSym::CallCtors->setOutputIndex(FunctionIndex);
 
-  // First write the body bytes to a string.
-  std::string FunctionBody;
+  // First write the body's contents to a string.
+  std::string BodyContent;
   {
-    raw_string_ostream OS(FunctionBody);
+    raw_string_ostream OS(BodyContent);
     writeUleb128(OS, 0, "num locals");
     for (const WasmInitEntry &F : InitFunctions) {
       writeU8(OS, OPCODE_CALL, "CALL");
@@ -872,14 +870,16 @@
   }
 
   // Once we know the size of the body we can create the final function body
-  raw_string_ostream OS(CtorFunctionBody);
-  writeUleb128(OS, FunctionBody.size(), "function size");
-  OS.flush();
-  CtorFunctionBody += FunctionBody;
+  std::string FunctionBody;
+  {
+    raw_string_ostream OS(FunctionBody);
+    writeUleb128(OS, BodyContent.size(), "function size");
+    OS << BodyContent;
+  }
 
   const WasmSignature *Sig = WasmSym::CallCtors->getFunctionType();
   SyntheticFunction *F = make<SyntheticFunction>(
-      *Sig, toArrayRef(CtorFunctionBody), WasmSym::CallCtors->getName());
+      *Sig, std::move(FunctionBody), WasmSym::CallCtors->getName());
 
   F->setOutputIndex(FunctionIndex);
   F->Live = true;
Index: wasm/InputChunks.h
===================================================================
--- wasm/InputChunks.h
+++ wasm/InputChunks.h
@@ -24,6 +24,7 @@
 #include "Config.h"
 #include "InputFiles.h"
 #include "lld/Common/ErrorHandler.h"
+#include "lld/Common/Strings.h"
 #include "llvm/Object/Wasm.h"
 
 using llvm::object::WasmSegment;
@@ -151,17 +152,16 @@
 
 class SyntheticFunction : public InputFunction {
 public:
-  SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
-                    StringRef Name)
-      : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
+  SyntheticFunction(const WasmSignature &S, std::string Body, StringRef Name)
+      : InputFunction(S, nullptr, nullptr), Name(Name), Body(std::move(Body)) {}
 
   StringRef getName() const override { return Name; }
 
 protected:
-  ArrayRef<uint8_t> data() const override { return Body; }
+  ArrayRef<uint8_t> data() const override { return toArrayRef(Body); }
 
   StringRef Name;
-  ArrayRef<uint8_t> Body;
+  std::string Body;
 };
 
 } // namespace wasm


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43933.136548.patch
Type: text/x-patch
Size: 2976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180301/dd167f1d/attachment-0001.bin>


More information about the llvm-commits mailing list