Skip to content

Commit 890146b

Browse files
pmatosvitalybuka
authored andcommitted
[WebAssembly] Initial support for reference type externref in clang
This patch introduces a new type __externref_t that denotes a WebAssembly opaque reference type. It also implements builtin __builtin_wasm_ref_null_extern(), that returns a null value of __externref_t. This lays the ground work for further builtins and reference types. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D122215
1 parent e2069be commit 890146b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+522
-17
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
11271127
#define RVV_TYPE(Name, Id, SingletonId) \
11281128
CanQualType SingletonId;
11291129
#include "clang/Basic/RISCVVTypes.def"
1130+
#define WASM_TYPE(Name, Id, SingletonId) CanQualType SingletonId;
1131+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
11301132

11311133
// Types for deductions in C++0x [stmt.ranged]'s desugaring. Built on demand.
11321134
mutable QualType AutoDeductTy; // Deduction against 'auto'.
@@ -1475,6 +1477,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
14751477
/// \pre \p EltTy must be a built-in type.
14761478
QualType getScalableVectorType(QualType EltTy, unsigned NumElts) const;
14771479

1480+
/// Return a WebAssembly externref type.
1481+
QualType getWebAssemblyExternrefType() const;
1482+
14781483
/// Return the unique reference to a vector type of the specified
14791484
/// element type and size.
14801485
///

clang/include/clang/AST/Type.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,10 @@ class alignas(8) Type : public ExtQualsTypeCommonBase {
20292029
/// Returns true for SVE scalable vector types.
20302030
bool isSVESizelessBuiltinType() const;
20312031

2032+
/// Check if this is a WebAssembly Reference Type.
2033+
bool isWebAssemblyReferenceType() const;
2034+
bool isWebAssemblyExternrefType() const;
2035+
20322036
/// Determines if this is a sizeless type supported by the
20332037
/// 'arm_sve_vector_bits' type attribute, which can be applied to a single
20342038
/// SVE vector or predicate, excluding tuple types such as svint32x4_t.
@@ -2642,6 +2646,9 @@ class BuiltinType : public Type {
26422646
// RVV Types
26432647
#define RVV_TYPE(Name, Id, SingletonId) Id,
26442648
#include "clang/Basic/RISCVVTypes.def"
2649+
// WebAssembly reference types
2650+
#define WASM_TYPE(Name, Id, SingletonId) Id,
2651+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
26452652
// All other builtin types
26462653
#define BUILTIN_TYPE(Id, SingletonId) Id,
26472654
#define LAST_BUILTIN_TYPE(Id) LastKind = Id

clang/include/clang/AST/TypeProperties.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,10 @@ let Class = BuiltinType in {
813813
case BuiltinType::ID: return ctx.SINGLETON_ID;
814814
#include "clang/Basic/RISCVVTypes.def"
815815

816+
#define WASM_TYPE(NAME, ID, SINGLETON_ID) \
817+
case BuiltinType::ID: return ctx.SINGLETON_ID;
818+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
819+
816820
#define BUILTIN_TYPE(ID, SINGLETON_ID) \
817821
case BuiltinType::ID: return ctx.SINGLETON_ID;
818822
#include "clang/AST/BuiltinTypes.def"

clang/include/clang/Basic/BuiltinsWebAssembly.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,9 @@ TARGET_BUILTIN(__builtin_wasm_relaxed_dot_i8x16_i7x16_s_i16x8, "V8sV16ScV16Sc",
190190
TARGET_BUILTIN(__builtin_wasm_relaxed_dot_i8x16_i7x16_add_s_i32x4, "V4iV16ScV16ScV4i", "nc", "relaxed-simd")
191191
TARGET_BUILTIN(__builtin_wasm_relaxed_dot_bf16x8_add_f32_f32x4, "V4fV8UsV8UsV4f", "nc", "relaxed-simd")
192192

193+
// Reference Types builtins
194+
195+
TARGET_BUILTIN(__builtin_wasm_ref_null_extern, "i", "nct", "reference-types")
196+
193197
#undef BUILTIN
194198
#undef TARGET_BUILTIN

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11800,4 +11800,10 @@ def err_loongarch_builtin_requires_la32 : Error<
1180011800
def err_builtin_pass_in_regs_non_class : Error<
1180111801
"argument %0 is not an unqualified class type">;
1180211802

11803+
11804+
// WebAssembly reference type and table diagnostics.
11805+
def err_wasm_reference_pr : Error<
11806+
"%select{pointer|reference}0 to WebAssembly reference type is not allowed">;
11807+
def err_wasm_ca_reference : Error<
11808+
"cannot %select{capture|take address of}0 WebAssembly reference">;
1180311809
} // end of sema component.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- WebAssemblyReferenceTypes.def - Wasm reference types ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines externref_t. The macros are:
10+
//
11+
// WASM_TYPE(Name, Id, SingletonId)
12+
// WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS)
13+
//
14+
// where:
15+
//
16+
// - Name is the name of the builtin type.
17+
//
18+
// - MangledNameBase is the base used for name mangling.
19+
//
20+
// - BuiltinType::Id is the enumerator defining the type.
21+
//
22+
// - Context.SingletonId is the global singleton of this type.
23+
//
24+
// - AS indicates the address space for values of this type.
25+
//
26+
// To include this file, define either WASM_REF_TYPE or WASM_TYPE, depending on
27+
// how much information you want. The macros will be undefined after inclusion.
28+
//
29+
//===----------------------------------------------------------------------===//
30+
31+
32+
#ifndef WASM_REF_TYPE
33+
#define WASM_REF_TYPE(Name, MangledNameBase, Id, SingletonId, AS) \
34+
WASM_TYPE(Name, Id, SingletonId)
35+
#endif
36+
37+
WASM_REF_TYPE("__externref_t", "externref_t", WasmExternRef, WasmExternRefTy, 10)
38+
39+
#undef WASM_TYPE
40+
#undef WASM_REF_TYPE

clang/include/clang/Sema/Sema.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13496,6 +13496,9 @@ class Sema final {
1349613496
CallExpr *TheCall);
1349713497
bool CheckLoongArchBuiltinFunctionCall(const TargetInfo &TI,
1349813498
unsigned BuiltinID, CallExpr *TheCall);
13499+
bool CheckWebAssemblyBuiltinFunctionCall(const TargetInfo &TI,
13500+
unsigned BuiltinID,
13501+
CallExpr *TheCall);
1349913502

1350013503
bool SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall);
1350113504
bool SemaBuiltinVAStartARMMicrosoft(CallExpr *Call);
@@ -13561,6 +13564,9 @@ class Sema final {
1356113564
ExprResult SemaBuiltinMatrixColumnMajorStore(CallExpr *TheCall,
1356213565
ExprResult CallResult);
1356313566

13567+
// WebAssembly builtin handling.
13568+
bool BuiltinWasmRefNullExtern(CallExpr *TheCall);
13569+
1356413570
public:
1356513571
enum FormatStringType {
1356613572
FST_Scanf,

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,9 @@ enum PredefinedTypeIDs {
10961096
// \brief RISC-V V types with auto numeration
10971097
#define RVV_TYPE(Name, Id, SingletonId) PREDEF_TYPE_##Id##_ID,
10981098
#include "clang/Basic/RISCVVTypes.def"
1099+
// \brief WebAssembly reference types with auto numeration
1100+
#define WASM_TYPE(Name, Id, SingletonId) PREDEF_TYPE_##Id##_ID,
1101+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
10991102
};
11001103

11011104
/// The number of predefined type IDs that are reserved for

clang/include/clang/module.modulemap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ module Clang_Basic {
7474
textual header "Basic/Sanitizers.def"
7575
textual header "Basic/TargetCXXABI.def"
7676
textual header "Basic/TransformTypeTraits.def"
77+
textual header "Basic/TokenKinds.def"
78+
textual header "Basic/WebAssemblyReferenceTypes.def"
7779

7880
module * { export * }
7981
}

clang/lib/AST/ASTContext.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,12 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target,
15071507
#include "clang/Basic/RISCVVTypes.def"
15081508
}
15091509

1510+
if (Target.getTriple().isWasm() && Target.hasFeature("reference-types")) {
1511+
#define WASM_TYPE(Name, Id, SingletonId) \
1512+
InitBuiltinType(SingletonId, BuiltinType::Id);
1513+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
1514+
}
1515+
15101516
// Builtin type for __objc_yes and __objc_no
15111517
ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
15121518
SignedCharTy : BoolTy);
@@ -2335,6 +2341,12 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
23352341
Align = 8; \
23362342
break;
23372343
#include "clang/Basic/RISCVVTypes.def"
2344+
#define WASM_TYPE(Name, Id, SingletonId) \
2345+
case BuiltinType::Id: \
2346+
Width = 0; \
2347+
Align = 8; \
2348+
break;
2349+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
23382350
}
23392351
break;
23402352
case Type::ObjCObjectPointer:
@@ -4072,6 +4084,19 @@ ASTContext::getBuiltinVectorTypeInfo(const BuiltinType *Ty) const {
40724084
}
40734085
}
40744086

4087+
/// getExternrefType - Return a WebAssembly externref type, which represents an
4088+
/// opaque reference to a host value.
4089+
QualType ASTContext::getWebAssemblyExternrefType() const {
4090+
if (Target->getTriple().isWasm() && Target->hasFeature("reference-types")) {
4091+
#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \
4092+
if (BuiltinType::Id == BuiltinType::WasmExternRef) \
4093+
return SingletonId;
4094+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
4095+
}
4096+
assert(false &&
4097+
"shouldn't try to generate type externref outside WebAssembly target");
4098+
}
4099+
40754100
/// getScalableVectorType - Return the unique reference to a scalable vector
40764101
/// type of the specified element type and size. VectorType must be a built-in
40774102
/// type.
@@ -8111,6 +8136,8 @@ static char getObjCEncodingForPrimitiveType(const ASTContext *C,
81118136
#include "clang/Basic/AArch64SVEACLETypes.def"
81128137
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
81138138
#include "clang/Basic/RISCVVTypes.def"
8139+
#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
8140+
#include "clang/Basic/WebAssemblyReferenceTypes.def"
81148141
{
81158142
DiagnosticsEngine &Diags = C->getDiagnostics();
81168143
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,

0 commit comments

Comments
 (0)