Skip to content

Commit e37ec21

Browse files
committed
add a semantic attribute "optimize.sil.inline.constant.arguments"
This forces inlining the annotated function if its arguments are constant.
1 parent 98d9b1c commit e37ec21

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ SEMANTICS_ATTR(TYPENAME, "typeName")
7171
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_NEVER, "optimize.sil.specialize.generic.never")
7272
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_PARTIAL_NEVER,
7373
"optimize.sil.specialize.generic.partial.never")
74+
SEMANTICS_ATTR(OPTIMIZE_SIL_INLINE_CONSTANT_ARGUMENTS,
75+
"optimize.sil.inline.constant.arguments")
7476
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
7577
"optimize.sil.specialize.generic.size.never")
7678
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_OWNED2GUARANTEE_NEVER,

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,33 @@ bool isProfitableToInlineAutodiffVJP(SILFunction *vjp, SILFunction *caller,
456456
return true;
457457
}
458458

459+
static bool isConstantValue(SILValue v, ValueSet &visited) {
460+
if (!visited.insert(v))
461+
return true;
462+
463+
if (isa<LiteralInst>(v))
464+
return true;
465+
if (auto *s = dyn_cast<StructInst>(v)) {
466+
for (Operand &op : s->getAllOperands()) {
467+
if (!isConstantValue(op.get(), visited))
468+
return false;
469+
}
470+
return true;
471+
}
472+
return false;
473+
}
474+
475+
static bool hasConstantArguments(FullApplySite fas) {
476+
ValueSet visited(fas.getFunction());
477+
for (Operand &op : fas.getArgumentOperands()) {
478+
if (!fas.isIndirectResultOperand(op)) {
479+
if (!isConstantValue(op.get(), visited))
480+
return false;
481+
}
482+
}
483+
return true;
484+
}
485+
459486
bool SILPerformanceInliner::isProfitableToInline(
460487
FullApplySite AI, Weight CallerWeight, ConstantTracker &callerTracker,
461488
int &NumCallerBlocks,
@@ -524,6 +551,11 @@ bool SILPerformanceInliner::isProfitableToInline(
524551
return true;
525552
}
526553

554+
if (Callee->hasSemanticsAttr(semantics::OPTIMIZE_SIL_INLINE_CONSTANT_ARGUMENTS) &&
555+
hasConstantArguments(AI)) {
556+
return true;
557+
}
558+
527559
// Bail out if this generic call can be optimized by means of
528560
// the generic specialization, because we prefer generic specialization
529561
// to inlining of generics.

0 commit comments

Comments
 (0)