Skip to content

Commit cab9142

Browse files
authored
Merge pull request #1 from dan-zheng/tensorflow-merge
Fix 11/1 post-merge compiler crashes.
2 parents ed83da5 + c7f0821 commit cab9142

File tree

9 files changed

+87
-44
lines changed

9 files changed

+87
-44
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ endfunction()
593593
# [LINK_FLAGS flag1...]
594594
# [API_NOTES_NON_OVERLAY]
595595
# [FILE_DEPENDS target1 ...]
596+
# SWIFT_ENABLE_TENSORFLOW
597+
# [EXTRA_RPATHS rpath1 ...]
598+
# END SWIFT_ENABLE_TENSORFLOW
596599
# [DONT_EMBED_BITCODE]
597600
# [IS_STDLIB]
598601
# [FORCE_BUILD_OPTIMIZED]
@@ -652,6 +655,11 @@ endfunction()
652655
# FILE_DEPENDS
653656
# Additional files this library depends on.
654657
#
658+
# SWIFT_ENABLE_TENSORFLOW
659+
# EXTRA_RPATHS
660+
# List of directories to add to this library's RPATH.
661+
# END SWIFT_ENABLE_TENSORFLOW
662+
#
655663
# DONT_EMBED_BITCODE
656664
# Don't embed LLVM bitcode in this target, even if it is enabled globally.
657665
#
@@ -695,6 +703,9 @@ function(_add_swift_library_single target name)
695703
C_COMPILE_FLAGS
696704
DEPENDS
697705
FILE_DEPENDS
706+
# SWIFT_ENABLE_TENSORFLOW
707+
EXTRA_RPATHS
708+
# END SWIFT_ENABLE_TENSORFLOW
698709
FRAMEWORK_DEPENDS
699710
FRAMEWORK_DEPENDS_WEAK
700711
INCORPORATE_OBJECT_LIBRARIES
@@ -1031,6 +1042,28 @@ function(_add_swift_library_single target name)
10311042
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/cygwin")
10321043
endif()
10331044

1045+
# SWIFT_ENABLE_TENSORFLOW
1046+
# Hande extra RPATHs.
1047+
set(local_rpath "")
1048+
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "LINUX" AND NOT "${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID")
1049+
set(local_rpath "$ORIGIN:/usr/lib/swift/linux")
1050+
elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "CYGWIN")
1051+
set(local_rpath "$ORIGIN:/usr/lib/swift/cygwin")
1052+
endif()
1053+
foreach(rpath_element ${SWIFTLIB_SINGLE_EXTRA_RPATHS})
1054+
if("${local_rpath}" STREQUAL "")
1055+
set(local_rpath "${rpath_element}")
1056+
else()
1057+
set(local_rpath "${local_rpath}:${rpath_element}")
1058+
endif()
1059+
endforeach()
1060+
if(NOT "${local_rpath}" STREQUAL "")
1061+
set_target_properties("${target}"
1062+
PROPERTIES
1063+
INSTALL_RPATH "${local_rpath}")
1064+
endif()
1065+
# END SWIFT_ENABLE_TENSORFLOW
1066+
10341067
set_target_properties("${target}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES)
10351068
set_target_properties("${target}" PROPERTIES FOLDER "Swift libraries")
10361069

@@ -2131,6 +2164,20 @@ function(_add_swift_executable_single name)
21312164
"-Xlinker" "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
21322165
endif()
21332166

2167+
# SWIFT_ENABLE_TENSORFLOW
2168+
set(swift_relative_library_path "../lib/swift/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
2169+
is_darwin_based_sdk("${SWIFTEXE_SINGLE_SDK}" IS_DARWIN)
2170+
# NOTE: Adding "${SWIFTLIB_DIR}/linux" to the rpath is a hack solely for
2171+
# working around tests like Driver/linker.swift which copy/hard link Swift
2172+
# executables to different directories without also copying the "libs"
2173+
# directory. A more robust solution should be found.
2174+
if("${SWIFTEXE_SINGLE_SDK}" STREQUAL "LINUX" AND NOT "${SWIFTEXE_SINGLE_SDK}" STREQUAL "ANDROID")
2175+
set(local_rpath "$ORIGIN:$ORIGIN/${swift_relative_library_path}:${SWIFTLIB_DIR}/linux:/usr/lib/swift/linux")
2176+
elseif("${SWIFTEXE_SINGLE_SDK}" STREQUAL "CYGWIN")
2177+
set(local_rpath "$ORIGIN:$ORIGIN/${swift_relative_library_path}:${SWIFTLIB_DIR}/cygwin:/usr/lib/swift/cygwin")
2178+
endif()
2179+
# END SWIFT_ENABLE_TENSORFLOW
2180+
21342181
# Find the names of dependency library targets.
21352182
#
21362183
# We don't add the ${ARCH} to the target suffix because we want to link
@@ -2192,6 +2239,13 @@ function(_add_swift_executable_single name)
21922239

21932240
set_target_properties(${name}
21942241
PROPERTIES FOLDER "Swift executables")
2242+
2243+
# SWIFT_ENABLE_TENSORFLOW
2244+
if(NOT "${local_rpath}" STREQUAL "")
2245+
set_target_properties("${name}" PROPERTIES INSTALL_RPATH "${local_rpath}")
2246+
endif()
2247+
set_target_properties("${name}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES)
2248+
# END SWIFT_ENABLE_TENSORFLOW
21952249
endfunction()
21962250

21972251
# Add an executable for each target variant. Executables are given suffixes

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,6 @@ class IRGenModule {
559559
llvm::PointerType *RelativeAddressPtrTy;
560560
};
561561
llvm::IntegerType *Int64Ty; /// i64
562-
// SWIFT_ENABLE_TENSORFLOW
563-
llvm::Type *DoubleTy; /// double
564-
llvm::Type *FloatTy; /// float
565562

566563
union {
567564
llvm::IntegerType *SizeTy; /// usually i32 or i64

lib/SILOptimizer/Mandatory/TFCanonicalizeCFG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ class BasicBlockCloner : public SILClonerWithScopes<BasicBlockCloner> {
405405
return bb;
406406
}
407407

408-
SILValue remapValue(SILValue Value) {
408+
SILValue getMappedValue(SILValue Value) {
409409
auto VI = ValueMap.find(Value);
410410
if (VI != ValueMap.end())
411411
return VI->second;
@@ -1550,7 +1550,7 @@ void SingleExitLoopTransformer::unrollLoopBodyOnce() {
15501550
// A suitable value is found. Update the edge value in the unrolled
15511551
// loop with the corresponding cloned value.
15521552
changeEdgeValue(clonedPred->getTerminator(), clonedNewLatch, argIndex,
1553-
cloner.remapValue(value));
1553+
cloner.getMappedValue(value));
15541554
patched = true;
15551555
break;
15561556
}

lib/SILOptimizer/Mandatory/TFCanonicalizeCFG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_SILOPTIMIZER_TFCANONICALIZECFG_H
2121

2222
#include "swift/SIL/SILBasicBlock.h"
23+
#include "swift/SIL/BasicBlockUtils.h"
2324
#include "swift/SIL/LoopInfo.h"
2425

2526
namespace swift {

lib/SILOptimizer/Mandatory/TFDevicePartition.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class DevicePartitionCloner
190190
auto *newOperand = cloneSingleInst(cast<SingleValueInstruction>(operand));
191191
ValueMap[operand] = newOperand;
192192
}
193-
return this->remapValue(operand);
193+
return this->getMappedValue(operand);
194194
}
195195
};
196196
} // end anonymous namespace
@@ -220,7 +220,7 @@ void DevicePartitionCloner::visitGraphOperationInst(GraphOperationInst *inst) {
220220
auto opValue = inst->getOperand(i);
221221
assert(isTensorFlowValue(opValue->getType()) &&
222222
"ops should only use tensors");
223-
args.push_back(remapValue(opValue));
223+
args.push_back(getMappedValue(opValue));
224224
}
225225

226226
SmallVector<SILType, 2> resultTypes;
@@ -259,7 +259,7 @@ void DevicePartitionCloner::addD2DSend(GraphOperationInfo &graphOpInfo,
259259
{ctx.getIdentifier(TF_DEVICE_ATTR),
260260
SymbolicValue::getString(getDeviceString(thisDeviceType), allocator)});
261261

262-
auto valueToSend = remapValue(inst->getOperand(0));
262+
auto valueToSend = getMappedValue(inst->getOperand(0));
263263
newInstBuilder.addArgument(valueToSend);
264264
if (inst->getNumAttributes() > tensorShapeAttrIdx)
265265
newInstBuilder.addAttribute(inst->getAttribute(tensorShapeAttrIdx));
@@ -342,7 +342,7 @@ void DevicePartitionCloner::visitTensorTransferInst(
342342
for (auto destDeviceForSend : deviceInfo.getUsedDeviceTypes()) {
343343
if (destDeviceForSend == srcDevice) {
344344
// When dest is src, update the mapping, and do not send.
345-
ValueMap[getSingleValueResult(inst)] = remapValue(inst->getOperand(0));
345+
ValueMap[getSingleValueResult(inst)] = getMappedValue(inst->getOperand(0));
346346
continue;
347347
}
348348
addD2DSend(graphOpInfo, tensorShapeAttrIdx, transferId, destDeviceForSend);

lib/SILOptimizer/Mandatory/TFDifferentiation.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define DEBUG_TYPE "differentiation"
2323

2424
#include "swift/AST/ASTMangler.h"
25+
#include "swift/AST/ASTPrinter.h"
2526
#include "swift/AST/AutoDiff.h"
2627
#include "swift/AST/Builtins.h"
2728
#include "swift/AST/DeclContext.h"
@@ -767,11 +768,14 @@ void DifferentiationInvoker::print(llvm::raw_ostream &os) const {
767768
<< ") task=" << indDiff.second << ')';
768769
break;
769770
}
770-
case Kind::DifferentialOperator:
771+
case Kind::DifferentialOperator: {
772+
StreamPrinter printer(os);
773+
PrintOptions options;
771774
os << "differential_operator=(";
772-
getDifferentialOperator()->print(os);
775+
getDifferentialOperator()->print(printer, options);
773776
os << ')';
774777
break;
778+
}
775779
case Kind::DifferentiableAttribute: {
776780
auto diffAttr = getDifferentiableAttribute();
777781
os << "differentiable_attribute=(attr=(";
@@ -3379,7 +3383,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
33793383
}
33803384

33813385
/// Remap a value in the original function.
3382-
SILValue remapValue(SILValue value) {
3386+
SILValue getMappedValue(SILValue value) {
33833387
// If `value` is a checkpointed primal value, extract it from the primal
33843388
// value aggregate.
33853389
if (auto extractedPV = extractPrimalValueIfAny(value))
@@ -3388,7 +3392,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
33883392
// rematerialize it in the adjoint function.
33893393
if (auto *inst = value->getDefiningInstruction())
33903394
rematerializeOriginalInstruction(inst);
3391-
return rematCloner.remapValue(value);
3395+
return rematCloner.getMappedValue(value);
33923396
}
33933397

33943398
/// Handle `apply` instruction. If it's active (on the differentiation path),
@@ -3434,7 +3438,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
34343438
auto originalParamsWithoutSelf = ai->hasSelfArgument() ?
34353439
originalParams.slice(0, originalParams.size() - 1) : originalParams;
34363440
for (auto param : originalParamsWithoutSelf)
3437-
args.push_back(remapValue(param));
3441+
args.push_back(getMappedValue(param));
34383442

34393443
// Add nested primal values.
34403444
if (auto nestedPrimValAggr = extractPrimalValueIfAny(ai, /*nested*/ true)) {
@@ -3443,7 +3447,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
34433447
args.append(nestedPrimVals.begin(), nestedPrimVals.end());
34443448
}
34453449
// Add original results.
3446-
auto origResultAggr = remapValue(ai);
3450+
auto origResultAggr = getMappedValue(ai);
34473451
SmallVector<SILValue, 8> origResults;
34483452
extractAllElements(origResultAggr, builder, origResults);
34493453
args.append(origResults.begin(), origResults.end());
@@ -3466,7 +3470,7 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
34663470

34673471
// Push the mapped self parameter, if any.
34683472
if (ai->hasSelfArgument())
3469-
args.push_back(remapValue(ai->getSelfArgument()));
3473+
args.push_back(getMappedValue(ai->getSelfArgument()));
34703474

34713475
// Call the adjoint function.
34723476
auto *adjointRef = getBuilder().createFunctionRef(ai->getLoc(), adjoint);
@@ -3677,18 +3681,18 @@ class AdjointEmitter final : public SILInstructionVisitor<AdjointEmitter> {
36773681
auto adjVal = materializeAdjointDirect(adj, opLoc);
36783682
auto *adjLHS = builder.createBuiltinBinaryFunction(
36793683
opLoc, "fmul", opType, opType,
3680-
{adjVal, remapValue(bi->getOperand(1))});
3684+
{adjVal, getMappedValue(bi->getOperand(1))});
36813685
addAdjointValue(bi->getOperand(0), adjLHS);
36823686
auto *adjRHS = builder.createBuiltinBinaryFunction(
36833687
opLoc, "fmul", opType, opType,
3684-
{adjVal, remapValue(bi->getOperand(0))});
3688+
{adjVal, getMappedValue(bi->getOperand(0))});
36853689
addAdjointValue(bi->getOperand(1), adjRHS);
36863690
break;
36873691
}
36883692
case BuiltinValueKind::FDiv: {
36893693
auto adjVal = materializeAdjointDirect(adj, opLoc);
3690-
auto lhs = remapValue(bi->getOperand(0));
3691-
auto rhs = remapValue(bi->getOperand(1));
3694+
auto lhs = getMappedValue(bi->getOperand(0));
3695+
auto rhs = getMappedValue(bi->getOperand(1));
36923696
// x' = seed / y
36933697
auto adjLHS = builder.createBuiltinBinaryFunction(opLoc, "fdiv", opType,
36943698
opType, {adjVal, rhs});
@@ -3731,7 +3735,7 @@ void AdjointEmitter::rematerializeOriginalInstruction(SILInstruction *inst) {
37313735
});
37323736
// Ensure that all operands have a corresponding value in the adjoint.
37333737
for (auto &op : inst->getAllOperands())
3734-
remapValue(op.get());
3738+
getMappedValue(op.get());
37353739
rematCloner.setInsertionPointBeforeAnyTerminator(ncd);
37363740
rematCloner.visit(inst);
37373741
}

lib/SILOptimizer/Mandatory/TFPartition.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ void PartitionCloner::visitBranchInst(BranchInst *inst) {
23542354
unsigned opNum = 0;
23552355
for (auto &arg : inst->getAllOperands()) {
23562356
if (shouldCloneArgument(destBB->getArgument(opNum++)))
2357-
operands.push_back(remapValue(arg.get()));
2357+
operands.push_back(getMappedValue(arg.get()));
23582358
}
23592359

23602360
getBuilder().setCurrentDebugScope(getOpScope(inst->getDebugScope()));
@@ -2409,7 +2409,7 @@ void PartitionCloner::visitGraphOperationInst(GraphOperationInst *inst) {
24092409
//
24102410
// For this special op, we ignore any device placement. Logically, this
24112411
// means this op is always placed on ALL devices.
2412-
auto result = remapValue(inst->getOperand(0));
2412+
auto result = getMappedValue(inst->getOperand(0));
24132413
auto S2TResult = inst->getResult(0);
24142414
if (!S2TResult->getType().getASTType()->isEqual(
24152415
result->getType().getASTType())) {
@@ -2474,7 +2474,7 @@ void PartitionCloner::visitScalarInst(SingleValueInstruction *inst) {
24742474
// The TensorToScalar operation is promoted by just dropping the operation
24752475
// and using the incoming tensor value.
24762476
if (opKind == PromotedScalarKind::TensorToScalar) {
2477-
ValueMap[inst] = remapValue(inst->getOperand(1));
2477+
ValueMap[inst] = getMappedValue(inst->getOperand(1));
24782478
return;
24792479
}
24802480

@@ -2489,7 +2489,7 @@ void PartitionCloner::visitScalarInst(SingleValueInstruction *inst) {
24892489
operandRange = operandRange.drop_back();
24902490

24912491
for (auto &op : operandRange)
2492-
opBuilder.addArgument(remapValue(op.get()));
2492+
opBuilder.addArgument(getMappedValue(op.get()));
24932493

24942494
// The type of the new builtin is usually the same as the input type, but
24952495
// "remapped", which turns Float into TensorHandle<Float>.
@@ -2568,7 +2568,7 @@ void PartitionCloner::visitTupleExtractInst(TupleExtractInst *inst) {
25682568
if (classifyInst(inst->getOperand()->getDefiningInstruction()) ==
25692569
PartitioningClass::OverflowCheckingInst) {
25702570
assert(inst->getFieldNo() == 0 && "Unexpected extract to remap");
2571-
ValueMap[inst] = remapValue(inst->getOperand());
2571+
ValueMap[inst] = getMappedValue(inst->getOperand());
25722572
return;
25732573
}
25742574

@@ -2582,7 +2582,7 @@ void PartitionCloner::visitTupleExtractInst(TupleExtractInst *inst) {
25822582
/// a struct_extract is when this is safe.
25832583
void PartitionCloner::visitStructExtractInst(StructExtractInst *inst) {
25842584
assert(inst->getFieldNo() == 0 && "Unexpected extract to remap");
2585-
ValueMap[inst] = remapValue(inst->getOperand());
2585+
ValueMap[inst] = getMappedValue(inst->getOperand());
25862586
}
25872587

25882588
/// Wrap a value in a simple struct wrapper, these are common in the standard
@@ -3189,7 +3189,7 @@ bool PartitionCloner::insertAccelToHostTransfer(SILValue value, SILLocation loc)
31893189

31903190
// Create the send in the accelerator code. Each send/receive pair gets
31913191
// a unique ID to associate one with the other.
3192-
createAcceleratorSend(BA, loc, remapValue(value), nextSendID, FP.deviceInfo);
3192+
createAcceleratorSend(BA, loc, getMappedValue(value), nextSendID, FP.deviceInfo);
31933193

31943194
// Create the receive in the host code.
31953195
auto newVal = createHostReceive(BH, loc, value->getType(), tensorComputation,
@@ -3319,11 +3319,11 @@ void PartitionCloner::cloneFunction(ArrayRef<SILValue> resultValues) {
33193319
// Create a return of N values, producing a tuple if necessary.
33203320
SILValue result;
33213321
if (resultValues.size() == 1)
3322-
result = remapValue(resultValues[0]);
3322+
result = getMappedValue(resultValues[0]);
33233323
else {
33243324
SmallVector<SILValue, 4> results;
33253325
for (auto r : resultValues)
3326-
results.push_back(remapValue(r));
3326+
results.push_back(getMappedValue(r));
33273327

33283328
result = Builder.createTuple(FP.hostFn.getLocation(), results);
33293329
}

lib/Sema/TypeCheckExpr.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -720,19 +720,6 @@ TypeChecker::lookupFuncDecl(
720720
funcName))
721721
results = tmp;
722722
}
723-
// Note: static methods are omitted from `TypeChecker.lookupUnqualified` in
724-
// Swift 3. The code below is a workaround for resolving them.
725-
//
726-
// This is necessary because the stdlib is compiled with `-swift-version 3`
727-
// for Swift 3 compatibility, and floating point types use the
728-
// `@differentiable` attribute with static adjoint methods (such as
729-
// `_adjointAdd`).
730-
else if (lookupContext->getASTContext().isSwiftVersion3() &&
731-
results.empty() && lookupContext->isTypeContext()) {
732-
results = lookupMember(lookupContext,
733-
lookupContext->getSelfTypeInContext(),
734-
funcName);
735-
}
736723
}
737724

738725
// Initialize error flags.

stdlib/public/TensorFlow/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ add_swift_target_library(swiftTensorFlow ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_
8787
SWIFT_COMPILE_FLAGS "${swift_stdlib_compile_flags}"
8888
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
8989
INSTALL_IN_COMPONENT stdlib
90-
EXTRA_RPATH "${SWIFT_TENSORFLOW_TARGET_LIB_DIR}")
90+
EXTRA_RPATHS "${SWIFT_TENSORFLOW_TARGET_LIB_DIR}")

0 commit comments

Comments
 (0)