[PATCH] D62922: [WebAssembly] Implement "Reactor" mode

2020-06-03 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd496437a0bfd: [WebAssembly] Add support for 
-mexec-model=reactor (authored by sunfishcode).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D62922

Files:
  clang/docs/ClangCommandLineReference.rst
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/WebAssembly.cpp
  clang/test/Driver/wasm-toolchain.c


Index: clang/test/Driver/wasm-toolchain.c
===
--- clang/test/Driver/wasm-toolchain.c
+++ clang/test/Driver/wasm-toolchain.c
@@ -107,3 +107,14 @@
 // RUN: %clang %s -### -fsanitize=address -target wasm32-unknown-emscripten 
2>&1 | FileCheck -check-prefix=CHECK-ASAN-EMSCRIPTEN %s
 // CHECK-ASAN-EMSCRIPTEN: "-fsanitize=address"
 // CHECK-ASAN-EMSCRIPTEN: "-fsanitize-address-globals-dead-stripping"
+
+// Basic exec-model tests.
+
+// RUN: %clang %s -### -no-canonical-prefixes -target wasm32-unknown-unknown 
-mexec-model=command 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-COMMAND %s
+// CHECK-COMMAND: clang{{.*}}" "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// CHECK-COMMAND: wasm-ld{{.*}}" "crt1.o" "[[temp]]" "-lc" 
"{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
+
+// RUN: %clang %s -### -no-canonical-prefixes -target wasm32-unknown-unknown 
-mexec-model=reactor 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-REACTOR %s
+// CHECK-REACTOR: wasm-ld{{.*}}" {{.*}} "--entry" "_initialize" {{.*}}
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -69,8 +69,26 @@
   Args.AddAllArgs(CmdArgs, options::OPT_u);
   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
 
+  const char *Crt1 = "crt1.o";
+  const char *Entry = NULL;
+  if (const Arg *A = Args.getLastArg(options::OPT_mexec_model_EQ)) {
+StringRef CM = A->getValue();
+if (CM == "command") {
+  // Use default values.
+} else if (CM == "reactor") {
+  Crt1 = "crt1-reactor.o";
+  Entry = "_initialize";
+} else {
+  ToolChain.getDriver().Diag(diag::err_drv_invalid_argument_to_option)
+  << CM << A->getOption().getName();
+}
+  }
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
-CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
+CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(Crt1)));
+  if (Entry) {
+CmdArgs.push_back(Args.MakeArgString("--entry"));
+CmdArgs.push_back(Args.MakeArgString(Entry));
+  }
 
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
 
Index: clang/include/clang/Driver/Options.td
===
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -171,6 +171,10 @@
Group, DocName<"PowerPC">;
 def m_wasm_Features_Group : OptionGroup<"">,
 Group, DocName<"WebAssembly">;
+// The features added by this group will not be added to target features.
+// These are explicitly handled.
+def m_wasm_Features_Driver_Group : OptionGroup<"">,
+   Group, DocName<"WebAssembly 
Driver">;
 def m_x86_Features_Group : OptionGroup<"">,
Group, Flags<[CoreOption]>, DocName<"X86">;
 def m_riscv_Features_Group : OptionGroup<"">,
@@ -2456,6 +2460,9 @@
 def mno_tail_call : Flag<["-"], "mno-tail-call">, Group;
 def mreference_types : Flag<["-"], "mreference-types">, 
Group;
 def mno_reference_types : Flag<["-"], "mno-reference-types">, 
Group;
+def mexec_model_EQ : Joined<["-"], "mexec-model=">, 
Group,
+ Values<"command,reactor">,
+ HelpText<"Execution model (WebAssembly only)">;
 
 def mamdgpu_debugger_abi : Joined<["-"], "mamdgpu-debugger-abi=">,
   Flags<[HelpHidden]>,
Index: clang/docs/ClangCommandLineReference.rst
===
--- clang/docs/ClangCommandLineReference.rst
+++ clang/docs/ClangCommandLineReference.rst
@@ -3051,6 +3051,12 @@
 
 .. option:: -munimplemented-simd128, -mno-unimplemented-simd128
 
+.. option:: -mexec-model=
+
+Select between "command" and "reactor" executable models. Commands have a main
+function which scopes the lifetime of the program. Reactors are activated and
+remain active until explicitly terminated.
+
 X86
 ---
 .. option:: -m3dnow, -mno-3dnow


Index: clang/test/Driver/wasm-toolchain.c
===
--- clang/test/Driver/wasm-toolchain.c
+++ clang/test/Driver/wasm-toolchain.c
@@ -107,3 +107,14 @@
 // RUN: %clang %s -### -fsanitize=address -target wasm32-unknown-emscripten 2>&1 | FileCheck -check-prefix=CHECK-ASAN-EMSCRIPTEN %s
 // CHECK-ASAN-EMSCRIPTEN: 

[PATCH] D59520: [WebAssembly] Address review comments on r352930

2020-06-05 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG931fcd3ba011: [WebAssembly] Improve clang diagnostics for 
wasm attributes (authored by sunfishcode).

Changed prior to commit:
  https://reviews.llvm.org/D59520?vs=268329&id=268900#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D59520

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/AST/ast-dump-wasm-attr-export.c
  clang/test/AST/ast-dump-wasm-attr-import.c
  clang/test/Sema/attr-wasm.c

Index: clang/test/Sema/attr-wasm.c
===
--- /dev/null
+++ clang/test/Sema/attr-wasm.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fsyntax-only -verify %s
+
+void name_a() __attribute__((import_name)); //expected-error {{'import_name' attribute takes one argument}}
+
+int name_b __attribute__((import_name("foo"))); //expected-error {{'import_name' attribute only applies to functions}}
+
+void name_c() __attribute__((import_name("foo", "bar"))); //expected-error {{'import_name' attribute takes one argument}}
+
+void name_d() __attribute__((import_name("foo", "bar", "qux"))); //expected-error {{'import_name' attribute takes one argument}}
+
+void name_z() __attribute__((import_name("foo"))); //expected-note {{previous attribute is here}}
+
+void name_z() __attribute__((import_name("bar"))); //expected-warning {{import name (bar) does not match the import name (foo) of the previous declaration}}
+
+void module_a() __attribute__((import_module)); //expected-error {{'import_module' attribute takes one argument}}
+
+int module_b __attribute__((import_module("foo"))); //expected-error {{'import_module' attribute only applies to functions}}
+
+void module_c() __attribute__((import_module("foo", "bar"))); //expected-error {{'import_module' attribute takes one argument}}
+
+void module_d() __attribute__((import_module("foo", "bar", "qux"))); //expected-error {{'import_module' attribute takes one argument}}
+
+void module_z() __attribute__((import_module("foo"))); //expected-note {{previous attribute is here}}
+
+void module_z() __attribute__((import_module("bar"))); //expected-warning {{import module (bar) does not match the import module (foo) of the previous declaration}}
+
+void both() __attribute__((import_name("foo"), import_module("bar")));
Index: clang/test/AST/ast-dump-wasm-attr-import.c
===
--- /dev/null
+++ clang/test/AST/ast-dump-wasm-attr-import.c
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown -ast-dump %s | FileCheck --strict-whitespace %s
+
+// Test that functions can be redeclared and they retain their attributes.
+
+__attribute__((import_name("import_red"), import_module("mod"))) void red(void);
+__attribute__((import_name("import_orange"), import_module("mod"))) void orange(void);
+__attribute__((import_name("import_yellow"), import_module("mod"))) void yellow(void);
+
+void red(void);
+void orange(void);
+void yellow(void);
+
+void calls(void) {
+red();
+orange();
+yellow();
+}
+
+// CHECK: |-FunctionDecl {{.+}} used red 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} "import_red"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} "mod"
+// CHECK: |-FunctionDecl {{.+}} used orange 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} "import_orange"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} "mod"
+// CHECK: |-FunctionDecl {{.+}} used yellow 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} "import_yellow"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} "mod"
+// CHECK: |-FunctionDecl {{.+}} used red 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} Inherited "import_red"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} Inherited "mod"
+// CHECK: |-FunctionDecl {{.+}} used orange 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} Inherited "import_orange"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} Inherited "mod"
+// CHECK: |-FunctionDecl {{.+}} used yellow 'void (void)'
+// CHECK: | |-WebAssemblyImportNameAttr {{.+}} Inherited "import_yellow"
+// CHECK: | `-WebAssemblyImportModuleAttr {{.+}} Inherited "mod"
Index: clang/test/AST/ast-dump-wasm-attr-export.c
===
--- /dev/null
+++ clang/test/AST/ast-dump-wasm-attr-export.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown -ast-dump %s | FileCheck --strict-whitespace %s
+
+// Test that functions can be redeclared and they retain their attributes.
+
+__attribute__((export_name("export_red"))) void red(void) {}
+__attribute__((export_name("export_orange"))) void orange(void) {}
+__attribute__((export_name("export_yellow"))) void yellow(voi

[PATCH] D85074: [WebAssembly] Use "signed char" instead of "char" in SIMD intrinsics.

2020-08-04 Thread sunfishcode via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG47f7174ffa71: [WebAssembly] Use "signed char" 
instead of "char" in SIMD intrinsics. (authored by sunfishcode).

Changed prior to commit:
  https://reviews.llvm.org/D85074?vs=282608&id=283004#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D85074

Files:
  clang/include/clang/Basic/BuiltinsWebAssembly.def
  clang/lib/Headers/wasm_simd128.h
  clang/test/CodeGen/builtins-wasm.c

Index: clang/test/CodeGen/builtins-wasm.c
===
--- clang/test/CodeGen/builtins-wasm.c
+++ clang/test/CodeGen/builtins-wasm.c
@@ -3,7 +3,7 @@
 // RUN: not %clang_cc1 -triple wasm64-unknown-unknown -target-feature +nontrapping-fptoint -target-feature +exception-handling -target-feature +bulk-memory -target-feature +atomics -flax-vector-conversions=none -O3 -emit-llvm -o - %s 2>&1 | FileCheck %s -check-prefixes MISSING-SIMD
 
 // SIMD convenience types
-typedef char i8x16 __attribute((vector_size(16)));
+typedef signed char i8x16 __attribute((vector_size(16)));
 typedef short i16x8 __attribute((vector_size(16)));
 typedef int i32x4 __attribute((vector_size(16)));
 typedef long long i64x2 __attribute((vector_size(16)));
@@ -201,7 +201,7 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-int extract_lane_u_i8x16(i8x16 v) {
+int extract_lane_u_i8x16(u8x16 v) {
   return __builtin_wasm_extract_lane_u_i8x16(v, 13);
   // WEBASSEMBLY: extractelement <16 x i8> %v, i32 13
   // WEBASSEMBLY-NEXT: zext
@@ -215,7 +215,7 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-int extract_lane_u_i16x8(i16x8 v) {
+int extract_lane_u_i16x8(u16x8 v) {
   return __builtin_wasm_extract_lane_u_i16x8(v, 7);
   // WEBASSEMBLY: extractelement <8 x i16> %v, i32 7
   // WEBASSEMBLY-NEXT: zext
@@ -291,7 +291,7 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-i8x16 add_saturate_u_i8x16(i8x16 x, i8x16 y) {
+u8x16 add_saturate_u_i8x16(u8x16 x, u8x16 y) {
   return __builtin_wasm_add_saturate_u_i8x16(x, y);
   // WEBASSEMBLY: call <16 x i8> @llvm.uadd.sat.v16i8(
   // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y)
@@ -305,7 +305,7 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-i16x8 add_saturate_u_i16x8(i16x8 x, i16x8 y) {
+u16x8 add_saturate_u_i16x8(u16x8 x, u16x8 y) {
   return __builtin_wasm_add_saturate_u_i16x8(x, y);
   // WEBASSEMBLY: call <8 x i16> @llvm.uadd.sat.v8i16(
   // WEBASSEMBLY-SAME: <8 x i16> %x, <8 x i16> %y)
@@ -319,7 +319,7 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-i8x16 sub_saturate_u_i8x16(i8x16 x, i8x16 y) {
+u8x16 sub_saturate_u_i8x16(u8x16 x, u8x16 y) {
   return __builtin_wasm_sub_saturate_u_i8x16(x, y);
   // WEBASSEMBLY: call <16 x i8> @llvm.wasm.sub.saturate.unsigned.v16i8(
   // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y)
@@ -357,7 +357,7 @@
   // WEBASSEMBLY-NEXT: ret <16 x i8> %1
 }
 
-i8x16 min_u_i8x16(i8x16 x, i8x16 y) {
+u8x16 min_u_i8x16(u8x16 x, u8x16 y) {
   return __builtin_wasm_min_u_i8x16(x, y);
   // WEBASSEMBLY: %0 = icmp ult <16 x i8> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
@@ -371,7 +371,7 @@
   // WEBASSEMBLY-NEXT: ret <16 x i8> %1
 }
 
-i8x16 max_u_i8x16(i8x16 x, i8x16 y) {
+u8x16 max_u_i8x16(u8x16 x, u8x16 y) {
   return __builtin_wasm_max_u_i8x16(x, y);
   // WEBASSEMBLY: %0 = icmp ugt <16 x i8> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <16 x i1> %0, <16 x i8> %x, <16 x i8> %y
@@ -385,7 +385,7 @@
   // WEBASSEMBLY-NEXT: ret <8 x i16> %1
 }
 
-i16x8 min_u_i16x8(i16x8 x, i16x8 y) {
+u16x8 min_u_i16x8(u16x8 x, u16x8 y) {
   return __builtin_wasm_min_u_i16x8(x, y);
   // WEBASSEMBLY: %0 = icmp ult <8 x i16> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
@@ -399,7 +399,7 @@
   // WEBASSEMBLY-NEXT: ret <8 x i16> %1
 }
 
-i16x8 max_u_i16x8(i16x8 x, i16x8 y) {
+u16x8 max_u_i16x8(u16x8 x, u16x8 y) {
   return __builtin_wasm_max_u_i16x8(x, y);
   // WEBASSEMBLY: %0 = icmp ugt <8 x i16> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <8 x i1> %0, <8 x i16> %x, <8 x i16> %y
@@ -413,7 +413,7 @@
   // WEBASSEMBLY-NEXT: ret <4 x i32> %1
 }
 
-i32x4 min_u_i32x4(i32x4 x, i32x4 y) {
+u32x4 min_u_i32x4(u32x4 x, u32x4 y) {
   return __builtin_wasm_min_u_i32x4(x, y);
   // WEBASSEMBLY: %0 = icmp ult <4 x i32> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
@@ -427,7 +427,7 @@
   // WEBASSEMBLY-NEXT: ret <4 x i32> %1
 }
 
-i32x4 max_u_i32x4(i32x4 x, i32x4 y) {
+u32x4 max_u_i32x4(u32x4 x, u32x4 y) {
   return __builtin_wasm_max_u_i32x4(x, y);
   // WEBASSEMBLY: %0 = icmp ugt <4 x i32> %x, %y
   // WEBASSEMBLY-NEXT: %1 = select <4 x i1> %0, <4 x i32> %x, <4 x i32> %y
@@ -441,21 +441,21 @@
   // WEBASSEMBLY-NEXT: ret
 }
 
-i16x8 sub_saturate_u_i16x8(i16x8 x, i16x8 y) {
+u16x8 sub_saturate_u_i16x8(u16x8 x, u16x8 y) {
   return __builtin_wasm_sub_saturate_u_i16x8(x, y);
   // WEBASSEMBLY: call <8 x

[PATCH] D70700: [WebAssembly] Mangle the argc/argv `main` as `__main_argc_argv`

2020-02-27 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG00072c08c750: [WebAssembly] Mangle the argc/argv `main` as 
`__wasm_argc_argv`. (authored by sunfishcode).

Changed prior to commit:
  https://reviews.llvm.org/D70700?vs=234942&id=247011#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70700

Files:
  clang/lib/AST/Mangle.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Frontend/InitHeaderSearch.cpp
  clang/test/CodeGen/wasm-call-main.c
  clang/test/CodeGen/wasm-main.c
  clang/test/CodeGen/wasm-main_argc_argv.c

Index: clang/test/CodeGen/wasm-main_argc_argv.c
===
--- /dev/null
+++ clang/test/CodeGen/wasm-main_argc_argv.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm %s | FileCheck %s
+
+// Mangle the argc/argv form of main.
+
+int main(int argc, char **argv) {
+  return 0;
+}
+
+// CHECK-LABEL: define i32 @__main_argc_argv(i32 %argc, i8** %argv)
Index: clang/test/CodeGen/wasm-main.c
===
--- /dev/null
+++ clang/test/CodeGen/wasm-main.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm %s | FileCheck %s
+
+// Don't mangle the no-arg form of main.
+
+int main(void) {
+  return 0;
+}
+
+// CHECK-LABEL: define i32 @main()
Index: clang/test/CodeGen/wasm-call-main.c
===
--- /dev/null
+++ clang/test/CodeGen/wasm-call-main.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm %s | FileCheck %s
+
+// Mangle argc/argv main even when it's not defined in this TU.
+
+#include 
+
+int main(int argc, char *argv[]);
+
+int foo(void) {
+return main(0, NULL);
+}
+
+// CHECK: call i32 @__main_argc_argv(
Index: clang/lib/Frontend/InitHeaderSearch.cpp
===
--- clang/lib/Frontend/InitHeaderSearch.cpp
+++ clang/lib/Frontend/InitHeaderSearch.cpp
@@ -433,8 +433,7 @@
 break;
 
   case llvm::Triple::UnknownOS:
-if (triple.getArch() == llvm::Triple::wasm32 ||
-triple.getArch() == llvm::Triple::wasm64)
+if (triple.isWasm())
   return;
 break;
   }
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1023,6 +1023,9 @@
   /// for the uninstrumented functions.
   void EmitDeferredUnusedCoverageMappings();
 
+  /// Emit an alias for "main" if it has no arguments (needed for wasm).
+  void EmitMainVoidAlias();
+
   /// Tell the consumer that this variable has been instantiated.
   void HandleCXXStaticMemberVarInstantiation(VarDecl *VD);
 
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -448,6 +448,10 @@
 CodeGenFunction(*this).EmitCfiCheckStub();
   }
   emitAtAvailableLinkGuard();
+  if (Context.getTargetInfo().getTriple().isWasm() &&
+  !Context.getTargetInfo().getTriple().isOSEmscripten()) {
+EmitMainVoidAlias();
+  }
   emitLLVMUsed();
   if (SanStats)
 SanStats->finish();
@@ -5600,6 +5604,17 @@
   }
 }
 
+void CodeGenModule::EmitMainVoidAlias() {
+  // In order to transition away from "__original_main" gracefully, emit an
+  // alias for "main" in the no-argument case so that libc can detect when
+  // new-style no-argument main is in used.
+  if (llvm::Function *F = getModule().getFunction("main")) {
+if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
+F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth()))
+  addUsedGlobal(llvm::GlobalAlias::create("__main_void", F));
+  }
+}
+
 /// Turns the given pointer into a constant.
 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
   const void *Ptr) {
Index: clang/lib/AST/Mangle.cpp
===
--- clang/lib/AST/Mangle.cpp
+++ clang/lib/AST/Mangle.cpp
@@ -50,7 +50,8 @@
   CCM_Fast,
   CCM_RegCall,
   CCM_Vector,
-  CCM_Std
+  CCM_Std,
+  CCM_WasmMainArgcArgv
 };
 
 static bool isExternC(const NamedDecl *ND) {
@@ -63,6 +64,16 @@
  const NamedDecl *ND) {
   const TargetInfo &TI = Context.getTargetInfo();
   const llvm::Triple &Triple = TI.getTriple();
+
+  // On wasm, the argc/argv form of "main" is renamed so that the startup code
+  // can call it with the correct function signature.
+  // On Emscripten, users may be exporting "main" and expecting to call it
+  // themselves, so we can't mangle it.
+  if (Triple.isWasm() && !Triple.isOSEmscripten())
+if (const FunctionDecl *FD = dyn_cast(ND))

[PATCH] D70500: [WebAssembly] Enable use of wasm-opt and LTO-enabled system libraries

2019-11-22 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG812828984c10: [WebAssembly] Use wasm-opt and LTO libraries 
when available. (authored by sunfishcode).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70500

Files:
  clang/lib/Driver/ToolChains/WebAssembly.cpp
  clang/test/Driver/wasm-toolchain-lto.c


Index: clang/test/Driver/wasm-toolchain-lto.c
===
--- /dev/null
+++ clang/test/Driver/wasm-toolchain-lto.c
@@ -0,0 +1,6 @@
+// A basic C link command-line with optimization with known OS and LTO enabled.
+
+// RUN: %clang -### -O2 -flto -no-canonical-prefixes -target wasm32-wasi 
--sysroot=/foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=LINK_OPT_KNOWN %s
+// LINK_OPT_KNOWN: clang{{.*}}" "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_OPT_KNOWN: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi/llvm-lto/
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -8,6 +8,7 @@
 
 #include "WebAssembly.h"
 #include "CommonArgs.h"
+#include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
@@ -90,6 +91,31 @@
   CmdArgs.push_back(Output.getFilename());
 
   C.addCommand(std::make_unique(JA, *this, Linker, CmdArgs, Inputs));
+
+  // When optimizing, if wasm-opt is in the PATH, run wasm-opt.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+if (llvm::ErrorOr WasmOptPath =
+   llvm::sys::findProgramByName("wasm-opt")) {
+  StringRef OOpt = "s";
+  if (A->getOption().matches(options::OPT_O4) ||
+  A->getOption().matches(options::OPT_Ofast))
+OOpt = "4";
+  else if (A->getOption().matches(options::OPT_O0))
+OOpt = "0";
+  else if (A->getOption().matches(options::OPT_O))
+OOpt = A->getValue();
+
+  if (OOpt != "0") {
+const char *WasmOpt = Args.MakeArgString(*WasmOptPath);
+ArgStringList CmdArgs;
+CmdArgs.push_back(Output.getFilename());
+CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
+CmdArgs.push_back("-o");
+CmdArgs.push_back(Output.getFilename());
+C.addCommand(std::make_unique(JA, *this, WasmOpt, CmdArgs, 
Inputs));
+  }
+}
+  }
 }
 
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
@@ -109,6 +135,16 @@
   } else {
 const std::string MultiarchTriple =
 getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
+if (D.isUsingLTO()) {
+  auto LLVMRevision = getLLVMRevision();
+  if (!LLVMRevision.empty()) {
+// For LTO, enable use of lto-enabled sysroot libraries too, if 
available.
+// Note that the directory is keyed to the LLVM revision, as LLVM's
+// bitcode format is not stable.
+getFilePaths().push_back(getDriver().SysRoot + "/lib/" + 
MultiarchTriple +
+ "/llvm-lto/" + LLVMRevision);
+  }
+}
 getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
   }
 }


Index: clang/test/Driver/wasm-toolchain-lto.c
===
--- /dev/null
+++ clang/test/Driver/wasm-toolchain-lto.c
@@ -0,0 +1,6 @@
+// A basic C link command-line with optimization with known OS and LTO enabled.
+
+// RUN: %clang -### -O2 -flto -no-canonical-prefixes -target wasm32-wasi --sysroot=/foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=LINK_OPT_KNOWN %s
+// LINK_OPT_KNOWN: clang{{.*}}" "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_OPT_KNOWN: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi/llvm-lto/
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -8,6 +8,7 @@
 
 #include "WebAssembly.h"
 #include "CommonArgs.h"
+#include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
@@ -90,6 +91,31 @@
   CmdArgs.push_back(Output.getFilename());
 
   C.addCommand(std::make_unique(JA, *this, Linker, CmdArgs, Inputs));
+
+  // When optimizing, if wasm-opt is in the PATH, run wasm-opt.
+  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+if (llvm::ErrorOr WasmOptPath =
+   llvm::sys::findProgramByName("wasm-opt")) {
+  StringRef OOpt = "s";
+  if (A->getOption().matches(options::OPT_O4) ||
+  A->getOption().matches(options::OPT_Ofast))
+OOpt = "4";
+  else if (A->getOption().matches(options::OPT_O0))
+OOpt = "0";
+  else if (A->getOption().matches(options::OPT_O))
+OOpt = A->getValue();
+
+  if (OOpt

[PATCH] D70677: [WebAssembly] Change the llvm-lto dir to use the LLVM Version

2019-11-25 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG872a53ef9489: [WebAssembly] Change the llvm-lto dir to use 
the LLVM Version (authored by sunfishcode).

Changed prior to commit:
  https://reviews.llvm.org/D70677?vs=230919&id=230934#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70677

Files:
  clang/lib/Driver/ToolChains/WebAssembly.cpp


Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -118,6 +118,14 @@
   }
 }
 
+/// Given a base library directory, append path components to form the
+/// LTO directory.
+static std::string AppendLTOLibDir(const std::string &Dir) {
+// The version allows the path to be keyed to the specific version of
+// LLVM in used, as the bitcode format is not stable.
+return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
+}
+
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
  const llvm::opt::ArgList &Args)
 : ToolChain(D, Triple, Args) {
@@ -126,26 +134,24 @@
 
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
+  auto SysRoot = getDriver().SysRoot;
   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
 // Theoretically an "unknown" OS should mean no standard libraries, however
 // it could also mean that a custom set of libraries is in use, so just add
 // /lib to the search path. Disable multiarch in this case, to discourage
 // paths containing "unknown" from acquiring meanings.
-getFilePaths().push_back(getDriver().SysRoot + "/lib");
+getFilePaths().push_back(SysRoot + "/lib");
   } else {
 const std::string MultiarchTriple =
-getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
+getMultiarchTriple(getDriver(), Triple, SysRoot);
 if (D.isUsingLTO()) {
-  auto LLVMRevision = getLLVMRevision();
-  if (!LLVMRevision.empty()) {
-// For LTO, enable use of lto-enabled sysroot libraries too, if 
available.
-// Note that the directory is keyed to the LLVM revision, as LLVM's
-// bitcode format is not stable.
-getFilePaths().push_back(getDriver().SysRoot + "/lib/" + 
MultiarchTriple +
- "/llvm-lto/" + LLVMRevision);
-  }
+  // For LTO, enable use of lto-enabled sysroot libraries too, if 
available.
+  // Note that the directory is keyed to the LLVM revision, as LLVM's
+  // bitcode format is not stable.
+  auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
+  getFilePaths().push_back(Dir);
 }
-getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple);
+getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
   }
 }
 


Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -118,6 +118,14 @@
   }
 }
 
+/// Given a base library directory, append path components to form the
+/// LTO directory.
+static std::string AppendLTOLibDir(const std::string &Dir) {
+// The version allows the path to be keyed to the specific version of
+// LLVM in used, as the bitcode format is not stable.
+return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
+}
+
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
  const llvm::opt::ArgList &Args)
 : ToolChain(D, Triple, Args) {
@@ -126,26 +134,24 @@
 
   getProgramPaths().push_back(getDriver().getInstalledDir());
 
+  auto SysRoot = getDriver().SysRoot;
   if (getTriple().getOS() == llvm::Triple::UnknownOS) {
 // Theoretically an "unknown" OS should mean no standard libraries, however
 // it could also mean that a custom set of libraries is in use, so just add
 // /lib to the search path. Disable multiarch in this case, to discourage
 // paths containing "unknown" from acquiring meanings.
-getFilePaths().push_back(getDriver().SysRoot + "/lib");
+getFilePaths().push_back(SysRoot + "/lib");
   } else {
 const std::string MultiarchTriple =
-getMultiarchTriple(getDriver(), Triple, getDriver().SysRoot);
+getMultiarchTriple(getDriver(), Triple, SysRoot);
 if (D.isUsingLTO()) {
-  auto LLVMRevision = getLLVMRevision();
-  if (!LLVMRevision.empty()) {
-// For LTO, enable use of lto-enabled sysroot libraries too, if available.
-// Note that the directory is keyed to the LLVM revision, as LLVM's
-// bitcode format is not stable.
-getFilePaths().push_back(getDriver().SysRoot + "/lib/" + MultiarchTriple +
- "/llvm-lto/" + LLVMRevision);
-  }
+  // For LTO, enable use of lto-en

[PATCH] D70780: [WebAssembly] Find wasm-opt with GetProgramPath

2019-12-02 Thread sunfishcode via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8f1e2151b8e9: [WebAssembly] Find wasm-opt with 
GetProgramPath (authored by sunfishcode).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70780

Files:
  clang/lib/Driver/ToolChains/WebAssembly.cpp


Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -92,10 +92,10 @@
 
   C.addCommand(std::make_unique(JA, *this, Linker, CmdArgs, Inputs));
 
-  // When optimizing, if wasm-opt is in the PATH, run wasm-opt.
+  // When optimizing, if wasm-opt is available, run it.
   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
-if (llvm::ErrorOr WasmOptPath =
-   llvm::sys::findProgramByName("wasm-opt")) {
+auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
+if (WasmOptPath != "wasm-opt") {
   StringRef OOpt = "s";
   if (A->getOption().matches(options::OPT_O4) ||
   A->getOption().matches(options::OPT_Ofast))
@@ -106,7 +106,7 @@
 OOpt = A->getValue();
 
   if (OOpt != "0") {
-const char *WasmOpt = Args.MakeArgString(*WasmOptPath);
+const char *WasmOpt = Args.MakeArgString(WasmOptPath);
 ArgStringList CmdArgs;
 CmdArgs.push_back(Output.getFilename());
 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));


Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -92,10 +92,10 @@
 
   C.addCommand(std::make_unique(JA, *this, Linker, CmdArgs, Inputs));
 
-  // When optimizing, if wasm-opt is in the PATH, run wasm-opt.
+  // When optimizing, if wasm-opt is available, run it.
   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
-if (llvm::ErrorOr WasmOptPath =
-   llvm::sys::findProgramByName("wasm-opt")) {
+auto WasmOptPath = getToolChain().GetProgramPath("wasm-opt");
+if (WasmOptPath != "wasm-opt") {
   StringRef OOpt = "s";
   if (A->getOption().matches(options::OPT_O4) ||
   A->getOption().matches(options::OPT_Ofast))
@@ -106,7 +106,7 @@
 OOpt = A->getValue();
 
   if (OOpt != "0") {
-const char *WasmOpt = Args.MakeArgString(*WasmOptPath);
+const char *WasmOpt = Args.MakeArgString(WasmOptPath);
 ArgStringList CmdArgs;
 CmdArgs.push_back(Output.getFilename());
 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-O") + OOpt));
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits