Skip to content

Commit da93d3e

Browse files
committed
[SIL] Support SILFunction as a GraphOperationInst attribute. (#17374)
- Implement parsing/printing for SILFunction SymbolicValues. - It's necessary to include function type in the textual representation to enable forward references to SIL functions. - Assert that SILFunction SymbolicValue has no conformances before printing. - Update test.
1 parent 3da2822 commit da93d3e

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/ParseSIL/ParseSIL.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ void SILParser::convertRequirements(SILFunction *F,
895895
/// - A metatype (the instance type is parsed) ($Float).
896896
/// - An aggregate ([i32 1, i64 2, f32 3.0]).
897897
/// - Aggregates values represent constant arrays/structs/tuples.
898+
/// - A SIL function reference (@foo : $(Int) -> Int).
898899
/// Returns true on error.
899900
static bool parseSymbolicValue(SymbolicValue &value, SILParser &SP,
900901
SILBuilder &B) {
@@ -1010,6 +1011,15 @@ static bool parseSymbolicValue(SymbolicValue &value, SILParser &SP,
10101011
value = SymbolicValue::getMetatype(metatype);
10111012
return false;
10121013
}
1014+
// Handle SIL function references.
1015+
if (P.Tok.is(tok::at_sign)) {
1016+
SILFunction *func;
1017+
SILLocation funcLoc = RegularLocation(P.getEndOfPreviousLoc());
1018+
if (SP.parseSILFunctionRef(funcLoc, func))
1019+
return true;
1020+
value = SymbolicValue::getFunction(func);
1021+
return false;
1022+
}
10131023
// Handle aggregate literals.
10141024
if (P.Tok.is(tok::l_square)) {
10151025
SourceLoc lSquareLoc = P.consumeToken(tok::l_square);

lib/SIL/SILPrinter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,15 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
12171217
*this << SILType::getPrimitiveObjectType(metatype.getInstanceType());
12181218
return;
12191219
}
1220+
case SymbolicValue::Function: {
1221+
assert(!v.getFunctionValue().second &&
1222+
"SILFunction SymbolicValues with protocol conformances cannot be "
1223+
"printed");
1224+
auto function = v.getFunctionValue().first;
1225+
*this << "@" << function->getName();
1226+
*this << " : $" << function->getLoweredFunctionType();
1227+
return;
1228+
}
12201229
case SymbolicValue::Aggregate:
12211230
*this << "[";
12221231
interleave(v.getAggregateValue(), [&](SymbolicValue element) {
@@ -1226,7 +1235,6 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
12261235
});
12271236
*this << "]";
12281237
return;
1229-
case SymbolicValue::Function:
12301238
case SymbolicValue::UninitMemory:
12311239
case SymbolicValue::Unknown:
12321240
llvm_unreachable("Unimplemented SymbolicValue case");

test/TensorFlow/graph_op_inst.sil

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ bb0:
1515
%2 = graph_op "tf.Dummy"() {float1: f64 3.14, float2: f32 -3.14} : $Tensor<Float>
1616
%3 = graph_op "tf.Dummy"() {string1: "hello", string2: "world"} : $Tensor<Float>
1717
%4 = graph_op "tf.Dummy"() {metatype1: $Float, metatype2: $Tensor<Float>} : $Tensor<Float>
18-
%5 = graph_op "tf.Dummy"() {array: [[i8 1, i32 -2], [f32 -1.0, $Float]]} : $Tensor<Float>
18+
%5 = graph_op "tf.Dummy"() {function1: @chained_op_test : $@convention(thin) (Tensor<Float>, Tensor<Float>) -> Tensor<Float>} : $Tensor<Float>
19+
%6 = graph_op "tf.Dummy"() {array: [[i8 1, i32 -2], [f32 -1.0, $Float]]} : $Tensor<Float>
1920
return %0 : $Tensor<Float>
2021
}
2122

@@ -26,7 +27,8 @@ bb0:
2627
// CHECK-NEXT: %2 = graph_op "tf.Dummy"() {float1: f64 0x40091EB851EB851F /* 3.1400000000000001 */, float2: f32 0xC048F5C3 /* -3.1400001 */} : $Tensor<Float>
2728
// CHECK-NEXT: %3 = graph_op "tf.Dummy"() {string1: "hello", string2: "world"} : $Tensor<Float>
2829
// CHECK-NEXT: %4 = graph_op "tf.Dummy"() {metatype1: $Float, metatype2: $Tensor<Float>} : $Tensor<Float>
29-
// CHECK-NEXT: %5 = graph_op "tf.Dummy"() {array: {{\[\[}}i8 1, i32 -2], [f32 0xBF800000 /* -1 */, $Float]]} : $Tensor<Float>
30+
// CHECK-NEXT: %5 = graph_op "tf.Dummy"() {function1: @chained_op_test : $@convention(thin) (Tensor<Float>, Tensor<Float>) -> Tensor<Float>} : $Tensor<Float>
31+
// CHECK-NEXT: %6 = graph_op "tf.Dummy"() {array: {{\[\[}}i8 1, i32 -2], [f32 0xBF800000 /* -1 */, $Float]]} : $Tensor<Float>
3032
// CHECK-NEXT: return %0 : $Tensor<Float>
3133
// CHECK-NEXT: }
3234

0 commit comments

Comments
 (0)