Skip to content

[constant-folding] Fix asan error. #26609

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
Changes from all 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
48 changes: 25 additions & 23 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,25 @@ ConstantFolder::processWorkList() {
continue;
}

// See if we have a CondFailMessage that we can canonicalize.
if (isApplyOfBuiltin(*I, BuiltinValueKind::CondFailMessage)) {
// See if our operand is a string literal inst. In such a case, fold into
// cond_fail instruction.
if (auto *sli = dyn_cast<StringLiteralInst>(I->getOperand(1))) {
if (sli->getEncoding() == StringLiteralInst::Encoding::UTF8) {
SILBuilderWithScope builder(I);
auto *cfi = builder.createCondFail(I->getLoc(), I->getOperand(0),
sli->getValue());
WorkList.insert(cfi);
recursivelyDeleteTriviallyDeadInstructions(
I, /*force*/ true,
[&](SILInstruction *DeadI) { WorkList.remove(DeadI); });
InvalidateInstructions = true;
}
}
continue;
}

// Go through all users of the constant and try to fold them.
FoldedUsers.clear();
for (auto Result : I->getResults()) {
Expand Down Expand Up @@ -1784,29 +1803,12 @@ ConstantFolder::processWorkList() {
WorkList.insert(User);
}

// See if we have a CondFailMessage. If we do, see if we can transform
// it into a UTF8.
if (auto *bi = dyn_cast<BuiltinInst>(User)) {
if (auto kind = bi->getBuiltinKind()) {
if (*kind == BuiltinValueKind::CondFailMessage) {
// See if our original instruction was a string literal inst.
if (auto *sli = dyn_cast<StringLiteralInst>(I)) {
if (sli->getEncoding() == StringLiteralInst::Encoding::UTF8) {
SILBuilderWithScope builder(bi);
auto *cfi = builder.createCondFail(
bi->getLoc(), bi->getOperand(0), sli->getValue());
WorkList.insert(cfi);
recursivelyDeleteTriviallyDeadInstructions(
bi, /*force*/ true,
[&](SILInstruction *DeadI) { WorkList.remove(DeadI); });
InvalidateInstructions = true;
continue;
}
}

// If we weren't able to simplify into a cond_fail, add it to the
// folded user set to see if the condfail msg is dead.
FoldedUsers.insert(bi);
// See if we have a CondFailMessage of a string_Literal. If we do, add
// it to the worklist, so we can clean it up.
if (isApplyOfBuiltin(*User, BuiltinValueKind::CondFailMessage)) {
if (auto *sli = dyn_cast<StringLiteralInst>(I)) {
if (sli->getEncoding() == StringLiteralInst::Encoding::UTF8) {
WorkList.insert(User);
}
}
}
Expand Down