pengfei created this revision. pengfei added reviewers: skan, RKSimon, craig.topper, FreddyYe, LuoYuanke. pengfei requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
`__bfloat16` is defined as X86 specific type that represents the brain floating-point format. It is only usable with X86 intrinsics. Arithmatic operations with this type need to be forbidden. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D120395 Files: clang/lib/Headers/avx512bf16intrin.h clang/lib/Headers/avx512vlbf16intrin.h clang/test/CodeGen/X86/avx512bf16-error.c Index: clang/test/CodeGen/X86/avx512bf16-error.c =================================================================== --- /dev/null +++ clang/test/CodeGen/X86/avx512bf16-error.c @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -ffreestanding -triple x86_64-linux-pc %s + +// expected-error@+1 3 {{unknown type name '__bfloat16'}} +__bfloat16 foo(__bfloat16 a, __bfloat16 b) { + return a + b; +} + +#include <immintrin.h> + +// expected-error@+2 {{invalid operands to binary expression ('__bfloat16' (aka 'struct __bfloat16_s') and '__bfloat16')}} +__bfloat16 bar(__bfloat16 a, __bfloat16 b) { + return a + b; +} Index: clang/lib/Headers/avx512vlbf16intrin.h =================================================================== --- clang/lib/Headers/avx512vlbf16intrin.h +++ clang/lib/Headers/avx512vlbf16intrin.h @@ -415,9 +415,10 @@ /// and fraction field is truncated to 7 bits. static __inline__ __bfloat16 __DEFAULT_FN_ATTRS128 _mm_cvtness_sbh(float __A) { __v4sf __V = {__A, 0, 0, 0}; - __v8hi __R = __builtin_ia32_cvtneps2bf16_128_mask( + __v8hi __R1 = __builtin_ia32_cvtneps2bf16_128_mask( (__v4sf)__V, (__v8hi)_mm_undefined_si128(), (__mmask8)-1); - return __R[0]; + __bfloat16 __R2 = {__R1[0]}; + return __R2; } /// Convert Packed BF16 Data to Packed float Data. Index: clang/lib/Headers/avx512bf16intrin.h =================================================================== --- clang/lib/Headers/avx512bf16intrin.h +++ clang/lib/Headers/avx512bf16intrin.h @@ -15,7 +15,14 @@ typedef short __m512bh __attribute__((__vector_size__(64), __aligned__(64))); typedef short __m256bh __attribute__((__vector_size__(32), __aligned__(32))); -typedef unsigned short __bfloat16; + +/// \typedef __bfloat16 +/// A target specific type to represent the storage only brain floating-point +/// format type. Define through structure to explicitly prohibit any +/// arithmatic operations. +typedef struct __bfloat16_s { + short _Value; +} __bfloat16; #define __DEFAULT_FN_ATTRS512 \ __attribute__((__always_inline__, __nodebug__, __target__("avx512bf16"), \ @@ -34,7 +41,7 @@ /// \returns A float data whose sign field and exponent field keep unchanged, /// and fraction field is extended to 23 bits. static __inline__ float __DEFAULT_FN_ATTRS _mm_cvtsbh_ss(__bfloat16 __A) { - return __builtin_ia32_cvtsbf162ss_32(__A); + return __builtin_ia32_cvtsbf162ss_32(__A._Value); } /// Convert Two Packed Single Data to One Packed BF16 Data.
Index: clang/test/CodeGen/X86/avx512bf16-error.c =================================================================== --- /dev/null +++ clang/test/CodeGen/X86/avx512bf16-error.c @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -ffreestanding -triple x86_64-linux-pc %s + +// expected-error@+1 3 {{unknown type name '__bfloat16'}} +__bfloat16 foo(__bfloat16 a, __bfloat16 b) { + return a + b; +} + +#include <immintrin.h> + +// expected-error@+2 {{invalid operands to binary expression ('__bfloat16' (aka 'struct __bfloat16_s') and '__bfloat16')}} +__bfloat16 bar(__bfloat16 a, __bfloat16 b) { + return a + b; +} Index: clang/lib/Headers/avx512vlbf16intrin.h =================================================================== --- clang/lib/Headers/avx512vlbf16intrin.h +++ clang/lib/Headers/avx512vlbf16intrin.h @@ -415,9 +415,10 @@ /// and fraction field is truncated to 7 bits. static __inline__ __bfloat16 __DEFAULT_FN_ATTRS128 _mm_cvtness_sbh(float __A) { __v4sf __V = {__A, 0, 0, 0}; - __v8hi __R = __builtin_ia32_cvtneps2bf16_128_mask( + __v8hi __R1 = __builtin_ia32_cvtneps2bf16_128_mask( (__v4sf)__V, (__v8hi)_mm_undefined_si128(), (__mmask8)-1); - return __R[0]; + __bfloat16 __R2 = {__R1[0]}; + return __R2; } /// Convert Packed BF16 Data to Packed float Data. Index: clang/lib/Headers/avx512bf16intrin.h =================================================================== --- clang/lib/Headers/avx512bf16intrin.h +++ clang/lib/Headers/avx512bf16intrin.h @@ -15,7 +15,14 @@ typedef short __m512bh __attribute__((__vector_size__(64), __aligned__(64))); typedef short __m256bh __attribute__((__vector_size__(32), __aligned__(32))); -typedef unsigned short __bfloat16; + +/// \typedef __bfloat16 +/// A target specific type to represent the storage only brain floating-point +/// format type. Define through structure to explicitly prohibit any +/// arithmatic operations. +typedef struct __bfloat16_s { + short _Value; +} __bfloat16; #define __DEFAULT_FN_ATTRS512 \ __attribute__((__always_inline__, __nodebug__, __target__("avx512bf16"), \ @@ -34,7 +41,7 @@ /// \returns A float data whose sign field and exponent field keep unchanged, /// and fraction field is extended to 23 bits. static __inline__ float __DEFAULT_FN_ATTRS _mm_cvtsbh_ss(__bfloat16 __A) { - return __builtin_ia32_cvtsbf162ss_32(__A); + return __builtin_ia32_cvtsbf162ss_32(__A._Value); } /// Convert Two Packed Single Data to One Packed BF16 Data.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits