Skip to content

Commit 75d6f50

Browse files
authored
[Interpreter] Add initialization of array members (llvm#66172)
Currently, the interpreter does not initialize `undef` constants of aggregate types correctly (with respect to arrays). The initialization of the array elements is skipped although it is valid to directly write to them. Instructions like `insertvalue {i32, [4 x i32]} undef, i32 1, 1, 2` therefore lead to a crash. This is fixed by initializing array values just as fixed-size vectors or structs.
1 parent 97e06a0 commit 75d6f50

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

llvm/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,18 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
618618
case Type::ScalableVectorTyID:
619619
report_fatal_error(
620620
"Scalable vector support not yet implemented in ExecutionEngine");
621-
case Type::FixedVectorTyID:
621+
case Type::ArrayTyID: {
622+
auto *ArrTy = cast<ArrayType>(C->getType());
623+
Type *ElemTy = ArrTy->getElementType();
624+
unsigned int elemNum = ArrTy->getNumElements();
625+
Result.AggregateVal.resize(elemNum);
626+
if (ElemTy->isIntegerTy())
627+
for (unsigned int i = 0; i < elemNum; ++i)
628+
Result.AggregateVal[i].IntVal =
629+
APInt(ElemTy->getPrimitiveSizeInBits(), 0);
630+
break;
631+
}
632+
case Type::FixedVectorTyID: {
622633
// if the whole vector is 'undef' just reserve memory for the value.
623634
auto *VTy = cast<FixedVectorType>(C->getType());
624635
Type *ElemTy = VTy->getElementType();
@@ -629,6 +640,7 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
629640
Result.AggregateVal[i].IntVal =
630641
APInt(ElemTy->getPrimitiveSizeInBits(), 0);
631642
break;
643+
}
632644
}
633645
return Result;
634646
}

llvm/test/ExecutionEngine/2010-01-15-UndefValue.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ define i32 @main() {
44
%a = add i32 0, undef
55
%b = fadd float 0.0, undef
66
%c = fadd double 0.0, undef
7+
%d = insertvalue {i32, [4 x i32]} undef, i32 1, 1, 2
78
ret i32 0
89
}

0 commit comments

Comments
 (0)