Skip to content

Commit

Permalink
codegen: flatten union data array into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Oct 18, 2017
1 parent e3cd320 commit 43a6206
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
8 changes: 5 additions & 3 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,14 @@ static Type *julia_struct_to_llvm(jl_value_t *jt, jl_unionall_t *ua, bool *isbox
else if (jl_is_uniontype(ty)) {
// pick an Integer type size such that alignment will be correct
// and always end with an Int8 (selector byte)
Type *lty0 = ArrayType::get(IntegerType::get(jl_LLVMContext, 8 * al), (fsz - 1) / al);
latypes.push_back(lty0);
latypes.push_back(T_int8);
Type *AlignmentType = IntegerType::get(jl_LLVMContext, 8 * al);
unsigned NumATy = (fsz - 1) / al;
unsigned remainder = (fsz - 1) % al;
while (NumATy--)
latypes.push_back(AlignmentType);
while (remainder--)
latypes.push_back(T_int8);
latypes.push_back(T_int8);
isarray = false;
allghost = false;
continue;
Expand Down
48 changes: 26 additions & 22 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,39 +208,43 @@ static Constant *julia_const_to_llvm(const void *ptr, jl_datatype_t *bt)
uint8_t sel = ((const uint8_t*)ptr)[offs + fsz - 1];
jl_value_t *active_ty = jl_nth_union_component(ft, sel);
size_t active_sz = jl_datatype_size(active_ty);
ArrayType *aty = cast<ArrayType>(lct->getTypeAtIndex(llvm_idx));
unsigned num_array_elem = aty->getNumElements();
assert(aty->getElementType() == IntegerType::get(jl_LLVMContext, 8 * al) &&
num_array_elem == (fsz - 1) / al);
std::vector<Constant*> ArrayElements(num_array_elem);
for (unsigned j = 0; j < num_array_elem; j++) {
APInt Elem(8 * al, 0);
void *bits = const_cast<uint64_t*>(Elem.getRawData());
if (active_sz > al) {
memcpy(bits, ov, al);
active_sz -= al;
Type *AlignmentType = IntegerType::get(jl_LLVMContext, 8 * al);
unsigned NumATy = (fsz - 1) / al;
unsigned remainder = (fsz - 1) % al;
while (NumATy--) {
Constant *fld;
if (active_sz > 0) {
APInt Elem(8 * al, 0);
void *bits = const_cast<uint64_t*>(Elem.getRawData());
if (active_sz > al) {
memcpy(bits, ov, al);
active_sz -= al;
}
else {
memcpy(bits, ov, active_sz);
active_sz = 0;
}
fld = ConstantInt::get(AlignmentType, Elem);
}
else if (active_sz > 0) {
memcpy(bits, ov, active_sz);
active_sz = 0;
else {
fld = UndefValue::get(AlignmentType);
}
ov += al;
ArrayElements[j] = ConstantInt::get(aty->getElementType(), Elem);
fields.push_back(fld);
}
fields.push_back(ConstantArray::get(aty, ArrayElements));
unsigned remainder = (fsz - 1) % al;
while (remainder--) {
uint8_t byte;
Constant *fld;
if (active_sz > 0) {
byte = *ov;
uint8_t byte = *ov;
APInt Elem(8, byte);
active_sz -= 1;
fld = ConstantInt::get(T_int8, Elem);
}
else {
byte = 0;
fld = UndefValue::get(T_int8);
}
ov += 1;
APInt Elem(8, byte);
fields.push_back(ConstantInt::get(T_int8, Elem));
fields.push_back(fld);
}
fields.push_back(ConstantInt::get(T_int8, sel));
}
Expand Down

0 comments on commit 43a6206

Please sign in to comment.