Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llvm-exegesis] Add middle half repetition mode (#77020)
This patch adds two new repetition modes to llvm-exegesis, particularly loop and duplicate repetition modes of what I am terming the middle half repetition mode. The middle half repetition mode essentially runs each measurement twice, one with twice the number of iterations of the other. These two measurements are then agregated by taking their difference. This subtracts away any setup/overhead that is unrelated to the code in the snippet, providing more accurate results. Using this mode on a couple toy examples, I am able to get exact (integer) throughput values on all of them in contrast to the default duplicate/loop repetition modes which show a little bit of noise on the snippet value.
- Loading branch information
1 parent
6a21e00
commit d8b61d7
Showing
10 changed files
with
271 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# REQUIRES: exegesis-can-measure-latency, x86_64-linux | ||
|
||
# Check that we can use the middle-half repetition mode without crashing | ||
|
||
# RUN: llvm-exegesis -mtriple=x86_64-unknown-unknown -mode=latency -opcode-name=ADD64rr -repetition-mode=middle-half-duplicate | FileCheck %s | ||
# RUN: llvm-exegesis -mtriple=x86_64-unknown-unknown -mode=latency -opcode-name=ADD64rr -repetition-mode=middle-half-loop | FileCheck %s | ||
|
||
# CHECK: - { key: latency, value: {{[0-9.]*}}, per_snippet_value: {{[0-9.]*}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===-- ResultAggregator.cpp ------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "ResultAggregator.h" | ||
|
||
namespace llvm { | ||
namespace exegesis { | ||
|
||
class DefaultResultAggregator : public ResultAggregator { | ||
void AggregateResults(Benchmark &Result, | ||
ArrayRef<Benchmark> OtherResults) const override{}; | ||
void AggregateMeasurement(BenchmarkMeasure &Measurement, | ||
const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const override{}; | ||
}; | ||
|
||
class MinimumResultAggregator : public ResultAggregator { | ||
void AggregateMeasurement(BenchmarkMeasure &Measurement, | ||
const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const override; | ||
}; | ||
|
||
void MinimumResultAggregator::AggregateMeasurement( | ||
BenchmarkMeasure &Measurement, const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const { | ||
Measurement.PerInstructionValue = std::min( | ||
Measurement.PerInstructionValue, NewMeasurement.PerInstructionValue); | ||
Measurement.PerSnippetValue = | ||
std::min(Measurement.PerSnippetValue, NewMeasurement.PerSnippetValue); | ||
Measurement.RawValue = | ||
std::min(Measurement.RawValue, NewMeasurement.RawValue); | ||
} | ||
|
||
class MiddleHalfResultAggregator : public ResultAggregator { | ||
void AggregateMeasurement(BenchmarkMeasure &Measurement, | ||
const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const override; | ||
}; | ||
|
||
void MiddleHalfResultAggregator::AggregateMeasurement( | ||
BenchmarkMeasure &Measurement, const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const { | ||
Measurement.RawValue = NewMeasurement.RawValue - Measurement.RawValue; | ||
Measurement.PerInstructionValue = Measurement.RawValue; | ||
Measurement.PerInstructionValue /= Result.NumRepetitions; | ||
Measurement.PerSnippetValue = Measurement.RawValue; | ||
Measurement.PerSnippetValue /= | ||
std::ceil(Result.NumRepetitions / | ||
static_cast<double>(Result.Key.Instructions.size())); | ||
} | ||
|
||
void ResultAggregator::AggregateResults( | ||
Benchmark &Result, ArrayRef<Benchmark> OtherResults) const { | ||
for (const Benchmark &OtherResult : OtherResults) { | ||
append_range(Result.AssembledSnippet, OtherResult.AssembledSnippet); | ||
|
||
if (OtherResult.Measurements.empty()) | ||
continue; | ||
|
||
assert(OtherResult.Measurements.size() == Result.Measurements.size() && | ||
"Expected to have an identical number of measurements"); | ||
|
||
for (auto I : zip(Result.Measurements, OtherResult.Measurements)) { | ||
BenchmarkMeasure &Measurement = std::get<0>(I); | ||
const BenchmarkMeasure &NewMeasurement = std::get<1>(I); | ||
|
||
assert(Measurement.Key == NewMeasurement.Key && | ||
"Expected measurements to be symmetric"); | ||
|
||
AggregateMeasurement(Measurement, NewMeasurement, Result); | ||
} | ||
} | ||
} | ||
|
||
std::unique_ptr<ResultAggregator> | ||
ResultAggregator::CreateAggregator(Benchmark::RepetitionModeE RepetitionMode) { | ||
switch (RepetitionMode) { | ||
case Benchmark::RepetitionModeE::Duplicate: | ||
case Benchmark::RepetitionModeE::Loop: | ||
return std::make_unique<DefaultResultAggregator>(); | ||
case Benchmark::RepetitionModeE::AggregateMin: | ||
return std::make_unique<MinimumResultAggregator>(); | ||
case Benchmark::RepetitionModeE::MiddleHalfDuplicate: | ||
case Benchmark::RepetitionModeE::MiddleHalfLoop: | ||
return std::make_unique<MiddleHalfResultAggregator>(); | ||
} | ||
} | ||
|
||
} // namespace exegesis | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===-- ResultAggregator.h --------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// Defines result aggregators that are used to aggregate the results from | ||
/// multiple full benchmark runs. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "BenchmarkResult.h" | ||
|
||
namespace llvm { | ||
namespace exegesis { | ||
|
||
class ResultAggregator { | ||
public: | ||
static std::unique_ptr<ResultAggregator> | ||
CreateAggregator(Benchmark::RepetitionModeE RepetitionMode); | ||
|
||
virtual void AggregateResults(Benchmark &Result, | ||
ArrayRef<Benchmark> OtherResults) const; | ||
virtual void AggregateMeasurement(BenchmarkMeasure &Measurement, | ||
const BenchmarkMeasure &NewMeasurement, | ||
const Benchmark &Result) const = 0; | ||
|
||
virtual ~ResultAggregator() = default; | ||
}; | ||
|
||
} // namespace exegesis | ||
} // namespace llvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.