Index: docs/CommandGuide/lit.rst
===================================================================
--- docs/CommandGuide/lit.rst
+++ docs/CommandGuide/lit.rst
@@ -152,6 +152,23 @@
 
  Run the tests in a random order.
 
+.. option:: --num-shards=M
+
+ Divide the set of selected tests into ``M`` equal-sized subsets or
+ "shards", and run only one of them.  Must be used with the
+ ``--run-shard=N`` option, which selects the shard to run. The environment
+ variable ``LIT_NUM_SHARDS`` can also be used in place of this
+ option. These two options provide a coarse mechanism for paritioning large
+ testsuites, for parallel execution on separate machines (say in a large
+ testing farm).
+
+.. option:: --run-shard=N
+
+ Select which shard to run, assuming the ``--num-shards=M`` option was
+ provided. The two options must be used together, and the value of ``N``
+ must be in the range ``0..M-1``. The environment variable
+ ``LIT_RUN_SHARD`` can also be used in place of this option.
+
 ADDITIONAL OPTIONS
 ------------------
 
Index: utils/lit/lit/main.py
===================================================================
--- utils/lit/lit/main.py
+++ utils/lit/lit/main.py
@@ -16,6 +16,7 @@
 import argparse
 import tempfile
 import shutil
+import math
 
 import lit.ProgressBar
 import lit.LitConfig
@@ -259,6 +260,14 @@
                      help=("Only run tests with paths matching the given "
                            "regular expression"),
                      action="store", default=None)
+    selection_group.add_argument("--num-shards", dest="numShards", metavar="M",
+                     help="Split testsuite into N pieces and only run one",
+                     action="store", type=int,
+                     default=os.environ.get("LIT_NUM_SHARDS"))
+    selection_group.add_argument("--run-shard", dest="runShard", metavar="N",
+                     help="Run the Nth shard of the testsuite",
+                     action="store", type=int,
+                     default=os.environ.get("LIT_RUN_SHARD"))
 
     debug_group = parser.add_argument_group("Debug and Experimental Options")
     debug_group.add_argument("--debug",
@@ -399,6 +408,33 @@
     else:
         run.tests.sort(key = lambda t: (not t.isEarlyTest(), t.getFullName()))
 
+    # Then optionally restrict our attention to a shard of the tests.
+    if (opts.numShards is not None) or (opts.runShard is not None):
+        if (opts.numShards is None) or (opts.runShard is None):
+            parser.error("--num-shards and --run-shard must be used together")
+        if opts.numShards <= 0:
+            parser.error("--num-shards must be positive")
+        if (opts.runShard < 0) or (opts.runShard >= opts.numShards):
+            parser.error("--run-shard must be between 0 and --num-shards (exclusive)")
+        nTests = len(run.tests)
+        # Take a ceiling so that any remainder is spread over all the shards,
+        # rather than accumulating it all in the last one.
+        shard_size = int(math.ceil(float(nTests) / opts.numShards))
+        shard_begin = opts.runShard * shard_size
+        if shard_begin >= nTests:
+            litConfig.note('Selecting shard %d/%d = size 0/%d' %
+                           (opts.runShard, opts.numShards, nTests))
+            run.tests = []
+        else:
+            # If there was a remainder, final shard might be a little short.
+            shard_end = min(nTests, shard_begin + shard_size)
+            litConfig.note('Selecting shard %d/%d = size %d/%d = range [%d, %d]' % \
+                           (opts.runShard, opts.numShards,
+                            (shard_end - shard_begin), nTests,
+                            # note: lit reports test numbers starting from 1
+                            shard_begin+1, shard_end))
+            run.tests = run.tests[shard_begin:shard_end]
+
     # Finally limit the number of tests, if desired.
     if opts.maxTests is not None:
         run.tests = run.tests[:opts.maxTests]
Index: utils/lit/tests/selecting.py
===================================================================
--- /dev/null
+++ utils/lit/tests/selecting.py
@@ -0,0 +1,90 @@
+# RUN: %{lit} %{inputs}/discovery | FileCheck --check-prefix=CHECK-BASIC %s
+# CHECK-BASIC: Testing: 5 tests
+
+
+# Check that regex-filtering works
+#
+# RUN: %{lit} --filter 'o[a-z]e' %{inputs}/discovery | FileCheck --check-prefix=CHECK-FILTER %s
+# CHECK-FILTER: Testing: 2 of 5 tests
+
+
+# Check that maximum counts work
+#
+# RUN: %{lit} --max-tests 3 %{inputs}/discovery | FileCheck --check-prefix=CHECK-MAX %s
+# CHECK-MAX: Testing: 3 of 5 tests
+
+
+# Check that sharding partitions the testsuite in a way that distributes the
+# rounding error nicely (i.e. 5/3 => 2 2 1, not 1 1 3 or whatever)
+#
+# RUN: %{lit} --num-shards 3 --run-shard 0 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD0-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD0-OUT < %t.out %s
+# CHECK-SHARD0-ERR: note: Selecting shard 0/3 = size 2/5 = range [1, 2]
+# CHECK-SHARD0-OUT: Testing: 2 of 5 tests
+#
+# RUN: %{lit} --num-shards 3 --run-shard 1 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD1-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD1-OUT < %t.out %s
+# CHECK-SHARD1-ERR: note: Selecting shard 1/3 = size 2/5 = range [3, 4]
+# CHECK-SHARD1-OUT: Testing: 2 of 5 tests
+#
+# RUN: %{lit} --num-shards 3 --run-shard 2 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD2-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD2-OUT < %t.out %s
+# CHECK-SHARD2-ERR: note: Selecting shard 2/3 = size 1/5 = range [5]
+# CHECK-SHARD2-OUT: Testing: 1 of 5 tests
+
+
+# Check that sharding via env vars works.
+#
+# RUN: env LIT_NUM_SHARDS=3 LIT_RUN_SHARD=0 %{lit} %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD0-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD0-OUT < %t.out %s
+# CHECK-SHARD0-ERR: note: Selecting shard 0/3 = size 2/5 = range [1, 2]
+# CHECK-SHARD0-OUT: Testing: 2 of 5 tests
+#
+# RUN: env LIT_NUM_SHARDS=3 LIT_RUN_SHARD=1 %{lit} %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD1-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD1-OUT < %t.out %s
+# CHECK-SHARD1-ERR: note: Selecting shard 1/3 = size 2/5 = range [3, 4]
+# CHECK-SHARD1-OUT: Testing: 2 of 5 tests
+#
+# RUN: env LIT_NUM_SHARDS=3 LIT_RUN_SHARD=2 %{lit} %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD2-ERR < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD2-OUT < %t.out %s
+# CHECK-SHARD2-ERR: note: Selecting shard 2/3 = size 1/5 = range [5]
+# CHECK-SHARD2-OUT: Testing: 1 of 5 tests
+
+
+# Check that providing more shards than tests results in 1 test per shard
+# until we run out, then 0.
+#
+# RUN: %{lit} --num-shards 100 --run-shard 2 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD-BIG-ERR1 < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD-BIG-OUT1 < %t.out %s
+# CHECK-SHARD-BIG-ERR1: note: Selecting shard 2/100 = size 1/5 = range [3, 3]
+# CHECK-SHARD-BIG-OUT1: Testing: 1 of 5 tests
+#
+# RUN: %{lit} --num-shards 100 --run-shard 5 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD-BIG-ERR2 < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD-BIG-OUT2 < %t.out %s
+# CHECK-SHARD-BIG-ERR2: note: Selecting shard 5/100 = size 0
+# CHECK-SHARD-BIG-OUT2: Testing: 0 of 5 tests
+#
+# RUN: %{lit} --num-shards 100 --run-shard 50 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD-BIG-ERR3 < %t.err %s
+# FileCheck --check-prefix=CHECK-SHARD-BIG-OUT3 < %t.out %s
+# CHECK-SHARD-BIG-ERR3: note: Selecting shard 50/100 = size 0
+# CHECK-SHARD-BIG-OUT3: Testing: 0 of 5 tests
+
+
+# Check that range constraints are enforced
+#
+# RUN: not %{lit} --num-shards 0 --run-shard 2 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD-ERR < %t.err %s
+# CHECK-SHARD-ERR: error: --num-shards must be positive
+#
+# RUN: not %{lit} --num-shards 3 --run-shard 3 %{inputs}/discovery >%t.out 2>%t.err
+# FileCheck --check-prefix=CHECK-SHARD-ERR2 < %t.err %s
+# CHECK-SHARD-ERR2: error: --run-shard must be between 0 and --num-shards (exclusive)