Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated to use Module::createRNG and removed anonymous namespace where static functions were appropriate #110

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/DuplicateBB.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include "llvm/Pass.h"

#include <map>
#include <memory>

namespace llvm {
class RandomNumberGenerator;
} // namespace llvm

//------------------------------------------------------------------------------
// New PM interface
Expand Down Expand Up @@ -51,6 +56,8 @@ struct DuplicateBB : public llvm::PassInfoMixin<DuplicateBB> {
// decorated with the optnone LLVM attribute. Note that clang -O0 decorates
// all functions with optnone.
static bool isRequired() { return true; }

std::unique_ptr<llvm::RandomNumberGenerator> pRNG;
};

#endif
6 changes: 1 addition & 5 deletions lib/ConvertFCmpEq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
using namespace llvm;

// Unnamed namespace for private functions
namespace {

FCmpInst *convertFCmpEqInstruction(FCmpInst *FCmp) noexcept {
static FCmpInst *convertFCmpEqInstruction(FCmpInst *FCmp) noexcept {
assert(FCmp && "The given fcmp instruction is null");

if (!FCmp->isEquality()) {
Expand Down Expand Up @@ -115,8 +113,6 @@ FCmpInst *convertFCmpEqInstruction(FCmpInst *FCmp) noexcept {
return FCmp;
}

} // namespace

static constexpr char PassArg[] = "convert-fcmp-eq";
static constexpr char PassName[] =
"Convert floating-point equality comparisons";
Expand Down
15 changes: 5 additions & 10 deletions lib/DuplicateBB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Support/RandomNumberGenerator.h"

#include <random>

Expand All @@ -100,15 +101,6 @@ DuplicateBB::BBToSingleRIVMap
DuplicateBB::findBBsToDuplicate(Function &F, const RIV::Result &RIVResult) {
BBToSingleRIVMap BlocksToDuplicate;

// Get a random number generator. This will be used to choose a context
// value for the injected `if-then-else` construct.
// FIXME Switch to 'F.getParent()->createRNG("DuplicateBB")' once possible.
// This patch implements the necessary API:
// * https://reviews.llvm.org/rG73713f3e5ef2ecf1e5afafa89f76ab89cc06b18e
// It should be available in LLVM 11.
std::random_device RD;
std::mt19937_64 RNG(RD());

for (BasicBlock &BB : F) {
// Basic blocks which are landing pads are used for handling exceptions.
// That's out of scope of this pass.
Expand All @@ -129,7 +121,7 @@ DuplicateBB::findBBsToDuplicate(Function &F, const RIV::Result &RIVResult) {
// Get a random context value from the RIV set
auto Iter = ReachableValues.begin();
std::uniform_int_distribution<> Dist(0, ReachableValuesCount - 1);
std::advance(Iter, Dist(RNG));
std::advance(Iter, Dist(*pRNG));

if (dyn_cast<GlobalValue>(*Iter)) {
LLVM_DEBUG(errs() << "Random context value is a global variable. "
Expand Down Expand Up @@ -243,6 +235,9 @@ void DuplicateBB::cloneBB(BasicBlock &BB, Value *ContextValue,

PreservedAnalyses DuplicateBB::run(llvm::Function &F,
llvm::FunctionAnalysisManager &FAM) {
if (!pRNG)
pRNG = F.getParent()->createRNG("duplicate-bb");

BBToSingleRIVMap Targets = findBBsToDuplicate(F, FAM.getResult<RIV>(F));

// This map is used to keep track of the new bindings. Otherwise, the
Expand Down
5 changes: 0 additions & 5 deletions lib/FindFCmpEq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@

using namespace llvm;

// Unnamed namespace for internal functions
namespace {

static void
printFCmpEqInstructions(raw_ostream &OS, Function &Func,
const FindFCmpEq::Result &FCmpEqInsts) noexcept {
Expand All @@ -68,8 +65,6 @@ printFCmpEqInstructions(raw_ostream &OS, Function &Func,
}
}

} // namespace

static constexpr char PassArg[] = "find-fcmp-eq";
static constexpr char PassName[] =
"Floating-point equality comparisons locator";
Expand Down
Loading