Skip to content

Commit 6ea732c

Browse files
committed
[FP16] Add a feature flag for FP16.
Ensure the "fp16" feature is enabled for FP16 instructions.
1 parent 692e55c commit 6ea732c

21 files changed

Lines changed: 96 additions & 17 deletions

src/tools/tool-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct ToolOptions : public Options {
9595
.addFeature(FeatureSet::MultiMemory, "multimemory")
9696
.addFeature(FeatureSet::TypedContinuations, "typed continuations")
9797
.addFeature(FeatureSet::SharedEverything, "shared-everything threads")
98+
.addFeature(FeatureSet::FP16, "float 16 operations")
9899
.add("--enable-typed-function-references",
99100
"",
100101
"Deprecated compatibility flag",

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ extern const char* StringsFeature;
436436
extern const char* MultiMemoryFeature;
437437
extern const char* TypedContinuationsFeature;
438438
extern const char* SharedEverythingFeature;
439+
extern const char* FP16Feature;
439440

440441
enum Subsection {
441442
NameModule = 0,

src/wasm-features.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ struct FeatureSet {
4646
MultiMemory = 1 << 15,
4747
TypedContinuations = 1 << 16,
4848
SharedEverything = 1 << 17,
49+
FP16 = 1 << 18,
4950
MVP = None,
5051
// Keep in sync with llvm default features:
5152
// https://github.com/llvm/llvm-project/blob/c7576cb89d6c95f03968076e902d3adfd1996577/clang/lib/Basic/Targets/WebAssembly.cpp#L150-L153
5253
Default = SignExt | MutableGlobals,
53-
All = (1 << 18) - 1,
54+
All = (1 << 19) - 1,
5455
};
5556

5657
static std::string toString(Feature f) {
@@ -91,6 +92,8 @@ struct FeatureSet {
9192
return "typed-continuations";
9293
case SharedEverything:
9394
return "shared-everything";
95+
case FP16:
96+
return "fp16";
9497
default:
9598
WASM_UNREACHABLE("unexpected feature");
9699
}
@@ -141,6 +144,7 @@ struct FeatureSet {
141144
bool hasSharedEverything() const {
142145
return (features & SharedEverything) != 0;
143146
}
147+
bool hasFP16() const { return (features & FP16) != 0; }
144148
bool hasAll() const { return (features & All) != 0; }
145149

146150
void set(FeatureSet f, bool v = true) {
@@ -164,6 +168,7 @@ struct FeatureSet {
164168
void setMultiMemory(bool v = true) { set(MultiMemory, v); }
165169
void setTypedContinuations(bool v = true) { set(TypedContinuations, v); }
166170
void setSharedEverything(bool v = true) { set(SharedEverything, v); }
171+
void setFP16(bool v = true) { set(FP16, v); }
167172
void setMVP() { features = MVP; }
168173
void setAll() { features = All; }
169174

src/wasm/wasm-binary.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,8 @@ void WasmBinaryWriter::writeFeaturesSection() {
13581358
return BinaryConsts::CustomSections::TypedContinuationsFeature;
13591359
case FeatureSet::SharedEverything:
13601360
return BinaryConsts::CustomSections::SharedEverythingFeature;
1361+
case FeatureSet::FP16:
1362+
return BinaryConsts::CustomSections::FP16Feature;
13611363
case FeatureSet::None:
13621364
case FeatureSet::Default:
13631365
case FeatureSet::All:
@@ -3892,6 +3894,8 @@ void WasmBinaryReader::readFeatures(size_t payloadLen) {
38923894
feature = FeatureSet::TypedContinuations;
38933895
} else if (name == BinaryConsts::CustomSections::SharedEverythingFeature) {
38943896
feature = FeatureSet::SharedEverything;
3897+
} else if (name == BinaryConsts::CustomSections::FP16Feature) {
3898+
feature = FeatureSet::FP16;
38953899
} else {
38963900
// Silently ignore unknown features (this may be and old binaryen running
38973901
// on a new wasm).

src/wasm/wasm-validator.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,9 @@ void FunctionValidator::visitSIMDExtract(SIMDExtract* curr) {
12741274
lanes = 2;
12751275
break;
12761276
case ExtractLaneVecF16x8:
1277+
shouldBeTrue(getModule()->features.hasFP16(),
1278+
curr,
1279+
"FP16 operations require FP16 [--enable-fp16]");
12771280
lane_t = Type::f32;
12781281
lanes = 8;
12791282
break;
@@ -1324,6 +1327,9 @@ void FunctionValidator::visitSIMDReplace(SIMDReplace* curr) {
13241327
lanes = 2;
13251328
break;
13261329
case ReplaceLaneVecF16x8:
1330+
shouldBeTrue(getModule()->features.hasFP16(),
1331+
curr,
1332+
"FP16 operations require FP16 [--enable-fp16]");
13271333
lane_t = Type::f32;
13281334
lanes = 8;
13291335
break;
@@ -1708,6 +1714,24 @@ void FunctionValidator::visitBinary(Binary* curr) {
17081714
curr->left->type, Type(Type::f64), curr, "f64 op");
17091715
break;
17101716
}
1717+
case EqVecF16x8:
1718+
case NeVecF16x8:
1719+
case LtVecF16x8:
1720+
case LeVecF16x8:
1721+
case GtVecF16x8:
1722+
case GeVecF16x8:
1723+
case AddVecF16x8:
1724+
case SubVecF16x8:
1725+
case MulVecF16x8:
1726+
case DivVecF16x8:
1727+
case MinVecF16x8:
1728+
case MaxVecF16x8:
1729+
case PMinVecF16x8:
1730+
case PMaxVecF16x8:
1731+
shouldBeTrue(getModule()->features.hasFP16(),
1732+
curr,
1733+
"FP16 operations require FP16 [--enable-fp16]");
1734+
[[fallthrough]];
17111735
case EqVecI8x16:
17121736
case NeVecI8x16:
17131737
case LtSVecI8x16:
@@ -1744,12 +1768,6 @@ void FunctionValidator::visitBinary(Binary* curr) {
17441768
case LeSVecI64x2:
17451769
case GtSVecI64x2:
17461770
case GeSVecI64x2:
1747-
case EqVecF16x8:
1748-
case NeVecF16x8:
1749-
case LtVecF16x8:
1750-
case LeVecF16x8:
1751-
case GtVecF16x8:
1752-
case GeVecF16x8:
17531771
case EqVecF32x4:
17541772
case NeVecF32x4:
17551773
case LtVecF32x4:
@@ -1813,14 +1831,6 @@ void FunctionValidator::visitBinary(Binary* curr) {
18131831
case ExtMulHighSVecI64x2:
18141832
case ExtMulLowUVecI64x2:
18151833
case ExtMulHighUVecI64x2:
1816-
case AddVecF16x8:
1817-
case SubVecF16x8:
1818-
case MulVecF16x8:
1819-
case DivVecF16x8:
1820-
case MinVecF16x8:
1821-
case MaxVecF16x8:
1822-
case PMinVecF16x8:
1823-
case PMaxVecF16x8:
18241834
case AddVecF32x4:
18251835
case SubVecF32x4:
18261836
case MulVecF32x4:
@@ -2060,6 +2070,10 @@ void FunctionValidator::visitUnary(Unary* curr) {
20602070
curr->value->type, Type(Type::i64), curr, "expected i64 splat value");
20612071
break;
20622072
case SplatVecF16x8:
2073+
shouldBeTrue(getModule()->features.hasFP16(),
2074+
curr,
2075+
"FP16 operations require FP16 [--enable-fp16]");
2076+
[[fallthrough]];
20632077
case SplatVecF32x4:
20642078
shouldBeEqual(
20652079
curr->type, Type(Type::v128), curr, "expected splat to have v128 type");

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const char* StringsFeature = "strings";
5656
const char* MultiMemoryFeature = "multimemory";
5757
const char* TypedContinuationsFeature = "typed-continuations";
5858
const char* SharedEverythingFeature = "shared-everything";
59+
const char* FP16Feature = "fp16";
5960
} // namespace CustomSections
6061
} // namespace BinaryConsts
6162

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Features.RelaxedSIMD: 4096
3333
Features.ExtendedConst: 8192
3434
Features.Strings: 16384
3535
Features.MultiMemory: 32768
36-
Features.All: 262143
36+
Features.All: 524287
3737
InvalidId: 0
3838
BlockId: 1
3939
IfId: 2

test/example/c-api-kitchen-sink.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BinaryenFeatureMemory64: 2048
4747
BinaryenFeatureRelaxedSIMD: 4096
4848
BinaryenFeatureExtendedConst: 8192
4949
BinaryenFeatureStrings: 16384
50-
BinaryenFeatureAll: 262143
50+
BinaryenFeatureAll: 524287
5151
(f32.neg
5252
(f32.const -33.61199951171875)
5353
)

test/lit/help/wasm-as.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
;; CHECK-NEXT:
113113
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
114114
;; CHECK-NEXT:
115+
;; CHECK-NEXT: --enable-fp16 Enable float 16 operations
116+
;; CHECK-NEXT:
117+
;; CHECK-NEXT: --disable-fp16 Disable float 16 operations
118+
;; CHECK-NEXT:
115119
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
116120
;; CHECK-NEXT:
117121
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag

test/lit/help/wasm-ctor-eval.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@
119119
;; CHECK-NEXT:
120120
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
121121
;; CHECK-NEXT:
122+
;; CHECK-NEXT: --enable-fp16 Enable float 16 operations
123+
;; CHECK-NEXT:
124+
;; CHECK-NEXT: --disable-fp16 Disable float 16 operations
125+
;; CHECK-NEXT:
122126
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
123127
;; CHECK-NEXT:
124128
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag

0 commit comments

Comments
 (0)