@@ -2071,6 +2071,32 @@ static bool parseAssignOwnershipQualifier(AssignOwnershipQualifier &Result,
2071
2071
return false ;
2072
2072
}
2073
2073
2074
+ // SWIFT_ENABLE_TENSORFLOW
2075
+ // Parse a list of integer indices, prefaced with the given string label.
2076
+ // Returns true on error.
2077
+ static bool parseIndexList (Parser &P, StringRef label,
2078
+ SmallVectorImpl<unsigned > &indices,
2079
+ const Diagnostic &parseIndexDiag) {
2080
+ SourceLoc loc;
2081
+ // Parse `[<label> <integer_literal>...]`.
2082
+ if (P.parseToken (tok::l_square, diag::sil_autodiff_expected_lsquare,
2083
+ " index list" ) ||
2084
+ P.parseSpecificIdentifier (
2085
+ label, diag::sil_autodiff_expected_index_list_label, label))
2086
+ return true ;
2087
+ while (P.Tok .is (tok::integer_literal)) {
2088
+ unsigned index;
2089
+ if (P.parseUnsignedInteger (index, loc, parseIndexDiag))
2090
+ return true ;
2091
+ indices.push_back (index);
2092
+ }
2093
+ if (P.parseToken (tok::r_square, diag::sil_autodiff_expected_rsquare,
2094
+ " index list" ))
2095
+ return true ;
2096
+ return false ;
2097
+ };
2098
+ // SWIFT_ENABLE_TENSORFLOW END
2099
+
2074
2100
// SWIFT_ENABLE_TENSORFLOW
2075
2101
// / sil-differentiability-witness-config-and-function ::=
2076
2102
// / '[' 'parameters' index-subset ']'
@@ -2083,35 +2109,14 @@ static bool parseAssignOwnershipQualifier(AssignOwnershipQualifier &Result,
2083
2109
static Optional<std::pair<AutoDiffConfig, SILFunction *>>
2084
2110
parseSILDifferentiabilityWitnessConfigAndFunction (Parser &P, SILParser &SP,
2085
2111
SILLocation L) {
2086
- SourceLoc lastLoc;
2087
- // Parse an index set, prefaced with the given label.
2088
- auto parseIndexSet = [&](StringRef label, SmallVectorImpl<unsigned > &indices,
2089
- const Diagnostic &parseIndexDiag) -> bool {
2090
- // Parse `[<label> <integer_literal>...]`.
2091
- if (P.parseToken (tok::l_square, diag::sil_autodiff_expected_lsquare,
2092
- " index list" ) ||
2093
- P.parseSpecificIdentifier (
2094
- label, diag::sil_autodiff_expected_index_list_label, label))
2095
- return true ;
2096
- while (P.Tok .is (tok::integer_literal)) {
2097
- unsigned index;
2098
- if (P.parseUnsignedInteger (index, lastLoc, parseIndexDiag))
2099
- return true ;
2100
- indices.push_back (index);
2101
- }
2102
- if (P.parseToken (tok::r_square, diag::sil_autodiff_expected_rsquare,
2103
- " index list" ))
2104
- return true ;
2105
- return false ;
2106
- };
2107
2112
// Parse parameter and result indices.
2108
2113
SmallVector<unsigned , 8 > parameterIndices;
2109
2114
SmallVector<unsigned , 8 > resultIndices;
2110
- if (parseIndexSet ( " parameters" , parameterIndices,
2111
- diag::sil_autodiff_expected_parameter_index))
2115
+ if (parseIndexList (P, " parameters" , parameterIndices,
2116
+ diag::sil_autodiff_expected_parameter_index))
2112
2117
return {};
2113
- if (parseIndexSet ( " results" , resultIndices,
2114
- diag::sil_autodiff_expected_result_index))
2118
+ if (parseIndexList (P, " results" , resultIndices,
2119
+ diag::sil_autodiff_expected_result_index))
2115
2120
return {};
2116
2121
// Parse witness generic parameter clause.
2117
2122
GenericSignature witnessGenSig = GenericSignature ();
@@ -2938,27 +2943,12 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
2938
2943
//
2939
2944
// e.g. differentiable_function [parameters 0 1 2] %0 : $T with_derivative
2940
2945
// {%1 : $T, %2 : $T}
2941
- // ^ jvp ^ vjp
2942
- SourceLoc lastLoc;
2946
+ // ^~ jvp ^~ vjp
2947
+ // Parse `[parameters <integer_literal>...]`.
2943
2948
SmallVector<unsigned , 8 > parameterIndices;
2944
- // Parse optional `[parameters <integer_literal>...]`
2945
- if (P.Tok .is (tok::l_square) &&
2946
- P.peekToken ().is (tok::identifier) &&
2947
- P.peekToken ().getText () == " parameters" ) {
2948
- P.consumeToken (tok::l_square);
2949
- P.consumeToken (tok::identifier);
2950
- // Parse indices.
2951
- while (P.Tok .is (tok::integer_literal)) {
2952
- unsigned index;
2953
- if (P.parseUnsignedInteger (index, lastLoc,
2954
- diag::sil_autodiff_expected_parameter_index))
2955
- return true ;
2956
- parameterIndices.push_back (index);
2957
- }
2958
- if (P.parseToken (tok::r_square, diag::sil_autodiff_expected_rsquare,
2959
- " parameter index list" ))
2960
- return true ;
2961
- }
2949
+ if (parseIndexList (P, " parameters" , parameterIndices,
2950
+ diag::sil_autodiff_expected_parameter_index))
2951
+ return true ;
2962
2952
// Parse the original function value.
2963
2953
SILValue original;
2964
2954
SourceLoc originalOperandLoc;
@@ -3001,26 +2991,11 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
3001
2991
case SILInstructionKind::LinearFunctionInst: {
3002
2992
// e.g. linear_function [parameters 0 1 2] %0 : $T
3003
2993
// e.g. linear_function [parameters 0 1 2] %0 : $T with_transpose %1 : $T
3004
- SourceLoc lastLoc;
2994
+ // Parse `[parameters <integer_literal>...]`.
3005
2995
SmallVector<unsigned , 8 > parameterIndices;
3006
- // Parse optional `[parameters <integer_literal>...]`
3007
- if (P.Tok .is (tok::l_square) &&
3008
- P.peekToken ().is (tok::identifier) &&
3009
- P.peekToken ().getText () == " parameters" ) {
3010
- P.consumeToken (tok::l_square);
3011
- P.consumeToken (tok::identifier);
3012
- // Parse indices.
3013
- while (P.Tok .is (tok::integer_literal)) {
3014
- unsigned index;
3015
- if (P.parseUnsignedInteger (index, lastLoc,
3016
- diag::sil_autodiff_expected_parameter_index))
3017
- return true ;
3018
- parameterIndices.push_back (index);
3019
- }
3020
- if (P.parseToken (tok::r_square, diag::sil_autodiff_expected_rsquare,
3021
- " parameter index list" ))
3022
- return true ;
3023
- }
2996
+ if (parseIndexList (P, " parameters" , parameterIndices,
2997
+ diag::sil_autodiff_expected_parameter_index))
2998
+ return true ;
3024
2999
// Parse the original function value.
3025
3000
SILValue original;
3026
3001
SourceLoc originalOperandLoc;
@@ -3117,9 +3092,8 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
3117
3092
SourceLoc keyStartLoc = P.Tok .getLoc ();
3118
3093
auto configAndFn = parseSILDifferentiabilityWitnessConfigAndFunction (
3119
3094
P, *this , InstLoc);
3120
- if (!configAndFn) {
3095
+ if (!configAndFn)
3121
3096
return true ;
3122
- }
3123
3097
auto config = configAndFn->first ;
3124
3098
auto originalFn = configAndFn->second ;
3125
3099
auto *witness = SILMod.lookUpDifferentiabilityWitness (
0 commit comments