[PATCH] D83713: [WebAssembly] Triple::wasm64 related cleanup

2020-07-16 Thread Wouter van Oortmerssen via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG29f8c9f6c25d: [WebAssembly] Triple::wasm64 related cleanup 
(authored by aardappel).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83713/new/

https://reviews.llvm.org/D83713

Files:
  clang/lib/Driver/ToolChain.cpp
  lld/wasm/Config.h
  lld/wasm/Driver.cpp
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  lld/wasm/SyntheticSections.cpp
  lld/wasm/Writer.cpp
  llvm/include/llvm/Object/Wasm.h
  llvm/lib/Object/WasmObjectFile.cpp

Index: llvm/lib/Object/WasmObjectFile.cpp
===
--- llvm/lib/Object/WasmObjectFile.cpp
+++ llvm/lib/Object/WasmObjectFile.cpp
@@ -957,6 +957,8 @@
   break;
 case wasm::WASM_EXTERNAL_MEMORY:
   Im.Memory = readLimits(Ctx);
+  if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
+HasMemory64 = true;
   break;
 case wasm::WASM_EXTERNAL_TABLE:
   Im.Table = readTable(Ctx);
@@ -1019,7 +1021,10 @@
   uint32_t Count = readVaruint32(Ctx);
   Memories.reserve(Count);
   while (Count--) {
-Memories.push_back(readLimits(Ctx));
+auto Limits = readLimits(Ctx);
+if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
+  HasMemory64 = true;
+Memories.push_back(Limits);
   }
   if (Ctx.Ptr != Ctx.End)
 return make_error("Memory section ended prematurely",
@@ -1576,11 +1581,15 @@
   return section_iterator(SectionRef(Ref, this));
 }
 
-uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
+uint8_t WasmObjectFile::getBytesInAddress() const {
+  return HasMemory64 ? 8 : 4;
+}
 
 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
 
-Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
+Triple::ArchType WasmObjectFile::getArch() const {
+  return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
+}
 
 SubtargetFeatures WasmObjectFile::getFeatures() const {
   return SubtargetFeatures();
Index: llvm/include/llvm/Object/Wasm.h
===
--- llvm/include/llvm/Object/Wasm.h
+++ llvm/include/llvm/Object/Wasm.h
@@ -282,6 +282,7 @@
   bool HasLinkingSection = false;
   bool HasDylinkSection = false;
   bool SeenCodeSection = false;
+  bool HasMemory64 = false;
   wasm::WasmLinkingData LinkingData;
   uint32_t NumImportedGlobals = 0;
   uint32_t NumImportedFunctions = 0;
Index: lld/wasm/Writer.cpp
===
--- lld/wasm/Writer.cpp
+++ lld/wasm/Writer.cpp
@@ -304,7 +304,8 @@
   if (WasmSym::heapBase)
 WasmSym::heapBase->setVirtualAddress(memoryPtr);
 
-  uint64_t maxMemorySetting = 1ULL << (config->is64 ? 48 : 32);
+  uint64_t maxMemorySetting = 1ULL
+  << (config->is64.getValueOr(false) ? 48 : 32);
 
   if (config->initialMemory != 0) {
 if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
Index: lld/wasm/SyntheticSections.cpp
===
--- lld/wasm/SyntheticSections.cpp
+++ lld/wasm/SyntheticSections.cpp
@@ -139,7 +139,7 @@
 }
 if (config->sharedMemory)
   import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
-if (config->is64)
+if (config->is64.getValueOr(false))
   import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
 writeImport(os, import);
   }
@@ -236,7 +236,7 @@
 flags |= WASM_LIMITS_FLAG_HAS_MAX;
   if (config->sharedMemory)
 flags |= WASM_LIMITS_FLAG_IS_SHARED;
-  if (config->is64)
+  if (config->is64.getValueOr(false))
 flags |= WASM_LIMITS_FLAG_IS_64;
   writeUleb128(os, flags, "memory limits flags");
   writeUleb128(os, numMemoryPages, "initial pages");
Index: lld/wasm/InputFiles.cpp
===
--- lld/wasm/InputFiles.cpp
+++ lld/wasm/InputFiles.cpp
@@ -576,10 +576,16 @@
   obj = check(lto::InputFile::create(MemoryBufferRef(
   mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier();
   Triple t(obj->getTargetTriple());
-  if (t.getArch() != Triple::wasm32) {
-error(toString(this) + ": machine type must be wasm32");
+  if (!t.isWasm()) {
+error(toString(this) + ": machine type must be wasm32 or wasm64");
 return;
   }
+  bool is64 = t.getArch() == Triple::wasm64;
+  if (config->is64.hasValue() && *config->is64 != is64) {
+error(toString(this) + ": machine type for all bitcode files must match");
+return;
+  }
+  config->is64 = is64;
   std::vector keptComdats;
   for (StringRef s : obj->getComdatTable())
 keptComdats.push_back(symtab->addComdat(s));
Index: lld/wasm/InputChunks.cpp
===
--- lld/wasm/InputChunks.cpp
+++ lld/wasm/InputChunks.cpp
@@ -335,10 +335,12 @@
   LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
 << " count=" << 

[PATCH] D83713: [WebAssembly] Triple::wasm64 related cleanup

2020-07-13 Thread Wouter van Oortmerssen via Phabricator via cfe-commits
aardappel updated this revision to Diff 277627.
aardappel added a comment.

Made LLD `is64` optional, so we can know if is set consistently.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83713/new/

https://reviews.llvm.org/D83713

Files:
  clang/lib/Driver/ToolChain.cpp
  lld/wasm/Config.h
  lld/wasm/Driver.cpp
  lld/wasm/InputChunks.cpp
  lld/wasm/InputFiles.cpp
  lld/wasm/SyntheticSections.cpp
  lld/wasm/Writer.cpp
  llvm/include/llvm/Object/Wasm.h
  llvm/lib/Object/WasmObjectFile.cpp

Index: llvm/lib/Object/WasmObjectFile.cpp
===
--- llvm/lib/Object/WasmObjectFile.cpp
+++ llvm/lib/Object/WasmObjectFile.cpp
@@ -957,6 +957,8 @@
   break;
 case wasm::WASM_EXTERNAL_MEMORY:
   Im.Memory = readLimits(Ctx);
+  if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
+HasMemory64 = true;
   break;
 case wasm::WASM_EXTERNAL_TABLE:
   Im.Table = readTable(Ctx);
@@ -1019,7 +1021,10 @@
   uint32_t Count = readVaruint32(Ctx);
   Memories.reserve(Count);
   while (Count--) {
-Memories.push_back(readLimits(Ctx));
+auto Limits = readLimits(Ctx);
+if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64)
+  HasMemory64 = true;
+Memories.push_back(Limits);
   }
   if (Ctx.Ptr != Ctx.End)
 return make_error("Memory section ended prematurely",
@@ -1576,11 +1581,15 @@
   return section_iterator(SectionRef(Ref, this));
 }
 
-uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
+uint8_t WasmObjectFile::getBytesInAddress() const {
+  return HasMemory64 ? 8 : 4;
+}
 
 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
 
-Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
+Triple::ArchType WasmObjectFile::getArch() const {
+  return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
+}
 
 SubtargetFeatures WasmObjectFile::getFeatures() const {
   return SubtargetFeatures();
Index: llvm/include/llvm/Object/Wasm.h
===
--- llvm/include/llvm/Object/Wasm.h
+++ llvm/include/llvm/Object/Wasm.h
@@ -282,6 +282,7 @@
   bool HasLinkingSection = false;
   bool HasDylinkSection = false;
   bool SeenCodeSection = false;
+  bool HasMemory64 = false;
   wasm::WasmLinkingData LinkingData;
   uint32_t NumImportedGlobals = 0;
   uint32_t NumImportedFunctions = 0;
Index: lld/wasm/Writer.cpp
===
--- lld/wasm/Writer.cpp
+++ lld/wasm/Writer.cpp
@@ -304,7 +304,8 @@
   if (WasmSym::heapBase)
 WasmSym::heapBase->setVirtualAddress(memoryPtr);
 
-  uint64_t maxMemorySetting = 1ULL << (config->is64 ? 48 : 32);
+  uint64_t maxMemorySetting = 1ULL
+  << (config->is64.getValueOr(false) ? 48 : 32);
 
   if (config->initialMemory != 0) {
 if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
Index: lld/wasm/SyntheticSections.cpp
===
--- lld/wasm/SyntheticSections.cpp
+++ lld/wasm/SyntheticSections.cpp
@@ -139,7 +139,7 @@
 }
 if (config->sharedMemory)
   import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
-if (config->is64)
+if (config->is64.getValueOr(false))
   import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
 writeImport(os, import);
   }
@@ -236,7 +236,7 @@
 flags |= WASM_LIMITS_FLAG_HAS_MAX;
   if (config->sharedMemory)
 flags |= WASM_LIMITS_FLAG_IS_SHARED;
-  if (config->is64)
+  if (config->is64.getValueOr(false))
 flags |= WASM_LIMITS_FLAG_IS_64;
   writeUleb128(os, flags, "memory limits flags");
   writeUleb128(os, numMemoryPages, "initial pages");
Index: lld/wasm/InputFiles.cpp
===
--- lld/wasm/InputFiles.cpp
+++ lld/wasm/InputFiles.cpp
@@ -576,10 +576,16 @@
   obj = check(lto::InputFile::create(MemoryBufferRef(
   mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier();
   Triple t(obj->getTargetTriple());
-  if (t.getArch() != Triple::wasm32) {
-error(toString(this) + ": machine type must be wasm32");
+  if (!t.isWasm()) {
+error(toString(this) + ": machine type must be wasm32 or wasm64");
 return;
   }
+  bool is64 = t.getArch() == Triple::wasm64;
+  if (config->is64.hasValue() && *config->is64 != is64) {
+error(toString(this) + ": machine type for all bitcode files must match");
+return;
+  }
+  config->is64 = is64;
   std::vector keptComdats;
   for (StringRef s : obj->getComdatTable())
 keptComdats.push_back(symtab->addComdat(s));
Index: lld/wasm/InputChunks.cpp
===
--- lld/wasm/InputChunks.cpp
+++ lld/wasm/InputChunks.cpp
@@ -335,10 +335,12 @@
   LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
 << " count=" << relocations.size() << "\n");
 
-  unsigned opcode_ptr_const =
-  

[PATCH] D83713: [WebAssembly] Triple::wasm64 related cleanup

2020-07-13 Thread Sam Clegg via Phabricator via cfe-commits
sbc100 accepted this revision.
sbc100 added inline comments.
This revision is now accepted and ready to land.



Comment at: lld/wasm/InputFiles.cpp:583
   }
+  config->is64 = t.getArch() == Triple::wasm64;
   std::vector keptComdats;

Should we error out here if you try to link two files of different arch?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83713/new/

https://reviews.llvm.org/D83713



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D83713: [WebAssembly] Triple::wasm64 related cleanup

2020-07-13 Thread Wouter van Oortmerssen via Phabricator via cfe-commits
aardappel created this revision.
aardappel added reviewers: dschuff, sbc100.
Herald added subscribers: llvm-commits, cfe-commits, sunfish, aheejin, 
hiraditya, jgravelle-google.
Herald added projects: clang, LLVM.

A few cases that didn't cover Triple::wasm64 correctly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83713

Files:
  clang/lib/Driver/ToolChain.cpp
  lld/wasm/InputFiles.cpp
  llvm/include/llvm/Object/Wasm.h
  llvm/lib/Object/WasmObjectFile.cpp


Index: llvm/lib/Object/WasmObjectFile.cpp
===
--- llvm/lib/Object/WasmObjectFile.cpp
+++ llvm/lib/Object/WasmObjectFile.cpp
@@ -957,6 +957,7 @@
   break;
 case wasm::WASM_EXTERNAL_MEMORY:
   Im.Memory = readLimits(Ctx);
+  if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) HasMemory64 = true;
   break;
 case wasm::WASM_EXTERNAL_TABLE:
   Im.Table = readTable(Ctx);
@@ -1019,7 +1020,9 @@
   uint32_t Count = readVaruint32(Ctx);
   Memories.reserve(Count);
   while (Count--) {
-Memories.push_back(readLimits(Ctx));
+auto Limits = readLimits(Ctx);
+if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64) HasMemory64 = true;
+Memories.push_back(Limits);
   }
   if (Ctx.Ptr != Ctx.End)
 return make_error("Memory section ended prematurely",
@@ -1576,11 +1579,15 @@
   return section_iterator(SectionRef(Ref, this));
 }
 
-uint8_t WasmObjectFile::getBytesInAddress() const { return 4; }
+uint8_t WasmObjectFile::getBytesInAddress() const {
+  return HasMemory64 ? 8 : 4;
+}
 
 StringRef WasmObjectFile::getFileFormatName() const { return "WASM"; }
 
-Triple::ArchType WasmObjectFile::getArch() const { return Triple::wasm32; }
+Triple::ArchType WasmObjectFile::getArch() const {
+  return HasMemory64 ? Triple::wasm64 : Triple::wasm32;
+}
 
 SubtargetFeatures WasmObjectFile::getFeatures() const {
   return SubtargetFeatures();
Index: llvm/include/llvm/Object/Wasm.h
===
--- llvm/include/llvm/Object/Wasm.h
+++ llvm/include/llvm/Object/Wasm.h
@@ -282,6 +282,7 @@
   bool HasLinkingSection = false;
   bool HasDylinkSection = false;
   bool SeenCodeSection = false;
+  bool HasMemory64 = false;
   wasm::WasmLinkingData LinkingData;
   uint32_t NumImportedGlobals = 0;
   uint32_t NumImportedFunctions = 0;
Index: lld/wasm/InputFiles.cpp
===
--- lld/wasm/InputFiles.cpp
+++ lld/wasm/InputFiles.cpp
@@ -576,10 +576,11 @@
   obj = check(lto::InputFile::create(MemoryBufferRef(
   mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier();
   Triple t(obj->getTargetTriple());
-  if (t.getArch() != Triple::wasm32) {
-error(toString(this) + ": machine type must be wasm32");
+  if (!t.isWasm()) {
+error(toString(this) + ": machine type must be wasm32 or wasm64");
 return;
   }
+  config->is64 = t.getArch() == Triple::wasm64;
   std::vector keptComdats;
   for (StringRef s : obj->getComdatTable())
 keptComdats.push_back(symtab->addComdat(s));
Index: clang/lib/Driver/ToolChain.cpp
===
--- clang/lib/Driver/ToolChain.cpp
+++ clang/lib/Driver/ToolChain.cpp
@@ -632,8 +632,7 @@
Triple.getArch() == llvm::Triple::armeb ||
Triple.getArch() == llvm::Triple::thumb ||
Triple.getArch() == llvm::Triple::thumbeb ||
-   Triple.getArch() == llvm::Triple::wasm32 ||
-   Triple.getArch() == llvm::Triple::wasm64;
+   Triple.isWasm();
   } else if (Model == "posix")
 return true;
 
@@ -1000,8 +999,7 @@
   if (getTriple().getArch() == llvm::Triple::x86 ||
   getTriple().getArch() == llvm::Triple::x86_64 ||
   getTriple().getArch() == llvm::Triple::arm ||
-  getTriple().getArch() == llvm::Triple::wasm32 ||
-  getTriple().getArch() == llvm::Triple::wasm64 || getTriple().isAArch64())
+  getTriple().isWasm() || getTriple().isAArch64())
 Res |= SanitizerKind::CFIICall;
   if (getTriple().getArch() == llvm::Triple::x86_64 || getTriple().isAArch64())
 Res |= SanitizerKind::ShadowCallStack;


Index: llvm/lib/Object/WasmObjectFile.cpp
===
--- llvm/lib/Object/WasmObjectFile.cpp
+++ llvm/lib/Object/WasmObjectFile.cpp
@@ -957,6 +957,7 @@
   break;
 case wasm::WASM_EXTERNAL_MEMORY:
   Im.Memory = readLimits(Ctx);
+  if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) HasMemory64 = true;
   break;
 case wasm::WASM_EXTERNAL_TABLE:
   Im.Table = readTable(Ctx);
@@ -1019,7 +1020,9 @@
   uint32_t Count = readVaruint32(Ctx);
   Memories.reserve(Count);
   while (Count--) {
-Memories.push_back(readLimits(Ctx));
+auto Limits = readLimits(Ctx);
+if (Limits.Flags & wasm::WASM_LIMITS_FLAG_IS_64) HasMemory64 = true;
+Memories.push_back(Limits);
   }
   if (Ctx.Ptr != Ctx.End)
 return