Skip to content

Commit

Permalink
for now only use array ABI for tuples
Browse files Browse the repository at this point in the history
fix some tuple type syntax
  • Loading branch information
JeffBezanson committed Apr 17, 2015
1 parent 9190c21 commit a93e9a1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
9 changes: 6 additions & 3 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function next(e::Enumerate, state)
end
done(e::Enumerate, state) = done(e.itr, state[2])

eltype{I}(::Type{Enumerate{I}}) = (Int, eltype(I))
eltype{I}(::Type{Enumerate{I}}) = Tuple{Int, eltype(I)}

# zip

Expand All @@ -28,7 +28,7 @@ end
zip(a) = a
zip(a, b) = Zip2(a, b)
length(z::Zip2) = min(length(z.a), length(z.b))
eltype{I1,I2}(::Type{Zip2{I1,I2}}) = (eltype(I1), eltype(I2))
eltype{I1,I2}(::Type{Zip2{I1,I2}}) = Tuple{eltype(I1), eltype(I2)}
start(z::Zip2) = (start(z.a), start(z.b))
function next(z::Zip2, st)
n1 = next(z.a,st[1])
Expand All @@ -43,7 +43,10 @@ immutable Zip{I, Z<:AbstractZipIterator} <: AbstractZipIterator
end
zip(a, b, c...) = Zip(a, zip(b, c...))
length(z::Zip) = min(length(z.a), length(z.z))
eltype{I,Z}(::Type{Zip{I,Z}}) = tuple(eltype(I), eltype(Z)...)
stagedfunction tuple_type_cons{S,T<:Tuple}(::Type{S}, ::Type{T})
Tuple{S, T.parameters...}
end
eltype{I,Z}(::Type{Zip{I,Z}}) = tuple_type_cons(eltype(I), eltype(Z))
start(z::Zip) = tuple(start(z.a), start(z.z))
function next(z::Zip, st)
n1 = next(z.a, st[1])
Expand Down
14 changes: 9 additions & 5 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,14 @@ static Type *julia_struct_to_llvm(jl_value_t *jt)
else {
if (isvector && lasttype != T_int1 && lasttype != T_void) {
// TODO: currently we get LLVM assertion failures for other vector sizes
// TODO: DataTypes do not yet support vector alignment requirements,
// so only use array ABI for tuples for now.
/*
bool validVectorSize = ntypes <= 6 || (ntypes&1)==0;
if (lasttype->isSingleValueType() && !lasttype->isVectorTy() && validVectorSize)
jst->struct_decl = VectorType::get(lasttype,ntypes);
else
*/
jst->struct_decl = ArrayType::get(lasttype,ntypes);
}
else {
Expand Down Expand Up @@ -1155,7 +1159,7 @@ static Value *emit_getfield_unknownidx(Value *strct, Value *idx, jl_datatype_t *
jl_value_t *jt = jl_field_type(stt, 0);
idx = emit_bounds_check(strct, NULL, idx, ConstantInt::get(T_size, nfields), ctx);
Value *ptr = data_pointer(strct);
return typed_load(ptr, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut, 1);
return typed_load(ptr, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut);
}
else {
idx = builder.CreateSub(idx, ConstantInt::get(T_size, 1));
Expand Down Expand Up @@ -1186,7 +1190,7 @@ static Value *emit_getfield_unknownidx(Value *strct, Value *idx, jl_datatype_t *
}
// TODO: pass correct thing to emit_bounds_check ?
idx = emit_bounds_check(tempSpace, (jl_value_t*)stt, idx, ConstantInt::get(T_size, nfields), ctx);
fld = typed_load(tempSpace, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut, 1);
fld = typed_load(tempSpace, idx, jt, ctx, stt->mutabl ? tbaa_user : tbaa_immut);
builder.CreateCall(Intrinsic::getDeclaration(jl_Module,Intrinsic::stackrestore),
stacksave);
}
Expand Down Expand Up @@ -1215,7 +1219,7 @@ static Value *emit_getfield_knownidx(Value *strct, unsigned idx, jl_datatype_t *
return fldv;
}
else {
return typed_load(addr, ConstantInt::get(T_size, 0), jfty, ctx, tbaa, 1);
return typed_load(addr, ConstantInt::get(T_size, 0), jfty, ctx, tbaa);
}
}
else {
Expand Down Expand Up @@ -1429,7 +1433,7 @@ static Value *init_bits_value(Value *newv, Value *jt, Type *t, Value *v)
{
builder.CreateStore(jt, builder.CreateBitCast(emit_typeptr_addr(newv), jl_ppvalue_llvmt));
// TODO: stricter alignment if possible
builder.CreateAlignedStore(v, builder.CreateBitCast(data_pointer(newv), PointerType::get(t,0)), 1);
builder.CreateAlignedStore(v, builder.CreateBitCast(data_pointer(newv), PointerType::get(t,0)), sizeof(void*));
return newv;
}

Expand Down Expand Up @@ -1708,7 +1712,7 @@ static Value *emit_setfield(jl_datatype_t *sty, Value *strct, size_t idx0,
if (wb) emit_checked_write_barrier(ctx, strct, rhs);
}
else {
typed_store(addr, ConstantInt::get(T_size, 0), rhs, jfty, ctx, sty->mutabl ? tbaa_user : tbaa_immut, strct, 1);
typed_store(addr, ConstantInt::get(T_size, 0), rhs, jfty, ctx, sty->mutabl ? tbaa_user : tbaa_immut, strct);
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static Value *emit_unbox(Type *to, Value *x, jl_value_t *jt)
return UndefValue::get(to);
}
// TODO: stricter alignment if possible
return builder.CreateAlignedLoad(builder.CreateBitCast(p, to->getPointerTo()), 1, false);
return builder.CreateAlignedLoad(builder.CreateBitCast(p, to->getPointerTo()), sizeof(void*), false);
}

// unbox trying to determine type automatically
Expand Down
37 changes: 26 additions & 11 deletions test/llvmcall.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
using Base.llvmcall

function add1234(x::Tuple{Int32,Int32,Int32,Int32})
llvmcall("""%3 = add <4 x i32> %1, %0
ret <4 x i32> %3""",Tuple{Int32,Int32,Int32,Int32},
llvmcall("""%3 = extractvalue [4 x i32] %0, 0
%4 = extractvalue [4 x i32] %0, 1
%5 = extractvalue [4 x i32] %0, 2
%6 = extractvalue [4 x i32] %0, 3
%7 = extractvalue [4 x i32] %1, 0
%8 = extractvalue [4 x i32] %1, 1
%9 = extractvalue [4 x i32] %1, 2
%10 = extractvalue [4 x i32] %1, 3
%11 = add i32 %3, %7
%12 = add i32 %4, %8
%13 = add i32 %5, %9
%14 = add i32 %6, %10
%15 = insertvalue [4 x i32] undef, i32 %11, 0
%16 = insertvalue [4 x i32] %15, i32 %12, 1
%17 = insertvalue [4 x i32] %16, i32 %13, 2
%18 = insertvalue [4 x i32] %17, i32 %14, 3
ret [4 x i32] %18""",Tuple{Int32,Int32,Int32,Int32},
Tuple{Tuple{Int32,Int32,Int32,Int32},Tuple{Int32,Int32,Int32,Int32}},
(Int32(1),Int32(2),Int32(3),Int32(4)),
x)
x)
end

function add1234(x::NTuple{4,Int64})
llvmcall("""%3 = add <4 x i64> %1, %0
ret <4 x i64> %3""",NTuple{4,Int64},
Tuple{NTuple{4,Int64},NTuple{4,Int64}},
(Int64(1),Int64(2),Int64(3),Int64(4)),
x)
end
# function add1234(x::NTuple{4,Int64})
# llvmcall("""%3 = add <4 x i64> %1, %0
# ret <4 x i64> %3""",NTuple{4,Int64},
# Tuple{NTuple{4,Int64},NTuple{4,Int64}},
# (Int64(1),Int64(2),Int64(3),Int64(4)),
# x)
# end

@test add1234(map(Int32,(2,3,4,5))) === map(Int32,(3,5,7,9))
@test add1234(map(Int64,(2,3,4,5))) === map(Int64,(3,5,7,9))
#@test add1234(map(Int64,(2,3,4,5))) === map(Int64,(3,5,7,9))

# Test whether llvmcall escapes the function name correctly
baremodule PlusTest
Expand Down

0 comments on commit a93e9a1

Please sign in to comment.