[clang] 29f8c9f - [WebAssembly] Triple::wasm64 related cleanup

2020-07-16 Thread Wouter van Oortmerssen via cfe-commits

Author: Wouter van Oortmerssen
Date: 2020-07-16T12:01:10-07:00
New Revision: 29f8c9f6c25d50fd21e255060ea36eb0025ca2eb

URL: 
https://github.com/llvm/llvm-project/commit/29f8c9f6c25d50fd21e255060ea36eb0025ca2eb
DIFF: 
https://github.com/llvm/llvm-project/commit/29f8c9f6c25d50fd21e255060ea36eb0025ca2eb.diff

LOG: [WebAssembly] Triple::wasm64 related cleanup

Differential Revision: https://reviews.llvm.org/D83713

Added: 


Modified: 
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

Removed: 




diff  --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index b8c12fc9241a..b7256eb08ac6 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -631,9 +631,7 @@ bool ToolChain::isThreadModelSupported(const StringRef 
Model) const {
 return Triple.getArch() == llvm::Triple::arm ||
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.getArch() == llvm::Triple::thumbeb || Triple.isWasm();
   } else if (Model == "posix")
 return true;
 
@@ -999,9 +997,8 @@ SanitizerMask ToolChain::getSupportedSanitizers() const {
   SanitizerKind::Nullability | SanitizerKind::LocalBounds;
   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().getArch() == llvm::Triple::arm || getTriple().isWasm() ||
+  getTriple().isAArch64())
 Res |= SanitizerKind::CFIICall;
   if (getTriple().getArch() == llvm::Triple::x86_64 || getTriple().isAArch64())
 Res |= SanitizerKind::ShadowCallStack;

diff  --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index cae2852baf86..e8d018f09bf6 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -37,7 +37,7 @@ struct Configuration {
   bool importMemory;
   bool sharedMemory;
   bool importTable;
-  bool is64;
+  llvm::Optional is64;
   bool mergeDataSegments;
   bool pie;
   bool printGcSections;

diff  --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index d0805bf3b303..7307aaa3f7be 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -380,7 +380,6 @@ static void readConfigs(opt::InputArgList &args) {
   args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
 
   // Parse wasm32/64.
-  config->is64 = false;
   if (auto *arg = args.getLastArg(OPT_m)) {
 StringRef s = arg->getValue();
 if (s == "wasm32")
@@ -528,7 +527,7 @@ createUndefinedGlobal(StringRef name, 
llvm::wasm::WasmGlobalType *type) {
 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable,
   int value) {
   llvm::wasm::WasmGlobal wasmGlobal;
-  if (config->is64) {
+  if (config->is64.getValueOr(false)) {
 wasmGlobal.Type = {WASM_TYPE_I64, isMutable};
 wasmGlobal.InitExpr.Value.Int64 = value;
 wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I64_CONST;
@@ -570,16 +569,18 @@ static void createSyntheticSymbols() {
 
 
   if (config->isPic) {
-WasmSym::stackPointer = createUndefinedGlobal(
-"__stack_pointer",
-config->is64 ? &mutableGlobalTypeI64 : &mutableGlobalTypeI32);
+WasmSym::stackPointer =
+createUndefinedGlobal("__stack_pointer", config->is64.getValueOr(false)
+ ? &mutableGlobalTypeI64
+ : &mutableGlobalTypeI32);
 // For PIC code, we import two global variables (__memory_base and
 // __table_base) from the environment and use these as the offset at
 // which to load our static data and function table.
 // See:
 // 
https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
 WasmSym::memoryBase = createUndefinedGlobal(
-"__memory_base", config->is64 ? &globalTypeI64 : &globalTypeI32);
+"__memory_base",
+config->is64.getValueOr(false) ? &globalTypeI64 : &globalTypeI32);
 WasmSym::tableBase = createUndefinedGlobal("__table_base", &globalTypeI32);
 WasmSym::memoryBase->markLive();
 WasmSym::tableBase->markLive();
@@ -604,9 +605,9 @@ static void createSyntheticSymbols() {
 WasmSym::tlsAlign = createGlobalVariable("__tls_align", false, 1);
 WasmSym::initTLS = symtab->addSyntheticFunction(
 "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_H

[clang] 16d83c3 - [WebAssembly] Added 64-bit memory.grow/size/copy/fill

2020-07-06 Thread Wouter van Oortmerssen via cfe-commits

Author: Wouter van Oortmerssen
Date: 2020-07-06T12:49:50-07:00
New Revision: 16d83c395a1f8660fc583a66e1927a5c433fbbe1

URL: 
https://github.com/llvm/llvm-project/commit/16d83c395a1f8660fc583a66e1927a5c433fbbe1
DIFF: 
https://github.com/llvm/llvm-project/commit/16d83c395a1f8660fc583a66e1927a5c433fbbe1.diff

LOG: [WebAssembly] Added 64-bit memory.grow/size/copy/fill

This covers both the existing memory functions as well as the new bulk memory 
proposal.
Added new test files since changes where also required in the inputs.

Also removes unused init/drop intrinsics rather than trying to make them work 
for 64-bit.

Differential Revision: https://reviews.llvm.org/D82821

Added: 
llvm/test/CodeGen/WebAssembly/bulk-memory64.ll
llvm/test/CodeGen/WebAssembly/memory-addr64.ll

Modified: 
clang/include/clang/Basic/BuiltinsWebAssembly.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/test/CodeGen/builtins-wasm.c
llvm/include/llvm/IR/IntrinsicsWebAssembly.td
llvm/lib/Target/WebAssembly/WebAssemblyInstrBulkMemory.td
llvm/lib/Target/WebAssembly/WebAssemblyInstrMemory.td
llvm/lib/Target/WebAssembly/WebAssemblySelectionDAGInfo.cpp
llvm/test/MC/WebAssembly/bulk-memory-encodings.s

Removed: 
llvm/test/CodeGen/WebAssembly/bulk-memory-intrinsics.ll



diff  --git a/clang/include/clang/Basic/BuiltinsWebAssembly.def 
b/clang/include/clang/Basic/BuiltinsWebAssembly.def
index 5e6f0d90ab46..ecee7782920f 100644
--- a/clang/include/clang/Basic/BuiltinsWebAssembly.def
+++ b/clang/include/clang/Basic/BuiltinsWebAssembly.def
@@ -25,10 +25,6 @@
 BUILTIN(__builtin_wasm_memory_size, "zIi", "n")
 BUILTIN(__builtin_wasm_memory_grow, "zIiz", "n")
 
-// Bulk memory builtins
-TARGET_BUILTIN(__builtin_wasm_memory_init, "vIUiIUiv*UiUi", "", "bulk-memory")
-TARGET_BUILTIN(__builtin_wasm_data_drop, "vIUi", "", "bulk-memory")
-
 // Thread-local storage
 TARGET_BUILTIN(__builtin_wasm_tls_size, "z", "nc", "bulk-memory")
 TARGET_BUILTIN(__builtin_wasm_tls_align, "z", "nc", "bulk-memory")

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 265fee392a82..91969267cdb9 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -16101,30 +16101,6 @@ Value 
*CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
 Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_memory_grow, 
ResultType);
 return Builder.CreateCall(Callee, Args);
   }
-  case WebAssembly::BI__builtin_wasm_memory_init: {
-llvm::APSInt SegConst;
-if (!E->getArg(0)->isIntegerConstantExpr(SegConst, getContext()))
-  llvm_unreachable("Constant arg isn't actually constant?");
-llvm::APSInt MemConst;
-if (!E->getArg(1)->isIntegerConstantExpr(MemConst, getContext()))
-  llvm_unreachable("Constant arg isn't actually constant?");
-if (!MemConst.isNullValue())
-  ErrorUnsupported(E, "non-zero memory index");
-Value *Args[] = {llvm::ConstantInt::get(getLLVMContext(), SegConst),
- llvm::ConstantInt::get(getLLVMContext(), MemConst),
- EmitScalarExpr(E->getArg(2)), 
EmitScalarExpr(E->getArg(3)),
- EmitScalarExpr(E->getArg(4))};
-Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_memory_init);
-return Builder.CreateCall(Callee, Args);
-  }
-  case WebAssembly::BI__builtin_wasm_data_drop: {
-llvm::APSInt SegConst;
-if (!E->getArg(0)->isIntegerConstantExpr(SegConst, getContext()))
-  llvm_unreachable("Constant arg isn't actually constant?");
-Value *Arg = llvm::ConstantInt::get(getLLVMContext(), SegConst);
-Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_data_drop);
-return Builder.CreateCall(Callee, {Arg});
-  }
   case WebAssembly::BI__builtin_wasm_tls_size: {
 llvm::Type *ResultType = ConvertType(E->getType());
 Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_tls_size, ResultType);

diff  --git a/clang/test/CodeGen/builtins-wasm.c 
b/clang/test/CodeGen/builtins-wasm.c
index 36d259f7405d..f7e3dc1ea5e7 100644
--- a/clang/test/CodeGen/builtins-wasm.c
+++ b/clang/test/CodeGen/builtins-wasm.c
@@ -26,18 +26,6 @@ __SIZE_TYPE__ memory_grow(__SIZE_TYPE__ delta) {
   // WEBASSEMBLY64: call i64 @llvm.wasm.memory.grow.i64(i32 0, i64 %{{.*}})
 }
 
-void memory_init(void *dest, int offset, int size) {
-  __builtin_wasm_memory_init(3, 0, dest, offset, size);
-  // WEBASSEMBLY32: call void @llvm.wasm.memory.init(i32 3, i32 0, i8* 
%{{.*}}, i32 %{{.*}}, i32 %{{.*}})
-  // WEBASSEMBLY64: call void @llvm.wasm.memory.init(i32 3, i32 0, i8* 
%{{.*}}, i32 %{{.*}}, i32 %{{.*}})
-}
-
-void data_drop() {
-  __builtin_wasm_data_drop(3);
-  // WEBASSEMBLY32: call void @llvm.wasm.data.drop(i32 3)
-  // WEBASSEMBLY64: call void @llvm.wasm.data.drop(i32 3)
-}
-
 __SIZE_TYPE__ tls_size() {
   return __builtin_wasm_tls_size();
   // WEBASSEMBLY32: call i32 @llvm.wasm.tls.size.i32()

diff  --git a/llv

[clang] b9a539c - [WebAssembly] Adding 64-bit versions of __stack_pointer and other globals

2020-06-25 Thread Wouter van Oortmerssen via cfe-commits

Author: Wouter van Oortmerssen
Date: 2020-06-25T15:52:44-07:00
New Revision: b9a539c01084a572e406579c326a0da1e22e286f

URL: 
https://github.com/llvm/llvm-project/commit/b9a539c01084a572e406579c326a0da1e22e286f
DIFF: 
https://github.com/llvm/llvm-project/commit/b9a539c01084a572e406579c326a0da1e22e286f.diff

LOG: [WebAssembly] Adding 64-bit versions of __stack_pointer and other globals

We have 6 globals, all of which except for __table_base are 64-bit under wasm64.

Differential Revision: https://reviews.llvm.org/D82130

Added: 


Modified: 
clang/lib/Driver/ToolChains/WebAssembly.cpp
lld/wasm/Config.h
lld/wasm/Driver.cpp
lld/wasm/InputChunks.cpp
lld/wasm/Options.td
llvm/include/llvm/BinaryFormat/Wasm.h
llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.h
llvm/lib/Target/WebAssembly/WebAssemblyISelDAGToDAG.cpp
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp
llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp
llvm/test/CodeGen/WebAssembly/stack-alignment.ll
llvm/test/CodeGen/WebAssembly/userstack.ll
llvm/test/MC/WebAssembly/stack-ptr.ll
llvm/test/MC/WebAssembly/wasm64.s

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp 
b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index 48f9a9b603db..13d713dfb54e 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -62,6 +62,12 @@ void wasm::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   const char *Linker = Args.MakeArgString(getLinkerPath(Args));
   ArgStringList CmdArgs;
 
+  CmdArgs.push_back("-m");
+  if (getToolChain().getTriple().isArch64Bit())
+CmdArgs.push_back("wasm64");
+  else
+CmdArgs.push_back("wasm32");
+
   if (Args.hasArg(options::OPT_s))
 CmdArgs.push_back("--strip-all");
 

diff  --git a/lld/wasm/Config.h b/lld/wasm/Config.h
index 1ba28899e64d..4a1f7a69d079 100644
--- a/lld/wasm/Config.h
+++ b/lld/wasm/Config.h
@@ -36,6 +36,7 @@ struct Configuration {
   bool importMemory;
   bool sharedMemory;
   bool importTable;
+  bool is64;
   bool mergeDataSegments;
   bool pie;
   bool printGcSections;

diff  --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index c6036111ec79..636aa6509b25 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -378,6 +378,18 @@ static void readConfigs(opt::InputArgList &args) {
   config->exportDynamic =
   args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
 
+  // Parse wasm32/64.
+  config->is64 = false;
+  if (auto *arg = args.getLastArg(OPT_m)) {
+StringRef s = arg->getValue();
+if (s == "wasm32")
+  config->is64 = false;
+else if (s == "wasm64")
+  config->is64 = true;
+else
+  error("invalid target architecture: " + s);
+  }
+
   // --threads= takes a positive integer and provides the default value for
   // --thinlto-jobs=.
   if (auto *arg = args.getLastArg(OPT_threads)) {
@@ -498,9 +510,15 @@ createUndefinedGlobal(StringRef name, 
llvm::wasm::WasmGlobalType *type) {
 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable,
   int value) {
   llvm::wasm::WasmGlobal wasmGlobal;
-  wasmGlobal.Type = {WASM_TYPE_I32, isMutable};
-  wasmGlobal.InitExpr.Value.Int32 = value;
-  wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
+  if (config->is64) {
+wasmGlobal.Type = {WASM_TYPE_I64, isMutable};
+wasmGlobal.InitExpr.Value.Int64 = value;
+wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I64_CONST;
+  } else {
+wasmGlobal.Type = {WASM_TYPE_I32, isMutable};
+wasmGlobal.InitExpr.Value.Int32 = value;
+wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
+  }
   wasmGlobal.SymbolName = name;
   return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN,
 make(wasmGlobal, nullptr));
@@ -513,9 +531,13 @@ static void createSyntheticSymbols() {
 
   static WasmSignature nullSignature = {{}, {}};
   static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
+  static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
   static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
+  static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
   static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
 true};
+  static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
+true};
   WasmSym::callCtors = symtab->addSyntheticFunction(