Skip to content

Commit b13eed3

Browse files
authored
[SimpleGVN] Improve casting (#2620)
* [SimpleGVN] Improve casting * add test
1 parent 6a6ffa9 commit b13eed3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

enzyme/Enzyme/SimpleGVN.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ Value *extractValue(IRBuilder<> &Builder, Value *StoredVal, Type *LoadType,
115115
return StoredVal;
116116
}
117117

118+
if (RelativeOffset == 0 && isa<PointerType>(LoadType) &&
119+
isa<PointerType>(StoreType)) {
120+
return Builder.CreatePointerCast(StoredVal, LoadType);
121+
}
118122
// Handle extraction with offset or type mismatch
119123
// First, bitcast to an integer type if needed
120124
if (!StoreType->isIntegerTy()) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -simple-gvn -S | FileCheck %s; fi
2+
; RUN: %opt < %s %newLoadEnzyme -passes="simple-gvn" -S | FileCheck %s
3+
4+
define i8* @test_byte_extraction(i8** noalias nocapture %ptr, float* %x) {
5+
entry:
6+
%c = bitcast i8** %ptr to float**
7+
store float* %x, float** %c
8+
%val = load i8*, i8** %ptr
9+
ret i8* %val
10+
}
11+
12+
; CHECK: define i8* @test_byte_extraction(i8** noalias nocapture %ptr, float* %x) {
13+
; CHECK-NEXT: entry:
14+
; CHECK-NEXT: %c = bitcast i8** %ptr to float**
15+
; CHECK-NEXT: store float* %x, float** %c, align 8
16+
; CHECK-NEXT: %0 = bitcast float* %x to i8*
17+
; CHECK-NEXT: ret i8* %0
18+
; CHECK-NEXT: }

0 commit comments

Comments
 (0)