Skip to content

Commit

Permalink
Mark mpi functions of booleans as inactive (#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses authored Oct 6, 2023
1 parent 07f3e71 commit 9aa0b8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
37 changes: 37 additions & 0 deletions enzyme/Enzyme/Enzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,43 @@ void attributeKnownFunctions(llvm::Function &F) {
F.addParamAttr(2, Attribute::WriteOnly);
F.addParamAttr(2, Attribute::NoCapture);
}
// Map of MPI function name to the arg index of its type argument
std::map<std::string, int> MPI_TYPE_ARGS = {
{"MPI_Send", 2}, {"MPI_Ssend", 2}, {"MPI_Bsend", 2},
{"MPI_Recv", 2}, {"MPI_Brecv", 2}, {"PMPI_Send", 2},
{"PMPI_Ssend", 2}, {"PMPI_Bsend", 2}, {"PMPI_Recv", 2},
{"PMPI_Brecv", 2},

{"MPI_Isend", 2}, {"MPI_Irecv", 2}, {"PMPI_Isend", 2},
{"PMPI_Irecv", 2},

{"MPI_Reduce", 3}, {"PMPI_Reduce", 3},

{"MPI_Allreduce", 3}, {"PMPI_Allreduce", 3}};
{
auto found = MPI_TYPE_ARGS.find(F.getName().str());
if (found != MPI_TYPE_ARGS.end()) {
for (auto user : F.users()) {
if (auto CI = dyn_cast<CallBase>(user))
if (CI->getCalledFunction() == &F) {
if (Constant *C =
dyn_cast<Constant>(CI->getArgOperand(found->second))) {
while (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
C = CE->getOperand(0);
}
if (auto GV = dyn_cast<GlobalVariable>(C)) {
if (GV->getName() == "ompi_mpi_cxx_bool") {
CI->addAttribute(
AttributeList::FunctionIndex,
Attribute::get(CI->getContext(), "enzyme_inactive"));
}
}
}
}
}
}
}

if (F.getName() == "omp_get_max_threads" ||
F.getName() == "omp_get_thread_num") {
#if LLVM_VERSION_MAJOR >= 16
Expand Down
13 changes: 11 additions & 2 deletions enzyme/Enzyme/TypeAnalysis/TypeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4031,6 +4031,8 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
buf.insert({0}, Type::getDoubleTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_float") {
buf.insert({0}, Type::getFloatTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_cxx_bool") {
buf.insert({0}, BaseType::Integer);
}
} else if (auto CI = dyn_cast<ConstantInt>(C)) {
// MPICH
Expand All @@ -4051,7 +4053,8 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
updateAnalysis(&call, TypeTree(BaseType::Integer).Only(-1, &call), &call);
return;
}
if (funcName == "MPI_Isend" || funcName == "MPI_Irecv") {
if (funcName == "MPI_Isend" || funcName == "MPI_Irecv" ||
funcName == "PMPI_Isend" || funcName == "PMPI_Irecv") {
TypeTree buf = TypeTree(BaseType::Pointer);

if (Constant *C = dyn_cast<Constant>(call.getOperand(2))) {
Expand All @@ -4063,6 +4066,8 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
buf.insert({0}, Type::getDoubleTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_float") {
buf.insert({0}, Type::getFloatTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_cxx_bool") {
buf.insert({0}, BaseType::Integer);
}
} else if (auto CI = dyn_cast<ConstantInt>(C)) {
// MPICH
Expand Down Expand Up @@ -4137,6 +4142,8 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
buf.insert({0}, Type::getDoubleTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_float") {
buf.insert({0}, Type::getFloatTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_cxx_bool") {
buf.insert({0}, BaseType::Integer);
}
} else if (auto CI = dyn_cast<ConstantInt>(C)) {
// MPICH
Expand Down Expand Up @@ -4164,7 +4171,7 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
updateAnalysis(&call, TypeTree(BaseType::Integer).Only(-1, &call), &call);
return;
}
if (funcName == "MPI_Allreduce") {
if (funcName == "MPI_Allreduce" || funcName == "PMPI_Allreduce") {
TypeTree buf = TypeTree(BaseType::Pointer);

if (Constant *C = dyn_cast<Constant>(call.getOperand(3))) {
Expand All @@ -4176,6 +4183,8 @@ void TypeAnalyzer::visitCallBase(CallBase &call) {
buf.insert({0}, Type::getDoubleTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_float") {
buf.insert({0}, Type::getFloatTy(C->getContext()));
} else if (GV->getName() == "ompi_mpi_cxx_bool") {
buf.insert({0}, BaseType::Integer);
}
} else if (auto CI = dyn_cast<ConstantInt>(C)) {
// MPICH
Expand Down

0 comments on commit 9aa0b8d

Please sign in to comment.