Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 47 additions & 6 deletions llvm/lib/Target/DirectX/DXILLegalizePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static void fixI8UseChain(Instruction &I,
Type *ElementType = NewOperands[0]->getType();
if (auto *AI = dyn_cast<AllocaInst>(NewOperands[0]))
ElementType = AI->getAllocatedType();
if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0]))
ElementType = GEP->getSourceElementType();
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewOperands[0]);
ReplacedValues[Load] = NewLoad;
ToRemove.push_back(Load);
Expand Down Expand Up @@ -164,6 +166,37 @@ static void fixI8UseChain(Instruction &I,
if (AdjustedCast)
Cast->replaceAllUsesWith(AdjustedCast);
}
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (!GEP->getType()->isPointerTy() ||
!GEP->getSourceElementType()->isIntegerTy(8))
return;

Value *BasePtr = GEP->getPointerOperand();
if (ReplacedValues.count(BasePtr))
BasePtr = ReplacedValues[BasePtr];

Type *ElementType = BasePtr->getType();
if (auto *AI = dyn_cast<AllocaInst>(BasePtr))
ElementType = AI->getAllocatedType();
if (auto *ArrTy = dyn_cast<ArrayType>(ElementType))
ElementType = ArrTy->getArrayElementType();

ConstantInt *Offset = dyn_cast<ConstantInt>(GEP->getOperand(1));
// Note: i8 to i32 offset conversion without emitting IR requires constant
// ints. Since offset conversion is common, we can safely assume Offset is
// always a ConstantInt, so no need to have a conditional bail out on
// nullptr, instead assert this is the case.
assert(Offset && "Offset is expected to be a ConstantInt");
uint32_t ByteOffset = Offset->getZExtValue();
uint32_t ElemSize = GEP->getDataLayout().getTypeAllocSize(ElementType);
assert(ElemSize > 0 && "ElementSize must be set");
uint32_t Index = ByteOffset / ElemSize;
Value *NewGEP =
Builder.CreateGEP(ElementType, BasePtr, Builder.getInt32(Index),
GEP->getName(), GEP->getNoWrapFlags());
ReplacedValues[GEP] = NewGEP;
ToRemove.push_back(GEP);
}
}

static void upcastI8AllocasAndUses(Instruction &I,
Expand All @@ -175,15 +208,12 @@ static void upcastI8AllocasAndUses(Instruction &I,

Type *SmallestType = nullptr;

for (User *U : AI->users()) {
auto *Load = dyn_cast<LoadInst>(U);
if (!Load)
continue;
auto ProcessLoad = [&](LoadInst *Load) {
for (User *LU : Load->users()) {
Type *Ty = nullptr;
if (auto *Cast = dyn_cast<CastInst>(LU))
if (CastInst *Cast = dyn_cast<CastInst>(LU))
Ty = Cast->getType();
if (CallInst *CI = dyn_cast<CallInst>(LU)) {
else if (CallInst *CI = dyn_cast<CallInst>(LU)) {
if (CI->getIntrinsicID() == Intrinsic::memset)
Ty = Type::getInt32Ty(CI->getContext());
}
Expand All @@ -195,6 +225,17 @@ static void upcastI8AllocasAndUses(Instruction &I,
Ty->getPrimitiveSizeInBits() < SmallestType->getPrimitiveSizeInBits())
SmallestType = Ty;
}
};

for (User *U : AI->users()) {
if (auto *Load = dyn_cast<LoadInst>(U))
ProcessLoad(Load);
else if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
for (User *GU : GEP->users()) {
if (auto *Load = dyn_cast<LoadInst>(GU))
ProcessLoad(Load);
}
}
}

if (!SmallestType)
Expand Down
54 changes: 54 additions & 0 deletions llvm/test/CodeGen/DirectX/legalize-i8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,57 @@ define i32 @all_imm() {
%2 = sext i8 %1 to i32
ret i32 %2
}

define i32 @scalar_i8_geps() {
; CHECK-LABEL: define i32 @scalar_i8_geps(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 0
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca i8, align 4
%2 = getelementptr inbounds nuw i8, ptr %1, i32 0
%3 = load i8, ptr %2
%4 = sext i8 %3 to i32
ret i32 %4
}

define i32 @i8_geps_index0() {
; CHECK-LABEL: define i32 @i8_geps_index0(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 0
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca [2 x i32], align 8
%2 = getelementptr inbounds nuw i8, ptr %1, i32 0
%3 = load i8, ptr %2
%4 = sext i8 %3 to i32
ret i32 %4
}

define i32 @i8_geps_index1() {
; CHECK-LABEL: define i32 @i8_geps_index1(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 1
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]]
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca [2 x i32], align 8
%2 = getelementptr inbounds nuw i8, ptr %1, i32 4
%3 = load i8, ptr %2
%4 = sext i8 %3 to i32
ret i32 %4
}

define i32 @i8_gep_store() {
; CHECK-LABEL: define i32 @i8_gep_store(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw i32, ptr [[ALLOCA]], i32 1
; CHECK-NEXT: store i32 1, ptr [[GEP]], align 4
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]]
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca [2 x i32], align 8
%2 = getelementptr inbounds nuw i8, ptr %1, i32 4
store i8 1, ptr %2
%3 = load i8, ptr %2
%4 = sext i8 %3 to i32
ret i32 %4
}