Skip to content

Commit 27a6038

Browse files
committed
Only construct legal int types during alloc opt.
When promoting heap to stack allocations, make sure we only emit legal integer allocas by checking with the module's datalayout. X86 doesn't seem to care, but back-ends like SPIR-V don't know how to handle arbitrarily-sized integers. Fixes JuliaGPU/oneAPI.jl#55
1 parent 33573ec commit 27a6038

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/llvm-alloc-opt.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,12 @@ void Optimizer::moveToStack(CallInst *orig_inst, size_t sz, bool has_ref)
936936
ptr = cast<Instruction>(prolog_builder.CreateBitCast(buff, pass.T_pint8));
937937
}
938938
else {
939-
buff = prolog_builder.CreateAlloca(Type::getIntNTy(pass.getLLVMContext(), sz * 8));
939+
Type *buffty;
940+
if (pass.DL->isLegalInteger(sz * 8))
941+
buffty = Type::getIntNTy(pass.getLLVMContext(), sz * 8);
942+
else
943+
buffty = ArrayType::get(Type::getInt8Ty(pass.getLLVMContext()), sz);
944+
buff = prolog_builder.CreateAlloca(buffty);
940945
buff->setAlignment(Align(align));
941946
ptr = cast<Instruction>(prolog_builder.CreateBitCast(buff, pass.T_pint8));
942947
}
@@ -1193,8 +1198,10 @@ void Optimizer::splitOnStack(CallInst *orig_inst)
11931198
else if (field.elty && !field.multiloc) {
11941199
allocty = field.elty;
11951200
}
1196-
else {
1201+
else if (pass.DL->isLegalInteger(field.size * 8)) {
11971202
allocty = Type::getIntNTy(pass.getLLVMContext(), field.size * 8);
1203+
} else {
1204+
allocty = ArrayType::get(Type::getInt8Ty(pass.getLLVMContext()), field.size);
11981205
}
11991206
slot.slot = prolog_builder.CreateAlloca(allocty);
12001207
insertLifetime(prolog_builder.CreateBitCast(slot.slot, pass.T_pint8),

test/llvmpasses/alloc-opt2.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,30 @@ L3:
7979
""")
8080
# CHECK-LABEL: }{{$}}
8181

82+
# CHECK-LABEL: @legal_int_types
83+
# CHECK: alloca [12 x i8]
84+
# CHECK-NOT: alloca i96
85+
# CHECK: ret void
86+
println("""
87+
define void @legal_int_types() {
88+
%ptls = call {}*** @julia.ptls_states()
89+
%ptls_i8 = bitcast {}*** %ptls to i8*
90+
%var1 = call {} addrspace(10)* @julia.gc_alloc_obj(i8* %ptls_i8, $isz 12, {} addrspace(10)* @tag)
91+
%var2 = addrspacecast {} addrspace(10)* %var1 to {} addrspace(11)*
92+
%var3 = call {}* @julia.pointer_from_objref({} addrspace(11)* %var2)
93+
ret void
94+
}
95+
""")
96+
# CHECK-LABEL: }{{$}}
97+
98+
99+
82100
println("""
83101
declare void @external_function()
84102
declare {} addrspace(10)* @external_function2()
85103
declare {}*** @julia.ptls_states()
86104
declare noalias {} addrspace(10)* @julia.gc_alloc_obj(i8*, $isz, {} addrspace(10)*)
87-
declare i64 @julia.pointer_from_objref({} addrspace(11)*)
105+
declare {}* @julia.pointer_from_objref({} addrspace(11)*)
88106
declare void @llvm.memcpy.p11i8.p0i8.i64(i8 addrspace(11)* nocapture writeonly, i8* nocapture readonly, i64, i32, i1)
89107
declare token @llvm.julia.gc_preserve_begin(...)
90108
declare void @llvm.julia.gc_preserve_end(token)

0 commit comments

Comments
 (0)