Skip to content

Commit 979b679

Browse files
KB Remote Agentclaude
andcommitted
Bitcast a moved stack slot to a pointee-changing addrspacecast's type
When a stack slot moves to the heap, RecursivelyReplaceAddressSpace replaced an addrspacecast into the new allocation's address space by the allocation itself. With typed pointers an addrspacecast may also change the pointee type: instcombine folds a bitcast into it, as in Julia's `addrspacecast T* %slot to i8 addrspace(11)*` for jl_object_id_. The call then received a pointer of the allocation's type and failed verification ("Call parameter type does not match function signature!"). Replace such a cast by a bitcast of the allocation to the cast's type. With opaque pointers the types are equal and nothing changes. Julia 1.10 hits this through Base.view of a SubArray of a custom AbstractArray inside a non-inlined function that holds it in a stack slot: view calls unalias, whose dataids take the objectid of the parent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KtEN3g4xbzjw9VURDXoFTE
1 parent 3a825a5 commit 979b679

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎enzyme/Enzyme/FunctionUtils.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,16 @@ void RecursivelyReplaceAddressSpace(
304304
if (auto ASC = dyn_cast<AddrSpaceCastInst>(inst)) {
305305
auto AS = cast<PointerType>(rep->getType())->getAddressSpace();
306306
if (AS == ASC->getDestAddressSpace()) {
307-
ASC->replaceAllUsesWith(rep);
307+
// With typed pointers an addrspacecast may also change the pointee
308+
// type (instcombine folds a bitcast into it, as Julia's
309+
// `addrspacecast T* %slot to i8 addrspace(11)*` for jl_object_id_),
310+
// so the moved pointer needs a bitcast to the cast's type.
311+
Value *nrep = rep;
312+
if (rep->getType() != ASC->getType()) {
313+
IRBuilder<> B(ASC);
314+
nrep = B.CreatePointerCast(rep, ASC->getType());
315+
}
316+
ASC->replaceAllUsesWith(nrep);
308317
assert(ASC);
309318
toErase.push_back(ASC);
310319
continue;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme -enzyme-preopt=false -S | FileCheck %s; fi
2+
; RUN: if [ %llvmver -lt 17 ]; then %opt < %s %newLoadEnzyme -passes="enzyme" -enzyme-preopt=false -S | FileCheck %s; fi
3+
4+
; A stack slot of a function differentiated as a callee moves to the heap.
5+
; With typed pointers an addrspacecast may also change the pointee type, as
6+
; Julia's `addrspacecast T* %slot to i8 addrspace(11)*` for jl_object_id_
7+
; does. Where the moved slot is already in the cast's address space, its uses
8+
; must get a bitcast of the new allocation to the cast's type: the
9+
; allocation's own type made a call that failed verification.
10+
11+
target datalayout = "e-m:e-i64:64-n32:64-A5"
12+
13+
declare i64 @hash_bytes(i8* nocapture readonly) "enzyme_inactive" readonly nofree nounwind willreturn
14+
15+
define double @callee(double %x) {
16+
entry:
17+
%slot = alloca { double, double }, align 8, addrspace(5)
18+
%f0 = getelementptr inbounds { double, double }, { double, double } addrspace(5)* %slot, i64 0, i32 0
19+
store double %x, double addrspace(5)* %f0, align 8
20+
%f1 = getelementptr inbounds { double, double }, { double, double } addrspace(5)* %slot, i64 0, i32 1
21+
store double 2.0, double addrspace(5)* %f1, align 8
22+
%whole = addrspacecast { double, double } addrspace(5)* %slot to i8*
23+
%h0 = call i64 @hash_bytes(i8* %whole)
24+
%second = addrspacecast double addrspace(5)* %f1 to i8*
25+
%h1 = call i64 @hash_bytes(i8* %second)
26+
%same = icmp eq i64 %h0, %h1
27+
%ld = load double, double addrspace(5)* %f0, align 8
28+
%sq = fmul double %ld, %ld
29+
%res = select i1 %same, double %x, double %sq
30+
ret double %res
31+
}
32+
33+
define double @outer(double %x) {
34+
entry:
35+
%m = call double @callee(double %x)
36+
%r = fmul double %m, %x
37+
ret double %r
38+
}
39+
40+
declare double @__enzyme_autodiff(i8*, double)
41+
42+
define double @test(double %x) {
43+
entry:
44+
%r = call double @__enzyme_autodiff(i8* bitcast (double (double)* @outer to i8*), double %x)
45+
ret double %r
46+
}
47+
48+
; CHECK: define internal { i1, double } @augmented_callee(double %x)
49+
; CHECK: %[[slot:[^ ]+]] = bitcast i8* %{{[^ ]+}} to { double, double }*, !enzyme_caststack
50+
; CHECK: %[[whole:[^ ]+]] = bitcast { double, double }* %[[slot]] to i8*
51+
; CHECK-NEXT: %h0 = call i64 @hash_bytes(i8* %[[whole]])
52+
; CHECK-NEXT: %[[second:[^ ]+]] = bitcast double* %f1 to i8*
53+
; CHECK-NEXT: %h1 = call i64 @hash_bytes(i8* %[[second]])

0 commit comments

Comments
 (0)