Skip to content

Commit 144ebba

Browse files
authored
Support LLVM 14 (upstream Git main) (#43628)
Compiles against llvm/llvm-project#2ec3ca747732, with some of our unmerged local patches from 13.x still required. Unfortunately, there is quite a bit of fallout from the various attribute API renames. I chose to introduce some function shims to separate out all the preprocessor #if clutter.
1 parent 40d3596 commit 144ebba

16 files changed

+287
-129
lines changed

src/aotcompile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include <llvm/Analysis/TargetLibraryInfo.h>
99
#include <llvm/Analysis/TargetTransformInfo.h>
1010
#include <llvm/IR/DataLayout.h>
11+
#if JL_LLVM_VERSION >= 140000
12+
#include <llvm/MC/TargetRegistry.h>
13+
#else
1114
#include <llvm/Support/TargetRegistry.h>
15+
#endif
1216
#include <llvm/Target/TargetMachine.h>
1317

1418
// analysis passes

src/ccall.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ static GlobalVariable *emit_plt_thunk(
223223
// NoReturn function can trigger LLVM verifier error when declared as
224224
// MustTail since other passes might replace the `ret` with
225225
// `unreachable` (LLVM should probably accept `unreachable`).
226-
if (attrs.hasAttribute(AttributeList::FunctionIndex,
227-
Attribute::NoReturn)) {
226+
if (hasFnAttr(attrs, Attribute::NoReturn)) {
228227
irbuilder.CreateUnreachable();
229228
}
230229
else {
@@ -272,7 +271,7 @@ static Value *emit_plt(
272271
functype, attrs, cc, f_lib, f_name, libptrgv, llvmgv, runtime_lib);
273272
}
274273
GlobalVariable *got = prepare_global_in(jl_Module, sharedgot);
275-
LoadInst *got_val = ctx.builder.CreateAlignedLoad(got, Align(sizeof(void*)));
274+
LoadInst *got_val = ctx.builder.CreateAlignedLoad(got->getType()->getElementType(), got, Align(sizeof(void*)));
276275
// See comment in `runtime_sym_lookup` above. This in principle needs a
277276
// consume ordering too. This is even less likely to cause issues though
278277
// since the only thing we do to this loaded pointer is to call it
@@ -404,7 +403,7 @@ static Value *llvm_type_rewrite(
404403
to = emit_bitcast(ctx, from, target_type->getPointerTo());
405404
}
406405
ctx.builder.CreateAlignedStore(v, from, Align(align));
407-
return ctx.builder.CreateAlignedLoad(to, Align(align));
406+
return ctx.builder.CreateAlignedLoad(target_type, to, Align(align));
408407
}
409408

410409
// --- argument passing and scratch space utilities ---
@@ -422,8 +421,7 @@ static Value *runtime_apply_type_env(jl_codectx_t &ctx, jl_value_t *ty)
422421
ConstantInt::get(T_size, sizeof(jl_svec_t) / sizeof(jl_value_t*)))
423422
};
424423
auto call = ctx.builder.CreateCall(prepare_call(jlapplytype_func), makeArrayRef(args));
425-
call->addAttribute(AttributeList::ReturnIndex,
426-
Attribute::getWithAlignment(jl_LLVMContext, Align(16)));
424+
addRetAttr(call, Attribute::getWithAlignment(jl_LLVMContext, Align(16)));
427425
return call;
428426
}
429427

@@ -1125,16 +1123,13 @@ std::string generate_func_sig(const char *fname)
11251123
const auto &as = paramattrs.at(i);
11261124
if (!as.hasAttributes())
11271125
continue;
1128-
attributes = attributes.addAttributes(jl_LLVMContext, i + 1, as);
1126+
attributes = addAttributesAtIndex(attributes, jl_LLVMContext, i + 1, as);
11291127
}
11301128
// If return value is boxed it must be non-null.
11311129
if (retboxed)
1132-
attributes = attributes.addAttribute(jl_LLVMContext, AttributeList::ReturnIndex,
1133-
Attribute::NonNull);
1130+
attributes = addRetAttribute(attributes, jl_LLVMContext, Attribute::NonNull);
11341131
if (rt == jl_bottom_type) {
1135-
attributes = attributes.addAttribute(jl_LLVMContext,
1136-
AttributeList::FunctionIndex,
1137-
Attribute::NoReturn);
1132+
attributes = addFnAttribute(attributes, jl_LLVMContext, Attribute::NoReturn);
11381133
}
11391134
return "";
11401135
}
@@ -1487,8 +1482,8 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
14871482
JL_GC_POP();
14881483
Value *ptask_i16 = emit_bitcast(ctx, get_current_task(ctx), T_pint16);
14891484
const int tid_offset = offsetof(jl_task_t, tid);
1490-
Value *ptid = ctx.builder.CreateInBoundsGEP(ptask_i16, ConstantInt::get(T_size, tid_offset / sizeof(int16_t)));
1491-
LoadInst *tid = ctx.builder.CreateAlignedLoad(ptid, Align(sizeof(int16_t)));
1485+
Value *ptid = ctx.builder.CreateInBoundsGEP(T_int16, ptask_i16, ConstantInt::get(T_size, tid_offset / sizeof(int16_t)));
1486+
LoadInst *tid = ctx.builder.CreateAlignedLoad(T_int16, ptid, Align(sizeof(int16_t)));
14921487
tbaa_decorate(tbaa_gcframe, tid);
14931488
return mark_or_box_ccall_result(ctx, tid, retboxed, rt, unionall, static_rt);
14941489
}
@@ -1500,8 +1495,8 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
15001495
JL_GC_POP();
15011496
Value *ptls_i32 = emit_bitcast(ctx, get_current_ptls(ctx), T_pint32);
15021497
const int finh_offset = offsetof(jl_tls_states_t, finalizers_inhibited);
1503-
Value *pfinh = ctx.builder.CreateInBoundsGEP(ptls_i32, ConstantInt::get(T_size, finh_offset / 4));
1504-
LoadInst *finh = ctx.builder.CreateAlignedLoad(pfinh, Align(sizeof(int32_t)));
1498+
Value *pfinh = ctx.builder.CreateInBoundsGEP(T_int32, ptls_i32, ConstantInt::get(T_size, finh_offset / 4));
1499+
LoadInst *finh = ctx.builder.CreateAlignedLoad(T_int32, pfinh, Align(sizeof(int32_t)));
15051500
Value *newval;
15061501
if (is_libjulia_func(jl_gc_disable_finalizers_internal)) {
15071502
newval = ctx.builder.CreateAdd(finh, ConstantInt::get(T_int32, 1));
@@ -1527,7 +1522,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
15271522
JL_GC_POP();
15281523
Value *ptls_pv = emit_bitcast(ctx, get_current_ptls(ctx), T_ppjlvalue);
15291524
const int nt_offset = offsetof(jl_tls_states_t, next_task);
1530-
Value *pnt = ctx.builder.CreateInBoundsGEP(ptls_pv, ConstantInt::get(T_size, nt_offset / sizeof(void*)));
1525+
Value *pnt = ctx.builder.CreateInBoundsGEP(T_pjlvalue, ptls_pv, ConstantInt::get(T_size, nt_offset / sizeof(void*)));
15311526
ctx.builder.CreateStore(emit_pointer_from_objref(ctx, boxed(ctx, argv[0])), pnt);
15321527
return ghostValue(jl_nothing_type);
15331528
}
@@ -1537,7 +1532,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
15371532
JL_GC_POP();
15381533
ctx.builder.CreateCall(prepare_call(gcroot_flush_func));
15391534
Value *pdefer_sig = emit_defer_signal(ctx);
1540-
Value *defer_sig = ctx.builder.CreateLoad(pdefer_sig);
1535+
Value *defer_sig = ctx.builder.CreateLoad(T_sigatomic, pdefer_sig);
15411536
defer_sig = ctx.builder.CreateAdd(defer_sig, ConstantInt::get(T_sigatomic, 1));
15421537
ctx.builder.CreateStore(defer_sig, pdefer_sig);
15431538
emit_signal_fence(ctx);
@@ -1549,7 +1544,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
15491544
JL_GC_POP();
15501545
ctx.builder.CreateCall(prepare_call(gcroot_flush_func));
15511546
Value *pdefer_sig = emit_defer_signal(ctx);
1552-
Value *defer_sig = ctx.builder.CreateLoad(pdefer_sig);
1547+
Value *defer_sig = ctx.builder.CreateLoad(T_sigatomic, pdefer_sig);
15531548
emit_signal_fence(ctx);
15541549
error_unless(ctx,
15551550
ctx.builder.CreateICmpNE(defer_sig, ConstantInt::get(T_sigatomic, 0)),
@@ -1566,6 +1561,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
15661561
checkBB, contBB);
15671562
ctx.builder.SetInsertPoint(checkBB);
15681563
ctx.builder.CreateLoad(
1564+
T_size,
15691565
ctx.builder.CreateConstInBoundsGEP1_32(T_size, get_current_signal_page(ctx), -1),
15701566
true);
15711567
ctx.builder.CreateBr(contBB);
@@ -1725,8 +1721,8 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
17251721
JL_GC_POP();
17261722
const int hash_offset = offsetof(jl_sym_t, hash);
17271723
Value *ph1 = emit_bitcast(ctx, decay_derived(ctx, boxed(ctx, val)), T_psize);
1728-
Value *ph2 = ctx.builder.CreateInBoundsGEP(ph1, ConstantInt::get(T_size, hash_offset / sizeof(size_t)));
1729-
LoadInst *hashval = ctx.builder.CreateAlignedLoad(ph2, Align(sizeof(size_t)));
1724+
Value *ph2 = ctx.builder.CreateInBoundsGEP(T_size, ph1, ConstantInt::get(T_size, hash_offset / sizeof(size_t)));
1725+
LoadInst *hashval = ctx.builder.CreateAlignedLoad(T_size, ph2, Align(sizeof(size_t)));
17301726
tbaa_decorate(tbaa_const, hashval);
17311727
return mark_or_box_ccall_result(ctx, hashval, retboxed, rt, unionall, static_rt);
17321728
}
@@ -1951,7 +1947,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
19511947
// something alloca'd above is SSA
19521948
if (static_rt)
19531949
return mark_julia_slot(result, rt, NULL, tbaa_stack);
1954-
result = ctx.builder.CreateLoad(result);
1950+
result = ctx.builder.CreateLoad(cast<PointerType>(result->getType())->getElementType(), result);
19551951
}
19561952
}
19571953
else {

src/cgutils.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -795,19 +795,21 @@ static Value *emit_nthptr_addr(jl_codectx_t &ctx, Value *v, Value *idx)
795795
idx);
796796
}
797797

798-
static LoadInst *emit_nthptr_recast(jl_codectx_t &ctx, Value *v, Value *idx, MDNode *tbaa, Type *ptype)
798+
static LoadInst *emit_nthptr_recast(jl_codectx_t &ctx, Value *v, Value *idx, MDNode *tbaa, Type *type)
799799
{
800-
// p = (jl_value_t**)v; *(ptype)&p[n]
800+
// p = (jl_value_t**)v; *(type*)&p[n]
801801
Value *vptr = emit_nthptr_addr(ctx, v, idx);
802-
return cast<LoadInst>(tbaa_decorate(tbaa, ctx.builder.CreateLoad(emit_bitcast(ctx, vptr, ptype))));
802+
return cast<LoadInst>(tbaa_decorate(tbaa, ctx.builder.CreateLoad(type,
803+
emit_bitcast(ctx, vptr, PointerType::get(type, 0)))));
803804
}
804805

805-
static LoadInst *emit_nthptr_recast(jl_codectx_t &ctx, Value *v, ssize_t n, MDNode *tbaa, Type *ptype)
806+
static LoadInst *emit_nthptr_recast(jl_codectx_t &ctx, Value *v, ssize_t n, MDNode *tbaa, Type *type)
806807
{
807-
// p = (jl_value_t**)v; *(ptype)&p[n]
808+
// p = (jl_value_t**)v; *(type*)&p[n]
808809
Value *vptr = emit_nthptr_addr(ctx, v, n);
809-
return cast<LoadInst>(tbaa_decorate(tbaa, ctx.builder.CreateLoad(emit_bitcast(ctx, vptr, ptype))));
810-
}
810+
return cast<LoadInst>(tbaa_decorate(tbaa, ctx.builder.CreateLoad(type,
811+
emit_bitcast(ctx, vptr, PointerType::get(type, 0)))));
812+
}
811813

812814
static Value *boxed(jl_codectx_t &ctx, const jl_cgval_t &v);
813815

@@ -1506,7 +1508,7 @@ static jl_cgval_t typed_load(jl_codectx_t &ctx, Value *ptr, Value *idx_0based, j
15061508
alignment = sizeof(void*);
15071509
else if (!alignment)
15081510
alignment = julia_alignment(jltype);
1509-
LoadInst *load = ctx.builder.CreateAlignedLoad(data, Align(alignment), false);
1511+
LoadInst *load = ctx.builder.CreateAlignedLoad(elty, data, Align(alignment), false);
15101512
load->setOrdering(Order);
15111513
if (aliasscope)
15121514
load->setMetadata("alias.scope", aliasscope);
@@ -1519,7 +1521,7 @@ static jl_cgval_t typed_load(jl_codectx_t &ctx, Value *ptr, Value *idx_0based, j
15191521
instr = ctx.builder.CreateTrunc(instr, realelty);
15201522
if (intcast) {
15211523
ctx.builder.CreateStore(instr, ctx.builder.CreateBitCast(intcast, instr->getType()->getPointerTo()));
1522-
instr = ctx.builder.CreateLoad(intcast);
1524+
instr = ctx.builder.CreateLoad(intcast->getAllocatedType(), intcast);
15231525
}
15241526
if (maybe_null_if_boxed) {
15251527
Value *first_ptr = isboxed ? instr : extract_first_ptr(ctx, instr);
@@ -1586,7 +1588,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
15861588
return emit_new_struct(ctx, (jl_value_t*)rettyp, 2, argv);
15871589
}
15881590
}
1589-
Value *intcast = nullptr;
1591+
AllocaInst *intcast = nullptr;
15901592
if (!isboxed && Order != AtomicOrdering::NotAtomic && !elty->isIntOrPtrTy()) {
15911593
const DataLayout &DL = jl_data_layout;
15921594
unsigned nb = DL.getTypeSizeInBits(elty);
@@ -1736,7 +1738,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
17361738
if (intcast) {
17371739
ctx.builder.CreateStore(realCompare, ctx.builder.CreateBitCast(intcast, realCompare->getType()->getPointerTo()));
17381740
if (maybe_null_if_boxed)
1739-
realCompare = ctx.builder.CreateLoad(intcast);
1741+
realCompare = ctx.builder.CreateLoad(intcast->getAllocatedType(), intcast);
17401742
}
17411743
if (maybe_null_if_boxed) {
17421744
Value *first_ptr = isboxed ? Compare : extract_first_ptr(ctx, Compare);
@@ -1816,7 +1818,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
18161818
ctx.builder.CreateStore(realinstr, ctx.builder.CreateBitCast(intcast, realinstr->getType()->getPointerTo()));
18171819
oldval = mark_julia_slot(intcast, jltype, NULL, tbaa_stack);
18181820
if (maybe_null_if_boxed)
1819-
realinstr = ctx.builder.CreateLoad(intcast);
1821+
realinstr = ctx.builder.CreateLoad(intcast->getAllocatedType(), intcast);
18201822
}
18211823
else {
18221824
oldval = mark_julia_type(ctx, realinstr, isboxed, jltype);
@@ -1876,7 +1878,7 @@ static jl_cgval_t typed_store(jl_codectx_t &ctx,
18761878
instr = ctx.builder.Insert(CastInst::Create(Instruction::Trunc, instr, realelty));
18771879
if (intcast) {
18781880
ctx.builder.CreateStore(instr, ctx.builder.CreateBitCast(intcast, instr->getType()->getPointerTo()));
1879-
instr = ctx.builder.CreateLoad(intcast);
1881+
instr = ctx.builder.CreateLoad(intcast->getAllocatedType(), intcast);
18801882
}
18811883
if (maybe_null_if_boxed) {
18821884
Value *first_ptr = isboxed ? instr : extract_first_ptr(ctx, instr);
@@ -1949,18 +1951,18 @@ static void emit_memcpy_llvm(jl_codectx_t &ctx, Value *dst, MDNode *tbaa_dst, Va
19491951
dstty = dstel->getPointerTo();
19501952
}
19511953

1952-
bool direct = false;
1954+
llvm::Type *directel = nullptr;
19531955
if (srcel->isSized() && srcel->isSingleValueType() && DL.getTypeStoreSize(srcel) == sz) {
1954-
direct = true;
1956+
directel = srcel;
19551957
dst = emit_bitcast(ctx, dst, srcty);
19561958
}
19571959
else if (dstel->isSized() && dstel->isSingleValueType() &&
19581960
DL.getTypeStoreSize(dstel) == sz) {
1959-
direct = true;
1961+
directel = dstel;
19601962
src = emit_bitcast(ctx, src, dstty);
19611963
}
1962-
if (direct) {
1963-
auto val = tbaa_decorate(tbaa_src, ctx.builder.CreateAlignedLoad(src, Align(align), is_volatile));
1964+
if (directel) {
1965+
auto val = tbaa_decorate(tbaa_src, ctx.builder.CreateAlignedLoad(directel, src, Align(align), is_volatile));
19641966
tbaa_decorate(tbaa_dst, ctx.builder.CreateAlignedStore(val, dst, Align(align), is_volatile));
19651967
return;
19661968
}
@@ -2430,7 +2432,7 @@ static Value *emit_arraysize(jl_codectx_t &ctx, const jl_cgval_t &tinfo, Value *
24302432
auto load = emit_nthptr_recast(ctx,
24312433
t,
24322434
ctx.builder.CreateAdd(dim, ConstantInt::get(dim->getType(), o)),
2433-
tbaa, T_psize);
2435+
tbaa, T_size);
24342436
MDBuilder MDB(jl_LLVMContext);
24352437
auto rng = MDB.createRange(V_size0, ConstantInt::get(T_size, arraytype_maxsize(tinfo.typ)));
24362438
load->setMetadata(LLVMContext::MD_range, rng);
@@ -2466,7 +2468,7 @@ static Value *emit_arraylen_prim(jl_codectx_t &ctx, const jl_cgval_t &tinfo)
24662468
Value *addr = ctx.builder.CreateStructGEP(jl_array_llvmt,
24672469
emit_bitcast(ctx, decay_derived(ctx, t), jl_parray_llvmt),
24682470
1); //index (not offset) of length field in jl_parray_llvmt
2469-
LoadInst *len = ctx.builder.CreateAlignedLoad(addr, Align(sizeof(size_t)));
2471+
LoadInst *len = ctx.builder.CreateAlignedLoad(T_size, addr, Align(sizeof(size_t)));
24702472
len->setOrdering(AtomicOrdering::NotAtomic);
24712473
MDBuilder MDB(jl_LLVMContext);
24722474
auto rng = MDB.createRange(V_size0, ConstantInt::get(T_size, arraytype_maxsize(tinfo.typ)));
@@ -2519,7 +2521,8 @@ static Value *emit_arrayptr_internal(jl_codectx_t &ctx, const jl_cgval_t &tinfo,
25192521
PointerType::get(PPT->getElementType(), AS),
25202522
PT->getAddressSpace()));
25212523
}
2522-
LoadInst *LI = ctx.builder.CreateAlignedLoad(addr, Align(sizeof(char*)));
2524+
LoadInst *LI = ctx.builder.CreateAlignedLoad(
2525+
cast<PointerType>(addr->getType())->getElementType(), addr, Align(sizeof(char*)));
25232526
LI->setOrdering(AtomicOrdering::NotAtomic);
25242527
LI->setMetadata(LLVMContext::MD_nonnull, MDNode::get(jl_LLVMContext, None));
25252528
tbaa_decorate(tbaa, LI);
@@ -2828,9 +2831,9 @@ static Value *as_value(jl_codectx_t &ctx, Type *to, const jl_cgval_t &v)
28282831
static Value *load_i8box(jl_codectx_t &ctx, Value *v, jl_datatype_t *ty)
28292832
{
28302833
auto jvar = ty == jl_int8_type ? jlboxed_int8_cache : jlboxed_uint8_cache;
2831-
Constant *gv = prepare_global_in(jl_Module, jvar);
2834+
GlobalVariable *gv = prepare_global_in(jl_Module, jvar);
28322835
Value *idx[] = {ConstantInt::get(T_int32, 0), ctx.builder.CreateZExt(v, T_int32)};
2833-
auto slot = ctx.builder.CreateInBoundsGEP(gv, idx);
2836+
auto slot = ctx.builder.CreateInBoundsGEP(gv->getType()->getElementType(), gv, idx);
28342837
return tbaa_decorate(tbaa_const, maybe_mark_load_dereferenceable(
28352838
ctx.builder.CreateAlignedLoad(T_pjlvalue, slot, Align(sizeof(void*))), false,
28362839
(jl_value_t*)ty));

0 commit comments

Comments
 (0)