-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
244b01e
[DirectX] add GEP i8 legalization
farzonl 19668a1
fix comment up, fix up some style guide reuquired changes called out …
farzonl 6f4702a
add replaceAllUsesWith back to update old gep to new gep
farzonl 74b05e0
Add support for GEPs with global ptr operands. Add support for loads …
farzonl 5f1e240
make geps have array types to make the validator happy
farzonl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -164,6 +166,36 @@ 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: the only way to convert an i8 offset to an i32 offset without | ||
// emitting code Would be to emit code. We sould expect this value to be a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should |
||
// ConstantInt since Offsets are very regulalrly converted. | ||
farzonl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
|
@@ -175,25 +207,35 @@ 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 (auto *Cast = dyn_cast<CastInst>(LU)) { | ||
spall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Ty = Cast->getType(); | ||
if (CallInst *CI = dyn_cast<CallInst>(LU)) { | ||
} else if (auto *CI = dyn_cast<CallInst>(LU)) { | ||
spall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (CI->getIntrinsicID() == Intrinsic::memset) | ||
Ty = Type::getInt32Ty(CI->getContext()); | ||
} | ||
|
||
if (!Ty) | ||
continue; | ||
|
||
if (!SmallestType || | ||
Ty->getPrimitiveSizeInBits() < SmallestType->getPrimitiveSizeInBits()) | ||
if (!SmallestType || Ty->getPrimitiveSizeInBits() < | ||
SmallestType->getPrimitiveSizeInBits()) { | ||
spall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.