Skip to content

Commit

Permalink
Initialize hoisted object allocations (#49584)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed May 4, 2023
1 parent 93ce36c commit 5032a1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/llvm-julia-licm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ struct JuliaLICMPassLegacy : public LoopPass {
getLoopAnalysisUsage(AU);
}
};

struct JuliaLICM : public JuliaPassContext {
function_ref<DominatorTree &()> GetDT;
function_ref<LoopInfo &()> GetLI;
Expand Down Expand Up @@ -339,6 +338,19 @@ struct JuliaLICM : public JuliaPassContext {
});
++HoistedAllocation;
moveInstructionBefore(*call, *preheader->getTerminator(), MSSAU, SE);
IRBuilder<> builder(preheader->getTerminator());
builder.SetCurrentDebugLocation(call->getDebugLoc());
auto obj_i8 = builder.CreateBitCast(call, Type::getInt8PtrTy(call->getContext(), call->getType()->getPointerAddressSpace()));
// Note that this alignment is assuming the GC allocates at least pointer-aligned memory
auto align = Align(DL.getPointerSize(0));
auto clear_obj = builder.CreateMemSet(obj_i8, ConstantInt::get(Type::getInt8Ty(call->getContext()), 0), call->getArgOperand(1), align);
if (MSSAU.getMemorySSA()) {
auto alloc_mdef = MSSAU.getMemorySSA()->getMemoryAccess(call);
assert(isa<MemoryDef>(alloc_mdef) && "Expected alloc to be associated with a memory def!");
auto clear_mdef = MSSAU.createMemoryAccessAfter(clear_obj, nullptr, alloc_mdef);
assert(isa<MemoryDef>(clear_mdef) && "Expected memset to be associated with a memory def!");
(void) clear_mdef;
}
changed = true;
}
}
Expand Down Expand Up @@ -395,6 +407,10 @@ PreservedAnalyses JuliaLICMPass::run(Loop &L, LoopAnalysisManager &AM,
};
auto juliaLICM = JuliaLICM(GetDT, GetLI, GetMSSA, GetSE);
if (juliaLICM.runOnLoop(&L, ORE)) {
#ifdef JL_DEBUG_BUILD
if (AR.MSSA)
AR.MSSA->verifyMemorySSA();
#endif
auto preserved = getLoopPassPreservedAnalyses();
preserved.preserveSet<CFGAnalyses>();
preserved.preserve<MemorySSAAnalysis>();
Expand Down
8 changes: 6 additions & 2 deletions test/llvmpasses/julia-licm.ll
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ L4: ; preds = %top
%current_task112 = getelementptr inbounds {}**, {}*** %1, i64 -12
%current_task1 = bitcast {}*** %current_task112 to {}**
; CHECK: %3 = call noalias nonnull {} addrspace(10)* @julia.gc_alloc_obj({}** nonnull %current_task1, i64 8, {} addrspace(10)* @tag)
; CHECK-NEXT: %4 = bitcast {} addrspace(10)* %3 to i8 addrspace(10)*
; CHECK-NEXT: call void @llvm.memset.p10i8.i64(i8 addrspace(10)* align {{[0-9]+}} %4, i8 0, i64 8, i1 false)
; CHECK-NEXT: br label %L22
br label %L22

L22: ; preds = %L4, %L22
%value_phi5 = phi i64 [ 1, %L4 ], [ %5, %L22 ]
; CHECK: %value_phi5 = phi i64 [ 1, %L4 ], [ %5, %L22 ]
; CHECK-NEXT %4 = bitcast {} addrspace(10)* %3 to i64 addrspace(10)*
; CHECK: %value_phi5 = phi i64 [ 1, %L4 ], [ %6, %L22 ]
; CHECK-NEXT %5 = bitcast {} addrspace(10)* %3 to i64 addrspace(10)*
%3 = call noalias nonnull {} addrspace(10)* @julia.gc_alloc_obj({}** nonnull %current_task1, i64 8, {} addrspace(10)* @tag) #1
%4 = bitcast {} addrspace(10)* %3 to i64 addrspace(10)*
store i64 %value_phi5, i64 addrspace(10)* %4, align 8, !tbaa !2
Expand All @@ -55,6 +57,8 @@ top:
; CHECK: preheader:
preheader:
; CHECK-NEXT: %alloc = call noalias nonnull {} addrspace(10)* @julia.gc_alloc_obj({}** nonnull %current_task, i64 8, {} addrspace(10)* @tag)
; CHECK-NEXT: [[casted:%.*]] = bitcast {} addrspace(10)* %alloc to i8 addrspace(10)*
; CHECK-NEXT: call void @llvm.memset.p10i8.i64(i8 addrspace(10)* align {{[0-9]+}} [[casted]], i8 0, i64 8, i1 false)
; CHECK-NEXT: br label %loop
br label %loop
loop:
Expand Down

0 comments on commit 5032a1a

Please sign in to comment.