Skip to content

[Test] Moved SIL function tests next to tested code. #67069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jul 5, 2023

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Jun 30, 2023

Refactored the infrastructure for testing individual SIL utilites/arbitrary compiler code in the context of a SIL function.

Instead of creating a new class, now it's only necessary to instantiate a global FunctionTest. At instantiation, the name of the test and a lambda to run are provided. When the test running pass encounters a test_specification naming the test, the lambda is invoked.

The infrastructure is now (mostly) in the SIL library so that it can be used from any library that can import its headers. There is a bit of indirection in the FunctionTest/UnitTestRunner types in order to make this work without layering violations, but it is mostly transparent to the tests whose implementations changed very little.

The most interesting commit is [Test] Refactored SIL "unit" tests..

@nate-chandler nate-chandler force-pushed the test/20230629/2 branch 2 times, most recently from b0c95db to 9bb7889 Compare June 30, 2023 23:30
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please test macos platform

//
//===----------------------------------------------------------------------===//
//
// This file defines test::FunctionTest.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define FunctionTest. Is it just any test that takes a SILFunction as input and produces any arbitrary output? I suppose this is a specific kind of "functional" test but that's not what the name refers to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a definition for this type just above it among the other header changes.

@@ -0,0 +1,102 @@
//===-- UnitTest.h - Testing based on test_specification insts -*- C++ --*-===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason this is not called FunctionTest.h?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we want to add a ModuleTest in here someday.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped "Unit" here and elsewhere.

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

My main suggestion is to separate the C++ trickery for bypassing the layering violation that exists with pure SIL tests from the essential interface that test writers need to see. It is easy to confuse all of the abstraction in UnitTest.h for extension points that need to be implemented, when they are all just implementation details that only exist to our particular cmake configuration happy.

In UnitTest.h please add comments that explain the specific motivation for each weird abstraction or template argument. Like specifically mentioning that the template argument is designed to issue a compile-time error if the API is called outside of the SILOptimizer library.

I think the FunctionTest/Arguments interfaces are basically good as long as they can be implemented in Swift compiler source without any of the complexity that we need here for the C++ build.

@nate-chandler nate-chandler force-pushed the test/20230629/2 branch 2 times, most recently from b230004 to 794cfdb Compare July 4, 2023 01:13
@nate-chandler
Copy link
Contributor Author

Reorganized the header so that the parts relevant to people writing tests appear at the top with docs and are separated by ASCII art from the various implementation details.

Doc'd the two tricks required for layering.

I think the SwiftSources side will work pretty straightforwardly. With an enum for Argument. And a Dependencies protocol which the test-running pass will instantiate and pass to FunctionTest.run. It would then be extended in the SILOptimizer module with getAnalysis, getFunctionPass, etc.

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

In preparation for moving unit test infrastructure there so that it can
be used anywhere the SIL library is used.
Renamed UnitTest to FunctionTest.

FunctionTests are now instantiated once as global objects--with their
names and the code they are to run--at which time they are stored by
name in a global registry.

Moved the types to the SIL library.

Together, these changes enable defining unit tests in the source file
containing the code to be tested.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
-with-list-of-last-users-insertion-points.  Moved the test next to the
code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
-switch-enum.  Moved the test next to the code it calls.
-enum-block.  Moved the test next to the code it calls.
-enum-on-objc-class-optional.  Moved the test next to the code it calls.
-enum-unreachable-blocks.  Moved the test next to the code it calls.
-term-with-identical-dest-blocks.  Moved the test next to the code it
calls.
Moved the test next to the code it calls.
Moved the test next to the code it calls.
-liverange.  Moved the test next to the code it calls.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler merged commit d2c8ede into swiftlang:main Jul 5, 2023
@nate-chandler nate-chandler deleted the test/20230629/2 branch July 5, 2023 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants