Skip to content

Commit cf6c4c9

Browse files
authored
Merge pull request swiftlang#79699 from meg-gupta/prboundscheck
Add support for bounds check optimization of Span and InlineArray
2 parents 92ead52 + 1fd2689 commit cf6c4c9

21 files changed

+872
-427
lines changed

include/swift/AST/SemanticAttrs.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,8 @@ SEMANTICS_ATTR(DELETE_IF_UNUSED, "sil.optimizer.delete_if_unused")
160160
// Force the use of the frame pointer for the specified function
161161
SEMANTICS_ATTR(USE_FRAME_POINTER, "use_frame_pointer")
162162

163+
SEMANTICS_ATTR(FIXED_STORAGE_CHECK_INDEX, "fixed_storage.check_index")
164+
SEMANTICS_ATTR(FIXED_STORAGE_GET_COUNT, "fixed_storage.get_count")
165+
163166
#undef SEMANTICS_ATTR
164167

include/swift/SIL/InstWrappers.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SIL_WRAPPERTYPES_H
1414
#define SWIFT_SIL_WRAPPERTYPES_H
1515

16+
#include "swift/SIL/SILFunction.h"
1617
#include "swift/SIL/SILInstruction.h"
1718

1819
namespace swift {
@@ -361,6 +362,43 @@ class ForwardingOperation {
361362
// operation.
362363
bool visitForwardedValues(function_ref<bool(SILValue)> visitor);
363364
};
365+
366+
enum class FixedStorageSemanticsCallKind { None, CheckIndex, GetCount };
367+
368+
struct FixedStorageSemanticsCall {
369+
ApplyInst *apply = nullptr;
370+
FixedStorageSemanticsCallKind kind = FixedStorageSemanticsCallKind::None;
371+
372+
FixedStorageSemanticsCall(SILInstruction *input) {
373+
auto *applyInst = dyn_cast<ApplyInst>(input);
374+
if (!applyInst) {
375+
return;
376+
}
377+
auto *callee = applyInst->getReferencedFunctionOrNull();
378+
if (!callee) {
379+
return;
380+
}
381+
for (auto &attr : callee->getSemanticsAttrs()) {
382+
if (attr == "fixed_storage.check_index") {
383+
apply = applyInst;
384+
kind = FixedStorageSemanticsCallKind::CheckIndex;
385+
break;
386+
} else if (attr == "fixed_storage.get_count") {
387+
apply = applyInst;
388+
kind = FixedStorageSemanticsCallKind::GetCount;
389+
break;
390+
}
391+
}
392+
}
393+
394+
FixedStorageSemanticsCallKind getKind() const { return kind; }
395+
explicit operator bool() const { return apply != nullptr; }
396+
const ApplyInst *operator->() const { return apply; }
397+
ApplyInst *operator->() { return apply; }
398+
};
399+
400+
bool isFixedStorageSemanticsCallKind(SILFunction *function);
401+
364402
} // end namespace swift
365403

366404
#endif

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686

8787
SWIFT_FUNCTION_PASS(AliasInfoDumper, "dump-alias-info",
8888
"Dump Alias Analysis over all Pairs")
89-
PASS(ABCOpt, "abcopts",
90-
"Array Bounds Check Optimization")
89+
PASS(BoundsCheckOpts, "bcopts",
90+
"Bounds Check Optimization")
9191
PASS(AccessEnforcementDom, "access-enforcement-dom",
9292
"Remove dominated access checks with no nested conflict")
9393
PASS(AccessEnforcementOpts, "access-enforcement-opts",

lib/SIL/IR/SILInstruction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,11 +1448,11 @@ bool SILInstruction::isTriviallyDuplicatable() const {
14481448
if (auto *PA = dyn_cast<PartialApplyInst>(this)) {
14491449
return !PA->isOnStack();
14501450
}
1451-
// Like partial_apply [onstack], mark_dependence [nonescaping] creates a
1452-
// borrow scope. We currently assume that a set of dominated scope-ending uses
1453-
// can be found.
1451+
// Like partial_apply [onstack], mark_dependence [nonescaping] on values
1452+
// creates a borrow scope. We currently assume that a set of dominated
1453+
// scope-ending uses can be found.
14541454
if (auto *MD = dyn_cast<MarkDependenceInst>(this)) {
1455-
return !MD->isNonEscaping();
1455+
return !MD->isNonEscaping() || MD->getType().isAddress();
14561456
}
14571457

14581458
if (isa<OpenExistentialAddrInst>(this) || isa<OpenExistentialRefInst>(this) ||

lib/SIL/Utils/InstWrappers.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,13 @@ bool ForwardingOperation::visitForwardedValues(
9999
return visitor(args[0]);
100100
});
101101
}
102+
103+
bool swift::isFixedStorageSemanticsCallKind(SILFunction *function) {
104+
for (auto &attr : function->getSemanticsAttrs()) {
105+
if (attr == "fixed_storage.check_index" ||
106+
attr == "fixed_storage.get_count") {
107+
return true;
108+
}
109+
}
110+
return false;
111+
}

0 commit comments

Comments
 (0)