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

[Truncate] Corrently handle constant returns #1848

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5281,6 +5281,7 @@ class TruncateGenerator : public llvm::InstVisitor<TruncateGenerator>,
SI.isVolatile(), SI.getOrdering(), SI.getSyncScopeID(),
/*mask=*/nullptr);
}
// TODO Is there a possibility we GEP a const and get a FP value?
void visitGetElementPtrInst(llvm::GetElementPtrInst &gep) { return; }
void visitPHINode(llvm::PHINode &phi) { return; }
void visitCastInst(llvm::CastInst &CI) {
Expand Down Expand Up @@ -5431,11 +5432,20 @@ class TruncateGenerator : public llvm::InstVisitor<TruncateGenerator>,
newI->eraseFromParent();
return true;
}

void visitIntrinsicInst(llvm::IntrinsicInst &II) {
handleIntrinsic(II, II.getIntrinsicID());
}

void visitReturnInst(llvm::ReturnInst &I) { return; }
void visitReturnInst(llvm::ReturnInst &I) {
auto newI = cast<llvm::ReturnInst>(getNewFromOriginal(&I));
if (newI->getNumOperands() == 0)
return;
IRBuilder<> B(newI);
if (isa<ConstantFP>(newI->getOperand(0)))
newI->setOperand(0, createFPRTConstCall(B, newI->getReturnValue()));
return;
}

void visitBranchInst(llvm::BranchInst &I) { return; }
void visitSwitchInst(llvm::SwitchInst &I) { return; }
Expand Down
18 changes: 9 additions & 9 deletions enzyme/test/Integration/Truncate/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ int main() {
double trunc = __enzyme_expand_mem_value(__enzyme_truncate_mem_func(intrinsics, FROM, TO)(a, b), FROM, TO);
APPROX_EQ(trunc, truth, 1e-5);
}
{
double a = 2;
double b = 3;
double truth = constt(a, b);
a = __enzyme_truncate_mem_value(a, FROM, TO);
b = __enzyme_truncate_mem_value(b, FROM, TO);
double trunc = __enzyme_expand_mem_value(__enzyme_truncate_mem_func(constt, FROM, TO)(a, b), FROM, TO);
APPROX_EQ(trunc, truth, 1e-5);
}
#endif
// {
// double a = 2;
// double b = 3;
// double truth = intrinsics(a, b);
// a = __enzyme_truncate_mem_value(a, FROM, TO);
// b = __enzyme_truncate_mem_value(b, FROM, TO);
// double trunc = __enzyme_expand_mem_value(__enzyme_truncate_mem_func(constt, FROM, TO)(a, b), FROM, TO);
// APPROX_EQ(trunc, truth, 1e-5);
// }

#ifdef TRUNC_OP
{
Expand Down
Loading