Skip to content

Commit 1e1e710

Browse files
wsmosesvchuravygiordanogbaraldi
authored
AllocOpt: Fix stack lowering where alloca continas boxed and unboxed data (#55306)
Co-authored-by: Valentin Churavy <[email protected]> Co-authored-by: Mosè Giordano <[email protected]> Co-authored-by: Gabriel Baraldi <[email protected]>
1 parent b0a8024 commit 1e1e710

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/llvm-alloc-helpers.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ bool AllocUseInfo::addMemOp(Instruction *inst, unsigned opno, uint32_t offset,
8888
memop.isaggr = isa<StructType>(elty) || isa<ArrayType>(elty) || isa<VectorType>(elty);
8989
memop.isobjref = hasObjref(elty);
9090
auto &field = getField(offset, size, elty);
91+
field.second.hasunboxed |= !hasObjref(elty) || (hasObjref(elty) && !isa<PointerType>(elty));
92+
9193
if (field.second.hasobjref != memop.isobjref)
9294
field.second.multiloc = true; // can't split this field, since it contains a mix of references and bits
9395
if (!isstore)
@@ -198,6 +200,7 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
198200
auto elty = inst->getType();
199201
required.use_info.has_unknown_objref |= hasObjref(elty);
200202
required.use_info.has_unknown_objrefaggr |= hasObjref(elty) && !isa<PointerType>(elty);
203+
required.use_info.has_unknown_unboxed |= !hasObjref(elty) || (hasObjref(elty) && !isa<PointerType>(elty));
201204
required.use_info.hasunknownmem = true;
202205
} else if (!required.use_info.addMemOp(inst, 0, cur.offset,
203206
inst->getType(),
@@ -289,6 +292,7 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
289292
auto elty = storev->getType();
290293
required.use_info.has_unknown_objref |= hasObjref(elty);
291294
required.use_info.has_unknown_objrefaggr |= hasObjref(elty) && !isa<PointerType>(elty);
295+
required.use_info.has_unknown_unboxed |= !hasObjref(elty) || (hasObjref(elty) && !isa<PointerType>(elty));
292296
required.use_info.hasunknownmem = true;
293297
} else if (!required.use_info.addMemOp(inst, use->getOperandNo(),
294298
cur.offset, storev->getType(),
@@ -310,10 +314,14 @@ void jl_alloc::runEscapeAnalysis(llvm::CallInst *I, EscapeAnalysisRequiredArgs r
310314
}
311315
required.use_info.hasload = true;
312316
auto storev = isa<AtomicCmpXchgInst>(inst) ? cast<AtomicCmpXchgInst>(inst)->getNewValOperand() : cast<AtomicRMWInst>(inst)->getValOperand();
317+
Type *elty = storev->getType();
313318
if (cur.offset == UINT32_MAX || !required.use_info.addMemOp(inst, use->getOperandNo(),
314-
cur.offset, storev->getType(),
319+
cur.offset, elty,
315320
true, required.DL)) {
316321
LLVM_DEBUG(dbgs() << "Atomic inst has unknown offset\n");
322+
required.use_info.has_unknown_objref |= hasObjref(elty);
323+
required.use_info.has_unknown_objrefaggr |= hasObjref(elty) && !isa<PointerType>(elty);
324+
required.use_info.has_unknown_unboxed |= !hasObjref(elty) || (hasObjref(elty) && !isa<PointerType>(elty));
317325
required.use_info.hasunknownmem = true;
318326
}
319327
required.use_info.refload = true;

src/llvm-alloc-helpers.h

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace jl_alloc {
4646
bool hasaggr:1;
4747
bool multiloc:1;
4848
bool hasload:1;
49+
// The alloc has a unboxed object at this offset.
50+
bool hasunboxed:1;
4951
llvm::Type *elty;
5052
llvm::SmallVector<MemOp,4> accesses;
5153
Field(uint32_t size, llvm::Type *elty)
@@ -54,6 +56,7 @@ namespace jl_alloc {
5456
hasaggr(false),
5557
multiloc(false),
5658
hasload(false),
59+
hasunboxed(false),
5760
elty(elty)
5861
{
5962
}
@@ -95,6 +98,9 @@ namespace jl_alloc {
9598
// The alloc has an aggregate Julia object reference not in an explicit field.
9699
bool has_unknown_objrefaggr:1;
97100

101+
// The alloc has an unboxed object at an unknown offset.
102+
bool has_unknown_unboxed:1;
103+
98104
void reset()
99105
{
100106
escaped = false;
@@ -110,6 +116,7 @@ namespace jl_alloc {
110116
allockind = llvm::AllocFnKind::Unknown;
111117
has_unknown_objref = false;
112118
has_unknown_objrefaggr = false;
119+
has_unknown_unboxed = false;
113120
uses.clear();
114121
preserves.clear();
115122
memops.clear();

src/llvm-alloc-opt.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ void Optimizer::optimizeAll()
252252
removeAlloc(orig);
253253
continue;
254254
}
255+
bool has_unboxed = use_info.has_unknown_unboxed;
255256
bool has_ref = use_info.has_unknown_objref;
256257
bool has_refaggr = use_info.has_unknown_objrefaggr;
257258
for (auto memop: use_info.memops) {
258259
auto &field = memop.second;
260+
has_unboxed |= field.hasunboxed;
259261
if (field.hasobjref) {
260262
has_ref = true;
261263
// This can be relaxed a little based on hasload
@@ -284,6 +286,19 @@ void Optimizer::optimizeAll()
284286
splitOnStack(orig);
285287
continue;
286288
}
289+
// The move to stack code below, if has_ref is set, changes the allocation to an array of jlvalue_t's. This is fine
290+
// if all objects are jlvalue_t's. However, if part of the allocation is an unboxed value (e.g. it is a { float, jlvaluet }),
291+
// then moveToStack will create a [2 x jlvaluet] bitcast to { float, jlvaluet }.
292+
// This later causes the GC rooting pass, to miss-characterize the float as a pointer to a GC value
293+
if (has_unboxed && has_ref) {
294+
REMARK([&]() {
295+
return OptimizationRemarkMissed(DEBUG_TYPE, "Escaped", orig)
296+
<< "GC allocation could not be split since it contains both boxed and unboxed values, unable to move to stack " << ore::NV("GC Allocation", orig);
297+
});
298+
if (use_info.hastypeof)
299+
optimizeTag(orig);
300+
continue;
301+
}
287302
REMARK([&](){
288303
return OptimizationRemark(DEBUG_TYPE, "Stack Move Allocation", orig)
289304
<< "GC allocation moved to stack " << ore::NV("GC Allocation", orig);

test/llvmpasses/alloc-opt-bits.ll

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
; RUN: opt --load-pass-plugin=libjulia-codegen%shlibext -passes='function(AllocOpt)' -S %s | FileCheck %s
4+
5+
6+
@tag = external addrspace(10) global {}
7+
8+
@glob = external addrspace(10) global {}
9+
10+
; Test that the gc_preserve intrinsics are deleted directly.
11+
12+
; CHECK-LABEL: @ptr_and_bits
13+
; CHECK-NOT: alloca
14+
; CHECK: call noalias ptr addrspace(10) @julia.gc_alloc_obj
15+
16+
define void @ptr_and_bits(ptr %fptr, i1 %b, i1 %b2, i32 %idx) {
17+
%pgcstack = call ptr @julia.get_pgcstack()
18+
%ptls = call ptr @julia.ptls_states()
19+
%ptls_i8 = bitcast ptr %ptls to ptr
20+
%v = call noalias ptr addrspace(10) @julia.gc_alloc_obj(ptr %ptls_i8, i64 16, ptr addrspace(10) @tag)
21+
22+
%g0 = getelementptr { i64, ptr addrspace(10) }, ptr addrspace(10) %v, i32 %idx, i32 1
23+
store ptr addrspace(10) @glob, ptr addrspace(10) %g0
24+
25+
%g1 = getelementptr { i64, ptr addrspace(10) }, ptr addrspace(10) %v, i32 %idx, i32 0
26+
store i64 7, ptr addrspace(10) %g1
27+
28+
%res = load ptr addrspace(10), ptr addrspace(10) %g0
29+
%res2 = load i64, ptr addrspace(10) %g1
30+
ret void
31+
}
32+
33+
declare noalias ptr addrspace(10) @julia.gc_alloc_obj(ptr, i64, ptr addrspace(10))
34+
35+
declare ptr @julia.ptls_states()
36+
37+
declare ptr @julia.get_pgcstack()

0 commit comments

Comments
 (0)