-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[SandboxIR][Region] Implement an auxiliary vector in Region #126376
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,41 @@ void Region::add(Instruction *I) { | |
Scoreboard.add(I); | ||
} | ||
|
||
void Region::setAux(ArrayRef<Instruction *> Aux) { | ||
this->Aux = SmallVector<Instruction *>(Aux); | ||
auto &LLVMCtx = Ctx.LLVMCtx; | ||
for (auto [Idx, I] : enumerate(Aux)) { | ||
llvm::ConstantInt *IdxC = | ||
llvm::ConstantInt::get(LLVMCtx, llvm::APInt(32, Idx, false)); | ||
assert(cast<llvm::Instruction>(I->Val)->getMetadata(AuxMDKind) == nullptr && | ||
"Instruction already in Aux!"); | ||
cast<llvm::Instruction>(I->Val)->setMetadata( | ||
AuxMDKind, MDNode::get(LLVMCtx, ConstantAsMetadata::get(IdxC))); | ||
} | ||
} | ||
|
||
void Region::setAux(unsigned Idx, Instruction *I) { | ||
assert((Idx >= Aux.size() || Aux[Idx] == nullptr) && | ||
"There is already an Instruction at Idx in Aux!"); | ||
unsigned ExpectedSz = Idx + 1; | ||
if (Aux.size() < ExpectedSz) { | ||
auto SzBefore = Aux.size(); | ||
Aux.resize(ExpectedSz); | ||
// Initialize the gap with nullptr. | ||
for (unsigned Idx = SzBefore; Idx + 1 < ExpectedSz; ++Idx) | ||
Aux[Idx] = nullptr; | ||
} | ||
Aux[Idx] = I; | ||
} | ||
|
||
void Region::clearAux() { | ||
for (unsigned Idx : seq<unsigned>(0, Aux.size())) { | ||
auto *LLVMI = cast<llvm::Instruction>(Aux[Idx]->Val); | ||
LLVMI->setMetadata(AuxMDKind, nullptr); | ||
} | ||
Aux.clear(); | ||
} | ||
|
||
void Region::remove(Instruction *I) { | ||
// Keep track of the instruction cost. This need to be done *before* we remove | ||
// `I` from the region. | ||
|
@@ -78,6 +113,15 @@ bool Region::operator==(const Region &Other) const { | |
void Region::dump(raw_ostream &OS) const { | ||
for (auto *I : Insts) | ||
OS << *I << "\n"; | ||
if (!Aux.empty()) { | ||
OS << "\nAux:\n"; | ||
for (auto *I : Aux) { | ||
if (I == nullptr) | ||
OS << "NULL\n"; | ||
else | ||
OS << *I << "\n"; | ||
} | ||
} | ||
} | ||
|
||
void Region::dump() const { | ||
|
@@ -93,16 +137,34 @@ Region::createRegionsFromMD(Function &F, TargetTransformInfo &TTI) { | |
auto &Ctx = F.getContext(); | ||
for (BasicBlock &BB : F) { | ||
for (Instruction &Inst : BB) { | ||
if (auto *MDN = cast<llvm::Instruction>(Inst.Val)->getMetadata(MDKind)) { | ||
auto *LLVMI = cast<llvm::Instruction>(Inst.Val); | ||
if (auto *MDN = LLVMI->getMetadata(MDKind)) { | ||
Region *R = nullptr; | ||
auto [It, Inserted] = MDNToRegion.try_emplace(MDN); | ||
if (Inserted) { | ||
Regions.push_back(std::make_unique<Region>(Ctx, TTI)); | ||
It->second = Regions.back().get(); | ||
R = Regions.back().get(); | ||
It->second = R; | ||
} else { | ||
R = It->second; | ||
} | ||
R->add(&Inst); | ||
|
||
if (auto *AuxMDN = LLVMI->getMetadata(AuxMDKind)) { | ||
llvm::Constant *IdxC = | ||
dyn_cast<ConstantAsMetadata>(AuxMDN->getOperand(0))->getValue(); | ||
auto Idx = cast<llvm::ConstantInt>(IdxC)->getSExtValue(); | ||
R->setAux(Idx, &Inst); | ||
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. Here we could have a check that we don't have two different values whose metadata claims the same index in the aux vector. 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. I added a check in 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. You could have incorrect metadata, right? Something like
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. You are right, I think I should be able to add a test for it too. |
||
} | ||
It->second->add(&Inst); | ||
} | ||
} | ||
} | ||
#ifndef NDEBUG | ||
// Check that there are no gaps in the Aux vector. | ||
for (auto &RPtr : Regions) | ||
for (auto *I : RPtr->getAux()) | ||
assert(I != nullptr && "Gap in Aux!"); | ||
#endif | ||
return Regions; | ||
} | ||
|
||
|
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.
https://discourse.llvm.org/t/second-stage-of-build-on-windows-fails-in-sandboxir/84841
@vporpo - could you replace the APInt with the ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) variant?
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.
@RKSimon Thanks for letting me know about the issue. I just created a PR with your suggestion: #129082