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

Float and Double Vector Support #45

Merged
merged 2 commits into from
Sep 21, 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
12 changes: 11 additions & 1 deletion tools/mutator-utils/mutator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,19 @@ llvm::Value *FunctionMutator::getRandomConstant(llvm::Type *ty) {
}

if (auto vecTy = llvm::dyn_cast<llvm::FixedVectorType>(ty);
vecTy && vecTy->getElementType()) {
vecTy && vecTy->getElementType()->isIntegerTy()) {
return mutator_util::getRandomLLVMIntegerVector(vecTy);
}

if (auto vecTy = llvm::dyn_cast<llvm::FixedVectorType>(ty);
vecTy && vecTy->getElementType()->isFloatTy()) {
return mutator_util::getRandomLLVMFloatVector(vecTy);
}

if (auto vecTy = llvm::dyn_cast<llvm::FixedVectorType>(ty);
vecTy && vecTy->getElementType()->isDoubleTy()) {
return mutator_util::getRandomLLVMDoubleVector(vecTy);
}
return llvm::UndefValue::get(ty);
}

Expand Down
78 changes: 78 additions & 0 deletions tools/mutator-utils/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,84 @@ mutator_util::getRandomLLVMIntegerVector(llvm::FixedVectorType *ty) {
return llvm::ConstantVector::get(result);
}

llvm::Constant *
mutator_util::getRandomLLVMFloatVector(llvm::FixedVectorType *ty) {
assert(ty->getElementType()->isFloatTy() && "expect a float vector");
llvm::Type *floatTy = llvm::Type::getFloatTy(ty->getContext());
unsigned length = ty->getNumElements();
llvm::SmallVector<llvm::Constant *> result;
for (size_t i = 0; i < length; ++i) {
result.push_back(llvm::ConstantFP::get(floatTy, getRandomLLVMFloat()));
}
// we set threshold to 5, if p is less than threshold , we assign poison to
// the vector
unsigned threshold = 5, p = Random::getRandomUnsigned() % 100;
if (p < threshold) {
unsigned poisonNum = Random::getRandomUnsigned(2) + 1;
poisonNum = std::min(poisonNum, length);
for (size_t times = 0; times < poisonNum; ++times) {
result[Random::getRandomUnsigned() % length] =
llvm::PoisonValue::get(floatTy);
}
}
return llvm::ConstantVector::get(result);
}

llvm::Constant *mutator_util::updateFloatVector(llvm::ConstantVector *ty) {
unsigned length = ty->getType()->getNumElements();
assert(ty->getType()->getElementType()->isFloatTy() &&
"only support for updating float type");
llvm::Type *floatTy = llvm::Type::getFloatTy(ty->getContext());
llvm::SmallVector<llvm::Constant *> result;
for (size_t i = 0; i < length; ++i) {
if (Random::getRandomBool()) {
result.push_back(llvm::ConstantFP::get(floatTy, getRandomLLVMFloat()));
} else {
result.push_back(ty->getAggregateElement(i));
}
}
return llvm::ConstantVector::get(result);
}

llvm::Constant *
mutator_util::getRandomLLVMDoubleVector(llvm::FixedVectorType *ty) {
assert(ty->getElementType()->isDoubleTy() && "expect a double vector");
llvm::Type *doubleTy = llvm::Type::getDoubleTy(ty->getContext());
unsigned length = ty->getNumElements();
llvm::SmallVector<llvm::Constant *> result;
for (size_t i = 0; i < length; ++i) {
result.push_back(llvm::ConstantFP::get(doubleTy, getRandomLLVMDouble()));
}
// we set threshold to 5, if p is less than threshold , we assign poison to
// the vector
unsigned threshold = 5, p = Random::getRandomUnsigned() % 100;
if (p < threshold) {
unsigned poisonNum = Random::getRandomUnsigned(2) + 1;
poisonNum = std::min(poisonNum, length);
for (size_t times = 0; times < poisonNum; ++times) {
result[Random::getRandomUnsigned() % length] =
llvm::PoisonValue::get(doubleTy);
}
}
return llvm::ConstantVector::get(result);
}

llvm::Constant *mutator_util::updateDoubleVector(llvm::ConstantVector *ty) {
unsigned length = ty->getType()->getNumElements();
assert(ty->getType()->getElementType()->isDoubleTy() &&
"only support for updating double type");
llvm::Type *doubleTy = llvm::Type::getDoubleTy(ty->getContext());
llvm::SmallVector<llvm::Constant *> result;
for (size_t i = 0; i < length; ++i) {
if (Random::getRandomBool()) {
result.push_back(llvm::ConstantFP::get(doubleTy, getRandomLLVMDouble()));
} else {
result.push_back(ty->getAggregateElement(i));
}
}
return llvm::ConstantVector::get(result);
}

llvm::Constant *mutator_util::updateIntegerVector(llvm::ConstantVector *ty) {
unsigned length = ty->getType()->getNumElements();
assert(ty->getType()->getElementType()->isIntegerTy() &&
Expand Down
4 changes: 4 additions & 0 deletions tools/mutator-utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,8 @@ class mutator_util {
static llvm::ConstantRange getRandomLLVMConstantRange(llvm::IntegerType *ty);
static llvm::Constant *getRandomLLVMIntegerVector(llvm::FixedVectorType *ty);
static llvm::Constant *updateIntegerVector(llvm::ConstantVector *ty);
static llvm::Constant *getRandomLLVMFloatVector(llvm::FixedVectorType *ty);
static llvm::Constant *updateFloatVector(llvm::ConstantVector *ty);
static llvm::Constant *getRandomLLVMDoubleVector(llvm::FixedVectorType *ty);
static llvm::Constant *updateDoubleVector(llvm::ConstantVector *ty);
};
Loading