|
| 1 | +//===--- SILAutoDiffIndices.cpp - Tests SILAutoDiffIndices ----------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// SWIFT_ENABLE_TENSORFLOW |
| 13 | + |
| 14 | +#include "TestContext.h" |
| 15 | +#include "swift/AST/AutoDiff.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/Type.h" |
| 18 | +#include "swift/AST/TensorFlow.h" |
| 19 | +#include "gtest/gtest.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | +using namespace swift::unittest; |
| 23 | + |
| 24 | +class TypeContainsTensorFlowValueTest : public ::testing::Test { |
| 25 | +protected: |
| 26 | + TestContext testContext; |
| 27 | + ASTContext& Ctx; |
| 28 | + Type floatType; |
| 29 | + BoundGenericClassType *tensorHandleType; |
| 30 | + tf::TypeContainsTensorFlowValue tctf; |
| 31 | + |
| 32 | + TypeContainsTensorFlowValueTest() |
| 33 | + : Ctx(testContext.Ctx), floatType(createFloatType()), |
| 34 | + tensorHandleType(createTensorHandleType(floatType)) {} |
| 35 | + |
| 36 | + Type createStructWithField(const char *name, Type fieldType) { |
| 37 | + auto *structDecl = testContext.makeNominal<StructDecl>(name); |
| 38 | + structDecl->computeType(); |
| 39 | + auto *structType = StructType::get(structDecl, Type(), Ctx); |
| 40 | + auto *varDecl = new (Ctx) VarDecl( |
| 41 | + /*IsStatic*/ false, VarDecl::Specifier::Var, |
| 42 | + /*IsCaptureList*/ false, SourceLoc(), Ctx.getIdentifier("field"), |
| 43 | + structDecl); |
| 44 | + varDecl->setInterfaceType(fieldType); |
| 45 | + structDecl->addMember(varDecl); |
| 46 | + return structType; |
| 47 | + } |
| 48 | + |
| 49 | + bool containsTensorFlowValue(Type t) { |
| 50 | + // TODO: Add tests with CheckHigherOrderFunctions set to true. |
| 51 | + return tctf.containsTensorFlowValue(t, /*CheckHigherOrderFunctions*/false); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +private: |
| 56 | + Type createFloatType() { |
| 57 | + // Float type. |
| 58 | + auto *floatDecl = testContext.makeNominal<StructDecl>("Float"); |
| 59 | + return StructType::get(floatDecl, Type(), Ctx); |
| 60 | + } |
| 61 | + |
| 62 | + BoundGenericClassType *createTensorHandleType(Type genericType) { |
| 63 | + auto *tensorDecl = testContext.makeNominal<ClassDecl>("TensorHandle"); |
| 64 | + |
| 65 | + // Generic parameter list. |
| 66 | + auto *floatGenericParamDecl = new (Ctx) |
| 67 | + GenericTypeParamDecl(tensorDecl->getDeclContext(), |
| 68 | + Ctx.getIdentifier("Scalar"), SourceLoc(), 0, 0); |
| 69 | + auto *paramList = GenericParamList::create( |
| 70 | + Ctx, SourceLoc(), {floatGenericParamDecl}, SourceLoc()); |
| 71 | + tensorDecl->setGenericParams(paramList); |
| 72 | + |
| 73 | + return BoundGenericClassType::get(tensorDecl, Type(), {genericType}); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +TEST_F(TypeContainsTensorFlowValueTest, ClassifiesCorrectly) { |
| 78 | + EXPECT_TRUE(containsTensorFlowValue(tensorHandleType)); |
| 79 | + EXPECT_TRUE(containsTensorFlowValue( |
| 80 | + createStructWithField("StructWithTensor", tensorHandleType))); |
| 81 | + EXPECT_FALSE(containsTensorFlowValue( |
| 82 | + createStructWithField("StructWithNoTensor", floatType))); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(TypeContainsTensorFlowValueTest, WorksForRecursiveTypes) { |
| 86 | + // Creates a recursive type for testing purposes. Note that this is not a |
| 87 | + // valid swift type, but should suffice for the purposes of this unit test. |
| 88 | + // |
| 89 | + auto *recursiveDecl = testContext.makeNominal<StructDecl>("RecursiveStruct"); |
| 90 | + recursiveDecl->computeType(); |
| 91 | + auto *recursiveType = StructType::get(recursiveDecl, Type(), Ctx); |
| 92 | + // Add a field of the recursiveType. |
| 93 | + // (This is not possible in swift, but ok for tests.) |
| 94 | + auto *varDecl = new (Ctx) VarDecl( |
| 95 | + /*IsStatic*/ false, VarDecl::Specifier::Var, |
| 96 | + /*IsCaptureList*/ false, SourceLoc(), Ctx.getIdentifier("someField"), |
| 97 | + recursiveDecl); |
| 98 | + varDecl->setInterfaceType(recursiveType); |
| 99 | + recursiveDecl->addMember(varDecl); |
| 100 | + EXPECT_FALSE(containsTensorFlowValue(recursiveType)); |
| 101 | +} |
0 commit comments