tlively created this revision.
tlively added reviewers: aheejin, sbc100, dschuff.
Herald added subscribers: llvm-commits, cfe-commits, jdoerfert, jfb, rupprecht, 
dexonsmith, steven_wu, sunfish, hiraditya, jgravelle-google, mehdi_amini.
Herald added projects: clang, LLVM.

In the clang UI, replaces -mthread-model posix with -matomics as the
source of truth on threading. In the backend, replaces
-thread-model=posix with the atomics target feature, which is now
collected on the WebAssemblyTargetMachine along with all other used
features. These collected features will also be used to emit the
target features section in the future.

The default configuration for the backend is thread-model=posix and no
atomics, which was previously an invalid configuration. This change
makes the default valid because the thread model is ignored.

A side effect of this change is that objects are never emitted with
passive segments. It will instead be up to the linker to decide
whether sections should be active or passive based on whether atomics
are used in the final link.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D58742

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Driver/ToolChain.h
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/WebAssembly.cpp
  clang/lib/Driver/ToolChains/WebAssembly.h
  clang/test/Driver/wasm-toolchain.c
  clang/test/Preprocessor/wasm-target-features.c
  lld/test/wasm/init-fini.ll
  lld/test/wasm/lto/atomics.ll
  lld/wasm/LTO.cpp
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
  llvm/test/CodeGen/WebAssembly/atomic-mem-consistency.ll
  llvm/test/CodeGen/WebAssembly/atomic-rmw.ll
  llvm/test/CodeGen/WebAssembly/global.ll
  llvm/test/CodeGen/WebAssembly/tls.ll
  llvm/test/CodeGen/WebAssembly/vtable.ll
  llvm/test/MC/WebAssembly/bss.ll
  llvm/test/MC/WebAssembly/comdat.ll
  llvm/test/MC/WebAssembly/debug-info.ll
  llvm/test/MC/WebAssembly/explicit-sections.ll
  llvm/test/MC/WebAssembly/external-data.ll
  llvm/test/MC/WebAssembly/external-func-address.ll
  llvm/test/MC/WebAssembly/global-ctor-dtor.ll
  llvm/test/MC/WebAssembly/init-flags.ll
  llvm/test/MC/WebAssembly/reloc-data.ll
  llvm/test/MC/WebAssembly/unnamed-data.ll
  llvm/test/MC/WebAssembly/weak-alias.ll
  llvm/test/tools/llvm-nm/wasm/local-symbols.ll
  llvm/test/tools/llvm-objdump/WebAssembly/relocations.test

Index: llvm/test/tools/llvm-objdump/WebAssembly/relocations.test
===================================================================
--- llvm/test/tools/llvm-objdump/WebAssembly/relocations.test
+++ llvm/test/tools/llvm-objdump/WebAssembly/relocations.test
@@ -1,4 +1,4 @@
-; RUN: llc -thread-model=single -mtriple=wasm32-unknown-unknown -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s
+; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %s -o - | llvm-objdump -r - | FileCheck %s
 
 @foo = external global i32, align 4
 @bar = global i32* @foo, align 4
Index: llvm/test/tools/llvm-nm/wasm/local-symbols.ll
===================================================================
--- llvm/test/tools/llvm-nm/wasm/local-symbols.ll
+++ llvm/test/tools/llvm-nm/wasm/local-symbols.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single -mtriple=wasm32-unknown-unknown -o %t.o %s
+; RUN: llc -filetype=obj -mtriple=wasm32-unknown-unknown -o %t.o %s
 ; RUN: llvm-nm %t.o | FileCheck %s
 
 @foo = internal global i32 1, align 4
Index: llvm/test/MC/WebAssembly/weak-alias.ll
===================================================================
--- llvm/test/MC/WebAssembly/weak-alias.ll
+++ llvm/test/MC/WebAssembly/weak-alias.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single -wasm-keep-registers %s -o %t.o
+; RUN: llc -filetype=obj -wasm-keep-registers %s -o %t.o
 ; RUN: obj2yaml %t.o | FileCheck %s
 ; RUN: llvm-objdump -t %t.o | FileCheck --check-prefix=CHECK-SYMS %s
 
Index: llvm/test/MC/WebAssembly/unnamed-data.ll
===================================================================
--- llvm/test/MC/WebAssembly/unnamed-data.ll
+++ llvm/test/MC/WebAssembly/unnamed-data.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/reloc-data.ll
===================================================================
--- llvm/test/MC/WebAssembly/reloc-data.ll
+++ llvm/test/MC/WebAssembly/reloc-data.ll
@@ -1,4 +1,4 @@
-; RUN: llc -O0 -filetype=obj -thread-model=single %s -o - | llvm-readobj -r -expand-relocs | FileCheck %s
+; RUN: llc -O0 -filetype=obj %s -o - | llvm-readobj -r -expand-relocs | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/init-flags.ll
===================================================================
--- llvm/test/MC/WebAssembly/init-flags.ll
+++ /dev/null
@@ -1,25 +0,0 @@
-; RUN: llc -filetype=obj %s -thread-model=single -o - | obj2yaml | FileCheck %s --check-prefix=SINGLE
-; RUN: llc -filetype=obj %s -thread-model=posix -o - | obj2yaml | FileCheck %s --check-prefix=THREADS
-
-; Test that setting thread-model=posix causes data segments to be
-; emitted as passive segments (i.e. have InitFlags set to 1).
-
-target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
-target triple = "wasm32-unknown-unknown"
-
-@str = private unnamed_addr constant [7 x i8] c"Hello!\00", align 1
-
-; SINGLE:       - Type:            DATA
-; SINGLE-NEXT:    Segments:
-; SINGLE-NEXT:      - SectionOffset:   6
-; SINGLE-NEXT:        InitFlags:       0
-; SINGLE-NEXT:        Offset:
-; SINGLE-NEXT:          Opcode:          I32_CONST
-; SINGLE-NEXT:          Value:           0
-; SINGLE-NEXT:        Content:         48656C6C6F2100
-
-; THREADS:       - Type:            DATA
-; THREADS-NEXT:    Segments:
-; THREADS-NEXT:      - SectionOffset:   3
-; THREADS-NEXT:        InitFlags:       1
-; THREADS-NEXT:        Content:         48656C6C6F2100
Index: llvm/test/MC/WebAssembly/global-ctor-dtor.ll
===================================================================
--- llvm/test/MC/WebAssembly/global-ctor-dtor.ll
+++ llvm/test/MC/WebAssembly/global-ctor-dtor.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/external-func-address.ll
===================================================================
--- llvm/test/MC/WebAssembly/external-func-address.ll
+++ llvm/test/MC/WebAssembly/external-func-address.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/external-data.ll
===================================================================
--- llvm/test/MC/WebAssembly/external-data.ll
+++ llvm/test/MC/WebAssembly/external-data.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/explicit-sections.ll
===================================================================
--- llvm/test/MC/WebAssembly/explicit-sections.ll
+++ llvm/test/MC/WebAssembly/explicit-sections.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/debug-info.ll
===================================================================
--- llvm/test/MC/WebAssembly/debug-info.ll
+++ llvm/test/MC/WebAssembly/debug-info.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | llvm-readobj -r -s -symbols | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | llvm-readobj -r -s -symbols | FileCheck %s
 
 ; CHECK: Format: WASM
 ; CHECK-NEXT:Arch: wasm32
Index: llvm/test/MC/WebAssembly/comdat.ll
===================================================================
--- llvm/test/MC/WebAssembly/comdat.ll
+++ llvm/test/MC/WebAssembly/comdat.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/MC/WebAssembly/bss.ll
===================================================================
--- llvm/test/MC/WebAssembly/bss.ll
+++ llvm/test/MC/WebAssembly/bss.ll
@@ -1,4 +1,4 @@
-; RUN: llc -filetype=obj -thread-model=single %s -o - | obj2yaml | FileCheck %s
+; RUN: llc -filetype=obj %s -o - | obj2yaml | FileCheck %s
 
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/CodeGen/WebAssembly/vtable.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/vtable.ll
+++ llvm/test/CodeGen/WebAssembly/vtable.ll
@@ -1,6 +1,6 @@
-; RUN: llc < %s -asm-verbose=false -thread-model=single -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=TYPEINFONAME
-; RUN: llc < %s -asm-verbose=false -thread-model=single -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=VTABLE
-; RUN: llc < %s -asm-verbose=false -thread-model=single -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=TYPEINFO
+; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=TYPEINFONAME
+; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=VTABLE
+; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefix=TYPEINFO
 
 ; Test that simple vtables assemble as expected.
 ;
Index: llvm/test/CodeGen/WebAssembly/tls.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/tls.ll
+++ llvm/test/CodeGen/WebAssembly/tls.ll
@@ -1,4 +1,4 @@
-; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -thread-model=single | FileCheck --check-prefix=SINGLE %s
+; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck --check-prefix=SINGLE %s
 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
 target triple = "wasm32-unknown-unknown"
 
Index: llvm/test/CodeGen/WebAssembly/global.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/global.ll
+++ llvm/test/CodeGen/WebAssembly/global.ll
@@ -1,5 +1,5 @@
-; RUN: llc < %s -thread-model=single -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefixes=CHECK,SINGLE
-; RUN: llc < %s -thread-model=posix -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s --check-prefixes=CHECK,THREADS
+; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=-atomics | FileCheck %s
+; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+atomics | FileCheck %s
 
 ; Test that globals assemble as expected.
 
@@ -192,8 +192,7 @@
 
 ; Constant global.
 ; CHECK: .type    rom,@object{{$}}
-; SINGLE: .section .rodata.rom,""
-; THREADS: .section .rodata.rom,"passive"
+; CHECK: .section .rodata.rom,""
 ; CHECK: .globl   rom{{$}}
 ; CHECK: .p2align   4{{$}}
 ; CHECK: rom:
@@ -206,8 +205,7 @@
 ; CHECK-NEXT: .skip       8
 ; CHECK-NEXT: .size       array, 8
 ; CHECK: .type       pointer_to_array,@object
-; SINGLE-NEXT: .section    .rodata.pointer_to_array,""
-; THREADS-NEXT: .section    .rodata.pointer_to_array,"passive"
+; CHECK-NEXT: .section    .rodata.pointer_to_array,""
 ; CHECK-NEXT: .globl      pointer_to_array
 ; CHECK-NEXT: .p2align      2
 ; CHECK-NEXT: pointer_to_array:
Index: llvm/test/CodeGen/WebAssembly/atomic-rmw.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/atomic-rmw.ll
+++ llvm/test/CodeGen/WebAssembly/atomic-rmw.ll
@@ -1,4 +1,4 @@
-; RUN: not llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers
+; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers
 ; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+atomics,+sign-ext | FileCheck %s
 
 ; Test atomic RMW (read-modify-write) instructions are assembled properly.
Index: llvm/test/CodeGen/WebAssembly/atomic-mem-consistency.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/atomic-mem-consistency.ll
+++ llvm/test/CodeGen/WebAssembly/atomic-mem-consistency.ll
@@ -1,4 +1,4 @@
-; RUN: not llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt
+; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt
 ; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+atomics,+sign-ext | FileCheck %s
 
 ; Currently all wasm atomic memory access instructions are sequentially
Index: llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
+++ llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
@@ -23,6 +23,7 @@
 class WebAssemblyTargetMachine final : public LLVMTargetMachine {
   std::unique_ptr<TargetLoweringObjectFile> TLOF;
   mutable StringMap<std::unique_ptr<WebAssemblySubtarget>> SubtargetMap;
+  mutable FeatureBitset UsedFeatures;
 
 public:
   WebAssemblyTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
@@ -32,6 +33,9 @@
                            bool JIT);
 
   ~WebAssemblyTargetMachine() override;
+
+  const WebAssemblySubtarget *getSubtargetImpl(std::string CPU,
+                                               std::string FS) const;
   const WebAssemblySubtarget *
   getSubtargetImpl(const Function &F) const override;
 
@@ -42,6 +46,8 @@
     return TLOF.get();
   }
 
+  FeatureBitset getUsedFeatures() const { return UsedFeatures; }
+
   TargetTransformInfo getTargetTransformInfo(const Function &F) override;
 
   bool usesPhysRegsForPEI() const override { return false; }
Index: llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -115,6 +115,10 @@
 
   initAsmInfo();
 
+  // Create a subtarget using the unmodified target machine features to
+  // initialize the used feature set with explicitly enabled features.
+  getSubtargetImpl(getTargetCPU(), getTargetFeatureString());
+
   // Note that we don't use setRequiresStructuredCFG(true). It disables
   // optimizations than we're ok with, and want, such as critical edge
   // splitting and tail merging.
@@ -122,6 +126,17 @@
 
 WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
 
+const WebAssemblySubtarget *
+WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
+                                           std::string FS) const {
+  auto &I = SubtargetMap[CPU + FS];
+  if (!I) {
+    I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
+    UsedFeatures |= I->getFeatureBits();
+  }
+  return I.get();
+}
+
 const WebAssemblySubtarget *
 WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
   Attribute CPUAttr = F.getFnAttribute("target-cpu");
@@ -134,15 +149,12 @@
                        ? FSAttr.getValueAsString().str()
                        : TargetFS;
 
-  auto &I = SubtargetMap[CPU + FS];
-  if (!I) {
-    // This needs to be done before we create a new subtarget since any
-    // creation will depend on the TM and the code generation flags on the
-    // function that reside in TargetOptions.
-    resetTargetOptions(F);
-    I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
-  }
-  return I.get();
+  // This needs to be done before we create a new subtarget since any
+  // creation will depend on the TM and the code generation flags on the
+  // function that reside in TargetOptions.
+  resetTargetOptions(F);
+
+  return getSubtargetImpl(CPU, FS);
 }
 
 namespace {
@@ -202,14 +214,15 @@
 //===----------------------------------------------------------------------===//
 
 void WebAssemblyPassConfig::addIRPasses() {
-  if (TM->Options.ThreadModel == ThreadModel::Single) {
-    // In "single" mode, atomics get lowered to non-atomics.
-    addPass(createLowerAtomicPass());
-    addPass(new StripThreadLocal());
-  } else {
+  if (static_cast<WebAssemblyTargetMachine *>(TM)
+          ->getUsedFeatures()[WebAssembly::FeatureAtomics]) {
     // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
     // control specifically what gets lowered.
     addPass(createAtomicExpandPass());
+  } else {
+    // If atomics are not enabled, they get lowered to non-atomics.
+    addPass(createLowerAtomicPass());
+    addPass(new StripThreadLocal());
   }
 
   // Add signatures to prototype-less function declarations
Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
===================================================================
--- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -1697,9 +1697,6 @@
       getContext().getWasmSection(Name, Kind, Group,
                                   MCContext::GenericSectionID);
 
-  if (TM.Options.ThreadModel != ThreadModel::Single)
-    Section->setPassive();
-
   return Section;
 }
 
@@ -1730,11 +1727,7 @@
     (*NextUniqueID)++;
   }
 
-  MCSectionWasm* Section = Ctx.getWasmSection(Name, Kind, Group, UniqueID);
-  if (Section->isWasmData() && TM.Options.ThreadModel != ThreadModel::Single)
-    Section->setPassive();
-
-  return Section;
+  return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
 }
 
 MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
Index: lld/wasm/LTO.cpp
===================================================================
--- lld/wasm/LTO.cpp
+++ lld/wasm/LTO.cpp
@@ -47,9 +47,6 @@
   C.Options.FunctionSections = true;
   C.Options.DataSections = true;
 
-  // Wasm currently only supports ThreadModel::Single
-  C.Options.ThreadModel = ThreadModel::Single;
-
   C.DisableVerify = Config->DisableVerify;
   C.DiagHandler = diagnosticHandler;
   C.OptLevel = Config->LTOO;
Index: lld/test/wasm/lto/atomics.ll
===================================================================
--- lld/test/wasm/lto/atomics.ll
+++ lld/test/wasm/lto/atomics.ll
@@ -1,7 +1,8 @@
 ; RUN: llvm-as %s -o %t.o
 ; RUN: wasm-ld %t.o -o %t.wasm -lto-O0
-; Atomic operations with fail to compile if the ThreadModel is not
-; correctly set to Single (i.e. if atomics are not lowered to regular ops).
+
+; Atomic operations will not fail to compile if atomics are not
+; enabled because LLVM atomics will be lowered to regular ops.
 
 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
 target triple = "wasm32-unknown-unknown-wasm"
Index: lld/test/wasm/init-fini.ll
===================================================================
--- lld/test/wasm/init-fini.ll
+++ lld/test/wasm/init-fini.ll
@@ -1,5 +1,5 @@
-; RUN: llc -filetype=obj -thread-model=single -o %t.o %s
-; RUN: llc -filetype=obj -thread-model=single %S/Inputs/global-ctor-dtor.ll -o %t.global-ctor-dtor.o
+; RUN: llc -filetype=obj -o %t.o %s
+; RUN: llc -filetype=obj %S/Inputs/global-ctor-dtor.ll -o %t.global-ctor-dtor.o
 
 target triple = "wasm32-unknown-unknown"
 
Index: clang/test/Preprocessor/wasm-target-features.c
===================================================================
--- clang/test/Preprocessor/wasm-target-features.c
+++ clang/test/Preprocessor/wasm-target-features.c
@@ -6,7 +6,7 @@
 // RUN:   | FileCheck %s -check-prefix=SIMD128
 //
 // SIMD128:#define __wasm_simd128__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -munimplemented-simd128 \
 // RUN:   | FileCheck %s -check-prefix=SIMD128-UNIMPLEMENTED
@@ -15,7 +15,7 @@
 // RUN:   | FileCheck %s -check-prefix=SIMD128-UNIMPLEMENTED
 //
 // SIMD128-UNIMPLEMENTED:#define __wasm_unimplemented_simd128__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mnontrapping-fptoint \
 // RUN:   | FileCheck %s -check-prefix=NONTRAPPING-FPTOINT
@@ -24,7 +24,7 @@
 // RUN:   | FileCheck %s -check-prefix=NONTRAPPING-FPTOINT
 //
 // NONTRAPPING-FPTOINT:#define __wasm_nontrapping_fptoint__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -msign-ext \
 // RUN:   | FileCheck %s -check-prefix=SIGN-EXT
@@ -33,7 +33,7 @@
 // RUN:   | FileCheck %s -check-prefix=SIGN-EXT
 //
 // SIGN-EXT:#define __wasm_sign_ext__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mexception-handling \
 // RUN:   | FileCheck %s -check-prefix=EXCEPTION-HANDLING
@@ -42,7 +42,7 @@
 // RUN:   | FileCheck %s -check-prefix=EXCEPTION-HANDLING
 //
 // EXCEPTION-HANDLING:#define __wasm_exception_handling__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mbulk-memory \
 // RUN:   | FileCheck %s -check-prefix=BULK-MEMORY
@@ -51,18 +51,27 @@
 // RUN:   | FileCheck %s -check-prefix=BULK-MEMORY
 //
 // BULK-MEMORY:#define __wasm_bulk_memory__ 1{{$}}
-//
-// We don't use -matomics directly and '-mthread-model posix' sets the atomics
-// target feature.
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
-// RUN:     -target wasm32-unknown-unknown -mthread-model posix \
+// RUN:     -target wasm32-unknown-unknown -matomics \
 // RUN:   | FileCheck %s -check-prefix=ATOMICS
 // RUN: %clang -E -dM %s -o - 2>&1 \
-// RUN:     -target wasm64-unknown-unknown -mthread-model posix \
+// RUN:     -target wasm64-unknown-unknown -matomics \
 // RUN:   | FileCheck %s -check-prefix=ATOMICS
 //
-// ATOMICS:#define __wasm_atomics__ 1{{$}}
+// ATOMICS-DAG:#define __wasm_atomics__ 1{{$}}
+// ATOMICS-DAG:#define __wasm_bulk_memory__ 1{{$}}
+
+// RUN: %clang -E -dM %s -o - 2>&1 \
+// RUN:     -target wasm32-unknown-unknown -pthread \
+// RUN:   | FileCheck %s -check-prefix=PTHREAD
+// RUN: %clang -E -dM %s -o - 2>&1 \
+// RUN:     -target wasm64-unknown-unknown -pthread \
+// RUN:   | FileCheck %s -check-prefix=PTHREAD
 //
+// PTHREAD-DAG:#define __wasm_atomics__ 1{{$}}
+// PTHREAD-DAG:#define __wasm_bulk_memory__ 1{{$}}
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mcpu=mvp \
 // RUN:   | FileCheck %s -check-prefix=MVP
@@ -77,7 +86,7 @@
 // MVP-NOT:#define __wasm_exception_handling__
 // MVP-NOT:#define __wasm_bulk_memory__
 // MVP-NOT:#define __wasm_atomics__
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mcpu=bleeding-edge \
 // RUN:   | FileCheck %s -check-prefix=BLEEDING-EDGE
@@ -90,7 +99,7 @@
 // BLEEDING-EDGE-DAG:#define __wasm_simd128__ 1{{$}}
 // BLEEDING-EDGE-DAG:#define __wasm_atomics__ 1{{$}}
 // BLEEDING-EDGE-NOT:#define __wasm_unimplemented_simd128__ 1{{$}}
-//
+
 // RUN: %clang -E -dM %s -o - 2>&1 \
 // RUN:     -target wasm32-unknown-unknown -mcpu=bleeding-edge -mno-simd128 \
 // RUN:   | FileCheck %s -check-prefix=BLEEDING-EDGE-NO-SIMD128
Index: clang/test/Driver/wasm-toolchain.c
===================================================================
--- clang/test/Driver/wasm-toolchain.c
+++ clang/test/Driver/wasm-toolchain.c
@@ -40,14 +40,19 @@
 // COMPILE: clang{{.*}}" "-cc1" {{.*}} "-internal-isystem" "/foo/include/wasm32-wasi-musl" "-internal-isystem" "/foo/include"
 
 // Thread-related command line tests.
-// - '-mthread-model' sets '-target-feature +matomics'
-// - '-phread' sets both '-target-featuer +atomics' and '-mthread-model posix'
-
-// RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -mthread-model posix 2>&1 | FileCheck -check-prefix=POSIX %s
-// POSIX: clang{{.*}}" "-cc1" {{.*}} "-target-feature" "+atomics"
 
+// '-pthread' sets '-target-feature +atomics' and '-target-feature +bulk-memory'
 // RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -pthread 2>&1 | FileCheck -check-prefix=PTHREAD %s
-// PTHREAD: clang{{.*}}" "-cc1" {{.*}} "-mthread-model" "posix" {{.*}} "-target-feature" "+atomics"
+// PTHREAD: clang{{.*}}" "-cc1" {{.*}} "-target-feature" "+atomics" "-target-feature" "+bulk-memory"
+
+// '-pthread' not allowed with '-mno-atomics'
+// RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -pthread -mno-atomics 2>&1 | FileCheck -check-prefix=PTHREAD_NO_ATOMICS %s
+// PTHREAD_NO_ATOMICS: invalid argument '-pthread' not allowed with '-mno-atomics'
+
+// '-pthread' not allowed with '-mno-bulk-memory'
+// RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -pthread -mno-bulk-memory 2>&1 | FileCheck -check-prefix=PTHREAD_NO_BULKMEM %s
+// PTHREAD_NO_BULKMEM: invalid argument '-pthread' not allowed with '-mno-bulk-memory'
 
-// RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -pthread -mthread-model single 2>&1 | FileCheck -check-prefix=THREAD_OPT_ERROR %s
-// THREAD_OPT_ERROR: invalid argument '-pthread' not allowed with '-mthread-model single'
+// '-matomics' not allowed with '-mno-bulk-memory'
+// RUN: %clang -### -no-canonical-prefixes -target wasm32-unknown-unknown --sysroot=/foo %s -matomics -mno-bulk-memory 2>&1 | FileCheck -check-prefix=ATOMICS_NO_BULKMEM %s
+// ATOMICS_NO_BULKMEM: invalid argument '-matomics' not allowed with '-mno-bulk-memory'
Index: clang/lib/Driver/ToolChains/WebAssembly.h
===================================================================
--- clang/lib/Driver/ToolChains/WebAssembly.h
+++ clang/lib/Driver/ToolChains/WebAssembly.h
@@ -64,7 +64,6 @@
       llvm::opt::ArgStringList &CC1Args) const override;
   void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
                            llvm::opt::ArgStringList &CmdArgs) const override;
-  std::string getThreadModel(const llvm::opt::ArgList &) const override;
 
   const char *getDefaultLinker() const override { return "wasm-ld"; }
 
Index: clang/lib/Driver/ToolChains/WebAssembly.cpp
===================================================================
--- clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -20,27 +20,6 @@
 using namespace clang;
 using namespace llvm::opt;
 
-void parseThreadArgs(const Driver &Driver, const ArgList &DriverArgs,
-                     bool &Pthread, StringRef &ThreadModel,
-                     bool CheckForErrors = true) {
-  // Default value for -pthread / -mthread-model options, each being false /
-  // "single".
-  Pthread =
-      DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread, false);
-  ThreadModel =
-      DriverArgs.getLastArgValue(options::OPT_mthread_model, "single");
-  if (!CheckForErrors)
-    return;
-
-  // Did user explicitly specify -mthread-model / -pthread?
-  bool HasThreadModel = DriverArgs.hasArg(options::OPT_mthread_model);
-  bool HasPthread = Pthread && DriverArgs.hasArg(options::OPT_pthread);
-  // '-pthread' cannot be used with '-mthread-model single'
-  if (HasPthread && HasThreadModel && ThreadModel == "single")
-    Driver.Diag(diag::err_drv_argument_not_allowed_with)
-        << "-pthread" << "-mthread-model single";
-}
-
 wasm::Linker::Linker(const ToolChain &TC)
     : GnuTool("wasm::Linker", "lld", TC) {}
 
@@ -145,15 +124,36 @@
                          options::OPT_fno_use_init_array, true))
     CC1Args.push_back("-fuse-init-array");
 
-  // Either '-mthread-model posix' or '-pthread' sets '-target-feature
-  // +atomics'. We intentionally didn't create '-matomics' and set the atomics
-  // target feature here depending on the other two options.
-  bool Pthread = false;
-  StringRef ThreadModel = "";
-  parseThreadArgs(getDriver(), DriverArgs, Pthread, ThreadModel);
-  if (Pthread || ThreadModel != "single") {
+  // '-pthread' implies '-target-feature +atomics' and
+  // '-target-feature +bulk-memory'
+  if (DriverArgs.hasFlag(options::OPT_pthread, options::OPT_no_pthread,
+                         false)) {
+    if (DriverArgs.hasFlag(options::OPT_mno_atomics, options::OPT_matomics,
+                           false))
+      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
+          << "-pthread"
+          << "-mno-atomics";
+    if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
+                           options::OPT_mbulk_memory, false))
+      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
+          << "-pthread"
+          << "-mno-bulk-memory";
     CC1Args.push_back("-target-feature");
     CC1Args.push_back("+atomics");
+    CC1Args.push_back("-target-feature");
+    CC1Args.push_back("+bulk-memory");
+  }
+
+  // '-matomics' implies '-mbulk-memory'
+  if (DriverArgs.hasFlag(options::OPT_matomics, options::OPT_mno_atomics,
+                         false)) {
+    if (DriverArgs.hasFlag(options::OPT_mno_bulk_memory,
+                           options::OPT_mbulk_memory, false))
+      getDriver().Diag(diag::err_drv_argument_not_allowed_with)
+          << "-matomics"
+          << "-mno-bulk-memory";
+    CC1Args.push_back("-target-feature");
+    CC1Args.push_back("+bulk-memory");
   }
 }
 
@@ -212,17 +212,6 @@
   }
 }
 
-std::string WebAssembly::getThreadModel(const ArgList &DriverArgs) const {
-  // The WebAssembly MVP does not yet support threads. We set this to "posix"
-  // when '-pthread' is set.
-  bool Pthread = false;
-  StringRef ThreadModel = "";
-  parseThreadArgs(getDriver(), DriverArgs, Pthread, ThreadModel, false);
-  if (Pthread)
-    return "posix";
-  return ThreadModel;
-}
-
 Tool *WebAssembly::buildLinker() const {
   return new tools::wasm::Linker(*this);
 }
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3830,7 +3830,7 @@
     CmdArgs.push_back(A->getValue());
   }
   else
-    CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel(Args)));
+    CmdArgs.push_back(Args.MakeArgString(TC.getThreadModel()));
 
   Args.AddLastArg(CmdArgs, options::OPT_fveclib);
 
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1528,7 +1528,7 @@
     if (TC.isThreadModelSupported(A->getValue()))
       OS << "Thread model: " << A->getValue();
   } else
-    OS << "Thread model: " << TC.getThreadModel(C.getArgs());
+    OS << "Thread model: " << TC.getThreadModel();
   OS << '\n';
 
   // Print out the install directory.
Index: clang/include/clang/Driver/ToolChain.h
===================================================================
--- clang/include/clang/Driver/ToolChain.h
+++ clang/include/clang/Driver/ToolChain.h
@@ -453,9 +453,7 @@
   virtual bool SupportsEmbeddedBitcode() const { return false; }
 
   /// getThreadModel() - Which thread model does this target use?
-  virtual std::string getThreadModel(const llvm::opt::ArgList &) const {
-    return "posix";
-  }
+  virtual std::string getThreadModel() const { return "posix"; }
 
   /// isThreadModelSupported() - Does this target support a thread model?
   virtual bool isThreadModelSupported(const StringRef Model) const;
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2160,6 +2160,8 @@
 def mno_sign_ext : Flag<["-"], "mno-sign-ext">, Group<m_wasm_Features_Group>;
 def mexception_handing : Flag<["-"], "mexception-handling">, Group<m_wasm_Features_Group>;
 def mno_exception_handing : Flag<["-"], "mno-exception-handling">, Group<m_wasm_Features_Group>;
+def matomics : Flag<["-"], "matomics">, Group<m_wasm_Features_Group>;
+def mno_atomics : Flag<["-"], "mno-atomics">, Group<m_wasm_Features_Group>;
 def mbulk_memory : Flag<["-"], "mbulk-memory">, Group<m_wasm_Features_Group>;
 def mno_bulk_memory : Flag<["-"], "mno-bulk-memory">, Group<m_wasm_Features_Group>;
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to