diff --git a/ecmascript/compiler/fast_stub.cpp b/ecmascript/compiler/fast_stub.cpp index 7396daa8b46e91e5c83c9f5335362ed1c579d798..56fa5e9c5446f2b9eaad62dc92bf0819cbc7b42d 100644 --- a/ecmascript/compiler/fast_stub.cpp +++ b/ecmascript/compiler/fast_stub.cpp @@ -25,16 +25,102 @@ namespace panda::ecmascript::kungfu { using namespace panda::ecmascript; -void FastAddStub::GenerateCircuit(const CompilationConfig *cfg) +#ifndef NDEBUG +void PhiGateTestStub::GenerateCircuit(const CompilationConfig *cfg) { Stub::GenerateCircuit(cfg); auto env = GetEnvironment(); - GateRef x = TaggedArgument(0); - GateRef y = TaggedArgument(1); - DEFVARIABLE(intX, MachineType::INT32, 0); - DEFVARIABLE(intY, MachineType::INT32, 0); - DEFVARIABLE(doubleX, MachineType::FLOAT64, 0); - DEFVARIABLE(doubleY, MachineType::FLOAT64, 0); + DEFVARIABLE(z, MachineType::INT32, GetInt32Constant(0)); + DEFVARIABLE(x, MachineType::INT32, Int32Argument(0)); + Label ifTrue(env); + Label ifFalse(env); + Label next(env); + + Branch(Word32Equal(*x, GetInt32Constant(10)), &ifTrue, &ifFalse); // 10 : size of entry + Bind(&ifTrue); + z = Int32Add(*x, GetInt32Constant(10)); // 10 : size of entry + Jump(&next); + Bind(&ifFalse); + z = Int32Add(*x, GetInt32Constant(100)); // 100 : size of entry + Jump(&next); + Bind(&next); + Return(*z); +} + +void LoopTestStub::GenerateCircuit(const CompilationConfig *cfg) +{ + Stub::GenerateCircuit(cfg); + auto env = GetEnvironment(); + DEFVARIABLE(z, MachineType::INT32, GetInt32Constant(0)); + DEFVARIABLE(y, MachineType::INT32, Int32Argument(0)); + Label loopHead(env); + Label loopEnd(env); + Label afterLoop(env); + Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopHead, &afterLoop); // 10 : size of entry + LoopBegin(&loopHead); + Label ifTrue(env); + Label ifFalse(env); + Label next(env); + Branch(Word32Equal(Int32Argument(0), GetInt32Constant(9)), &ifTrue, &ifFalse); // 9 : size of entry + Bind(&ifTrue); + z = Int32Add(*y, GetInt32Constant(10)); // 10 : size of entry + y = Int32Add(*z, GetInt32Constant(1)); + Jump(&next); + Bind(&ifFalse); + z = Int32Add(*y, GetInt32Constant(100)); // 100 : size of entry + Jump(&next); + Bind(&next); + y = Int32Add(*y, GetInt32Constant(1)); + Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopEnd, &afterLoop); // 10 : size of entry + Bind(&loopEnd); + LoopEnd(&loopHead); + Bind(&afterLoop); + Return(*z); +} + +void LoopTest1Stub::GenerateCircuit(const CompilationConfig *cfg) +{ + Stub::GenerateCircuit(cfg); + auto env = GetEnvironment(); + DEFVARIABLE(y, MachineType::INT32, Int32Argument(0)); + DEFVARIABLE(x, MachineType::INT32, Int32Argument(0)); + DEFVARIABLE(z, MachineType::INT32, Int32Argument(0)); + Label loopHead(env); + Label loopEnd(env); + Label afterLoop(env); + Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopHead, &afterLoop); // 10 : size of entry + LoopBegin(&loopHead); + x = Int32Add(*z, GetInt32Constant(3)); // 3 : size of entry + Label ifTrue(env); + Label next(env); + Branch(Word32Equal(*x, GetInt32Constant(9)), &ifTrue, &next); // 9 : size of entry + Bind(&ifTrue); + y = Int32Add(*z, *x); + Jump(&next); + Bind(&next); + y = Int32Add(*y, GetInt32Constant(1)); + Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopEnd, &afterLoop); // 10 : size of entry + Bind(&loopEnd); + LoopEnd(&loopHead); + Bind(&afterLoop); + z = *y; + Return(*z); +} + +void FastMulGCTestStub::GenerateCircuit(const CompilationConfig *cfg) +{ + Stub::GenerateCircuit(cfg); + auto env = GetEnvironment(); + env->GetCircuit()->SetFrameType(FrameType::OPTIMIZED_ENTRY_FRAME); + GateRef glue = PtrArgument(0); + GateRef x = Int64Argument(1); + GateRef y = Int64Argument(2); // 2: 3rd argument + + DEFVARIABLE(intX, MachineType::INT64, GetWord64Constant(0)); + DEFVARIABLE(intY, MachineType::INT64, GetWord64Constant(0)); + DEFVARIABLE(valuePtr, MachineType::INT64, GetWord64Constant(0)); + DEFVARIABLE(doubleX, MachineType::FLOAT64, GetDoubleConstant(0)); + DEFVARIABLE(doubleY, MachineType::FLOAT64, GetDoubleConstant(0)); Label xIsNumber(env); Label xNotNumberOryNotNumber(env); Label xIsNumberAndyIsNumber(env); @@ -52,8 +138,8 @@ void FastAddStub::GenerateCircuit(const CompilationConfig *cfg) Branch(TaggedIsInt(x), &xIsInt, &xNotInt); Bind(&xIsInt); { - intX = TaggedCastToInt32(x); - doubleX = ChangeInt32ToFloat64(*intX); + intX = TaggedCastToInt64(x); + doubleX = CastInt64ToFloat64(*intX); Jump(&xIsNumberAndyIsNumber); } Bind(&xNotInt); @@ -72,8 +158,8 @@ void FastAddStub::GenerateCircuit(const CompilationConfig *cfg) Branch(TaggedIsInt(y), &yIsInt, &yNotInt); Bind(&yIsInt); { - intY = TaggedCastToInt32(y); - doubleY = ChangeInt32ToFloat64(*intY); + intY = TaggedCastToInt64(y); + doubleY = CastInt64ToFloat64(*intY); Jump(&xIsDoubleAndyIsDouble); } Bind(&yNotInt); @@ -83,25 +169,32 @@ void FastAddStub::GenerateCircuit(const CompilationConfig *cfg) } } Bind(&xIsDoubleAndyIsDouble); - doubleX = DoubleAdd(*doubleX, *doubleY); + doubleX = DoubleMul(*doubleX, *doubleY); + StubDescriptor *getTaggedArrayPtr = GET_STUBDESCRIPTOR(GetTaggedArrayPtrTest); + GateRef ptr1 = CallRuntime(getTaggedArrayPtr, glue, GetWord64Constant(FAST_STUB_ID(GetTaggedArrayPtrTest)), { + glue + }); + GateRef ptr2 = CallRuntime(getTaggedArrayPtr, glue, GetWord64Constant(FAST_STUB_ID(GetTaggedArrayPtrTest)), { + glue + }); + (void)ptr2; + auto value = Load(MachineType::UINT64, ptr1); + GateRef value2 = CastInt64ToFloat64(value); + doubleX = DoubleMul(*doubleX, value2); Return(DoubleBuildTaggedWithNoGC(*doubleX)); } +#endif -#ifndef NDEBUG -void FastMulGCTestStub::GenerateCircuit(const CompilationConfig *cfg) +void FastAddStub::GenerateCircuit(const CompilationConfig *cfg) { Stub::GenerateCircuit(cfg); auto env = GetEnvironment(); - env->GetCircuit()->SetFrameType(FrameType::OPTIMIZED_ENTRY_FRAME); - GateRef glue = PtrArgument(0); - GateRef x = Int64Argument(1); - GateRef y = Int64Argument(2); // 2: 3rd argument - - DEFVARIABLE(intX, MachineType::INT64, GetWord64Constant(0)); - DEFVARIABLE(intY, MachineType::INT64, GetWord64Constant(0)); - DEFVARIABLE(valuePtr, MachineType::INT64, GetWord64Constant(0)); - DEFVARIABLE(doubleX, MachineType::FLOAT64, GetDoubleConstant(0)); - DEFVARIABLE(doubleY, MachineType::FLOAT64, GetDoubleConstant(0)); + GateRef x = TaggedArgument(0); + GateRef y = TaggedArgument(1); + DEFVARIABLE(intX, MachineType::INT32, 0); + DEFVARIABLE(intY, MachineType::INT32, 0); + DEFVARIABLE(doubleX, MachineType::FLOAT64, 0); + DEFVARIABLE(doubleY, MachineType::FLOAT64, 0); Label xIsNumber(env); Label xNotNumberOryNotNumber(env); Label xIsNumberAndyIsNumber(env); @@ -119,8 +212,8 @@ void FastMulGCTestStub::GenerateCircuit(const CompilationConfig *cfg) Branch(TaggedIsInt(x), &xIsInt, &xNotInt); Bind(&xIsInt); { - intX = TaggedCastToInt64(x); - doubleX = CastInt64ToFloat64(*intX); + intX = TaggedCastToInt32(x); + doubleX = ChangeInt32ToFloat64(*intX); Jump(&xIsNumberAndyIsNumber); } Bind(&xNotInt); @@ -139,8 +232,8 @@ void FastMulGCTestStub::GenerateCircuit(const CompilationConfig *cfg) Branch(TaggedIsInt(y), &yIsInt, &yNotInt); Bind(&yIsInt); { - intY = TaggedCastToInt64(y); - doubleY = CastInt64ToFloat64(*intY); + intY = TaggedCastToInt32(y); + doubleY = ChangeInt32ToFloat64(*intY); Jump(&xIsDoubleAndyIsDouble); } Bind(&yNotInt); @@ -150,21 +243,9 @@ void FastMulGCTestStub::GenerateCircuit(const CompilationConfig *cfg) } } Bind(&xIsDoubleAndyIsDouble); - doubleX = DoubleMul(*doubleX, *doubleY); - StubDescriptor *getTaggedArrayPtr = GET_STUBDESCRIPTOR(GetTaggedArrayPtrTest); - GateRef ptr1 = CallRuntime(getTaggedArrayPtr, glue, GetWord64Constant(FAST_STUB_ID(GetTaggedArrayPtrTest)), { - glue - }); - GateRef ptr2 = CallRuntime(getTaggedArrayPtr, glue, GetWord64Constant(FAST_STUB_ID(GetTaggedArrayPtrTest)), { - glue - }); - (void)ptr2; - auto value = Load(MachineType::UINT64, ptr1); - GateRef value2 = CastInt64ToFloat64(value); - doubleX = DoubleMul(*doubleX, value2); + doubleX = DoubleAdd(*doubleX, *doubleY); Return(DoubleBuildTaggedWithNoGC(*doubleX)); } -#endif void FastSubStub::GenerateCircuit(const CompilationConfig *cfg) { diff --git a/ecmascript/compiler/fast_stub.h b/ecmascript/compiler/fast_stub.h index 68c13a824a358a0b92c1f5e81835e09654b706ed..8e8b3e10e7ccaa6040fd5de02d1a888a4f506665 100644 --- a/ecmascript/compiler/fast_stub.h +++ b/ecmascript/compiler/fast_stub.h @@ -20,6 +20,45 @@ #include "ecmascript/compiler/stub-inl.h" namespace panda::ecmascript::kungfu { +#ifndef NDEBUG +class PhiGateTestStub : public Stub { +public: + explicit PhiGateTestStub(Circuit *circuit) : Stub("Phi", 1, circuit) {} + ~PhiGateTestStub() = default; + NO_MOVE_SEMANTIC(PhiGateTestStub); + NO_COPY_SEMANTIC(PhiGateTestStub); + void GenerateCircuit(const CompilationConfig *cfg) override; +}; + +class LoopTestStub : public Stub { +public: + explicit LoopTestStub(Circuit *circuit) : Stub("LoopTest", 1, circuit) {} + ~LoopTestStub() = default; + NO_MOVE_SEMANTIC(LoopTestStub); + NO_COPY_SEMANTIC(LoopTestStub); + void GenerateCircuit(const CompilationConfig *cfg) override; +}; + +class LoopTest1Stub : public Stub { +public: + explicit LoopTest1Stub(Circuit *circuit) : Stub("LoopTest1", 1, circuit) {} + ~LoopTest1Stub() = default; + NO_MOVE_SEMANTIC(LoopTest1Stub); + NO_COPY_SEMANTIC(LoopTest1Stub); + void GenerateCircuit(const CompilationConfig *cfg) override; +}; + +class FastMulGCTestStub : public Stub { +public: + // 3 : 3 means argument counts + explicit FastMulGCTestStub(Circuit *circuit) : Stub("FastMulGCTest", 3, circuit) {} + ~FastMulGCTestStub() = default; + NO_MOVE_SEMANTIC(FastMulGCTestStub); + NO_COPY_SEMANTIC(FastMulGCTestStub); + void GenerateCircuit(const CompilationConfig *cfg) override; +}; +#endif + class FastAddStub : public Stub { public: // 2 : 2 means argument counts @@ -53,16 +92,6 @@ public: void GenerateCircuit(const CompilationConfig *cfg) override; }; -class FastMulGCTestStub : public Stub { -public: - // 3 : 3 means argument counts - explicit FastMulGCTestStub(Circuit *circuit) : Stub("FastMulGCTest", 3, circuit) {} - ~FastMulGCTestStub() = default; - NO_MOVE_SEMANTIC(FastMulGCTestStub); - NO_COPY_SEMANTIC(FastMulGCTestStub); - void GenerateCircuit(const CompilationConfig *cfg) override; -}; - class FastDivStub : public Stub { public: // 2 : 2 means argument counts diff --git a/ecmascript/compiler/tests/stub_tests.cpp b/ecmascript/compiler/tests/stub_tests.cpp index bd1e513a16c92e7f07e06ed8c02e033d76a05ba8..12a4b6d1be2183c8b25ddd7bfca2fbe75dbffc04 100644 --- a/ecmascript/compiler/tests/stub_tests.cpp +++ b/ecmascript/compiler/tests/stub_tests.cpp @@ -79,40 +79,13 @@ public: LLVMStubModule stubModule {"fast_stub", "x86_64-unknown-linux-gnu"}; }; -class PhiStub : public Stub { -public: - explicit PhiStub(Circuit *circuit) : Stub("Phi", 1, circuit) {} - ~PhiStub() = default; - NO_MOVE_SEMANTIC(PhiStub); - NO_COPY_SEMANTIC(PhiStub); - void GenerateCircuit(const CompilationConfig *cfg) override - { - Stub::GenerateCircuit(cfg); - auto env = GetEnvironment(); - DEFVARIABLE(z, MachineType::INT32, GetInt32Constant(0)); - DEFVARIABLE(x, MachineType::INT32, Int32Argument(0)); - Label ifTrue(env); - Label ifFalse(env); - Label next(env); - - Branch(Word32Equal(*x, GetInt32Constant(10)), &ifTrue, &ifFalse); // 10 : size of entry - Bind(&ifTrue); - z = Int32Add(*x, GetInt32Constant(10)); // 10 : size of entry - Jump(&next); - Bind(&ifFalse); - z = Int32Add(*x, GetInt32Constant(100)); // 100 : size of entry - Jump(&next); - Bind(&next); - Return(*z); - } -}; - +#ifndef NDEBUG HWTEST_F_L0(StubTest, PhiGateTest) { auto module = stubModule.GetModule(); auto function = stubModule.GetTestFunction(FAST_STUB_ID(PhiGateTest)); Circuit netOfGates; - PhiStub optimizer(&netOfGates); + PhiGateTestStub optimizer(&netOfGates); optimizer.GenerateCircuit(stubModule.GetCompilationConfig()); netOfGates.PrintAllGates(); auto cfg = Scheduler::Run(&netOfGates); @@ -129,50 +102,12 @@ HWTEST_F_L0(StubTest, PhiGateTest) EXPECT_EQ(valB, 100); // 100 : expected res for fn(0) } -class LoopStub : public Stub { -public: - explicit LoopStub(Circuit *circuit) : Stub("loop", 1, circuit) {} - ~LoopStub() = default; - NO_MOVE_SEMANTIC(LoopStub); - NO_COPY_SEMANTIC(LoopStub); - void GenerateCircuit(const CompilationConfig *cfg) override - { - Stub::GenerateCircuit(cfg); - auto env = GetEnvironment(); - DEFVARIABLE(z, MachineType::INT32, GetInt32Constant(0)); - DEFVARIABLE(y, MachineType::INT32, Int32Argument(0)); - Label loopHead(env); - Label loopEnd(env); - Label afterLoop(env); - Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopHead, &afterLoop); // 10 : size of entry - LoopBegin(&loopHead); - Label ifTrue(env); - Label ifFalse(env); - Label next(env); - Branch(Word32Equal(Int32Argument(0), GetInt32Constant(9)), &ifTrue, &ifFalse); // 9 : size of entry - Bind(&ifTrue); - z = Int32Add(*y, GetInt32Constant(10)); // 10 : size of entry - y = Int32Add(*z, GetInt32Constant(1)); - Jump(&next); - Bind(&ifFalse); - z = Int32Add(*y, GetInt32Constant(100)); // 100 : size of entry - Jump(&next); - Bind(&next); - y = Int32Add(*y, GetInt32Constant(1)); - Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopEnd, &afterLoop); // 10 : size of entry - Bind(&loopEnd); - LoopEnd(&loopHead); - Bind(&afterLoop); - Return(*z); - } -}; - HWTEST_F_L0(StubTest, LoopTest) { auto module = stubModule.GetModule(); auto function = stubModule.GetTestFunction(FAST_STUB_ID(LoopTest)); Circuit netOfGates; - LoopStub optimizer(&netOfGates); + LoopTestStub optimizer(&netOfGates); optimizer.GenerateCircuit(stubModule.GetCompilationConfig()); netOfGates.PrintAllGates(); auto cfg = Scheduler::Run(&netOfGates); @@ -191,48 +126,12 @@ HWTEST_F_L0(StubTest, LoopTest) EXPECT_EQ(valC, 0); } -class LoopStub1 : public Stub { -public: - explicit LoopStub1(Circuit *circuit) : Stub("loop1", 1, circuit) {} - ~LoopStub1() = default; - NO_MOVE_SEMANTIC(LoopStub1); - NO_COPY_SEMANTIC(LoopStub1); - void GenerateCircuit(const CompilationConfig *cfg) override - { - Stub::GenerateCircuit(cfg); - auto env = GetEnvironment(); - DEFVARIABLE(y, MachineType::INT32, Int32Argument(0)); - DEFVARIABLE(x, MachineType::INT32, Int32Argument(0)); - DEFVARIABLE(z, MachineType::INT32, Int32Argument(0)); - Label loopHead(env); - Label loopEnd(env); - Label afterLoop(env); - Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopHead, &afterLoop); // 10 : size of entry - LoopBegin(&loopHead); - x = Int32Add(*z, GetInt32Constant(3)); // 3 : size of entry - Label ifTrue(env); - Label next(env); - Branch(Word32Equal(*x, GetInt32Constant(9)), &ifTrue, &next); // 9 : size of entry - Bind(&ifTrue); - y = Int32Add(*z, *x); - Jump(&next); - Bind(&next); - y = Int32Add(*y, GetInt32Constant(1)); - Branch(Int32LessThan(*y, GetInt32Constant(10)), &loopEnd, &afterLoop); // 10 : size of entry - Bind(&loopEnd); - LoopEnd(&loopHead); - Bind(&afterLoop); - z = *y; - Return(*z); - } -}; - HWTEST_F_L0(StubTest, LoopTest1) { auto module = stubModule.GetModule(); auto function = stubModule.GetTestFunction(FAST_STUB_ID(LoopTest1)); Circuit netOfGates; - LoopStub1 optimizer(&netOfGates); + LoopTest1Stub optimizer(&netOfGates); optimizer.GenerateCircuit(stubModule.GetCompilationConfig()); netOfGates.PrintAllGates(); auto cfg = Scheduler::Run(&netOfGates); @@ -250,6 +149,7 @@ HWTEST_F_L0(StubTest, LoopTest1) EXPECT_EQ(valB, 10); // 10 : expected res for fn(9) EXPECT_EQ(valC, 11); // 10 : expected res for fn(11) } +#endif HWTEST_F_L0(StubTest, FastAddTest) {