Skip to content

[DirectX] add GEP i8 legalization #142475

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
merged 5 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 30 additions & 7 deletions llvm/lib/Target/DirectX/DXILLegalizePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ 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]))
if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0])) {
ElementType = GEP->getSourceElementType();
if (ElementType->isArrayTy())
ElementType = ElementType->getArrayElementType();
}
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewOperands[0]);
ReplacedValues[Load] = NewLoad;
ToRemove.push_back(Load);
Expand All @@ -116,9 +119,23 @@ static void fixI8UseChain(Instruction &I,
uint32_t ByteOffset = Offset->getZExtValue();
uint32_t ElemSize = Load->getDataLayout().getTypeAllocSize(ElementType);
uint32_t Index = ByteOffset / ElemSize;
Value *NewGEP = Builder.CreateGEP(ElementType, GEP->getPointerOperand(),
Builder.getInt32(Index), GEP->getName(),
GEP->getNoWrapFlags());

Value *PtrOperand = GEP->getPointerOperand();
Type *GEPType = GEP->getPointerOperandType();

if (auto *GV = dyn_cast<GlobalVariable>(PtrOperand))
GEPType = GV->getValueType();
if (auto *AI = dyn_cast<AllocaInst>(PtrOperand))
GEPType = AI->getAllocatedType();

if (auto *ArrTy = dyn_cast<ArrayType>(GEPType))
GEPType = ArrTy;
else
GEPType = ArrayType::get(ElementType, 1); // its a scalar

Value *NewGEP = Builder.CreateGEP(
GEPType, PtrOperand, {Builder.getInt32(0), Builder.getInt32(Index)},
GEP->getName(), GEP->getNoWrapFlags());

LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewGEP);
ReplacedValues[Load] = NewLoad;
Expand Down Expand Up @@ -181,6 +198,7 @@ static void fixI8UseChain(Instruction &I,
Cast->replaceAllUsesWith(Replacement);
return;
}

Value *AdjustedCast = nullptr;
if (Cast->getOpcode() == Instruction::ZExt)
AdjustedCast = Builder.CreateZExtOrTrunc(Replacement, Cast->getType());
Expand All @@ -200,12 +218,17 @@ static void fixI8UseChain(Instruction &I,
BasePtr = ReplacedValues[BasePtr];

Type *ElementType = BasePtr->getType();

if (auto *AI = dyn_cast<AllocaInst>(BasePtr))
ElementType = AI->getAllocatedType();
if (auto *GV = dyn_cast<GlobalVariable>(BasePtr))
ElementType = GV->getValueType();

Type *GEPType = ElementType;
if (auto *ArrTy = dyn_cast<ArrayType>(ElementType))
ElementType = ArrTy->getArrayElementType();
else
GEPType = ArrayType::get(ElementType, 1); // its a scalar

ConstantInt *Offset = dyn_cast<ConstantInt>(GEP->getOperand(1));
// Note: i8 to i32 offset conversion without emitting IR requires constant
Expand All @@ -217,9 +240,9 @@ static void fixI8UseChain(Instruction &I,
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());
Value *NewGEP = Builder.CreateGEP(
GEPType, BasePtr, {Builder.getInt32(0), Builder.getInt32(Index)},
GEP->getName(), GEP->getNoWrapFlags());
ReplacedValues[GEP] = NewGEP;
GEP->replaceAllUsesWith(NewGEP);
ToRemove.push_back(GEP);
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/DirectX/legalize-i8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ define i32 @all_imm() {
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: [[GEP:%.*]] = getelementptr inbounds nuw [1 x i32], ptr [[ALLOCA]], i32 0, i32 0
; CHECK: [[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
Expand All @@ -123,8 +123,8 @@ define i32 @scalar_i8_geps() {
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: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 0
; CHECK: [[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
Expand All @@ -136,8 +136,8 @@ define i32 @i8_geps_index0() {
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: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 1
; CHECK: [[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 4
Expand All @@ -149,9 +149,9 @@ define i32 @i8_geps_index1() {
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: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 1
; CHECK-NEXT: store i32 1, ptr [[GEP]], align 4
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]]
; CHECK: [[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 4
Expand All @@ -164,7 +164,7 @@ define i32 @i8_gep_store() {
@g = local_unnamed_addr addrspace(3) global [2 x float] zeroinitializer, align 4
define float @i8_gep_global_index() {
; CHECK-LABEL: define float @i8_gep_global_index(
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw (float, ptr addrspace(3) @g, i32 1), align 4
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw ([2 x float], ptr addrspace(3) @g, i32 0, i32 1), align 4
; CHECK-NEXT: ret float [[LOAD]]
%1 = getelementptr inbounds nuw i8, ptr addrspace(3) @g, i32 4
%2 = load float, ptr addrspace(3) %1, align 4
Expand All @@ -173,7 +173,7 @@ define float @i8_gep_global_index() {

define float @i8_gep_global_constexpr() {
; CHECK-LABEL: define float @i8_gep_global_constexpr(
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw (float, ptr addrspace(3) @g, i32 1), align 4
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr addrspace(3) getelementptr inbounds nuw ([2 x float], ptr addrspace(3) @g, i32 0, i32 1), align 4
; CHECK-NEXT: ret float [[LOAD]]
%1 = load float, ptr addrspace(3) getelementptr inbounds nuw (i8, ptr addrspace(3) @g, i32 4), align 4
ret float %1
Expand Down