[zorg] Add polly test-suite builder.

Add a builder for compiling and running the test-suite using polly.

Reviewed By: gkistanova

Differential Revision: https://reviews.llvm.org/D78955
diff --git a/buildbot/osuosl/master/config/builders.py b/buildbot/osuosl/master/config/builders.py
index 4d30359..e32b61a 100644
--- a/buildbot/osuosl/master/config/builders.py
+++ b/buildbot/osuosl/master/config/builders.py
@@ -830,7 +830,7 @@
                                 "-DCMAKE_C_COMPILER:FILEPATH=/local/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04/bin/clang",
                                 "-DCMAKE_CXX_COMPILER:FILEPATH=/local/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04/bin/clang++"])},
         {'name': "polly-x86_64-linux",
-         'slavenames': ["polly-x86_64-fdcserver", "polly-x86_64-gce1", "polly-x86_64-gce2"],
+         'slavenames': ["polly-x86_64-fdcserver", "polly-x86_64-gce1"],
          'builddir': "polly-x86_64-linux",
          'factory': PollyBuilder.getPollyBuildFactory(
                 clean=False,
@@ -841,7 +841,23 @@
                                 "-DLLVM_TARGETS_TO_BUILD='X86;NVPTX'",
                                 "-DCLANG_ENABLE_ARCMT=OFF",
                                 "-DCLANG_ENABLE_STATIC_ANALYZER=OFF",
-                                ])}
+                                ])},
+        {'name': "polly-x86_64-linux-test-suite",
+         'slavenames': ["polly-x86_64-fdcserver", "polly-x86_64-gce2"],
+         'builddir':"polly-x86_64-linux-test-suite",
+         'factory': PollyBuilder.getPollyBuildFactory(
+                clean=False,
+                install=False,
+                make='ninja',
+                extraCmakeArgs=["-G", "Ninja",
+                                "-DLLVM_ENABLE_ASSERTIONS=True",
+                                "-DLLVM_TARGETS_TO_BUILD='X86;NVPTX'",
+                                "-DCLANG_ENABLE_ARCMT=OFF",
+                                "-DCLANG_ENABLE_STATIC_ANALYZER=OFF",
+                                ],
+                testsuite=True,
+                extraTestsuiteCmakeArgs=["-G", "Ninja"]
+                )}
        ]
 
 # AOSP builders.
diff --git a/zorg/buildbot/builders/PollyBuilder.py b/zorg/buildbot/builders/PollyBuilder.py
index 76f7e83..f137791 100644
--- a/zorg/buildbot/builders/PollyBuilder.py
+++ b/zorg/buildbot/builders/PollyBuilder.py
@@ -1,8 +1,9 @@
-from buildbot.steps.shell import Configure, ShellCommand
-from buildbot.process.properties import WithProperties
+import os
 
-from zorg.buildbot.builders import LNTBuilder
-from zorg.buildbot.builders import ClangBuilder
+from buildbot.process.properties import WithProperties
+from buildbot.steps.shell import Configure, ShellCommand, WarningCountingShellCommand, SetProperty
+from buildbot.steps.slave import RemoveDirectory
+from zorg.buildbot.commands.LitTestCommand import LitTestCommand
 from zorg.buildbot.process.factory import LLVMBuildFactory
 
 def getPollyBuildFactory(
@@ -13,13 +14,20 @@
     checkAll=False,
     env=None,
     extraCmakeArgs=None,
+    testsuite=False,extraTestsuiteCmakeArgs=None,
     **kwargs):
 
     if extraCmakeArgs is None:
-        extraCmakeArgs=[],
+        extraCmakeArgs=[]
+    if extraTestsuiteCmakeArgs is None:
+        extraTestsuiteCmakeArgs = []
+
     llvm_srcdir = "llvm.src"
     llvm_objdir = "llvm.obj"
     llvm_instdir = "llvm.inst"
+    testsuite_srcdir = "test-suite.src"
+    testsuite_builddir = "test-suite.build"
+
     jobs_cmd = []
     if jobs is not None:
         jobs_cmd = ["-j"+str(jobs)]
@@ -38,6 +46,9 @@
         merged_env.update(env)  # Overwrite pre-set items with the given ones, so user can set anything.
 
     depends_on_projects = ['llvm','clang','polly']
+    if testsuite:
+        # XRay tests in test-suite require compiler-rt
+        depends_on_projects += ['compiler-rt']
 
     cleanBuildRequestedByProperty = lambda step: step.build.getProperty("clean", False)
     cleanBuildRequested = lambda step: clean or step.build.getProperty("clean", default=step.build.getProperty("clean_obj"))
@@ -50,24 +61,18 @@
             cleanBuildRequested=cleanBuildRequested,
             **kwargs) # Pass through all the extra arguments.
 
-    f.addStep(ShellCommand(name='clean-src-dir',
-                           command=['rm', '-rf', f.monorepo_dir],
+    f.addStep(RemoveDirectory(name='clean-src-dir',
+                           dir=f.monorepo_dir,
                            warnOnFailure=True,
-                           description=["clean src dir"],
-                           workdir='.',
-                           env=merged_env,
                            doStepIf=cleanBuildRequestedByProperty))
 
     # Get the source code.
     f.addGetSourcecodeSteps(**kwargs)
 
     # Clean build dir
-    f.addStep(ShellCommand(name='clean-build-dir',
-                           command=['rm', '-rf', llvm_objdir],
+    f.addStep(RemoveDirectory(name='clean-build-dir',
+                           dir=llvm_objdir,
                            warnOnFailure=True,
-                           description=["clean build dir"],
-                           workdir='.',
-                           env=merged_env,
                            doStepIf=cleanBuildRequested))
 
     # Create configuration files with cmake
@@ -87,44 +92,106 @@
                            env=merged_env))
 
     # Build
-    f.addStep(ShellCommand(name="build",
+    f.addStep(WarningCountingShellCommand(name="build",
                            command=build_cmd,
                            haltOnFailure=True,
                            description=["build"],
                            workdir=llvm_objdir,
                            env=merged_env))
 
+    clangexe = "%(workdir)s/" + llvm_objdir + "/bin/clang"
+    clangxxexe = "%(workdir)s/" + llvm_objdir + "/bin/clang++"
+    litexe = "%(workdir)s/" + llvm_objdir + "/bin/llvm-lit"
+    sizeexe = "%(workdir)s/" + llvm_objdir + "/bin/llvm-size"
+
     # Clean install dir
     if install:
-        f.addStep(ShellCommand(name='clean-install-dir',
-                               command=['rm', '-rf', llvm_instdir],
+        f.addStep(RemoveDirectory(name='clean-install-dir',
+                               dir=llvm_instdir,
                                haltOnFailure=False,
-                               description=["clean install dir"],
-                               workdir='.',
-                               env=merged_env,
                                doStepIf=cleanBuildRequested))
 
         f.addStep(ShellCommand(name="install",
                                command=install_cmd,
-                               haltOnFailure=False,
+                               haltOnFailure=True,
                                description=["install"],
                                workdir=llvm_objdir,
                                env=merged_env))
 
+        # If installing, use the installed version of clang.
+        clangexe = "%(workdir)s/" + llvm_instdir + "/bin/clang"
+        clangxxexe = "%(workdir)s/" + llvm_instdir + "/bin/clang++"
+        sizeexe = "%(workdir)s/" + llvm_instdir + "/bin/llvm-size"
+
     # Test
     if checkAll:
-        f.addStep(ShellCommand(name="check_all",
+        f.addStep(LitTestCommand(name="check_all",
                                command=check_all_cmd,
                                haltOnFailure=False,
                                description=["check all"],
                                workdir=llvm_objdir,
                                env=merged_env))
     else:
-        f.addStep(ShellCommand(name="check_polly",
+        f.addStep(LitTestCommand(name="check_polly",
                                command=check_polly_cmd,
                                haltOnFailure=False,
                                description=["check polly"],
                                workdir=llvm_objdir,
                                env=merged_env))
 
+    if testsuite:
+        f.addStep(RemoveDirectory(name='test-suite_clean-src-dir',
+                           dir=testsuite_srcdir,
+                           haltOnFailure=False,
+                           warnOnFailure=True,
+                           doStepIf=cleanBuildRequestedByProperty))
+
+        f.addGetSourcecodeForProject(
+            project='test-suite',
+            src_dir=testsuite_srcdir,
+            alwaysUseLatest=True)
+
+        f.addStep(RemoveDirectory(name='test-suite_clean-build-dir',
+                           dir=testsuite_builddir,
+                           haltOnFailure=False,
+                           warnOnFailure=True))
+
+        # -Wno-unused-command-line-argument is needed because linking will not uses the "-mllvm -polly" argument.
+        f.addStep(ShellCommand(name='test-suite_cmake-configure',
+                           description=["Test-Suite: cmake"],
+                           command=["cmake", '-B', testsuite_builddir, '-S', testsuite_srcdir,
+                                    "-DCMAKE_BUILD_TYPE=Release",
+                                    "-DTEST_SUITE_COLLECT_STATS=ON",
+                                    "-DTEST_SUITE_EXTRA_C_FLAGS=-Wno-unused-command-line-argument -mllvm -polly",
+                                    "-DTEST_SUITE_EXTRA_CXX_FLAGS=-mllvm -polly",
+                                    "-DTEST_SUITE_LIT_FLAGS=-vv;-o;report.json",
+                                    WithProperties("-DCMAKE_C_COMPILER=" + clangexe),
+                                    WithProperties("-DCMAKE_CXX_COMPILER=" + clangxxexe),
+                                    WithProperties("-DTEST_SUITE_LLVM_SIZE=" + sizeexe),
+                                    WithProperties("-DTEST_SUITE_LIT=" + litexe),
+                                ] + extraTestsuiteCmakeArgs,
+                           haltOnFailure=True,
+                           workdir='.',
+                           env=merged_env))
+
+        f.addStep(WarningCountingShellCommand(name='test-suite_build',
+                           description=["Test-Suite: build"],
+                           # Continue building; programs that don't compile will fail with NOEXE.
+                           command=[make, 'all', '-k0'] + jobs_cmd,
+                           haltOnFailure=False,
+                           flunkOnFailure=True,
+                           workdir=testsuite_builddir,
+                           env=merged_env))
+
+        f.addStep(LitTestCommand(name='test-suite_run',
+                            description=['Test-Suite: run'],
+                            command=[WithProperties(litexe), '-vv', '-o', 'report.json', '.'],
+                            haltOnFailure=True,
+                            workdir=testsuite_builddir,
+                            logfiles={
+                                'test.log'   : 'test.log',
+                                'report.json': 'report.json'
+                                 },
+                            env=merged_env))
+
     return f