Skip to content

Commit

Permalink
More fprintf handling (#1844)
Browse files Browse the repository at this point in the history
Co-authored-by: William S. Moses <gh@wsmoses.com>
  • Loading branch information
Brant-Skywalker and wsmoses committed May 1, 2024
1 parent d5eac0f commit cbccccf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion enzyme/Enzyme/Enzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ llvm::cl::opt<std::string> EnzymeTruncateAll(
#endif
bool attributeKnownFunctions(llvm::Function &F) {
bool changed = false;
if (F.getName() == "fprintf") {
for (auto &arg : F.args()) {
if (arg.getType()->isPointerTy()) {
arg.addAttr(Attribute::NoCapture);
changed = true;
}
}
}
if (F.getName().contains("__enzyme_float") ||
F.getName().contains("__enzyme_double") ||
F.getName().contains("__enzyme_integer") ||
Expand Down Expand Up @@ -334,7 +342,8 @@ bool attributeKnownFunctions(llvm::Function &F) {
F.getName() == "_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_"
"createERmm" ||
F.getName() ==
"_ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEmmm") {
"_ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEmmm" ||
F.getName() == "fprintf") {
changed = true;
F.addAttribute(
AttributeList::FunctionIndex,
Expand Down
1 change: 1 addition & 0 deletions enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6364,6 +6364,7 @@ llvm::Function *EnzymeLogic::CreateNoFree(RequestContext context, Function *F) {
};

StringSet<> NoFrees = {"mpfr_greater_p",
"fprintf",
"memchr",
"time",
"strlen",
Expand Down
28 changes: 28 additions & 0 deletions enzyme/test/Enzyme/ReverseMode/fprintf.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; RUN: if [ %llvmver -ge 15 ]; then %opt < %s %OPnewLoadEnzyme -enzyme-preopt=false -passes="enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s; fi

@stdout = external local_unnamed_addr global ptr, align 8
@.str = private unnamed_addr constant [18 x i8] c" whatever ( %g )\0A\00", align 1

; Function Attrs: noinline nounwind optnone uwtable
define internal double @dotabs(double %ld) {
%r = call i32 (ptr, ptr, ...) @fprintf(ptr @stdout, ptr noundef @.str, double noundef %ld)
ret double %ld
}

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...)

; Function Attrs: noinline nounwind optnone uwtable
define void @f(ptr %x, ptr %dx) {
%r = call double (...) @__enzyme_autodiff(ptr @dotabs, double 1.0)
ret void
}

declare double @__enzyme_autodiff(...)

; CHECK: define internal { double } @diffedotabs(double %ld, double %differeturn)
; CHECK-NEXT: invert:
; CHECK-NEXT: %r = call i32 (ptr, ptr, ...) @fprintf(ptr @stdout, ptr noundef @.str, double noundef %ld)
; CHECK-NEXT: %0 = insertvalue { double } {{(undef|poison)}}, double %differeturn, 0
; CHECK-NEXT: ret { double } %0
; CHECK-NEXT: }

0 comments on commit cbccccf

Please sign in to comment.