Skip to content

Commit f9b0f08

Browse files
committed
---
yaml --- r: 311289 b: refs/heads/tensorflow-merge c: d8ce852 h: refs/heads/master i: 311287: fd23282
1 parent 693f54d commit f9b0f08

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ refs/heads/chase-my-tail: 8bb91443a9e81bbfac92a2621a0af887a1da8dbf
13791379
refs/heads/consider-outer-alternatives: 708bac749ec60a22a79e2eefbe734f9488a7370d
13801380
refs/heads/revert-25740-oops-i-linked-it-again: fdd41aeb682fc488572bdc1cf71b2ff6997ba576
13811381
refs/heads/swift-5.1-branch-06-12-2019: e63b7b2d3b93c48232d386099d0ec525d21d8f8d
1382-
refs/heads/tensorflow-merge: 6564b11d6f062d2f43d3419a8387fcfa7bc74787
1382+
refs/heads/tensorflow-merge: d8ce8529027a9d6a506462204a286d718916dda2
13831383
refs/heads/update-checkout-sha-info: 5832743c5c2a842976c42a508a4c6dcceefb0aef
13841384
refs/tags/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-12-a: 228f0448d9bb909aacbba4afcb7c600a405d15da
13851385
refs/tags/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-14-a: 922861a77b5fc2bf46bc917da70ceb15eef76836

branches/tensorflow-merge/lib/SILOptimizer/Mandatory/TFLowerGraph.cpp

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ struct TFGraphLowering : public SILInstructionVisitor<TFGraphLowering> {
217217
llvm::SmallSet<int, 4> processedTensorIdsForSend;
218218
llvm::SmallSet<int, 4> processedTensorIdsForReceive;
219219

220+
/// Mapping from declarations to the number to times a TF_Function took the
221+
/// name from the declaration. This will be used in `getUniqueName` to produce
222+
/// uniqued graph node names.
223+
llvm::SmallDenseMap<ValueDecl *, unsigned> uniqueNames;
224+
220225
/// This flag gets set if lowering code to the graph produces a TensorFlow
221226
/// error and emits a diagnostic. This tells us to stop lowering and give up
222227
/// gracefully.
@@ -558,14 +563,29 @@ static void escapeOpName(std::string &name) {
558563
// Currently, invalid characters are simply replaced with underscores.
559564
// TODO: Use a more robust escaping transformation. It should handle unicode
560565
// characters (using llvm::UTF8 or some other means) and be reversible.
561-
for (unsigned i = 0, n = name.size(); i < n; i++) {
562-
char c = name[i];
563-
if (!std::isalnum(c) && c != '.')
564-
if (i == 0 || (i != '_' && i != '/'))
565-
name[i] = '_';
566+
for (auto i : indices(name)) {
567+
char &c = name[i];
568+
if (!llvm::isAlnum(c) && c != '.')
569+
if (i == 0 || (c != '_' && c != '/'))
570+
c = '_';
566571
}
567572
}
568573

574+
/// Given a DeclName, returns an escaped (TF-compatible)
575+
/// name that replaces parentheses with '.' and colons with '_', for example:
576+
/// `foo(x:y:z:)` -> `foo.x_y_z_.`.
577+
static std::string escapeDeclName(DeclName name) {
578+
SmallVector<char, 8> buffer;
579+
auto newName = name.getString(buffer, /*skipEmptyArgumentNames*/ true);
580+
for (char &c : buffer) {
581+
if (c == '(' || c == ')')
582+
c = '.';
583+
else if (!llvm::isAlnum(c))
584+
c = '_';
585+
}
586+
return newName.str();
587+
}
588+
569589
/// Produce a "stack trace" for the specified location, producing it in a form
570590
/// that we can use as a unique op name.
571591
std::string TFGraphLowering::getUniqueName(SILDebugLocation loc,
@@ -580,6 +600,9 @@ std::string TFGraphLowering::getUniqueName(SILDebugLocation loc,
580600
// Form a name for this op based on the user's source location and "stack
581601
// trace" of where it got inlined in user code. We use the form
582602
// "file:line:col".
603+
//
604+
// FIXME: InlinedCallSite is always nullptr even if we use the performance
605+
// inliner, so it currently does not track the inlined call site.
583606
for (auto ds = loc.getScope(); ds; ds = ds->InlinedCallSite) {
584607
// If the call site location is invalid, stop scanning.
585608
if (!ds->Loc.getSourceLoc().isValid())
@@ -596,7 +619,27 @@ std::string TFGraphLowering::getUniqueName(SILDebugLocation loc,
596619
if (fnName.endswith(".device_partition"))
597620
fnName = fnName.drop_back(strlen(".device_partition"));
598621

599-
name += "." + fnName.str() + "." + llvm::utostr(lineCol.first);
622+
// Separate functions using '/' so that TensorBoard can treat it as a
623+
// hierarchical separator.
624+
name += '/';
625+
626+
// If the SIL function is backed by a Swift decl, use the decl name.
627+
// Otherwise, use the SIL name.
628+
std::string funcName;
629+
auto *dc = SILFn.getDeclContext();
630+
if (auto *afd = dyn_cast_or_null<AbstractFunctionDecl>(dc)) {
631+
funcName = escapeDeclName(afd->getEffectiveFullName());
632+
// Make sure the name is unique.
633+
auto declCountLookup = uniqueNames.find(afd);
634+
if (declCountLookup != uniqueNames.end())
635+
funcName += "_" + llvm::itostr(declCountLookup->getSecond()++);
636+
else
637+
uniqueNames.insert({afd, 1});
638+
} else {
639+
funcName = fnName.str();
640+
}
641+
642+
name += funcName + "." + llvm::utostr(lineCol.first);
600643
name += "." + llvm::utostr(lineCol.second);
601644
}
602645
}
@@ -608,7 +651,7 @@ std::string TFGraphLowering::getUniqueName(SILDebugLocation loc,
608651
if (sourceLoc.isValid()) {
609652
auto lineCol = SM.getLineAndColumn(sourceLoc);
610653
auto bufferID = SM.getBufferIdentifierForLoc(sourceLoc);
611-
name += "." + bufferID.str() + "." + llvm::utostr(lineCol.first);
654+
name += "/" + bufferID.str() + "." + llvm::utostr(lineCol.first);
612655
name += "." + llvm::utostr(lineCol.second);
613656
}
614657
}
@@ -1099,7 +1142,6 @@ void TFGraphLowering::visitTFDataset(BuiltinInst *inst) {
10991142
StringRef filePath;
11001143
if (dataSource != DatasetCreationContext::FAKE) {
11011144
auto operand = inst->getOperand(1);
1102-
auto opInfo = tfopInfo.operandClasses[1];
11031145
auto *sli = cast<StringLiteralInst>(operand);
11041146
assert(sli->getEncoding() == StringLiteralInst::Encoding::UTF8);
11051147
filePath = sli->getValue();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %target-swift-frontend -Xllvm -tf-dump-graph -O -emit-sil %s -verify | %FileCheck %s
2+
3+
import TensorFlow
4+
5+
6+
func foo() -> Tensor<Float> {
7+
return Tensor(1)
8+
}
9+
10+
func bar() -> Tensor<Float> {
11+
return sin(foo())
12+
}
13+
14+
func baz() -> Tensor<Float> {
15+
return bar() + 1
16+
}
17+
18+
func main() {
19+
_ = baz()
20+
}
21+
22+
main()
23+
24+
/// CHECK: op: "Const"
25+
/// CHECK: op: "Sin"
26+
/// CHECK: op: "Add"
27+
28+
/// FIXME: Currently the performance inliner is not capturing inlining trace for
29+
/// some reason. When that's fixed, add checks in this file.
30+
///
31+
/// Expected node names:
32+
/// - Sin: op/main/baz/bar/sin
33+
/// - Const: op/main/baz/bar/foo

branches/tensorflow-merge/test/TensorFlow/sends_recvs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public func test1SendWithParam(x: Float) {
5454
// CHECK: node_def {
5555
// CHECK: name: "RunControlDependency"
5656
// CHECK: op: "Identity"
57-
// CHECK-NEXT: input: "op.
57+
// CHECK-NEXT: input: "op/test1SendWithParam.x_
5858
// CHECK-NEXT: input: "^tf_send_0"
5959

6060
public func test2Sends() {

0 commit comments

Comments
 (0)