Commit cca5c3f6 authored by bradnelson's avatar bradnelson Committed by Commit bot

Fix conversion to float32, typing issue, split apart asm-wasm tests.

Add missing conversions from other types to f32 in fround.
Restrict fround() to only float, double, signed, unsigned (no unions / intish).
Restrict Bitwise operations to intish, particularly |0, when not applied to a foreign function.

Adding more exhaustive tests of stdlib Math, move to a separate file.
Adding tests of interesting values for the stdlib asm.js functions.

BUG= https://bugs.chromium.org/p/v8/issues/detail?id=4203
TEST=test-asm-validator,asm-wasm
R=titzer@chromium.org,rossberg@chromium.org
LOG=N

Review URL: https://codereview.chromium.org/1804243003

Cr-Commit-Position: refs/heads/master@{#34967}
parent e6f4b749
......@@ -1605,11 +1605,13 @@ Node* WasmGraphBuilder::BuildCFuncInstruction(ExternalReference ref,
}
Node* WasmGraphBuilder::BuildF32SConvertI64(Node* input) {
// TODO(titzer/bradnelson): Check handlng of asm.js case.
return BuildIntToFloatConversionInstruction(
input, ExternalReference::wasm_int64_to_float32(jsgraph()->isolate()),
MachineRepresentation::kWord64, MachineType::Float32());
}
Node* WasmGraphBuilder::BuildF32UConvertI64(Node* input) {
// TODO(titzer/bradnelson): Check handlng of asm.js case.
return BuildIntToFloatConversionInstruction(
input, ExternalReference::wasm_uint64_to_float32(jsgraph()->isolate()),
MachineRepresentation::kWord64, MachineType::Float32());
......
......@@ -934,6 +934,54 @@ void AsmTyper::VisitProperty(Property* expr) {
FAIL(expr, "invalid property access");
}
void AsmTyper::CheckPolymorphicStdlibArguments(
enum StandardMember standard_member, ZoneList<Expression*>* args) {
if (args->length() == 0) {
return;
}
// Handle polymorphic stdlib functions specially.
Expression* arg0 = args->at(0);
Type* arg0_type = arg0->bounds().upper;
switch (standard_member) {
case kMathFround: {
if (!arg0_type->Is(cache_.kAsmFloat) &&
!arg0_type->Is(cache_.kAsmDouble) &&
!arg0_type->Is(cache_.kAsmSigned) &&
!arg0_type->Is(cache_.kAsmUnsigned)) {
FAIL(arg0, "illegal function argument type");
}
break;
}
case kMathCeil:
case kMathFloor:
case kMathSqrt: {
if (!arg0_type->Is(cache_.kAsmFloat) &&
!arg0_type->Is(cache_.kAsmDouble)) {
FAIL(arg0, "illegal function argument type");
}
break;
}
case kMathAbs:
case kMathMin:
case kMathMax: {
if (!arg0_type->Is(cache_.kAsmFloat) &&
!arg0_type->Is(cache_.kAsmDouble) &&
!arg0_type->Is(cache_.kAsmSigned)) {
FAIL(arg0, "illegal function argument type");
}
if (args->length() > 1) {
Type* other = Type::Intersect(args->at(0)->bounds().upper,
args->at(1)->bounds().upper, zone());
if (!other->Is(cache_.kAsmFloat) && !other->Is(cache_.kAsmDouble) &&
!other->Is(cache_.kAsmSigned)) {
FAIL(arg0, "function arguments types don't match");
}
}
break;
}
default: { break; }
}
}
void AsmTyper::VisitCall(Call* expr) {
Type* expected_type = expected_type_;
......@@ -987,29 +1035,7 @@ void AsmTyper::VisitCall(Call* expr) {
result_type = computed_type_;
}
}
// Handle polymorphic stdlib functions specially.
if (standard_member == kMathCeil || standard_member == kMathFloor ||
standard_member == kMathSqrt) {
if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) &&
!args->at(0)->bounds().upper->Is(cache_.kAsmDouble)) {
FAIL(expr, "illegal function argument type");
}
} else if (standard_member == kMathAbs || standard_member == kMathMin ||
standard_member == kMathMax) {
if (!args->at(0)->bounds().upper->Is(cache_.kAsmFloat) &&
!args->at(0)->bounds().upper->Is(cache_.kAsmDouble) &&
!args->at(0)->bounds().upper->Is(cache_.kAsmSigned)) {
FAIL(expr, "illegal function argument type");
}
if (args->length() > 1) {
Type* other = Type::Intersect(args->at(0)->bounds().upper,
args->at(1)->bounds().upper, zone());
if (!other->Is(cache_.kAsmFloat) && !other->Is(cache_.kAsmDouble) &&
!other->Is(cache_.kAsmSigned)) {
FAIL(expr, "function arguments types don't match");
}
}
}
RECURSE(CheckPolymorphicStdlibArguments(standard_member, args));
intish_ = 0;
IntersectResult(expr, result_type);
}
......@@ -1156,13 +1182,16 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) {
FAIL(expr, "illegal logical operator");
case Token::BIT_OR: {
// BIT_OR allows Any since it is used as a type coercion.
VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ,
cache_.kAsmSigned, true);
RECURSE(VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ,
cache_.kAsmSigned, true));
if (expr->left()->IsCall() && expr->op() == Token::BIT_OR &&
Type::Number()->Is(expr->left()->bounds().upper)) {
// Force the return types of foreign functions.
expr->left()->set_bounds(Bounds(cache_.kAsmSigned));
}
if (in_function_ && !expr->left()->bounds().upper->Is(cache_.kAsmIntQ)) {
FAIL(expr->left(), "intish required");
}
return;
}
case Token::BIT_XOR: {
......@@ -1180,20 +1209,20 @@ void AsmTyper::VisitBinaryOperation(BinaryOperation* expr) {
}
}
// BIT_XOR allows Any since it is used as a type coercion (via ~~).
VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ,
cache_.kAsmSigned, true);
RECURSE(VisitIntegerBitwiseOperator(expr, Type::Any(), cache_.kAsmIntQ,
cache_.kAsmSigned, true));
return;
}
case Token::SHR: {
VisitIntegerBitwiseOperator(expr, cache_.kAsmIntQ, cache_.kAsmIntQ,
cache_.kAsmUnsigned, false);
RECURSE(VisitIntegerBitwiseOperator(
expr, cache_.kAsmIntQ, cache_.kAsmIntQ, cache_.kAsmUnsigned, false));
return;
}
case Token::SHL:
case Token::SAR:
case Token::BIT_AND: {
VisitIntegerBitwiseOperator(expr, cache_.kAsmIntQ, cache_.kAsmIntQ,
cache_.kAsmSigned, false);
RECURSE(VisitIntegerBitwiseOperator(
expr, cache_.kAsmIntQ, cache_.kAsmIntQ, cache_.kAsmSigned, false));
return;
}
case Token::ADD:
......
......@@ -135,6 +135,9 @@ class AsmTyper : public AstVisitor {
void VisitHeapAccess(Property* expr, bool assigning, Type* assignment_type);
void CheckPolymorphicStdlibArguments(enum StandardMember standard_member,
ZoneList<Expression*>* args);
Expression* GetReceiverOfPropertyAccess(Expression* expr, const char* name);
bool IsMathObject(Expression* expr);
bool IsSIMDObject(Expression* expr);
......
......@@ -997,6 +997,22 @@ class AsmWasmBuilderImpl : public AstVisitor {
return true;
}
}
switch (TypeIndexOf(args->at(0))) {
case kInt32:
case kFixnum:
current_function_builder_->Emit(kExprF32SConvertI32);
break;
case kUint32:
current_function_builder_->Emit(kExprF32UConvertI32);
break;
case kFloat32:
break;
case kFloat64:
current_function_builder_->Emit(kExprF32ConvertF64);
break;
default:
UNREACHABLE();
}
break;
}
default: {
......
......@@ -1153,7 +1153,7 @@ TEST(TernaryMismatchIntish) {
TEST(TernaryMismatchInt32Float32) {
CHECK_FUNC_ERROR(
"function bar() { var x = 1; var y = 2; return (x?fround(y):x)|0; }\n"
"function bar() { var x = 1; var y = 2.0; return (x?fround(y):x)|0; }\n"
"function foo() { bar(); }",
"asm: line 1: then and else expressions in ? must have the same type\n");
}
......@@ -1173,9 +1173,16 @@ TEST(BadIntishMultiply) {
"asm: line 1: intish not allowed in multiply\n");
}
TEST(FroundFloat32) {
CHECK_FUNC_TYPES_BEGIN(
TEST(IntToFloat32) {
CHECK_FUNC_ERROR(
"function bar() { var x = 1; return fround(x); }\n"
"function foo() { bar(); }",
"asm: line 1: illegal function argument type\n");
}
TEST(Int32ToFloat32) {
CHECK_FUNC_TYPES_BEGIN(
"function bar() { var x = 1; return fround(x|0); }\n"
"function foo() { bar(); }") {
CHECK_EXPR(FunctionLiteral, FUNC_F_TYPE) {
CHECK_EXPR(Assignment, Bounds(cache.kAsmInt)) {
......@@ -1184,7 +1191,32 @@ TEST(FroundFloat32) {
}
CHECK_EXPR(Call, Bounds(cache.kAsmFloat)) {
CHECK_VAR(fround, FUNC_N2F_TYPE);
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
}
}
CHECK_SKIP();
}
CHECK_FUNC_TYPES_END
}
TEST(Uint32ToFloat32) {
CHECK_FUNC_TYPES_BEGIN(
"function bar() { var x = 1; return fround(x>>>0); }\n"
"function foo() { bar(); }") {
CHECK_EXPR(FunctionLiteral, FUNC_F_TYPE) {
CHECK_EXPR(Assignment, Bounds(cache.kAsmInt)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
CHECK_EXPR(Call, Bounds(cache.kAsmFloat)) {
CHECK_VAR(fround, FUNC_N2F_TYPE);
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmUnsigned)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
}
}
CHECK_SKIP();
......@@ -1192,6 +1224,55 @@ TEST(FroundFloat32) {
CHECK_FUNC_TYPES_END
}
TEST(Float64ToFloat32) {
CHECK_FUNC_TYPES_BEGIN(
"function bar() { var x = 1.0; return fround(x); }\n"
"function foo() { bar(); }") {
CHECK_EXPR(FunctionLiteral, FUNC_F_TYPE) {
CHECK_EXPR(Assignment, Bounds(cache.kAsmDouble)) {
CHECK_VAR(x, Bounds(cache.kAsmDouble));
CHECK_EXPR(Literal, Bounds(cache.kAsmDouble));
}
CHECK_EXPR(Call, Bounds(cache.kAsmFloat)) {
CHECK_VAR(fround, FUNC_N2F_TYPE);
CHECK_VAR(x, Bounds(cache.kAsmDouble));
}
}
CHECK_SKIP();
}
CHECK_FUNC_TYPES_END
}
TEST(Int32ToFloat32ToInt32) {
CHECK_FUNC_TYPES_BEGIN(
"function bar() { var x = 1; return ~~fround(x|0) | 0; }\n"
"function foo() { bar(); }") {
CHECK_EXPR(FunctionLiteral, FUNC_I_TYPE) {
CHECK_EXPR(Assignment, Bounds(cache.kAsmInt)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_EXPR(Call, Bounds(cache.kAsmFloat)) {
CHECK_VAR(fround, FUNC_N2F_TYPE);
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
}
CHECK_EXPR(Literal, Bounds(cache.kAsmSigned));
}
CHECK_EXPR(Literal, Bounds(cache.kAsmSigned));
}
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
}
CHECK_SKIP();
}
CHECK_FUNC_TYPES_END
}
TEST(Addition4) {
CHECK_FUNC_TYPES_BEGIN(
......@@ -1276,7 +1357,7 @@ TEST(CompareMismatchInt32Uint32) {
TEST(CompareMismatchInt32Float32) {
CHECK_FUNC_ERROR(
"function bar() { var x = 1; var y = 2; return (x < fround(y))|0; }\n"
"function bar() { var x = 1; var y = 2.0; return (x < fround(y))|0; }\n"
"function foo() { bar(); }",
"asm: line 1: left and right side of comparison must match\n");
}
......@@ -1754,6 +1835,12 @@ TEST(LogicalOrOperator) {
"asm: line 1: illegal logical operator\n");
}
TEST(BitOrDouble) {
CHECK_FUNC_ERROR(
"function bar() { var x = 1.0; return x | 0; }\n"
"function foo() { bar(); }",
"asm: line 1: intish required\n");
}
TEST(BadLiteral) {
CHECK_FUNC_ERROR(
......
......@@ -269,6 +269,8 @@
# TODO(titzer): correct WASM adapter frame alignment on arm64
'wasm/*': [PASS, ['arch == arm64', SKIP]],
'wasm/asm-wasm': [PASS, ['arch in [arm, arm64, mips, mipsel, mips64, mips64el]', SKIP]],
# TODO(branelson): Figure out why ignition + asm-wasm-stdlib fails.
'wasm/asm-wasm-stdlib': [PASS, ['arch in [arm, arm64, mips, mipsel, mips64, mips64el] or ignition == True', SKIP]],
# TODO(branelson): Figure out why ignition + asm->wasm fails embenchen.
'wasm/embenchen/*': [PASS, ['ignition == True', SKIP]],
......
This diff is collapsed.
......@@ -1256,196 +1256,10 @@ TestForeignVariables();
})();
(function TestStdlibConstants() {
function Module(stdlib) {
"use asm";
var StdlibInfinity = stdlib.Infinity;
var StdlibNaN = stdlib.NaN;
var StdlibMathE = stdlib.Math.E;
var StdlibMathLN10 = stdlib.Math.LN10;
var StdlibMathLN2 = stdlib.Math.LN2;
var StdlibMathLOG2E = stdlib.Math.LOG2E;
var StdlibMathLOG10E = stdlib.Math.LOG10E;
var StdlibMathPI = stdlib.Math.PI;
var StdlibMathSQRT1_2 = stdlib.Math.SQRT1_2;
var StdlibMathSQRT2 = stdlib.Math.SQRT2;
function caller() {
if (StdlibInfinity != 1.0 / 0.0) return 0;
if (StdlibMathE != 2.718281828459045) return 0;
if (StdlibMathLN10 != 2.302585092994046) return 0;
if (StdlibMathLN2 != 0.6931471805599453) return 0;
if (StdlibMathLOG2E != 1.4426950408889634) return 0;
if (StdlibMathLOG10E != 0.4342944819032518) return 0;
if (StdlibMathPI != 3.141592653589793) return 0;
if (StdlibMathSQRT1_2 != 0.7071067811865476) return 0;
if (StdlibMathSQRT2 != 1.4142135623730951) return 0;
return 1;
}
function nanCheck() {
return +StdlibNaN;
}
return {caller:caller, nanCheck:nanCheck};
}
var m =Wasm.instantiateModuleFromAsm(Module.toString());
assertEquals(1, m.caller());
assertTrue(isNaN(m.nanCheck()));
})();
(function TestStdlibFunctions() {
function Module(stdlib) {
"use asm";
var StdlibMathCeil = stdlib.Math.ceil;
var StdlibMathFloor = stdlib.Math.floor;
var StdlibMathSqrt = stdlib.Math.sqrt;
var StdlibMathAbs = stdlib.Math.abs;
var StdlibMathMin = stdlib.Math.min;
var StdlibMathMax = stdlib.Math.max;
var StdlibMathAcos = stdlib.Math.acos;
var StdlibMathAsin = stdlib.Math.asin;
var StdlibMathAtan = stdlib.Math.atan;
var StdlibMathCos = stdlib.Math.cos;
var StdlibMathSin = stdlib.Math.sin;
var StdlibMathTan = stdlib.Math.tan;
var StdlibMathExp = stdlib.Math.exp;
var StdlibMathLog = stdlib.Math.log;
var StdlibMathAtan2 = stdlib.Math.atan2;
var StdlibMathPow = stdlib.Math.pow;
var StdlibMathImul = stdlib.Math.imul;
var fround = stdlib.Math.fround;
function deltaEqual(x, y) {
x = +x;
y = +y;
var t = 0.0;
t = x - y;
if (t < 0.0) {
t = t * -1.0;
}
return (t < 1.0e-13) | 0;
}
function caller() {
if (!deltaEqual(StdlibMathSqrt(123.0), 11.090536506409418)) return 0;
if (StdlibMathSqrt(fround(256.0)) != fround(16.0)) return 0;
if (StdlibMathCeil(123.7) != 124.0) return 0;
if (StdlibMathCeil(fround(123.7)) != fround(124.0)) return 0;
if (StdlibMathFloor(123.7) != 123.0) return 0;
if (StdlibMathFloor(fround(123.7)) != fround(123.0)) return 0;
if (StdlibMathAbs(-123.0) != 123.0) return 0;
if (StdlibMathAbs(fround(-123.0)) != fround(123.0)) return 0;
if (StdlibMathMin(123.4, 1236.4) != 123.4) return 0;
if (StdlibMathMin(fround(123.4),
fround(1236.4)) != fround(123.4)) return 0;
if (StdlibMathMax(123.4, 1236.4) != 1236.4) return 0;
if (StdlibMathMax(fround(123.4), fround(1236.4))
!= fround(1236.4)) return 0;
if (!deltaEqual(StdlibMathAcos(0.1), 1.4706289056333368)) return 0;
if (!deltaEqual(StdlibMathAsin(0.2), 0.2013579207903308)) return 0;
if (!deltaEqual(StdlibMathAtan(0.2), 0.19739555984988078)) return 0;
if (!deltaEqual(StdlibMathCos(0.2), 0.9800665778412416)) return 0;
if (!deltaEqual(StdlibMathSin(0.2), 0.19866933079506122)) return 0;
if (!deltaEqual(StdlibMathTan(0.2), 0.20271003550867250)) return 0;
if (!deltaEqual(StdlibMathExp(0.2), 1.2214027581601699)) return 0;
if (!deltaEqual(StdlibMathLog(0.2), -1.6094379124341003)) return 0;
if (StdlibMathImul(6, 7) != 42) return 0;
if (!deltaEqual(StdlibMathAtan2(6.0, 7.0), 0.7086262721276703)) return 0;
if (StdlibMathPow(6.0, 7.0) != 279936.0) return 0;
return 1;
}
return {caller:caller};
}
var m = Wasm.instantiateModuleFromAsm(Module.toString());
assertEquals(1, m.caller());
})();
(function TestAbsInt() {
function Module(stdlib) {
"use asm";
var abs = stdlib.Math.abs;
function func(x) {
x = x | 0;
return abs(x|0)|0;
}
return {func:func};
}
var m = Wasm.instantiateModuleFromAsm(Module.toString());
var values = [0, 1, -1, 0x40000000, 0x7FFFFFFF, -0x80000000];
for (var i = 0; i < values.length; i++) {
var val = values[i];
assertEquals(Math.abs(val) | 0, m.func(val));
}
})();
(function TestAbsFloat() {
function Module(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var abs = stdlib.Math.abs;
function func(x) {
x = fround(x);
x = abs(x);
return fround(x);
}
return {func:func};
}
var m = Wasm.instantiateModuleFromAsm(Module.toString());
var values = [
0, -0, 1, -1, 0.9, -0.9, 1.414, 0x7F, -0x80, -0x8000, -0x80000000,
0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN
];
for (var i = 0; i < values.length; i++) {
var val = values[i];
assertEquals(Math.fround(Math.abs(val)), m.func(val));
}
})();
(function TestAbsDouble() {
function Module(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var abs = stdlib.Math.abs;
function func(x) {
x = +x;
x = abs(x);
return +x;
}
return {func:func};
}
var m = Wasm.instantiateModuleFromAsm(Module.toString());
var values = [
0, -0, 1, -1, 0.9, -0.9, 1.414, 0x7F, -0x80, -0x8000, -0x80000000,
0x7FFF, 0x7FFFFFFF, Infinity, -Infinity, NaN
];
for (var i = 0; i < values.length; i++) {
var val = values[i];
assertEquals(Math.abs(val), m.func(val));
}
})();
(function TestFloatAsDouble() {
function Module(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var abs = stdlib.Math.abs;
function func() {
var x = fround(1.0);
return +fround(x);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment