Index: include/clang/Basic/BuiltinsWebAssembly.def =================================================================== --- include/clang/Basic/BuiltinsWebAssembly.def +++ include/clang/Basic/BuiltinsWebAssembly.def @@ -16,6 +16,10 @@ // The format of this database matches clang/Basic/Builtins.def. +#if defined(BUILTIN) && !defined(TARGET_BUILTIN) +# define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) BUILTIN(ID, TYPE, ATTRS) +#endif + // Query the current memory size, and increase the current memory size. // Note that memory.size is not "c" (readnone) because it must be sequenced // with respect to memory.grow calls. @@ -30,9 +34,15 @@ BUILTIN(__builtin_wasm_current_memory, "z", "n") BUILTIN(__builtin_wasm_grow_memory, "zz", "n") +// Floating point min/max +BUILTIN(__builtin_wasm_min_f32, "fff", "nc") +BUILTIN(__builtin_wasm_max_f32, "fff", "nc") +BUILTIN(__builtin_wasm_min_f64, "ddd", "nc") +BUILTIN(__builtin_wasm_max_f64, "ddd", "nc") + // Exception handling builtins. -BUILTIN(__builtin_wasm_throw, "vUiv*", "r") -BUILTIN(__builtin_wasm_rethrow, "v", "r") +TARGET_BUILTIN(__builtin_wasm_throw, "vUiv*", "r", "exception-handling") +TARGET_BUILTIN(__builtin_wasm_rethrow, "v", "r", "exception-handling") // Atomic wait and notify. BUILTIN(__builtin_wasm_atomic_wait_i32, "ii*iLLi", "n") @@ -40,73 +50,68 @@ BUILTIN(__builtin_wasm_atomic_notify, "Uii*Ui", "n") // Saturating fp-to-int conversions -BUILTIN(__builtin_wasm_trunc_saturate_s_i32_f32, "if", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i32_f32, "if", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_s_i32_f64, "id", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i32_f64, "id", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_s_i64_f32, "LLif", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f32, "LLif", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_s_i64_f64, "LLid", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f64, "LLid", "nc") - -// Floating point min/max -BUILTIN(__builtin_wasm_min_f32, "fff", "nc") -BUILTIN(__builtin_wasm_max_f32, "fff", "nc") -BUILTIN(__builtin_wasm_min_f64, "ddd", "nc") -BUILTIN(__builtin_wasm_max_f64, "ddd", "nc") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i32_f32, "if", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i32_f32, "if", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i32_f64, "id", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i32_f64, "id", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i64_f32, "LLif", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f32, "LLif", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i64_f64, "LLid", "nc", "nontrapping-fptoint") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i64_f64, "LLid", "nc", "nontrapping-fptoint") // SIMD builtins -BUILTIN(__builtin_wasm_extract_lane_s_i8x16, "iV16cIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_u_i8x16, "iV16cIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_s_i16x8, "iV8sIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_u_i16x8, "iV8sIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_i32x4, "iV4iIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_i64x2, "LLiV2LLiIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_f32x4, "fV4fIi", "nc") -BUILTIN(__builtin_wasm_extract_lane_f64x2, "dV2dIi", "nc") - -BUILTIN(__builtin_wasm_replace_lane_i8x16, "V16cV16cIii", "nc") -BUILTIN(__builtin_wasm_replace_lane_i16x8, "V8sV8sIii", "nc") -BUILTIN(__builtin_wasm_replace_lane_i32x4, "V4iV4iIii", "nc") -BUILTIN(__builtin_wasm_replace_lane_i64x2, "V2LLiV2LLiIiLLi", "nc") -BUILTIN(__builtin_wasm_replace_lane_f32x4, "V4fV4fIif", "nc") -BUILTIN(__builtin_wasm_replace_lane_f64x2, "V2dV2dIid", "nc") - -BUILTIN(__builtin_wasm_add_saturate_s_i8x16, "V16cV16cV16c", "nc") -BUILTIN(__builtin_wasm_add_saturate_u_i8x16, "V16cV16cV16c", "nc") -BUILTIN(__builtin_wasm_add_saturate_s_i16x8, "V8sV8sV8s", "nc") -BUILTIN(__builtin_wasm_add_saturate_u_i16x8, "V8sV8sV8s", "nc") - -BUILTIN(__builtin_wasm_sub_saturate_s_i8x16, "V16cV16cV16c", "nc") -BUILTIN(__builtin_wasm_sub_saturate_u_i8x16, "V16cV16cV16c", "nc") -BUILTIN(__builtin_wasm_sub_saturate_s_i16x8, "V8sV8sV8s", "nc") -BUILTIN(__builtin_wasm_sub_saturate_u_i16x8, "V8sV8sV8s", "nc") - -BUILTIN(__builtin_wasm_bitselect, "V4iV4iV4iV4i", "nc") - -BUILTIN(__builtin_wasm_any_true_i8x16, "iV16c", "nc") -BUILTIN(__builtin_wasm_any_true_i16x8, "iV8s", "nc") -BUILTIN(__builtin_wasm_any_true_i32x4, "iV4i", "nc") -BUILTIN(__builtin_wasm_any_true_i64x2, "iV2LLi", "nc") -BUILTIN(__builtin_wasm_all_true_i8x16, "iV16c", "nc") -BUILTIN(__builtin_wasm_all_true_i16x8, "iV8s", "nc") -BUILTIN(__builtin_wasm_all_true_i32x4, "iV4i", "nc") -BUILTIN(__builtin_wasm_all_true_i64x2, "iV2LLi", "nc") - -BUILTIN(__builtin_wasm_abs_f32x4, "V4fV4f", "nc") -BUILTIN(__builtin_wasm_abs_f64x2, "V2dV2d", "nc") - -BUILTIN(__builtin_wasm_min_f32x4, "V4fV4fV4f", "nc") -BUILTIN(__builtin_wasm_max_f32x4, "V4fV4fV4f", "nc") -BUILTIN(__builtin_wasm_min_f64x2, "V2dV2dV2d", "nc") -BUILTIN(__builtin_wasm_max_f64x2, "V2dV2dV2d", "nc") - -BUILTIN(__builtin_wasm_sqrt_f32x4, "V4fV4f", "nc") -BUILTIN(__builtin_wasm_sqrt_f64x2, "V2dV2d", "nc") - -BUILTIN(__builtin_wasm_trunc_saturate_s_i32x4_f32x4, "V4iV4f", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i32x4_f32x4, "V4iV4f", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_s_i64x2_f64x2, "V2LLiV2d", "nc") -BUILTIN(__builtin_wasm_trunc_saturate_u_i64x2_f64x2, "V2LLiV2d", "nc") +TARGET_BUILTIN(__builtin_wasm_extract_lane_s_i8x16, "iV16cIi", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_u_i8x16, "iV16cIi", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_s_i16x8, "iV8sIi", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_u_i16x8, "iV8sIi", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_i32x4, "iV4iIi", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_i64x2, "LLiV2LLiIi", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_f32x4, "fV4fIi", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_extract_lane_f64x2, "dV2dIi", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_replace_lane_i8x16, "V16cV16cIii", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_replace_lane_i16x8, "V8sV8sIii", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_replace_lane_i32x4, "V4iV4iIii", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_replace_lane_i64x2, "V2LLiV2LLiIiLLi", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_replace_lane_f32x4, "V4fV4fIif", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_replace_lane_f64x2, "V2dV2dIid", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_add_saturate_s_i8x16, "V16cV16cV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_add_saturate_u_i8x16, "V16cV16cV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_add_saturate_s_i16x8, "V8sV8sV8s", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_add_saturate_u_i16x8, "V8sV8sV8s", "nc", "simd128") + +TARGET_BUILTIN(__builtin_wasm_sub_saturate_s_i8x16, "V16cV16cV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_sub_saturate_u_i8x16, "V16cV16cV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_sub_saturate_s_i16x8, "V8sV8sV8s", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_sub_saturate_u_i16x8, "V8sV8sV8s", "nc", "simd128") + +TARGET_BUILTIN(__builtin_wasm_bitselect, "V4iV4iV4iV4i", "nc", "simd128") + +TARGET_BUILTIN(__builtin_wasm_any_true_i8x16, "iV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_any_true_i16x8, "iV8s", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_any_true_i32x4, "iV4i", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_any_true_i64x2, "iV2LLi", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_all_true_i8x16, "iV16c", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_all_true_i16x8, "iV8s", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_all_true_i32x4, "iV4i", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_all_true_i64x2, "iV2LLi", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_abs_f32x4, "V4fV4f", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_abs_f64x2, "V2dV2d", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_min_f32x4, "V4fV4fV4f", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_max_f32x4, "V4fV4fV4f", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_min_f64x2, "V2dV2dV2d", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_max_f64x2, "V2dV2dV2d", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_sqrt_f32x4, "V4fV4f", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_sqrt_f64x2, "V2dV2d", "nc", "unimplemented-simd128") + +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i32x4_f32x4, "V4iV4f", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i32x4_f32x4, "V4iV4f", "nc", "simd128") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_s_i64x2_f64x2, "V2LLiV2d", "nc", "unimplemented-simd128") +TARGET_BUILTIN(__builtin_wasm_trunc_saturate_u_i64x2_f64x2, "V2LLiV2d", "nc", "unimplemented-simd128") #undef BUILTIN +#undef TARGET_BUILTIN Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -2141,6 +2141,8 @@ HelpText<"Enforce targets of indirect branches and function returns">; def msimd128 : Flag<["-"], "msimd128">, Group; +def munimplemented_simd128 : Flag<["-"], "munimplemented-simd128">, Group; +def mno_unimplemented_simd128 : Flag<["-"], "mno-unimplemented-simd128">, Group; def mno_simd128 : Flag<["-"], "mno-simd128">, Group; def mnontrapping_fptoint : Flag<["-"], "mnontrapping-fptoint">, Group; def mno_nontrapping_fptoint : Flag<["-"], "mno-nontrapping-fptoint">, Group; Index: lib/Basic/Targets/WebAssembly.h =================================================================== --- lib/Basic/Targets/WebAssembly.h +++ lib/Basic/Targets/WebAssembly.h @@ -28,7 +28,8 @@ enum SIMDEnum { NoSIMD, SIMD128, - } SIMDLevel; + UnimplementedSIMD128, + } SIMDLevel = NoSIMD; bool HasNontrappingFPToInt; bool HasSignExt; @@ -59,18 +60,12 @@ MacroBuilder &Builder) const override; private: + static void setSIMDLevel(llvm::StringMap &Features, SIMDEnum Level); + bool initFeatureMap(llvm::StringMap &Features, DiagnosticsEngine &Diags, StringRef CPU, - const std::vector &FeaturesVec) const override { - if (CPU == "bleeding-edge") { - Features["simd128"] = true; - Features["nontrapping-fptoint"] = true; - Features["sign-ext"] = true; - } - return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec); - } - + const std::vector &FeaturesVec) const override; bool hasFeature(StringRef Feature) const final; bool handleTargetFeatures(std::vector &Features, Index: lib/Basic/Targets/WebAssembly.cpp =================================================================== --- lib/Basic/Targets/WebAssembly.cpp +++ lib/Basic/Targets/WebAssembly.cpp @@ -24,6 +24,8 @@ const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = { #define BUILTIN(ID, TYPE, ATTRS) \ {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr}, +#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \ + {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE}, #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \ {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr}, #include "clang/Basic/BuiltinsWebAssembly.def" @@ -35,6 +37,7 @@ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const { return llvm::StringSwitch(Feature) .Case("simd128", SIMDLevel >= SIMD128) + .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128) .Case("nontrapping-fptoint", HasNontrappingFPToInt) .Case("sign-ext", HasSignExt) .Case("exception-handling", HasExceptionHandling) @@ -55,6 +58,44 @@ defineCPUMacros(Builder, "wasm", /*Tuning=*/false); if (SIMDLevel >= SIMD128) Builder.defineMacro("__wasm_simd128__"); + if (SIMDLevel >= UnimplementedSIMD128) + Builder.defineMacro("__wasm_unimplemented_simd128__"); +} + +void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap &Features, + SIMDEnum Level) { + switch (Level) { + case UnimplementedSIMD128: + Features["unimplemented-simd128"] = true; + LLVM_FALLTHROUGH; + case SIMD128: + Features["simd128"] = true; + LLVM_FALLTHROUGH; + case NoSIMD: + break; + } +} + +bool WebAssemblyTargetInfo::initFeatureMap( + llvm::StringMap &Features, DiagnosticsEngine &Diags, StringRef CPU, + const std::vector &FeaturesVec) const { + if (CPU == "bleeding-edge") { + Features["nontrapping-fptoint"] = true; + Features["sign-ext"] = true; + setSIMDLevel(Features, SIMD128); + } + // Other targets do not consider user-configured features here, but while we + // are actively developing new features it is useful to let user-configured + // features control availability of builtins + setSIMDLevel(Features, SIMDLevel); + if (HasNontrappingFPToInt) + Features["nontrapping-fptoint"] = true; + if (HasSignExt) + Features["sign-ext"] = true; + if (HasExceptionHandling) + Features["exception-handling"] = true; + + return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec); } bool WebAssemblyTargetInfo::handleTargetFeatures( @@ -68,6 +109,14 @@ SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1)); continue; } + if (Feature == "+unimplemented-simd128") { + SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128)); + continue; + } + if (Feature == "-unimplemented-simd128") { + SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1)); + continue; + } if (Feature == "+nontrapping-fptoint") { HasNontrappingFPToInt = true; continue; Index: test/CodeGen/builtins-wasm.c =================================================================== --- test/CodeGen/builtins-wasm.c +++ test/CodeGen/builtins-wasm.c @@ -1,9 +1,6 @@ -// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fno-lax-vector-conversions \ -// RUN: -O3 -emit-llvm -o - %s \ -// RUN: | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY32 -// RUN: %clang_cc1 -triple wasm64-unknown-unknown -fno-lax-vector-conversions \ -// RUN: -O3 -emit-llvm -o - %s \ -// RUN: | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY64 +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +unimplemented-simd128 -target-feature +nontrapping-fptoint -target-feature +exception-handling -fno-lax-vector-conversions -O3 -emit-llvm -o - %s | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY32 +// RUN: %clang_cc1 -triple wasm64-unknown-unknown -target-feature +unimplemented-simd128 -target-feature +nontrapping-fptoint -target-feature +exception-handling -fno-lax-vector-conversions -O3 -emit-llvm -o - %s | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY64 +// RUN: not %clang_cc1 -triple wasm64-unknown-unknown -target-feature +nontrapping-fptoint -target-feature +exception-handling -fno-lax-vector-conversions -O3 -emit-llvm -o - %s 2>&1 | FileCheck %s -check-prefixes MISSING-SIMD // SIMD convenience types typedef char i8x16 __attribute((vector_size(16))); @@ -158,6 +155,7 @@ int extract_lane_s_i8x16(i8x16 v) { return __builtin_wasm_extract_lane_s_i8x16(v, 13); + // MISSING-SIMD: error: '__builtin_wasm_extract_lane_s_i8x16' needs target feature simd128 // WEBASSEMBLY: extractelement <16 x i8> %v, i32 13 // WEBASSEMBLY-NEXT: sext // WEBASSEMBLY-NEXT: ret Index: test/Preprocessor/wasm-target-features.c =================================================================== --- test/Preprocessor/wasm-target-features.c +++ test/Preprocessor/wasm-target-features.c @@ -8,6 +8,15 @@ // 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 +// RUN: %clang -E -dM %s -o - 2>&1 \ +// RUN: -target wasm64-unknown-unknown -munimplemented-simd128 \ +// 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 -mcpu=mvp \ // RUN: | FileCheck %s -check-prefix=MVP // RUN: %clang -E -dM %s -o - 2>&1 \