Skip to content

[SILCombine] Handle indirect error in apply(convert_function). #73408

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 4 commits into from
May 3, 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: 18 additions & 0 deletions include/swift/SIL/SILFunctionConventions.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ class SILFunctionConventions {
return 0;
}

std::optional<SILResultInfo> getIndirectErrorResult() const {
if (!silConv.loweredAddresses)
return std::nullopt;
auto info = funcTy->getOptionalErrorResult();
if (!info)
return std::nullopt;
if (info->getConvention() != ResultConvention::Indirect)
return std::nullopt;
return info;
}

SILType getIndirectErrorResultType(TypeExpansionContext context) const {
auto result = getIndirectErrorResult();
if (!result)
return SILType();
return getSILType(*result, context);
}

bool isArgumentIndexOfIndirectErrorResult(unsigned idx) {
unsigned indirectResults = getNumIndirectSILResults();
return idx >= indirectResults &&
Expand Down
4 changes: 4 additions & 0 deletions include/swift/SIL/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class FunctionTest final {
template <typename Analysis, typename Transform = SILFunctionTransform>
Analysis *getAnalysis();

SILFunctionTransform *getPass();

SwiftPassInvocation *getSwiftPassInvocation();

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -302,6 +304,8 @@ template <typename Analysis, typename Transform>
Analysis *FunctionTest::getAnalysis() {
return impl::getAnalysisFromTransform<Analysis, Transform>(pass);
}

inline SILFunctionTransform *FunctionTest::getPass() { return pass; }
} // namespace test
} // namespace swift

Expand Down
24 changes: 24 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SIL/Test.h"
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h"
Expand Down Expand Up @@ -498,6 +499,29 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
return MadeChange;
}

namespace swift::test {
// Arguments:
// - instruction: the instruction to be canonicalized
// Dumps:
// - the function after the canonicalization is attempted
static FunctionTest SILCombineCanonicalizeInstruction(
"sil_combine_instruction", [](auto &function, auto &arguments, auto &test) {
SILCombiner combiner(test.getPass(), false, false);
auto inst = arguments.takeInstruction();
combiner.Builder.setInsertionPoint(inst);
auto *result = combiner.visit(inst);
if (result) {
combiner.Worklist.replaceInstructionWithInstruction(inst, result
#ifndef NDEBUG
,
""
#endif
);
}
function.dump();
});
} // end namespace swift::test

bool SILCombiner::runOnFunction(SILFunction &F) {
clear();

Expand Down
6 changes: 5 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ class SILCombiner :
/// lifetimes in OSSA.
NonLocalAccessBlockAnalysis *NLABA;

public:
/// Worklist containing all of the instructions primed for simplification.
SmallSILInstructionWorklist<256> Worklist;

private:
/// Utility for dead code removal.
InstructionDeleter deleter;

Expand Down Expand Up @@ -102,10 +104,12 @@ class SILCombiner :
// The tracking list is used by `Builder` for newly added
// instructions, which we will periodically move to our worklist.
llvm::SmallVector<SILInstruction *, 64> TrackingList;


public:
/// Builder used to insert instructions.
SILBuilder Builder;

private:
SILOptFunctionBuilder FuncBuilder;

/// Cast optimizer
Expand Down
12 changes: 11 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
auto context = AI.getFunction()->getTypeExpansionContext();
auto oldOpRetTypes = substConventions.getIndirectSILResultTypes(context);
auto newOpRetTypes = convertConventions.getIndirectSILResultTypes(context);
auto oldIndirectErrorResultType =
substConventions.getIndirectErrorResultType(context);
auto newIndirectErrorResultType =
convertConventions.getIndirectErrorResultType(context);
auto oldOpParamTypes = substConventions.getParameterSILTypes(context);
auto newOpParamTypes = convertConventions.getParameterSILTypes(context);

Expand Down Expand Up @@ -186,7 +190,13 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
++OpI, ++newRetI, ++oldRetI) {
convertOp(Ops[OpI], *oldRetI, *newRetI);
}


if (oldIndirectErrorResultType) {
assert(newIndirectErrorResultType);
convertOp(Ops[OpI], oldIndirectErrorResultType, newIndirectErrorResultType);
++OpI;
}

auto newParamI = newOpParamTypes.begin();
auto oldParamI = oldOpParamTypes.begin();
for (auto e = newOpParamTypes.end(); newParamI != e;
Expand Down
34 changes: 34 additions & 0 deletions test/SILOptimizer/sil_combine_apply_unit.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -test-runner | %FileCheck %s

import Builtin

struct Input {}
struct Output {}
enum Nunca {}

sil @rdar127452206_callee : $@convention(thin) @Sendable @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_2, @error_indirect τ_0_1) for <Input, Nunca, Output>

// CHECK-LABEL: sil @rdar127452206 : {{.*}} {
// CHECK: bb0([[INPUT:%[^,]+]] :
// CHECK: [[OUTPUT:%[^,]+]] = alloc_stack $Output
// CHECK: [[NUNCA:%[^,]+]] = alloc_stack $Nunca
// CHECK: [[OUTPUT_AS_OUTPUT:%[^,]+]] = unchecked_addr_cast [[OUTPUT]]
// CHECK: [[NUNCA_AS_NUNCA:%[^,]+]] = unchecked_addr_cast [[NUNCA]]
// CHECK: [[INPUT_AS_INPUT:%[^,]+]] = unchecked_addr_cast [[INPUT]]
// CHECK: apply [nothrow] {{%[^,]+}}([[OUTPUT_AS_OUTPUT]], [[NUNCA_AS_NUNCA]], [[INPUT_AS_INPUT]])
// CHECK-LABEL: } // end sil function 'rdar127452206'
sil @rdar127452206 : $@convention(thin) (@in Input) -> () {
entry(%input : $*Input):
%output = alloc_stack $Output
%nunca = alloc_stack $Nunca
%callee = function_ref @rdar127452206_callee : $@convention(thin) @Sendable @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_2, @error_indirect τ_0_1) for <Input, Nunca, Output>
%convert = convert_function %callee : $@convention(thin) @Sendable @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_2, @error_indirect τ_0_1) for <Input, Nunca, Output> to $@convention(thin) @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_2, @error_indirect τ_0_1) for <Input, Nunca, Output> // user: %216
specify_test "sil_combine_instruction @instruction[4]"
apply [nothrow] %convert(%output, %nunca, %input) : $@convention(thin) @substituted <τ_0_0, τ_0_1, τ_0_2> (@in_guaranteed τ_0_0) -> (@out τ_0_2, @error_indirect τ_0_1) for <Input, Nunca, Output>
dealloc_stack %nunca : $*Nunca
dealloc_stack %output : $*Output
%retval = tuple ()
return %retval : $()
}