Skip to content

[SILCombine] Dominance check for inject_enum_addr. #73040

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 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ static llvm::cl::opt<DebugOnlyPassNumberOpt, true,
llvm::cl::location(DebugOnlyPassNumberOptLoc),
llvm::cl::ValueRequired);

static llvm::cl::opt<bool> SILPrintEverySubpass(
"sil-print-every-subpass", llvm::cl::init(false),
llvm::cl::desc("Print the function before every subpass run of passes that "
"have multiple subpasses"));

static bool isInPrintFunctionList(SILFunction *F) {
for (const std::string &printFnName : SILPrintFunction) {
if (printFnName == F->getName())
Expand Down Expand Up @@ -478,13 +483,22 @@ bool SILPassManager::continueTransforming() {
bool SILPassManager::continueWithNextSubpassRun(SILInstruction *forInst,
SILFunction *function,
SILTransform *trans) {
unsigned subPass = numSubpassesRun++;

if (forInst && isFunctionSelectedForPrinting(function) &&
SILPrintEverySubpass) {
dumpPassInfo("*** SIL function before ", trans, function);
if (forInst) {
llvm::dbgs() << " *** sub-pass " << subPass << " for " << *forInst;
}
function->dump(getOptions().EmitVerboseSIL);
}

if (isMandatory)
return true;
if (NumPassesRun != maxNumPassesToRun - 1)
return true;

unsigned subPass = numSubpassesRun++;

if (subPass == maxNumSubpassesToRun - 1 && SILPrintLast) {
dumpPassInfo("*** SIL function before ", trans, function);
if (forInst) {
Expand Down
86 changes: 61 additions & 25 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,14 +865,20 @@ SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
return nullptr;
}

/// Create a value from stores to an address.
/// Whether there exists a unique value to which \p addr is always initialized
/// at \p forInst.
///
/// If there are only stores to \p addr, return the stored value. Also, if there
/// are address projections, create aggregate instructions for it.
/// If builder is null, it's just a dry-run to check if it's possible.
static SILValue createValueFromAddr(SILValue addr, SILBuilder *builder,
SILLocation loc) {
SmallVector<SILValue, 4> elems;
/// If \p builder is passed, create the value using it. If \p addr is
/// initialized piecewise via initializations of tuple element memory, the full
/// tuple is constructed via the builder.
///
/// A best effort.
/// TODO: Construct structs.
/// Handle stores of identical values on multiple paths.
static std::optional<std::pair<SILValue, SILInstruction *>>
createValueFromAddr(SILValue addr, SILInstruction *forInst, DominanceInfo *DI,
SILBuilder *builder, SILLocation loc) {
SmallVector<std::optional<std::pair<SILValue, SILInstruction *>>, 4> pairs;
enum Kind {
none, store, tuple
} kind = none;
Expand All @@ -884,7 +890,7 @@ static SILValue createValueFromAddr(SILValue addr, SILBuilder *builder,

auto *st = dyn_cast<StoreInst>(user);
if (st && kind == none && st->getDest() == addr) {
elems.push_back(st->getSrc());
pairs.push_back({{st->getSrc(), st}});
kind = store;
// We cannot just return st->getSrc() here because we also have to check
// if the store destination is the only use of addr.
Expand All @@ -893,35 +899,55 @@ static SILValue createValueFromAddr(SILValue addr, SILBuilder *builder,

if (auto *telem = dyn_cast<TupleElementAddrInst>(user)) {
if (kind == none) {
elems.resize(addr->getType().castTo<TupleType>()->getNumElements());
pairs.resize(addr->getType().castTo<TupleType>()->getNumElements());
kind = tuple;
}
if (kind == tuple) {
if (elems[telem->getFieldIndex()])
return SILValue();
elems[telem->getFieldIndex()] = createValueFromAddr(telem, builder, loc);
if (pairs[telem->getFieldIndex()]) {
// Already found a tuple_element_addr at this index. Assume that a
// different value is stored to addr by it.
return std::nullopt;
}
pairs[telem->getFieldIndex()] =
createValueFromAddr(telem, forInst, DI, builder, loc);
continue;
}
}
// TODO: handle StructElementAddrInst to create structs.

return SILValue();
return std::nullopt;
}
switch (kind) {
case none:
return SILValue();
return std::nullopt;
case store:
assert(elems.size() == 1);
return elems[0];
assert(pairs.size() == 1);
{
auto pair = pairs[0];
assert(pair.has_value());
bool isEmpty = pair->first->getType().isEmpty(*addr->getFunction());
if (isEmpty && !DI->properlyDominates(pair->second, forInst))
return std::nullopt;
Copy link
Contributor

@meg-gupta meg-gupta Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like without the dominance check here, we can have bad SIL with non empty types too!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we can't have them here either because that would imply there was a memory location whose type is non-empty which wasn't initialized on some path, which is invalid SIL.

return pair;
}
case tuple:
if (std::any_of(elems.begin(), elems.end(),
[](SILValue v){ return !(bool)v; }))
return SILValue();
if (std::any_of(pairs.begin(), pairs.end(), [&](auto pair) {
return !pair.has_value() ||
(pair->first->getType().isEmpty(*addr->getFunction()) &&
!DI->properlyDominates(pair->second, forInst));
}))
return std::nullopt;
if (builder) {
return builder->createTuple(loc, addr->getType().getObjectType(), elems);
SmallVector<SILValue, 4> elements;
for (auto pair : pairs) {
elements.push_back(pair->first);
}
auto *tuple =
builder->createTuple(loc, addr->getType().getObjectType(), elements);
return {{tuple, tuple}};
}
// Just return anything not null for the dry-run.
return elems[0];
return pairs[0];
}
llvm_unreachable("invalid kind");
}
Expand Down Expand Up @@ -1185,9 +1211,12 @@ SILCombiner::visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI) {
// store %payload1 to %elem1_addr
// inject_enum_addr %enum_addr, $EnumType.case
//
if (createValueFromAddr(DataAddrInst, nullptr, DataAddrInst->getLoc())) {
SILValue en =
createValueFromAddr(DataAddrInst, &Builder, DataAddrInst->getLoc());
auto DI = DA->get(IEAI->getFunction());
if (createValueFromAddr(DataAddrInst, IEAI, DI, nullptr,
DataAddrInst->getLoc())) {
SILValue en = createValueFromAddr(DataAddrInst, IEAI, DI, &Builder,
DataAddrInst->getLoc())
->first;
assert(en);

// In that case, create the payload enum/store.
Expand Down Expand Up @@ -1224,7 +1253,14 @@ SILCombiner::visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI) {
// store %1 to %nopayload_addr
//
auto *AI = dyn_cast_or_null<ApplyInst>(getSingleNonDebugUser(DataAddrInst));
if (!AI)
bool hasEmptyAssociatedType =
IEAI->getElement()->hasAssociatedValues()
? IEAI->getOperand()
->getType()
.getEnumElementType(IEAI->getElement(), func)
.isEmpty(*func)
: false;
if (!AI || (hasEmptyAssociatedType && !DI->properlyDominates(AI, IEAI)))
return nullptr;

unsigned ArgIdx = 0;
Expand Down
Loading