Skip to content

[NFC] [Package CMO] Move shouldSerialize into canSerializeFunction #77054

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 1 commit into from
Oct 16, 2024
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
69 changes: 26 additions & 43 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ class CrossModuleOptimization {

bool canUseFromInline(SILFunction *func);

bool shouldSerialize(SILFunction *F);

void serializeFunction(SILFunction *function,
const FunctionFlags &canSerializeFlags);

Expand Down Expand Up @@ -510,10 +508,34 @@ bool CrossModuleOptimization::canSerializeFunction(
return false;
}

// Ask the heuristic.
if (!shouldSerialize(function))
if (function->hasSemanticsAttr("optimize.no.crossmodule"))
return false;

// If package-cmo is enabled, we don't want to limit inlining
// or should at least increase the size limit.
bool skipSizeLimitCheck = isPackageCMOEnabled(M.getSwiftModule());

if (!conservative) {
// The basic heuristic: serialize all generic functions, because it makes a
// huge difference if generic functions can be specialized or not.
if (function->getLoweredFunctionType()->isPolymorphic())
skipSizeLimitCheck = true;
if (function->getLinkage() == SILLinkage::Shared)
skipSizeLimitCheck = true;
}

if (!skipSizeLimitCheck) {
// Also serialize "small" non-generic functions.
int size = 0;
for (SILBasicBlock &block : *function) {
for (SILInstruction &inst : block) {
size += (int)instructionInlineCost(inst);
if (size >= CMOFunctionSizeLimit)
return false;
}
}
}

// Check if any instruction prevents serializing the function.
InstructionVisitor visitor(*function, *this, InstructionVisitor::VisitMode::DetectSerializableInst);

Expand Down Expand Up @@ -767,45 +789,6 @@ bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
return true;
}

/// Decide whether to serialize a function.
bool CrossModuleOptimization::shouldSerialize(SILFunction *function) {
// Check if we already handled this function before.
if (isSerializedWithRightKind(M, function))
return false;

if (everything)
return true;

if (function->hasSemanticsAttr("optimize.no.crossmodule"))
return false;

if (!conservative) {
// The basic heuristic: serialize all generic functions, because it makes a
// huge difference if generic functions can be specialized or not.
if (function->getLoweredFunctionType()->isPolymorphic())
return true;

if (function->getLinkage() == SILLinkage::Shared)
return true;
}

// If package-cmo is enabled, we don't want to limit inlining
// or should at least increase the cap.
if (!M.getSwiftModule()->serializePackageEnabled()) {
// Also serialize "small" non-generic functions.
int size = 0;
for (SILBasicBlock &block : *function) {
for (SILInstruction &inst : block) {
size += (int)instructionInlineCost(inst);
if (size >= CMOFunctionSizeLimit)
return false;
}
}
}

return true;
}

/// Serialize \p function and recursively all referenced functions which are
/// marked in \p canSerializeFlags.
void CrossModuleOptimization::serializeFunction(SILFunction *function,
Expand Down