Skip to content

Commit

Permalink
Redesign x87 optimization framework
Browse files Browse the repository at this point in the history
  • Loading branch information
pmatos committed Apr 17, 2024
1 parent 02ebb6e commit cdfb112
Show file tree
Hide file tree
Showing 16 changed files with 1,533 additions and 116 deletions.
8 changes: 8 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-const-correctness,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,-misc-use-anonymous-namespace,readability-identifier-naming,-misc-include-cleaner'
CheckOptions:
- key: readability-identifier-naming.IgnoreMainLikeFunctions
value: 1
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
value: 1
- key: modernize-use-default-member-init.UseAssignment
value: 1
2 changes: 2 additions & 0 deletions FEXCore/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ set (SRCS
Interface/Core/OpcodeDispatcher/Flags.cpp
Interface/Core/OpcodeDispatcher/Vector.cpp
Interface/Core/OpcodeDispatcher/X87.cpp
Interface/Core/OpcodeDispatcher/X87New.cpp
Interface/Core/OpcodeDispatcher/X87F64.cpp
Interface/Core/OpcodeDispatcher.cpp
Interface/Core/X86Tables.cpp
Expand Down Expand Up @@ -150,6 +151,7 @@ set (SRCS
Interface/IR/Passes/DeadStoreElimination.cpp
Interface/IR/Passes/RegisterAllocationPass.cpp
Interface/IR/Passes/InlineCallOptimization.cpp
Interface/IR/Passes/x87StackOptimizationPass.cpp
Utils/Telemetry.cpp
Utils/Threads.cpp
Utils/Profiler.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ bool InterpreterOps::GetFallbackHandler(bool SupportsPreserveAllABI, const IR::I
uint8_t OpSize = IROp->Size;
switch (IROp->Op) {
case IR::OP_F80CVTTO: {
auto Op = IROp->C<IR::IROp_F80CVTTo>();
const auto* Op = IROp->C<IR::IROp_F80CVTTo>();

switch (Op->SrcSize) {
case 4: {
Expand Down Expand Up @@ -119,7 +119,7 @@ bool InterpreterOps::GetFallbackHandler(bool SupportsPreserveAllABI, const IR::I
break;
}
case IR::OP_F80CVTINT: {
auto Op = IROp->C<IR::IROp_F80CVTInt>();
const auto* Op = IROp->C<IR::IROp_F80CVTInt>();

switch (OpSize) {
case 2: {
Expand Down Expand Up @@ -154,7 +154,7 @@ bool InterpreterOps::GetFallbackHandler(bool SupportsPreserveAllABI, const IR::I
break;
}
case IR::OP_F80CMP: {
auto Op = IROp->C<IR::IROp_F80Cmp>();
const auto* Op = IROp->C<IR::IROp_F80Cmp>();

static constexpr std::array handlers {
&FEXCore::CPU::OpHandlers<IR::OP_F80CMP>::handle<0>, &FEXCore::CPU::OpHandlers<IR::OP_F80CMP>::handle<1>,
Expand All @@ -169,7 +169,7 @@ bool InterpreterOps::GetFallbackHandler(bool SupportsPreserveAllABI, const IR::I
}

case IR::OP_F80CVTTOINT: {
auto Op = IROp->C<IR::IROp_F80CVTToInt>();
const auto* Op = IROp->C<IR::IROp_F80CVTToInt>();

switch (Op->SrcSize) {
case 2: {
Expand Down
1 change: 0 additions & 1 deletion FEXCore/Source/Interface/Core/JIT/Arm64/MiscOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,5 @@ DEF_OP(RDRAND) {
DEF_OP(Yield) {
yield();
}

#undef DEF_OP
} // namespace FEXCore::CPU
259 changes: 171 additions & 88 deletions FEXCore/Source/Interface/Core/OpcodeDispatcher.cpp

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions FEXCore/Source/Interface/Core/OpcodeDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,14 @@ class OpDispatchBuilder final : public IREmitter {
template<bool Truncate>
void FIST(OpcodeArgs);

// OpResult is used for Stack operations,
// describes if the result of the operation is stored in ST(0) or ST(i),
// where ST(i) is one of the arguments to the operation.
enum class OpResult {
RES_ST0,
RES_STI,
};

template<size_t width, bool Integer, OpResult ResInST0>
void FADD(OpcodeArgs);
template<size_t width, bool Integer, OpResult ResInST0>
Expand Down Expand Up @@ -2070,6 +2074,7 @@ class OpDispatchBuilder final : public IREmitter {

bool Multiblock {};
uint64_t Entry;
IROp_IRHeader *CurrentHeader{};

OrderedNode* _StoreMemAutoTSO(FEXCore::IR::RegisterClassType Class, uint8_t Size, OrderedNode* Addr, OrderedNode* Value, uint8_t Align = 1) {
if (CTX->IsAtomicTSOEnabled()) {
Expand Down
66 changes: 45 additions & 21 deletions FEXCore/Source/Interface/Core/OpcodeDispatcher/X87.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ OrderedNode* OpDispatchBuilder::ReconstructX87StateFromFSW(OrderedNode* FSW) {
return Top;
}

#if 0
template<size_t width>
void OpDispatchBuilder::FLD(OpcodeArgs) {
// Update TOP
Expand Down Expand Up @@ -157,9 +158,13 @@ void OpDispatchBuilder::FLD(OpcodeArgs) {
//_StoreContext(converted, 16, offsetof(FEXCore::Core::CPUState, mm[7][0]));
}

template void OpDispatchBuilder::FLD<32>(OpcodeArgs);
template void OpDispatchBuilder::FLD<64>(OpcodeArgs);
template void OpDispatchBuilder::FLD<80>(OpcodeArgs);
template
void OpDispatchBuilder::FLD<32>(OpcodeArgs);
template
void OpDispatchBuilder::FLD<64>(OpcodeArgs);
template
void OpDispatchBuilder::FLD<80>(OpcodeArgs);
#endif

void OpDispatchBuilder::FBLD(OpcodeArgs) {
// Update TOP
Expand Down Expand Up @@ -254,6 +259,7 @@ void OpDispatchBuilder::FILD(OpcodeArgs) {
_StoreContextIndexed(converted, top, 16, MMBaseOffset(), 16, FPRClass);
}

#if 0
template<size_t width>
void OpDispatchBuilder::FST(OpcodeArgs) {
auto orig_top = GetX87Top();
Expand All @@ -274,9 +280,13 @@ void OpDispatchBuilder::FST(OpcodeArgs) {
}
}

template void OpDispatchBuilder::FST<32>(OpcodeArgs);
template void OpDispatchBuilder::FST<64>(OpcodeArgs);
template void OpDispatchBuilder::FST<80>(OpcodeArgs);
template
void OpDispatchBuilder::FST<32>(OpcodeArgs);
template
void OpDispatchBuilder::FST<64>(OpcodeArgs);
template
void OpDispatchBuilder::FST<80>(OpcodeArgs);
#endif

template<bool Truncate>
void OpDispatchBuilder::FIST(OpcodeArgs) {
Expand All @@ -300,7 +310,8 @@ void OpDispatchBuilder::FIST(OpcodeArgs) {
template void OpDispatchBuilder::FIST<false>(OpcodeArgs);
template void OpDispatchBuilder::FIST<true>(OpcodeArgs);

template<size_t width, bool Integer, OpDispatchBuilder::OpResult ResInST0>
#if 0
template <size_t width, bool Integer, OpDispatchBuilder::OpResult ResInST0>
void OpDispatchBuilder::FADD(OpcodeArgs) {
auto top = GetX87Top();
OrderedNode* StackLocation = top;
Expand Down Expand Up @@ -351,9 +362,13 @@ template void OpDispatchBuilder::FADD<64, false, OpDispatchBuilder::OpResult::RE
template void OpDispatchBuilder::FADD<80, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FADD<80, false, OpDispatchBuilder::OpResult::RES_STI>(OpcodeArgs);

template void OpDispatchBuilder::FADD<16, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FADD<32, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template
void OpDispatchBuilder::FADD<16, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template
void OpDispatchBuilder::FADD<32, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
#endif

#if 0
template<size_t width, bool Integer, OpDispatchBuilder::OpResult ResInST0>
void OpDispatchBuilder::FMUL(OpcodeArgs) {
auto top = GetX87Top();
Expand Down Expand Up @@ -407,9 +422,12 @@ template void OpDispatchBuilder::FMUL<64, false, OpDispatchBuilder::OpResult::RE
template void OpDispatchBuilder::FMUL<80, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FMUL<80, false, OpDispatchBuilder::OpResult::RES_STI>(OpcodeArgs);

template void OpDispatchBuilder::FMUL<16, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FMUL<32, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);

template
void OpDispatchBuilder::FMUL<16, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template
void OpDispatchBuilder::FMUL<32, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
#endif
#if 0
template<size_t width, bool Integer, bool reverse, OpDispatchBuilder::OpResult ResInST0>
void OpDispatchBuilder::FDIV(OpcodeArgs) {
auto top = GetX87Top();
Expand Down Expand Up @@ -478,9 +496,12 @@ template void OpDispatchBuilder::FDIV<80, false, true, OpDispatchBuilder::OpResu
template void OpDispatchBuilder::FDIV<16, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FDIV<16, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);

template void OpDispatchBuilder::FDIV<32, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FDIV<32, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);

template
void OpDispatchBuilder::FDIV<32, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template
void OpDispatchBuilder::FDIV<32, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
#endif
#if 0
template<size_t width, bool Integer, bool reverse, OpDispatchBuilder::OpResult ResInST0>
void OpDispatchBuilder::FSUB(OpcodeArgs) {
auto top = GetX87Top();
Expand Down Expand Up @@ -549,9 +570,11 @@ template void OpDispatchBuilder::FSUB<80, false, true, OpDispatchBuilder::OpResu
template void OpDispatchBuilder::FSUB<16, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FSUB<16, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);

template void OpDispatchBuilder::FSUB<32, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template void OpDispatchBuilder::FSUB<32, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);

template
void OpDispatchBuilder::FSUB<32, true, false, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
template
void OpDispatchBuilder::FSUB<32, true, true, OpDispatchBuilder::OpResult::RES_ST0>(OpcodeArgs);
#endif
void OpDispatchBuilder::FCHS(OpcodeArgs) {
auto top = GetX87Top();
auto a = _LoadContextIndexed(top, 16, MMBaseOffset(), 16, FPRClass);
Expand Down Expand Up @@ -911,7 +934,7 @@ void OpDispatchBuilder::X87ATAN(OpcodeArgs) {
// Write to ST[TOP]
_StoreContextIndexed(result, top, 16, MMBaseOffset(), 16, FPRClass);
}

#if 0
void OpDispatchBuilder::X87LDENV(OpcodeArgs) {
const auto Size = GetSrcSize(Op);
OrderedNode* Mem = MakeSegmentAddress(Op, Op->Src[0]);
Expand All @@ -929,7 +952,8 @@ void OpDispatchBuilder::X87LDENV(OpcodeArgs) {
SetX87FTW(_LoadMem(GPRClass, Size, MemLocation, Size));
}
}

#endif
#if 0
void OpDispatchBuilder::X87FNSTENV(OpcodeArgs) {
// 14 bytes for 16bit
// 2 Bytes : FCW
Expand Down Expand Up @@ -995,7 +1019,7 @@ void OpDispatchBuilder::X87FNSTENV(OpcodeArgs) {
_StoreMem(GPRClass, Size, MemLocation, ZeroConst, Size);
}
}

#endif
void OpDispatchBuilder::X87FLDCW(OpcodeArgs) {
OrderedNode* NewFCW = LoadSource(GPRClass, Op, Op->Src[0], Op->Flags);
_StoreContext(2, GPRClass, NewFCW, offsetof(FEXCore::Core::CPUState, FCW));
Expand Down
Loading

0 comments on commit cdfb112

Please sign in to comment.