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

Various improvements to trunc mode #2085

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions enzyme/Enzyme/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,5 @@ install(TARGETS LLDEnzyme-${LLVM_VERSION_MAJOR}
if (ENZYME_MLIR)
add_subdirectory(MLIR)
endif()

add_subdirectory(Runtimes/FPRT)
56 changes: 40 additions & 16 deletions enzyme/Enzyme/Enzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//
//===----------------------------------------------------------------------===//
#include <llvm/Config/llvm-config.h>
#include <llvm/IR/GlobalValue.h>
#include <memory>

#if LLVM_VERSION_MAJOR >= 16
Expand Down Expand Up @@ -1463,26 +1464,49 @@ class EnzymeBase {

bool HandleTruncateValue(CallInst *CI, bool isTruncate) {
IRBuilder<> Builder(CI);
if (CI->arg_size() != 3) {
unsigned ArgSize = CI->arg_size();
if (ArgSize != 4 && ArgSize != 3) {
EmitFailure("TooManyArgs", CI->getDebugLoc(), CI,
"Had incorrect number of args to __enzyme_truncate_value",
*CI, " - expected 3");
return false;
}
auto Cfrom = cast<ConstantInt>(CI->getArgOperand(1));
assert(Cfrom);
auto Cto = cast<ConstantInt>(CI->getArgOperand(2));
assert(Cto);
auto Addr = CI->getArgOperand(0);
RequestContext context(CI, &Builder);
bool res = Logic.CreateTruncateValue(
context, Addr,
getDefaultFloatRepr((unsigned)Cfrom->getValue().getZExtValue()),
getDefaultFloatRepr((unsigned)Cto->getValue().getZExtValue()),
isTruncate);
if (!res)
return false;
return true;
if (ArgSize == 3) {
auto Cfrom = cast<ConstantInt>(CI->getArgOperand(1));
assert(Cfrom);
auto Cto = cast<ConstantInt>(CI->getArgOperand(2));
assert(Cto);
auto Addr = CI->getArgOperand(0);
RequestContext context(CI, &Builder);
bool res = Logic.CreateTruncateValue(
context, Addr,
getDefaultFloatRepr((unsigned)Cfrom->getValue().getZExtValue()),
getDefaultFloatRepr((unsigned)Cto->getValue().getZExtValue()),
isTruncate);
if (!res)
return false;
return true;
} else if (ArgSize == 4) {
auto Cfrom = cast<ConstantInt>(CI->getArgOperand(1));
assert(Cfrom);
auto Cto_exponent = cast<ConstantInt>(CI->getArgOperand(2));
assert(Cto_exponent);
auto Cto_significand = cast<ConstantInt>(CI->getArgOperand(3));
assert(Cto_significand);
auto Addr = CI->getArgOperand(0);
RequestContext context(CI, &Builder);
bool res = Logic.CreateTruncateValue(
context, Addr,
getDefaultFloatRepr((unsigned)Cfrom->getValue().getZExtValue()),
FloatRepresentation(
(unsigned)Cto_exponent->getValue().getZExtValue(),
(unsigned)Cto_significand->getValue().getZExtValue()),
isTruncate);
if (!res)
return false;
return true;
}
llvm_unreachable("??");
}

bool HandleBatch(CallInst *CI) {
Expand Down Expand Up @@ -2217,7 +2241,7 @@ class EnzymeBase {
#endif
RemapFunction(F, Mapping,
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
TruncatedFunc->deleteBody();
TruncatedFunc->eraseFromParent();
}
return true;
}
Expand Down
Loading
Loading