Skip to content

Commit

Permalink
Remove redundant nullability field from Wasm type representation.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=244810

Reviewed by Justin Michaud.

* Source/JavaScriptCore/wasm/WasmAirIRGenerator.cpp:
(JSC::Wasm::AirIRGenerator::addRefFunc):
(JSC::Wasm::AirIRGenerator::addI31New):
* Source/JavaScriptCore/wasm/WasmFormat.h:
(JSC::Wasm::funcrefType):
(JSC::Wasm::externrefType):
* Source/JavaScriptCore/wasm/WasmFunctionParser.h:
(JSC::Wasm::FunctionParser<Context>::parseExpression):
* Source/JavaScriptCore/wasm/WasmParser.h:
(JSC::Wasm::Parser<SuccessType>::parseBlockSignature):
(JSC::Wasm::Parser<SuccessType>::parseValueType):
* Source/JavaScriptCore/wasm/WasmSectionParser.cpp:
(JSC::Wasm::SectionParser::parseInitExpr):
* Source/JavaScriptCore/wasm/WasmTypeDefinition.cpp:
(JSC::Wasm::computeSignatureHash):
(JSC::Wasm::computeStructTypeHash):
(JSC::Wasm::TypeDefinition::substitute):
* Source/JavaScriptCore/wasm/generateWasmOpsHeader.py:

Canonical link: https://commits.webkit.org/254384@main
  • Loading branch information
dbezhetskov authored and Justin Michaud committed Sep 12, 2022
1 parent 62339da commit 2f87b6a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/wasm/WasmAirIRGenerator.cpp
Expand Up @@ -1330,7 +1330,7 @@ auto AirIRGenerator::addRefFunc(uint32_t index, ExpressionType& result) -> Parti
// FIXME: Emit this inline <https://bugs.webkit.org/show_bug.cgi?id=198506>.
if (Options::useWebAssemblyTypedFunctionReferences()) {
TypeIndex typeIndex = m_info.typeIndexFromFunctionIndexSpace(index);
result = tmpForType(Type { TypeKind::Ref, Nullable::No, typeIndex });
result = tmpForType(Type { TypeKind::Ref, typeIndex });
} else
result = tmpForType(Types::Funcref);
emitCCall(&operationWasmRefFunc, result, instanceValue(), addConstant(Types::I32, index));
Expand Down Expand Up @@ -3018,7 +3018,7 @@ auto AirIRGenerator::truncSaturated(Ext1OpType op, ExpressionType arg, Expressio
auto AirIRGenerator::addI31New(ExpressionType value, ExpressionType& result) -> PartialResult
{
auto tmp1 = g32();
result = gRef(Type { TypeKind::Ref, Nullable::No, static_cast<TypeIndex>(TypeKind::I31ref) });
result = gRef(Type { TypeKind::Ref, static_cast<TypeIndex>(TypeKind::I31ref) });

append(Move, Arg::bigImm(0x7fffffff), tmp1);
append(And32, tmp1, value, tmp1);
Expand Down
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/wasm/WasmFormat.h
Expand Up @@ -130,14 +130,14 @@ inline bool isI31ref(Type type)
inline Type funcrefType()
{
if (Options::useWebAssemblyTypedFunctionReferences())
return Wasm::Type { Wasm::TypeKind::RefNull, Wasm::Nullable::Yes, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Funcref) };
return Wasm::Type { Wasm::TypeKind::RefNull, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Funcref) };
return Types::Funcref;
}

inline Type externrefType()
{
if (Options::useWebAssemblyTypedFunctionReferences())
return Wasm::Type { Wasm::TypeKind::RefNull, Wasm::Nullable::Yes, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Externref) };
return Wasm::Type { Wasm::TypeKind::RefNull, static_cast<Wasm::TypeIndex>(Wasm::TypeKind::Externref) };
return Types::Externref;
}

Expand Down
8 changes: 4 additions & 4 deletions Source/JavaScriptCore/wasm/WasmFunctionParser.h
Expand Up @@ -1073,7 +1073,7 @@ FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_CASE)
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addI31New(value, result));

m_expressionStack.constructAndAppend(Type { TypeKind::Ref, Nullable::No, static_cast<TypeIndex>(TypeKind::I31ref) }, result);
m_expressionStack.constructAndAppend(Type { TypeKind::Ref, static_cast<TypeIndex>(TypeKind::I31ref) }, result);
return { };
}
case GCOpType::I31GetS: {
Expand Down Expand Up @@ -1153,9 +1153,9 @@ FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_CASE)
WASM_PARSER_FAIL_IF(!parseHeapType(m_info, heapType), "ref.null heaptype must be funcref, externref or type_idx");
if (isTypeIndexHeapType(heapType)) {
TypeIndex typeIndex = TypeInformation::get(m_info.typeSignatures[heapType].get());
typeOfNull = Type { TypeKind::RefNull, Nullable::Yes, typeIndex };
typeOfNull = Type { TypeKind::RefNull, typeIndex };
} else
typeOfNull = Type { TypeKind::RefNull, Nullable::Yes, static_cast<TypeIndex>(heapType) };
typeOfNull = Type { TypeKind::RefNull, static_cast<TypeIndex>(heapType) };
} else
WASM_PARSER_FAIL_IF(!parseRefType(m_info, typeOfNull), "ref.null type must be a reference type");
m_expressionStack.constructAndAppend(typeOfNull, m_context.addConstant(typeOfNull, JSValue::encode(jsNull())));
Expand Down Expand Up @@ -1184,7 +1184,7 @@ FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_CASE)

if (Options::useWebAssemblyTypedFunctionReferences()) {
TypeIndex typeIndex = m_info.typeIndexFromFunctionIndexSpace(index);
m_expressionStack.constructAndAppend(Type { TypeKind::Ref, Nullable::No, typeIndex }, result);
m_expressionStack.constructAndAppend(Type { TypeKind::Ref, typeIndex }, result);
return { };
}

Expand Down
6 changes: 2 additions & 4 deletions Source/JavaScriptCore/wasm/WasmParser.h
Expand Up @@ -289,7 +289,7 @@ ALWAYS_INLINE typename Parser<SuccessType>::PartialResult Parser<SuccessType>::p
{
int8_t typeKind;
if (peekInt7(typeKind) && isValidTypeKind(typeKind)) {
Type type = {static_cast<TypeKind>(typeKind), Nullable::Yes, 0};
Type type = { static_cast<TypeKind>(typeKind), 0 };
WASM_PARSER_FAIL_IF(!(isValueType(type) || type.isVoid()), "result type of block: ", makeString(type.kind), " is not a value type or Void");
result = m_typeInformation.thunkFor(type);
m_offset++;
Expand Down Expand Up @@ -340,13 +340,11 @@ ALWAYS_INLINE bool Parser<SuccessType>::parseValueType(const ModuleInformation&
return false;

TypeKind typeKind = static_cast<TypeKind>(kind);
bool isNullable = true;
TypeIndex typeIndex = 0;
if (Options::useWebAssemblyTypedFunctionReferences() && (typeKind == TypeKind::Funcref || typeKind == TypeKind::Externref || typeKind == TypeKind::I31ref)) {
typeIndex = static_cast<TypeIndex>(typeKind);
typeKind = TypeKind::RefNull;
} else if (typeKind == TypeKind::Ref || typeKind == TypeKind::RefNull) {
isNullable = typeKind == TypeKind::RefNull;
int32_t heapType;
if (!parseHeapType(info, heapType))
return false;
Expand All @@ -365,7 +363,7 @@ ALWAYS_INLINE bool Parser<SuccessType>::parseValueType(const ModuleInformation&
}
}

Type type = { typeKind, static_cast<Nullable>(isNullable), typeIndex };
Type type = { typeKind, typeIndex };
if (!isValueType(type))
return false;
result = type;
Expand Down
6 changes: 3 additions & 3 deletions Source/JavaScriptCore/wasm/WasmSectionParser.cpp
Expand Up @@ -625,9 +625,9 @@ auto SectionParser::parseInitExpr(uint8_t& opcode, uint64_t& bitsOrImportNumber,
WASM_PARSER_FAIL_IF(!parseHeapType(m_info, heapType), "ref.null heaptype must be funcref, externref or type_idx");
if (isTypeIndexHeapType(heapType)) {
TypeIndex typeIndex = TypeInformation::get(m_info->typeSignatures[heapType].get());
typeOfNull = Type { TypeKind::RefNull, Nullable::Yes, typeIndex };
typeOfNull = Type { TypeKind::RefNull, typeIndex };
} else
typeOfNull = Type { TypeKind::RefNull, Nullable::Yes, static_cast<TypeIndex>(heapType) };
typeOfNull = Type { TypeKind::RefNull, static_cast<TypeIndex>(heapType) };
} else
WASM_PARSER_FAIL_IF(!parseRefType(m_info, typeOfNull), "ref.null type must be a reference type");

Expand All @@ -644,7 +644,7 @@ auto SectionParser::parseInitExpr(uint8_t& opcode, uint64_t& bitsOrImportNumber,

if (Options::useWebAssemblyTypedFunctionReferences()) {
TypeIndex typeIndex = m_info->typeIndexFromFunctionIndexSpace(index);
resultType = { TypeKind::Ref, Nullable::No, typeIndex };
resultType = { TypeKind::Ref, typeIndex };
} else
resultType = Types::Funcref;

Expand Down
5 changes: 1 addition & 4 deletions Source/JavaScriptCore/wasm/WasmTypeDefinition.cpp
Expand Up @@ -130,12 +130,10 @@ static unsigned computeSignatureHash(size_t returnCount, const Type* returnTypes
unsigned accumulator = 0xa1bcedd8u;
for (uint32_t i = 0; i < argumentCount; ++i) {
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(argumentTypes[i].kind)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(argumentTypes[i].nullable)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<unsigned>::hash(argumentTypes[i].index));
}
for (uint32_t i = 0; i < returnCount; ++i) {
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(returnTypes[i].kind)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(returnTypes[i].nullable)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<unsigned>::hash(returnTypes[i].index));
}
return accumulator;
Expand All @@ -146,7 +144,6 @@ static unsigned computeStructTypeHash(size_t fieldCount, const StructField* fiel
unsigned accumulator = 0x15d2546;
for (uint32_t i = 0; i < fieldCount; ++i) {
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(fields[i].type.kind)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(fields[i].type.nullable)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(fields[i].type.index)));
accumulator = WTF::pairIntHash(accumulator, WTF::IntHash<uint8_t>::hash(static_cast<uint8_t>(fields[i].mutability)));
}
Expand Down Expand Up @@ -248,7 +245,7 @@ Type TypeDefinition::substitute(Type type, TypeIndex projectee)
if (type.kind == TypeKind::Rec) {
RefPtr<TypeDefinition> projection = TypeInformation::typeDefinitionForProjection(projectee, static_cast<ProjectionIndex>(type.index));
TypeKind kind = type.isNullable() ? TypeKind::RefNull : TypeKind::Ref;
return Type { kind, type.nullable, projection->index() };
return Type { kind, projection->index() };
}

return type;
Expand Down
12 changes: 3 additions & 9 deletions Source/JavaScriptCore/wasm/generateWasmOpsHeader.py
Expand Up @@ -226,21 +226,15 @@ def atomicMemoryLog2AlignmentGenerator(filter):
};
#undef CREATE_ENUM_VALUE
enum class Nullable : bool {
No = false,
Yes = true,
};
using TypeIndex = uintptr_t;
struct Type {
TypeKind kind;
Nullable nullable;
TypeIndex index;
bool operator==(const Type& other) const
{
return other.kind == kind && other.nullable == nullable && other.index == index;
return other.kind == kind && other.isNullable() == isNullable() && other.index == index;
}
bool operator!=(const Type& other) const
Expand All @@ -250,7 +244,7 @@ def atomicMemoryLog2AlignmentGenerator(filter):
bool isNullable() const
{
return static_cast<bool>(nullable);
return kind == TypeKind::RefNull || kind == TypeKind::Externref || kind == TypeKind::Funcref;
}
void dump(PrintStream& out) const
Expand All @@ -271,7 +265,7 @@ def atomicMemoryLog2AlignmentGenerator(filter):
namespace Types
{
#define CREATE_CONSTANT(name, id, ...) constexpr Type name = Type{TypeKind::name, Nullable::Yes, 0u};
#define CREATE_CONSTANT(name, id, ...) constexpr Type name = Type{TypeKind::name, 0u};
FOR_EACH_WASM_TYPE(CREATE_CONSTANT)
#undef CREATE_CONSTANT
} // namespace Types
Expand Down

0 comments on commit 2f87b6a

Please sign in to comment.