Skip to content

Commit

Permalink
[FP16] Add a feature flag for FP16. (WebAssembly#6864)
Browse files Browse the repository at this point in the history
Ensure the "fp16" feature is enabled for FP16 instructions.
  • Loading branch information
brendandahl authored Aug 22, 2024
1 parent 2d99e10 commit 95a280f
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 134 deletions.
247 changes: 130 additions & 117 deletions src/tools/fuzzing/fuzzing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3271,116 +3271,119 @@ Expression* TranslateToFuzzReader::makeBinary(Type type) {
}
case Type::v128: {
assert(wasm.features.hasSIMD());
return buildBinary({pick(EqVecI8x16,
NeVecI8x16,
LtSVecI8x16,
LtUVecI8x16,
GtSVecI8x16,
GtUVecI8x16,
LeSVecI8x16,
LeUVecI8x16,
GeSVecI8x16,
GeUVecI8x16,
EqVecI16x8,
NeVecI16x8,
LtSVecI16x8,
LtUVecI16x8,
GtSVecI16x8,
GtUVecI16x8,
LeSVecI16x8,
LeUVecI16x8,
GeSVecI16x8,
GeUVecI16x8,
EqVecI32x4,
NeVecI32x4,
LtSVecI32x4,
LtUVecI32x4,
GtSVecI32x4,
GtUVecI32x4,
LeSVecI32x4,
LeUVecI32x4,
GeSVecI32x4,
GeUVecI32x4,
EqVecF16x8,
EqVecF16x8,
NeVecF16x8,
LtVecF16x8,
GtVecF16x8,
LeVecF16x8,
GeVecF16x8,
EqVecF32x4,
NeVecF32x4,
LtVecF32x4,
GtVecF32x4,
LeVecF32x4,
GeVecF32x4,
EqVecF64x2,
NeVecF64x2,
LtVecF64x2,
GtVecF64x2,
LeVecF64x2,
GeVecF64x2,
AndVec128,
OrVec128,
XorVec128,
AndNotVec128,
AddVecI8x16,
AddSatSVecI8x16,
AddSatUVecI8x16,
SubVecI8x16,
SubSatSVecI8x16,
SubSatUVecI8x16,
MinSVecI8x16,
MinUVecI8x16,
MaxSVecI8x16,
MaxUVecI8x16,
// TODO: avgr_u
// TODO: q15mulr_sat_s
// TODO: extmul
AddVecI16x8,
AddSatSVecI16x8,
AddSatUVecI16x8,
SubVecI16x8,
SubSatSVecI16x8,
SubSatUVecI16x8,
MulVecI16x8,
MinSVecI16x8,
MinUVecI16x8,
MaxSVecI16x8,
MaxUVecI16x8,
AddVecI32x4,
SubVecI32x4,
MulVecI32x4,
MinSVecI32x4,
MinUVecI32x4,
MaxSVecI32x4,
MaxUVecI32x4,
DotSVecI16x8ToVecI32x4,
AddVecI64x2,
SubVecI64x2,
AddVecF16x8,
SubVecF16x8,
MulVecF16x8,
DivVecF16x8,
MinVecF16x8,
MaxVecF16x8,
AddVecF32x4,
SubVecF32x4,
MulVecF32x4,
DivVecF32x4,
MinVecF32x4,
MaxVecF32x4,
AddVecF64x2,
SubVecF64x2,
MulVecF64x2,
DivVecF64x2,
MinVecF64x2,
MaxVecF64x2,
NarrowSVecI16x8ToVecI8x16,
NarrowUVecI16x8ToVecI8x16,
NarrowSVecI32x4ToVecI16x8,
NarrowUVecI32x4ToVecI16x8,
SwizzleVecI8x16),
return buildBinary({pick(FeatureOptions<BinaryOp>()
.add(FeatureSet::SIMD,
EqVecI8x16,
NeVecI8x16,
LtSVecI8x16,
LtUVecI8x16,
GtSVecI8x16,
GtUVecI8x16,
LeSVecI8x16,
LeUVecI8x16,
GeSVecI8x16,
GeUVecI8x16,
EqVecI16x8,
NeVecI16x8,
LtSVecI16x8,
LtUVecI16x8,
GtSVecI16x8,
GtUVecI16x8,
LeSVecI16x8,
LeUVecI16x8,
GeSVecI16x8,
GeUVecI16x8,
EqVecI32x4,
NeVecI32x4,
LtSVecI32x4,
LtUVecI32x4,
GtSVecI32x4,
GtUVecI32x4,
LeSVecI32x4,
LeUVecI32x4,
GeSVecI32x4,
GeUVecI32x4,
EqVecF32x4,
NeVecF32x4,
LtVecF32x4,
GtVecF32x4,
LeVecF32x4,
GeVecF32x4,
EqVecF64x2,
NeVecF64x2,
LtVecF64x2,
GtVecF64x2,
LeVecF64x2,
GeVecF64x2,
AndVec128,
OrVec128,
XorVec128,
AndNotVec128,
AddVecI8x16,
AddSatSVecI8x16,
AddSatUVecI8x16,
SubVecI8x16,
SubSatSVecI8x16,
SubSatUVecI8x16,
MinSVecI8x16,
MinUVecI8x16,
MaxSVecI8x16,
MaxUVecI8x16,
// TODO: avgr_u
// TODO: q15mulr_sat_s
// TODO: extmul
AddVecI16x8,
AddSatSVecI16x8,
AddSatUVecI16x8,
SubVecI16x8,
SubSatSVecI16x8,
SubSatUVecI16x8,
MulVecI16x8,
MinSVecI16x8,
MinUVecI16x8,
MaxSVecI16x8,
MaxUVecI16x8,
AddVecI32x4,
SubVecI32x4,
MulVecI32x4,
MinSVecI32x4,
MinUVecI32x4,
MaxSVecI32x4,
MaxUVecI32x4,
DotSVecI16x8ToVecI32x4,
AddVecI64x2,
SubVecI64x2,
AddVecF32x4,
SubVecF32x4,
MulVecF32x4,
DivVecF32x4,
MinVecF32x4,
MaxVecF32x4,
AddVecF64x2,
SubVecF64x2,
MulVecF64x2,
DivVecF64x2,
MinVecF64x2,
MaxVecF64x2,
NarrowSVecI16x8ToVecI8x16,
NarrowUVecI16x8ToVecI8x16,
NarrowSVecI32x4ToVecI16x8,
NarrowUVecI32x4ToVecI16x8,
SwizzleVecI8x16)
.add(FeatureSet::FP16,
EqVecF16x8,
EqVecF16x8,
NeVecF16x8,
LtVecF16x8,
GtVecF16x8,
LeVecF16x8,
GeVecF16x8,
AddVecF16x8,
SubVecF16x8,
MulVecF16x8,
DivVecF16x8,
MinVecF16x8,
MaxVecF16x8)),
make(Type::v128),
make(Type::v128)});
}
Expand Down Expand Up @@ -3586,7 +3589,9 @@ Expression* TranslateToFuzzReader::makeSIMDExtract(Type type) {
op = ExtractLaneVecI64x2;
break;
case Type::f32:
op = ExtractLaneVecF32x4;
op = pick(FeatureOptions<SIMDExtractOp>()
.add(FeatureSet::SIMD, ExtractLaneVecF32x4)
.add(FeatureSet::FP16, ExtractLaneVecF16x8));
break;
case Type::f64:
op = ExtractLaneVecF64x2;
Expand Down Expand Up @@ -3621,12 +3626,16 @@ Expression* TranslateToFuzzReader::makeSIMDExtract(Type type) {
}

Expression* TranslateToFuzzReader::makeSIMDReplace() {
SIMDReplaceOp op = pick(ReplaceLaneVecI8x16,
ReplaceLaneVecI16x8,
ReplaceLaneVecI32x4,
ReplaceLaneVecI64x2,
ReplaceLaneVecF32x4,
ReplaceLaneVecF64x2);
SIMDReplaceOp op =
pick(FeatureOptions<SIMDReplaceOp>()
.add(FeatureSet::SIMD,
ReplaceLaneVecI8x16,
ReplaceLaneVecI16x8,
ReplaceLaneVecI32x4,
ReplaceLaneVecI64x2,
ReplaceLaneVecF32x4,
ReplaceLaneVecF64x2)
.add(FeatureSet::FeatureSet::FP16, ReplaceLaneVecF16x8));
Expression* vec = make(Type::v128);
uint8_t index;
Type lane_t;
Expand All @@ -3647,6 +3656,10 @@ Expression* TranslateToFuzzReader::makeSIMDReplace() {
index = upTo(2);
lane_t = Type::i64;
break;
case ReplaceLaneVecF16x8:
index = upTo(8);
lane_t = Type::f32;
break;
case ReplaceLaneVecF32x4:
index = upTo(4);
lane_t = Type::f32;
Expand Down
1 change: 1 addition & 0 deletions src/tools/tool-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ struct ToolOptions : public Options {
.addFeature(FeatureSet::MultiMemory, "multimemory")
.addFeature(FeatureSet::TypedContinuations, "typed continuations")
.addFeature(FeatureSet::SharedEverything, "shared-everything threads")
.addFeature(FeatureSet::FP16, "float 16 operations")
.add("--enable-typed-function-references",
"",
"Deprecated compatibility flag",
Expand Down
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ extern const char* StringsFeature;
extern const char* MultiMemoryFeature;
extern const char* TypedContinuationsFeature;
extern const char* SharedEverythingFeature;
extern const char* FP16Feature;

enum Subsection {
NameModule = 0,
Expand Down
7 changes: 6 additions & 1 deletion src/wasm-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ struct FeatureSet {
MultiMemory = 1 << 15,
TypedContinuations = 1 << 16,
SharedEverything = 1 << 17,
FP16 = 1 << 18,
MVP = None,
// Keep in sync with llvm default features:
// https://github.com/llvm/llvm-project/blob/c7576cb89d6c95f03968076e902d3adfd1996577/clang/lib/Basic/Targets/WebAssembly.cpp#L150-L153
Default = SignExt | MutableGlobals,
All = (1 << 18) - 1,
All = (1 << 19) - 1,
};

static std::string toString(Feature f) {
Expand Down Expand Up @@ -91,6 +92,8 @@ struct FeatureSet {
return "typed-continuations";
case SharedEverything:
return "shared-everything";
case FP16:
return "fp16";
default:
WASM_UNREACHABLE("unexpected feature");
}
Expand Down Expand Up @@ -141,6 +144,7 @@ struct FeatureSet {
bool hasSharedEverything() const {
return (features & SharedEverything) != 0;
}
bool hasFP16() const { return (features & FP16) != 0; }
bool hasAll() const { return (features & All) != 0; }

void set(FeatureSet f, bool v = true) {
Expand All @@ -164,6 +168,7 @@ struct FeatureSet {
void setMultiMemory(bool v = true) { set(MultiMemory, v); }
void setTypedContinuations(bool v = true) { set(TypedContinuations, v); }
void setSharedEverything(bool v = true) { set(SharedEverything, v); }
void setFP16(bool v = true) { set(FP16, v); }
void setMVP() { features = MVP; }
void setAll() { features = All; }

Expand Down
4 changes: 4 additions & 0 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,8 @@ void WasmBinaryWriter::writeFeaturesSection() {
return BinaryConsts::CustomSections::TypedContinuationsFeature;
case FeatureSet::SharedEverything:
return BinaryConsts::CustomSections::SharedEverythingFeature;
case FeatureSet::FP16:
return BinaryConsts::CustomSections::FP16Feature;
case FeatureSet::None:
case FeatureSet::Default:
case FeatureSet::All:
Expand Down Expand Up @@ -3892,6 +3894,8 @@ void WasmBinaryReader::readFeatures(size_t payloadLen) {
feature = FeatureSet::TypedContinuations;
} else if (name == BinaryConsts::CustomSections::SharedEverythingFeature) {
feature = FeatureSet::SharedEverything;
} else if (name == BinaryConsts::CustomSections::FP16Feature) {
feature = FeatureSet::FP16;
} else {
// Silently ignore unknown features (this may be and old binaryen running
// on a new wasm).
Expand Down
Loading

0 comments on commit 95a280f

Please sign in to comment.