Skip to content

Commit 764f408

Browse files
committed
[WebAssembly] Add reference types target feature
Summary: This adds the reference types target feature. This does not enable any more functionality in LLVM/clang for now, but this is necessary to embed the info in the target features section, which is used by Binaryen and Emscripten. It turned out that after D69832 `-fwasm-exceptions` crashed because we didn't have the reference types target feature. Reviewers: tlively Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D73320
1 parent d3bf06b commit 764f408

File tree

8 files changed

+71
-7
lines changed

8 files changed

+71
-7
lines changed

clang/lib/Basic/Targets/WebAssembly.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
4545
.Case("mutable-globals", HasMutableGlobals)
4646
.Case("multivalue", HasMultivalue)
4747
.Case("tail-call", HasTailCall)
48+
.Case("reference-types", HasReferenceTypes)
4849
.Default(false);
4950
}
5051

@@ -80,6 +81,8 @@ void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
8081
Builder.defineMacro("__wasm_multivalue__");
8182
if (HasTailCall)
8283
Builder.defineMacro("__wasm_tail_call__");
84+
if (HasReferenceTypes)
85+
Builder.defineMacro("__wasm_reference_types__");
8386
}
8487

8588
void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
@@ -126,6 +129,8 @@ bool WebAssemblyTargetInfo::initFeatureMap(
126129
Features["multivalue"] = true;
127130
if (HasTailCall)
128131
Features["tail-call"] = true;
132+
if (HasReferenceTypes)
133+
Features["reference-types"] = true;
129134

130135
return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
131136
}
@@ -213,6 +218,14 @@ bool WebAssemblyTargetInfo::handleTargetFeatures(
213218
HasTailCall = false;
214219
continue;
215220
}
221+
if (Feature == "+reference-types") {
222+
HasReferenceTypes = true;
223+
continue;
224+
}
225+
if (Feature == "-reference-types") {
226+
HasReferenceTypes = false;
227+
continue;
228+
}
216229

217230
Diags.Report(diag::err_opt_not_valid_with_opt)
218231
<< Feature << "-target-feature";

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
3838
bool HasMutableGlobals = false;
3939
bool HasMultivalue = false;
4040
bool HasTailCall = false;
41+
bool HasReferenceTypes = false;
4142

4243
public:
4344
explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)

clang/test/Preprocessor/wasm-target-features.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@
9696
// RUN: | FileCheck %s -check-prefix=TAIL-CALL
9797
//
9898
// TAIL-CALL:#define __wasm_tail_call__ 1{{$}}
99+
//
100+
// RUN: %clang -E -dM %s -o - 2>&1 \
101+
// RUN: -target wasm32-unknown-unknown -mreference-types \
102+
// RUN: | FileCheck %s -check-prefix=REFERENCE-TYPES
103+
// RUN: %clang -E -dM %s -o - 2>&1 \
104+
// RUN: -target wasm64-unknown-unknown -mreference-types \
105+
// RUN: | FileCheck %s -check-prefix=REFERENCE-TYPES
106+
//
107+
// REFERENCE-TYPES:#define __wasm_reference_types__ 1{{$}}
99108

100109
// RUN: %clang -E -dM %s -o - 2>&1 \
101110
// RUN: -target wasm32-unknown-unknown -mcpu=mvp \
@@ -114,6 +123,7 @@
114123
// MVP-NOT:#define __wasm_mutable_globals__
115124
// MVP-NOT:#define __wasm_multivalue__
116125
// MVP-NOT:#define __wasm_tail_call__
126+
// MVP-NOT:#define __wasm_reference_types__
117127

118128
// RUN: %clang -E -dM %s -o - 2>&1 \
119129
// RUN: -target wasm32-unknown-unknown -mcpu=bleeding-edge \
@@ -130,6 +140,7 @@
130140
// BLEEDING-EDGE-NOT:#define __wasm_unimplemented_simd128__ 1{{$}}
131141
// BLEEDING-EDGE-NOT:#define __wasm_multivalue__ 1{{$}}
132142
// BLEEDING-EDGE-NOT:#define __wasm_tail_call__ 1{{$}}
143+
// BLEEDING-EDGE-NOT:#define __wasm_reference_types__ 1{{$}}
133144

134145
// RUN: %clang -E -dM %s -o - 2>&1 \
135146
// RUN: -target wasm32-unknown-unknown -mcpu=bleeding-edge -mno-simd128 \

llvm/lib/Target/WebAssembly/WebAssembly.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def FeatureMutableGlobals :
6666
SubtargetFeature<"mutable-globals", "HasMutableGlobals", "true",
6767
"Enable mutable globals">;
6868

69+
def FeatureReferenceTypes :
70+
SubtargetFeature<"reference-types", "HasReferenceTypes", "true",
71+
"Enable reference types">;
72+
6973
//===----------------------------------------------------------------------===//
7074
// Architectures.
7175
//===----------------------------------------------------------------------===//

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def HasBulkMemory :
6262
Predicate<"Subtarget->hasBulkMemory()">,
6363
AssemblerPredicate<"FeatureBulkMemory", "bulk-memory">;
6464

65+
def HasReferenceTypes :
66+
Predicate<"Subtarget->hasReferenceTypes()">,
67+
AssemblerPredicate<"FeatureReferenceTypes", "reference-types">;
68+
6569
//===----------------------------------------------------------------------===//
6670
// WebAssembly-specific DAG Node Types.
6771
//===----------------------------------------------------------------------===//

llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
4747
bool HasMultivalue = false;
4848
bool HasMutableGlobals = false;
4949
bool HasTailCall = false;
50+
bool HasReferenceTypes = false;
5051

5152
/// String name of used CPU.
5253
std::string CPUString;
@@ -104,6 +105,7 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
104105
bool hasMultivalue() const { return HasMultivalue; }
105106
bool hasMutableGlobals() const { return HasMutableGlobals; }
106107
bool hasTailCall() const { return HasTailCall; }
108+
bool hasReferenceTypes() const { return HasReferenceTypes; }
107109

108110
/// Parses features string setting specified subtarget options. Definition of
109111
/// function is auto generated by tblgen.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc < %s -mattr=+reference-types | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4+
target triple = "wasm32-unknown-unknown"
5+
6+
; CHECK-LABEL: reference-types
7+
define void @reference-types() {
8+
ret void
9+
}
10+
11+
; CHECK: .int8 1
12+
; CHECK-NEXT: .int8 43
13+
; CHECK-NEXT: .int8 15
14+
; CHECK-NEXT: .ascii "reference-types"

llvm/test/CodeGen/WebAssembly/target-features.ll

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ define void @fn_nontrapping_fptoint(i32* %p1, float %f2) #1 {
2323
ret void
2424
}
2525

26+
define void @fn_reference_types() #2 {
27+
ret void
28+
}
29+
2630
attributes #0 = { "target-features"="+atomics" }
2731
attributes #1 = { "target-features"="+nontrapping-fptoint" }
32+
attributes #2 = { "target-features"="+reference-types" }
2833

2934
; CHECK-LABEL: fn_atomics:
3035

@@ -51,29 +56,36 @@ attributes #1 = { "target-features"="+nontrapping-fptoint" }
5156

5257
; CHECK-LABEL: .custom_section.target_features,"",@
5358

54-
; +atomics, +nontrapping-fptoint
55-
; ATTRS-NEXT: .int8 2
59+
; +atomics, +nontrapping-fptoint, +reference-types
60+
; ATTRS-NEXT: .int8 3
5661
; ATTRS-NEXT: .int8 43
5762
; ATTRS-NEXT: .int8 7
5863
; ATTRS-NEXT: .ascii "atomics"
5964
; ATTRS-NEXT: .int8 43
6065
; ATTRS-NEXT: .int8 19
6166
; ATTRS-NEXT: .ascii "nontrapping-fptoint"
67+
; ATTRS-NEXT: .int8 43
68+
; ATTRS-NEXT: .int8 15
69+
; ATTRS-NEXT: .ascii "reference-types"
6270

63-
; +atomics, +simd128
64-
; SIMD128-NEXT: .int8 3
71+
; +atomics, +nontrapping-fptoint, +reference-types, +simd128
72+
; SIMD128-NEXT: .int8 4
6573
; SIMD128-NEXT: .int8 43
6674
; SIMD128-NEXT: .int8 7
6775
; SIMD128-NEXT: .ascii "atomics"
6876
; SIMD128-NEXT: .int8 43
6977
; SIMD128-NEXT: .int8 19
70-
; SIMD128-NEXT: .ascii "nontrapping-fptoint"
78+
; SIMD128-NEXT: .ascii "nontrapping-fptoint"
79+
; SIMD128-NEXT: .int8 43
80+
; SIMD128-NEXT: .int8 15
81+
; SIMD128-NEXT: .ascii "reference-types"
7182
; SIMD128-NEXT: .int8 43
7283
; SIMD128-NEXT: .int8 7
7384
; SIMD128-NEXT: .ascii "simd128"
7485

75-
; +atomics, +nontrapping-fptoint, +sign-ext, +simd128
76-
; BLEEDING-EDGE-NEXT: .int8 5
86+
; +atomics, +mutable-globals, +nontrapping-fptoint, +reference-types, +sign-ext,
87+
; +simd128
88+
; BLEEDING-EDGE-NEXT: .int8 6
7789
; BLEEDING-EDGE-NEXT: .int8 43
7890
; BLEEDING-EDGE-NEXT: .int8 7
7991
; BLEEDING-EDGE-NEXT: .ascii "atomics"
@@ -84,6 +96,9 @@ attributes #1 = { "target-features"="+nontrapping-fptoint" }
8496
; BLEEDING-EDGE-NEXT: .int8 19
8597
; BLEEDING-EDGE-NEXT: .ascii "nontrapping-fptoint"
8698
; BLEEDING-EDGE-NEXT: .int8 43
99+
; BLEEDING-EDGE-NEXT: .int8 15
100+
; BLEEDING-EDGE-NEXT: .ascii "reference-types"
101+
; BLEEDING-EDGE-NEXT: .int8 43
87102
; BLEEDING-EDGE-NEXT: .int8 8
88103
; BLEEDING-EDGE-NEXT: .ascii "sign-ext"
89104
; BLEEDING-EDGE-NEXT: .int8 43

0 commit comments

Comments
 (0)