Move llvm-external-projects/iree-compiler-api -> iree/compiler/API. (#8911)

* Move llvm-external-projects/iree-compiler-api -> iree/compiler/Api.

Non-functional changes:

* Relayers the build so that we can simply delegate to the LLVM macros for C/Python API building.
* Various escalation of include hack battle.
* Brings the compile_sample_module.py test from a sample which did not run to a real test.
* Reworks tests to be based on IREE macros (as possible). No more magic.
* Retired the old build/install locations for this piece: PYTHONPATH=${iree_build}/bindings/python gets everything now.

Functional changes:

* Since this is "in-tree" now, includes are laid out per the rest of the project. So:
  * `#include <iree-compiler-c/Compiler.h>` -> `#include <iree/compiler/Api/Compiler.h>
* Adds `iree.compiler.transforms.ireec.register_all_dialects(context)`, needed to make the `compile_sample_module.py` test work (Fixes #8604).
* iree/compiler/setup.py should now work from both the source and build dir. If using from the latter, it will use in-situ artifacts (will not rebuild).

In a subsequent change, I will do a similar refactoring for the Python runtime API and update user docs. For now the iree/compiler/README.md has instructions for building/installing a Python package from source (we've never had documentation for this, so something > nothing and we can move it around later).

For integration into internal Google, this shouldn't require more than some path changes from `llvm-external-projects/iree-compiler-api` -> `iree/compiler/Api` in BUILD files.

Fixes #8597
Progress on #6958
Progress on #3817

* Silence pytype for setup.py and remove obsolte path from CI.

* Re-enable namespace package auto-detection.

* Fix typo in .env file (should fix integrations tests)

* Rename Api -> API

* Fix one more Api -> API rename

* Address comments
diff --git a/.github/workflows/build_package.yml b/.github/workflows/build_package.yml
index f8daeba..75eaf98 100644
--- a/.github/workflows/build_package.yml
+++ b/.github/workflows/build_package.yml
@@ -194,7 +194,7 @@
           # In pip 21.3, in-tree builds became the default and only way to
           # build. We require that and make sure to be past that threshold.
           export CIBW_BEFORE_BUILD="python -m pip install --upgrade pip>=21.3"
-          python -m cibuildwheel --output-dir bindist ./main_checkout/llvm-external-projects/iree-compiler-api
+          python -m cibuildwheel --output-dir bindist ./main_checkout/iree/compiler
 
       # Compiler tools wheels are not python version specific, so just build
       # for one examplar python version.
diff --git a/.gitignore b/.gitignore
index a6b3352..db16c8f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,6 +49,9 @@
 *.venv
 .venv
 
+# Python deployment artifacts
+*.whl
+
 # Android Studio / Gradle files
 .cxx
 .gradle
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e6bd799..ff9e985 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -476,12 +476,23 @@
     ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include
   )
 
+  # TODO: It should be possible to fix upstream targets so as to not require
+  # any of these. Remove as they become unnecessary.
   function(_hack_llvm_include_paths)
     set(_common_include_dirs
+      # LLVM
       $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/llvm/include>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/include>
+
+      # MLIR
       $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/mlir/include>
       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include>
+
+      # LLD
+      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/lld/include>
+      $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/lld/include>
+
+      # MHLO
       $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir-hlo/include>
     )
     # Avoid globally modifying paths by instead adding the include paths to the
@@ -645,6 +656,16 @@
 # and tracy, and must come after it.
 if(${IREE_BUILD_PYTHON_BINDINGS})
   add_subdirectory(bindings/python)
+  # Write out a .env file to make IDEs and developers happy.
+  # Yes, we are writing this to the source dir. It is only for IDEs and if
+  # it gets clobbered, it is fine.
+  set(_pythonpath_env "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}/bindings/python\n")
+  file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env"
+    CONTENT "${_pythonpath_env}"
+  )
+  file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env"
+    CONTENT "${_pythonpath_env}"
+  )
 endif()
 
 if(${IREE_BUILD_BINDINGS_TFLITE})
@@ -673,36 +694,6 @@
 set(IREE_PUBLIC_INCLUDE_DIRS "${IREE_COMMON_INCLUDE_DIRS}"
     CACHE INTERNAL "IREE: Include Directories" FORCE)
 
-# Include the iree-compiler-api sub-project. We do this for development
-# and CI so that developers have access to the API and tools it provides
-# (otherwise, they would need to build multiple top-level projects).
-# However, logically, iree-compiler-api depends on iree, and for deployment
-# is always built standalone, taking responsibility to include iree and LLVM
-# as sub-projects.
-# The dependency mode is controlled by the variables
-# IREE_COMPILER_API_STANDALONE, which will be set if iree-compiler-api is
-# top-level. Otherwise, we set IREE_COMPILER_API_SUB_PROJECT, indicating it
-# is being embedded as a sub project.
-if(IREE_BUILD_COMPILER AND IREE_BUILD_PYTHON_BINDINGS)
-  if(NOT IREE_COMPILER_API_STANDALONE)
-    message(STATUS "Including iree-compiler-api as a sub-project")
-    set(IREE_COMPILER_API_SUB_PROJECT ON)
-    add_subdirectory(llvm-external-projects/iree-compiler-api
-        "${CMAKE_CURRENT_BINARY_DIR}/compiler-api")
-
-    # Write out a .env file to make IDEs and developers happy.
-    # Yes, we are writing this to the source dir. It is only for IDEs and if
-    # it gets clobbered, it is fine.
-    set(_pythonpath_env "PYTHONPATH=$<SHELL_PATH:${CMAKE_CURRENT_BINARY_DIR}/compiler-api/python_package;${CMAKE_CURRENT_BINARY_DIR}/bindings/python>\n")
-    file(GENERATE OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/.env"
-      CONTENT "${_pythonpath_env}"
-    )
-    file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/.env"
-      CONTENT "${_pythonpath_env}"
-    )
-  endif()
-endif()
-
 #-------------------------------------------------------------------------------
 # IREE benchmark tools
 #-------------------------------------------------------------------------------
diff --git a/build_tools/bazel/build_core.sh b/build_tools/bazel/build_core.sh
index e076e9c..a696b1a 100755
--- a/build_tools/bazel/build_core.sh
+++ b/build_tools/bazel/build_core.sh
@@ -84,7 +84,6 @@
   query \
     --config=non_darwin \
     //iree/... + //build_tools/... + \
-    //llvm-external-projects/iree-compiler-api/... + \
     //llvm-external-projects/iree-dialects/... | \
       xargs --max-args 1000000 --max-chars 1000000 --exit \
         bazel \
diff --git a/build_tools/cmake/iree_python.cmake b/build_tools/cmake/iree_python.cmake
index 48fd3d3..18d66b0 100644
--- a/build_tools/cmake/iree_python.cmake
+++ b/build_tools/cmake/iree_python.cmake
@@ -336,7 +336,7 @@
 
   set_property(TEST ${_NAME_PATH} PROPERTY LABELS "${_RULE_LABELS}")
   set_property(TEST ${_NAME_PATH} PROPERTY ENVIRONMENT
-      "PYTHONPATH=${IREE_BINARY_DIR}/compiler-api/python_package:${IREE_BINARY_DIR}/bindings/python:$ENV{PYTHONPATH}"
+      "PYTHONPATH=${IREE_BINARY_DIR}/bindings/python:$ENV{PYTHONPATH}"
       "TEST_TMPDIR=${IREE_BINARY_DIR}/tmp/${_NAME}_test_tmpdir"
   )
   iree_add_test_environment_properties(${_NAME_PATH})
diff --git a/build_tools/pytype/check_diff.sh b/build_tools/pytype/check_diff.sh
index 9b7c8b4..a7756b7 100755
--- a/build_tools/pytype/check_diff.sh
+++ b/build_tools/pytype/check_diff.sh
@@ -26,7 +26,9 @@
 
 # We seperate the python files into multiple pytype calls because otherwise
 # Ninja gets confused. See https://github.com/google/pytype/issues/198
-BASE=$(echo "${FILES?}" | grep -vP '^(\./)?integrations/*$')
+BASE=$(echo "${FILES?}" | \
+       grep -vP '^(\./)?integrations/*$' | \
+       grep -vP '(\./)?setup\.py$')
 IREE_TF=$(echo "${FILES?}" | \
           grep -P '^(\./)?integrations/tensorflow/bindings/python/iree/tf/.*')
 IREE_XLA=$(echo "${FILES?}" | \
diff --git a/llvm-external-projects/iree-compiler-api/BUILD.bazel b/iree/compiler/API/BUILD
similarity index 87%
rename from llvm-external-projects/iree-compiler-api/BUILD.bazel
rename to iree/compiler/API/BUILD
index 8ad8fcd..7051778 100644
--- a/llvm-external-projects/iree-compiler-api/BUILD.bazel
+++ b/iree/compiler/API/BUILD
@@ -37,13 +37,13 @@
 
 filegroup(
     name = "python_compiler_api",
-    srcs = glob(["python/iree/compiler/api/*.py"]),
+    srcs = glob(["python/iree/compiler/transforms/*.py"]),
 )
 
 filegroup(
     name = "unittests_tools_testdata",
     srcs = glob([
-        "unittests/tools/testdata/*",
+        "python/test/tools/testdata/*",
     ]),
 )
 
@@ -54,15 +54,14 @@
 cc_library(
     name = "CAPI",
     srcs = [
-        "lib/CAPI/Compiler.cpp",
-        "lib/CAPI/Lld.cpp",
-        "lib/CAPI/Tools.cpp",
+        "Compiler.cpp",
+        "Lld.cpp",
+        "Tools.cpp",
     ],
     hdrs = [
-        "include/iree-compiler-c/Compiler.h",
-        "include/iree-compiler-c/Tools.h",
+        "Compiler.h",
+        "Tools.h",
     ],
-    includes = ["include"],
     deps = [
         "//iree/compiler/ConstEval",
         "//iree/compiler/Dialect/VM/IR",
diff --git a/iree/compiler/API/CMakeLists.txt b/iree/compiler/API/CMakeLists.txt
new file mode 100644
index 0000000..0bcacfd
--- /dev/null
+++ b/iree/compiler/API/CMakeLists.txt
@@ -0,0 +1,83 @@
+# Copyright 2022 The IREE Authors
+#
+# Licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+################################################################################
+# CAPI library using the LLVM build system.
+#
+# WARNING WILL ROBINSON!
+# This does not look like the rest of IREE. It is directly using the upstream
+# LLVM build system in order to create bundled compiler API binaries that
+# are consistent with LLVM. Consult upstream CMake macros if you don't
+# understand what this does.
+################################################################################
+
+include(AddLLVM)
+include(AddMLIR)
+
+# LLD may not have been enabled at the global level (typically because we
+# are not building for a CPU backend). If not, don't add API support for it.
+set(_OPTIONAL_SOURCES)
+set(_OPTIONAL_LINK_LIBS)
+if(TARGET lld)
+  list(APPEND _OPTIONAL_SOURCES Lld.cpp)
+  list(APPEND _OPTIONAL_LINK_LIBS
+    lldCommon
+    lldCOFF
+    lldELF
+    lldMachO
+    lldMinGW
+    lldWasm
+  )
+endif()
+
+add_mlir_public_c_api_library(IREECompilerCAPILib
+  Compiler.cpp
+  Tools.cpp
+  ${_OPTIONAL_SOURCES}
+  PARTIAL_SOURCES_INTENDED
+  # TODO: If installing, complains about IREEVM not being in any export set.
+  DISABLE_INSTALL
+  LINK_COMPONENTS
+    Support
+  LINK_LIBS PUBLIC
+    MLIRIR
+    iree::compiler::ConstEval
+    iree::compiler::InputConversion::MHLO::MHLO
+    iree::compiler::InputConversion::TOSA::TOSA
+    iree::compiler::Dialect::VM::IR::IR
+    iree::compiler::Dialect::VM::Target::Bytecode::Bytecode
+    iree::compiler::Pipelines
+
+    # Passes and dialects.
+    iree::tools::init_llvmir_translations
+    iree::tools::init_passes_and_dialects
+
+    # All HAL Targets.
+    iree::tools::init_targets
+
+    # Tools.
+    iree::tools::iree_translate_lib
+
+    ${_OPTIONAL_LINK_LIBS}
+)
+
+if(IREE_BUILD_TORCH_MLIR_SUPPORT)
+  target_compile_definitions(obj.IREECompilerCAPILib
+    PUBLIC
+    IREE_HAVE_TORCH_MLIR_DIALECTS
+  )
+endif()
+
+add_subdirectory(test)
+
+################################################################################
+# Language specific bindings.
+# These have an ordering dependency on the backing CAPI being defined.
+################################################################################
+
+if(IREE_BUILD_PYTHON_BINDINGS)
+  add_subdirectory(python)
+endif()
diff --git a/llvm-external-projects/iree-compiler-api/lib/CAPI/Compiler.cpp b/iree/compiler/API/Compiler.cpp
similarity index 98%
rename from llvm-external-projects/iree-compiler-api/lib/CAPI/Compiler.cpp
rename to iree/compiler/API/Compiler.cpp
index 51aa04e..0a4b03a 100644
--- a/llvm-external-projects/iree-compiler-api/lib/CAPI/Compiler.cpp
+++ b/iree/compiler/API/Compiler.cpp
@@ -4,7 +4,7 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "iree-compiler-c/Compiler.h"
+#include "iree/compiler/API/Compiler.h"
 
 #include "iree/compiler/ConstEval/Passes.h"
 #include "iree/compiler/Dialect/VM/IR/VMOps.h"
diff --git a/llvm-external-projects/iree-compiler-api/include/iree-compiler-c/Compiler.h b/iree/compiler/API/Compiler.h
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/include/iree-compiler-c/Compiler.h
rename to iree/compiler/API/Compiler.h
diff --git a/llvm-external-projects/iree-compiler-api/lib/CAPI/Lld.cpp b/iree/compiler/API/Lld.cpp
similarity index 98%
rename from llvm-external-projects/iree-compiler-api/lib/CAPI/Lld.cpp
rename to iree/compiler/API/Lld.cpp
index 5fa3239..d1a9b87 100644
--- a/llvm-external-projects/iree-compiler-api/lib/CAPI/Lld.cpp
+++ b/iree/compiler/API/Lld.cpp
@@ -12,7 +12,7 @@
 #include <cstdlib>
 #include <vector>
 
-#include "iree-compiler-c/Tools.h"
+#include "iree/compiler/API/Tools.h"
 #include "lld/Common/Driver.h"
 #include "lld/Common/ErrorHandler.h"
 #include "lld/Common/Memory.h"
diff --git a/llvm-external-projects/iree-compiler-api/README.md b/iree/compiler/API/README.md
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/README.md
rename to iree/compiler/API/README.md
diff --git a/llvm-external-projects/iree-compiler-api/lib/CAPI/Tools.cpp b/iree/compiler/API/Tools.cpp
similarity index 90%
rename from llvm-external-projects/iree-compiler-api/lib/CAPI/Tools.cpp
rename to iree/compiler/API/Tools.cpp
index e54b595..d4c7a5c 100644
--- a/llvm-external-projects/iree-compiler-api/lib/CAPI/Tools.cpp
+++ b/iree/compiler/API/Tools.cpp
@@ -4,7 +4,7 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "iree-compiler-c/Tools.h"
+#include "iree/compiler/API/Tools.h"
 
 #include "iree/tools/iree_translate_lib.h"
 
diff --git a/llvm-external-projects/iree-compiler-api/include/iree-compiler-c/Tools.h b/iree/compiler/API/Tools.h
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/include/iree-compiler-c/Tools.h
rename to iree/compiler/API/Tools.h
diff --git a/llvm-external-projects/iree-compiler-api/python/CMakeLists.txt b/iree/compiler/API/python/CMakeLists.txt
similarity index 64%
rename from llvm-external-projects/iree-compiler-api/python/CMakeLists.txt
rename to iree/compiler/API/python/CMakeLists.txt
index 43e4dd6..1587cc7 100644
--- a/llvm-external-projects/iree-compiler-api/python/CMakeLists.txt
+++ b/iree/compiler/API/python/CMakeLists.txt
@@ -1,9 +1,37 @@
+# Copyright 2022 The IREE Authors
+#
+# Licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
 include(AddMLIRPython)
 
 # Specifies that all MLIR packages are co-located under npcomp.
 # TODO: Add an upstream cmake param for this vs having a global here.
 add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=iree.compiler.")
 
+set(_PYTHON_BUILD_PREFIX "${IREE_BINARY_DIR}/bindings/python")
+set(_PYTHON_INSTALL_PREFIX "python_packages/iree-compiler")
+
+# HACK: This should not be necessary, but add_mlir_python_extension is
+# accidentally closing over an errant include_directories from up-tree, so
+# when built in-tree it is somehow working based on that. This will need
+# to be fixed to capture the correct include directories in that macro.
+include_directories(
+  "${IREE_SOURCE_DIR}"
+  "${IREE_SOURCE_DIR}/llvm-external-projects/iree-dialects/include"
+  "${IREE_SOURCE_DIR}/third_party/llvm-project/mlir/include"
+  "${IREE_SOURCE_DIR}/third_party/mlir-hlo/include"
+)
+
+# On Unixes, disable the creation of versioned/symlinked `.so` files. With
+# this set, we just generate libIREECompilerAggregateCAPI.so vs making that
+# a symlink to a versioned file right next to it. When packaging for Python,
+# symlinks are duplicated, so this is pretty important. It is usually set at
+# a toolchain level for dedicated Python builds, but is nice to override since
+# it makes the build directory suitable for directly packaging.
+set(CMAKE_PLATFORM_NO_VERSIONED_SONAME 1)
+
 ################################################################################
 # Sources
 ################################################################################
@@ -36,7 +64,7 @@
   SOURCES
     IREECTransforms.cpp
   EMBED_CAPI_LINK_LIBS
-    IREECompilerAPICompilerCAPI
+    IREECompilerCAPILib
   PRIVATE_LINK_LIBS
     LLVMSupport
 )
@@ -82,15 +110,15 @@
 
 add_mlir_python_common_capi_library(IREECompilerAggregateCAPI
   INSTALL_COMPONENT IREECompilerPythonModules
-  INSTALL_DESTINATION python_package/iree/compiler/_mlir_libs
-  OUTPUT_DIRECTORY "${IREE_COMPILER_API_BINARY_DIR}/python_package/iree/compiler/_mlir_libs"
+  INSTALL_DESTINATION ${_PYTHON_INSTALL_PREFIX}/iree/compiler/_mlir_libs
+  OUTPUT_DIRECTORY "${_PYTHON_BUILD_PREFIX}/iree/compiler/_mlir_libs"
   RELATIVE_INSTALL_ROOT "../../../.."
   DECLARED_SOURCES ${_source_components}
 )
 
 add_mlir_python_modules(IREECompilerPythonModules
-  ROOT_PREFIX "${IREE_COMPILER_API_BINARY_DIR}/python_package/iree/compiler"
-  INSTALL_PREFIX "python_package/iree/compiler"
+  ROOT_PREFIX "${_PYTHON_BUILD_PREFIX}/iree/compiler"
+  INSTALL_PREFIX "${_PYTHON_INSTALL_PREFIX}/iree/compiler"
   DECLARED_SOURCES ${_source_components}
   COMMON_CAPI_LINK_LIBS
     IREECompilerAggregateCAPI
@@ -116,12 +144,12 @@
   set_target_properties(${target}
     PROPERTIES
       OUTPUT_NAME "${ARG_OUTPUT_NAME}"
-      RUNTIME_OUTPUT_DIRECTORY "${IREE_COMPILER_API_BINARY_DIR}/python_package/iree/compiler/_mlir_libs"
+      RUNTIME_OUTPUT_DIRECTORY "${_PYTHON_BUILD_PREFIX}/iree/compiler/_mlir_libs"
   )
   mlir_python_setup_extension_rpath(${target})
   add_dependencies(IREECompilerPythonModules ${target})
   install(TARGETS ${target}
-    DESTINATION "python_package/iree/compiler/_mlir_libs"
+    DESTINATION "${_PYTHON_INSTALL_PREFIX}/iree/compiler/_mlir_libs"
   )
 endfunction()
 
@@ -140,3 +168,9 @@
       LldTool.c
   )
 endif()
+
+################################################################################
+# Subdirectories
+################################################################################
+
+add_subdirectory(test)
diff --git a/llvm-external-projects/iree-compiler-api/python/IREECTransforms.cpp b/iree/compiler/API/python/IREECTransforms.cpp
similarity index 97%
rename from llvm-external-projects/iree-compiler-api/python/IREECTransforms.cpp
rename to iree/compiler/API/python/IREECTransforms.cpp
index 74029b1..b35049f 100644
--- a/llvm-external-projects/iree-compiler-api/python/IREECTransforms.cpp
+++ b/iree/compiler/API/python/IREECTransforms.cpp
@@ -4,7 +4,7 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "iree-compiler-c/Compiler.h"
+#include "iree/compiler/API/Compiler.h"
 #include "mlir-c/Bindings/Python/Interop.h"
 #include "mlir/Bindings/Python/PybindAdaptors.h"
 
@@ -142,6 +142,9 @@
   m.doc() = "ireec transforms API";
   ireeCompilerRegisterTargetBackends();
 
+  m.def("register_all_dialects",
+        [](MlirContext context) { ireeCompilerRegisterAllDialects(context); });
+
   py::class_<PyCompilerOptions>(m, "CompilerOptions",
                                 "Options for the IREE backend compiler.")
       .def(py::init([](py::args args) {
diff --git a/llvm-external-projects/iree-compiler-api/python/IREECompileTool.c b/iree/compiler/API/python/IREECompileTool.c
similarity index 88%
rename from llvm-external-projects/iree-compiler-api/python/IREECompileTool.c
rename to iree/compiler/API/python/IREECompileTool.c
index 5428add..2eae64e 100644
--- a/llvm-external-projects/iree-compiler-api/python/IREECompileTool.c
+++ b/iree/compiler/API/python/IREECompileTool.c
@@ -4,6 +4,6 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "iree-compiler-c/Tools.h"
+#include "iree/compiler/API/Tools.h"
 
 int main(int argc, char **argv) { return ireeCompilerRunMain(argc, argv); }
diff --git a/llvm-external-projects/iree-compiler-api/python/LldTool.c b/iree/compiler/API/python/LldTool.c
similarity index 88%
rename from llvm-external-projects/iree-compiler-api/python/LldTool.c
rename to iree/compiler/API/python/LldTool.c
index 4fd0c9b..dfc2ac6 100644
--- a/llvm-external-projects/iree-compiler-api/python/LldTool.c
+++ b/iree/compiler/API/python/LldTool.c
@@ -4,6 +4,6 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "iree-compiler-c/Tools.h"
+#include "iree/compiler/API/Tools.h"
 
 int main(int argc, char **argv) { return ireeCompilerRunLldMain(argc, argv); }
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/__init__.py b/iree/compiler/API/python/iree/compiler/__init__.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/__init__.py
rename to iree/compiler/API/python/iree/compiler/__init__.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tf.py b/iree/compiler/API/python/iree/compiler/tf.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tf.py
rename to iree/compiler/API/python/iree/compiler/tf.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tflite.py b/iree/compiler/API/python/iree/compiler/tflite.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tflite.py
rename to iree/compiler/API/python/iree/compiler/tflite.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/__init__.py b/iree/compiler/API/python/iree/compiler/tools/__init__.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/__init__.py
rename to iree/compiler/API/python/iree/compiler/tools/__init__.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/binaries.py b/iree/compiler/API/python/iree/compiler/tools/binaries.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/binaries.py
rename to iree/compiler/API/python/iree/compiler/tools/binaries.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/core.py b/iree/compiler/API/python/iree/compiler/tools/core.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/core.py
rename to iree/compiler/API/python/iree/compiler/tools/core.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/debugging.py b/iree/compiler/API/python/iree/compiler/tools/debugging.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/debugging.py
rename to iree/compiler/API/python/iree/compiler/tools/debugging.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/scripts/__init__.py b/iree/compiler/API/python/iree/compiler/tools/scripts/__init__.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/scripts/__init__.py
rename to iree/compiler/API/python/iree/compiler/tools/scripts/__init__.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/scripts/ireec/__main__.py b/iree/compiler/API/python/iree/compiler/tools/scripts/ireec/__main__.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/scripts/ireec/__main__.py
rename to iree/compiler/API/python/iree/compiler/tools/scripts/ireec/__main__.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/tf.py b/iree/compiler/API/python/iree/compiler/tools/tf.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/tf.py
rename to iree/compiler/API/python/iree/compiler/tools/tf.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/tflite.py b/iree/compiler/API/python/iree/compiler/tools/tflite.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/tflite.py
rename to iree/compiler/API/python/iree/compiler/tools/tflite.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/xla.py b/iree/compiler/API/python/iree/compiler/tools/xla.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/tools/xla.py
rename to iree/compiler/API/python/iree/compiler/tools/xla.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/transforms/ireec.py b/iree/compiler/API/python/iree/compiler/transforms/ireec.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/transforms/ireec.py
rename to iree/compiler/API/python/iree/compiler/transforms/ireec.py
diff --git a/llvm-external-projects/iree-compiler-api/python/iree/compiler/xla.py b/iree/compiler/API/python/iree/compiler/xla.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/python/iree/compiler/xla.py
rename to iree/compiler/API/python/iree/compiler/xla.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt b/iree/compiler/API/python/test/CMakeLists.txt
similarity index 68%
copy from llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
copy to iree/compiler/API/python/test/CMakeLists.txt
index e0a903d..22bb86c 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
+++ b/iree/compiler/API/python/test/CMakeLists.txt
@@ -4,9 +4,5 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-iree_compiler_api_py_test(
-  NAME
-    compiler_options_test
-  MAIN
-    "compiler_options_test.py"
-)
+add_subdirectory(tools)
+add_subdirectory(transforms)
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/CMakeLists.txt b/iree/compiler/API/python/test/tools/CMakeLists.txt
similarity index 76%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/CMakeLists.txt
rename to iree/compiler/API/python/test/tools/CMakeLists.txt
index e276d13..5a2a29a 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/tools/CMakeLists.txt
+++ b/iree/compiler/API/python/test/tools/CMakeLists.txt
@@ -4,30 +4,30 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-iree_compiler_api_py_test(
+iree_py_test(
   NAME
     compiler_core_test
-  MAIN
+  SRCS
     "compiler_core_test.py"
 )
 
-iree_compiler_api_py_test(
+iree_py_test(
   NAME
     compiler_tf_test
-  MAIN
+  SRCS
     "compiler_tf_test.py"
 )
 
-iree_compiler_api_py_test(
+iree_py_test(
   NAME
     compiler_tflite_test
-  MAIN
+  SRCS
     "compiler_tflite_test.py"
 )
 
-iree_compiler_api_py_test(
+iree_py_test(
   NAME
     compiler_xla_test
-  MAIN
+  SRCS
     "compiler_xla_test.py"
 )
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/README.md b/iree/compiler/API/python/test/tools/README.md
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/README.md
rename to iree/compiler/API/python/test/tools/README.md
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/compiler_core_test.py b/iree/compiler/API/python/test/tools/compiler_core_test.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/compiler_core_test.py
rename to iree/compiler/API/python/test/tools/compiler_core_test.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/compiler_tf_test.py b/iree/compiler/API/python/test/tools/compiler_tf_test.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/compiler_tf_test.py
rename to iree/compiler/API/python/test/tools/compiler_tf_test.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/compiler_tflite_test.py b/iree/compiler/API/python/test/tools/compiler_tflite_test.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/compiler_tflite_test.py
rename to iree/compiler/API/python/test/tools/compiler_tflite_test.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/compiler_xla_test.py b/iree/compiler/API/python/test/tools/compiler_xla_test.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/compiler_xla_test.py
rename to iree/compiler/API/python/test/tools/compiler_xla_test.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/testdata/generate_tflite.py b/iree/compiler/API/python/test/tools/testdata/generate_tflite.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/testdata/generate_tflite.py
rename to iree/compiler/API/python/test/tools/testdata/generate_tflite.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/testdata/generate_xla.py b/iree/compiler/API/python/test/tools/testdata/generate_xla.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/testdata/generate_xla.py
rename to iree/compiler/API/python/test/tools/testdata/generate_xla.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/testdata/tflite_sample.fb b/iree/compiler/API/python/test/tools/testdata/tflite_sample.fb
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/testdata/tflite_sample.fb
rename to iree/compiler/API/python/test/tools/testdata/tflite_sample.fb
Binary files differ
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/testdata/xla_sample.hlo b/iree/compiler/API/python/test/tools/testdata/xla_sample.hlo
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/testdata/xla_sample.hlo
rename to iree/compiler/API/python/test/tools/testdata/xla_sample.hlo
diff --git a/llvm-external-projects/iree-compiler-api/unittests/tools/testdata/xla_sample.pb b/iree/compiler/API/python/test/tools/testdata/xla_sample.pb
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/tools/testdata/xla_sample.pb
rename to iree/compiler/API/python/test/tools/testdata/xla_sample.pb
Binary files differ
diff --git a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt b/iree/compiler/API/python/test/transforms/CMakeLists.txt
similarity index 68%
copy from llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
copy to iree/compiler/API/python/test/transforms/CMakeLists.txt
index e0a903d..18914fd 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
+++ b/iree/compiler/API/python/test/transforms/CMakeLists.txt
@@ -4,9 +4,4 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-iree_compiler_api_py_test(
-  NAME
-    compiler_options_test
-  MAIN
-    "compiler_options_test.py"
-)
+add_subdirectory(ireec)
diff --git a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt b/iree/compiler/API/python/test/transforms/ireec/CMakeLists.txt
similarity index 72%
rename from llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
rename to iree/compiler/API/python/test/transforms/ireec/CMakeLists.txt
index e0a903d..384f33a 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/CMakeLists.txt
+++ b/iree/compiler/API/python/test/transforms/ireec/CMakeLists.txt
@@ -4,9 +4,16 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-iree_compiler_api_py_test(
+iree_py_test(
   NAME
     compiler_options_test
-  MAIN
+  SRCS
     "compiler_options_test.py"
 )
+
+iree_py_test(
+  NAME
+    compile_sample_module
+  SRCS
+    "compile_sample_module.py"
+)
diff --git a/llvm-external-projects/iree-compiler-api/build_tools/smoketest.py b/iree/compiler/API/python/test/transforms/ireec/compile_sample_module.py
similarity index 69%
rename from llvm-external-projects/iree-compiler-api/build_tools/smoketest.py
rename to iree/compiler/API/python/test/transforms/ireec/compile_sample_module.py
index 2c12eba..55eccd8 100644
--- a/llvm-external-projects/iree-compiler-api/build_tools/smoketest.py
+++ b/iree/compiler/API/python/test/transforms/ireec/compile_sample_module.py
@@ -9,14 +9,15 @@
 
 from iree.compiler import ir
 from iree.compiler import passmanager
-from iree.compiler import version
+from iree.compiler.transforms import ireec
+
+# The compiler re-exports API access to a number of dialects. If one of these
+# fails to import, it indicates a build issue.
 from iree.compiler.dialects import arith
 from iree.compiler.dialects import chlo
 from iree.compiler.dialects import mhlo
 from iree.compiler.dialects import iree_input
 from iree.compiler.dialects import builtin
-from iree.compiler.dialects import std
-from iree.compiler.dialects import linalg
 from iree.compiler.dialects import linalg
 from iree.compiler.dialects import math
 from iree.compiler.dialects import memref
@@ -25,17 +26,13 @@
 from iree.compiler.dialects import tosa
 from iree.compiler.dialects import vector
 
-from iree.compiler.transforms import ireec
-
 # Test the compiler API.
 with ir.Context() as ctx:
-  chlo.register_chlo_dialect(ctx)
-  mhlo.register_mhlo_dialect(ctx)
-  iree_input.register_dialect(ctx)
+  ireec.register_all_dialects(ctx)
 
   input_module = ir.Module.parse(r"""
     builtin.module  {
-      builtin.func @fabs(%arg0: tensor<1x4xf32>, %arg1: tensor<4x1xf32>) -> tensor<4x4xf32> {
+      func.func @fabs(%arg0: tensor<1x4xf32>, %arg1: tensor<4x1xf32>) -> tensor<4x4xf32> {
         %0 = chlo.broadcast_add %arg0, %arg1 : (tensor<1x4xf32>, tensor<4x1xf32>) -> tensor<4x4xf32>
         %1 = "mhlo.abs"(%0) : (tensor<4x4xf32>) -> tensor<4x4xf32>
         return %1 : tensor<4x4xf32>
@@ -43,10 +40,10 @@
     }
   """)
 
-  options = ireec.CompilerOptions("--iree-input-type=mhlo",
-                                  "--iree-hal-target-backends=cpu")
+  options = ireec.CompilerOptions("--iree-hal-target-backends=cpu")
   print(options)
   pm = passmanager.PassManager()
+  ireec.build_mhlo_import_pass_pipeline(pm)
   ireec.build_iree_vm_pass_pipeline(options, pm)
   pm.run(input_module)
 
@@ -54,11 +51,3 @@
   bytecode_io = io.BytesIO()
   ireec.translate_module_to_vm_bytecode(options, input_module, bytecode_io)
   print(f"Bytecode module len = {len(bytecode_io.getbuffer())}")
-
-# Check version.
-print(f"PACKAGE_SUFFIX={version.PACKAGE_SUFFIX}")
-print(f"VERSION={version.VERSION}")
-print(f"REVISIONS={version.REVISIONS!r}")
-
-# Check console scripts.
-subprocess.check_output(["iree-compile", "-help"])
diff --git a/llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/compiler_options_test.py b/iree/compiler/API/python/test/transforms/ireec/compiler_options_test.py
similarity index 100%
rename from llvm-external-projects/iree-compiler-api/unittests/transforms/ireec/compiler_options_test.py
rename to iree/compiler/API/python/test/transforms/ireec/compiler_options_test.py
diff --git a/llvm-external-projects/iree-compiler-api/unittests/CAPI/CMakeLists.txt b/iree/compiler/API/test/CMakeLists.txt
similarity index 94%
rename from llvm-external-projects/iree-compiler-api/unittests/CAPI/CMakeLists.txt
rename to iree/compiler/API/test/CMakeLists.txt
index 5729d14..5c4514e 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/CAPI/CMakeLists.txt
+++ b/iree/compiler/API/test/CMakeLists.txt
@@ -10,7 +10,7 @@
 )
 target_link_libraries(iree-compiler-api-compile-mhlo-test
   PRIVATE
-    IREECompilerAPICompilerCAPI
+    IREECompilerCAPILib
     MLIRCAPIIR
 )
 
diff --git a/llvm-external-projects/iree-compiler-api/unittests/CAPI/compile-mhlo-test-main.c b/iree/compiler/API/test/compile-mhlo-test-main.c
similarity index 98%
rename from llvm-external-projects/iree-compiler-api/unittests/CAPI/compile-mhlo-test-main.c
rename to iree/compiler/API/test/compile-mhlo-test-main.c
index 6f8f95a..dd44804 100644
--- a/llvm-external-projects/iree-compiler-api/unittests/CAPI/compile-mhlo-test-main.c
+++ b/iree/compiler/API/test/compile-mhlo-test-main.c
@@ -12,8 +12,8 @@
 // be the first user of the combined MLIR+IREE CAPI:
 // https://github.com/google/iree/pull/8582
 
-#include <iree-compiler-c/Compiler.h>
 #include <iree/base/string_builder.h>
+#include <iree/compiler/API/Compiler.h>
 #include <stdio.h>
 
 static void bytecode_builder_callback(MlirStringRef str, void* userdata) {
diff --git a/iree/compiler/BUILD b/iree/compiler/BUILD
deleted file mode 100644
index 7c08db2..0000000
--- a/iree/compiler/BUILD
+++ /dev/null
@@ -1,17 +0,0 @@
-# Copyright 2019 The IREE Authors
-#
-# Licensed under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-package(
-    default_visibility = ["//visibility:public"],
-    features = ["layering_check"],
-    licenses = ["notice"],  # Apache 2.0
-)
-
-#===------------------------------------------------------------------------===#
-# Public API
-#===------------------------------------------------------------------------===#
-# TODO(#3817): expose :compiler as the public C API.
-# TODO(#3817): expose :cc as the public C++ wrapper API.
diff --git a/iree/compiler/CMakeLists.txt b/iree/compiler/CMakeLists.txt
index 63535ef..30987f3 100644
--- a/iree/compiler/CMakeLists.txt
+++ b/iree/compiler/CMakeLists.txt
@@ -11,3 +11,10 @@
 iree_add_all_subdirs()
 
 ### BAZEL_TO_CMAKE_PRESERVES_ALL_CONTENT_BELOW_THIS_LINE ###
+
+# Copy Python packaging files to the build dir so that we can install from
+# there.
+if(IREE_BUILD_PYTHON_BINDINGS)
+  configure_file(pyproject.toml pyproject.toml COPYONLY)
+  configure_file(setup.py setup.py @ONLY)
+endif()
diff --git a/iree/compiler/README.md b/iree/compiler/README.md
index 5eb2b89..6d7f6f0 100644
--- a/iree/compiler/README.md
+++ b/iree/compiler/README.md
@@ -7,6 +7,8 @@
 
 ```
 └── compiler/
+    ├── setup.py     (builds/installs the compiler Python API)
+    ├── API/         (C and Python APIs)
     ├── Bindings/    (used to generate different ABI bindings)
     ├── Codegen/     (device code generation for assorted APIs)
     ├── Dialect/
@@ -22,6 +24,7 @@
     │   └── VM/      (abstract Virtual Machine)
     ├── InputConversion/  (conversions from frontend/input dialects)
     └── Translation/      (translation pipeline definitions)
+
 ```
 
 Noteworthy compiler components _not_ included here include:
@@ -76,3 +79,45 @@
 * https://google.github.io/styleguide/cppguide.html
 * https://llvm.org/docs/CodingStandards.html
 * https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+
+## Standalone Python Builds
+
+The included `setup.py` file can be used to build Python binaries or directly
+install the IREE compiler API. Do note that the compiler is quite heavy and
+unless you are developing it and on a significant machine, you will want to
+use released binaries.
+
+There are two ways to build/install Python packages:
+
+* Directly from the source tree (this is how official releases are done).
+* From the build directory while developing.
+
+It is recommended to use your favorite method for managing
+[virtual environemnts](https://docs.python.org/3/library/venv.html) instead
+of modifying the system installation.
+
+Only relatively recent versions of `pip` are supported. Always use the latest
+via `pip install --upgrade pip`.
+
+You can build either from the source or build tree (assumes that CMake has
+been configured and the project built). The latter is typically used by
+project developers who are already setup for development and want to
+incrementally generate Python packages without rebuilding.
+
+To build a wheel that can be installed on the same Python version and OS:
+
+```
+python -m pip wheel iree/compiler
+```
+
+To directly install:
+
+```
+python -m pip install iree/compiler
+```
+
+In order to sanity check once the package is installed:
+
+```
+python iree/compiler/Api/python/test/transforms/ireec/compile_sample_module.py
+```
diff --git a/llvm-external-projects/iree-compiler-api/pyproject.toml b/iree/compiler/pyproject.toml
similarity index 95%
rename from llvm-external-projects/iree-compiler-api/pyproject.toml
rename to iree/compiler/pyproject.toml
index 43e846c..d1195bc 100644
--- a/llvm-external-projects/iree-compiler-api/pyproject.toml
+++ b/iree/compiler/pyproject.toml
@@ -8,6 +8,7 @@
     "ninja==1.10.2",
     # MLIR build depends.
     "numpy",
+    "packaging",
     # Version 2.7.0 excluded: https://github.com/pybind/pybind11/issues/3136
     "pybind11>=2.6.0,!=2.7.0",
     "PyYAML",
diff --git a/iree/compiler/setup.py b/iree/compiler/setup.py
new file mode 100644
index 0000000..5156477
--- /dev/null
+++ b/iree/compiler/setup.py
@@ -0,0 +1,357 @@
+# Copyright 2021 The IREE Authors
+#
+# Licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# Build/install the iree-compiler-backend python package.
+# Note that this includes a relatively large build of LLVM (~2400 C++ files)
+# and can take a considerable amount of time, especially with defaults.
+# To install:
+#   pip install .
+# To build a wheel:
+#   pip wheel .
+#
+# It is recommended to build with Ninja and ccache. To do so, set environment
+# variables by prefixing to above invocations:
+#   CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache
+#
+# On CIs, it is often advantageous to re-use/control the CMake build directory.
+# This can be set with the IREE_COMPILER_API_CMAKE_BUILD_DIR env var.
+from gettext import install
+import json
+from multiprocessing.spawn import prepare
+import os
+import platform
+import re
+import shutil
+import subprocess
+import sys
+import sysconfig
+
+from distutils.command.build import build as _build
+from setuptools import find_namespace_packages, setup, Extension
+from setuptools.command.build_ext import build_ext as _build_ext
+from setuptools.command.build_py import build_py as _build_py
+
+
+def check_pip_version():
+  from packaging import version
+  # Pip versions < 22.0.3 default to out of tree builds, which is quite
+  # incompatible with what we do (and has other issues). Pip >= 22.0.4
+  # removed this option entirely and are only in-tree builds. Since the
+  # old behavior can silently produce unworking installations, we aggressively
+  # suppress it.
+  try:
+    import pip
+  except ModuleNotFoundError:
+    # If pip not installed, we are obviously not trying to package via pip.
+    pass
+  else:
+    if (version.parse(pip.__version__) < version.parse("21.3")):
+      print("ERROR: pip version >= 21.3 required")
+      print("Upgrade: pip install pip --upgrade")
+      sys.exit(2)
+
+
+check_pip_version()
+
+# This file can be run directly from the source tree or it can be CMake
+# configured so it can run from the build tree with an already existing
+# build tree. We detect the difference based on whether the following
+# are expanded by CMake.
+CONFIGURED_SOURCE_DIR = "@IREE_SOURCE_DIR@"
+CONFIGURED_BINARY_DIR = "@IREE_BINARY_DIR@"
+
+IREE_SOURCE_DIR = None
+IREE_BINARY_DIR = None
+
+# We must do the intermediate installation to a fixed location that agrees
+# between what we pass to setup() and cmake. So hard-code it here.
+# Note that setup() needs a relative path (to the setup.py file).
+SETUPPY_DIR = os.path.realpath(os.path.dirname(__file__))
+CMAKE_INSTALL_DIR_REL = os.path.join("build", "cmake_install")
+CMAKE_INSTALL_DIR_ABS = os.path.join(SETUPPY_DIR, CMAKE_INSTALL_DIR_REL)
+
+IS_CONFIGURED = CONFIGURED_SOURCE_DIR[0] != "@"
+if IS_CONFIGURED:
+  IREE_SOURCE_DIR = CONFIGURED_SOURCE_DIR
+  IREE_BINARY_DIR = CONFIGURED_BINARY_DIR
+  print(
+      f"Running setup.py from build tree: "
+      f"SOURCE_DIR = {IREE_SOURCE_DIR} "
+      f"BINARY_DIR = {IREE_BINARY_DIR}",
+      file=sys.stderr)
+else:
+  IREE_SOURCE_DIR = os.path.join(SETUPPY_DIR, "..", "..")
+  IREE_BINARY_DIR = os.getenv("IREE_COMPILER_API_CMAKE_BUILD_DIR")
+  if not IREE_BINARY_DIR:
+    # Note that setuptools always builds into a "build" directory that
+    # is a sibling of setup.py, so we just colonize a sub-directory of that
+    # by default.
+    IREE_BINARY_DIR = os.path.join(SETUPPY_DIR, "build", "cmake_build")
+  print(
+      f"Running setup.py from source tree: "
+      f"SOURCE_DIR = {IREE_SOURCE_DIR} "
+      f"BINARY_DIR = {IREE_BINARY_DIR}",
+      file=sys.stderr)
+
+# Setup and get version information.
+VERSION_INFO_FILE = os.path.join(IREE_SOURCE_DIR, "version_info.json")
+
+
+def load_version_info():
+  with open(VERSION_INFO_FILE, "rt") as f:
+    return json.load(f)
+
+
+try:
+  version_info = load_version_info()
+except FileNotFoundError:
+  print("version_info.json not found. Using defaults", file=sys.stderr)
+  version_info = {}
+
+PACKAGE_SUFFIX = version_info.get("package-suffix") or ""
+PACKAGE_VERSION = version_info.get("package-version") or "0.1dev1"
+
+
+def maybe_nuke_cmake_cache():
+  # From run to run under pip, we can end up with different paths to ninja,
+  # which isn't great and will confuse cmake. Detect if the location of
+  # ninja changes and force a cache flush.
+  ninja_path = ""
+  try:
+    import ninja
+  except ModuleNotFoundError:
+    pass
+  else:
+    ninja_path = ninja.__file__
+  expected_stamp_contents = f"{sys.executable}\n{ninja_path}"
+
+  # In order to speed things up on CI and not rebuild everything, we nuke
+  # the CMakeCache.txt file if the path to the Python interpreter changed.
+  # Ideally, CMake would let us reconfigure this dynamically... but it does
+  # not (and gets very confused).
+  # We only do this because the compiler is so expensive to build and very
+  # little of it depends on the Python version. This is a hack.
+  PYTHON_STAMP_FILE = os.path.join(IREE_BINARY_DIR, "python_stamp.txt")
+  if os.path.exists(PYTHON_STAMP_FILE):
+    with open(PYTHON_STAMP_FILE, "rt") as f:
+      actual_stamp_contents = f.read()
+      if actual_stamp_contents == expected_stamp_contents:
+        # All good.
+        return
+
+  # Mismatch or not found. Clean it.
+  cmake_cache_file = os.path.join(IREE_BINARY_DIR, "CMakeCache.txt")
+  if os.path.exists(cmake_cache_file):
+    print("Removing CMakeCache.txt because Python version changed",
+          file=sys.stderr)
+    os.remove(cmake_cache_file)
+
+  # And write.
+  with open(PYTHON_STAMP_FILE, "wt") as f:
+    f.write(expected_stamp_contents)
+
+
+def prepare_installation():
+  version_py_content = generate_version_py()
+  print(f"Generating version.py:\n{version_py_content}", file=sys.stderr)
+
+  if not IS_CONFIGURED:
+    # Build from source tree.
+    subprocess.check_call(["cmake", "--version"])
+    os.makedirs(IREE_BINARY_DIR, exist_ok=True)
+    maybe_nuke_cmake_cache()
+    print(f"CMake build dir: {IREE_BINARY_DIR}", file=sys.stderr)
+    print(f"CMake install dir: {CMAKE_INSTALL_DIR_ABS}", file=sys.stderr)
+    cfg = "Release"
+    cmake_args = [
+        "-GNinja",
+        "--log-level=VERBOSE",
+        "-DIREE_BUILD_PYTHON_BINDINGS=ON",
+        "-DPython3_EXECUTABLE={}".format(sys.executable),
+        "-DCMAKE_BUILD_TYPE={}".format(cfg),
+    ]
+
+    # Enable CUDA if specified.
+    cuda_target_option = os.getenv("IREE_TARGET_BACKEND_CUDA")
+    if cuda_target_option:
+      cmake_args.append(f"-DIREE_TARGET_BACKEND_CUDA={cuda_target_option}")
+
+    # Only do a from-scratch configure if not already configured.
+    cmake_cache_file = os.path.join(IREE_BINARY_DIR, "CMakeCache.txt")
+    if not os.path.exists(cmake_cache_file):
+      print(f"Configuring with: {cmake_args}", file=sys.stderr)
+      subprocess.check_call(["cmake", IREE_SOURCE_DIR] + cmake_args,
+                            cwd=IREE_BINARY_DIR)
+    else:
+      print(f"Not re-configuring (already configured)", file=sys.stderr)
+
+    # Build.
+    subprocess.check_call(
+        ["cmake", "--build", ".", "--target", "iree/compiler/API/python/all"],
+        cwd=IREE_BINARY_DIR)
+    print("Build complete.", file=sys.stderr)
+
+  # Install the directory we care about.
+  install_subdirectory = os.path.join(IREE_BINARY_DIR, "iree", "compiler",
+                                      "API", "python")
+  install_args = [
+      "-DCMAKE_INSTALL_DO_STRIP=ON",
+      f"-DCMAKE_INSTALL_PREFIX={CMAKE_INSTALL_DIR_ABS}",
+      "-P",
+      os.path.join(install_subdirectory, "cmake_install.cmake"),
+  ]
+  print(f"Installing with: {install_args}", file=sys.stderr)
+  subprocess.check_call(["cmake"] + install_args, cwd=install_subdirectory)
+
+  # Write version.py directly into install dir.
+  version_py_file = os.path.join(CMAKE_INSTALL_DIR_ABS, "python_packages",
+                                 "iree-compiler", "iree", "compiler",
+                                 "version.py")
+  os.makedirs(os.path.dirname(version_py_file), exist_ok=True)
+  with open(version_py_file, "wt") as f:
+    f.write(version_py_content)
+
+  print(f"Installation prepared: {CMAKE_INSTALL_DIR_ABS}", file=sys.stderr)
+
+
+class CMakeBuildPy(_build_py):
+
+  def run(self):
+    # It is critical that the target directory contain all built extensions,
+    # or else setuptools will helpfully compile an empty binary for us
+    # (this is the **worst** possible thing it could do). We just copy
+    # everything. What's another hundred megs between friends?
+    target_dir = os.path.abspath(self.build_lib)
+    print(f"Building in target dir: {target_dir}", file=sys.stderr)
+    os.makedirs(target_dir, exist_ok=True)
+    print("Copying install to target.", file=sys.stderr)
+    if os.path.exists(target_dir):
+      shutil.rmtree(target_dir)
+    shutil.copytree(os.path.join(CMAKE_INSTALL_DIR_ABS, "python_packages",
+                                 "iree-compiler"),
+                    target_dir,
+                    symlinks=False)
+    print("Target populated.", file=sys.stderr)
+
+
+class CustomBuild(_build):
+
+  def run(self):
+    self.run_command("build_py")
+    self.run_command("build_ext")
+    self.run_command("build_scripts")
+
+
+class CMakeExtension(Extension):
+
+  def __init__(self, name, sourcedir=""):
+    Extension.__init__(self, name, sources=[])
+    self.sourcedir = os.path.abspath(sourcedir)
+
+
+class NoopBuildExtension(_build_ext):
+
+  def __init__(self, *args, **kwargs):
+    assert False
+
+  def build_extension(self, ext):
+    pass
+
+
+def generate_version_py():
+  return f"""# Auto-generated version info.
+PACKAGE_SUFFIX = "{PACKAGE_SUFFIX}"
+VERSION = "{PACKAGE_VERSION}"
+REVISIONS = {json.dumps(find_git_versions())}
+"""
+
+
+def find_git_versions():
+  revisions = {}
+  try:
+    revisions["IREE"] = subprocess.check_output(
+        ["git", "rev-parse", "HEAD"],
+        cwd=IREE_SOURCE_DIR).decode("utf-8").strip()
+  except subprocess.SubprocessError as e:
+    print(f"ERROR: Could not get IREE revision: {e}", file=sys.stderr)
+  revisions["LLVM_PROJECT"] = find_git_submodule_revision(
+      "third_party/llvm-project")
+  revisions["MLIR_HLO"] = find_git_submodule_revision("third_party/mlir-hlo")
+  return revisions
+
+
+def find_git_submodule_revision(submodule_path):
+  try:
+    data = subprocess.check_output(["git", "ls-tree", "HEAD", submodule_path],
+                                   cwd=IREE_SOURCE_DIR).decode("utf-8").strip()
+    columns = re.split("\\s+", data)
+    return columns[2]
+  except Exception as e:
+    print(
+        f"ERROR: Could not get submodule revision for {submodule_path}"
+        f" ({e})",
+        file=sys.stderr)
+    return ""
+
+
+prepare_installation()
+
+packages = find_namespace_packages(where=os.path.join(CMAKE_INSTALL_DIR_ABS,
+                                                      "python_packages",
+                                                      "iree-compiler"),
+                                   include=[
+                                       "iree.compiler",
+                                       "iree.compiler.*",
+                                   ])
+print(f"Found compiler packages: {packages}")
+
+setup(
+    name=f"iree-compiler{PACKAGE_SUFFIX}",
+    version=f"{PACKAGE_VERSION}",
+    author="IREE Authors",
+    author_email="iree-discuss@googlegroups.com",
+    description="IREE Compiler API",
+    long_description="",
+    license="Apache-2.0",
+    classifiers=[
+        "Development Status :: 3 - Alpha",
+        "License :: OSI Approved :: Apache Software License",
+        "Programming Language :: Python :: 3.7",
+        "Programming Language :: Python :: 3.8",
+        "Programming Language :: Python :: 3.9",
+    ],
+    ext_modules=[
+        CMakeExtension("iree.compiler._mlir_libs._mlir"),
+        CMakeExtension("iree.compiler._mlir_libs._ireeDialects"),
+        CMakeExtension("iree.compiler._mlir_libs._ireecTransforms"),
+        CMakeExtension("iree.compiler._mlir_libs._mlirHlo"),
+        CMakeExtension("iree.compiler._mlir_libs._mlirLinalgPasses"),
+    ],
+    cmdclass={
+        "build": CustomBuild,
+        "built_ext": NoopBuildExtension,
+        "build_py": CMakeBuildPy,
+    },
+    zip_safe=False,
+    package_dir={
+        # Note: Must be relative path, so we line this up with the absolute
+        # path built above. Note that this must exist prior to the call.
+        "": f"{CMAKE_INSTALL_DIR_REL}/python_packages/iree-compiler",
+    },
+    packages=packages,
+    entry_points={
+        "console_scripts": [
+            "iree-compile = iree.compiler.tools.scripts.ireec.__main__:main",
+            # TODO: We have renamed to iree-compile on 2022-03-18. Remove
+            # this alias once no longer needed.
+            "ireec = iree.compiler.tools.scripts.ireec.__main__:main",
+        ],
+    },
+    install_requires=[
+        "numpy",
+        "PyYAML",
+    ],
+)
diff --git a/llvm-external-projects/iree-compiler-api/.gitignore b/llvm-external-projects/iree-compiler-api/.gitignore
deleted file mode 100644
index 381498f..0000000
--- a/llvm-external-projects/iree-compiler-api/.gitignore
+++ /dev/null
@@ -1,4 +0,0 @@
-/build/
-/build_intree/
-/wheels/
-/*.egg-info/
diff --git a/llvm-external-projects/iree-compiler-api/CMakeLists.txt b/llvm-external-projects/iree-compiler-api/CMakeLists.txt
deleted file mode 100644
index 8d475b9..0000000
--- a/llvm-external-projects/iree-compiler-api/CMakeLists.txt
+++ /dev/null
@@ -1,163 +0,0 @@
-# This is a standalone project for the creation of compiler API and tools
-# packages. It is meant to be built directly and aggregates all other projects
-# that make up the IREE compiler input surface area. As a standalone
-# project intended for direct distribution, it hard-codes a number of important
-# CMake settings for producing optimal binaries and packages.
-cmake_minimum_required(VERSION 3.17...3.22)
-
-if(POLICY CMP0068)
-  cmake_policy(SET CMP0068 NEW)
-  set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
-endif()
-
-if(POLICY CMP0075)
-  cmake_policy(SET CMP0075 NEW)
-endif()
-
-if(POLICY CMP0077)
-  cmake_policy(SET CMP0077 NEW)
-endif()
-
-# Honor all visibility presets (please, please, pretty-please).
-# This needs to be set as a default because some IREE dependencies specify
-# a CMake min version of 3.0, which causes them to set it locally to OLD.
-set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
-
-project(IREE_COMPILER_API LANGUAGES C CXX)
-
-if(IREE_COMPILER_API_SUB_PROJECT)
-  # Building as part of a top-level IREE project.
-  message(STATUS "Building iree-compiler-api as part of IREE")
-  set(LLVM_MAIN_SRC_DIR "${IREE_SOURCE_DIR}/third_party/llvm-project/llvm")
-  set(LLVM_MAIN_BINARY_DIR "${IREE_BINARY_DIR}/third_party/llvm-project")
-else()
-  # Standalone build.
-  message(STATUS "Building iree-compiler-api standalone")
-  set(IREE_COMPILER_API_STANDALONE ON)
-  set(IREE_BUILD_TESTS OFF)  # Conflicts with our tests if we are top-level.
-  set(IREE_BUILD_SAMPLES OFF)
-  set(LLVM_EXTERNAL_MLIR_IREE_DIALECTS_SOURCE_DIR "${IREE_COMPILER_API_SOURCE_DIR}/../iree-dialects")
-  set(IREE_SOURCE_DIR "${IREE_COMPILER_API_SOURCE_DIR}/../..")
-  set(IREE_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/iree")
-  set(LLVM_MAIN_SRC_DIR "${IREE_COMPILER_API_SOURCE_DIR}/../../third_party/llvm-project/llvm")
-  set(LLVM_MAIN_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/llvm")
-  set(LLVM_EXTERNAL_MLIR_HLO_SOURCE_DIR "${IREE_COMPILER_API_SOURCE_DIR}/../../third_party/mlir-hlo")
-  option(IREE_BUILD_TORCH_MLIR_SUPPORT "Builds support for compiling torch-mlir programs." ON)
-  if(IREE_BUILD_TORCH_MLIR_SUPPORT)
-    set(LLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR "${IREE_COMPILER_API_SOURCE_DIR}/../../third_party/torch-mlir-dialects")
-  endif()
-  # Resources generated on windows must have valid version numbers.
-  # See set_windows_version_resource_properties.
-  set(LLVM_VERSION_MAJOR 0)
-  set(LLVM_VERSION_MINOR 0)
-  set(LLVM_VERSION_PATCH 0)
-  set(PACKAGE_VERSION 0)
-  enable_testing()
-endif()
-
-message(STATUS "iree-compiler-api Directories:
-     IREE_COMPILER_API_SOURCE_DIR = ${IREE_COMPILER_API_SOURCE_DIR}
-     IREE_COMPILER_API_BINARY_DIR = ${IREE_COMPILER_API_BINARY_DIR}
-     IREE_SOURCE_DIR = ${IREE_SOURCE_DIR}
-     IREE_BINARY_DIR = ${IREE_BINARY_DIR}
-     LLVM_MAIN_SRC_DIR = ${LLVM_MAIN_SRC_DIR}
-     LLVM_MAIN_BINARY_DIR = ${LLVM_MAIN_BINARY_DIR}")
-
-# LLVM dependent project directories.
-set(LLD_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../lld)
-set(LLD_MAIN_BINARY_DIR ${LLVM_MAIN_BINARY_DIR}/tools/lld)
-set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
-set(MLIR_MAIN_BINARY_DIR "${LLVM_MAIN_BINARY_DIR}/tools/mlir")
-
-# Configuration includes.
-include(${IREE_SOURCE_DIR}/build_tools/cmake/iree_cmake_options.cmake)
-include(${IREE_SOURCE_DIR}/build_tools/cmake/iree_external_cmake_options.cmake)
-
-# CMake settings.
-set(BUILD_SHARED_LIBS OFF)
-set(CMAKE_PLATFORM_NO_VERSIONED_SONAME ON)
-set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
-set(CMAKE_C_VISIBILITY_PRESET "hidden")
-set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
-
-# Hack system includes to account for LLVM not doing includes right.
-# TODO: Fix this upstream. Each of these system include hacks is broken in
-# a different way, so there is not an easy local fix. They should be removed
-# one be one until this project builds. Since this is the first time all of this
-# has been mashed together, this is disappointing but not unexpected.
-macro(add_system_include_hack include_dir)
-  if("${include_dir}" STREQUAL "")
-    message(SEND_ERROR "Cannot add empty include dir (will kill tablegen): ${include_dir}")
-  endif()
-  include_directories(SYSTEM "${include_dir}")
-endmacro()
-
-add_system_include_hack(${LLVM_MAIN_SRC_DIR}/include)
-add_system_include_hack(${LLVM_MAIN_BINARY_DIR}/include)
-add_system_include_hack(${LLD_MAIN_SRC_DIR}/include)
-add_system_include_hack(${LLD_MAIN_BINARY_DIR}/include)
-add_system_include_hack(${MLIR_MAIN_SRC_DIR}/include)
-add_system_include_hack(${MLIR_MAIN_BINARY_DIR}/include)
-add_system_include_hack(${IREE_SOURCE_DIR})
-add_system_include_hack(${IREE_BINARY_DIR})
-add_system_include_hack(${LLVM_EXTERNAL_MLIR_IREE_DIALECTS_SOURCE_DIR}/include)
-add_system_include_hack(${LLVM_MAIN_BINARY_DIR}/tools/iree-dialects/include)
-add_system_include_hack(${LLVM_EXTERNAL_MLIR_HLO_SOURCE_DIR}/include)
-add_system_include_hack(${LLVM_MAIN_BINARY_DIR}/tools/mlir-hlo/include)
-if(IREE_BUILD_TORCH_MLIR_SUPPORT)
-  add_system_include_hack(${LLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR}/include)
-  add_system_include_hack(${LLVM_MAIN_BINARY_DIR}/tools/torch-mlir-dialects/include)
-endif()
-add_system_include_hack(${IREE_COMPILER_API_SOURCE_DIR}/include)
-
-function(iree_compiler_target_includes target)
-  target_include_directories(${target} PUBLIC
-    $<BUILD_INTERFACE:${IREE_COMPILER_API_SOURCE_DIR}/include>
-    $<BUILD_INTERFACE:${IREE_COMPILER_API_BINARY_DIR}/include>
-  )
-endfunction()
-
-# Common CMake module paths.
-list(APPEND CMAKE_MODULE_PATH ${MLIR_MAIN_SRC_DIR}/cmake/modules)
-list(APPEND CMAKE_MODULE_PATH ${LLVM_MAIN_SRC_DIR}/cmake/modules)
-
-# Configure Python3 deps so that everyone downstream of here latches to the same
-# thing.
-include(MLIRDetectPythonEnv)
-mlir_configure_python_dev_packages()
-
-# Include IREE and LLVM if building standalone.
-if(IREE_COMPILER_API_STANDALONE)
-  iree_set_compiler_cmake_options()
-  iree_set_llvm_cmake_options()
-  iree_add_llvm_external_project(mlir-iree-dialects MLIR_IREE_DIALECTS ${LLVM_EXTERNAL_MLIR_IREE_DIALECTS_SOURCE_DIR})
-  iree_add_llvm_external_project(mlir-hlo MLIR_HLO ${LLVM_EXTERNAL_MLIR_HLO_SOURCE_DIR})
-  if(IREE_BUILD_TORCH_MLIR_SUPPORT)
-    iree_add_llvm_external_project(torch-mlir-dialects TORCH_MLIR_DIALECTS ${LLVM_EXTERNAL_TORCH_MLIR_DIALECTS_SOURCE_DIR})
-  endif()
-  set(MLIR_ENABLE_BINDINGS_PYTHON ON)
-
-  # Required mlir-hlo settings.
-  # TODO: Consider removing this upstream and just using the main
-  # MLIR_ENABLE_BINDINGS_PYTHON option.
-  set(MHLO_ENABLE_BINDINGS_PYTHON ON)
-
-  # Required IREE settings.
-  set(IREE_BUILD_PYTHON_BINDINGS ON)
-
-  # Include LLVM.
-  message(STATUS "Configuring LLVM from (${LLVM_MAIN_SRC_DIR} into ${LLVM_MAIN_BINARY_DIR})...")
-  add_subdirectory("${LLVM_MAIN_SRC_DIR}" "${LLVM_MAIN_BINARY_DIR}" EXCLUDE_FROM_ALL)
-
-  # Include IREE.
-  message(STATUS "Configuring IREE from (${IREE_SOURCE_DIR} into ${IREE_BINARY_DIR}")
-  add_subdirectory("${IREE_SOURCE_DIR}" "${IREE_BINARY_DIR}" EXCLUDE_FROM_ALL)
-endif()
-
-# Sub-directories.
-# Since building outside of the LLVM build system, setup options for local
-# sources.
-include(HandleLLVMOptions)
-add_subdirectory(lib)
-add_subdirectory(python)
-add_subdirectory(unittests)
diff --git a/llvm-external-projects/iree-compiler-api/build_tools/build_python_wheels.sh b/llvm-external-projects/iree-compiler-api/build_tools/build_python_wheels.sh
deleted file mode 100755
index 65e76d7..0000000
--- a/llvm-external-projects/iree-compiler-api/build_tools/build_python_wheels.sh
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/bin/bash
-# Copyright 2021 The IREE Authors
-#
-# Licensed under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-set -e
-
-if [ -z "$PYTHON" ]; then
-  PYTHON="$(which python)"
-fi
-version="$("$PYTHON" --version)"
-echo "Using python: $PYTHON (version $version)"
-
-repo_root="$(cd $(dirname $0)/.. && pwd)"
-wheelhouse="$repo_root/wheels"
-mkdir -p "$wheelhouse"
-
-echo "---- BUILDING iree-compiler ----"
-if [ -x "$(command -v ccache)" ]; then
-  echo "Using ccache"
-  export CMAKE_C_COMPILER_LAUNCHER=ccache
-  export CMAKE_CXX_COMPILER_LAUNCHER=ccache
-fi
-if [ -x "$(command -v ninja)" ]; then
-  echo "Using ninja"
-  export CMAKE_GENERATOR=Ninja
-fi
-$PYTHON -m pip wheel "${repo_root}" \
-  -w "$wheelhouse" -v
-
-echo "---- INSTALLING iree-compiler ----"
-$PYTHON -m pip install -f "$wheelhouse" --force-reinstall iree-compiler-dev
-
-echo "---- QUICK SMOKE TEST ----"
-$PYTHON $repo_root/build_tools/smoketest.py
diff --git a/llvm-external-projects/iree-compiler-api/lib/CAPI/CMakeLists.txt b/llvm-external-projects/iree-compiler-api/lib/CAPI/CMakeLists.txt
deleted file mode 100644
index e257db0..0000000
--- a/llvm-external-projects/iree-compiler-api/lib/CAPI/CMakeLists.txt
+++ /dev/null
@@ -1,57 +0,0 @@
-# LLD may not have been enabled at the global level (typically because we
-# are not building for a CPU backend). If not, don't add API support for it.
-set(_OPTIONAL_SOURCES)
-set(_OPTIONAL_LINK_LIBS)
-if(TARGET lld)
-  list(APPEND _OPTIONAL_SOURCES Lld.cpp)
-  list(APPEND _OPTIONAL_LINK_LIBS
-    lldCommon
-    lldCOFF
-    lldELF
-    lldMachO
-    lldMinGW
-    lldWasm
-  )
-endif()
-
-add_mlir_public_c_api_library(IREECompilerAPICompilerCAPI
-  Compiler.cpp
-  Tools.cpp
-  ${_OPTIONAL_SOURCES}
-  PARTIAL_SOURCES_INTENDED
-  # TODO: If installing, complains about IREEVM not being in any export set.
-  DISABLE_INSTALL
-  LINK_COMPONENTS
-    Support
-  LINK_LIBS PUBLIC
-    MLIRIR
-    iree::compiler::ConstEval
-    iree::compiler::InputConversion::MHLO::MHLO
-    iree::compiler::InputConversion::TOSA::TOSA
-    iree::compiler::Dialect::VM::IR::IR
-    iree::compiler::Dialect::VM::Target::Bytecode::Bytecode
-    iree::compiler::Pipelines
-
-    # Passes and dialects.
-    iree::tools::init_llvmir_translations
-    iree::tools::init_passes_and_dialects
-
-    # All HAL Targets.
-    iree::tools::init_targets
-
-    # Tools.
-    iree::tools::iree_translate_lib
-
-    ${_OPTIONAL_LINK_LIBS}
-)
-
-if(IREE_BUILD_TORCH_MLIR_SUPPORT)
-  target_compile_definitions(obj.IREECompilerAPICompilerCAPI
-    PUBLIC
-    IREE_HAVE_TORCH_MLIR_DIALECTS
-  )
-endif()
-
-# TODO: Fix upstream so there is a way to know what the actual compile target
-# is (versus prefixing with "obj." which is conditional).
-iree_compiler_target_includes(obj.IREECompilerAPICompilerCAPI)
diff --git a/llvm-external-projects/iree-compiler-api/lib/CMakeLists.txt b/llvm-external-projects/iree-compiler-api/lib/CMakeLists.txt
deleted file mode 100644
index a5bbf9e..0000000
--- a/llvm-external-projects/iree-compiler-api/lib/CMakeLists.txt
+++ /dev/null
@@ -1 +0,0 @@
-add_subdirectory(CAPI)
diff --git a/llvm-external-projects/iree-compiler-api/setup.py b/llvm-external-projects/iree-compiler-api/setup.py
deleted file mode 100644
index bc6f873..0000000
--- a/llvm-external-projects/iree-compiler-api/setup.py
+++ /dev/null
@@ -1,227 +0,0 @@
-# Copyright 2021 The IREE Authors
-#
-# Licensed under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-# Build/install the iree-compiler-backend python package.
-# Note that this includes a relatively large build of LLVM (~2400 C++ files)
-# and can take a considerable amount of time, especially with defaults.
-# To install:
-#   pip install . --use-feature=in-tree-build
-# To build a wheel:
-#   pip wheel . --use-feature=in-tree-build
-#
-# It is recommended to build with Ninja and ccache. To do so, set environment
-# variables by prefixing to above invocations:
-#   CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache
-#
-# On CIs, it is often advantageous to re-use/control the CMake build directory.
-# This can be set with the IREE_COMPILER_API_CMAKE_BUILD_DIR env var.
-import json
-import os
-import platform
-import re
-import shutil
-import subprocess
-import sys
-import sysconfig
-
-from distutils.command.build import build as _build
-from setuptools import find_namespace_packages, setup, Extension
-from setuptools.command.build_ext import build_ext as _build_ext
-from setuptools.command.build_py import build_py as _build_py
-
-# Setup and get version information.
-THIS_DIR = os.path.realpath(os.path.dirname(__file__))
-IREESRC_DIR = os.path.join(THIS_DIR, "..", "..")
-VERSION_INFO_FILE = os.path.join(IREESRC_DIR, "version_info.json")
-
-
-def load_version_info():
-  with open(VERSION_INFO_FILE, "rt") as f:
-    return json.load(f)
-
-
-try:
-  version_info = load_version_info()
-except FileNotFoundError:
-  print("version_info.json not found. Using defaults")
-  version_info = {}
-
-PACKAGE_SUFFIX = version_info.get("package-suffix") or ""
-PACKAGE_VERSION = version_info.get("package-version") or "0.1dev1"
-
-
-class CustomBuild(_build):
-
-  def run(self):
-    self.run_command("build_py")
-    self.run_command("build_ext")
-    self.run_command("build_scripts")
-
-
-class CMakeExtension(Extension):
-
-  def __init__(self, name, sourcedir=""):
-    Extension.__init__(self, name, sources=[])
-    self.sourcedir = os.path.abspath(sourcedir)
-
-
-class CMakeBuildPy(_build_py):
-
-  def run(self):
-    version_py_content = generate_version_py()
-    print(f"Generating version.py:\n{version_py_content}", file=sys.stderr)
-    subprocess.check_call(["cmake", "--version"])
-
-    target_dir = os.path.abspath(self.build_lib)
-    print(f"Building in target dir: {target_dir}", file=sys.stderr)
-    os.makedirs(target_dir, exist_ok=True)
-    cmake_build_dir = os.getenv("IREE_COMPILER_API_CMAKE_BUILD_DIR")
-    if not cmake_build_dir:
-      cmake_build_dir = os.path.join(target_dir, "..", "cmake_build")
-    os.makedirs(cmake_build_dir, exist_ok=True)
-    cmake_build_dir = os.path.abspath(cmake_build_dir)
-    print(f"CMake build dir: {cmake_build_dir}", file=sys.stderr)
-    cmake_install_dir = os.path.abspath(
-        os.path.join(target_dir, "..", "cmake_install"))
-    print(f"CMake install dir: {cmake_install_dir}", file=sys.stderr)
-    src_dir = os.path.abspath(os.path.dirname(__file__))
-    cfg = "Release"
-    cmake_args = [
-        "-GNinja",
-        "--log-level=VERBOSE",
-        "-DCMAKE_INSTALL_PREFIX={}".format(cmake_install_dir),
-        "-DPython3_EXECUTABLE={}".format(sys.executable),
-        "-DPython3_INCLUDE_DIRS={}".format(sysconfig.get_path("include")),
-        "-DIREE_VERSION_INFO={}".format(self.distribution.get_version()),
-        "-DCMAKE_BUILD_TYPE={}".format(cfg),
-    ]
-
-    # Enable CUDA if specified.
-    cuda_target_option = os.getenv("IREE_TARGET_BACKEND_CUDA")
-    if cuda_target_option:
-      cmake_args.append(f"-DIREE_TARGET_BACKEND_CUDA={cuda_target_option}")
-
-    build_args = []
-    if os.path.exists(cmake_install_dir):
-      shutil.rmtree(cmake_install_dir)
-    cmake_cache_file = os.path.join(cmake_build_dir, "CMakeCache.txt")
-    if os.path.exists(cmake_cache_file):
-      os.remove(cmake_cache_file)
-    install_target = "install/strip"
-    if platform.system() == "Windows":
-      install_target = "install"
-    print(f"Configuring with: {cmake_args}", file=sys.stderr)
-    subprocess.check_call(["cmake", src_dir] + cmake_args, cwd=cmake_build_dir)
-    subprocess.check_call(
-        ["cmake", "--build", ".", "--target", install_target] + build_args,
-        cwd=cmake_build_dir)
-    print("Build complete.", file=sys.stderr)
-    if os.path.exists(target_dir):
-      shutil.rmtree(target_dir)
-    print("Copying install to target.", file=sys.stderr)
-    shutil.copytree(os.path.join(cmake_install_dir, "python_package"),
-                    target_dir,
-                    symlinks=False)
-    print("Target populated.", file=sys.stderr)
-
-    # Write version.py.
-    version_py_file = os.path.join(target_dir, "iree", "compiler", "version.py")
-    os.makedirs(os.path.dirname(version_py_file), exist_ok=True)
-    with open(version_py_file, "wt") as f:
-      f.write(version_py_content)
-
-
-class NoopBuildExtension(_build_ext):
-
-  def __init__(self, *args, **kwargs):
-    assert False
-
-  def build_extension(self, ext):
-    pass
-
-
-def generate_version_py():
-  return f"""# Auto-generated version info.
-PACKAGE_SUFFIX = "{PACKAGE_SUFFIX}"
-VERSION = "{PACKAGE_VERSION}"
-REVISIONS = {json.dumps(find_git_versions())}
-"""
-
-
-def find_git_versions():
-  revisions = {}
-  try:
-    revisions["IREE"] = subprocess.check_output(
-        ["git", "rev-parse", "HEAD"], cwd=IREESRC_DIR).decode("utf-8").strip()
-  except subprocess.SubprocessError as e:
-    print(f"ERROR: Could not get IREE revision: {e}", file=sys.stderr)
-  revisions["LLVM_PROJECT"] = find_git_submodule_revision(
-      "third_party/llvm-project")
-  revisions["TENSORFLOW"] = find_git_submodule_revision(
-      "third_party/tensorflow")
-  revisions["MLIR_HLO"] = find_git_submodule_revision("third_party/mlir-hlo")
-  return revisions
-
-
-def find_git_submodule_revision(submodule_path):
-  try:
-    data = subprocess.check_output(["git", "ls-tree", "HEAD", submodule_path],
-                                   cwd=IREESRC_DIR).decode("utf-8").strip()
-    columns = re.split("\\s+", data)
-    return columns[2]
-  except Exception as e:
-    print(
-        f"ERROR: Could not get submodule revision for {submodule_path}"
-        f" ({e})",
-        file=sys.stderr)
-    return ""
-
-
-setup(
-    name=f"iree-compiler{PACKAGE_SUFFIX}",
-    version=f"{PACKAGE_VERSION}",
-    author="IREE Authors",
-    author_email="iree-discuss@googlegroups.com",
-    description="IREE Compiler API",
-    long_description="",
-    license="Apache-2.0",
-    classifiers=[
-        "Development Status :: 3 - Alpha",
-        "License :: OSI Approved :: Apache Software License",
-        "Programming Language :: Python :: 3.7",
-        "Programming Language :: Python :: 3.8",
-        "Programming Language :: Python :: 3.9",
-    ],
-    ext_modules=[
-        CMakeExtension("iree.compiler._mlir_libs._mlir"),
-        CMakeExtension("iree.compiler._mlir_libs._ireeDialects"),
-        CMakeExtension("iree.compiler._mlir_libs._ireecTransforms"),
-        CMakeExtension("iree.compiler._mlir_libs._mlirHlo"),
-        CMakeExtension("iree.compiler._mlir_libs._mlirLinalgPasses"),
-    ],
-    cmdclass={
-        "build": CustomBuild,
-        "built_ext": NoopBuildExtension,
-        "build_py": CMakeBuildPy,
-    },
-    zip_safe=False,
-    packages=find_namespace_packages(include=[
-        "iree.compiler",
-        "iree.compiler.*",
-    ],),
-    entry_points={
-        "console_scripts": [
-            "iree-compile = iree.compiler.tools.scripts.ireec.__main__:main",
-            # TODO: We have renamed to iree-compile on 2022-03-18. Remove
-            # this alias once no longer needed.
-            "ireec = iree.compiler.tools.scripts.ireec.__main__:main",
-        ],
-    },
-    install_requires=[
-        "numpy",
-        "PyYAML",
-    ],
-)
diff --git a/llvm-external-projects/iree-compiler-api/unittests/CMakeLists.txt b/llvm-external-projects/iree-compiler-api/unittests/CMakeLists.txt
deleted file mode 100644
index 31c231a..0000000
--- a/llvm-external-projects/iree-compiler-api/unittests/CMakeLists.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-function(iree_compiler_api_py_test)
-  cmake_parse_arguments(
-    ARG
-    ""
-    "NAME;MAIN"
-    ""
-    ${ARGN}
-  )
-  set(TEST_NAME "iree-compiler-api-${ARG_NAME}")
-  add_test(
-    NAME
-      ${TEST_NAME}
-    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
-    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/${ARG_MAIN}"
-  )
-  set_tests_properties(${TEST_NAME} PROPERTIES
-    ENVIRONMENT PYTHONPATH=${IREE_COMPILER_API_BINARY_DIR}/python_package)
-endfunction()
-
-add_subdirectory(CAPI)
-add_subdirectory(tools)
-add_subdirectory(transforms/ireec)