Make fast_exp take an Isolate* paramter

We still share the code globally, but if we wanted, it would be easy to
make it per isolate now

BUG=v8:2487
R=yangguo@chromium.org,jkummerow@chromium.org
LOG=n

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

Cr-Commit-Position: refs/heads/master@{#32217}
This commit is contained in:
jochen 2015-11-24 07:34:16 -08:00 committed by Commit bot
parent 062586dbf9
commit 0fb2edd15d
12 changed files with 79 additions and 70 deletions

View File

@ -18,23 +18,22 @@ namespace internal {
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
byte* fast_exp_arm_machine_code = NULL; byte* fast_exp_arm_machine_code = nullptr;
double fast_exp_simulator(double x) { double fast_exp_simulator(double x, Isolate* isolate) {
return Simulator::current(Isolate::Current())->CallFPReturnsDouble( return Simulator::current(isolate)
fast_exp_arm_machine_code, x, 0); ->CallFPReturnsDouble(fast_exp_arm_machine_code, x, 0);
} }
#endif #endif
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
{ {
DwVfpRegister input = d0; DwVfpRegister input = d0;
@ -67,11 +66,11 @@ UnaryMathFunction CreateExpFunction() {
masm.GetCode(&desc); masm.GetCode(&desc);
DCHECK(!RelocInfo::RequiresRelocation(desc)); DCHECK(!RelocInfo::RequiresRelocation(desc));
Assembler::FlushICacheWithoutIsolate(buffer, actual_size); Assembler::FlushICache(isolate, buffer, actual_size);
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
#if !defined(USE_SIMULATOR) #if !defined(USE_SIMULATOR)
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
#else #else
fast_exp_arm_machine_code = buffer; fast_exp_arm_machine_code = buffer;
return &fast_exp_simulator; return &fast_exp_simulator;

View File

@ -16,9 +16,9 @@ namespace internal {
#define __ ACCESS_MASM(masm) #define __ ACCESS_MASM(masm)
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
byte* fast_exp_arm64_machine_code = NULL; byte* fast_exp_arm64_machine_code = nullptr;
double fast_exp_simulator(double x) { double fast_exp_simulator(double x, Isolate* isolate) {
Simulator * simulator = Simulator::current(Isolate::Current()); Simulator * simulator = Simulator::current(isolate);
Simulator::CallArgument args[] = { Simulator::CallArgument args[] = {
Simulator::CallArgument(x), Simulator::CallArgument(x),
Simulator::CallArgument::End() Simulator::CallArgument::End()
@ -28,19 +28,17 @@ double fast_exp_simulator(double x) {
#endif #endif
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
// Use the Math.exp implemetation in MathExpGenerator::EmitMathExp() to create // Use the Math.exp implemetation in MathExpGenerator::EmitMathExp() to create
// an AAPCS64-compliant exp() function. This will be faster than the C // an AAPCS64-compliant exp() function. This will be faster than the C
// library's exp() function, but probably less accurate. // library's exp() function, but probably less accurate.
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
masm.SetStackPointer(csp); masm.SetStackPointer(csp);
// The argument will be in d0 on entry. // The argument will be in d0 on entry.
@ -64,11 +62,11 @@ UnaryMathFunction CreateExpFunction() {
masm.GetCode(&desc); masm.GetCode(&desc);
DCHECK(!RelocInfo::RequiresRelocation(desc)); DCHECK(!RelocInfo::RequiresRelocation(desc));
Assembler::FlushICacheWithoutIsolate(buffer, actual_size); Assembler::FlushICache(isolate, buffer, actual_size);
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
#if !defined(USE_SIMULATOR) #if !defined(USE_SIMULATOR)
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
#else #else
fast_exp_arm64_machine_code = buffer; fast_exp_arm64_machine_code = buffer;
return &fast_exp_simulator; return &fast_exp_simulator;

View File

@ -73,15 +73,32 @@ double fast_##name(double x) { \
return (*fast_##name##_function)(x); \ return (*fast_##name##_function)(x); \
} }
UNARY_MATH_FUNCTION(exp, CreateExpFunction())
UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction()) UNARY_MATH_FUNCTION(sqrt, CreateSqrtFunction())
#undef UNARY_MATH_FUNCTION #undef UNARY_MATH_FUNCTION
static UnaryMathFunctionWithIsolate fast_exp_function = NULL;
void lazily_initialize_fast_exp() {
if (fast_exp_function == NULL) { double std_exp(double x, Isolate* isolate) {
init_fast_exp_function(); return std::exp(x);
}
void init_fast_exp_function(Isolate* isolate) {
if (FLAG_fast_math) fast_exp_function = CreateExpFunction(isolate);
if (!fast_exp_function) fast_exp_function = std_exp;
}
double fast_exp(double x, Isolate* isolate) {
return (*fast_exp_function)(x, isolate);
}
void lazily_initialize_fast_exp(Isolate* isolate) {
if (fast_exp_function == nullptr) {
init_fast_exp_function(isolate);
} }
} }

View File

@ -90,20 +90,21 @@ class CodeGenerator {
// from the one we use in our generated code. Therefore we use the same // from the one we use in our generated code. Therefore we use the same
// generated code both in runtime and compiled code. // generated code both in runtime and compiled code.
typedef double (*UnaryMathFunction)(double x); typedef double (*UnaryMathFunction)(double x);
typedef double (*UnaryMathFunctionWithIsolate)(double x, Isolate* isolate);
UnaryMathFunction CreateExpFunction(); UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate);
UnaryMathFunction CreateSqrtFunction(); UnaryMathFunction CreateSqrtFunction();
double modulo(double x, double y); double modulo(double x, double y);
// Custom implementation of math functions. // Custom implementation of math functions.
double fast_exp(double input); double fast_exp(double input, Isolate* isolate);
double fast_sqrt(double input); double fast_sqrt(double input);
#ifdef _WIN64 #ifdef _WIN64
void init_modulo_function(); void init_modulo_function();
#endif #endif
void lazily_initialize_fast_exp(); void lazily_initialize_fast_exp(Isolate* isolate);
void init_fast_sqrt_function(); void init_fast_sqrt_function();

View File

@ -4080,7 +4080,8 @@ HInstruction* HUnaryMathOperation::New(Isolate* isolate, Zone* zone,
} }
switch (op) { switch (op) {
case kMathExp: case kMathExp:
return H_CONSTANT_DOUBLE(fast_exp(d)); lazily_initialize_fast_exp(isolate);
return H_CONSTANT_DOUBLE(fast_exp(d, isolate));
case kMathLog: case kMathLog:
return H_CONSTANT_DOUBLE(std::log(d)); return H_CONSTANT_DOUBLE(std::log(d));
case kMathSqrt: case kMathSqrt:

View File

@ -34,15 +34,14 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
#define __ masm. #define __ masm.
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
// esp[1 * kPointerSize]: raw double input // esp[1 * kPointerSize]: raw double input
// esp[0 * kPointerSize]: return address // esp[0 * kPointerSize]: return address
{ {
@ -65,9 +64,9 @@ UnaryMathFunction CreateExpFunction() {
masm.GetCode(&desc); masm.GetCode(&desc);
DCHECK(!RelocInfo::RequiresRelocation(desc)); DCHECK(!RelocInfo::RequiresRelocation(desc));
Assembler::FlushICacheWithoutIsolate(buffer, actual_size); Assembler::FlushICache(isolate, buffer, actual_size);
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
} }

View File

@ -18,23 +18,21 @@ namespace internal {
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
byte* fast_exp_mips_machine_code = NULL; byte* fast_exp_mips_machine_code = nullptr;
double fast_exp_simulator(double x) { double fast_exp_simulator(double x, Isolate* isolate) {
return Simulator::current(Isolate::Current())->CallFP( return Simulator::current(isolate)->CallFP(fast_exp_mips_machine_code, x, 0);
fast_exp_mips_machine_code, x, 0);
} }
#endif #endif
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
{ {
DoubleRegister input = f12; DoubleRegister input = f12;
@ -63,7 +61,7 @@ UnaryMathFunction CreateExpFunction() {
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
#if !defined(USE_SIMULATOR) #if !defined(USE_SIMULATOR)
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
#else #else
fast_exp_mips_machine_code = buffer; fast_exp_mips_machine_code = buffer;
return &fast_exp_simulator; return &fast_exp_simulator;

View File

@ -18,23 +18,21 @@ namespace internal {
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
byte* fast_exp_mips_machine_code = NULL; byte* fast_exp_mips_machine_code = nullptr;
double fast_exp_simulator(double x) { double fast_exp_simulator(double x, Isolate* isolate) {
return Simulator::current(Isolate::Current())->CallFP( return Simulator::current(isolate)->CallFP(fast_exp_mips_machine_code, x, 0);
fast_exp_mips_machine_code, x, 0);
} }
#endif #endif
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
{ {
DoubleRegister input = f12; DoubleRegister input = f12;
@ -63,7 +61,7 @@ UnaryMathFunction CreateExpFunction() {
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
#if !defined(USE_SIMULATOR) #if !defined(USE_SIMULATOR)
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
#else #else
fast_exp_mips_machine_code = buffer; fast_exp_mips_machine_code = buffer;
return &fast_exp_simulator; return &fast_exp_simulator;

View File

@ -18,23 +18,22 @@ namespace internal {
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
byte* fast_exp_ppc_machine_code = NULL; byte* fast_exp_ppc_machine_code = nullptr;
double fast_exp_simulator(double x) { double fast_exp_simulator(double x, Isolate* isolate) {
return Simulator::current(Isolate::Current()) return Simulator::current(isolate)
->CallFPReturnsDouble(fast_exp_ppc_machine_code, x, 0); ->CallFPReturnsDouble(fast_exp_ppc_machine_code, x, 0);
} }
#endif #endif
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
{ {
DoubleRegister input = d1; DoubleRegister input = d1;
@ -62,11 +61,11 @@ UnaryMathFunction CreateExpFunction() {
DCHECK(!RelocInfo::RequiresRelocation(desc)); DCHECK(!RelocInfo::RequiresRelocation(desc));
#endif #endif
Assembler::FlushICacheWithoutIsolate(buffer, actual_size); Assembler::FlushICache(isolate, buffer, actual_size);
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
#if !defined(USE_SIMULATOR) #if !defined(USE_SIMULATOR)
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
#else #else
fast_exp_ppc_machine_code = buffer; fast_exp_ppc_machine_code = buffer;
return &fast_exp_simulator; return &fast_exp_simulator;

View File

@ -107,8 +107,8 @@ RUNTIME_FUNCTION(Runtime_MathExpRT) {
isolate->counters()->math_exp()->Increment(); isolate->counters()->math_exp()->Increment();
CONVERT_DOUBLE_ARG_CHECKED(x, 0); CONVERT_DOUBLE_ARG_CHECKED(x, 0);
lazily_initialize_fast_exp(); lazily_initialize_fast_exp(isolate);
return *isolate->factory()->NewNumber(fast_exp(x)); return *isolate->factory()->NewNumber(fast_exp(x, isolate));
} }

View File

@ -32,15 +32,14 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
#define __ masm. #define __ masm.
UnaryMathFunction CreateExpFunction() { UnaryMathFunctionWithIsolate CreateExpFunction(Isolate* isolate) {
if (!FLAG_fast_math) return &std::exp;
size_t actual_size; size_t actual_size;
byte* buffer = byte* buffer =
static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true)); static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
if (buffer == NULL) return &std::exp; if (buffer == nullptr) return nullptr;
ExternalReference::InitializeMathExpData(); ExternalReference::InitializeMathExpData();
MacroAssembler masm(NULL, buffer, static_cast<int>(actual_size)); MacroAssembler masm(nullptr, buffer, static_cast<int>(actual_size));
// xmm0: raw double input. // xmm0: raw double input.
XMMRegister input = xmm0; XMMRegister input = xmm0;
XMMRegister result = xmm1; XMMRegister result = xmm1;
@ -58,9 +57,9 @@ UnaryMathFunction CreateExpFunction() {
masm.GetCode(&desc); masm.GetCode(&desc);
DCHECK(!RelocInfo::RequiresRelocation(desc)); DCHECK(!RelocInfo::RequiresRelocation(desc));
Assembler::FlushICacheWithoutIsolate(buffer, actual_size); Assembler::FlushICache(isolate, buffer, actual_size);
base::OS::ProtectCode(buffer, actual_size); base::OS::ProtectCode(buffer, actual_size);
return FUNCTION_CAST<UnaryMathFunction>(buffer); return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
} }

View File

@ -36,7 +36,7 @@ void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
UnaryMathFunction CreateExpFunction() { UnaryMathFunction CreateExpFunction() {
// No SSE2 support // No SSE2 support
return &std::exp; return nullptr;
} }