Skip to content

Commit

Permalink
JLInstSimplify correctly check for double pointer (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses committed May 27, 2024
1 parent 2115270 commit a5a0881
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
24 changes: 14 additions & 10 deletions enzyme/Enzyme/JLInstSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,23 +178,26 @@ bool jlInstSimplify(llvm::Function &F, TargetLibraryInfo &TLI,
auto lhs = getBaseObject(I.getOperand(0), /*offsetAllowed*/ false);
auto rhs = getBaseObject(I.getOperand(1), /*offsetAllowed*/ false);
if (lhs == rhs) {
I.replaceAllUsesWith(ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 1)
: ConstantInt::get(I.getType(), 0));
auto repval = ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 1)
: ConstantInt::get(I.getType(), 0);
I.replaceAllUsesWith(repval);
changed = true;
continue;
}
if ((isNoAlias(lhs) && (isNoAlias(rhs) || isa<Argument>(rhs))) ||
(isNoAlias(rhs) && isa<Argument>(lhs))) {
I.replaceAllUsesWith(ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 0)
: ConstantInt::get(I.getType(), 1));
auto repval = ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 0)
: ConstantInt::get(I.getType(), 1);
I.replaceAllUsesWith(repval);
changed = true;
continue;
}
auto llhs = dyn_cast<LoadInst>(lhs);
auto lrhs = dyn_cast<LoadInst>(rhs);
if (llhs && lrhs) {
if (llhs && lrhs && isa<PointerType>(llhs->getType()) &&
isa<PointerType>(lrhs->getType())) {
auto lhsv =
getBaseObject(llhs->getOperand(0), /*offsetAllowed*/ false);
auto rhsv =
Expand Down Expand Up @@ -238,9 +241,10 @@ bool jlInstSimplify(llvm::Function &F, TargetLibraryInfo &TLI,
}

if (legal && lhsv != rhsv) {
I.replaceAllUsesWith(ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 0)
: ConstantInt::get(I.getType(), 1));
auto repval = ICmpInst::isTrueWhenEqual(pred)
? ConstantInt::get(I.getType(), 0)
: ConstantInt::get(I.getType(), 1);
I.replaceAllUsesWith(repval);
changed = true;
continue;
}
Expand Down
4 changes: 3 additions & 1 deletion enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3143,6 +3143,8 @@ llvm::Value *get1ULP(llvm::IRBuilder<> &builder, llvm::Value *res) {
return absres;
}

void dumpModule(llvm::Module *mod) { llvm::errs() << *mod << "\n"; }

void dumpValue(llvm::Value *val) { llvm::errs() << *val << "\n"; }

void dumpType(llvm::Type *ty) { llvm::errs() << *ty << "\n"; }
void dumpType(llvm::Type *ty) { llvm::errs() << *ty << "\n"; }
22 changes: 22 additions & 0 deletions enzyme/test/Enzyme/JLSimplify/noi64.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -jl-inst-simplify -S | FileCheck %s; fi
; RUN: %opt < %s %newLoadEnzyme -passes="jl-inst-simplify" -S | FileCheck %s

declare i64* @malloc(i64)

define fastcc i1 @augmented_julia__affine_normalize_1484() {
%i5 = call noalias i64* @malloc(i64 16)
%i29 = load i64, i64* %i5, align 8
%i31 = call noalias nonnull i64 addrspace(10)* inttoptr (i64 137352001798896 to i64 addrspace(10)* ({} addrspace(10)*, i64, i64)*)({} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 137351863426640 to {}*) to {} addrspace(10)*), i64 10, i64 10)
%i35 = load i64, i64 addrspace(10)* %i31, align 8
%i39 = icmp ne i64 %i35, %i29
ret i1 %i39
}

; CHECK: define fastcc i1 @augmented_julia__affine_normalize_1484() {
; CHECK-NEXT: %i5 = call noalias i64* @malloc(i64 16)
; CHECK-NEXT: %i29 = load i64, i64* %i5, align 8
; CHECK-NEXT: %i31 = call noalias nonnull i64 addrspace(10)* inttoptr (i64 137352001798896 to i64 addrspace(10)* ({} addrspace(10)*, i64, i64)*)({} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 137351863426640 to {}*) to {} addrspace(10)*), i64 10, i64 10)
; CHECK-NEXT: %i35 = load i64, i64 addrspace(10)* %i31, align 8
; CHECK-NEXT: %i39 = icmp ne i64 %i35, %i29
; CHECK-NEXT: ret i1 %i39
; CHECK-NEXT: }
15 changes: 15 additions & 0 deletions enzyme/test/Enzyme/JLSimplify/yesptr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -jl-inst-simplify -S | FileCheck %s; fi
; RUN: %opt < %s %newLoadEnzyme -passes="jl-inst-simplify" -S | FileCheck %s

declare i8** @malloc(i64)

define fastcc i1 @augmented_julia__affine_normalize_1484() {
%i5 = call noalias i8** @malloc(i64 16)
%i29 = load i8*, i8** %i5, align 8
%i31 = call noalias nonnull i8* addrspace(10)* inttoptr (i64 137352001798896 to i8* addrspace(10)* ({} addrspace(10)*, i64, i64)*)({} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 137351863426640 to {}*) to {} addrspace(10)*), i64 10, i64 10)
%i35 = load i8*, i8* addrspace(10)* %i31, align 8
%i39 = icmp ne i8* %i35, %i29
ret i1 %i39
}

; CHECK: ret i1 true

0 comments on commit a5a0881

Please sign in to comment.