File tree Expand file tree Collapse file tree 5 files changed +63
-2
lines changed
test/tools/llvm-exegesis/X86 Expand file tree Collapse file tree 5 files changed +63
-2
lines changed Original file line number Diff line number Diff line change
1
+ # RUN: llvm-exegesis -mode=uops -opcode-name=FLDENVm,FLDL2E -repetition-mode=duplicate | FileCheck %s
2
+
3
+ CHECK: mode: uops
4
+ CHECK-NEXT: key:
5
+ CHECK-NEXT: instructions:
6
+ CHECK-NEXT: FLDENVm
Original file line number Diff line number Diff line change @@ -71,10 +71,10 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
71
71
SmallVector<StringRef, 2 > CounterNames;
72
72
StringRef (Counters).split (CounterNames, ' +' );
73
73
char *const ScratchPtr = Scratch->ptr ();
74
+ const ExegesisTarget &ET = State.getExegesisTarget ();
74
75
for (auto &CounterName : CounterNames) {
75
76
CounterName = CounterName.trim ();
76
- auto CounterOrError =
77
- State.getExegesisTarget ().createCounter (CounterName, State);
77
+ auto CounterOrError = ET.createCounter (CounterName, State);
78
78
79
79
if (!CounterOrError)
80
80
return CounterOrError.takeError ();
@@ -93,6 +93,7 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
93
93
.concat (std::to_string (Reserved)));
94
94
Scratch->clear ();
95
95
{
96
+ auto PS = ET.withSavedState ();
96
97
CrashRecoveryContext CRC;
97
98
CrashRecoveryContext::Enable ();
98
99
const bool Crashed = !CRC.RunSafely ([this , Counter, ScratchPtr]() {
@@ -101,6 +102,7 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
101
102
Counter->stop ();
102
103
});
103
104
CrashRecoveryContext::Disable ();
105
+ PS.reset ();
104
106
if (Crashed) {
105
107
std::string Msg = " snippet crashed while running" ;
106
108
#ifdef LLVM_ON_UNIX
Original file line number Diff line number Diff line change @@ -147,6 +147,8 @@ const PfmCountersInfo &ExegesisTarget::getPfmCounters(StringRef CpuName) const {
147
147
return *Found->PCI ;
148
148
}
149
149
150
+ ExegesisTarget::SavedState::~SavedState () {} // anchor.
151
+
150
152
namespace {
151
153
152
154
// Default implementation.
Original file line number Diff line number Diff line change @@ -172,6 +172,16 @@ class ExegesisTarget {
172
172
// counters are defined for this CPU).
173
173
const PfmCountersInfo &getPfmCounters (StringRef CpuName) const ;
174
174
175
+ // Saves the CPU state that needs to be preserved when running a benchmark,
176
+ // and returns and RAII object that restores the state on destruction.
177
+ // By default no state is preserved.
178
+ struct SavedState {
179
+ virtual ~SavedState ();
180
+ };
181
+ virtual std::unique_ptr<SavedState> withSavedState () const {
182
+ return std::make_unique<SavedState>();
183
+ }
184
+
175
185
private:
176
186
virtual bool matchesArch (Triple::ArchType Arch) const = 0;
177
187
Original file line number Diff line number Diff line change 26
26
#include < memory>
27
27
#include < string>
28
28
#include < vector>
29
+ #if defined(_MSC_VER)
30
+ #include < immintrin.h>
31
+ #endif
29
32
30
33
namespace llvm {
31
34
namespace exegesis {
@@ -594,6 +597,40 @@ void ConstantInliner::initStack(unsigned Bytes) {
594
597
595
598
namespace {
596
599
600
+ class X86SavedState : public ExegesisTarget ::SavedState {
601
+ public:
602
+ X86SavedState () {
603
+ #ifdef __x86_64__
604
+ # if defined(_MSC_VER)
605
+ _fxsave64 (FPState);
606
+ # elif defined(__GNUC__)
607
+ __builtin_ia32_fxsave64 (FPState);
608
+ # endif
609
+ #else
610
+ llvm_unreachable (" X86 exegesis running on non-X86 target" );
611
+ #endif
612
+ }
613
+
614
+ ~X86SavedState () {
615
+ // Restoring the X87 state does not flush pending exceptions, make sure
616
+ // these exceptions are flushed now.
617
+ #ifdef __x86_64__
618
+ # if defined(_MSC_VER)
619
+ _clearfp ();
620
+ _fxrstor64 (FPState);
621
+ # elif defined(__GNUC__)
622
+ asm volatile (" fwait" );
623
+ __builtin_ia32_fxrstor64 (FPState);
624
+ # endif
625
+ #else
626
+ llvm_unreachable (" X86 exegesis running on non-X86 target" );
627
+ #endif
628
+ }
629
+
630
+ private:
631
+ alignas (16 ) char FPState[512 ];
632
+ };
633
+
597
634
class ExegesisX86Target : public ExegesisTarget {
598
635
public:
599
636
ExegesisX86Target () : ExegesisTarget(X86CpuPfmCounters) {}
@@ -691,6 +728,10 @@ class ExegesisX86Target : public ExegesisTarget {
691
728
#endif
692
729
}
693
730
731
+ std::unique_ptr<SavedState> withSavedState () const override {
732
+ return std::make_unique<X86SavedState>();
733
+ }
734
+
694
735
static const unsigned kUnavailableRegisters [4 ];
695
736
};
696
737
You can’t perform that action at this time.
0 commit comments