Skip to content

Commit 7b18f9c

Browse files
committed
codegen: NFC refactoring to use Align type
(cherry picked from commit f38015f)
1 parent 9723ff1 commit 7b18f9c

File tree

4 files changed

+41
-39
lines changed

4 files changed

+41
-39
lines changed

src/ccall.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,8 @@ static Value *julia_to_native(
551551
// pass the address of an alloca'd thing, not a box
552552
// since those are immutable.
553553
Value *slot = emit_static_alloca(ctx, to);
554-
unsigned align = julia_alignment(jlto);
555-
cast<AllocaInst>(slot)->setAlignment(Align(align));
554+
Align align(julia_alignment(jlto));
555+
cast<AllocaInst>(slot)->setAlignment(align);
556556
setName(ctx.emission_context, slot, "native_convert_buffer");
557557
if (!jvinfo.ispointer()) {
558558
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, jvinfo.tbaa);
@@ -2091,7 +2091,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
20912091
Value *strct = emit_allocobj(ctx, (jl_datatype_t*)rt, true);
20922092
setName(ctx.emission_context, strct, "ccall_ret_box");
20932093
MDNode *tbaa = jl_is_mutable(rt) ? ctx.tbaa().tbaa_mutab : ctx.tbaa().tbaa_immut;
2094-
int boxalign = julia_alignment(rt);
2094+
Align boxalign(julia_alignment(rt));
20952095
// copy the data from the return value to the new struct
20962096
const DataLayout &DL = ctx.builder.GetInsertBlock()->getModule()->getDataLayout();
20972097
auto resultTy = result->getType();
@@ -2101,8 +2101,8 @@ jl_cgval_t function_sig_t::emit_a_ccall(
21012101
// When this happens, cast through memory.
21022102
auto slot = emit_static_alloca(ctx, resultTy);
21032103
setName(ctx.emission_context, slot, "type_pun_slot");
2104-
slot->setAlignment(Align(boxalign));
2105-
ctx.builder.CreateAlignedStore(result, slot, Align(boxalign));
2104+
slot->setAlignment(boxalign);
2105+
ctx.builder.CreateAlignedStore(result, slot, boxalign);
21062106
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa);
21072107
emit_memcpy(ctx, strct, ai, slot, ai, rtsz, boxalign, boxalign);
21082108
}

src/cgutils.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static Value *emit_pointer_from_objref(jl_codectx_t &ctx, Value *V)
310310
}
311311

312312
static Value *emit_unbox(jl_codectx_t &ctx, Type *to, const jl_cgval_t &x, jl_value_t *jt);
313-
static void emit_unbox_store(jl_codectx_t &ctx, const jl_cgval_t &x, Value* dest, MDNode *tbaa_dest, unsigned alignment, bool isVolatile=false);
313+
static void emit_unbox_store(jl_codectx_t &ctx, const jl_cgval_t &x, Value* dest, MDNode *tbaa_dest, Align alignment, bool isVolatile=false);
314314

315315
static Value *get_gc_root_for(jl_codectx_t &ctx, const jl_cgval_t &x)
316316
{
@@ -980,11 +980,10 @@ static Value *data_pointer(jl_codectx_t &ctx, const jl_cgval_t &x)
980980
}
981981

982982
static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const &dst_ai, Value *src,
983-
jl_aliasinfo_t const &src_ai, uint64_t sz, unsigned align_dst, unsigned align_src, bool is_volatile)
983+
jl_aliasinfo_t const &src_ai, uint64_t sz, Align align_dst, Align align_src, bool is_volatile)
984984
{
985985
if (sz == 0)
986986
return;
987-
assert(align_dst && "align must be specified");
988987
// If the types are small and simple, use load and store directly.
989988
// Going through memcpy can cause LLVM (e.g. SROA) to create bitcasts between float and int
990989
// that interferes with other optimizations.
@@ -1026,7 +1025,7 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const
10261025
if (isa<Instruction>(dst) && !dst->hasName())
10271026
setName(ctx.emission_context, dst, "memcpy_refined_dst");
10281027
auto val = src_ai.decorateInst(ctx.builder.CreateAlignedLoad(directel, src, MaybeAlign(align_src), is_volatile));
1029-
dst_ai.decorateInst(ctx.builder.CreateAlignedStore(val, dst, Align(align_dst), is_volatile));
1028+
dst_ai.decorateInst(ctx.builder.CreateAlignedStore(val, dst, align_dst, is_volatile));
10301029
++SkippedMemcpys;
10311030
return;
10321031
}
@@ -1044,12 +1043,12 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const
10441043
// above problem won't be as serious.
10451044

10461045
auto merged_ai = dst_ai.merge(src_ai);
1047-
ctx.builder.CreateMemCpy(dst, Align(align_dst), src, Align(align_src), sz, is_volatile,
1046+
ctx.builder.CreateMemCpy(dst, align_dst, src, align_src, sz, is_volatile,
10481047
merged_ai.tbaa, merged_ai.tbaa_struct, merged_ai.scope, merged_ai.noalias);
10491048
}
10501049

10511050
static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const &dst_ai, Value *src,
1052-
jl_aliasinfo_t const &src_ai, Value *sz, unsigned align_dst, unsigned align_src, bool is_volatile)
1051+
jl_aliasinfo_t const &src_ai, Value *sz, Align align_dst, Align align_src, bool is_volatile)
10531052
{
10541053
if (auto const_sz = dyn_cast<ConstantInt>(sz)) {
10551054
emit_memcpy_llvm(ctx, dst, dst_ai, src, src_ai, const_sz->getZExtValue(), align_dst, align_src, is_volatile);
@@ -1058,20 +1057,20 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const
10581057
++EmittedMemcpys;
10591058

10601059
auto merged_ai = dst_ai.merge(src_ai);
1061-
ctx.builder.CreateMemCpy(dst, MaybeAlign(align_dst), src, MaybeAlign(align_src), sz, is_volatile,
1060+
ctx.builder.CreateMemCpy(dst, align_dst, src, align_src, sz, is_volatile,
10621061
merged_ai.tbaa, merged_ai.tbaa_struct, merged_ai.scope, merged_ai.noalias);
10631062
}
10641063

10651064
template<typename T1>
10661065
static void emit_memcpy(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const &dst_ai, Value *src,
1067-
jl_aliasinfo_t const &src_ai, T1 &&sz, unsigned align_dst, unsigned align_src, bool is_volatile=false)
1066+
jl_aliasinfo_t const &src_ai, T1 &&sz, Align align_dst, Align align_src, bool is_volatile=false)
10681067
{
10691068
emit_memcpy_llvm(ctx, dst, dst_ai, src, src_ai, sz, align_dst, align_src, is_volatile);
10701069
}
10711070

10721071
template<typename T1>
10731072
static void emit_memcpy(jl_codectx_t &ctx, Value *dst, jl_aliasinfo_t const &dst_ai, const jl_cgval_t &src,
1074-
T1 &&sz, unsigned align_dst, unsigned align_src, bool is_volatile=false)
1073+
T1 &&sz, Align align_dst, Align align_src, bool is_volatile=false)
10751074
{
10761075
auto src_ai = jl_aliasinfo_t::fromTBAA(ctx, src.tbaa);
10771076
emit_memcpy_llvm(ctx, dst, dst_ai, data_pointer(ctx, src), src_ai, sz, align_dst, align_src, is_volatile);
@@ -1972,7 +1971,7 @@ static jl_cgval_t typed_load(jl_codectx_t &ctx, Value *ptr, Value *idx_0based, j
19721971
else if (!alignment)
19731972
alignment = julia_alignment(jltype);
19741973
if (intcast && Order == AtomicOrdering::NotAtomic) {
1975-
emit_memcpy(ctx, intcast, jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack), data, jl_aliasinfo_t::fromTBAA(ctx, tbaa), nb, alignment, intcast->getAlign().value());
1974+
emit_memcpy(ctx, intcast, jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack), data, jl_aliasinfo_t::fromTBAA(ctx, tbaa), nb, Align(alignment), intcast->getAlign());
19761975
}
19771976
else {
19781977
if (!isboxed && jl_is_genericmemoryref_type(jltype)) {
@@ -2152,7 +2151,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
21522151
}
21532152
else {
21542153
assert(Order == AtomicOrdering::NotAtomic && !isboxed && rhs.typ == jltype);
2155-
emit_unbox_store(ctx, rhs, ptr, tbaa, alignment);
2154+
emit_unbox_store(ctx, rhs, ptr, tbaa, Align(alignment));
21562155
}
21572156
}
21582157
else if (isswapfield) {
@@ -2304,7 +2303,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
23042303
}
23052304
else {
23062305
assert(!isboxed && rhs.typ == jltype);
2307-
emit_unbox_store(ctx, rhs, ptr, tbaa, alignment);
2306+
emit_unbox_store(ctx, rhs, ptr, tbaa, Align(alignment));
23082307
}
23092308
ctx.builder.CreateBr(DoneBB);
23102309
instr = load;
@@ -2617,7 +2616,7 @@ static jl_cgval_t emit_unionload(jl_codectx_t &ctx, Value *addr, Value *ptindex,
26172616
if (al > 1)
26182617
lv->setAlignment(Align(al));
26192618
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa);
2620-
emit_memcpy(ctx, lv, ai, addr, ai, fsz, al, al);
2619+
emit_memcpy(ctx, lv, ai, addr, ai, fsz, Align(al), Align(al));
26212620
addr = lv;
26222621
}
26232622
return mark_julia_slot(fsz > 0 ? addr : nullptr, jfty, tindex, tbaa);
@@ -2960,20 +2959,20 @@ static Value *emit_genericmemoryowner(jl_codectx_t &ctx, Value *t)
29602959
static Value *emit_allocobj(jl_codectx_t &ctx, jl_datatype_t *jt, bool fully_initialized);
29612960

29622961
static void init_bits_value(jl_codectx_t &ctx, Value *newv, Value *v, MDNode *tbaa,
2963-
unsigned alignment = sizeof(void*)) // min alignment in julia's gc is pointer-aligned
2962+
Align alignment = Align(sizeof(void*))) // min alignment in julia's gc is pointer-aligned
29642963
{
29652964
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa);
29662965
// newv should already be tagged
29672966
ai.decorateInst(ctx.builder.CreateAlignedStore(v, emit_bitcast(ctx, newv,
2968-
PointerType::get(v->getType(), 0)), Align(alignment)));
2967+
PointerType::get(v->getType(), 0)), alignment));
29692968
}
29702969

29712970
static void init_bits_cgval(jl_codectx_t &ctx, Value *newv, const jl_cgval_t& v, MDNode *tbaa)
29722971
{
29732972
// newv should already be tagged
29742973
if (v.ispointer()) {
29752974
unsigned align = std::max(julia_alignment(v.typ), (unsigned)sizeof(void*));
2976-
emit_memcpy(ctx, newv, jl_aliasinfo_t::fromTBAA(ctx, tbaa), v, jl_datatype_size(v.typ), align, julia_alignment(v.typ));
2975+
emit_memcpy(ctx, newv, jl_aliasinfo_t::fromTBAA(ctx, tbaa), v, jl_datatype_size(v.typ), Align(align), Align(julia_alignment(v.typ)));
29772976
}
29782977
else {
29792978
init_bits_value(ctx, newv, v.V, tbaa);
@@ -3432,7 +3431,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
34323431
if (jl_is_pointerfree(typ)) {
34333432
unsigned alignment = julia_alignment(typ);
34343433
if (!src.ispointer() || src.constant) {
3435-
emit_unbox_store(ctx, src, dest, tbaa_dst, alignment, isVolatile);
3434+
emit_unbox_store(ctx, src, dest, tbaa_dst, Align(alignment), isVolatile);
34363435
}
34373436
else {
34383437
Value *src_ptr = data_pointer(ctx, src);
@@ -3442,7 +3441,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
34423441
// if (skip) src_ptr = ctx.builder.CreateSelect(skip, dest, src_ptr);
34433442
auto f = [&] {
34443443
(void)emit_memcpy(ctx, dest, jl_aliasinfo_t::fromTBAA(ctx, tbaa_dst), src_ptr,
3445-
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), nb, alignment, alignment, isVolatile);
3444+
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), nb, Align(alignment), Align(alignment), isVolatile);
34463445
return nullptr;
34473446
};
34483447
if (skip)
@@ -3479,7 +3478,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
34793478
return;
34803479
} else {
34813480
emit_memcpy(ctx, dest, jl_aliasinfo_t::fromTBAA(ctx, tbaa_dst), src_ptr,
3482-
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), nb, alignment, alignment, isVolatile);
3481+
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), nb, Align(alignment), Align(alignment), isVolatile);
34833482
}
34843483
}
34853484
ctx.builder.CreateBr(postBB);
@@ -3505,7 +3504,7 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
35053504
Value *datatype = emit_typeof(ctx, src, false, false);
35063505
Value *copy_bytes = emit_datatype_size(ctx, datatype);
35073506
(void)emit_memcpy(ctx, dest, jl_aliasinfo_t::fromTBAA(ctx, tbaa_dst), data_pointer(ctx, src),
3508-
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), copy_bytes, 1, 1, isVolatile);
3507+
jl_aliasinfo_t::fromTBAA(ctx, src.tbaa), copy_bytes, Align(1), Align(1), isVolatile);
35093508
return nullptr;
35103509
};
35113510
if (skip)
@@ -3922,7 +3921,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
39223921
} else if (init_as_value) {
39233922
fval = emit_unbox(ctx, fty, fval_info, jtype);
39243923
} else {
3925-
emit_unbox_store(ctx, fval_info, dest, ctx.tbaa().tbaa_stack, jl_field_align(sty, i));
3924+
emit_unbox_store(ctx, fval_info, dest, ctx.tbaa().tbaa_stack, Align(jl_field_align(sty, i)));
39263925
}
39273926
}
39283927
if (init_as_value) {

src/codegen.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5512,7 +5512,7 @@ static jl_cgval_t emit_varinfo(jl_codectx_t &ctx, jl_varinfo_t &vi, jl_sym_t *va
55125512
else {
55135513
const DataLayout &DL = jl_Module->getDataLayout();
55145514
uint64_t sz = DL.getTypeStoreSize(T);
5515-
emit_memcpy(ctx, ssaslot, jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack), vi.value, sz, ssaslot->getAlign().value(), varslot->getAlign().value());
5515+
emit_memcpy(ctx, ssaslot, jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack), vi.value, sz, ssaslot->getAlign(), varslot->getAlign());
55165516
}
55175517
Value *tindex = NULL;
55185518
if (vi.pTIndex)
@@ -5620,8 +5620,9 @@ static void emit_vi_assignment_unboxed(jl_codectx_t &ctx, jl_varinfo_t &vi, Valu
56205620
// This check should probably mostly catch the relevant situations.
56215621
if (vi.value.V != rval_info.V) {
56225622
Value *copy_bytes = ConstantInt::get(getInt32Ty(ctx.builder.getContext()), jl_datatype_size(vi.value.typ));
5623+
Align alignment(julia_alignment(rval_info.typ));
56235624
emit_memcpy(ctx, vi.value.V, jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack), rval_info, copy_bytes,
5624-
julia_alignment(rval_info.typ), julia_alignment(rval_info.typ), vi.isVolatile);
5625+
alignment, alignment, vi.isVolatile);
56255626
}
56265627
}
56275628
else {
@@ -6698,8 +6699,9 @@ static void emit_cfunc_invalidate(
66986699
root1 = ctx.builder.CreateConstInBoundsGEP2_32(get_returnroots_type(ctx, return_roots), root1, 0, 0);
66996700
ctx.builder.CreateStore(gf_ret, root1);
67006701
}
6702+
Align alignment(julia_alignment(rettype));
67016703
emit_memcpy(ctx, &*gf_thunk->arg_begin(), jl_aliasinfo_t::fromTBAA(ctx, nullptr), gf_ret,
6702-
jl_aliasinfo_t::fromTBAA(ctx, nullptr), jl_datatype_size(rettype), julia_alignment(rettype), julia_alignment(rettype));
6704+
jl_aliasinfo_t::fromTBAA(ctx, nullptr), jl_datatype_size(rettype), Align(alignment), Align(alignment));
67036705
ctx.builder.CreateRetVoid();
67046706
break;
67056707
}
@@ -8566,7 +8568,7 @@ static jl_llvm_functions_t
85668568

85678569
jl_cgval_t closure_world = typed_load(ctx, worldaddr, NULL, (jl_value_t*)jl_long_type,
85688570
nullptr, nullptr, false, AtomicOrdering::NotAtomic, false, ctx.types().alignof_ptr.value());
8569-
emit_unbox_store(ctx, closure_world, world_age_field, ctx.tbaa().tbaa_gcframe, ctx.types().alignof_ptr.value());
8571+
emit_unbox_store(ctx, closure_world, world_age_field, ctx.tbaa().tbaa_gcframe, ctx.types().alignof_ptr);
85708572

85718573
// Load closure env
85728574
Value *envaddr = ctx.builder.CreateInBoundsGEP(
@@ -9073,8 +9075,9 @@ static jl_llvm_functions_t
90739075
}
90749076
if (returninfo.cc == jl_returninfo_t::SRet) {
90759077
assert(jl_is_concrete_type(jlrettype));
9078+
Align alignment(julia_alignment(jlrettype));
90769079
emit_memcpy(ctx, sret, jl_aliasinfo_t::fromTBAA(ctx, nullptr), retvalinfo,
9077-
jl_datatype_size(jlrettype), julia_alignment(jlrettype), julia_alignment(jlrettype));
9080+
jl_datatype_size(jlrettype), alignment, alignment);
90789081
}
90799082
else { // must be jl_returninfo_t::Union
90809083
emit_unionmove(ctx, sret, nullptr, retvalinfo, /*skip*/isboxed_union);
@@ -9307,7 +9310,7 @@ static jl_llvm_functions_t
93079310
// load of val) if the runtime type of val isn't phiType
93089311
Value *isvalid = emit_isa_and_defined(ctx, val, phiType);
93099312
emit_guarded_test(ctx, isvalid, nullptr, [&] {
9310-
emit_unbox_store(ctx, update_julia_type(ctx, val, phiType), dest, ctx.tbaa().tbaa_stack, julia_alignment(phiType));
9313+
emit_unbox_store(ctx, update_julia_type(ctx, val, phiType), dest, ctx.tbaa().tbaa_stack, Align(julia_alignment(phiType)));
93119314
return nullptr;
93129315
});
93139316
}
@@ -9334,7 +9337,7 @@ static jl_llvm_functions_t
93349337
if (VN)
93359338
V = Constant::getNullValue(ctx.types().T_prjlvalue);
93369339
if (dest)
9337-
emit_unbox_store(ctx, val, dest, ctx.tbaa().tbaa_stack, julia_alignment(val.typ));
9340+
emit_unbox_store(ctx, val, dest, ctx.tbaa().tbaa_stack, Align(julia_alignment(val.typ)));
93389341
RTindex = ConstantInt::get(getInt8Ty(ctx.builder.getContext()), tindex);
93399342
}
93409343
}

0 commit comments

Comments
 (0)