Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow quantum registers returned from functions during buffer deallocation #1408

Merged
merged 10 commits into from
Jan 8, 2025
Merged
26 changes: 6 additions & 20 deletions .github/workflows/build-wheel-linux-x86_64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,11 @@ jobs:
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
run: |
export PATH=$GITHUB_WORKSPACE/llvm-build/bin:$PATH
cmake -S mlir/llvm-project/llvm -B llvm-build -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_TARGETS_TO_BUILD="host" \
-DLLVM_ENABLE_PROJECTS="mlir" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=OFF \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DPython3_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-DPython3_NumPy_INCLUDE_DIRS=$(python${{ matrix.python_version }} -c "import numpy as np; print(np.get_include())") \
-DCMAKE_CXX_VISIBILITY_PRESET=default \
-DLLVM_ENABLE_LLD=ON

# TODO: when updating LLVM, test to see if mlir/unittests/Bytecode/BytecodeTest.cpp:55 is passing
# and remove filter
# This tests fails on CI/CD not locally.
LIT_FILTER_OUT="Bytecode" cmake --build llvm-build --target check-mlir
PYTHON=$(which python${{ matrix.python_version }}) \
LLVM_BUILD_DIR="llvm-build" \
LLVM_TARGETS="check-mlir" \
ENABLE_ZLIB=FORCE_ON \
make llvm

- name: Save LLVM Build
id: save-llvm-build
Expand Down Expand Up @@ -486,7 +472,7 @@ jobs:
if: github.event.schedule == '35 4 * * 4'
run: |
python${{ matrix.python_version }} -m pip install standalone_plugin_wheel/wheel/*.whl --no-deps

- name: Run Python Pytest Tests
run: |
python${{ matrix.python_version }} -m pytest frontend/test/pytest -n auto
Expand Down
24 changes: 5 additions & 19 deletions .github/workflows/build-wheel-macos-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,11 @@ jobs:
- name: Build LLVM / MLIR
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
run: |
cmake -S mlir/llvm-project/llvm -B llvm-build -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_TARGETS_TO_BUILD="host" \
-DLLVM_ENABLE_PROJECTS="mlir" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_ENABLE_LLD=OFF \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DPython3_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-DPython3_NumPy_INCLUDE_DIRS=$(python${{ matrix.python_version }} -c "import numpy as np; print(np.get_include())") \
-DCMAKE_CXX_VISIBILITY_PRESET=default

# TODO: when updating LLVM, test to see if mlir/unittests/Bytecode/BytecodeTest.cpp:55 is passing
# and remove filter
# This tests fails on CI/CD not locally.
LIT_FILTER_OUT="Bytecode" cmake --build llvm-build --target check-mlir
PYTHON=$(which python${{ matrix.python_version }}) \
LLVM_BUILD_DIR="llvm-build" \
LLVM_TARGETS="check-mlir" \
ENABLE_ZLIB=FORCE_ON \
make llvm

- name: Save LLVM Build
id: save-llvm-build
Expand Down
24 changes: 5 additions & 19 deletions .github/workflows/build-wheel-macos-x86_64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,11 @@ jobs:
- name: Build LLVM / MLIR
if: steps.cache-llvm-build.outputs.cache-hit != 'true'
run: |
cmake -S mlir/llvm-project/llvm -B llvm-build -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_TARGETS_TO_BUILD="host" \
-DLLVM_ENABLE_PROJECTS="mlir" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_ENABLE_LLD=OFF \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DPython3_EXECUTABLE=$(which python${{ matrix.python_version }}) \
-DPython3_NumPy_INCLUDE_DIRS=$(python${{ matrix.python_version }} -c "import numpy as np; print(np.get_include())") \
-DCMAKE_CXX_VISIBILITY_PRESET=default

# TODO: when updating LLVM, test to see if mlir/unittests/Bytecode/BytecodeTest.cpp:55 is passing
# and remove filter
# This tests fails on CI/CD not locally.
LIT_FILTER_OUT="Bytecode" cmake --build llvm-build --target check-mlir
PYTHON=$(which python${{ matrix.python_version }}) \
LLVM_BUILD_DIR="llvm-build" \
LLVM_TARGETS="check-mlir" \
ENABLE_ZLIB=FORCE_ON \
make llvm

- name: Save LLVM Build
id: save-llvm-build
Expand Down
20 changes: 4 additions & 16 deletions .github/workflows/scripts/linux_arm64/rh8/build_llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ export PATH=/opt/_internal/cpython-${PYTHON_VERSION}.${PYTHON_SUBVERSION}/bin:/o
/usr/bin/python3 -m pip install pennylane pybind11 PyYAML cmake ninja

# Build LLVM
cmake -S /catalyst/mlir/llvm-project/llvm -B /catalyst/llvm-build -G Ninja \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_BUILD_EXAMPLES=OFF \
-DLLVM_TARGETS_TO_BUILD="host" \
-DLLVM_ENABLE_PROJECTS="mlir" \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_INSTALL_UTILS=ON \
-DLLVM_ENABLE_ZLIB=FORCE_ON \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_ENABLE_LLD=ON \
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
-DPython3_EXECUTABLE=/usr/bin/python3 \
-DPython3_NumPy_INCLUDE_DIRS=/opt/_internal/cpython-${PYTHON_VERSION}.${PYTHON_SUBVERSION}/lib/python${PYTHON_VERSION}/site-packages/numpy/core/include \
-DCMAKE_CXX_VISIBILITY_PRESET=default

LIT_FILTER_OUT="Bytecode|tosa-to-tensor" cmake --build /catalyst/llvm-build --target check-mlir llvm-symbolizer
export LLVM_BUILD_DIR="/catalyst/llvm-build"
export LLVM_TARGETS="check-mlir"
export ENABLE_ZLIB=FORCE_ON
make llvm
17 changes: 13 additions & 4 deletions mlir/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ ENZYME_BUILD_DIR?=$(MK_DIR)/Enzyme/build
RT_BUILD_DIR?=$(MK_DIR)/../runtime/build
ENABLE_ASAN?=OFF
BUILD_TYPE?=Release
TARGET_FILE=$(MK_DIR)/mlir-hlo/mhlo/transforms/CMakeLists.txt
PATCH_FILE=$(MK_DIR)/patches/mhlo-Add-PassesIncGen-in-transforms-CMakeList.patch
LLVM_EXTERNAL_LIT ?= $(LLVM_BUILD_DIR)/bin/llvm-lit

ifeq ($(shell uname), Darwin)
Expand All @@ -36,6 +34,8 @@ USE_SANITIZER_NAMES=""
USE_SANITIZER_FLAGS=""
endif

LLVM_TARGETS ?= check-mlir llvm-symbolizer

.PHONY: help
help:
@echo "Please use \`make <target>' where <target> is one of"
Expand All @@ -53,8 +53,15 @@ help:
all: llvm mhlo enzyme dialects plugin

.PHONY: llvm
llvm: TARGET_FILE := $(MK_DIR)/llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
llvm: PATCH_FILE := $(MK_DIR)/patches/mlir-buffer-deallocation.patch
llvm:
@echo "build LLVM and MLIR enabling Python bindings"
# Patch in MLIR buffer deallocation bugfix
# TODO: remove once https://github.com/llvm/llvm-project/pull/121582 is merged & the dep updated
@if patch --dry-run -p1 -N $(TARGET_FILE) $(PATCH_FILE) > /dev/null 2>&1; then \
patch -p1 $(TARGET_FILE) $(PATCH_FILE); \
fi
cmake -G Ninja -S llvm-project/llvm -B $(LLVM_BUILD_DIR) \
-DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-DLLVM_BUILD_EXAMPLES=OFF \
Expand All @@ -75,10 +82,12 @@ llvm:
-DCMAKE_CXX_VISIBILITY_PRESET=$(SYMBOL_VISIBILITY)

# TODO: when updating LLVM, test to see if mlir/unittests/Bytecode/BytecodeTest.cpp:55 is passing
# and remove filter
LIT_FILTER_OUT="Bytecode" cmake --build $(LLVM_BUILD_DIR) --target check-mlir llvm-symbolizer
# and remove filter. This tests fails on CI/CD not locally.
erick-xanadu marked this conversation as resolved.
Show resolved Hide resolved
LIT_FILTER_OUT="Bytecode|tosa-to-tensor" cmake --build $(LLVM_BUILD_DIR) --target $(LLVM_TARGETS)
erick-xanadu marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: mhlo
mhlo: TARGET_FILE := $(MK_DIR)/mlir-hlo/mhlo/transforms/CMakeLists.txt
mhlo: PATCH_FILE := $(MK_DIR)/patches/mhlo-Add-PassesIncGen-in-transforms-CMakeList.patch
mhlo:
@echo "build MLIR-HLO"
# Patch MHLO cmake dependency
Expand Down
14 changes: 14 additions & 0 deletions mlir/patches/mlir-buffer-deallocation.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/mlir/llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp b/mlir/llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
index a0a81d4add..7b7be9e577 100644
--- a/mlir/llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
+++ b/mlir/llvm-project/mlir/lib/Dialect/Bufferization/Transforms/BufferDeallocation.cpp
@@ -308,6 +308,9 @@ private:

// Add new allocs and additional clone operations.
for (Value value : valuesToFree) {
+ if (!isa<BaseMemRefType>(value.getType())) {
+ continue;
+ }
if (failed(isa<BlockArgument>(value)
? introduceBlockArgCopy(cast<BlockArgument>(value))
: introduceValueCopyForRegionResult(value)))
Loading