-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SandboxIR] Implement LoadInst #99597
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 all commits
Commits
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
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
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 |
---|---|---|
|
@@ -140,14 +140,14 @@ void Value::replaceAllUsesWith(Value *Other) { | |
} | ||
|
||
#ifndef NDEBUG | ||
std::string Value::getName() const { | ||
std::string Value::getUid() const { | ||
std::stringstream SS; | ||
SS << "SB" << UID << "."; | ||
return SS.str(); | ||
} | ||
|
||
void Value::dumpCommonHeader(raw_ostream &OS) const { | ||
OS << getName() << " " << getSubclassIDStr(SubclassID) << " "; | ||
OS << getUid() << " " << getSubclassIDStr(SubclassID) << " "; | ||
} | ||
|
||
void Value::dumpCommonFooter(raw_ostream &OS) const { | ||
|
@@ -167,7 +167,7 @@ void Value::dumpCommonPrefix(raw_ostream &OS) const { | |
} | ||
|
||
void Value::dumpCommonSuffix(raw_ostream &OS) const { | ||
OS << " ; " << getName() << " (" << getSubclassIDStr(SubclassID) << ")"; | ||
OS << " ; " << getUid() << " (" << getSubclassIDStr(SubclassID) << ")"; | ||
} | ||
|
||
void Value::printAsOperandCommon(raw_ostream &OS) const { | ||
|
@@ -453,6 +453,49 @@ void Instruction::dump() const { | |
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
#endif // NDEBUG | ||
|
||
LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align, | ||
Instruction *InsertBefore, Context &Ctx, | ||
const Twine &Name) { | ||
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction(); | ||
auto &Builder = Ctx.getLLVMIRBuilder(); | ||
Builder.SetInsertPoint(BeforeIR); | ||
auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, | ||
/*isVolatile=*/false, Name); | ||
auto *NewSBI = Ctx.createLoadInst(NewLI); | ||
return NewSBI; | ||
} | ||
|
||
LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align, | ||
BasicBlock *InsertAtEnd, Context &Ctx, | ||
const Twine &Name) { | ||
auto &Builder = Ctx.getLLVMIRBuilder(); | ||
Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val)); | ||
auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, | ||
/*isVolatile=*/false, Name); | ||
auto *NewSBI = Ctx.createLoadInst(NewLI); | ||
return NewSBI; | ||
} | ||
|
||
bool LoadInst::classof(const Value *From) { | ||
return From->getSubclassID() == ClassID::Load; | ||
} | ||
|
||
Value *LoadInst::getPointerOperand() const { | ||
return Ctx.getValue(cast<llvm::LoadInst>(Val)->getPointerOperand()); | ||
} | ||
|
||
#ifndef NDEBUG | ||
void LoadInst::dump(raw_ostream &OS) const { | ||
dumpCommonPrefix(OS); | ||
dumpCommonSuffix(OS); | ||
} | ||
|
||
void LoadInst::dump() const { | ||
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
|
||
void OpaqueInst::dump(raw_ostream &OS) const { | ||
dumpCommonPrefix(OS); | ||
|
@@ -538,8 +581,8 @@ Value *Context::registerValue(std::unique_ptr<Value> &&VPtr) { | |
assert(VPtr->getSubclassID() != Value::ClassID::User && | ||
"Can't register a user!"); | ||
Value *V = VPtr.get(); | ||
llvm::Value *Key = V->Val; | ||
LLVMValueToValueMap[Key] = std::move(VPtr); | ||
auto Pair = LLVMValueToValueMap.insert({VPtr->Val, std::move(VPtr)}); | ||
assert(Pair.second && "Already exists!"); | ||
return V; | ||
} | ||
|
||
|
@@ -568,6 +611,17 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) { | |
return nullptr; | ||
} | ||
assert(isa<llvm::Instruction>(LLVMV) && "Expected Instruction"); | ||
|
||
switch (cast<llvm::Instruction>(LLVMV)->getOpcode()) { | ||
case llvm::Instruction::Load: { | ||
auto *LLVMLd = cast<llvm::LoadInst>(LLVMV); | ||
It->second = std::unique_ptr<LoadInst>(new LoadInst(LLVMLd, *this)); | ||
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. Please use 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. Just like the lines above we can't use |
||
return It->second.get(); | ||
} | ||
default: | ||
break; | ||
} | ||
|
||
It->second = std::unique_ptr<OpaqueInst>( | ||
new OpaqueInst(cast<llvm::Instruction>(LLVMV), *this)); | ||
return It->second.get(); | ||
|
@@ -582,6 +636,11 @@ BasicBlock *Context::createBasicBlock(llvm::BasicBlock *LLVMBB) { | |
return BB; | ||
} | ||
|
||
LoadInst *Context::createLoadInst(llvm::LoadInst *LI) { | ||
auto NewPtr = std::unique_ptr<LoadInst>(new LoadInst(LI, *this)); | ||
return cast<LoadInst>(registerValue(std::move(NewPtr))); | ||
} | ||
|
||
Value *Context::getValue(llvm::Value *V) const { | ||
auto It = LLVMValueToValueMap.find(V); | ||
if (It != LLVMValueToValueMap.end()) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
final
confused me. It comes fromUser
. For style and safeness, I would mark it asoverride
first.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm why would you mark it as
override
?LoadInst
won't have subclasses so it should be safe to mark itfinal
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. But
getUseOperandNo
overrides a virtual = 0 fromUser
. Once you mark itoverride
the compiler will do checks for you.override final
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
override final
is redundant, and I am not sure it's being used in LLVM. If you usefinal
the compiler will check that you are actually overriding and print an error if not, and on top of that it will also check that it is final.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I found one occurrence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that
LoadInst
is final, the finals are kind of redundant. Feel free to ignore, butoverride
would be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't they still useful though? If you misspell a
final
function name you will still get an error, but you won't get one if you just rely on the class being markedfinal
.Well, I think I would prefer to stick with
final
because it conveys both that it's overriden and that it's final.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries. The class is marked final. You cannot inherit from anyway and overwriting the functions. My only confusion is that final is kind of redundant.
override
is less confusing and gives the same compiler checks.