Skip to content

Commit d9fd0dd

Browse files
authored
[WebAssembly] Add half-precision feature (#90248)
This currently only defines a constant, but in the future will be used to gate builtins for experimenting and prototyping half-precision proposal (https://github.com/WebAssembly/half-precision).
1 parent eb5907d commit d9fd0dd

File tree

7 files changed

+29
-0
lines changed

7 files changed

+29
-0
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4881,6 +4881,8 @@ def msimd128 : Flag<["-"], "msimd128">, Group<m_wasm_Features_Group>;
48814881
def mno_simd128 : Flag<["-"], "mno-simd128">, Group<m_wasm_Features_Group>;
48824882
def mrelaxed_simd : Flag<["-"], "mrelaxed-simd">, Group<m_wasm_Features_Group>;
48834883
def mno_relaxed_simd : Flag<["-"], "mno-relaxed-simd">, Group<m_wasm_Features_Group>;
4884+
def mhalf_precision : Flag<["-"], "mhalf-precision">, Group<m_wasm_Features_Group>;
4885+
def mno_half_precision : Flag<["-"], "mno-half-precision">, Group<m_wasm_Features_Group>;
48844886
def mnontrapping_fptoint : Flag<["-"], "mnontrapping-fptoint">, Group<m_wasm_Features_Group>;
48854887
def mno_nontrapping_fptoint : Flag<["-"], "mno-nontrapping-fptoint">, Group<m_wasm_Features_Group>;
48864888
def msign_ext : Flag<["-"], "msign-ext">, Group<m_wasm_Features_Group>;

clang/lib/Basic/Targets/WebAssembly.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
4747
return llvm::StringSwitch<bool>(Feature)
4848
.Case("simd128", SIMDLevel >= SIMD128)
4949
.Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
50+
.Case("half-precision", HasHalfPrecision)
5051
.Case("nontrapping-fptoint", HasNontrappingFPToInt)
5152
.Case("sign-ext", HasSignExt)
5253
.Case("exception-handling", HasExceptionHandling)
@@ -156,6 +157,7 @@ bool WebAssemblyTargetInfo::initFeatureMap(
156157
Features["reference-types"] = true;
157158
Features["sign-ext"] = true;
158159
Features["tail-call"] = true;
160+
Features["half-precision"] = true;
159161
setSIMDLevel(Features, SIMD128, true);
160162
} else if (CPU == "generic") {
161163
Features["mutable-globals"] = true;
@@ -216,6 +218,15 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
216218
HasBulkMemory = false;
217219
continue;
218220
}
221+
if (Feature == "+half-precision") {
222+
SIMDLevel = std::max(SIMDLevel, SIMD128);
223+
HasHalfPrecision = true;
224+
continue;
225+
}
226+
if (Feature == "-half-precision") {
227+
HasHalfPrecision = false;
228+
continue;
229+
}
219230
if (Feature == "+atomics") {
220231
HasAtomics = true;
221232
continue;

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
6464
bool HasReferenceTypes = false;
6565
bool HasExtendedConst = false;
6666
bool HasMultiMemory = false;
67+
bool HasHalfPrecision = false;
6768

6869
std::string ABI;
6970

clang/test/Driver/wasm-features.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
// RELAXED-SIMD: "-target-feature" "+relaxed-simd"
7878
// NO-RELAXED-SIMD: "-target-feature" "-relaxed-simd"
7979

80+
// RUN: %clang --target=wasm32-unknown-unknown -### %s -mhalf-precision 2>&1 | FileCheck %s -check-prefix=HALF-PRECISION
81+
// RUN: %clang --target=wasm32-unknown-unknown -### %s -mno-half-precision 2>&1 | FileCheck %s -check-prefix=NO-HALF-PRECISION
82+
83+
// HALF-PRECISION: "-target-feature" "+half-precision"
84+
// NO-HALF-PRECISION: "-target-feature" "-half-precision"
85+
8086
// RUN: %clang --target=wasm32-unknown-unknown -### %s -mexception-handling 2>&1 | FileCheck %s -check-prefix=EXCEPTION-HANDLING
8187
// RUN: %clang --target=wasm32-unknown-unknown -### %s -mno-exception-handling 2>&1 | FileCheck %s -check-prefix=NO-EXCEPTION-HANDLING
8288

llvm/lib/Target/WebAssembly/WebAssembly.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def FeatureSIMD128 : SubtargetFeature<"simd128", "SIMDLevel", "SIMD128",
2828
def FeatureRelaxedSIMD : SubtargetFeature<"relaxed-simd", "SIMDLevel", "RelaxedSIMD",
2929
"Enable relaxed-simd instructions">;
3030

31+
def FeatureHalfPrecision : SubtargetFeature<"half-precision", "HasHalfPrecision", "true",
32+
"Enable half precision instructions">;
33+
3134
def FeatureAtomics : SubtargetFeature<"atomics", "HasAtomics", "true",
3235
"Enable Atomics">;
3336

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def HasRelaxedSIMD :
3030
Predicate<"Subtarget->hasRelaxedSIMD()">,
3131
AssemblerPredicate<(all_of FeatureRelaxedSIMD), "relaxed-simd">;
3232

33+
def HasHalfPrecision :
34+
Predicate<"Subtarget->hasHalfPrecision()">,
35+
AssemblerPredicate<(all_of FeatureHalfPrecision), "half-precision">;
36+
3337
def HasAtomics :
3438
Predicate<"Subtarget->hasAtomics()">,
3539
AssemblerPredicate<(all_of FeatureAtomics), "atomics">;

llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
5050
bool HasReferenceTypes = false;
5151
bool HasExtendedConst = false;
5252
bool HasMultiMemory = false;
53+
bool HasHalfPrecision = false;
5354

5455
/// What processor and OS we're targeting.
5556
Triple TargetTriple;
@@ -93,6 +94,7 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
9394
bool hasAddr64() const { return TargetTriple.isArch64Bit(); }
9495
bool hasSIMD128() const { return SIMDLevel >= SIMD128; }
9596
bool hasRelaxedSIMD() const { return SIMDLevel >= RelaxedSIMD; }
97+
bool hasHalfPrecision() const { return HasHalfPrecision; }
9698
bool hasAtomics() const { return HasAtomics; }
9799
bool hasNontrappingFPToInt() const { return HasNontrappingFPToInt; }
98100
bool hasSignExt() const { return HasSignExt; }

0 commit comments

Comments
 (0)