Merge branch 'master' into iree
diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml
new file mode 100644
index 0000000..0667967
--- /dev/null
+++ b/.github/workflows/build-and-test.yaml
@@ -0,0 +1,118 @@
+name: Build and test
+
+on:
+  push:
+    paths-ignore:
+    - 'scripts/**'
+
+env:
+  EMITC: emitc
+  LLVM: llvm
+
+jobs:
+  build-llvm:
+    name: Build LLVM
+    runs-on: ubuntu-20.04
+    steps:
+    - name: Checkout EmitC
+      uses: actions/checkout@v2
+      with:
+        path: ${{ env.EMITC }}
+        submodules: 'true'
+
+    - name: Get LLVM hash
+      id: get-llvm-hash
+      run: echo "llvm_hash=$(cat ${{ env.EMITC }}/build_tools/llvm_version.txt)" >> $GITHUB_ENV
+      shell: bash
+
+    - name: Cache LLVM
+      id: cache-llvm
+      uses: actions/cache@v2
+      with:
+        path: ${{ env.LLVM }}
+        key: ${{ runner.os }}-llvm-20.04-install-${{ env.llvm_hash }}
+
+    - name: Checkout LLVM
+      if: steps.cache-llvm.outputs.cache-hit != 'true'
+      uses: actions/checkout@v2
+      with:
+        repository: llvm/llvm-project
+        path: ${{ env.LLVM }}
+        ref: ${{ env.llvm_hash }}
+
+    - name: Rebuild and Install LLVM
+      if: steps.cache-llvm.outputs.cache-hit != 'true'
+      run: |
+        mkdir -p ${LLVM}/build
+        mkdir -p ${LLVM}/install
+        cd ${LLVM}/build
+        cmake ../llvm \
+          -DLLVM_INSTALL_UTILS=ON \
+          -DLLVM_ENABLE_LLD=ON \
+          -DLLVM_ENABLE_PROJECTS=mlir \
+          -DLLVM_TARGETS_TO_BUILD="host" \
+          -DLLVM_INCLUDE_TOOLS=ON \
+          -DLLVM_BUILD_TOOLS=OFF \
+          -DLLVM_INCLUDE_TESTS=OFF \
+          -DCMAKE_INSTALL_PREFIX=../install \
+          -DCMAKE_BUILD_TYPE=Release \
+          -DLLVM_ENABLE_ASSERTIONS=On \
+          -DCMAKE_C_COMPILER=clang \
+          -DCMAKE_CXX_COMPILER=clang++
+        cmake --build . --target install -- -j$(nproc)
+
+  build:
+    name: Build and test EmitC
+    needs: build-llvm
+    runs-on: ubuntu-20.04
+    steps:
+    - name: Configure Environment
+      run: echo "$GITHUB_WORKSPACE/${LLVM}/install/bin" >> $GITHUB_PATH
+
+    - name: Checkout EmitC
+      uses: actions/checkout@v2
+      with:
+        path: ${{ env.EMITC }}
+        submodules: 'true'
+
+    - name: Get LLVM hash
+      id: get-llvm-hash
+      run: echo "llvm_hash=$(cat ${{ env.EMITC }}/build_tools/llvm_version.txt)" >> $GITHUB_ENV
+      shell: bash
+
+    - name: Cache LLVM
+      id: cache-llvm
+      uses: actions/cache@v2
+      with:
+        path: ${{ env.LLVM }}
+        key: ${{ runner.os }}-llvm-20.04-install-${{ env.llvm_hash }}
+
+    - name: Build and test EmitC (Debug)
+      run: |
+        mkdir -p ${EMITC}/build_debug
+        cd ${EMITC}/build_debug
+        cmake .. \
+          -DCMAKE_BUILD_TYPE=Debug \
+          -DLLVM_ENABLE_ASSERTIONS=ON \
+          -DMLIR_DIR=$GITHUB_WORKSPACE/${LLVM}/install/lib/cmake/mlir/ \
+          -DLLVM_DIR=$GITHUB_WORKSPACE/${LLVM}/install/lib/cmake/llvm/ \
+          -DCMAKE_LINKER=lld \
+          -DCMAKE_C_COMPILER=clang \
+          -DCMAKE_CXX_COMPILER=clang++ \
+          -DLLVM_EXTERNAL_LIT=`pwd`/../../${LLVM}/build/bin/llvm-lit
+        cmake --build . --target check-emitc -- -j$(nproc)
+
+    - name: Build and test EmitC (Release)
+      run: |
+        mkdir -p ${EMITC}/build_release
+        cd ${EMITC}/build_release
+        cmake .. \
+          -DCMAKE_BUILD_TYPE=Release \
+          -DLLVM_ENABLE_ASSERTIONS=OFF \
+          -DMLIR_DIR=$GITHUB_WORKSPACE/${LLVM}/install/lib/cmake/mlir/ \
+          -DLLVM_DIR=$GITHUB_WORKSPACE/${LLVM}/install/lib/cmake/llvm/ \
+          -DCMAKE_LINKER=lld \
+          -DCMAKE_C_COMPILER=clang \
+          -DCMAKE_CXX_COMPILER=clang++ \
+          -DLLVM_EXTERNAL_LIT=`pwd`/../../${LLVM}/build/bin/llvm-lit
+        cmake --build . --target check-emitc -- -j$(nproc)
diff --git a/.gitignore b/.gitignore
index b118858..ff9864d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 # Build artifacts
-build*/
+build/
+build-*/
 
 # Visual Studio Code files
 .vscode/
diff --git a/build_tools/build_mlir.sh b/build_tools/build_mlir.sh
new file mode 100755
index 0000000..fd008f4
--- /dev/null
+++ b/build_tools/build_mlir.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Drived from/inspired by MLIR NPComp and TensorFlow's MLIR-HLO.
+
+if [[ $# -ne 3 ]] ; then
+  echo "Usage: $0 <path/to/llvm/src> <build_dir> <install_dir>"
+  exit 1
+fi
+
+# LLVM source
+LLVM_SRC_DIR="$1"
+build_dir="$2"
+install_dir="$3"
+
+if ! [ -f "$LLVM_SRC_DIR/llvm/CMakeLists.txt" ]; then
+  echo "Expected the path to LLVM to be set correctly (got '$LLVM_SRC_DIR'): can't find CMakeLists.txt"
+  exit 1
+fi
+echo "Using LLVM source dir: $LLVM_SRC_DIR"
+
+# Setup directories.
+echo "Building MLIR in $build_dir"
+echo "Install MLIR to $install_dir"
+mkdir -p "$build_dir"
+mkdir -p "$install_dir"
+
+echo "Beginning build (commands will echo)"
+set -x
+
+cmake -GNinja \
+  "-H$LLVM_SRC_DIR/llvm" \
+  "-B$build_dir" \
+  -DLLVM_INSTALL_UTILS=ON \
+  -DLLVM_ENABLE_LLD=ON \
+  -DLLVM_ENABLE_PROJECTS=mlir \
+  -DLLVM_TARGETS_TO_BUILD="X86" \
+  -DLLVM_INCLUDE_TOOLS=ON \
+  -DLLVM_BUILD_TOOLS=OFF \
+  -DLLVM_INCLUDE_TESTS=OFF \
+  "-DCMAKE_INSTALL_PREFIX=$install_dir" \
+  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
+  -DLLVM_ENABLE_ASSERTIONS=On
+
+cmake --build "$build_dir" --target install
diff --git a/build_tools/llvm_version.txt b/build_tools/llvm_version.txt
new file mode 100644
index 0000000..094611a
--- /dev/null
+++ b/build_tools/llvm_version.txt
@@ -0,0 +1 @@
+e7021232e66f4a8e21f2bcd77d9841d1fb414245
diff --git a/include/emitc/InitDialect.h b/include/emitc/InitDialect.h
index d72c196..62c82ad 100644
--- a/include/emitc/InitDialect.h
+++ b/include/emitc/InitDialect.h
@@ -24,13 +24,6 @@
   registry.insert<emitc::EmitCDialect>();
 }
 
-// This function should be called before creating any MLIRContext if one expect
-// all the possible dialects to be made available to the context automatically.
-inline void registerEmitCDialect() {
-  static bool initOnce =
-      ([]() { registerEmitCDialect(getGlobalDialectRegistry()); }(), true);
-  (void)initOnce;
-}
 } // namespace mlir
 
 #endif // MLIR_INITDIALECT_H