Full Code of sjtu-zhao-lab/pom for AI

main 332cf8b63e5a cached
53 files
532.8 KB
132.0k tokens
102 symbols
1 requests
Download .txt
Showing preview only (554K chars total). Download the full file or copy to clipboard to get everything.
Repository: sjtu-zhao-lab/pom
Branch: main
Commit: 332cf8b63e5a
Files: 53
Total size: 532.8 KB

Directory structure:
gitextract_f9e36swx/

├── .gitattributes
├── .gitignore
├── .gitmodules
├── CMakeLists.txt
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── ae_script.sh
├── build-pom.sh
├── clean.sh
├── include/
│   ├── CMakeLists.txt
│   └── polyhedral/
│       ├── codegen.h
│       ├── compute.h
│       ├── core.h
│       ├── debug.h
│       ├── expr.h
│       ├── function.h
│       ├── generator.h
│       ├── generator_isl.h
│       ├── placeholder.h
│       └── type.h
├── lib/
│   ├── CMakeLists.txt
│   └── polyhedral/
│       ├── CMakeLists.txt
│       ├── codegen.cpp
│       ├── compute.cpp
│       ├── core.cpp
│       ├── debug.cpp
│       ├── expr.cpp
│       ├── function.cpp
│       ├── generator.cpp
│       ├── generator_isl.cpp
│       ├── placeholer.cpp
│       └── test.cpp
├── results-gen.sh
├── run-code.sh
├── samples/
│   └── config.json
├── tcl-gen.sh
├── testbench/
│   ├── 2mm.cpp
│   ├── 3mm.cpp
│   ├── bicg.cpp
│   ├── blur.cpp
│   ├── edgeDetect.cpp
│   ├── gaussian.cpp
│   ├── gemm.cpp
│   ├── gesummv.cpp
│   ├── heat.cpp
│   ├── jacobi.cpp
│   ├── jacobi2d.cpp
│   ├── resnet18.cpp
│   ├── seidel.cpp
│   └── vgg16.cpp
└── vitis-reports.sh

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitattributes
================================================
# Auto detect text files and perform LF normalization
* text=auto


================================================
FILE: .gitignore
================================================
build
# balance/
# sample_l/
# samles_4_paper/
# samles_update/
build.txt
.vscode/
.env
scalehls/
**/tmp
**/cpp_src
**/mlir_src
**/hls_proj
**/dump_csv
**/.ipynb_checkpoints
**/__pycache__

*.onnx*
*.tmp
*.gv
*.png
*.log
*.csv
*.zip
*.swp
*.swo
*.swn


================================================
FILE: .gitmodules
================================================
[submodule "scalehls"]
	path = scalehls
	url = git@github.com:Jason048/scalehls.git
	branch = scalehls-pom


================================================
FILE: CMakeLists.txt
================================================
cmake_minimum_required(VERSION 3.13.4)

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()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_definitions(-w)
set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/build)
set(TEST_PATH ${PROJECT_SOURCE_DIR}/test)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)


if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  project(POM LANGUAGES CXX C)
  # llvm
  set(LLVM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/llvm)
  find_package(MLIR REQUIRED CONFIG)
  message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
  message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
  message(STATUS "Using LLVM_EXTERNAL_LIT.cmake in: ${LLVM_EXTERNAL_LIT}")

  list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
  list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")

  include(TableGen)
  include(AddLLVM)
  include(AddMLIR)
  include(HandleLLVMOptions)
else ()
  set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
  set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
  set(MLIR_INCLUDE_DIRS ${MLIR_MAIN_SRC_DIR}/include)
  set(MLIR_CMAKE_DIR ${MLIR_MAIN_SRC_DIR}/cmake/modules)
  set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
  set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
  include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
  include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})
  message(STATUS "Using LLVM_SOURCE_DIR in: ${LLVM_SOURCE_DIR}")
  message(STATUS "Using MLIR_MAIN_SRC_DIR in: ${MLIR_MAIN_SRC_DIR}")
endif()

# include_directories("/home/POM/third_party/llvm-project/mlir/include/mlir/IR")
set(POM_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(POM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(POM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include )
set(POM_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin)


list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include)
link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})



# ISL
#set(ISL_INCLUDE_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/isl/build/include/ CACHE PATH #"Path to ISL include directory")
#set(ISL_LIB_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/isl/build/.lib/ CACHE PATH "Path to #ISL library directory")

include_directories(${PROJECT_SOURCE_DIR}/include/polyhedral)
include_directories(${PROJECT_SOURCE_DIR}/lib/polyhedral)
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/testbench)
include_directories(${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/mlir/include)
include_directories(${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/llvm/include)
include_directories(${PROJECT_SOURCE_DIR}/scalehls/build/tools/scalehls/include)
include_directories($ISL_INCLUDE_DIRECTORY)
include_directories(${PROJECT_SOURCE_DIR}/scalehls/include)
#include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
include_directories(/usr/include/python3.8)


find_library(ISLLib isl PATHS /usr/local/lib NO_DEFAULT_PATH)
message(STATUS "Using ISLlib in: ${ISLLib}")



get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)


# find_library(MLIRSCF NAMES libMLIRSCF.a PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)
# message(STATUS "Using MLIRSCF in: ${MLIRSCF}")

find_library(MLIRHLS MLIRHLS PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)
message(STATUS "Using MLIRHLS in: ${MLIRHLS}")

find_library(MLIRScaleHLSSupport MLIRScaleHLSSupport PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)
message(STATUS "Using MLIRScaleHLSSupport in: ${MLIRScaleHLSSupport}")

find_library(MLIRScaleHLSTransforms MLIRScaleHLSTransforms PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)
message(STATUS "Using MLIRScaleHLSSupport in: ${MLIRScaleHLSTransforms}")


set(TEST_SOURCE_DIR ${PROJECT_SOURCE_DIR}/testbench)
file(GLOB_RECURSE  mains RELATIVE 
"${TEST_SOURCE_DIR}" 
"${TEST_SOURCE_DIR}/*.cpp"
)
message(STATUS "Using TEST_SOURCE_DIR in:${TEST_SOURCE_DIR}")

foreach(mainfile IN LISTS mains)
    # Get file name without directory
    get_filename_component(mainname ${mainfile} NAME_WE)
    # message(STATUS "get mainname in:${mainname}")
    add_executable(${mainname} ${TEST_SOURCE_DIR}/${mainfile})

    target_link_libraries(${mainname} Functions)
    target_link_libraries(${mainname} ${ISLLib})
    target_link_libraries(${mainname}
        ${dialect_libs}
        ${conversion_libs}
        ${translation_libs}
        ${MLIRScaleHLSTransforms}
        ${MLIRScaleHLSSupport}
        ${MLIRHLS}
        MLIRAffineTransforms
        MLIROptLib
        MLIRAnalysis
        MLIRCallInterfaces
        MLIRCastInterfaces
        MLIRIR
        MLIRParser
        MLIRPass
        MLIRSideEffectInterfaces
        MLIRSupport
        MLIRTransforms)
endforeach()

# add_executable(test test.cpp)
# add_executable(bicg bicg.cpp)
# target_link_libraries(test Functions)
# target_link_libraries(test ${ISLLib})
# target_link_libraries(test
#     ${dialect_libs}
#     ${conversion_libs}
#     ${translation_libs}
#     ${MLIRScaleHLSTransforms}
#     ${MLIRScaleHLSSupport}
#     ${MLIRHLSCpp}
#     MLIRAffineTransforms
#     MLIROptLib
#     MLIRAnalysis
#     MLIRCallInterfaces
#     MLIRCastInterfaces
#     MLIRIR
#     MLIRParser
#     MLIRPass
#     MLIRSideEffectInterfaces
#     MLIRSupport
#     MLIRTransforms)



add_subdirectory(include)
add_subdirectory(lib)
#add_subdirectory(test)
#add_subdirectory(standalone-opt)
#add_subdirectory(standalone-translate)
#add_subdirectory(POM-isl)
# add_subdirectory(pybind11)


#find_package(pybind11 REQUIRED)
#pybind11_add_module(wrapper wrapper.cpp)
#target_link_libraries(wrapper PRIVATE ${ISLLib})

# pybind11_add_module(core ${PROJECT_SOURCE_DIR}/lib/Polyhedral/core.cpp)
# pybind11_add_module(place ${PROJECT_SOURCE_DIR}/lib/Polyhedral/placeholer.cpp)
# pybind11_add_module(example2 ${PROJECT_SOURCE_DIR}/lib/Polyhedral/example2.cpp)


# add_library(core MODULE 
#            ${PROJECT_SOURCE_DIR}/lib/Polyhedral/core.cpp
#     )
# target_link_libraries(core 
#             pybind11::module
#   )



================================================
FILE: Dockerfile
================================================
# This Dockerfile configures a Docker environment that 
# contains all the required packages for the tool
FROM ubuntu:20.04
ARG UID
ARG GID
ARG VHLS_PATH
RUN echo "Group ID: $GID"
RUN echo "User ID: $UID"

USER root
RUN apt-get update -y && apt-get install apt-utils -y
RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata

# Install basic packages 
RUN apt-get upgrade -y 
RUN apt-get update -y \
    && apt-get install -y clang lld cmake libssl-dev\
                          pkg-config g++\
                          llvm gcc ninja-build \
                          build-essential autoconf libtool\
                          git vim wget sudo

CMD ["bash"]

# Add dev-user
# RUN groupadd -o -g $GID dev-user
# RUN useradd -r -g $GID -u $UID -m -d /home/dev-user -s /sbin/nologin -c "User" dev-user
# RUN echo "dev-user     ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
# USER dev-user

# Install PyTorch and Torch-MLIR
ENV PATH="${PATH}:~/.local/bin"
# RUN pip3 install --user --upgrade pip \
#     && pip3 install pandas dataclasses colorlog pyyaml


# Add environment variables
ENV vhls $VHLS_PATH
RUN printf "\
\nexport LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:\$LIBRARY_PATH \
\n# Vitis HLS setup \
\nsource ${vhls}/Vitis/2022.2/settings64.sh \
\nsource ${vhls}/Vitis_HLS/2022.2/settings64.sh \
\nexport PATH=$PATH:/workspace/build/bin:/workspace/scalehls/polygeist/llvm/build/bin:/workspace/scalehls/polygeist/build/bin:~/.local/bin \
\n" >> ~/.vimrc
#Add vim environment
RUN printf "\
\nset autoread \
\nautocmd BufWritePost *.cpp silent! !clang-format -i <afile> \
\nautocmd BufWritePost *.c   silent! !clang-format -i <afile> \
\nautocmd BufWritePost *.h   silent! !clang-format -i <afile> \
\nautocmd BufWritePost *.hpp silent! !clang-format -i <afile> \
\nautocmd BufWritePost *.cc  silent! !clang-format -i <afile> \
\nautocmd BufWritePost *.py  silent! !python3 -m black <afile> \
\nautocmd BufWritePost *.sv  silent! !verible-verilog-format --inplace <afile> \
\nautocmd BufWritePost *.v  silent! !verible-verilog-format --inplace <afile> \
\nautocmd BufWritePost * redraw! \
\n" >> ~/.vimrc

# Entrypoint set up
WORKDIR /home/workspace 
# COPY . /usr/src/workspace


================================================
FILE: LICENSE
================================================
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   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.


================================================
FILE: Makefile
================================================
user=$(if $(shell id -u),$(shell id -u),9001)
group=$(if $(shell id -g),$(shell id -g),1000)
# phism=/workspace
vhls=/data/Vivado

# docker buildx pruney

# Build docker container
build-docker: 
	(docker build --build-arg VHLS_PATH=$(vhls) . --tag pom_dev)

# Enter docker container
shell: build-docker
	docker run -it -v $(shell pwd):/home/workspace -v $(vhls):$(vhls) --name pom_dev pom_dev:latest /bin/bash 

# docker exec -it -v /home/wczhang/Xilinx:/home/wczhang/Xilinx flowgnn /bin/bash 




================================================
FILE: README.md
================================================

# POM: An Optimizing Framework on MLIR for Efficient FPGA-based Accelerator Generation
![GitHub License](https://img.shields.io/github/license/sjtu-zhao-lab/pom) ![GitHub Repo stars](https://img.shields.io/github/stars/sjtu-zhao-lab/pom)
## 1. Introduction


POM is an end-to-end optimizing framework on MLIR for efficient FPGA-based accelerator generation.  POM has the following technical contributions:  
- **Programmability**: POM provides a decoupled DSL that enables concise descriptions of functions, loops, and arrays. A rich collection of scheduling primitives is provided for flexible customization, leading to much fewer lines of code while maintaining high performance.
- **Extensibility**: POM explicitly introduces three layers of IR to perform operations at suitable abstraction levels in a unified framework, streamlining the implementation and debugging process and reducing the effort of supporting various optimization methods.
- **Quality**: POM provides a rich set of optimization methods and performs FPGA-oriented schedule operations at proper levels, relieving tight loop-carried dependence, exploiting parallelism, and improving overall performance.
- **Automation**: POM contains a design space exploration (DSE) engine to search for high-performance schedule schemes automatically and efficiently, while also allowing designers to set user-specified schedules.

Please refer to our [HPCA' 24 ](https://arxiv.org/abs/2401.05154)paper for more details: 
```
@inproceedings{zhanghpca2024pom,
  title={An Optimizing Framework on MLIR for Efficient FPGA-based Accelerator Generation},
  author={Weichuang Zhang and Jieru Zhao and Guan Shen and Quan Chen and Chen Chen and Minyi Guo},
  booktitle={2024 IEEE International Symposium on High-Performance Computer Architecture (HPCA)},
  year={2024}
}
```
***
## 2. Installation
### 2.1 Install Prerequisite: isl 
```
git clone git://repo.or.cz/isl.git  
cd isl 
git pull  
git submodule init  
git submodule update  
./autogen.sh  
./configure --with-int=imath  
make  
make check  
make install  
```
More details of isl installation: https://compsys-tools.ens-lyon.fr/iscc/isl.pdf

### 2.2 Install POM
```
git clone --recursive git@github.com:sjtu-zhao-lab/pom.git 
cd pom
```

### 2.3 Code structure
```
pom/
├── scalehls/
│    ├── polygeist /
│    │    ├──  llvm-project/ 
```

## 3. Build 
### 3.1 Build scalehls

```
# Go to scalehls/  
./build-scalehls.sh
```
### 3.2 Build POM

```
# Go to pom/  
./build-pom.sh
```
***
## 4. Getting Started with a GEMM kernel

```
# Go to pom/build/
cmake --build . --target gemm
```

You can run the following instruction to generate an optimized MLIR affine dialect:
```
./bin/gemm
```
The optimized IR is stored at pom/samples/gemm/test_gemm_4096.mlir .  
You can further translate the optimized IR into HLS C code with the following instruction:

```
../scalehls/build/bin/scalehls-opt ../samples/gemm/test_gemm_4096.mlir\
    --scalehls-func-preprocess="top-func=gemm" \
    --scalehls-qor-estimation="target-spec=../samples/config.json" \
    | ../scalehls/build/bin/scalehls-translate -emit-hlscpp >  ../samples/gemm/test_gemm_4096.cpp
```
## Repository Layout
- `include` and `lib` : Compiler implementation
- `scalehls` : the HLS C code generation
- `testbench`: Kernels and applications described with POM DSL
- `samples`: The generated designs

## Related Projects
- [ScaleHLS](https://github.com/hanchenye/scalehls)
- [Tiramisu](https://github.com/Tiramisu-Compiler/tiramisu)
- [MLIR](https://mlir.llvm.org/)


================================================
FILE: ae_script.sh
================================================

start_time=$(date +"%s")
echo ""
echo ">>> Start the experiment workflow"
echo ""
./build-pom.sh


./run-code.sh


./tcl-gen.sh


./vitis-reports.sh


./results-gen.sh

end_time=$(date +"%s")
execution_time=$(($end_time - $start_time))
echo ""
echo ">>> All Steps have been finished!"
echo ">>> Total Execution Time: $execution_time seconds"
echo ""


================================================
FILE: build-pom.sh
================================================
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

# The absolute path to the directory of this script.
POM_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# POM_DIR="$(dirname "$CURRENT_DIR")"

start_time=$(date +"%s")
echo ""
echo ">>> Step 1. Building POM ..."
echo ""

# Got to the build directory.
cd "${POM_DIR}"
mkdir -p build
cd build

if [ ! -f "CMakeCache.txt" ]; then
    LLVM_DIR="${POM_DIR}/scalehls/build/lib/cmake/llvm" \
    MLIR_DIR="${POM_DIR}/scalehls/build/lib/cmake/mlir" \
    cmake -G Ninja .. 
    # -DMLIR_DIR="${POM_DIR}/scalehls/build/lib/cmake/mlir" \
    # -DLLVM_EXTERNAL_LIT="${POM_DIR}/scalehls/build/bin/llvm-lit" 
fi

cd ../
# Run building.
# targets=("edgeDetect" "gaussian" "blur" "vgg16"  "resnet" "jacobi" "jacobi2d" "heat" "seidel")


echo ""
echo ">>> Step 2. Initializing samples/{testbench}"
echo ""
folders=("gemm" "bicg" "gesummv" "2mm" "3mm" "edgeDetect" "gaussian" "blur" "vgg16"  "resnet18" "jacobi" "jacobi2d" "heat" "seidel")

for folder in "${folders[@]}"
do
    mkdir -p samples/"${folder}"
done


# for target in "${targets[@]}"
# do
#     cmake --build . --target "$target"
# done
# end_time=$(date +"%s")
# execution_time=$(($end_time - $start_time))
echo ""
echo ">>> Building finished!"
echo ""


================================================
FILE: clean.sh
================================================
#!/bin/bash


folders=("gemm" "bicg" "gesummv" "2mm" "3mm" "edgeDetect" "gaussian" "blur" "vgg16"  "resnet" "jacobi" "jacobi2d" "heat" "seidel")

for folder in "${folders[@]}"
do
    rm -rf "samples/${folder}"/*
done



================================================
FILE: include/CMakeLists.txt
================================================
# add_subdirectory(Standalone)
# add_subdirectory(Dialect)
# add_subdirectory(Polyhedral)


================================================
FILE: include/polyhedral/codegen.h
================================================
#ifndef _H_polyfp_CODEGEN_
#define _H_polyfp_CODEGEN_

#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/space.h>
#include <isl/constraint.h>

#include <map>
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <unordered_set>
#include <sstream>

// #include "debug.h"
// #include "expr.h"
// #include "type.h"
#include "function.h"
#include "compute.h"

namespace polyfp{

class var;
std::string generate_new_variable_name();
polyfp::expr traverse_expr_and_replace_non_affine_accesses(polyfp::compute *comp,
                                                             const polyfp::expr &exp);

}



#endif

================================================
FILE: include/polyhedral/compute.h
================================================
#ifndef _H_polyfp_COMPUTE_
#define _H_polyfp_COMPUTE_

#include <isl/ctx.h>
#include <isl/aff.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/id.h>
#include <isl/constraint.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/space.h>
#include <isl/constraint.h>
#include <unordered_map>
#include<algorithm>
#include <isl/val.h>
#include <map>
#include "expr.h"
#include "placeholder.h"
#include "debug.h"
namespace polyfp{
std::string generate_new_computation_name();
class var;
class scheduler;
class function;
class placeholder;
class compute
{
  friend function;
  friend placeholder;
private:

    // The data access map
    isl_map *access;

    // The isl context of the function.
    isl_ctx *ctx;

    // The placeholder that stores the results
    polyfp::placeholder *plhd;
    polyfp::expr plhd_expr;

    polyfp::primitive_t data_type;


    // the expression (or statement) of the function
    polyfp::expr expression;

    polyfp::function *fct;

    /**
     * TODO: 
     */
    std::map<std::string, std::string > access_map;


    // The iteration domain of the compute(nested loop)
    isl_set *iteration_domain;


    // The name of the compute(nested loop)
    std::string name;

    // The number of dimensions in the original definition of the compute
    int number_of_dims;

    /**
     * TODO: Add predicates to the nested loops
     * Derived from Tiramisu:
     * A predicate around the compute. The compute is executed
     * only if this predicate is true. This is useful to insert a non-affine
     * condition around the compute.
     */
    polyfp::expr predicate;


    // The schedule of the compute.
    isl_map * schedule;

    /**
      * Derived from Tiramisu:
      * Time-processor domain of the compute.
      * In this representation, the logical time of execution and the
      * processor where the compute will be executed are both
      * specified.
      */
    isl_set *time_processor_domain;

    
    // The iteration variables(iterators) of the compute
    std::vector<polyfp::var> iteration_variables;

    /**
     * TODO: 
     */
    std::vector<polyfp::expr> placeholder_dims;
    std::vector<polyfp::expr > placeholder_accessmap;
    
    /**
    * TODO: add predicate
    * \p predicate is an expression that represents constraints on the iteration domain
    * (for example (i != j). The predicate has to be an affine
    * expression.
    */
    std::string construct_iteration_domain(std::string name, std::vector<var> iterator_variables, 
                                          polyfp::expr predicate);

    // Return the names of iteration domain dimensions.
    std::vector<std::string> get_iteration_domain_dimension_names();

    void check_dimensions_validity(std::vector<int> dimensions);

    // Get the number of dimensions of the compute
    int get_iteration_domain_dimensions_number();

    // Check that the names used in \p dimensions are not already in use.
    void assert_names_not_assigned(std::vector<std::string> dimensions);



    /**
      * Generate an identity schedule for the compute
      * Derived from Tiramisu:
      * This identity schedule is an identity relation created from the iteration domain.
      */
    isl_map *gen_identity_schedule_for_iteration_domain();

    /**
      * Generate an identity schedule for the compute.
      * Derived from Tiramisu:
      * This identity schedule is an identity relation created from the time-processor domain.
      */
    isl_map *gen_identity_schedule_for_time_space_domain();

    // Assign a name to iteration domain dimensions that do not have a name.
    void name_unnamed_iteration_domain_dimensions();

    // Assign a name to iteration domain dimensions that do not have a name.
    void name_unnamed_time_space_dimensions();


    /**
      * Set an identity schedule for the compute.
      * Derived from Tiramisu:
      * This identity schedule is an identity relation created from the iteration domain.
      */
    void set_identity_schedule_based_on_iteration_domain();

    // Set the iteration domain of the compute
    void set_iteration_domain(isl_set *domain);

    // Set the names of loop levels dimensions.
    void set_loop_level_names(std::vector<int> loop_levels, std::vector<std::string> names);
    void set_loop_level_names(std::vector<std::string> names);

    // Set the names of the dimensions of the schedule domain.
    void set_schedule_domain_dim_names(std::vector<int> loop_levels, std::vector<std::string> names);

    // Return the function where the compute is declared.
    polyfp::function *get_function() const;


    /**
      * Derived from Tiramisu:
      * Search the time-space domain (the range of the schedule) and
      * return the loop level numbers that correspond to the dimensions
      * named \p dim.
      */
    std::vector<int> get_loop_level_numbers_from_dimension_names(std::vector<std::string> dim_names);


    // Intersect set with the context of the compute.
    isl_set *intersect_set_with_context(isl_set *set);

    /**
      * Derived from Tiramisu:
      * Return the time-processor domain of the compute.
      * In this representation, the logical time of execution and the
      * processor where the compute will be executed are both specified.
      */
    isl_set *get_time_processor_domain() const;

    /**
      * Derived from Tiramisu:
      * Return the trimmed time-processor domain.
      * TODO: The first dimension of the time-processor domain is used
      * to indicate redundancy of the compute. In POM there is no redundancy 
      * of the compute. This feature will be removed soon.
      * The trimmed time-processor domain is the time-processor domain
      * without the dimension that represents the redundancy. We simply
      * take the time-processor domain and remove the first dimension.
      */
    isl_set *get_trimmed_time_processor_domain();

    /**
      * Derived from Tiramisu:
      * Update loop level names. This function should be called after each scheduling operation
      * because scheduling usually changes the original loop level names.
      * This function erases \p nb_loop_levels_to_erase loop level names starting from the
      * loop level \p start_erasing. It then inserts the loop level names \p new_names in
      * \p start_erasing. In other words, it replaces the names of loop levels from
      * \p start_erasing to \p start_erasing + \p nb_loop_levels_to_erase with the loop levels
      * indicated by \p new_names.  This function sets the non erased loop levels to be equal to the
      * original loop level names.
      *
      * \p original_loop_level_names : a vector containing the original loop level names (loop level
      * names before scheduling).
      *
      * \p new_names : the new loop level names.
      *
      * \p start_erasing : start erasing loop levels from this loop level.
      *
      * \p nb_loop_levels_to_erase : number of loop levels to erase.
      *
      * Example. Assuming the original loop levels are {i0, i1, i2, i3}
      *
      * Calling this->update_names({i0, i1, i2, i3}, {x0, x1}, 1, 2) updates the loop levels to become
      * {i0, x0, x1, i3}.
      */
    void update_names(std::vector<std::string> original_loop_level_names, std::vector<std::string> new_names,
                      int start_erasing, int nb_loop_levels_to_erase);

protected:

    isl_ctx *get_ctx() const;

    polyfp::expr get_predicate();

    /**
      * Return a unique name of compute; made of the following pattern:
      * [compute name]@[compute address in memory]
      */
    const std::string get_unique_name() const;

    // Set the name of the compute.
    void set_name(const std::string &n);

    void init_computation(std::string iteration_space_str,
                          polyfp::function *fct,
                          const polyfp::expr &e,
                          polyfp::primitive_t t, expr p);


    void set_schedule(isl_map *map);
    void set_schedule(std::string map_str);

    compute(std::string name,std::vector<var> iterator_variables, polyfp::expr e, primitive_t t, expr p);


public:
    compute();
    compute(std::string iteration_domain, polyfp::expr e,
                polyfp::primitive_t t,
                polyfp::function *fct, expr p);

    
    int II;
    bool is_unrolled;
    long latency;
    long best_latency = LLONG_MAX;
    int dsp;
    int minII;

    std::vector<polyfp::var> get_iteration_variables();

    isl_map * original_schedule;
    std::map<std::string, std::string > tile_map;
    std::map<std::string, int > tile_size_map;
    std::map<std::string, std::string > directive_map;
    std::map<std::string, std::string > directive_tool_map;
    std::vector<std::string> original_loop_level_name;
    std::vector<std::string> final_loop_level_names;
    std::vector<std::string> final_loop_level_names_reserved;
    std::vector<int> unroll_factor;
    std::vector<polyfp::expr> unroll_dimension;

    bool refused = false;
    std::map<std::string, std::string > temp_access_map;


    isl_map * best_schedule;
    std::map<std::string, std::string > best_tile_map;
    std::map<std::string, int > best_tile_size_map;
    std::map<std::string, std::string > best_directive_map;
    std::map<std::string, std::string > best_directive_tool_map;
    std::vector<std::string> best_loop_level_names;
    std::vector<int> best_unroll_factor;
    std::vector<polyfp::expr> best_unroll_dimension;

    std::map<std::string, int>iterators_location_map;
    int after_level;
    int ori_after_level;

    compute(std::string name, std::vector<var> iterator_variables, polyfp::expr e, expr p);
    compute(std::string name, std::vector<var> iterator_variables, int a, expr p);
    isl_map *get_access_relation() const;


    bool is_tiled = false ;
    bool is_skewed = false;
    bool is_optimized = false;
    bool is_pipelined = false;
    // bool is_first_opt = false;

    // TODO: Config file
    int current_factor = 1;
    int largest_factor = 2;

    std::string iterator_to_skew;
    std::string iterator_to_modify;
    int skew_factor;

    std::vector<std::string> get_loop_level_names();

    int get_loop_level_number_from_dimension_name(std::string dim_name)
    {
        return this->get_loop_level_numbers_from_dimension_names({dim_name})[0];
    }

    // Debug
    void dump_iteration_domain() const;

    // Debug
    void dump_schedule() const;

    // Debug
    void dump() const;

    void gen_time_space_domain();

    primitive_t get_data_type() const;

    const polyfp::expr &get_expr() const;

    std::vector<polyfp::expr> get_placeholder_dims();

    void set_placeholder_dims(std::vector<polyfp::expr> temp);

    int get_loop_levels_number();

    isl_set *get_iteration_domain() const;


    std::vector<polyfp::expr> compute_buffer_size();
    std::map<std::string, std::string > get_access_map();
    std::map<std::string, std::string > get_tile_map();
    std::map<std::string, int > get_tile_size_map();
    std::map<std::string, std::string > get_directive_map();
    std::map<std::string, std::string > get_directive_tool_map();
    void update_leader_components(polyfp::compute *comp);
    void delete_leader_components(polyfp::compute *comp);


    // DSE components
    std::map<polyfp::compute *, int> components;
    std::map<int, polyfp::compute *> component_level_map;
    polyfp::compute *leader;

    std::unordered_map<int, polyfp::compute *> childern;
    std::vector<polyfp::compute * > parents;

    bool is_leader;
    bool has_a_leader;
    bool is_top_parent;
    bool is_leaf;

    void dump_components();
    void dump_loads_stores();

    const std::string &get_name() const;

    isl_map *get_schedule() const;

    void set_expression(const polyfp::expr &e);

    void set_access(std::string access_str);
    void set_access(isl_map *access);

    placeholder *get_placeholder();
    expr get_placeholder_expr();


    // OPT 
    virtual void interchange(var L0, var L1);
    virtual void interchange(int L0, int L1);

    virtual void split(var L0, int sizeX);
    virtual void split(var L0, int sizeX, var L0_outer, var L0_inner);
    virtual void split(int L0, int sizeX);

    virtual void tile(var L0, var L1, int sizeX, int sizeY);
    virtual void tile(var L0, var L1, int sizeX, int sizeY,
                      var L0_outer, var L1_outer, var L0_inner, var L1_inner);
    virtual void tile(var L0, var L1, var L2, int sizeX, int sizeY, int sizeZ);
    virtual void tile(var L0, var L1, var L2, int sizeX, int sizeY, int sizeZ,
                      var L0_outer, var L1_outer, var L2_outer, var L0_inner,
                      var L1_inner, var L2_inner);
    virtual void tile(int L0, int L1, int sizeX, int sizeY);
    virtual void tile(int L0, int L1, int L2, int sizeX, int sizeY, int sizeZ);

    virtual void skew(var i, var j, int a , int b, var ni, var nj);
    virtual void skew(int i, int j, int a, int b); 

    void after(compute &comp, polyfp::var iterator);
    void after(compute &comp, int level);
    void after(compute *comp, polyfp::var iterator);
    void after(compute *comp, int level);

    void after_low_level(compute &comp, int level);
    void after_low_level(compute &comp, std::vector<int> levels);

    void pipeline(polyfp::expr dim, int II);
    void unroll(polyfp::expr dim, int factor);

    std::map<int, std::map<std::string, std::vector<polyfp::expr> > > map_loadstores;
    std::vector<polyfp::expr> get_loads();
    void get_loads_stores();
    void get_all_loadstores();
    void auto_loop_transformation();
    void compute_dependence_vectors();
    std::unordered_map<std::string, polyfp::expr *> load_map;
    std::unordered_map<std::string, polyfp::expr *> store_map;
    std::vector<polyfp::expr *> load_vector;
    std::vector<polyfp::expr *> store_vector;
    std::map<polyfp::expr *, std::vector<std::vector<int> > >  map_dependence_vectors;

    void dump_all_loadstores();
    void check_loop_interchange();
    void check_loop_skewing();
    void apply_opt_strategy(std::vector<int>);

    bool opt_finished = false;
    bool is_skewed_inDSE = false;
    std::vector<int> final_strategy;
    std::vector<int> current_strategy;
    std::vector<int> temp_strategy;

    const static int root_dimension = -1;
    
    template<typename... Args> polyfp::expr operator()(Args... args)
    {
        std::vector<polyfp::expr> access_expressions{std::forward<Args>(args)...};
        if (access_expressions.size() != this->number_of_dims)
        {
            polyfp::str_dump("Error - Incorrect access: " + this->get_name() + "(");
            for (int i = 0; i < access_expressions.size(); i++)
            {
                polyfp::expr e = access_expressions[i];
                e.dump(false);
                if (i != access_expressions.size() - 1)
                    polyfp::str_dump(", ");
            }
            polyfp::str_dump(").\n");
            polyfp::str_dump("The number of access dimensions does not match that used in the declaration of " + this->get_name() + ".\n\n");
            exit(1);
        }
        return polyfp::expr(polyfp::o_access,
                              this->get_name(),
                              access_expressions,
                              this->get_data_type());
    }
    operator expr();
};


}


#endif


================================================
FILE: include/polyhedral/core.h
================================================
#ifndef _H_polyfp_CORE_
#define _H_polyfp_CORE_

#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/space.h>
#include <isl/constraint.h>

#include <map>
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <unordered_set>
#include <sstream>

// #include "debug.h"
#include "expr.h"
#include "type.h"
#include "codegen.h"

namespace polyfp{

class compute;
class constant;
class generator;

void init(std::string name);
void init();
void codegen();

compute *get_computation_annotated_in_a_node(isl_ast_node *node);
int loop_level_into_dynamic_dimension(int level);
int loop_level_into_static_dimension(int level);
int dynamic_dimension_into_loop_level(int dim);

isl_map *add_eq_to_schedule_map(int dim0, int in_dim_coefficient, int out_dim_coefficient,
                                int const_conefficient, isl_map *sched);
}

#endif

================================================
FILE: include/polyhedral/debug.h
================================================
#ifndef _H_DEBUG_
#define _H_DEBUG_

#include <iostream>


namespace polyfp
{

void str_dump(const std::string &str);
void str_dump(const std::string &str, const char *str2);
void str_dump(const char *str, const char *str2);
void print_indentation();

extern int polyfp_indentation;

} // namespace polyfp

#define ERROR(message, exit_program) {                      \
    std::cerr << "Error in " << __FILE__ << ":"             \
              << __LINE__ << " - " << message << std::endl; \
    if (exit_program)                                       \
    {                                                       \
        exit(1);                                            \
    }                                                       \
}

#endif


================================================
FILE: include/polyhedral/expr.h
================================================
#ifndef _H_polyfp_EXPR_
#define _H_polyfp_EXPR_
#include<algorithm>

#include <map>
#include <unordered_map>
#include <vector>
#include <assert.h>
#include "debug.h"
#include "type.h"

namespace polyfp
{
class function;
class compute;

std::string generate_new_variable_name();
std::string str_from_polyfp_type_expr(polyfp::expr_t type);
std::string str_polyfp_type_op(polyfp::op_t type);
std::string str_from_polyfp_type_primitive(polyfp::primitive_t type);

// class placeholder;
class expr;
class var;
class global;

template <typename T>
using only_integral = typename std::enable_if<std::is_integral<T>::value, expr>::type;

class global
{
private:

    static primitive_t loop_iterator_type;
    static function *implicit_fct;

public:

    static std::string generate_new_placeholder_name()
    {
        static int counter = 0;
        return "b" + std::to_string(counter++);
    }

    static std::string generate_new_constant_name()
    {
        static int counter = 0;
        return "C" + std::to_string(counter++);
    }

    static function *get_implicit_function()
    {
        return global::implicit_fct;
    }

    static void set_implicit_function(function *fct)
    {
        global::implicit_fct = fct;
    }

    // TODO: The default data type
    static void set_default_polyfp_options()
    {
        global::loop_iterator_type = p_float32;
    }

    static void set_loop_iterator_type(primitive_t t) {
        global::loop_iterator_type = t;
    }

    static primitive_t get_loop_iterator_data_type()
    {
        return global::loop_iterator_type;
    }

    global()
    {
        set_default_polyfp_options();
    }
};


class expr
{
    friend class var;
    friend class computation;
    friend class generator;
    friend class p_max;

    // The type of the operator.
    polyfp::op_t _operator;
    std::vector<polyfp::expr> op;

    union
    {
        uint8_t     uint8_value;
        int8_t      int8_value;
        uint16_t    uint16_value;
        int16_t     int16_value;
        uint32_t    uint32_value;
        int32_t     int32_value;
        uint64_t    uint64_value;
        int64_t     int64_value;
        float       float32_value;
        double      float64_value;
    };


    // e.g. {i, j}
    std::vector<polyfp::expr> access_vector;

    bool defined;

protected:

    std::string name;

    polyfp::primitive_t dtype;

    polyfp::expr_t etype;

public:

  
    polyfp::compute *owner;

    // Create an undefined expression.
    expr()
    {
        this->defined = false;

        this->_operator = polyfp::o_none;
        this->etype = polyfp::e_none;
        this->dtype = polyfp::p_none;
    }

    // Create an undefined expression with type.
    expr(polyfp::primitive_t dtype)
    {
        this->defined = false;
        this->_operator = polyfp::o_none;
        this->etype = polyfp::e_none;
        this->dtype = dtype;
    }


    /**
      * Create an expression for a unary operator that applies
      * on a variable. For example: allocate(A) or free(B).
      */
    expr(polyfp::op_t o, std::string name)
    {
        this->_operator = o;
        this->etype = polyfp::e_op;
        this->dtype = polyfp::p_none;
        this->defined = true;
        this->name = name;
    }

    /**
     * Construct an expression for a binary operator.
     */
    expr(polyfp::op_t o, polyfp::expr expr0, polyfp::expr expr1)
    {
        if (expr0.get_data_type() != expr1.get_data_type())
        {
            polyfp::str_dump("Binary operation between two expressions of different types:\n");
            expr0.dump(false);
            polyfp::str_dump(" (" + str_from_polyfp_type_primitive(expr0.get_data_type()) + ")");
            polyfp::str_dump(" and ");
            expr1.dump(false);
            polyfp::str_dump(" (" + str_from_polyfp_type_primitive(expr1.get_data_type()) + ")");
            polyfp::str_dump("\n");
            ERROR("\nThe two expressions should be of the same type. Use casting to elevate the type of one expression to the other.\n", true);
        }

        this->_operator = o;
        this->etype = polyfp::e_op;
        this->dtype = expr0.get_data_type();
        this->defined = true;

        this->op.push_back(expr0);
        this->op.push_back(expr1);
    }


    // Construct an access
    expr(polyfp::op_t o, std::string name,
         std::vector<polyfp::expr> vec,
         polyfp::primitive_t type)
    {
        assert(((o == polyfp::o_access) || (o == polyfp::o_placeholder)) &&
               "The operator is not an access or a placeholder operator.");

        assert(vec.size() > 0);
        assert(name.size() > 0);

        this->_operator = o;
        this->etype = polyfp::e_op;
        this->dtype = type;
        this->defined = true;

        if (o == polyfp::o_access || o == polyfp::o_placeholder)
        {
            this->set_access(vec);
        }
        else
        {
            ERROR("Type of operator is not o_access or o_placeholder, or o_lin_index.", true);
        }

        this->name = name;
    }

    // Construct an unsigned 8-bit integer expression.
    expr(uint8_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;
        this->dtype = polyfp::p_uint8;
        this->uint8_value = val;
    }

    // Construct a signed 8-bit integer expression.
    expr(int8_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;
        this->dtype = polyfp::p_int8;
        this->int8_value = val;
    }

    // Construct an unsigned 16-bit integer expression.
    expr(uint16_t val)
    {
        this->defined = true;
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->dtype = polyfp::p_uint16;
        this->uint16_value = val;
    }

    expr(int16_t val)
    {
        this->defined = true;
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;

        this->dtype = polyfp::p_int16;
        this->int16_value = val;
    }

    expr(uint32_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_uint32;
        this->uint32_value = val;
    }

    expr(int32_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_int32;
        this->int32_value = val;
    }

    expr(uint64_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_uint64;
        this->uint64_value = val;
    }

    expr(int64_t val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_int64;
        this->int64_value = val;
    }

    expr(float val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_float32;
        this->float32_value = val;
    }

    polyfp::expr copy() const;

    expr(double val)
    {
        this->etype = polyfp::e_val;
        this->_operator = polyfp::o_none;
        this->defined = true;

        this->dtype = polyfp::p_float64;
        this->float64_value = val;
    }

    uint8_t get_uint8_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_uint8);

        return uint8_value;
    }

    int8_t get_int8_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_int8);

        return int8_value;
    }

    uint16_t get_uint16_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_uint16);

        return uint16_value;
    }

    int16_t get_int16_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_int16);

        return int16_value;
    }

    uint32_t get_uint32_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_uint32);

        return uint32_value;
    }

    int32_t get_int32_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_int32);

        return int32_value;
    }

    uint64_t get_uint64_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_uint64);

        return uint64_value;
    }

    int64_t get_int64_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_int64);

        return int64_value;
    }

    float get_float32_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_float32);

        return float32_value;
    }

    double get_float64_value() const
    {
        assert(this->get_expr_type() == polyfp::e_val);
        assert(this->get_data_type() == polyfp::p_float64);

        return float64_value;
    }

    int64_t get_int_val() const
    {
        assert(this->get_expr_type() == polyfp::e_val);

        int64_t result = 0;

        if (this->get_data_type() == polyfp::p_uint8)
        {
            result = this->get_uint8_value();
        }
        else if (this->get_data_type() == polyfp::p_int8)
        {
            result = this->get_int8_value();
        }
        else if (this->get_data_type() == polyfp::p_uint16)
        {
            result = this->get_uint16_value();
        }
        else if (this->get_data_type() == polyfp::p_int16)
        {
            result = this->get_int16_value();
        }
        else if (this->get_data_type() == polyfp::p_uint32)
        {
            result = this->get_uint32_value();
        }
        else if (this->get_data_type() == polyfp::p_int32)
        {
            result = this->get_int32_value();
        }
        else if (this->get_data_type() == polyfp::p_uint64)
        {
            result = this->get_uint64_value();
        }
        else if (this->get_data_type() == polyfp::p_int64)
        {
            result = this->get_int64_value();
        }
        else if (this->get_data_type() == polyfp::p_float32)
        {
            result = this->get_float32_value();
        }
        else if (this->get_data_type() == polyfp::p_float64)
        {
            result = this->get_float64_value();
        }
        else
        {
            ERROR("Calling get_int_val() on a non integer expression.", true);
        }

        return result;
    }

    double get_double_val() const
    {
        assert(this->get_expr_type() == polyfp::e_val);

        double result = 0;

        if (this->get_data_type() == polyfp::p_float32)
        {
            result = this->get_float32_value();
        }
        else if (this->get_data_type() == polyfp::p_float64)
        {
            result = this->get_float64_value();
        }
        else
        {
            ERROR("Calling get_double_val() on a non double expression.", true);
        }

        return result;
    }

    /**
      * Return the value of the \p i 'th operand of the expression.
      * \p i can be 0, 1 or 2.
      */
    const polyfp::expr &get_operand(int i) const
    {
        assert(this->get_expr_type() == polyfp::e_op);
        assert((i < (int)this->op.size()) && "Operand index is out of bounds.");

        return this->op[i];
    }

    // Return the number of arguments of the operator.
    int get_n_arg() const
    {
        assert(this->get_expr_type() == polyfp::e_op);

        return this->op.size();
    }

    polyfp::expr_t get_expr_type() const
    {
        return etype;
    }

    polyfp::primitive_t get_data_type() const
    {
        return dtype;
    }

    const std::string &get_name() const
    {
        assert(
            (this->get_expr_type() == polyfp::e_var) ||
               (this->get_op_type() == polyfp::o_access) ||
               (this->get_op_type() == polyfp::o_placeholder));
        return name;
    }

    void set_name(std::string &name)
    {
        assert((this->get_expr_type() == polyfp::e_var) ||
               (this->get_op_type() == polyfp::o_access));

        this->name = name;
    }

    polyfp::expr replace_op_in_expr(const std::string &to_replace,
                                      const std::string &replace_with)
    {
        if (this->name == to_replace) {
            this->name = replace_with;
            return *this;
        }
        for (int i = 0; i < this->op.size(); i++) {
            polyfp::expr operand = this->get_operand(i);
            this->op[i] = operand.replace_op_in_expr(to_replace, replace_with);
        }
        return *this;
    }

    // Get the type of the operator (polyfp::op_t)
    polyfp::op_t get_op_type() const
    {
        return _operator;
    }

    // e.g. For a placeholder access A[i+1,j], it will return {i+1, j}
    const std::vector<polyfp::expr> &get_access() const
    {
        assert(this->get_expr_type() == polyfp::e_op);
        assert(this->get_op_type() == polyfp::o_access || this->get_op_type() == polyfp::o_placeholder);

        return access_vector;
    }



    // Get the number of dimensions in the access vector.
    int get_n_dim_access() const
    {
        assert(this->get_expr_type() == polyfp::e_op);
        assert(this->get_op_type() == polyfp::o_access);

        return access_vector.size();
    }

    bool is_defined() const
    {
        return defined;
    }


    bool is_equal(polyfp::expr e) const
    {
        bool equal = true;

        if ((this->_operator != e._operator) ||
            (this->op.size() != e.op.size()) ||
            (this->access_vector.size()   != e.access_vector.size())   ||
            (this->defined != e.defined)     ||
            (this->name != e.name)           ||
            (this->dtype != e.dtype)         ||
            (this->etype != e.etype))
        {
            equal = false;
            return equal;
        }

        for (int i = 0; i < this->access_vector.size(); i++)
            equal = equal && this->access_vector[i].is_equal(e.access_vector[i]);

        for (int i = 0; i < this->op.size(); i++)
            equal = equal && this->op[i].is_equal(e.op[i]);

        if ((this->etype == e_val) && (e.etype == e_val))
        {
            if (this->get_int_val() != e.get_int_val())
                equal = false;
            if ((this->get_data_type() == polyfp::p_float32) ||
                (this->get_data_type() == polyfp::p_float64))
                if (this->get_double_val() != e.get_double_val())
                    equal = false;
        }

        return equal;
    }

    bool is_integer() const
    {
        return this->get_expr_type() == e_val &&
                (this->get_data_type() == p_uint8 ||
                 this->get_data_type() == p_uint16 ||
                 this->get_data_type() == p_uint32 ||
                 this->get_data_type() == p_uint64 ||
                 this->get_data_type() == p_int16 ||
                 this->get_data_type() == p_int32 ||
                 this->get_data_type() == p_int8 ||
                 this->get_data_type() == p_int64);
    }


    expr operator+(polyfp::expr other) const;
    expr operator-(polyfp::expr other) const;
    expr operator/(polyfp::expr other) const;
    expr operator*(polyfp::expr other) const;
    expr operator%(polyfp::expr other) const;
    expr operator>>(polyfp::expr other) const;

    // TODO: Extensions
    // Expression multiplied by (-1).

    polyfp::expr& operator=(polyfp::expr const &);

    void set_access(std::vector<polyfp::expr> vector)
    {
        access_vector = vector;
    }

    void set_access_dimension(int i, polyfp::expr acc)
    {
        assert((i < (int)this->access_vector.size()) && "index is out of bounds.");
        access_vector[i] = acc;
    }

    void get_access_vector(std::vector<polyfp::expr> &loads) const{

        switch (this->etype){
            case polyfp::e_op:
            {
                if (this->get_n_arg() > 0)
                {
                    for (int i = 0; i < this->get_n_arg(); i++)
                    {
                        this->op[i].get_access_vector(loads);
                    }
                }
                if ((this->get_op_type() == polyfp::o_access))
                {
                    // std::cout << "Access to " +  this->get_name() + ". Access expressions:" << std::endl;
                    loads.push_back(*this);
                }
                
                break;
            }
            case (polyfp::e_val):
            {
                // TODO: 
                // if (this->get_data_type() == polyfp::p_uint8)
                // {
                //     std::cout << "Value:" << this->get_uint8_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_int8)
                // {
                //     std::cout << "Value:" << this->get_int8_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_uint16)
                // {
                //     std::cout << "Value:" << this->get_uint16_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_int16)
                // {
                //     std::cout << "Value:" << this->get_int16_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_uint32)
                // {
                //     std::cout << "Value:" << this->get_uint32_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_int32)
                // {
                //     std::cout << "Value:" << this->get_int32_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_uint64)
                // {
                //     std::cout << "Value:" << this->get_uint64_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_int64)
                // {
                //     std::cout << "Value:" << this->get_int64_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_float32)
                // {
                //     std::cout << "Value:" << this->get_float32_value() << std::endl;
                // }
                // else if (this->get_data_type() == polyfp::p_float64)
                // {
                //     std::cout << "Value:" << this->get_float64_value() << std::endl;
                // }
                break;
            }
            case (polyfp::e_var):
            {
                // TODO:
                // std::cout << "Var name:" << this->get_name() << std::endl;
                // std::cout << "Expression value type:" << str_from_polyfp_type_primitive(this->dtype) << std::endl;
                break;
            }
        } 

    }

    void dump(bool exhaustive) const
    {
        if (this->get_expr_type() != e_none)
        {
            if (exhaustive == true)
            {
                if (this->is_defined())
                {
                    std::cout << "Expression:" << std::endl;
                    std::cout << "Expression type:" << str_from_polyfp_type_expr(this->etype) << std::endl;
                    switch (this->etype)
                    {
                    case polyfp::e_op:
                    {
                        std::cout << "Expression operator type:" << str_polyfp_type_op(this->_operator) << std::endl;
                        if (this->get_n_arg() > 0)
                        {
                            std::cout << "Number of operands:" << this->get_n_arg() << std::endl;
                            std::cout << "Dumping the operands:" << std::endl;
                            for (int i = 0; i < this->get_n_arg(); i++)
                            {
                                std::cout << "Operand " << std::to_string(i) << "." << std::endl;
                                this->op[i].dump(exhaustive);
                            }
                        }
                        if ((this->get_op_type() == polyfp::o_access))
                        {
                            std::cout << "Access to " +  this->get_name() + ". Access expressions:" << std::endl;
                            for (const auto &e : this->get_access())
                            {
                                e.dump(exhaustive);
                            }
                        }
                        
                        break;
                    }
                    case (polyfp::e_val):
                    {
                        std::cout << "Expression value type:" << str_from_polyfp_type_primitive(this->dtype) << std::endl;

                        if (this->get_data_type() == polyfp::p_uint8)
                        {
                            std::cout << "Value:" << this->get_uint8_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_int8)
                        {
                            std::cout << "Value:" << this->get_int8_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_uint16)
                        {
                            std::cout << "Value:" << this->get_uint16_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_int16)
                        {
                            std::cout << "Value:" << this->get_int16_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_uint32)
                        {
                            std::cout << "Value:" << this->get_uint32_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_int32)
                        {
                            std::cout << "Value:" << this->get_int32_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_uint64)
                        {
                            std::cout << "Value:" << this->get_uint64_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_int64)
                        {
                            std::cout << "Value:" << this->get_int64_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_float32)
                        {
                            std::cout << "Value:" << this->get_float32_value() << std::endl;
                        }
                        else if (this->get_data_type() == polyfp::p_float64)
                        {
                            std::cout << "Value:" << this->get_float64_value() << std::endl;
                        }
                        break;
                    }
                    case (polyfp::e_var):
                    {
                        std::cout << "Var name:" << this->get_name() << std::endl;
                        std::cout << "Expression value type:" << str_from_polyfp_type_primitive(this->dtype) << std::endl;
                        break;
                    }

                    }
                }
            }
            else
            {   std::cout << "dump expression"<<std::endl;
                std::cout << this->to_str();
            }
        }
    }

    bool is_constant() const
    {
        if (this->get_expr_type() == polyfp::e_val)
            return true;
        else
            return false;
    }

    int get_dependence_vector() const{
        // TODO: a more general method to calculate dependence vector
        // Already supported: A(i+4,j-5)-> A(i,j-1)
        // Not supported: A(2*i,j), A(i+j, j+9)
        int temp;
        if (this->get_expr_type() == e_op){
            switch (this->get_op_type()){
                case polyfp::o_add:
                    if ((this->get_operand(0).get_expr_type() == polyfp::e_val)){
                        temp = this->get_operand(0).get_int_val();
                    }else if((this->get_operand(1).get_expr_type() == polyfp::e_val)){
                        temp = this->get_operand(1).get_int_val();
                    }else{
                        std::cout<<"not supported type"<<std::endl;
                        return false;
                    }
                case polyfp::o_sub:
                    if ((this->get_operand(0).get_expr_type() == polyfp::e_val)){
                        temp = -(this->get_operand(1).get_int_val());
                    }else if((this->get_operand(1).get_expr_type() == polyfp::e_val)){
                        temp = -(this->get_operand(1).get_int_val());
                    }else{
                        std::cout<<"not supported type"<<std::endl;
                        return false;
                    }
            }
        }else if(this->get_expr_type() == e_var){
            temp = 0;
            

        }else{
            std::cout<<"not supported type"<<std::endl;
            return false;
        }
        return temp;


    }



    // Simplify the expression.
    polyfp::expr simplify() const
    {
        if (this->get_expr_type() != e_none)
        {
            switch (this->etype)
            {
                case polyfp::e_op:
                {
                    switch (this->get_op_type())
                    {
                    case polyfp::o_max:
                        return *this;
                    case polyfp::o_min:
                        return *this;
                    case polyfp::o_add:
                        this->get_operand(0).simplify();
                        this->get_operand(1).simplify();
                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))
                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))
                                return expr(this->get_operand(0).get_int_val() + this->get_operand(1).get_int_val());
                    case polyfp::o_sub:
                        this->get_operand(0).simplify();
                        this->get_operand(1).simplify();
                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))
                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))
                                return expr(this->get_operand(0).get_int_val() - this->get_operand(1).get_int_val());
                    case polyfp::o_mul:
                        this->get_operand(0).simplify();
                        this->get_operand(1).simplify();
                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))
                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))
                                return expr(this->get_operand(0).get_int_val() * this->get_operand(1).get_int_val());
                    case polyfp::o_div:
                        return *this;
                    case polyfp::o_mod:
                        return *this;
                    case polyfp::o_access:
                        return *this;
                    default:
                        ERROR("Simplifying an unsupported polyfp expression.", 1);
                    }
                    break;
                }
                case (polyfp::e_val):
                {
                    return *this;
                }
                case (polyfp::e_var):
                {
                    return *this;
                }
                default:
                    ERROR("Expression type not supported.", true);
            }
        }

        return *this;
    }
#include <iostream>

    std::string to_str() const
    {
        std::string str = std::string("");

        if (this->get_expr_type() != e_none)
        {
            // std::cout<<this->get_expr_type();
                switch (this->etype)
                {
                case polyfp::e_op:
                {
                    switch (this->get_op_type())
                    {
                    case polyfp::o_max:
                        str +=  "max(" + this->get_operand(0).to_str();
                        str +=  ", " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_min:
                        str +=  "min(" + this->get_operand(0).to_str();
                        str +=  ", " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_add:
                        str +=  "(" + this->get_operand(0).to_str();
                        str +=  " + " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_sub:
                        str +=  "(" + this->get_operand(0).to_str();
                        str +=  " - " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_mul:
                        str +=  "(" + this->get_operand(0).to_str();
                        str +=  " * " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_div:
                        str +=  "(" + this->get_operand(0).to_str();
                        str +=  " / " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_mod:
                        str +=  "(" + this->get_operand(0).to_str();
                        str +=  " % " + this->get_operand(1).to_str();
                        str +=  ")";
                        break;
                    case polyfp::o_access:
                    case polyfp::o_placeholder:
                        str +=  this->get_name() + "(";
                        for (int k = 0; k < this->get_access().size(); k++)
                        {
                            if (k != 0)
                            {
                                str +=  ", ";
                            }
                            str += this->get_access()[k].to_str();
                        }
                        str +=  ")";
                        break;
                    default:
                        ERROR("Dumping an unsupported polyfp expression.", 1);
                    }
                    break;
                }
                case (polyfp::e_val):
                {
                    if (this->get_data_type() == polyfp::p_uint8)
                    {
                        str +=  std::to_string((int)this->get_uint8_value());
                    }
                    else if (this->get_data_type() == polyfp::p_int8)
                    {
                        str +=  std::to_string((int)this->get_int8_value());
                    }
                    else if (this->get_data_type() == polyfp::p_uint16)
                    {
                        str +=  std::to_string(this->get_uint16_value());
                    }
                    else if (this->get_data_type() == polyfp::p_int16)
                    {
                        str +=  std::to_string(this->get_int16_value());
                    }
                    else if (this->get_data_type() == polyfp::p_uint32)
                    {
                        str +=  std::to_string(this->get_uint32_value());
                    }
                    else if (this->get_data_type() == polyfp::p_int32)
                    {
                        str +=  std::to_string(this->get_int32_value());
                    }
                    else if (this->get_data_type() == polyfp::p_uint64)
                    {
                        str +=  std::to_string(this->get_uint64_value());
                    }
                    else if (this->get_data_type() == polyfp::p_int64)
                    {
                        str +=  std::to_string(this->get_int64_value());
                    }
                    else if (this->get_data_type() == polyfp::p_float32)
                    {
                        str +=  std::to_string(this->get_float32_value());
                    }
                    else if (this->get_data_type() == polyfp::p_float64)
                    {
                        str +=  std::to_string(this->get_float64_value());
                    }
                    break;
                }
                case (polyfp::e_var):
                {
                    str += this->get_name();
                    break;
                }

                default:
                    ERROR("Expression type not supported.", true);
                }
            }

          return str;
        }

};


class var: public polyfp::expr
{
    friend compute;
private:

    static std::unordered_map<std::string, var> declared_vars;
    expr lower;
    expr upper;

public:

    // Return the upper bound of this variable.
    expr get_upper()
    {
	    return upper;
    }

    expr get_lower()
    {
	    return lower;
    }

    var(std::string name);

    var(std::string name, polyfp::primitive_t type);

    var(std::string name, int lower_bound, int upper_bound) : var(name)
    {
        lower = expr((int32_t) lower_bound);
        upper = expr((int32_t) upper_bound);
        // flag = 0;

    }
    var(std::string name, expr lower_bound, expr upper_bound) : var(name)
    {
        lower = lower_bound;
        upper = upper_bound;
        // flag = 0;
    }
     var(std::string name, int lower_bound, expr upper_bound) : var(name)
    {
        lower = expr((int32_t) lower_bound);
        upper = upper_bound;
        // flag = 0;

    }

    var(): var(generate_new_variable_name()) {}

    void show(){
        std::cout << "Saved variable " << this->name << " of type " << str_from_polyfp_type_primitive(this->dtype)<<std::endl;
    }


};


class constant: public polyfp::expr
{
    friend compute;
    friend function;
private:
    expr value;
    float float_value;
    polyfp::primitive_t datatype;
    polyfp::function *func;
    
public:


    constant(float value = 0, polyfp::primitive_t t = p_float32, polyfp::function *fct = global::get_implicit_function());
    polyfp::primitive_t get_type() const;
    
};

class p_max: public polyfp::expr
{
    friend compute;
    friend function;
private:
    expr left_value;
    expr right_value;
    polyfp::function *func;

    
public:
    p_max( polyfp::expr value1, polyfp::expr value2, polyfp::op_t o = polyfp::o_max, polyfp::function *fct = global::get_implicit_function());

};


/**
  * Takes in a primitive value \p val, and returns an expression
  * of polyfp type \p tT that represents \p val.
  */
template <typename cT>
expr value_cast(primitive_t tT, cT val) {

    switch (tT) {
        case p_int8:
            return expr{static_cast<int8_t>(val)};
        case p_uint8:
            return expr{static_cast<uint8_t>(val)};
        case p_int16:
            return expr{static_cast<int16_t>(val)};
        case p_uint16:
            return expr{static_cast<uint16_t>(val)};
        case p_int32:
            return expr{static_cast<int32_t>(val)};
        case p_uint32:
            return expr{static_cast<uint32_t>(val)};
        case p_int64:
            return expr{static_cast<int64_t>(val)};
        case p_uint64:
            return expr{static_cast<uint64_t>(val)};
        case p_float32:
            return expr{static_cast<float>(val)};
        case p_float64:
            return expr{static_cast<double>(val)};
        default:
            throw std::invalid_argument{"Type not supported"};
    }
}



template <typename T>
only_integral<T> operator+(const polyfp::expr &e, T val)
{
    return e + value_cast(e.get_data_type(), val);
}

template <typename T>
only_integral<T> operator+(T val, const polyfp::expr &e)
{
    return value_cast(e.get_data_type(), val) + e;
}

template <typename T>
only_integral<T> operator-(const polyfp::expr &e, T val)
{
    return e - value_cast(e.get_data_type(), val);
}

template <typename T>
only_integral<T> operator-(T val, const polyfp::expr &e)
{
    return value_cast(e.get_data_type(), val) - e;
}

template <typename T>
only_integral<T> operator/(const polyfp::expr &e, T val)
{
    return e / expr{val};
}

template <typename T>
only_integral<T> operator/(T val, const polyfp::expr &e)
{
    return expr{val} / e;
}

template <typename T>
only_integral<T> operator*(const polyfp::expr &e, T val)
{
    return e * value_cast(e.get_data_type(), val);
}

template <typename T>
only_integral<T> operator*(T val, const polyfp::expr &e)
{
    return value_cast(e.get_data_type(), val) * e;
}

template <typename T>
only_integral<T> operator%(const polyfp::expr &e, T val)
{
    return e % expr{val};
}

template <typename T>
only_integral<T> operator%(T val, const polyfp::expr &e)
{
    return expr{val} % e;
}

}


#endif


================================================
FILE: include/polyhedral/function.h
================================================
#ifndef _H_polyfp_function_
#define _H_polyfp_function_

#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/space.h>
#include <isl/constraint.h>

#include <map>
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <queue>

#include "scalehls/Transforms/Passes.h"
#include "scalehls/Transforms/Utils.h"
#include "scalehls/Transforms/Estimator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ToolOutputFile.h"
#include "mlir/Dialect/Affine/Analysis/Utils.h"
#include "mlir/Dialect/Affine/IR/AffineValueMap.h"
#include "mlir/Support/FileUtilities.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/MemoryBuffer.h"

#include "llvm/ADT/SmallPtrSet.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/IR/BuiltinOps.h"

#include "mlir/Dialect/Affine/Analysis/LoopAnalysis.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"

#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Verifier.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"

#include "expr.h"
#include "type.h"
#include "codegen.h"
#include "generator_isl.h"
#include "placeholder.h"

namespace polyfp{


class constant;
class compute;
class generator;
class placeholder;

isl_ast_node *for_code_generator_after_for(isl_ast_node *node, isl_ast_build *build, void *user);
void gen_mlir(polyfp::function *fct, isl_ast_node *node, int level);


class function{

    friend constant;
    friend compute;
    friend generator;
    friend placeholder;

private:

    std::string name;
    std::vector<polyfp::constant> invariants;
    std::map<std::string, polyfp::constant *> constant_list;
    std::vector<polyfp::placeholder *> function_arguments;

    std::map<std::string, polyfp::placeholder *> placeholders_list;
    std::map<std::string, polyfp::placeholder *> fct_argument_list;
    std::map<std::string, polyfp::placeholder *> global_argument_list;
    bool fct_argument_added = false;

    std::vector<std::tuple<std::string, std::vector<int>, std::vector<std::string>>> partition_map;

    // The isl context of the function.
    isl_ctx *ctx;

      
    // The isl AST generated by gen_isl_ast().
    isl_ast_node *ast;

    // Contains all the computes of the function
    std::vector<compute *> body;

    /**
     * TODO: Extend
     * Derived from Tiramisu:
     * The context set of the function, i.e. a set representing the
     * constraints over the parameters.
     * The parameters of a function are the function invariants (constants).
     */
    isl_set *context_set;

    std::vector<std::string> iterator_names;

    isl_union_set *get_trimmed_time_processor_domain() const;

    /**
      * Derived from Tiramisu:
      * This function iterates over the computes of the function.
      * It modifies the identity schedule of each computes in order to
      * make all the identity schedules have the same number of dimensions
      * in their ranges.
      * This is done by adding dimensions equal to 0 to the range of each
      * identity schedule that does not have enough dimensions.
      */
    isl_union_map *get_aligned_identity_schedules() const;

    /**
      * Derived from Tiramisu:
      * This function first computes the identity schedules,
      * then it computes the maximal dimension among the dimensions
      * of the ranges of all the identity schedules.
      */
    int get_max_identity_schedules_range_dim() const;

    void rename_computations();
  
    // Recursive function to perform the DFS step of dump_sched_graph.
    void dump_sched_graph_dfs(polyfp::compute *,
                              std::unordered_set<polyfp::compute *> &);

    // Recursive function to perform the DFS step of is_sched_graph_tree.
    bool is_sched_graph_tree_dfs(polyfp::compute *,
                                 std::unordered_set<polyfp::compute *> &);

protected:

    void dfs(int pos, int top, int end, int map[500][500], int n, int v[500],int stack[500]);
    polyfp::compute * update_latency();
    int get_longest_path();
    int get_longest_node(std::vector<long> path);
    void add_computation(compute *cpt);

    void add_invariant(std::pair<std::string, polyfp::constant *>  param);
    void add_placeholder(std::pair<std::string, polyfp::placeholder *> buf);

    const std::vector<std::string> &get_iterator_names() const;

    // void add_iterator_name(const std::string &it_name);

    const std::vector<compute *> &get_computations() const;

    /** TODO: remove
      * Derived from Tiramisu:
      * Return a set that represents the parameters of the function
      * (an ISL set that represents the parameters and constraints over
      * the parameters of the functions,  a parameter is an invariant
      * of the function). This set is also known as the context of
      * the program.
      * An example of a context set is the following:
      *          "[N,M]->{: M>0 and N>0}"
      * This context set indicates that the two parameters N and M
      * are strictly positive.
      */
    isl_set *get_program_context() const;

    std::vector<compute *> get_computation_by_name(std::string str) const;

    isl_ctx *get_isl_ctx() const;

    /**
      * Return the union of all the schedules
      * of the compute of the function.
      */
    isl_union_map *get_schedule() const;

    /**
      * Return the union of all the iteration domains
      * of the computes of the function.
      */
    isl_union_set *get_iteration_domain() const;

    /**
     * Return true if the usage of high level scheduling comments is valid; i.e. if
     * the scheduling relations formed using before, after, compute_at, etc.. form a tree.
     *
     * More specifically, it verifies that:
     *     - There should be exactly one compute with no compute scheduled before it.
     *     - Each other compute should have exactly one compute scheduled before it.
     */
    bool is_sched_graph_tree();

    /**
     * Modify the schedules of the computes of this function to reflect
     * the order specified using the high level scheduling commands.
     *
     * Commands like .after() do not directly modify the schedules
     * but rather modify the sched_graph.
     */
    void gen_ordering_schedules();

    /**
     * This functions iterates over the schedules of the function (the schedule
     * of each compute in the function) and computes the maximal dimension
     * among the dimensions of the ranges of all the schedules.
     */
    int get_max_schedules_range_dim() const;

    /**
      * Stores all high level scheduling instructions between computes; i.e. if a user calls
      * for example c2.after(c1, L), sched_graph[&c1] would contain the key &c2, and
      * sched_graph[&c1][&c2] = L.
      */
    std::unordered_map<polyfp::compute *,std::unordered_map<polyfp::compute *, int>> sched_graph;

    std::unordered_map<polyfp::compute *,
    std::unordered_map<polyfp::compute *, int>> sched_graph_reversed;

    /**
      * Return an ISL AST that represents this function.
      * The function gen_isl_ast() should be called before calling
      * this function.
      */
    isl_ast_node *get_isl_ast() const;
  

    // Generate a mlir stmt that represents the function.
    void gen_mlir_stmt();

public:

    bool is_dataflowed = false;
    void evaluate_func();
    std::unordered_set<polyfp::compute *> starting_computations;
    std::vector<polyfp::compute *> leader_computations;
    std::vector<polyfp::compute *> leaf_computations;
    std::map<polyfp::compute *,int> leader_computation_index;
    std::map<int,long> latency_map;
    std::map<int,long> all_latency_map;
    std::map<int,int> resource_map;
    std::map<int,std::vector<int>> path_map;
    std::vector<std::vector<long>> paths;
    std::vector<std::string> finish_list;
    bool consistent_flag = true;
    bool refused = false;


    void add_fct_argument(std::pair<std::string, polyfp::placeholder *> buf);

    void add_fct_argument();

    void add_global_argument(std::pair<std::string, polyfp::placeholder *> buf);

    void check_loop_fusion();

    int get_global_location(){
        return global_location;
    }

    void set_global_location(int new_location){
        this->global_location = new_location;
    }

    void dump_schedule(std::string path);

    long longest_path;
    long longest_node;
    long dsp_max;
    long dsp_usage;
    long best_dsp_usage = dsp_max;
    long best_latency;
    long current_latency;
    bool new_strategy = true;
    polyfp::compute * current_opt_comp;


    int global_location;
    bool one_compute;

    function(std::string name);

    /**
      * Derived from Tiramisu:
      * This method applies to the schedule of each compute
      * in the function.  It makes the dimensions of the ranges of
      * all the schedules equal.  This is done by adding dimensions
      * equal to 0 to the range of each schedule.
      * This function is called automatically when gen_isl_ast()
      * or gen_time_processor_domain() are called.
      */
    void align_schedules();

    const std::vector<compute *> &get_body() const;

    const std::map<std::string, polyfp::placeholder *> &get_placeholders() const;
    const std::map<std::string, polyfp::placeholder *> &get_fct_arguments() const;
    const std::map<std::string, polyfp::placeholder *> &get_global_arguments() const;

    const std::map<std::string, polyfp::constant *> &get_invariants() const;
    const std::vector<std::string> get_invariant_names() const;

    std::vector<std::tuple<std::string, std::vector<int>, std::vector<std::string>>> get_partition_map();

    void set_partition(std::string name, std::vector<int> tile_factors, std::vector<std::string> types);
    
    void dump_sched_graph();

    isl_ast_node *get_isl_ast1() const;


    /**
      * Compute the graph of dependences between the computes of the function. 
      * C[0] = 0
      * D[1] = C[0]
      * D[2] = C[0]
      * {C[0] -> D[1]; C[0]->D[2]}
      */
    isl_union_map *compute_dep_graph();
    void gen_isl_ast();

    /**
      * Generate the time-space domain of the function.
      *
      * In this representation, the logical time of execution and the
      * processor where the compute will be executed are both
      * specified.
      */
    void gen_time_space_domain();

    void gen_loop_location();
    std::string get_name();

    void collect_accesses();
    std::map<int, std::map<std::string, std::vector<polyfp::expr> > > map_loadstores;

    void codegen();
    void auto_DSE_loop_transformation();
    void auto_DSE(std::string path);
    void auto_DSE_tile_size(polyfp::compute* comp, int factor,std::string path);
    void dependence_analysis();
    void compute_dependency_graph();

    /**
      * Dump the function on standard output (dump most of the fields of
      * polyfp::function).This is mainly useful for debugging.
      */
    void dump(bool exhaustive) const;
    void gen_c_code() const;

    void trans();


};

}

#endif

================================================
FILE: include/polyhedral/generator.h
================================================
#include <isl/aff.h>
#include <isl/set.h>
#include <isl/constraint.h>
#include <isl/space.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>

#include <string>
#include "mlir/Dialect/Affine/Analysis/LoopAnalysis.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/Verifier.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
#include "mlir/Dialect/SCF/SCF.h"
#include "mlir/Target/LLVMIR/Import.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"

#include "mlir/Pass/PassManager.h"
#include "mlir/Target/LLVMIR/Export.h"
#include "mlir/Transforms/Passes.h"

#include <numeric>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/union_set.h>
#include <isl/ast_build.h>
#include <isl/schedule.h>
#include <isl/schedule_node.h>
#include <isl/space.h>
#include <isl/constraint.h>

#include <map>
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <queue>
#include <variant>
#include "expr.h"
#include "type.h"
#include "codegen.h"
#include "function.h"

using llvm::SmallVector;
using llvm::ArrayRef;
namespace polyfp{
class function;
class MLIRGenImpl {
friend function;
friend compute;

private:
  mlir::ModuleOp theModule;

  /// The builder is a helper class to create IR inside a function. The builder
  /// is stateful, in particular it keeps an "insertion point": this is where
  /// the next operations will be introduced.
  mlir::OpBuilder builder;
    
public:
  
    MLIRGenImpl(mlir::MLIRContext &context) : builder(&context) 
    {
        theModule = mlir::ModuleOp::create(builder.getUnknownLoc());
    }

    mlir::ModuleOp mlirGen1(const polyfp::function &fct, isl_ast_node *isl_node, int &level, bool flag, bool flag2, bool if_flag);

    //contains all loops 
    std::vector<mlir::AffineForOp> ops;
    std::vector<int> start_loops_position;


    // std::map<std::string, mlir::Type > argument_list;
    std::vector<std::string> argument_list;
    std::map<std::string,mlir::Value> argument_map;
    std::map<std::string,int> array_map;
    std::map<std::string,mlir::Value> get_argument_map();
    std::map<std::string,int> get_array_map();
    std::vector<mlir::Value> values;
    
    std::vector<mlir::memref::AllocOp> allocs;
    std::vector<mlir::FuncOp> funcs;
    std::vector<mlir::FuncOp> get_funcs();
    std::map<int, std::string > name_map;

    
    mlir::ModuleOp getModule();

    void a_print_expr(polyfp::expr polyfp_expr, polyfp::compute *comp, int level);

    // std::vector<mlir::Value> index_values;
    // SmallVector<mlir::AffineExpr> index_args;
    int get_iterator_location_from_name(polyfp::compute *comp,polyfp::expr polyfp_expr, std::vector<mlir::Value> &index_values);
    mlir::AffineExpr a_print_index(polyfp::expr polyfp_expr, polyfp::compute *comp, std::vector<mlir::Value> &index_values,int level);
    // std::vector<mlir::arith::AddFOp> add_op;
    // // std::vector<mlir::Op<>> sum_op;
    // std::vector<mlir::arith::MulFOp> mul_op;
    

    // std::vector<mlir::arith::AddFOp> all_add_op;
    // std::vector<mlir::arith::MulFOp> all_mul_op;
    using value = std::variant<mlir::arith::AddFOp, mlir::arith::MulFOp,mlir::arith::SubFOp,mlir::arith::DivFOp,mlir::arith::MaxFOp>;
    std::vector<value> current_op;
    std::vector<value> all_current_op;

    using AffineLoopBand = SmallVector<mlir::AffineForOp, 6>;
    using TileList = SmallVector<unsigned, 8>;

};
}

================================================
FILE: include/polyhedral/generator_isl.h
================================================
#ifndef _H_polyfp_function1_
#define _H_polyfp_function1_

#include "expr.h"
#include "type.h"
#include "compute.h"
#include "placeholder.h"
namespace polyfp{
class fucntion;
class generator
{
    friend function;
    friend compute;
    // friend placeholder;


protected:

    /**
     * Compute the accesses of the RHS of the compute
     * \p comp and store them in the accesses vector.
     * If \p return_buffer_accesses is set to true, this function returns access functions to
     * buffers. Otherwise it returns access functions to computes.
     */
    static void get_rhs_accesses(const polyfp::function *func, const polyfp::compute *comp,
                          std::vector<isl_map *> &accesses, bool return_buffer_accesses);

    /**
     * Derived from Tiramisu:
     * Analyze the \p access_expression and return a set of constraints
     * that correspond to the access pattern of the access_expression.
     *
     * access_dimension:
     *      The dimension of the access. For example, the access
     *      C0(i0, i1, i2) have three access dimensions: i0, i1 and i2.
     * access_expression:
     *      The expression of the access.
     *      This expression is parsed recursively (by calling get_constraint_for_access)
     *      and is gradually used to update the constraint.
     * access_relation:
     *      The access relation that represents the access.
     * cst:
     *      The constraint that defines the access and that is being constructed.
     *      Different calls to get_constraint_for_access modify this constraint
     *      gradually until the final constraint is created. Only the final constraint
     *      is added to the access_relation.
     * coeff:
     *      The coefficient in which all the dimension coefficients of the constraint
     *      are going to be multiplied. This coefficient is used to implement o_minus,
     *      o_mul and o_sub.
     */
    static isl_constraint *get_constraint_for_access(int access_dimension,
                                                     const polyfp::expr &access_expression,
                                                     isl_map *access_relation,
                                                     isl_constraint *cst,
                                                     int coeff,
                                                     const polyfp::function *fct);

    /**
     * Derived from Tiramisu:
     * Traverse a polyfp expression (\p exp) and extract the access relations
     * from the access operation passed in \p exp.  The access relations are added
     * to the vector \p accesses.
     * The access relation is from the domain of the compute \p comp to the
     * domain of the compute accessed by the access operation.
     * If \p return_buffer_accesses = true, an access to a buffer is created
     * instead of an access to computes.
     */
    static void traverse_expr_and_extract_accesses(const polyfp::function *fct,
                                            const polyfp::compute *comp,
                                            const polyfp::expr &exp,
                                            std::vector<isl_map *> &accesses,
                                            bool return_buffer_accesses);

public:
    // TODO
};

/**
 * A class containing utility functions.
 */
class utility
{
public:
    /**
     * Derived from Tiramisu:
     * Traverse recursively the ISL AST tree
     * \p node represents the root of the tree to be traversed.
     * \p dim is the dimension of the loop from which the bounds have to be
     * extracted.
     * \p upper is a boolean that should be set to true to extract
     * the upper bound and false to extract the lower bound.
     */
     static expr extract_bound_expression(isl_ast_node *ast, int dim, bool upper);

    /**
     * Derived from Tiramisu:
     * Return a polyfp::expr representing the bound of
     * the dimension \p dim in \p set.  If \p upper is true
     * then this function returns the upper bound otherwise
     * it returns the lower bound.
     *
     * For example, assuming that
     *
     * S = {S[i,j]: 0<=i<N and 0<=j<N and i<M}
     *
     * then
     *
     * get_upper_bound(S, 1)
     *
     * would return N-1, while
     *
     * get_upper_bound(S, 0)
     *
     * would return min(N-1,M-1)
     */
    static polyfp::expr get_bound(isl_set *set, int dim, int upper);

    /**
     * Create a comma separated string that represents the list
     * of the parameters of \p set.
     *
     * For example, if the set is
     *
     * [N,M,K]->{S[i]}
     *
     * this function returns the string "N,M,K".
     */
    static std::string get_parameters_list(isl_set *set);
};


}

#endif

================================================
FILE: include/polyhedral/placeholder.h
================================================
#ifndef _H_polyfp_PLACEHOLDER_
#define _H_polyfp_PLACEHOLDER_
// #include "compute.h"
#include "function.h"
#include "expr.h"
#include <map>

/**
  * A class that represents placeholders.
  *
  * placeholders have two use cases:
  * - used to store the results of computations, and
  * - used to represent input arguments to functions.
  */

namespace polyfp{
  class compute;

static std::string generate_new_p_operator_name()
{
    static int counter = 0;
    return "p" + std::to_string(counter++);
}


class placeholder
{
    friend compute;
    friend function;
    // friend generator;

private:

    /**
      * The sizes of the dimensions of the placeholder.  Assuming the following
      * placeholder buf[N0][N1][N2], dim_sizes should be {N0, N1, N2}.
      */
    std::vector<int64_t> dim_sizes;

    /**
      * The polyfp function where this placeholder is declared or where the
      * placeholder is an argument.
      */
    polyfp::function *fct;

    /**
      * The name of the placeholder.
      * placeholder names should not start with _ (an underscore).
      * Names starting with _ are reserved names.
      */
    std::string name;

    /**
      * The type of the elements of the placeholder.
      */
    polyfp::primitive_t type;



protected:
    /**
     * Set the size of a dimension of the placeholder.
     */
    void set_dim_size(int dim, int size);

public:
    /**
      * \brief Default polyfp constructor
      */
    placeholder();

    /**
      * A polyfp placeholder is equivalent to an array in C.
      *
      * placeholders have two use cases:
      * - Used to store the results of computes, and
      * - Used to represent input arguments to functions.
      *
      * \p name is the name of the placeholder.
      *
      * \p dim_sizes is a vector of polyfp expressions that represent the
      * size of each dimension in the placeholder.
      * Assuming we want to declare the placeholder buf[N0][N1][N2],
      * then the vector of sizes should be {N0, N1, N2}.
      * placeholder dimensions in polyfp have the same semantics as in
      * C/C++.
      *
      * \p type is the type of the elements of the placeholder.
      * It must be a primitive type (i.e. p_uint8, p_uint16, ...).
      * Possible types are declared in \ref polyfp::primitive_t
      * (in type.h).
      *
      * \p fct is a pointer to a polyfp function where the placeholder is
      * declared or used.  If this argument is not provided (which is
      * the common case), the function that was created automatically
      * during polyfp initialization will be used (we call that
      * function the "implicit function").
      */
    placeholder(std::string name, std::vector<int64_t> dim_sizes,
           polyfp::primitive_t type,
           polyfp::function *fct = global::get_implicit_function());

    
    void dump(bool exhaustive) const;

    const std::string &get_name() const;

    // Get the number of dimensions of the placeholder.
    int get_n_dims() const;

    polyfp::primitive_t get_elements_type() const;
    void partition(std::vector<int> factors, std::string type);
    void partition(std::vector<int> factors, std::vector<std::string> type);

    const std::vector<int64_t> &get_dim_sizes() const;

    template<typename... Args> polyfp::expr operator()(Args... args)
    {
        // TODO move to cpp
        std::vector<polyfp::expr> access_expressions{std::forward<Args>(args)...};
        if (access_expressions.size() != this->get_n_dims())
        {
            polyfp::str_dump("Error - Incorrect access: " + this->get_name() + "(");
            for (int i = 0; i < access_expressions.size(); i++)
            {
                polyfp::expr e = access_expressions[i];
                e.dump(false);
                if (i != access_expressions.size() - 1)
                    polyfp::str_dump(", ");
            }
            polyfp::str_dump(").\n");
            polyfp::str_dump("The number of access dimensions does not match that used in the declaration of " + this->get_name() + ".\n\n");
            exit(1);
        }
            return polyfp::expr(polyfp::o_access,
                                  this->get_name(),
                                  access_expressions,
                                  this->get_elements_type());
        // }
    }


    operator expr();


};







}



#endif

================================================
FILE: include/polyhedral/type.h
================================================
#ifndef _H_PolyFP_TYPE_
#define _H_PolyFP_TYPE_

#include <string.h>
#include <stdint.h>

namespace polyfp
{


// Type of expression
enum expr_t
{
    e_val,          // literal value, like 1, 2.4, 10, ...
    e_var,          // a variable of a primitive type (i.e., an identifier holding one value),
    e_op,           // an operation: add, mul, div, ...
    e_none          // undefined expression. The existence of an expression of e_none type means an error.
};

enum primitive_t
{
    p_uint8,
    p_uint16,
    p_uint32,
    p_uint64,
    p_int8,
    p_int16,
    p_int32,
    p_int64,
    p_float32,
    p_float64,
    // p_boolean,
    p_none
};


// Type of operator
enum op_t
{
    o_add,
    o_sub,
    o_mul,
    o_div,
    o_mod,
    o_max,
    o_min,
    o_access,
    o_placeholder,
    o_none,
};

}

#endif


================================================
FILE: lib/CMakeLists.txt
================================================
add_subdirectory(polyhedral)
# add_subdirectory(CAPI)
# add_subdirectory(Standalone)
# add_subdirectory(hello)


================================================
FILE: lib/polyhedral/CMakeLists.txt
================================================
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
# include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
# include_directories(/usr/include/python3.8)
aux_source_directory(. DIR_LIB_SRCS)
# 编译成静态库文件
# add_library(Functions  ${DIR_LIB_SRCS})
add_library(Functions  ${DIR_LIB_SRCS})


================================================
FILE: lib/polyhedral/codegen.cpp
================================================
#include "codegen.h"
namespace polyfp
{

std::vector<compute *> function::get_computation_by_name(std::string name) const
{
    assert(!name.empty());

    std::vector<polyfp::compute *> res_comp;

    for (const auto &comp : this->get_computations())
    {
        if (name == comp->get_name())
        {
            res_comp.push_back(comp);
        }
    }

    if (res_comp.empty())
    {
        polyfp::str_dump("Computation not found.");
    }
    else
    {
        // polyfp::str_dump("Computation found.");
    }

    return res_comp;
}


bool access_is_affine(const polyfp::expr &exp)
{

    // We assume that the access is affine until we find the opposite.
    bool affine = true;

    // Traverse the expression tree and try to find expressions that are non-affine.
    if (exp.get_expr_type() == polyfp::e_val ||
        exp.get_expr_type() == polyfp::e_var)
    {
        affine = true;
    }
    else if (exp.get_expr_type() == polyfp::e_op)
    {
        switch (exp.get_op_type())
        {
            case polyfp::o_access:
            case polyfp::o_placeholder:
                affine = false;
                break;
            case polyfp::o_add:
            case polyfp::o_sub:
                affine = access_is_affine(exp.get_operand(0)) && access_is_affine(exp.get_operand(1));
                break;
            case polyfp::o_max:
            case polyfp::o_min:
            case polyfp::o_mul:
            case polyfp::o_div:
            case polyfp::o_mod:
                break;
            default:
                ERROR("Unsupported polyfp expression passed to access_is_affine().", 1);
        }
    }

    return affine;
}


isl_ast_node *for_code_generator_after_for(isl_ast_node *node, isl_ast_build *build, void *user)
{
    return node;
}








}


================================================
FILE: lib/polyhedral/compute.cpp
================================================
#include "compute.h"
#include "core.h"
#include <algorithm>
namespace polyfp{

isl_ctx *polyfp::compute::get_ctx() const
{
    return ctx;
}

isl_set *polyfp::compute::get_iteration_domain() const
{
    assert(iteration_domain != NULL);
    return iteration_domain;
}

void polyfp::compute::set_iteration_domain(isl_set *domain)
{
    this->iteration_domain = domain;
}

int polyfp::compute::get_iteration_domain_dimensions_number()
{
    assert(iteration_domain != NULL);
    return isl_set_n_dim(this->iteration_domain);
}

isl_map *compute::get_schedule() const
{
    return this->schedule;
}

isl_set *polyfp::compute::get_trimmed_time_processor_domain()
{
    isl_set *tp_domain = isl_set_copy(this->get_time_processor_domain());
    const char *name = isl_set_get_tuple_name(isl_set_copy(tp_domain));
    isl_set *tp_domain_without_duplicate_dim =
        isl_set_project_out(isl_set_copy(tp_domain), isl_dim_set, 0, 1);
    tp_domain_without_duplicate_dim = isl_set_set_tuple_name(tp_domain_without_duplicate_dim, name);
    return tp_domain_without_duplicate_dim ;
}

void compute::name_unnamed_iteration_domain_dimensions()
{
    isl_set *iter = this->get_iteration_domain();
    assert(iter != NULL);
    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)
    {
        if (isl_set_has_dim_name(iter, isl_dim_set, i) == isl_bool_false)
            iter = isl_set_set_dim_name(iter, isl_dim_set, i,
                                        generate_new_variable_name().c_str());
    }
    this->set_iteration_domain(iter);
}

void compute::name_unnamed_time_space_dimensions()
{
    isl_map *sched = this->get_schedule();
    assert(sched != NULL);
    for (int i = 0; i < this->get_loop_levels_number(); i++)
    {
        if (isl_map_has_dim_name(sched, isl_dim_out, loop_level_into_dynamic_dimension(i)) == isl_bool_false)
            sched = isl_map_set_dim_name(sched, isl_dim_out, loop_level_into_dynamic_dimension(i), generate_new_variable_name().c_str());
    }
    this->set_schedule(sched);
}

isl_map *isl_map_add_dim_and_eq_constraint(isl_map *map, int dim_pos, int constant)
{
    assert(map != NULL);
    assert(dim_pos >= 0);
    assert(dim_pos <= (signed int) isl_map_dim(map, isl_dim_out));

    map = isl_map_insert_dims(map, isl_dim_out, dim_pos, 1);
    map = isl_map_set_tuple_name(map, isl_dim_out, isl_map_get_tuple_name(map, isl_dim_in));

    isl_space *sp = isl_map_get_space(map);
    isl_local_space *lsp =
        isl_local_space_from_space(isl_space_copy(sp));
    isl_constraint *cst = isl_constraint_alloc_equality(lsp);
    cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, dim_pos, 1);
    cst = isl_constraint_set_constant_si(cst, (-1) * constant);
    map = isl_map_add_constraint(map, cst);

    return map;
}

isl_map *polyfp::compute::gen_identity_schedule_for_iteration_domain()
{

    isl_space *sp = isl_set_get_space(this->get_iteration_domain());
    isl_map *sched = isl_map_identity(isl_space_map_from_set(sp));
    sched = isl_map_intersect_domain(sched, isl_set_copy(this->get_iteration_domain()));
    sched = isl_map_coalesce(sched);

    for (int i = 0; i < isl_space_dim(sp, isl_dim_out) + 1; i++)
    {
        sched = isl_map_add_dim_and_eq_constraint(sched, 2 * i, 0);
    }

    sched = isl_map_add_dim_and_eq_constraint(sched, 0, 0);
    return sched;
}

void compute::set_schedule(isl_map *map)
{
    this->schedule = map;
}
void compute::set_schedule(std::string map_str)
{
    assert(!map_str.empty());
    assert(this->ctx != NULL);

    isl_map *map = isl_map_read_from_str(this->ctx, map_str.c_str());
    assert(map != NULL);

    this->set_schedule(map);
}

void compute::dump_iteration_domain() const
{
    isl_set_dump(this->get_iteration_domain());

}

void compute::dump_schedule() const
{
    polyfp::str_dump("Dumping the schedule of the computation " + this->get_name() + " : ");
    std::flush(std::cout);
    isl_map_dump(this->get_schedule());

}

const polyfp::expr &polyfp::compute::get_expr() const
{
    return expression;
}

void compute::dump() const
{
    std::cout << std::endl << "Dumping the computation \"" + this->get_name() + "\" :" << std::endl;
    std::cout << "Iteration domain of the computation \"" << this->name << "\" : ";
    std::flush(std::cout);
    isl_set_dump(this->get_iteration_domain());
    std::flush(std::cout);
    this->dump_schedule();

    std::flush(std::cout);
    std::cout << "Expression of the computation : "; std::flush(std::cout);
    this->get_expr().dump(true);
    std::cout << std::endl; std::flush(std::cout);

    std::cout << "Access relation of the computation : "; std::flush(std::cout);
    isl_map_dump(this->get_access_relation());
    if (this->get_access_relation() == NULL)
    {
        std::cout << "\n";
    }
    std::flush(std::cout);

    if (this->get_time_processor_domain() != NULL)
    {
        std::cout << "Time-space domain " << std::endl; std::flush(std::cout);
        isl_set_dump(this->get_time_processor_domain());
    }
    else
    {
        std::cout << "Time-space domain : NULL." << std::endl;
    }

    polyfp::str_dump("\n");
    polyfp::str_dump("\n");
}

void polyfp::compute::set_identity_schedule_based_on_iteration_domain()
{
    isl_map *sched = this->gen_identity_schedule_for_iteration_domain();
    this->set_schedule(sched);
}

std::vector<std::string> compute::get_iteration_domain_dimension_names()
{

    isl_set *iter = this->get_iteration_domain();
    assert(iter != NULL);
    std::vector<std::string> result;
    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)
    {
        if (isl_set_has_dim_name(iter, isl_dim_set, i))
            result.push_back(std::string(isl_set_get_dim_name(iter,
                                                              isl_dim_set, i)));
        else
        {
            ERROR("All iteration domain dimensions must have "
                            "a name.", true);
        }
    }
    assert(result.size() == this->get_iteration_domain_dimensions_number());
    return result;
}

void compute::update_names(std::vector<std::string> original_loop_level_names, std::vector<std::string> new_names,
                               int erase_from, int nb_loop_levels_to_erase)
{
    this->final_loop_level_names.clear();
    this->final_loop_level_names = this->final_loop_level_names_reserved;
    // // std::cout<<"original names: "<<std::endl;
    // for (auto n: original_loop_level_names)
    // {
    //     polyfp::str_dump(n + " ");
    // }
    // // std::cout<<"finial names: "<<std::endl;
    // for (auto n: final_loop_level_names)
    // {
    //     polyfp::str_dump(n + " ");
    // }
    // polyfp::str_dump("Start erasing from: " + std::to_string(erase_from));
    // polyfp::str_dump("Number of loop levels to erase: " + std::to_string(nb_loop_levels_to_erase));

    original_loop_level_names.erase(original_loop_level_names.begin() + 
        erase_from, original_loop_level_names.begin() + erase_from + nb_loop_levels_to_erase);
    final_loop_level_names.erase(final_loop_level_names.begin() + 
        erase_from, final_loop_level_names.begin() + erase_from + nb_loop_levels_to_erase);

    original_loop_level_names.insert(original_loop_level_names.begin() + 
        erase_from, new_names.begin(), new_names.end());
    final_loop_level_names.insert(final_loop_level_names.begin() + 
        erase_from, new_names.begin(), new_names.end());
    
    // // std::cout<<"original names: "<<std::endl;
    // for (auto n: original_loop_level_names)
    // {
    //     polyfp::str_dump(n + " ");
    // }
    // // std::cout<<"finial names: "<<std::endl;
    // for (auto n: final_loop_level_names)
    // {
    //     polyfp::str_dump(n + " ");
    // }
    // this->final_loop_level_names.clear();
    // this->final_loop_level_names = original_loop_level_names;
    this->set_loop_level_names(original_loop_level_names);

}


void polyfp::compute::set_expression(const polyfp::expr &e)
{
    // polyfp::expr modified_e = traverse_expr_and_replace_non_affine_accesses(this, e);
    // polyfp::str_dump("The original expression is: "); modified_e.dump(false);
    this->expression = e.copy();
}

std::vector<std::string> compute::get_loop_level_names()
{
    // polyfp::str_dump("Collecting names of loop levels from the range of the schedule: ", isl_map_to_str(this->get_schedule()));

    std::vector<std::string> names;
    std::string names_to_print_for_debugging = "";

    for (int i = 0; i < this->get_loop_levels_number(); i++)
    {
        std::string dim_name = isl_map_get_dim_name(this->get_schedule(), 
            isl_dim_out, loop_level_into_dynamic_dimension(i));
        names.push_back(dim_name);
        names_to_print_for_debugging += dim_name + " ";
    }
    // polyfp::str_dump("Names of loop levels: " + names_to_print_for_debugging);

    return names;
}

std::vector<polyfp::expr> polyfp::compute::get_placeholder_dims()
{
    return placeholder_dims;
}

void polyfp::compute::set_placeholder_dims(std::vector<polyfp::expr> temp)
{
    this->placeholder_dims = temp ;
}

polyfp::function *polyfp::compute::get_function() const
{
    return fct;
}

int compute::get_loop_levels_number()
{
    assert(this->get_schedule() != NULL);
    int loop_levels_number = ((isl_map_dim(this->get_schedule(), isl_dim_out)) - 2)/2;

    return loop_levels_number;
}
void compute::set_loop_level_names(std::vector<std::string> names)
{
    assert(names.size() > 0);

    // polyfp::str_dump("Number of loop levels: " + std::to_string(this->get_loop_levels_number()));
    // polyfp::str_dump("Number of names to be set: " + std::to_string(names.size()));

    for (int i = 0; i < names.size(); i++)
    {
        if (isl_map_has_dim_name(this->get_schedule(), isl_dim_out, loop_level_into_dynamic_dimension(i)) == isl_bool_true)
        {
            this->schedule = isl_map_set_dim_name(this->get_schedule(),
                                                  isl_dim_out,
                                                  loop_level_into_dynamic_dimension(i),
                                                  names[i].c_str());
        //    polyfp::str_dump("Setting the name of loop level " + std::to_string(i) + " into " + names[i].c_str());
        }
    }

    // polyfp::str_dump("The schedule after renaming: ", isl_map_to_str(this->get_schedule()));

}

void compute::set_loop_level_names(std::vector<int> loop_levels,
        std::vector<std::string> names)
{
    this->check_dimensions_validity(loop_levels);
    assert(names.size() > 0);
    assert(names.size() == loop_levels.size());

    for (int i = 0; i < loop_levels.size(); i++)
    {
        if (loop_level_into_static_dimension(loop_levels[i]) <= isl_map_dim(this->get_schedule(), isl_dim_out))
        {
            this->schedule = isl_map_set_dim_name(this->get_schedule(),
                                                  isl_dim_out,
                                                  loop_level_into_dynamic_dimension(loop_levels[i]),
                                                  names[i].c_str());
            // polyfp::str_dump("Setting the name of loop level " + std::to_string(loop_levels[i]) + " into " + names[i].c_str());
           
        }
    }

    // polyfp::str_dump("The schedule after renaming: ", isl_map_to_str(this->get_schedule()));
}

isl_set *polyfp::compute::get_time_processor_domain() const
{
    return time_processor_domain;
}

void polyfp::compute::set_access(isl_map *access)
{
    assert(access != NULL);
    this->set_access(isl_map_to_str(access));
}


void polyfp::compute::set_access(std::string access_str)
{
    this->access = isl_map_read_from_str(this->ctx, access_str.c_str());

    std::vector<polyfp::compute *> same_name_computations =
        this->get_function()->get_computation_by_name(this->get_name());

    // TODO: Delete
    if (same_name_computations.size() > 1)
        for (auto c : same_name_computations)
        {
            c->access = isl_map_read_from_str(this->ctx, access_str.c_str());
        }

    std::vector<polyfp::compute *> computations =
        this->get_function()->get_computation_by_name(this->get_name());
    for (auto c : computations)
        if (isl_map_is_equal(this->get_access_relation(), c->get_access_relation()) == isl_bool_false)
        {
            ERROR("Computations that have the same name should also have the same access relation.",
                            true);
        }

    assert(this->access != nullptr && "Set access failed");
}

isl_map *polyfp::compute::get_access_relation() const
{
    return access;
}

polyfp::placeholder *polyfp::compute::get_placeholder()
{
    return this->plhd;
}
polyfp::expr polyfp::compute::get_placeholder_expr()
{
    return this->plhd_expr;
}
std::vector<polyfp::expr> compute::compute_buffer_size()
{

    std::vector<polyfp::expr> dim_sizes;

    // If the computation has an update, we first compute the union of all the
    // updates, then we compute the bounds of the union.
    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)
    {
        isl_set *union_iter_domain = isl_set_copy(this->get_iteration_domain());


        // polyfp::str_dump("Extracting bounds of the following set:", isl_set_to_str(union_iter_domain));
        polyfp::expr lower = utility::get_bound(union_iter_domain, i, false);
        polyfp::expr upper = utility::get_bound(union_iter_domain, i, true);
        polyfp::expr diff = (upper - lower + 1);
        dim_sizes.push_back(diff);
    }
    return dim_sizes;
}
std::map<std::string, std::string > compute::get_access_map(){
    return this->access_map;
}
std::map<std::string, std::string > compute::get_tile_map(){
    return this->tile_map;
}
std::map<std::string, int > compute::get_tile_size_map(){
    return this->tile_size_map;
}
std::map<std::string, std::string > compute::get_directive_map(){
    return this->directive_map;;
}
std::map<std::string, std::string > compute::get_directive_tool_map(){
    return this->directive_tool_map;;
}

void polyfp::compute::set_name(const std::string &n)
{
    this->name = n;
}

isl_map *polyfp::compute::gen_identity_schedule_for_time_space_domain()
{
    isl_set *tp_domain = this->get_trimmed_time_processor_domain();
    isl_space *sp = isl_set_get_space(tp_domain);
    isl_map *sched = isl_map_identity(isl_space_map_from_set(sp));
    sched = isl_map_intersect_domain(
                sched, isl_set_copy(this->get_trimmed_time_processor_domain()));
    sched = isl_map_set_tuple_name(sched, isl_dim_out, "");
    sched = isl_map_coalesce(sched);

    return sched;
}

void compute::assert_names_not_assigned(std::vector<std::string> dimensions)
{
    for (auto const dim: dimensions)
    {
        int d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_out,
                                         dim.c_str());
        if (d >= 0)
        {
            ERROR("Dimension " + dim + " is already in use.", true);
        }

        d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_in,
                                     dim.c_str());
        if (d >= 0)
        {
            ERROR("Dimension " + dim + " is already in use.", true);
        }
    }
}

void compute::check_dimensions_validity(std::vector<int> dimensions)
{
    assert(dimensions.size() > 0);

    for (auto const dim: dimensions)
    {
        assert(dim >= compute::root_dimension);

        if (loop_level_into_dynamic_dimension(dim) >=
            isl_space_dim(isl_map_get_space(this->get_schedule()),
                          isl_dim_out))
        {
            ERROR("The dynamic dimension " +
                            std::to_string(loop_level_into_dynamic_dimension(dim)) +
                            " is not less than the number of dimensions of the "
                            "time-space domain " +
                            std::to_string(isl_space_dim(isl_map_get_space(
                                    this->get_schedule()), isl_dim_out)), true);
        }
    }
}

void compute::set_schedule_domain_dim_names(std::vector<int> loop_levels,
        std::vector<std::string> names)
{
    this->check_dimensions_validity(loop_levels);
    assert(names.size() > 0);
    assert(names.size() == loop_levels.size());

    for (int i = 0; i < loop_levels.size(); i++)
    {
        assert(loop_levels[i] <= isl_map_dim(this->get_schedule(), isl_dim_in));
        this->schedule = isl_map_set_dim_name(this->get_schedule(),
                                              isl_dim_in, loop_levels[i], names[i].c_str());
    }

    // polyfp::str_dump("The schedule after renaming: ", isl_map_to_str(this->get_schedule()));
}

void polyfp::compute::init_computation(std::string iteration_space_str,
        polyfp::function *fction,
        const polyfp::expr &e,
        polyfp::primitive_t t, polyfp::expr p)
{
    // polyfp::str_dump("Constructing the computation: " + iteration_space_str);
    assert(iteration_space_str.length() > 0 && ("Empty iteration space"));
    access = NULL;
    time_processor_domain = NULL;
    predicate = polyfp::expr();
    this->data_type = t;
    this->ctx = fction->get_isl_ctx();
    //todo
    for(auto &kv : fction->get_placeholders()){
        if(kv.first == p.get_name())
            this->plhd = kv.second;
    }
    this->plhd_expr = p;
    this->plhd_expr.owner = this;

    placeholder_dims = p.get_access();
    iteration_domain = isl_set_read_from_str(ctx, iteration_space_str.c_str());
    //TODO
    name = std::string(isl_space_get_tuple_name(isl_set_get_space(iteration_domain),
                       isl_dim_type::isl_dim_set));

    number_of_dims = isl_set_dim(iteration_domain, isl_dim_type::isl_dim_set);

    // for (unsigned i = 0; i < number_of_dims; i++) {
    //     if (isl_set_has_dim_name(iteration_domain, isl_dim_type::isl_dim_set, i)) {
    //         std::string dim_name(isl_set_get_dim_name(iteration_domain, isl_dim_type::isl_dim_set, i));
    //         this->access_variables.push_back(make_pair(i, dim_name));
           
    //     }
    // }
    // for(auto &kv: access_variables){
    //     // std::cout<<std::to_string(kv.first)<<kv.second<<std::endl;
    // }
    fct = fction;
    this->is_leader = true;
    this->is_top_parent = true;
    this->is_leaf = true;
    this->has_a_leader = false;
    fct->leader_computations.push_back(this);
    this->after_level = -2;
    fct->add_computation(this);
    this->set_identity_schedule_based_on_iteration_domain();
    this->set_expression(e);

    std::vector<std::string> nms = this->get_iteration_domain_dimension_names();

    for (int i = 0; i< this->get_iteration_domain_dimensions_number(); i++)
        this->set_schedule_domain_dim_names({i}, {generate_new_variable_name()});
    for (int i = 0; i< nms.size(); i++){
        this->set_loop_level_names({i}, {nms[i]});
        this->final_loop_level_names.push_back(nms[i]);
        this->final_loop_level_names_reserved.push_back(nms[i]);
        // if(fct->get_body().size() == 1){
        //     this->iterators_location_map.insert(std::make_pair(nms[i],i));
        //     fct->global_location = nms.size();
        // }  
    }

}

compute::compute(std::string name, std::vector<polyfp::var> iterator_variables, polyfp::expr e, primitive_t t, expr p)
{   
    this->iteration_variables = iterator_variables;
    std::string iteration_space_str = construct_iteration_domain(name, iterator_variables, predicate);
    // std::cout<<iteration_space_str<<std::endl;
    init_computation(iteration_space_str, global::get_implicit_function(), e, t, p);
}

compute::compute(std::string iteration_domain_str, polyfp::expr e,
                         polyfp::primitive_t t,
                         polyfp::function *fct, expr p)
{
    init_computation(iteration_domain_str, fct, e, t, p);
}

compute::compute(std::string name, std::vector<polyfp::var> iterator_variables, polyfp::expr e, expr p)
    : compute(name, iterator_variables, e, p_float64, p) {}
compute::compute(std::string name, std::vector<polyfp::var> iterator_variables, int a, expr p)
    : compute(name, iterator_variables, expr((uint16_t) a), p_float64, p) {}

std::vector<polyfp::var> compute::get_iteration_variables()
{
    return this->iteration_variables;
}

std::string polyfp::compute::construct_iteration_domain(std::string name, std::vector<var> iterator_variables, polyfp::expr predicate)
{
    polyfp::function *function = global::get_implicit_function();

    std::string iteration_space_str = "";
    std::string comp_name = name;

    iteration_space_str += "{" + comp_name + "[";
    if (iterator_variables.size() == 0)
        iteration_space_str += "0";
    else
        for (int i = 0; i < iterator_variables.size(); i++)
        {
            var iter = iterator_variables[i];
            iteration_space_str += iter.get_name();
            if (i < iterator_variables.size() - 1)
                iteration_space_str += ", ";
        }

    iteration_space_str += "] ";

    if (iterator_variables.size() != 0)
        iteration_space_str += ": ";

    if (predicate.is_defined())
	 iteration_space_str += predicate.to_str() + " and ";

    bool insert_and = false;
    for (int i = 0; i < iterator_variables.size(); i++)
    {
        var iter = iterator_variables[i];

        if ((insert_and == true && (iter.lower.is_defined() || iter.upper.is_defined())))
        {
            iteration_space_str += " and ";
            insert_and = false;
        }

        if (iter.lower.is_defined() || iter.upper.is_defined())
        {
            iteration_space_str += iter.lower.to_str() + "<=" + iter.get_name() + "<" + iter.upper.to_str();
            insert_and = true;
        }
    }

    iteration_space_str += "}";

    return iteration_space_str;
}


const std::string &polyfp::compute::get_name() const
{
    return name;
}

polyfp::primitive_t polyfp::compute::get_data_type() const
{
    return data_type;
}

std::vector<int> compute::get_loop_level_numbers_from_dimension_names(
        std::vector<std::string> dim_names)
{
    assert(dim_names.size() > 0);
    std::vector<int> dim_numbers;

    for (auto const dim: dim_names)
    {
        assert(dim.size()>0);
        if (dim == "root")
        {
            int d = compute::root_dimension;
            dim_numbers.push_back(d);
        }
        else
        {
            int d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_out,
                                             dim.c_str());
            // polyfp::str_dump("Searching in the range of ", isl_map_to_str(this->get_schedule()));
            if (d < 0)
            {
                ERROR("Dimension " + dim + " not found.", true);
            }

            // polyfp::str_dump("Corresponding loop level is " + std::to_string(dynamic_dimension_into_loop_level(d)));
            dim_numbers.push_back(dynamic_dimension_into_loop_level(d));
        }
    }
    this->check_dimensions_validity(dim_numbers);
    return dim_numbers;
}

struct param_pack_1
{
    int in_dim;
    int out_constant;
};

/**
 * Derived from Tiramisu:
 * Take a basic map as input, go through all of its constraints,
 * identifies the constraint of the static dimension param_pack_1.in_dim
 * (passed in user) and replace the value of param_pack_1.out_constant if
 * the static dimension is bigger than that value.
 */
isl_stat extract_static_dim_value_from_bmap(__isl_take isl_basic_map *bmap, void *user)
{
    struct param_pack_1 *data = (struct param_pack_1 *) user;

    isl_constraint_list *list = isl_basic_map_get_constraint_list(bmap);
    int n_constraints = isl_constraint_list_n_constraint(list);

    for (int i = 0; i < n_constraints; i++)
    {
        isl_constraint *cst = isl_constraint_list_get_constraint(list, i);
        isl_val *val = isl_constraint_get_coefficient_val(cst, isl_dim_out, data->in_dim);
        if (isl_val_is_one(val)) // i.e., the coefficient of the dimension data->in_dim is 1
        {
            isl_val *val2 = isl_constraint_get_constant_val(cst);
            int const_val = (-1) * isl_val_get_num_si(val2);
            data->out_constant = const_val;
        }
    }

    return isl_stat_ok;
}

// Derived from Tiramisu:
// if multiple const values exist, choose the maximal value among them because we
// want to use this value to know by how much we shift the computations back.
// so we need to figure out the maximal const value and use it to shift the iterations
// backward so that that iteration runs before the consumer.
isl_stat extract_constant_value_from_bset(__isl_take isl_basic_set *bset, void *user)
{
    struct param_pack_1 *data = (struct param_pack_1 *) user;

    isl_constraint_list *list = isl_basic_set_get_constraint_list(bset);
    int n_constraints = isl_constraint_list_n_constraint(list);

    for (int i = 0; i < n_constraints; i++)
    {
        isl_constraint *cst = isl_constraint_list_get_constraint(list, i);
        if (isl_constraint_is_equality(cst) &&
                isl_constraint_involves_dims(cst, isl_dim_set, data->in_dim, 1))
        {
            isl_val *val = isl_constraint_get_coefficient_val(cst, isl_dim_out, data->in_dim);
            assert(isl_val_is_one(val));
            // assert that the coefficients of all the other dimension spaces are zero.
            isl_val *val2 = isl_constraint_get_constant_val(cst);
            int const_val = (-1) * isl_val_get_num_si(val2);
            data->out_constant = std::max(data->out_constant, const_val);
        }
    }
    return isl_stat_ok;
}


/**
 * Derived from Tiramisu:
 * Return the value of the static dimension.
 * For example, if we have a map M = {S0[i,j]->[0,0,i,1,j,2]; S0[i,j]->[1,0,i,1,j,3]}
 * and call isl_map_get_static_dim(M, 5, 1), it will return 3.
 */
int isl_map_get_static_dim(isl_map *map, int dim_pos)
{
    assert(map != NULL);
    assert(dim_pos >= 0);
    assert(dim_pos <= (signed int) isl_map_dim(map, isl_dim_out));

    // polyfp::str_dump("Getting the constant coefficient of ", isl_map_to_str(map));
    // polyfp::str_dump(" at dimension ");
    // polyfp::str_dump(std::to_string(dim_pos));
    struct param_pack_1 *data = (struct param_pack_1 *) malloc(sizeof(struct param_pack_1));
    data->out_constant = 0;
    data->in_dim = dim_pos;
    isl_map_foreach_basic_map(isl_map_copy(map),
                              &extract_static_dim_value_from_bmap,
                              data);
    // polyfp::str_dump("The constant is: ");
    // polyfp::str_dump(std::to_string(data->out_constant));
    return data->out_constant;
}

std::vector<polyfp::expr> compute::get_loads(){
    auto expr = this->get_expr();
    std::vector<polyfp::expr > loads;
    expr.get_access_vector(loads);  
    return loads;
}

void compute::get_loads_stores()
{
    auto s_loads =this->get_loads();
    std::map<std::string, std::vector<polyfp::expr>> s_single_ls;
    std::vector<polyfp::expr> s_stores;

    s_stores.push_back(this->get_placeholder_expr());
    s_single_ls.insert(std::pair("load", s_loads));
    s_single_ls.insert(std::pair("store", s_stores));
    this->map_loadstores.insert(std::pair(-1,s_single_ls));

    for (auto &edge: this->components)
    {   
        // std::cout<<"dump_component:"+edge.first->get_name()<<std::endl;
        auto loads = edge.first->get_loads();
        std::map<std::string, std::vector<polyfp::expr>> single_ls;
        std::vector<polyfp::expr> stores;
        stores.push_back(edge.first->get_placeholder_expr());
        // std::cout<<"component:"+std::to_string(loads.size())<<std::endl;
        // std::cout<<"component:"+edge.first->get_placeholder_expr().get_name()<<std::endl;
        single_ls.insert(std::pair("load", loads));
        single_ls.insert(std::pair("store", stores));
        this->map_loadstores.insert(std::pair(edge.second,single_ls));
    }
}

void compute::get_all_loadstores()
{
    this->get_loads_stores();
    for(auto &level: this->map_loadstores){
        for(auto &map: level.second){
            if(map.first == "load"){
                for(auto &op: map.second){
                    if(this->load_map.find(op.get_name()) == this->load_map.end()){
                        this->load_map.insert(std::pair(op.get_name(), &op));
                    }
                    this->load_vector.push_back(&op);
                    // // std::cout<<"load_vector:"+op.get_name()<<std::endl;            
                }
                
            }else if(map.first == "store"){
                for(auto &op: map.second){
                    if(this->store_map.find(op.get_name()) == this->store_map.end()){
                        this->store_map.insert(std::pair(op.get_name(), &op));
                    }
                    this->store_vector.push_back(&op);
                }
            }
        }
    }
}


void compute::check_loop_interchange(){
    bool is_legal = true;
    // std::map<int, polyfp::expr> new_order_map;
    std::map<polyfp::expr * , std::vector<polyfp::expr *> > new_order_map ;
    std::unordered_map<std::string, int> final_dim_order;
    std::vector<std::string> dims_no_dp;

    // std::cout<<"check_loop_interchange:"<<std::endl;
    for(auto &vector_list : this->map_dependence_vectors)
    {
        std::vector<std::string> dims_no_dp;
        auto vectors = vector_list.second;
        auto dim_list = vector_list.first->get_access();
        std::unordered_map<polyfp::expr *, int> new_vector_map;
        for(auto &vector: vectors)
        {
            int dims = vector.size();
            bool has_zero = false;
            int zero_number = 0;
            for(int i=0; i<dims; i++){
                if(vector[i] == 0)
                {
                    has_zero = true;
                    zero_number += 1;
                    if (dims_no_dp.size()==0)
                    {
                        if(dim_list[i].get_expr_type() == polyfp::e_op)
                        {
                            if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var){
                                dims_no_dp.push_back(dim_list[i].get_operand(0).get_name());
                            }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var){
                                dims_no_dp.push_back(dim_list[i].get_operand(1).get_name());
                            }
                        }
                        else{
                            dims_no_dp.push_back(dim_list[i].get_name());
                        }
                    }
                    else{                       
                        std::string name;
                        if(dim_list[i].get_expr_type()==polyfp::e_op)
                        {
                            if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var){
                                name = dim_list[i].get_operand(0).get_name();
                                // dims_no_dp.push_back(dim_list[i].get_operand(0).get_name());
                            }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var){
                                name = dim_list[i].get_operand(0).get_name();
                                // dims_no_dp.push_back(dim_list[i].get_operand(1).get_name());
                            }
                            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),name);
                            if ( iter==dims_no_dp.end()){
                                dims_no_dp.push_back(name);
                            }
                        }else{
                            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),dim_list[i].get_name());
                            if ( iter==dims_no_dp.end()){
                                dims_no_dp.push_back(dim_list[i].get_name());
                            }
                        }
                    }                  
                }
                if(vector[i] < 0)
                {
                    has_zero = true;
                    zero_number += 1;
                    is_legal = false;
                }
                if(vector[i] > 0)
                {
                    has_zero = true;
                    zero_number += 1;
                    std::string dim_name;
                    polyfp::expr temp_dim;
                    if(dim_list[i].get_expr_type()==polyfp::e_op)
                    {
                        if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var)
                        {
                            dim_name = dim_list[i].get_operand(0).get_name();
                            temp_dim = dim_list[i].get_operand(0);
                        }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var)
                        {
                            dim_name = dim_list[i].get_operand(1).get_name();
                            temp_dim = dim_list[i].get_operand(1);
                        }
                    }else
                    {
                        dim_name = dim_list[i].get_name();
                        temp_dim = dim_list[i];
                    }
                    if (new_vector_map.find(&temp_dim) == new_vector_map.end())
                    {
                        new_vector_map[&temp_dim] = vector[i];
                    }else if(new_vector_map[&temp_dim]>vector[i]){
                        new_vector_map[&temp_dim] = vector[i];
                    }
                    std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),dim_name);
                    if ( iter!=dims_no_dp.end())
                    {
                        iter = dims_no_dp.erase(iter);
                    }
                }
                
            }
            
        }
        std::vector<std::pair<polyfp::expr *, int>> tmp;
        std::vector<polyfp::expr *> dim_order;
        for (auto& i : new_vector_map)
            tmp.push_back(i);
        
        std::sort(tmp.begin(), tmp.end(), [=](std::pair<polyfp::expr *, int>& a, std::pair<polyfp::expr *, int>& b) { return a.second < b.second; });
        // other dims should be moved to outer level first.
        // std::cout<<"new_vector_map:"+ std::to_string(new_vector_map.size())<<std::endl;
        for(auto &other_dim:this->get_iteration_variables())
        {
            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),other_dim.get_name());
            if(new_vector_map.find(&other_dim) == new_vector_map.end()&& iter==dims_no_dp.end())
            {
                dim_order.push_back(&other_dim);
            }
        }
        // move dims that have loop carried dependencies.
        // remove all elements with value val
        for(auto &dim: tmp)
        {
            dim_order.push_back(dim.first);
        }

        new_order_map[vector_list.first] = dim_order;

        for(auto &kv :new_vector_map)
        {
            if (final_dim_order.find(kv.first->get_name()) == final_dim_order.end()&&final_dim_order.size()<this->get_iteration_domain_dimensions_number())
            {
                final_dim_order[kv.first->get_name()] = kv.second;
            }else if(final_dim_order[kv.first->get_name()]>kv.second)
            {
                final_dim_order[kv.first->get_name()] = kv.second;
            }

        }
    }
    // Decide a common order and detect conflicts
    // Define confilct: for all comps in the nested loop, 
    // number of dims that need to be interchanged should not exceed total dims number-1
    // gradually add computes until conflicts occur
    std::vector<std::string> waiting_list;
    bool need_split = false;
    polyfp::compute *comp_to_split ;
    
    for(auto &dvectors: new_order_map)
    {
        for(auto &dvector:dvectors.second)
        {
            std::vector<std::string>::iterator iter=find(waiting_list.begin(),waiting_list.end(),dvector->get_name());
            if ( iter==waiting_list.end())
            {
                waiting_list.push_back(dvector->get_name());
            }
            if(waiting_list.size() == this->get_iteration_domain_dimensions_number())
            {
                need_split = true;
                comp_to_split = dvectors.first->owner;
            }
        }
    }

    if(need_split == true && is_legal == true)
    {
        // TODO: if there is no dependency between comp_to_split and 
        // other comps(its leader and component), split it from the nested loop
        int top_level = 0;
        // for(auto &dim: waiting_list){
        //     int level = this->get_loop_level_number_from_dimension_name(dim);
        //     if(level!=0){
        //          comp_to_split->interchange(top_level,level);
        //     }
        // }
        // comp_to_split->after(comp_to_split->leader,comp_to_split->leader->get_iteration_domain_dimensions_number()-1);
        comp_to_split->after(comp_to_split->leader, -1);
        comp_to_split->get_all_loadstores();
        // comp->dump_components();
        // comp->dump_loads_stores();
        comp_to_split->dump_all_loadstores();
        comp_to_split->compute_dependence_vectors();
        comp_to_split->check_loop_interchange();

    }
    if(need_split == false)
    {
        int top_level = 0;
        for(auto &dim: waiting_list)
        {
            int level = this->get_loop_level_number_from_dimension_name(dim);
            this->interchange(top_level,level);
            int count = level-top_level-1;
            if(count!=0){
                for(int i=0; i<count;i++){
                    this->interchange(top_level+1+i,level);
                }
            }
            top_level+=1;
        }
        for(auto &map: this->components)
        {
            int dims = map.first->get_iteration_variables().size();
            if(map.first->after_level==dims-1)
            {
                int top_level2 = 0;
                for(auto &dim: waiting_list)
                {
                    int level = map.first->get_loop_level_number_from_dimension_name(dim);
                    //TODO: Potential bugs here
                    map.first->interchange(top_level2,level);
                    int count = level-top_level2-1;
                    if(count!=0)
                    {
                        for(int i=0; i<count;i++){
                            map.first->interchange(top_level2+1+i,level);
                        }
                    }
                    map.first->after(map.first->leader,this->get_iteration_domain_dimensions_number()-1);
                    top_level2+=1;
                }
            }
        }
    }
}

void compute::check_loop_skewing()
{
    bool is_legal = false;
    int factor=1;
    if(this->map_dependence_vectors.size() == 1)
    {
        for(auto &vector_list : this->map_dependence_vectors)
        {
            std::vector<std::string> dims_no_dp;
            auto vectors = vector_list.second;
            auto dim_list = vector_list.first->get_access();
            std::unordered_map<polyfp::expr *, int> new_vector_map;
            // TODO: factors
            if(8>=vectors.size()&&vectors.size()>=2)
            {
                is_legal = true;
                factor = 1;
            }
            else if(8<=vectors.size()){
                is_legal = true;
                factor = 2;
            }else{
                // TODO
            }
        }

        auto iterators = this->get_iteration_variables();
        int size = iterators.size();
        std::map<int,polyfp::var> iterator_map;

        for(auto &iter: iterators)
        {
            int loc = this->get_loop_level_number_from_dimension_name(iter.get_name());
            // std::cout<<iter.get_name()<<std::endl;
            iterator_map[loc] = iter;
        }
        var i0("i0"), j0("j0"),k0("k0"), i1("i1"), j1("j1"),k1("k1");
        
        if(is_legal == true)
        {
            if(size==3)
            {
                this->skew(iterator_map[1],iterator_map[2],1,factor,i0,j0);
            }else if(size==2)
            {
                this->skew(iterator_map[0],iterator_map[1],1,factor,i0,j0);
            }
            this->is_skewed_inDSE = true;
        }
    }
}

void compute::auto_loop_transformation()
{
    this->check_loop_interchange();
    this->check_loop_skewing();
}

void compute::apply_opt_strategy(std::vector<int> tile_size){
    std::map<int,polyfp::var> iterator_map;
    
    this->set_schedule(this->original_schedule);
    this->set_loop_level_names(this->original_loop_level_name);
    this->directive_map.clear();
    this->is_unrolled = false;
    this->unroll_factor.clear();
    this->unroll_dimension.clear();
    this->tile_map.clear();
    this->tile_size_map.clear();
    this->access_map.clear();
    auto iterators = this->get_iteration_variables();
    int size = iterators.size();
    //TODO: SKEW MAP
    for(auto &iter: iterators)
    {
        int loc = this->get_loop_level_number_from_dimension_name(iter.get_name());
        iterator_map[loc] = iter;
    }

    if(size >= 3)
    {
        var i0("i0"), j0("j0"),k0("k0"), i1("i1"), j1("j1"),k1("k1");
        // TODO: Config file
        if(tile_size[0]<=64 && tile_size[1]<64 && tile_size[2]<64)
        {
            int temp_index = this->get_iteration_variables().size()-3;

            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1)
            {  
                // TODO   
            }else
            {
                this->tile(iterator_map[temp_index],iterator_map[temp_index+1],
                    iterator_map[temp_index+2],tile_size[0],tile_size[1],tile_size[2],i0, j0, k0, i1, j1, k1);
            }
            // std::cout<<iterator_map[temp_index].get_name()<<std::endl;
            if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]!=1)
            {
                this->pipeline(k0,1);
                this->unroll(k1,-1);
                this->unroll(j1,-1);
                this->unroll(i1,-1);
            }
            if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]==1)
            {
                this->pipeline(k0,1);
                this->unroll(k1,-1);
                this->unroll(j1,-1);
            }
            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]!=1)
            {
                this->pipeline(iterator_map[temp_index+2],1);
                // comp->unroll(k1,-1);
                // comp->unroll(j1,-1);
                this->unroll(i1,-1);
            }
            if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]!=1)
            {
                this->pipeline(k0,1);
                this->unroll(k1,-1);
                this->unroll(i1,-1);
            }
            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1)
            {
                int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());
                int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());
                int range = upper-lower;
                // TODO: Config
                if(range<=7)
                {
                    this->pipeline(iterator_map[temp_index+1],1);
                    this->unroll(iterator_map[temp_index+2],-1);
                }
            }
            if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]==1)
            {
                this->pipeline(k0,1);
                this->unroll(k1,-1);
            }
            if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1)
            {
                int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());
                int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());
                int range = upper-lower;
                if(range<=6)
                {
                    this->pipeline(j0,1);
                    this->unroll(j1,-1);
                    this->unroll(i1,-1);
                    this->unroll(iterator_map[temp_index+2],-1);
                }else
                {
                    this->pipeline(iterator_map[temp_index+2],1);
                    this->unroll(j1,-1);
                    this->unroll(i1,-1);
                }
            }
            for(auto &part:this->components)
            {
                part.first->set_schedule(part.first->original_schedule);
                part.first->set_loop_level_names(part.first->original_loop_level_name);
                part.first->tile(iterator_map[temp_index+0],iterator_map[temp_index+1],
                    iterator_map[temp_index+2],tile_size[0],tile_size[1],tile_size[2],i0, j0, k0, i1, j1, k1);

                if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1)
                {
                    if(part.first->after_level == 2)
                    {
                        part.first->after(this,j1);
                    }
                    else if(part.first->after_level == 0)
                    {
                        part.first->after(this,i0);
                        part.first->pipeline(iterator_map[temp_index+2],1);   
                    }
                }else
                {
                    if(part.first->after_level == 2)
                    {
                        part.first->after(this,k1);
                    }
                    else if
                    (part.first->after_level == 0){
                        part.first->after(this,iterator_map[temp_index+0]);
                        part.first->pipeline(iterator_map[temp_index+2],1);   
                        //TODO
                        part.first->unroll(k1,-1);
                        part.first->unroll(j1,-1);
                    }
                }
            }
        }
    }
    else if(size == 2)
    {
        var i0("i0"), j0("j0"), i1("i1"), j1("j1");
        // TODO: Config file
        if(tile_size[0]<64 && tile_size[1]<64)
        {
            this->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);
            if(tile_size[1]!=1&&tile_size[0]!=1)
            {
                this->pipeline(j0,1);
                this->unroll(j1,-1);
                this->unroll(i1,-1);
            }else if(tile_size[1]==1&&tile_size[0]!=1)
            {
                this->pipeline(iterator_map[1],1);
                this->unroll(i1,-1);
            }else if(tile_size[0]==1&&tile_size[1]!=1)
            {
                this->pipeline(j0,1);
                this->unroll(j1,-1);
            }
            for(auto &part:this->components)
            {
                part.first->set_schedule(part.first->original_schedule);
                part.first->set_loop_level_names(part.first->original_loop_level_name);
                part.first->directive_map.clear();
                part.first->is_unrolled = false;
                part.first->unroll_factor.clear();
                part.first->unroll_dimension.clear();
                part.first->tile_map.clear();
                part.first->tile_size_map.clear();
                part.first->access_map.clear();
                part.first->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);

                if(tile_size[1]!=1&&tile_size[0]!=1)
                {
                    if(part.first->after_level == 1)
                    {
                        part.first->after(this,j1);
                    }
                    else if(part.first->after_level == 0)
                    {
                        part.first->after(this,i0);
                        part.first->pipeline(j0,1);
                    }
                    
                }
                else if(tile_size[1]==1&&tile_size[0]!=1)
                {
                    if(part.first->after_level == 1)
                    {
                        part.first->after(this,i1);
                    }
                    else if(part.first->after_level == 0)
                    {
                        part.first->pipeline(iterator_map[1],1);
                        part.first->after(this,i0);
                    }                  
                }
                else if(tile_size[0]==1&&tile_size[1]!=1)
                {
                    if(part.first->after_level == 1)
                    {
                        part.first->after(this,j1);                                  
                    }
                    else if(part.first->after_level == 0)
                    {
                        part.first->after(this,iterator_map[0]);
                        part.first->pipeline(j0,1);
                        part.first->unroll(j1,-1);
                    }
                    else if(part.first->after_level == 2)
                    {
                        part.first->after(this,j1);
                    }
                }
            }
        } 
    }
}



void compute::compute_dependence_vectors()
{
    for(auto &store: this->store_vector)
    {
        auto store_index = store->get_access();
        auto dims = store_index.size();
        for(auto &load: this->load_vector)
        {
            auto load_index = load->get_access();
            if(store->get_name() == load->get_name())
            {
                // std::cout<<"array " + store->get_name()<<std::endl;
                std::vector<int> vector_set;
                for(int i = 0; i < dims; i++)
                {
                    auto vector_element = store_index[i].get_dependence_vector()-load_index[i].get_dependence_vector();
                    // std::cout<<"vector of dimension " + std::to_string(i)+"is: "+std::to_string(vector_element)<<std::endl;
                    vector_set.push_back(vector_element);

                }
                this->map_dependence_vectors[store].push_back(vector_set);
            }
        }
    }
}

void compute::dump_all_loadstores()
{
    std::string result1 = "loads:";
    std::string result2 = "stores:";
    for(auto &op: this->load_map){
        result1 += op.first +" ";
    }
    for(auto &op: this->store_map){
        result2 += op.first +" ";
    }
}

void compute::interchange(polyfp::var L0_var, polyfp::var L1_var)
{
    assert(L0_var.get_name().length() > 0);
    assert(L1_var.get_name().length() > 0);
    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name(), L1_var.get_name()});

    this->check_dimensions_validity(dimensions);
    int L0 = dimensions[0];
    int L1 = dimensions[1];

    this->interchange(L0, L1);
}

void compute::interchange(int L0, int L1)
{
    int inDim0 = loop_level_into_dynamic_dimension(L0);
    int inDim1 = loop_level_into_dynamic_dimension(L1);

    assert(inDim0 >= 0);
    assert(inDim0 < isl_space_dim(isl_map_get_space(this->get_schedule()),
                                  isl_dim_out));
    assert(inDim1 >= 0);
    assert(inDim1 < isl_space_dim(isl_map_get_space(this->get_schedule()),
                                  isl_dim_out));

    isl_map *schedule = this->get_schedule();

    // polyfp::str_dump("Original schedule: ", isl_map_to_str(schedule));
    // polyfp::str_dump("Interchanging the dimensions " + std::to_string(
    //                                 L0) + " and " + std::to_string(L1));

    int n_dims = isl_map_dim(schedule, isl_dim_out);

    std::string inDim0_str = isl_map_get_dim_name(schedule, isl_dim_out, inDim0);
    std::string inDim1_str = isl_map_get_dim_name(schedule, isl_dim_out, inDim1);

    std::vector<isl_id *> dimensions;

    // Create a map for the duplicate schedule.
    std::string map = "{ " + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            int duplicate_ID = isl_map_get_static_dim(schedule, 0);
            map = map + std::to_string(duplicate_ID);
        }
        else
        {
            if (isl_map_get_dim_name(schedule, isl_dim_out, i) == NULL)
            {
                isl_id *new_id = isl_id_alloc(this->get_ctx(), generate_new_variable_name().c_str(), NULL);
                schedule = isl_map_set_dim_id(schedule, isl_dim_out, i, new_id);
            }

            map = map + isl_map_get_dim_name(schedule, isl_dim_out, i);
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }

    map = map + "] ->" + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            int duplicate_ID = isl_map_get_static_dim(schedule, 0);
            map = map + std::to_string(duplicate_ID);
        }
        else
        {
            if ((i != inDim0) && (i != inDim1))
            {
                map = map + isl_map_get_dim_name(schedule, isl_dim_out, i);
                dimensions.push_back(isl_map_get_dim_id(schedule, isl_dim_out, i));
            }
            else if (i == inDim0)
            {
                map = map + inDim1_str;
                isl_id *id1 = isl_id_alloc(this->get_ctx(), inDim1_str.c_str(), NULL);
                dimensions.push_back(id1);
            }
            else if (i == inDim1)
            {
                map = map + inDim0_str;
                isl_id *id1 = isl_id_alloc(this->get_ctx(), inDim0_str.c_str(), NULL);
                dimensions.push_back(id1);
            }
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }
    map = map + "]}";
    // polyfp::str_dump(map.c_str());

    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());

    transformation_map = isl_map_set_tuple_id(
                             transformation_map, isl_dim_in, isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));
    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);
    transformation_map = isl_map_set_tuple_id(
                             transformation_map, isl_dim_out, id_range);

    // Check that the names of each dimension is well set
    for (int i = 1; i < isl_map_dim(transformation_map, isl_dim_in); i++)
    {
        isl_id *dim_id = isl_id_copy(dimensions[i - 1]);
        transformation_map = isl_map_set_dim_id(transformation_map, isl_dim_out, i, dim_id);
        assert(isl_map_has_dim_name(transformation_map, isl_dim_in, i));
        assert(isl_map_has_dim_name(transformation_map, isl_dim_out, i));
    }
    // polyfp::str_dump("Final transformation map : ", isl_map_to_str(transformation_map));

    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));

    // polyfp::str_dump("Schedule after interchange: ", isl_map_to_str(schedule));

    this->set_schedule(schedule);
}




void compute::split(polyfp::var L0_var, int sizeX)
{
    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());
    this->split(L0_var, sizeX, L0_outer, L0_inner);
}

void compute::split(polyfp::var L0_var, int sizeX,
        polyfp::var L0_outer, polyfp::var L0_inner)
{
    // polyfp::str_dump("Schedule after interchange: ");
    assert(L0_var.get_name().length() > 0);

    std::vector<std::string> original_loop_level_names =
        this->get_loop_level_names();

    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name()});
    // polyfp::str_dump("Scget_loop_level_numbers_from_dimension_nameshedule after interchange: ");
    this->check_dimensions_validity(dimensions);
    int L0 = dimensions[0];
    this->assert_names_not_assigned({L0_outer.get_name(), L0_inner.get_name()});
    this->split(L0, sizeX);
    this->set_loop_level_names({L0_outer.get_name(), L0_inner.get_name()});
    // this->update_names(original_loop_level_names, {L0_outer.get_name(), L0_inner.get_name()}, L0, 1);
    // polyfp::str_dump(L0_outer.get_name());
    // polyfp::str_dump(L0_inner.get_name());
}

void compute::split(int L0, int sizeX)
{
    int inDim0 = loop_level_into_dynamic_dimension(L0);

    assert(this->get_schedule() != NULL);
    assert(inDim0 >= 0);
    assert(inDim0 < isl_space_dim(isl_map_get_space(this->get_schedule()), isl_dim_out));
    assert(sizeX >= 1);

    isl_map *schedule = this->get_schedule();
    int duplicate_ID = isl_map_get_static_dim(schedule, 0);

    schedule = isl_map_copy(schedule);
    schedule = isl_map_set_tuple_id(schedule, isl_dim_out,
                                    isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL));

    // polyfp::str_dump("Original schedule: ", isl_map_to_str(schedule));
    // polyfp::str_dump("Splitting dimension " + std::to_string(inDim0)
    //                             + " with split size " + std::to_string(sizeX));

    std::string inDim0_str;

    std::string outDim0_str = generate_new_variable_name();
    std::string static_dim_str = generate_new_variable_name();
    std::string outDim1_str = generate_new_variable_name();

    int n_dims = isl_map_dim(this->get_schedule(), isl_dim_out);
    std::vector<isl_id *> dimensions;
    std::vector<std::string> dimensions_str;
    std::string map = "{";

    map = map + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            std::string dim_str = generate_new_variable_name();
            dimensions_str.push_back(dim_str);
            map = map + dim_str;
        }
        else
        {
            std::string dim_str = generate_new_variable_name();
            dimensions_str.push_back(dim_str);
            map = map + dim_str;

            if (i == inDim0)
            {
                inDim0_str = dim_str;
            }
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }

    map = map + "] -> " + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            map = map + dimensions_str[i];
            dimensions.push_back(isl_id_alloc(
                                     this->get_ctx(),
                                     dimensions_str[i].c_str(),
                                     NULL));
        }
        else if (i != inDim0)
        {
            map = map + dimensions_str[i];
            dimensions.push_back(isl_id_alloc(
                                     this->get_ctx(),
                                     dimensions_str[i].c_str(),
                                     NULL));
        }
        else
        {
            map = map + outDim0_str + ", " + static_dim_str + ", " + outDim1_str;
            isl_id *id0 = isl_id_alloc(this->get_ctx(),
                                       outDim0_str.c_str(), NULL);
            isl_id *id2 = isl_id_alloc(this->get_ctx(),
                                       static_dim_str.c_str(), NULL);
            isl_id *id1 = isl_id_alloc(this->get_ctx(),
                                       outDim1_str.c_str(), NULL);
            dimensions.push_back(id0);
            dimensions.push_back(id2);
            dimensions.push_back(id1);
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }

    map = map + "] : " + dimensions_str[0] + " = " + std::to_string(duplicate_ID) + " and " +
          outDim0_str + " = floor(" + inDim0_str + "/" +
          std::to_string(sizeX) + ") and " + outDim1_str + " = (" +
          inDim0_str + "%" + std::to_string(sizeX) + ") and " + static_dim_str + " = 0}";
    // std::cout<<map;
    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());

    for (int i = 0; i < dimensions.size(); i++)
        transformation_map = isl_map_set_dim_id(
                                 transformation_map, isl_dim_out, i, isl_id_copy(dimensions[i]));

    transformation_map = isl_map_set_tuple_id(
                             transformation_map, isl_dim_in,
                             isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));
    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);
    transformation_map = isl_map_set_tuple_id(transformation_map, isl_dim_out, id_range);

    // polyfp::str_dump("Transformation map : ", isl_map_to_str(transformation_map));

    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));

    // polyfp::str_dump("Schedule after splitting: ", isl_map_to_str(schedule));

    this->set_schedule(schedule);

}

void compute::tile(polyfp::var L0, polyfp::var L1,
        polyfp::var L2, int sizeX, int sizeY, int sizeZ)
{
    assert(L0.get_name().length() > 0);
    assert(L1.get_name().length() > 0);
    assert(L2.get_name().length() > 0);

    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L1_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L2_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());
    polyfp::var L1_inner = polyfp::var(generate_new_variable_name());
    polyfp::var L2_inner = polyfp::var(generate_new_variable_name());

    this->tile(L0, L1, L2, sizeX, sizeY, sizeZ,
               L0_outer, L1_outer, L0_outer, L0_inner, L1_inner, L2_inner);
}

void compute::tile(polyfp::var L0, polyfp::var L1,
        int sizeX, int sizeY)
{
    assert(L0.get_name().length() > 0);
    assert(L1.get_name().length() > 0);

    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L1_outer = polyfp::var(generate_new_variable_name());
    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());
    polyfp::var L1_inner = polyfp::var(generate_new_variable_name());

    this->tile(L0, L1, sizeX, sizeY,
               L0_outer, L1_outer, L0_inner, L1_inner);
}

void compute::tile(polyfp::var L0, polyfp::var L1, polyfp::var L2,
        int sizeX, int sizeY, int sizeZ,
        polyfp::var L0_outer, polyfp::var L1_outer,
        polyfp::var L2_outer, polyfp::var L0_inner,
        polyfp::var L1_inner, polyfp::var L2_inner)
{
    assert(L0.get_name().length() > 0);
    assert(L1.get_name().length() > 0);
    assert(L2.get_name().length() > 0);
    assert(L0_outer.get_name().length() > 0);
    assert(L1_outer.get_name().length() > 0);
    assert(L2_outer.get_name().length() > 0);
    assert(L0_inner.get_name().length() > 0);
    assert(L1_inner.get_name().length() > 0);
    assert(L2_inner.get_name().length() > 0);

    if(sizeX==0 &&sizeY==0&&sizeZ==0)
    {
        return;
    }

    this->assert_names_not_assigned({L0_outer.get_name(), L1_outer.get_name(),
                                     L2_outer.get_name(), L0_inner.get_name(),
                                     L1_inner.get_name(), L2_inner.get_name()});

    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();

    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({L0.get_name(),
                                                           L1.get_name(),
                                                           L2.get_name()});
    assert(dimensions.size() == 3);

    this->tile(dimensions[0], dimensions[1], dimensions[2],
               sizeX, sizeY, sizeZ);

    if(sizeX == 1 && sizeY == 1 )
    {
        this->update_names(original_loop_level_names, {L0.get_name(), L1.get_name(), L2_outer.get_name(),
                                                       L2_inner.get_name()}, dimensions[0], 3);

    }
    else if(sizeX == 1 && sizeZ == 1 )
    {
        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L2.get_name(),
                                                       L1_inner.get_name()}, dimensions[0], 3);

    }else if(sizeY == 1 && sizeZ == 1 )
    {
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L2.get_name(),
                                                       L0_inner.get_name()}, dimensions[0], 3);

    }else if(sizeX == 1)
    {
        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L2_outer.get_name(),
                                                    L1_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);
    }else if(sizeY == 1)
    {
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L2_outer.get_name(),
                                                    L0_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);
    }else if(sizeZ == 1)
    {
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1_outer.get_name(), L2.get_name(),
                                                    L0_inner.get_name(), L1_inner.get_name()}, dimensions[0], 3);
    }else{
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1_outer.get_name(), L2_outer.get_name(),
                                                L0_inner.get_name(), L1_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);
    }

    this->access_map.insert(std::pair(L0.get_name(),L0_inner.get_name()));
    this->access_map.insert(std::pair(L1.get_name(),L1_inner.get_name()));
    this->access_map.insert(std::pair(L2.get_name(),L2_inner.get_name()));
    this->tile_map.insert(std::pair(L0_inner.get_name(),L0_outer.get_name()));
    this->tile_map.insert(std::pair(L1_inner.get_name(),L1_outer.get_name()));
    this->tile_map.insert(std::pair(L2_inner.get_name(),L2_outer.get_name()));
    this->tile_size_map.insert(std::pair(L0_inner.get_name(),sizeX));
    this->tile_size_map.insert(std::pair(L1_inner.get_name(),sizeY));
    this->tile_size_map.insert(std::pair(L2_inner.get_name(),sizeZ));
    this->is_tiled = true;

}

void compute::tile(polyfp::var L0, polyfp::var L1,
      int sizeX, int sizeY,
      polyfp::var L0_outer, polyfp::var L1_outer,
      polyfp::var L0_inner, polyfp::var L1_inner)
{
    assert(L0.get_name().length() > 0);
    assert(L1.get_name().length() > 0);
    assert(L0_outer.get_name().length() > 0);
    assert(L1_outer.get_name().length() > 0);
    assert(L0_inner.get_name().length() > 0);
    assert(L1_inner.get_name().length() > 0);

    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();

    this->assert_names_not_assigned({L0_outer.get_name(), L1_outer.get_name(),
                                     L0_inner.get_name(), L1_inner.get_name()});

    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({L0.get_name(),
                                                           L1.get_name()});

    assert(dimensions.size() == 2);

    this->tile(dimensions[0], dimensions[1], sizeX, sizeY);

    // Replace the original dimension name with new dimension names
    if(sizeX == 1)
    {
        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L1_inner.get_name()}, dimensions[0], 2);
    }
    else if(sizeY == 1)
    {
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L0_inner.get_name()}, dimensions[0], 2);
    }else
    {
        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1_outer.get_name(), L0_inner.get_name(),L1_inner.get_name()}, dimensions[0], 2);
    }

    this->access_map.insert(std::pair(L0.get_name(),L0_inner.get_name()));
    this->access_map.insert(std::pair(L1.get_name(),L1_inner.get_name()));
    this->tile_map.insert(std::pair(L0_inner.get_name(),L0_outer.get_name()));
    this->tile_map.insert(std::pair(L1_inner.get_name(),L1_outer.get_name()));
    this->tile_size_map.insert(std::pair(L0_inner.get_name(),sizeX));
    this->tile_size_map.insert(std::pair(L1_inner.get_name(),sizeY));
    this->is_tiled = true;

}

void compute::tile(int L0, int L1, int L2, int sizeX, int sizeY, int sizeZ)
{
    // Check that the two dimensions are consecutive.
    // Tiling only applies on a consecutive band of loop dimensions.
    assert(L1 == L0 + 1);
    assert(L2 == L1 + 1);
    assert((sizeX > 0) && (sizeY > 0) && (sizeZ > 0));
    assert(this->get_iteration_domain() != NULL);

    this->check_dimensions_validity({L0, L1, L2});

    //  Original loops
    //  L0
    //    L1
    //      L2

    this->split(L0, sizeX); // Split L0 into L0 and L0_prime
    // Compute the new L1 and the new L2 and the newly created L0 (called L0 prime)
    int L0_prime = L0 + 1;
    L1 = L1 + 1;
    L2 = L2 + 1;

    //  Loop after transformation
    //  L0
    //    L0_prime
    //      L1
    //        L2

    this->split(L1, sizeY);
    int L1_prime = L1 + 1;
    L2 = L2 + 1;

    //  Loop after transformation
    //  L0
    //    L0_prime
    //      L1
    //        L1_prime
    //          L2

    this->split(L2, sizeZ);

    //  Loop after transformation
    //  L0
    //    L0_prime
    //      L1
    //        L1_prime
    //          L2
    //            L2_prime

    this->interchange(L0_prime, L1);
    // Change the position of L0_prime to the new position
    int temp = L0_prime;
    L0_prime = L1;
    L1 = temp;

    //  Loop after transformation
    //  L0
    //    L1
    //      L0_prime
    //        L1_prime
    //          L2
    //            L2_prime

    this->interchange(L0_prime, L2);
    // Change the position of L0_prime to the new position
    temp = L0_prime;
    L0_prime = L2;
    L2 = temp;

    //  Loop after transformation
    //  L0
    //    L1
    //      L2
    //        L1_prime
    //          L0_prime
    //            L2_prime

    this->interchange(L1_prime, L0_prime);

    //  Loop after transformation
    //  L0
    //    L1
    //      L2
    //        L0_prime
    //          L1_prime
    //            L2_prime
}

void compute::tile(int L0, int L1, int sizeX, int sizeY)
{
    // Check that the two dimensions are consecutive.
    // Tiling only applies on a consecutive band of loop dimensions.
    assert(L1 == L0 + 1);
    assert((sizeX > 0) && (sizeY > 0));
    assert(this->get_iteration_domain() != NULL);
    this->check_dimensions_validity({L0, L1});

    if(sizeX != 1)
    {
        this->split(L0, sizeX);
        this->split(L1 + 1, sizeY);
        this->interchange(L0 + 1, L1 + 1);
    }else
    {
        this->split(L1, sizeY);
    }

}


void compute::skew(polyfp::var L0_var, polyfp::var L1_var,
		       int f_i, int f_j ,
		       polyfp::var new_L0_var, polyfp::var new_L1_var)
{
    assert(L0_var.get_name().length() > 0);
    assert(L1_var.get_name().length() > 0);
    assert(new_L0_var.get_name().length() > 0);
    assert(new_L1_var.get_name().length() > 0);

    this->assert_names_not_assigned({new_L0_var.get_name(), new_L1_var.get_name()});
    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();

    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name(), L1_var.get_name()});

    this->check_dimensions_validity(dimensions);
    this->is_skewed = true;
    int L0 = dimensions[0];
    int L1 = dimensions[1];
    this->skew(L0, L1, f_i,f_j );
    this->update_names(original_loop_level_names, {new_L0_var.get_name(), new_L1_var.get_name()}, dimensions[0], 2);
    this->access_map.insert(std::pair(L0_var.get_name(),new_L1_var.get_name()));
    this->access_map.insert(std::pair(L1_var.get_name(),new_L0_var.get_name()));
    this->iterator_to_skew = new_L1_var.get_name();
    this->iterator_to_modify = new_L0_var.get_name();
    this->skew_factor = f_j;

}

void compute::skew(int L0 , int L1 , int f_i , int f_j)
{
    if (L0 + 1 != L1)
    {
	    ERROR("Loop levels passed to angle_skew() should be consecutive. The first argument to angle_skew() should be the outer loop level.", true);
    }

    assert(f_j != 0);
    assert(f_i >= 0);
   
    int dim0 = loop_level_into_dynamic_dimension(L0);
    int dim1 = loop_level_into_dynamic_dimension(L1);

    assert(this->get_schedule() != NULL);
    assert(dim0 >= 0);
    assert(dim0 < isl_space_dim(isl_map_get_space(this->get_schedule()), isl_dim_out));
    isl_map *schedule = this->get_schedule();
    int duplicate_ID = isl_map_get_static_dim(schedule, 0);

    schedule = isl_map_copy(schedule);
    schedule = isl_map_set_tuple_id(schedule, isl_dim_out,
                                    isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL));
    // polyfp::str_dump("Original schedule: ", isl_map_to_str(schedule));
    // polyfp::str_dump("Angle _ Skewing dimensions " + std::to_string(dim0)
    //                             + " and " + std::to_string(dim1));
    std::string inDim0_str, inDim1_str;
    std::string outDim1_str = generate_new_variable_name();
    std::string outDim0_str = generate_new_variable_name();

    int n_dims = isl_map_dim(this->get_schedule(), isl_dim_out);
    std::vector<isl_id *> dimensions;
    std::vector<std::string> dimensions_str;

    std::string map = "{";
    map = map + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            std::string dim_str = generate_new_variable_name();
            dimensions_str.push_back(dim_str);
            map = map + dim_str;
        }
        else
        {
            std::string dim_str = generate_new_variable_name();
            dimensions_str.push_back(dim_str);
            map = map + dim_str;
            if (i == dim0)
                inDim0_str = dim_str;
            else if (i == dim1)
                inDim1_str = dim_str;
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }

    map = map + "] -> " + this->get_name() + "[";

    for (int i = 0; i < n_dims; i++)
    {
        if (i == 0)
        {
            map = map + dimensions_str[i];
            dimensions.push_back(isl_id_alloc(
                                     this->get_ctx(),
                                     dimensions_str[i].c_str(),
                                     NULL));
        }
        else if ((i != dim1) && (i!=dim0))
        {
            map = map + dimensions_str[i];
            dimensions.push_back(isl_id_alloc(
                                     this->get_ctx(),
                                     dimensions_str[i].c_str(),
                                     NULL));
        }
        else // i==dim1
        {
            if(i==dim1){
                  map = map + outDim1_str;
            isl_id *id0 = isl_id_alloc(this->get_ctx(),
                                       outDim1_str.c_str(), NULL);
            dimensions.push_back(id0);
            }
            else{// i== dim 0 
                  map = map + outDim0_str;
            isl_id *id0 = isl_id_alloc(this->get_ctx(),
                                       outDim0_str.c_str(), NULL);
            dimensions.push_back(id0);
            }
        }

        if (i != n_dims - 1)
        {
            map = map + ",";
        }
    }
    // Computes gcd of f_i and f_j

    int n1 = abs(f_i);
    int n2 = abs(f_j);

    while(n1 != n2)
    {
        if(n1 > n2)
            n1 -= n2;
        else
            n2 -= n1;
    }

    // polyfp::str_dump("The gcd of f_i = "+std::to_string(f_i)+" and fj = "+std::to_string(f_j)+" is pgcd = "+std::to_string(n1));

   

    // Update f_i and f_j to equivalent but prime between themselfs value
    f_i = f_i / n1;
    f_j = f_j / n1;
  
    int gamma = 0;
    int sigma = 1;
    bool found = false;

    if ((f_j == 1) || (f_i == 1))
    {
        gamma = f_i - 1;
        sigma = 1;
        /* Since sigma = 1  then
            f_i - gamma * f_j = 1 & using the previous condition :
             - f_i = 1 : then gamma = 0 (f_i-1) is enough
             - f_j = 1 : then gamma = f_i -1  */
    }
    else
    { 
        if((f_j == - 1) && (f_i > 1))
        {
            gamma = 1;
            sigma = 0;    
        }    
        else
        {   //General case : solving the Linear Diophantine equation & finding basic solution (sigma & gamma) for : f_i* sigma - f_j*gamma = 1 
            int i =0;
            while((i < 100) && (!found))
            {
                if (((sigma * f_i ) % abs(f_j)) ==  1){
                            found = true;
                }
                else{
                    sigma ++;
                    i++;
                }
            };

            if(!found)
            {
                // Detect infinite loop and prevent it in case where f_i and f_j are not prime between themselfs
                ERROR(" Error in solving the Linear Diophantine equation f_i* sigma - f_j*gamma = 1  ", true);
            }

            gamma = ((sigma * f_i) - 1 ) / f_j;
        }
    }
    
    map = map + "] : " + dimensions_str[0] + " = " + std::to_string(duplicate_ID) + " and " +
            outDim0_str + " = (" + inDim0_str + "*"+std::to_string(f_i)+" + "+inDim1_str+"*"+std::to_string(f_j)+" ) and "
          +outDim1_str+" = ("+inDim0_str+"*"+std::to_string(gamma)+" + "+inDim1_str+"*"+std::to_string(sigma)+" ) }";

    // polyfp::str_dump("Transformation angle map (string format) : " + map);

    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());

    for (int i = 0; i < dimensions.size(); i++)
        transformation_map = isl_map_set_dim_id(
                                 transformation_map, isl_dim_out, i, isl_id_copy(dimensions[i]));

    transformation_map = isl_map_set_tuple_id(
                             transformation_map, isl_dim_in,
                             isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));
    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);
    transformation_map = isl_map_set_tuple_id(transformation_map, isl_dim_out, id_range);           
    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));
    // polyfp::str_dump("Schedule after transformation is :  ",
    //                             isl_map_to_str(schedule));                            
    this->set_schedule(schedule);
}


void polyfp::compute::after(compute &comp, polyfp::var level)
{
    assert(level.get_name().length() > 0);

    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({level.get_name()});

    assert(dimensions.size() == 1);

    int current_level = dimensions[0];
    auto leader_dim_map = comp.iterators_location_map;
    this->after_level = current_level;
    this->ori_after_level = current_level;
    this->after(comp, dimensions[0]);
}

void polyfp::compute::after(compute *comp, polyfp::var level)
{
    assert(level.get_name().length() > 0);
    std::vector<int> dimensions =
        this->get_loop_level_numbers_from_dimension_names({level.get_name()});
    assert(dimensions.size() == 1);
    int current_level = dimensions[0];
    int counter = 0;
    auto leader_dim_map = comp->iterators_location_map;
    this->after_level = current_level;
    this->ori_after_level= current_level;

    this->after(comp, dimensions[0]);

}

void polyfp::compute::after(compute &comp, int level)
{
    auto &graph = this->get_function()->sched_graph;
    auto &edges = graph[&comp];
    auto level_it = edges.find(this);
    edges[this] = level;

    this->get_function()->starting_computations.erase(this);
    // todo
    // this->get_function()->sched_graph_reversed[this][&comp] = level;
    this->after_level = level;
    // this->ori_after_level= level;
    if(level != -1)
    {
        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();
        while(iter2 != this->get_function()->leader_computations.end())
        {
            if(*iter2 == this)
            {
                iter2 = this->get_function()->leader_computations.erase(iter2);
            }
            else
            {
                iter2++;
            }      
        }
        // this->get_function()->leader_computations.erase(this);
        this->is_leader = false;
        this->is_top_parent = false;
        this->has_a_leader = true;
        this->leader = &comp;

        int component_level = comp.components.size();

        if(component_level !=0)
        {
            std::map<polyfp::compute *, int>::reverse_iterator iter = comp.components.rbegin();
            component_level = iter->second+1;
        }

        auto iter = comp.components.find (this) ;
        if(iter != comp.components.end())
            iter = comp.components.erase (iter);
        comp.components.insert(std::pair(this,component_level));
        comp.update_leader_components(this);
        
    }else if(level == -1)
    {
        this->is_leader = true;
        this->has_a_leader = false;
        this->is_top_parent = false;
        this->leader = NULL; 
        comp.is_leaf = false;
        auto iter = comp.components.find (this) ;
        if(iter != comp.components.end()){
            iter = comp.components.erase (iter);
            comp.delete_leader_components(this);
        }
        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();
        while(iter2 != this->get_function()->leader_computations.end())
        {
            if(*iter2 == this)
            {
                iter2 = this->get_function()->leader_computations.erase(iter2);
            }
            else
            {
                iter2++;
            }      
        }
        this->get_function()->leader_computations.push_back(this);
        //TODO: check if it is in lead_comp list
        // int current_level = level;
        // int counter = 0;
        // auto dim_list = this->get_loop_level_names();
        // auto leader_dim_map = comp.iterators_location_map;
        // for(int i=0; i<dim_list.size(); i++){
        //     auto fct = global::get_implicit_function();
        //     auto next_level = fct->global_location;
        //     this->iterators_location_map.insert(std::make_pair(dim_list[counter],next_level));
        //     fct->global_location+=1;
        //     counter+=1;
        // }
    }
    
    assert(this->get_function()->sched_graph_reversed[this].size() < 2 &&
            "Node has more than one predecessor.");
    // polyfp::str_dump("sched_graph[" + comp.get_name() + ", " +
    //                              this->get_name() + "] = " + std::to_string(level));                             
}

void polyfp::compute::after(compute *comp, int level)
{
    // polyfp::str_dump("Scheduling " + this->get_name() + " to be executed after " +
    //                             comp.get_name() + " at level " + std::to_string(level));                            

    auto &graph = this->get_function()->sched_graph;
    auto &edges = graph[comp];
    auto level_it = edges.find(this);
    // if (level_it != edges.end())
    // {
    //     if (level_it->second > level)
    //     {
    //         level = level_it->second;
    //     }
    // }

    edges[this] = level;
    

    this->get_function()->starting_computations.erase(this);
    this->after_level = level;
     if(level != -1)
     {
        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();
        while(iter2 != this->get_function()->leader_computations.end())
        {
            if(*iter2 == this)
            {
                iter2 = this->get_function()->leader_computations.erase(iter2);
            }
            else
            {
                iter2++;
            }      
        }
        this->is_leader = false;
        this->is_top_parent = false;
        this->has_a_leader = true;
        this->leader = comp;

        int component_level = comp->components.size();
        if(component_level !=0)
        {
            std::map<polyfp::compute *, int>::reverse_iterator iter = comp->components.rbegin();
            component_level = iter->second+1;
        }

        auto iter = comp->components.find (this) ;
        if(iter != comp->components.end())
            iter = comp->components.erase (iter);
        comp->components.insert(std::pair(this,component_level));
        comp->update_leader_components(this);
        
    }else if(level == -1)
    {
        this->is_leader = true;
        this->has_a_leader = false;
        this->is_top_parent = false;
        this->leader = NULL; 
        comp->is_leaf = false;
        auto iter = comp->components.find (this) ;
        if(iter != comp->components.end())
        {
            iter = comp->components.erase (iter);
            comp->delete_leader_components(this);
        }
        this->get_function()->leader_computations.push_back(this);
    }
    assert(this->get_function()->sched_graph_reversed[this].size() < 2 &&
            "Node has more than one predecessor.");
}

void polyfp::compute::update_leader_components(polyfp::compute *comp)
{
    if(this->has_a_leader)
    {
        int component_level = this->leader->components.size()+1;
        this->leader->components.insert(std::pair(comp,component_level));
        this->leader->update_leader_components(comp);
    }
}

void polyfp::compute::delete_leader_components(polyfp::compute *comp)
{
    if(this->has_a_leader){
        auto iter = this->leader->components.find (this) ;
        if(iter != this->leader->components.end()){
            iter = this->leader->components.erase (iter);
        }
        // this->leader->components.insert(std::pair(comp,component_level));
        this->leader->update_leader_components(comp);
    }
}

void polyfp::compute::dump_components()
{
    std::string result = "";
    for (auto &edge: this->components)
    {
        result += edge.first->get_name() +"[" + std::to_string(edge.second )+ "]=>";

    }
    result += this->get_name();
    // std::cout<<result<<std::endl;
}

void polyfp::compute::dump_loads_stores()
{
    std::string result = "";

    for (auto &edge: this->map_loadstores)
    {
        result += std::to_string(edge.first )+":[" ;
        for(auto &map: edge.second){
            result+= map.first;
            for(auto &vec: map.second){
                result+= vec.get_name();
            }
        }
        result+=+  "]=>";
    }
        
    result += "root";
    // std::cout<<result<<std::endl;
}

void compute::after_low_level(compute &comp, int level)
{
    // for loop level i return 2*i+1 which represents the
    // the static dimension just after the dynamic dimension that
    // represents the loop level i.
    int dim = loop_level_into_static_dimension(level);
Download .txt
gitextract_f9e36swx/

├── .gitattributes
├── .gitignore
├── .gitmodules
├── CMakeLists.txt
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── ae_script.sh
├── build-pom.sh
├── clean.sh
├── include/
│   ├── CMakeLists.txt
│   └── polyhedral/
│       ├── codegen.h
│       ├── compute.h
│       ├── core.h
│       ├── debug.h
│       ├── expr.h
│       ├── function.h
│       ├── generator.h
│       ├── generator_isl.h
│       ├── placeholder.h
│       └── type.h
├── lib/
│   ├── CMakeLists.txt
│   └── polyhedral/
│       ├── CMakeLists.txt
│       ├── codegen.cpp
│       ├── compute.cpp
│       ├── core.cpp
│       ├── debug.cpp
│       ├── expr.cpp
│       ├── function.cpp
│       ├── generator.cpp
│       ├── generator_isl.cpp
│       ├── placeholer.cpp
│       └── test.cpp
├── results-gen.sh
├── run-code.sh
├── samples/
│   └── config.json
├── tcl-gen.sh
├── testbench/
│   ├── 2mm.cpp
│   ├── 3mm.cpp
│   ├── bicg.cpp
│   ├── blur.cpp
│   ├── edgeDetect.cpp
│   ├── gaussian.cpp
│   ├── gemm.cpp
│   ├── gesummv.cpp
│   ├── heat.cpp
│   ├── jacobi.cpp
│   ├── jacobi2d.cpp
│   ├── resnet18.cpp
│   ├── seidel.cpp
│   └── vgg16.cpp
└── vitis-reports.sh
Download .txt
SYMBOL INDEX (102 symbols across 34 files)

FILE: include/polyhedral/codegen.h
  function namespace (line 27) | namespace polyfp{

FILE: include/polyhedral/compute.h
  function class (line 35) | class compute

FILE: include/polyhedral/core.h
  function namespace (line 26) | namespace polyfp{

FILE: include/polyhedral/debug.h
  function namespace (line 7) | namespace polyfp

FILE: include/polyhedral/expr.h
  function namespace (line 12) | namespace polyfp
  function class (line 83) | class expr
  function polyfp (line 477) | const polyfp::expr &get_operand(int i) const
  function std (line 503) | const std::string &get_name() const
  function std (line 541) | const std::vector<polyfp::expr> &get_access() const
  function is_equal (line 566) | bool is_equal(polyfp::expr e) const
  function set_access (line 627) | void set_access(std::vector<polyfp::expr> vector)
  function set_access_dimension (line 632) | void set_access_dimension(int i, polyfp::expr acc)
  function get_access_vector (line 638) | void get_access_vector(std::vector<polyfp::expr> &loads) const{
  function dump (line 714) | void dump(bool exhaustive) const
  function get_dependence_vector (line 821) | int get_dependence_vector() const{
  function else (line 847) | else if(this->get_expr_type() == e_var){
  function expr (line 1067) | expr get_lower()
  function var (line 1097) | var(generate_new_variable_name()) {}
  function show (line 1099) | void show(){
  function class (line 1107) | class constant: public polyfp::expr

FILE: include/polyhedral/function.h
  function namespace (line 65) | namespace polyfp{

FILE: include/polyhedral/generator.h
  function namespace (line 65) | namespace polyfp{

FILE: include/polyhedral/generator_isl.h
  function class (line 10) | class generator

FILE: include/polyhedral/placeholder.h
  function namespace (line 16) | namespace polyfp{

FILE: include/polyhedral/type.h
  function namespace (line 7) | namespace polyfp

FILE: lib/polyhedral/codegen.cpp
  type polyfp (line 2) | namespace polyfp
    function access_is_affine (line 32) | bool access_is_affine(const polyfp::expr &exp)
    function isl_ast_node (line 71) | isl_ast_node *for_code_generator_after_for(isl_ast_node *node, isl_ast...

FILE: lib/polyhedral/compute.cpp
  type polyfp (line 4) | namespace polyfp{
    function isl_ctx (line 6) | isl_ctx *polyfp::compute::get_ctx() const
    function isl_set (line 11) | isl_set *polyfp::compute::get_iteration_domain() const
    function isl_map (line 28) | isl_map *compute::get_schedule() const
    function isl_set (line 33) | isl_set *polyfp::compute::get_trimmed_time_processor_domain()
    function isl_map (line 68) | isl_map *isl_map_add_dim_and_eq_constraint(isl_map *map, int dim_pos, ...
    function isl_map (line 88) | isl_map *polyfp::compute::gen_identity_schedule_for_iteration_domain()
    function isl_set (line 341) | isl_set *polyfp::compute::get_time_processor_domain() const
    function isl_map (line 379) | isl_map *polyfp::compute::get_access_relation() const
    function isl_map (line 433) | isl_map *polyfp::compute::gen_identity_schedule_for_time_space_domain()
    type param_pack_1 (line 688) | struct param_pack_1
    function isl_stat (line 701) | isl_stat extract_static_dim_value_from_bmap(__isl_take isl_basic_map *...
    function isl_stat (line 728) | isl_stat extract_constant_value_from_bset(__isl_take isl_basic_set *bs...
    function isl_map_get_static_dim (line 759) | int isl_map_get_static_dim(isl_map *map, int dim_pos)
    function isl_set (line 2502) | isl_set *compute::intersect_set_with_context(isl_set *set)

FILE: lib/polyhedral/core.cpp
  type polyfp (line 17) | namespace polyfp
    function init (line 32) | void init(std::string fct_name)
    function init (line 40) | void init()
    function codegen (line 45) | void codegen()
    function loop_level_into_dynamic_dimension (line 68) | int loop_level_into_dynamic_dimension(int level)
    function loop_level_into_static_dimension (line 86) | int loop_level_into_static_dimension(int level)
    function dynamic_dimension_into_loop_level (line 105) | int dynamic_dimension_into_loop_level(int dim)
    function generate_new_variable_name (line 112) | std::string generate_new_variable_name()
    function generate_new_computation_name (line 117) | std::string generate_new_computation_name()
    function str_from_polyfp_type_expr (line 122) | std::string str_from_polyfp_type_expr(polyfp::expr_t type)
    function str_from_polyfp_type_primitive (line 139) | std::string str_from_polyfp_type_primitive(polyfp::primitive_t type)
    function str_polyfp_type_op (line 168) | std::string str_polyfp_type_op(polyfp::op_t type)
    function isl_map (line 194) | isl_map *add_eq_to_schedule_map(int dim0, int in_dim_coefficient, int ...

FILE: lib/polyhedral/debug.cpp
  type polyfp (line 4) | namespace polyfp
    function str_dump (line 9) | void str_dump(const std::string &str)
    function str_dump (line 14) | void str_dump(const std::string &str, const char *str2)
    function str_dump (line 19) | void str_dump(const char *str, const char *str2)
    function print_indentation (line 24) | void print_indentation()

FILE: lib/polyhedral/expr.cpp
  type polyfp (line 5) | namespace polyfp
    function expr (line 177) | expr polyfp::expr::operator+(polyfp::expr other) const {
    function expr (line 181) | expr polyfp::expr::operator-(polyfp::expr other) const {
    function expr (line 185) | expr polyfp::expr::operator*(polyfp::expr other) const {
    function expr (line 189) | expr polyfp::expr::operator/(polyfp::expr other) const {
    function expr (line 193) | expr polyfp::expr::operator%(polyfp::expr other) const {

FILE: lib/polyhedral/function.cpp
  type polyfp (line 9) | namespace polyfp{
    function isl_map (line 12) | isl_map *isl_map_align_range_dims(isl_map *map, int max_dim)
    function isl_ctx (line 47) | isl_ctx *function::get_isl_ctx() const
    function isl_ast_node (line 164) | isl_ast_node *function::get_isl_ast() const
    function isl_ast_node (line 171) | isl_ast_node *function::get_isl_ast1() const
    function isl_union_set (line 178) | isl_union_set *polyfp::function::get_iteration_domain() const
    function isl_union_map (line 204) | isl_union_map *polyfp::function::get_aligned_identity_schedules() const
    function isl_set (line 375) | isl_set *function::get_program_context() const
    function isl_union_map (line 502) | isl_union_map *polyfp::function::get_schedule() const
    function isl_union_set (line 530) | isl_union_set *polyfp::function::get_trimmed_time_processor_domain() c...
    function isl_union_map (line 597) | isl_union_map *polyfp::function::compute_dep_graph()
    function cmp_value (line 2479) | bool cmp_value(const std::pair<int, long> left, const std::pair<int,lo...

FILE: lib/polyhedral/generator.cpp
  type polyfp (line 16) | namespace polyfp{
    function mlirGen2 (line 2922) | mlir::OwningOpRef<mlir::ModuleOp> mlirGen2(mlir::MLIRContext &context,...
    function gen_mlir (line 3028) | void gen_mlir(polyfp::function &fct, isl_ast_node *node, int &level)

FILE: lib/polyhedral/generator_isl.cpp
  type polyfp (line 3) | namespace polyfp{
    function isl_map (line 16) | isl_map *create_map_from_domain_and_range(isl_set *domain, isl_set *ra...
    function isl_constraint (line 73) | isl_constraint *generator::get_constraint_for_access(int access_dimens...
    function polyfp_expr_from_isl_ast_expr (line 380) | polyfp::expr polyfp_expr_from_isl_ast_expr(isl_ast_expr *isl_expr)

FILE: lib/polyhedral/placeholer.cpp
  type polyfp (line 3) | namespace polyfp{

FILE: lib/polyhedral/test.cpp
  function cf_test (line 3) | int cf_test()

FILE: testbench/2mm.cpp
  function main (line 30) | int main(){

FILE: testbench/3mm.cpp
  function main (line 29) | int main(){

FILE: testbench/bicg.cpp
  function main (line 9) | int main(){

FILE: testbench/blur.cpp
  function main (line 10) | int main(){

FILE: testbench/edgeDetect.cpp
  function main (line 9) | int main(){

FILE: testbench/gaussian.cpp
  function main (line 9) | int main(){

FILE: testbench/gemm.cpp
  function main (line 29) | int main(){

FILE: testbench/gesummv.cpp
  function main (line 29) | int main(){

FILE: testbench/heat.cpp
  function main (line 10) | int main(){

FILE: testbench/jacobi.cpp
  function main (line 10) | int main(){

FILE: testbench/jacobi2d.cpp
  function main (line 10) | int main(){

FILE: testbench/resnet18.cpp
  function main (line 14) | int main(){

FILE: testbench/seidel.cpp
  function main (line 27) | int main(){

FILE: testbench/vgg16.cpp
  function main (line 11) | int main(){
Condensed preview — 53 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (567K chars).
[
  {
    "path": ".gitattributes",
    "chars": 66,
    "preview": "# Auto detect text files and perform LF normalization\n* text=auto\n"
  },
  {
    "path": ".gitignore",
    "chars": 251,
    "preview": "build\n# balance/\n# sample_l/\n# samles_4_paper/\n# samles_update/\nbuild.txt\n.vscode/\n.env\nscalehls/\n**/tmp\n**/cpp_src\n**/m"
  },
  {
    "path": ".gitmodules",
    "chars": 107,
    "preview": "[submodule \"scalehls\"]\n\tpath = scalehls\n\turl = git@github.com:Jason048/scalehls.git\n\tbranch = scalehls-pom\n"
  },
  {
    "path": "CMakeLists.txt",
    "chars": 6723,
    "preview": "cmake_minimum_required(VERSION 3.13.4)\n\nif(POLICY CMP0068)\n  cmake_policy(SET CMP0068 NEW)\n  set(CMAKE_BUILD_WITH_INSTAL"
  },
  {
    "path": "Dockerfile",
    "chars": 2185,
    "preview": "# This Dockerfile configures a Docker environment that \n# contains all the required packages for the tool\nFROM ubuntu:20"
  },
  {
    "path": "LICENSE",
    "chars": 11357,
    "preview": "                                 Apache License\n                           Version 2.0, January 2004\n                   "
  },
  {
    "path": "Makefile",
    "chars": 496,
    "preview": "user=$(if $(shell id -u),$(shell id -u),9001)\ngroup=$(if $(shell id -g),$(shell id -g),1000)\n# phism=/workspace\nvhls=/da"
  },
  {
    "path": "README.md",
    "chars": 3534,
    "preview": "\n# POM: An Optimizing Framework on MLIR for Efficient FPGA-based Accelerator Generation\n![GitHub License](https://img.sh"
  },
  {
    "path": "ae_script.sh",
    "chars": 351,
    "preview": "\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Start the experiment workflow\"\necho \"\"\n./build-pom.sh\n\n\n./run-code.sh\n\n\n./tc"
  },
  {
    "path": "build-pom.sh",
    "chars": 1289,
    "preview": "#!/usr/bin/env bash\n\nset -o errexit\nset -o pipefail\nset -o nounset\n\n# The absolute path to the directory of this script."
  },
  {
    "path": "clean.sh",
    "chars": 218,
    "preview": "#!/bin/bash\n\n\nfolders=(\"gemm\" \"bicg\" \"gesummv\" \"2mm\" \"3mm\" \"edgeDetect\" \"gaussian\" \"blur\" \"vgg16\"  \"resnet\" \"jacobi\" \"ja"
  },
  {
    "path": "include/CMakeLists.txt",
    "chars": 90,
    "preview": "# add_subdirectory(Standalone)\n# add_subdirectory(Dialect)\n# add_subdirectory(Polyhedral)\n"
  },
  {
    "path": "include/polyhedral/codegen.h",
    "chars": 773,
    "preview": "#ifndef _H_polyfp_CODEGEN_\n#define _H_polyfp_CODEGEN_\n\n#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map"
  },
  {
    "path": "include/polyhedral/compute.h",
    "chars": 15576,
    "preview": "#ifndef _H_polyfp_COMPUTE_\n#define _H_polyfp_COMPUTE_\n\n#include <isl/ctx.h>\n#include <isl/aff.h>\n#include <isl/set.h>\n#i"
  },
  {
    "path": "include/polyhedral/core.h",
    "chars": 1000,
    "preview": "#ifndef _H_polyfp_CORE_\n#define _H_polyfp_CORE_\n\n#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#i"
  },
  {
    "path": "include/polyhedral/debug.h",
    "chars": 751,
    "preview": "#ifndef _H_DEBUG_\n#define _H_DEBUG_\n\n#include <iostream>\n\n\nnamespace polyfp\n{\n\nvoid str_dump(const std::string &str);\nvo"
  },
  {
    "path": "include/polyhedral/expr.h",
    "chars": 37728,
    "preview": "#ifndef _H_polyfp_EXPR_\n#define _H_polyfp_EXPR_\n#include<algorithm>\n\n#include <map>\n#include <unordered_map>\n#include <v"
  },
  {
    "path": "include/polyhedral/function.h",
    "chars": 11687,
    "preview": "#ifndef _H_polyfp_function_\n#define _H_polyfp_function_\n\n#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_m"
  },
  {
    "path": "include/polyhedral/generator.h",
    "chars": 4055,
    "preview": "#include <isl/aff.h>\n#include <isl/set.h>\n#include <isl/constraint.h>\n#include <isl/space.h>\n#include <isl/map.h>\n#inclu"
  },
  {
    "path": "include/polyhedral/generator_isl.h",
    "chars": 4728,
    "preview": "#ifndef _H_polyfp_function1_\n#define _H_polyfp_function1_\n\n#include \"expr.h\"\n#include \"type.h\"\n#include \"compute.h\"\n#inc"
  },
  {
    "path": "include/polyhedral/placeholder.h",
    "chars": 4374,
    "preview": "#ifndef _H_polyfp_PLACEHOLDER_\n#define _H_polyfp_PLACEHOLDER_\n// #include \"compute.h\"\n#include \"function.h\"\n#include \"ex"
  },
  {
    "path": "include/polyhedral/type.h",
    "chars": 825,
    "preview": "#ifndef _H_PolyFP_TYPE_\n#define _H_PolyFP_TYPE_\n\n#include <string.h>\n#include <stdint.h>\n\nnamespace polyfp\n{\n\n\n// Type o"
  },
  {
    "path": "lib/CMakeLists.txt",
    "chars": 111,
    "preview": "add_subdirectory(polyhedral)\n# add_subdirectory(CAPI)\n# add_subdirectory(Standalone)\n# add_subdirectory(hello)\n"
  },
  {
    "path": "lib/polyhedral/CMakeLists.txt",
    "chars": 366,
    "preview": "get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)\nget_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSIO"
  },
  {
    "path": "lib/polyhedral/codegen.cpp",
    "chars": 1791,
    "preview": "#include \"codegen.h\"\nnamespace polyfp\n{\n\nstd::vector<compute *> function::get_computation_by_name(std::string name) cons"
  },
  {
    "path": "lib/polyhedral/compute.cpp",
    "chars": 90576,
    "preview": "#include \"compute.h\"\n#include \"core.h\"\n#include <algorithm>\nnamespace polyfp{\n\nisl_ctx *polyfp::compute::get_ctx() const"
  },
  {
    "path": "lib/polyhedral/core.cpp",
    "chars": 6504,
    "preview": "#include <isl/ctx.h>\n#include <isl/aff.h>\n#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/id.h>\n#include <isl/co"
  },
  {
    "path": "lib/polyhedral/debug.cpp",
    "chars": 634,
    "preview": "#include <iostream>\n#include <sstream>\n\nnamespace polyfp\n{\n\nint polyfp_indentation = 0;\n\nvoid str_dump(const std::string"
  },
  {
    "path": "lib/polyhedral/expr.cpp",
    "chars": 5446,
    "preview": "#include \"expr.h\"\n#include \"function.h\";\n// #include <polyfp/core.h>\n// #include \"function.h\"\nnamespace polyfp\n{\n\npolyfp"
  },
  {
    "path": "lib/polyhedral/function.cpp",
    "chars": 97903,
    "preview": "\n#include \"function.h\"\n#include \"generator.h\"\n#include <iostream>\n#include <fstream>\n#include <filesystem>\n\n\nnamespace p"
  },
  {
    "path": "lib/polyhedral/generator.cpp",
    "chars": 152361,
    "preview": "#include \"generator.h\"\n#include <string>\n#include <iostream>\n#include <filesystem>\n#include \"scalehls/Transforms/Passes."
  },
  {
    "path": "lib/polyhedral/generator_isl.cpp",
    "chars": 18483,
    "preview": "#include \"generator_isl.h\"\n\nnamespace polyfp{\n\npolyfp::expr polyfp_expr_from_isl_ast_expr(isl_ast_expr *isl_expr);\n\nvoid"
  },
  {
    "path": "lib/polyhedral/placeholer.cpp",
    "chars": 2505,
    "preview": "#include \"placeholder.h\"\n#include <iostream>\nnamespace polyfp{\n\n\npolyfp::placeholder::placeholder(std::string name, std:"
  },
  {
    "path": "lib/polyhedral/test.cpp",
    "chars": 136,
    "preview": "// #include <iostream>\n#include <stdio.h> \nint cf_test()\n{\n\tprintf(\"hello python!\\n\");\n\treturn 0;\n}\n////////////////////"
  },
  {
    "path": "results-gen.sh",
    "chars": 13252,
    "preview": "\nstart_time=$(date +\"%s\")\n\necho \"\"\necho \">>> Step 5. Collecting experimental results...\"\necho \"\"\n\ndeclare -A baseline_la"
  },
  {
    "path": "run-code.sh",
    "chars": 4184,
    "preview": "#!/usr/bin/env bash\nstart=$(date +\"%s\")\necho \"\"\necho \">>> Step 2. Compiling the object files and Generating the optimize"
  },
  {
    "path": "samples/config.json",
    "chars": 894,
    "preview": "{\n    \"__max_init_parallel\": \"The maximum loop parallelism in the initial sampling\",\n    \"max_init_parallel\": 32,\n    \"_"
  },
  {
    "path": "tcl-gen.sh",
    "chars": 3558,
    "preview": "#!/bin/bash\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Step 3. Generating scripts for running Vitis_HLS...\"\necho \"\"\n\nexa"
  },
  {
    "path": "testbench/2mm.cpp",
    "chars": 2176,
    "preview": "#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build."
  },
  {
    "path": "testbench/3mm.cpp",
    "chars": 2323,
    "preview": "#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build."
  },
  {
    "path": "testbench/bicg.cpp",
    "chars": 1249,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#define N 4096\n#include <filesystem>\nusin"
  },
  {
    "path": "testbench/blur.cpp",
    "chars": 935,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\n#define N 4096\n\nusi"
  },
  {
    "path": "testbench/edgeDetect.cpp",
    "chars": 1054,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/gaussian.cpp",
    "chars": 1203,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/gemm.cpp",
    "chars": 1822,
    "preview": "#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build."
  },
  {
    "path": "testbench/gesummv.cpp",
    "chars": 1411,
    "preview": "#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build."
  },
  {
    "path": "testbench/heat.cpp",
    "chars": 832,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/jacobi.cpp",
    "chars": 1206,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/jacobi2d.cpp",
    "chars": 966,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/resnet18.cpp",
    "chars": 11233,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "testbench/seidel.cpp",
    "chars": 1232,
    "preview": "#include <isl/set.h>\n#include <isl/map.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build."
  },
  {
    "path": "testbench/vgg16.cpp",
    "chars": 8923,
    "preview": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std"
  },
  {
    "path": "vitis-reports.sh",
    "chars": 2127,
    "preview": "#!/bin/bash\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Step 4. Synthesising the optimized HLS C code...\"\necho \"\"\nexport "
  }
]

About this extraction

This page contains the full source code of the sjtu-zhao-lab/pom GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 53 files (532.8 KB), approximately 132.0k tokens, and a symbol index with 102 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!