Skip to content

[5.3] IRGen: Fix enumPayload value witness emission for huge types #31935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions lib/IRGen/GenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,26 @@ llvm::Value *irgen::getFixedTypeEnumTagSinglePayload(

llvm::Value *caseIndexFromValue = zero;
if (fixedSize > Size(0)) {
// Read up to one pointer-sized 'chunk' of the payload.
// The size of the chunk does not have to be a power of 2.
auto *caseIndexType = llvm::IntegerType::get(Ctx,
fixedSize.getValueInBits());
auto *caseIndexAddr = Builder.CreateBitCast(valueAddr,
caseIndexType->getPointerTo());
caseIndexFromValue = Builder.CreateZExtOrTrunc(
Builder.CreateLoad(Address(caseIndexAddr, Alignment(1))),
IGM.Int32Ty);
// llvm only supports integer types upto a certain size (i.e selection dag
// will crash).
if (fixedSize.getValueInBits() <= llvm::IntegerType::MAX_INT_BITS / 4) {
// Read up to one pointer-sized 'chunk' of the payload.
// The size of the chunk does not have to be a power of 2.
auto *caseIndexType =
llvm::IntegerType::get(Ctx, fixedSize.getValueInBits());
auto *caseIndexAddr =
Builder.CreateBitCast(valueAddr, caseIndexType->getPointerTo());
caseIndexFromValue = Builder.CreateZExtOrTrunc(
Builder.CreateLoad(Address(caseIndexAddr, Alignment(1))),
IGM.Int32Ty);
} else {
auto *caseIndexType = llvm::IntegerType::get(Ctx, 32);
auto *caseIndexAddr =
Builder.CreateBitCast(valueAddr, caseIndexType->getPointerTo());
caseIndexFromValue = Builder.CreateZExtOrTrunc(
Builder.CreateLoad(Address(caseIndexAddr, Alignment(1))),
IGM.Int32Ty);
}
}

auto *result1 = Builder.CreateAdd(
Expand Down Expand Up @@ -867,11 +878,25 @@ void irgen::storeFixedTypeEnumTagSinglePayload(
payloadIndex->addIncoming(payloadIndex0, payloadLT4BB);

if (fixedSize > Size(0)) {
// Write the value to the payload as a zero extended integer.
auto *intType = Builder.getIntNTy(fixedSize.getValueInBits());
Builder.CreateStore(
Builder.CreateZExtOrTrunc(payloadIndex, intType),
Builder.CreateBitCast(valueAddr, intType->getPointerTo()));
if (fixedSize.getValueInBits() <= llvm::IntegerType::MAX_INT_BITS / 4) {
// Write the value to the payload as a zero extended integer.
auto *intType = Builder.getIntNTy(fixedSize.getValueInBits());
Builder.CreateStore(
Builder.CreateZExtOrTrunc(payloadIndex, intType),
Builder.CreateBitCast(valueAddr, intType->getPointerTo()));
} else {
// Write the value to the payload as a zero extended integer.
Size limit = IGM.getPointerSize();
auto *intType = Builder.getIntNTy(limit.getValueInBits());
Builder.CreateStore(
Builder.CreateZExtOrTrunc(payloadIndex, intType),
Builder.CreateBitCast(valueAddr, intType->getPointerTo()));
// Zero the remainder of the payload.
auto zeroAddr = Builder.CreateConstByteArrayGEP(valueAddr, limit);
auto zeroSize = Builder.CreateSub(
size, llvm::ConstantInt::get(size->getType(), limit.getValue()));
Builder.CreateMemSet(zeroAddr, Builder.getInt8(0), zeroSize);
}
}
// Write to the extra tag bytes, if any.
emitSetTag(IGF, extraTagBitsAddr, extraTagIndex, numExtraTagBytes);
Expand Down
31 changes: 31 additions & 0 deletions test/IRGen/Inputs/huge_c_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdint.h>

typedef uint8_t bool;

#define CREATE_ARRAY(T, N) \
struct { \
T data[N]; \
uint64_t size; \
}

typedef struct {
int32_t a;
double b[16];
} Thing;

typedef struct {
uint64_t a;
bool b;
CREATE_ARRAY(Thing, 16) c;
uint32_t d;
uint64_t e;
uint64_t f;
CREATE_ARRAY(uint32_t, 4) g;
CREATE_ARRAY(uint64_t, 4) h;
CREATE_ARRAY(uint64_t, 4) i;
} Thing2;

typedef struct {
int64_t a;
CREATE_ARRAY(Thing2, 512) c;
} Thing3;
10 changes: 10 additions & 0 deletions test/IRGen/huge_c_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-frontend -import-objc-header %S/Inputs/huge_c_type.h %s -disable-llvm-optzns -emit-ir | %FileCheck %s
// Make sure that this does not crash during LLVM's ISel. It does not like huge
// llvm::IntegerTypes.
// RUN: %target-swift-frontend -import-objc-header %S/Inputs/huge_c_type.h %s -c

// CHECK-NOT:i9535616

public func doIt(a: Thing3) {
print(a)
}