Skip to content

Commit ec2d7fc

Browse files
authored
Merge pull request #62198 from nate-chandler/opaque-values/3/20221118
[AddressLowering] Don't end_borrow trivial args.
2 parents cded32f + dbda1bf commit ec2d7fc

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,14 @@ static void convertDirectToIndirectFunctionArgs(AddressLoweringState &pass) {
515515
auto loc = SILValue(arg).getLoc();
516516
SILValue undefAddress = SILUndef::get(addrType, *pass.function);
517517
SingleValueInstruction *load;
518-
if (param.isConsumed()) {
519-
load = argBuilder.createTrivialLoadOr(loc, undefAddress,
520-
LoadOwnershipQualifier::Take);
518+
if (addrType.isTrivial(*pass.function)) {
519+
load = argBuilder.createLoad(loc, undefAddress,
520+
LoadOwnershipQualifier::Trivial);
521+
} else if (param.isConsumed()) {
522+
load = argBuilder.createLoad(loc, undefAddress,
523+
LoadOwnershipQualifier::Take);
521524
} else {
522-
load = cast<SingleValueInstruction>(
523-
argBuilder.emitLoadBorrowOperation(loc, undefAddress));
525+
load = argBuilder.createLoadBorrow(loc, undefAddress);
524526
for (SILInstruction *termInst : pass.exitingInsts) {
525527
pass.getBuilder(termInst->getIterator())
526528
.createEndBorrow(pass.genLoc(), load);

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ STATISTIC(NumMandatoryInlines,
4040
"Number of function application sites inlined by the mandatory "
4141
"inlining pass");
4242

43+
//===----------------------------------------------------------------------===//
44+
// Printing Helpers
45+
//===----------------------------------------------------------------------===//
46+
47+
extern llvm::cl::opt<bool> SILPrintInliningCallee;
48+
49+
extern llvm::cl::opt<bool> SILPrintInliningCallerBefore;
50+
51+
extern llvm::cl::opt<bool> SILPrintInliningCallerAfter;
52+
53+
extern void printInliningDetailsCallee(StringRef passName, SILFunction *caller,
54+
SILFunction *callee);
55+
56+
extern void printInliningDetailsCallerBefore(StringRef passName,
57+
SILFunction *caller,
58+
SILFunction *callee);
59+
60+
extern void printInliningDetailsCallerAfter(StringRef passName,
61+
SILFunction *caller,
62+
SILFunction *callee);
63+
4364
template<typename...T, typename...U>
4465
static void diagnose(ASTContext &Context, SourceLoc loc, Diag<T...> diag,
4566
U &&...args) {
@@ -923,11 +944,21 @@ runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILFunction *F,
923944

924945
invalidatedStackNesting |= Inliner.invalidatesStackNesting(InnerAI);
925946

947+
if (SILPrintInliningCallee) {
948+
printInliningDetailsCallee("MandatoryInlining", F, CalleeFunction);
949+
}
950+
if (SILPrintInliningCallerBefore) {
951+
printInliningDetailsCallerBefore("MandatoryInlining", F,
952+
CalleeFunction);
953+
}
926954
// Inlining deletes the apply, and can introduce multiple new basic
927955
// blocks. After this, CalleeValue and other instructions may be invalid.
928956
// nextBB will point to the last inlined block
929957
SILBasicBlock *lastBB =
930958
Inliner.inlineFunction(CalleeFunction, InnerAI, FullArgs);
959+
if (SILPrintInliningCallerAfter) {
960+
printInliningDetailsCallerAfter("MandatoryInlining", F, CalleeFunction);
961+
}
931962
nextBB = lastBB->getReverseIterator();
932963
++NumMandatoryInlines;
933964

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ bool isFunctionSelectedForPrinting(SILFunction *F) {
182182
return true;
183183
}
184184

185+
void printInliningDetails(StringRef passName, SILFunction *caller,
186+
SILFunction *callee, bool isCaller,
187+
bool alreadyInlined) {
188+
if (!isFunctionSelectedForPrinting(caller))
189+
return;
190+
llvm::dbgs() << " " << passName
191+
<< (alreadyInlined ? " has inlined " : " will inline ")
192+
<< callee->getName() << " into " << caller->getName() << ".\n";
193+
auto *printee = isCaller ? caller : callee;
194+
printee->dump(caller->getModule().getOptions().EmitVerboseSIL);
195+
llvm::dbgs() << '\n';
196+
}
197+
198+
void printInliningDetailsCallee(StringRef passName, SILFunction *caller,
199+
SILFunction *callee) {
200+
printInliningDetails(passName, caller, callee, /*isCaller=*/false,
201+
/*alreadyInlined=*/false);
202+
}
203+
204+
void printInliningDetailsCallerBefore(StringRef passName, SILFunction *caller,
205+
SILFunction *callee) {
206+
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
207+
/*alreadyInlined=*/false);
208+
}
209+
210+
void printInliningDetailsCallerAfter(StringRef passName, SILFunction *caller,
211+
SILFunction *callee) {
212+
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
213+
/*alreadyInlined=*/true);
214+
}
215+
185216
static bool functionSelectionEmpty() {
186217
return SILPrintFunction.empty() && SILPrintFunctions.empty();
187218
}

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,16 @@ llvm::cl::opt<bool> SILPrintInliningCallerAfter(
6767
// Printing Helpers
6868
//===----------------------------------------------------------------------===//
6969

70-
extern bool isFunctionSelectedForPrinting(SILFunction *F);
70+
extern void printInliningDetailsCallee(StringRef passName, SILFunction *caller,
71+
SILFunction *callee);
7172

72-
static void printInliningDetails(StringRef passName, SILFunction *caller,
73-
SILFunction *callee, bool isCaller,
74-
bool alreadyInlined) {
75-
if (!isFunctionSelectedForPrinting(caller))
76-
return;
77-
llvm::dbgs() << " " << passName
78-
<< (alreadyInlined ? " has inlined " : " will inline ")
79-
<< callee->getName() << " into " << caller->getName() << ".\n";
80-
auto *printee = isCaller ? caller : callee;
81-
printee->dump(caller->getModule().getOptions().EmitVerboseSIL);
82-
llvm::dbgs() << '\n';
83-
}
84-
85-
static void printInliningDetailsCallee(StringRef passName, SILFunction *caller,
86-
SILFunction *callee) {
87-
printInliningDetails(passName, caller, callee, /*isCaller=*/false,
88-
/*alreadyInlined=*/false);
89-
}
90-
91-
static void printInliningDetailsCallerBefore(StringRef passName,
73+
extern void printInliningDetailsCallerBefore(StringRef passName,
9274
SILFunction *caller,
93-
SILFunction *callee) {
94-
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
95-
/*alreadyInlined=*/false);
96-
}
75+
SILFunction *callee);
9776

98-
static void printInliningDetailsCallerAfter(StringRef passName,
77+
extern void printInliningDetailsCallerAfter(StringRef passName,
9978
SILFunction *caller,
100-
SILFunction *callee) {
101-
printInliningDetails(passName, caller, callee, /*isCaller=*/true,
102-
/*alreadyInlined=*/true);
103-
}
79+
SILFunction *callee);
10480

10581
//===----------------------------------------------------------------------===//
10682
// Performance Inliner

test/SILOptimizer/address_lowering.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,28 @@ bb3(%15 : @owned $T):
10681068
return %15 : $T
10691069
}
10701070

1071+
// Verify that trivial arguments are loaded trivially and for which no
1072+
// end_borrows were created.
1073+
// CHECK-LABEL: sil [ossa] @f171_subtract_overflow : {{.*}} {
1074+
// CHECK: {{bb[0-9]+}}([[RETVAL:%[^,]+]] : $*Builtin.Int64, [[LHS_ADDR:%[^,]+]] : $*Builtin.Int64, [[RHS_ADDR:%[^,]+]] :
1075+
// CHECK: [[LHS:%[^,]+]] = load [trivial] [[LHS_ADDR]]
1076+
// CHECK: [[RHS:%[^,]+]] = load [trivial] [[RHS_ADDR]]
1077+
// CHECK: [[SUM_AND:%[^,]+]] = apply {{%[^,]+}}([[LHS]], [[RHS]])
1078+
// CHECK-NOT: end_borrow
1079+
// CHECK: ([[SUM:%[^,]+]], [[OVERFLOWED:%[^,]+]]) = destructure_tuple [[SUM_AND]]
1080+
// CHECK: store [[SUM]] to [trivial] [[RETVAL]]
1081+
// CHECK: return [[OVERFLOWED]]
1082+
// CHECK-LABEL: } // end sil function 'f171_subtract_overflow'
1083+
sil [ossa] @f171_subtract_overflow : $@convention(thin) (@in_guaranteed Int, @in_guaranteed Int) -> (@out Int, Bool) {
1084+
bb0(%0 : $Int, %1 : $Int):
1085+
%2 = function_ref @f171_subtract_overflow_callee : $@convention(method) (Int, Int) -> (Int, Bool)
1086+
%3 = apply %2(%0, %1) : $@convention(method) (Int, Int) -> (Int, Bool)
1087+
(%4, %5) = destructure_tuple %3 : $(Int, Bool)
1088+
%6 = tuple (%4 : $Int, %5 : $Bool)
1089+
return %6 : $(Int, Bool)
1090+
}
1091+
sil [ossa] @f171_subtract_overflow_callee : $@convention(method) (Int, Int) -> (Int, Bool)
1092+
10711093
// Test switching on a single opaque value.
10721094
// CHECK-LABEL: sil [ossa] @f210_testSwitchEnum : $@convention(method) <T> (@in Optional<T>, @inout T) -> () {
10731095
// CHECK: bb0(%0 : $*Optional<T>, %1 : $*T):

0 commit comments

Comments
 (0)