[
  {
    "path": ".gitattributes",
    "content": "# Auto detect text files and perform LF normalization\n* text=auto\n"
  },
  {
    "path": ".gitignore",
    "content": "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**/mlir_src\n**/hls_proj\n**/dump_csv\n**/.ipynb_checkpoints\n**/__pycache__\n\n*.onnx*\n*.tmp\n*.gv\n*.png\n*.log\n*.csv\n*.zip\n*.swp\n*.swo\n*.swn\n"
  },
  {
    "path": ".gitmodules",
    "content": "[submodule \"scalehls\"]\n\tpath = scalehls\n\turl = git@github.com:Jason048/scalehls.git\n\tbranch = scalehls-pom\n"
  },
  {
    "path": "CMakeLists.txt",
    "content": "cmake_minimum_required(VERSION 3.13.4)\n\nif(POLICY CMP0068)\n  cmake_policy(SET CMP0068 NEW)\n  set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)\nendif()\n\nif(POLICY CMP0075)\n  cmake_policy(SET CMP0075 NEW)\nendif()\n\nif(POLICY CMP0077)\n  cmake_policy(SET CMP0077 NEW)\nendif()\n\nset(CMAKE_CXX_STANDARD 17)\nset(CMAKE_CXX_STANDARD_REQUIRED YES)\n\nadd_definitions(-w)\nset(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})\nset(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/build)\nset(TEST_PATH ${PROJECT_SOURCE_DIR}/test)\nSET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)\nSET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)\n\n\nif (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)\n  project(POM LANGUAGES CXX C)\n  # llvm\n  set(LLVM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/llvm)\n  find_package(MLIR REQUIRED CONFIG)\n  message(STATUS \"Using MLIRConfig.cmake in: ${MLIR_DIR}\")\n  message(STATUS \"Using LLVMConfig.cmake in: ${LLVM_DIR}\")\n  message(STATUS \"Using LLVM_EXTERNAL_LIT.cmake in: ${LLVM_EXTERNAL_LIT}\")\n\n  list(APPEND CMAKE_MODULE_PATH \"${MLIR_CMAKE_DIR}\")\n  list(APPEND CMAKE_MODULE_PATH \"${LLVM_CMAKE_DIR}\")\n\n  include(TableGen)\n  include(AddLLVM)\n  include(AddMLIR)\n  include(HandleLLVMOptions)\nelse ()\n  set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})\n  set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)\n  set(MLIR_INCLUDE_DIRS ${MLIR_MAIN_SRC_DIR}/include)\n  set(MLIR_CMAKE_DIR ${MLIR_MAIN_SRC_DIR}/cmake/modules)\n  set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)\n  set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)\n  include_directories(SYSTEM ${MLIR_INCLUDE_DIR})\n  include_directories(SYSTEM ${MLIR_TABLEGEN_OUTPUT_DIR})\n  message(STATUS \"Using LLVM_SOURCE_DIR in: ${LLVM_SOURCE_DIR}\")\n  message(STATUS \"Using MLIR_MAIN_SRC_DIR in: ${MLIR_MAIN_SRC_DIR}\")\nendif()\n\n# include_directories(\"/home/POM/third_party/llvm-project/mlir/include/mlir/IR\")\nset(POM_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})\nset(POM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})\nset(POM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include )\nset(POM_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin)\n\n\nlist(APPEND CMAKE_MODULE_PATH \"${MLIR_MAIN_SRC_DIR}/cmake/modules\")\nlist(APPEND CMAKE_MODULE_PATH \"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules\")\n\ninclude_directories(${LLVM_INCLUDE_DIRS})\ninclude_directories(${MLIR_INCLUDE_DIRS})\ninclude_directories(${PROJECT_SOURCE_DIR}/include)\ninclude_directories(${PROJECT_BINARY_DIR}/include)\nlink_directories(${LLVM_BUILD_LIBRARY_DIR})\nadd_definitions(${LLVM_DEFINITIONS})\n\n\n\n# ISL\n#set(ISL_INCLUDE_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/isl/build/include/ CACHE PATH #\"Path to ISL include directory\")\n#set(ISL_LIB_DIRECTORY ${PROJECT_SOURCE_DIR}/third_party/isl/build/.lib/ CACHE PATH \"Path to #ISL library directory\")\n\ninclude_directories(${PROJECT_SOURCE_DIR}/include/polyhedral)\ninclude_directories(${PROJECT_SOURCE_DIR}/lib/polyhedral)\ninclude_directories(${PROJECT_SOURCE_DIR}/include)\ninclude_directories(${PROJECT_SOURCE_DIR}/testbench)\ninclude_directories(${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/mlir/include)\ninclude_directories(${PROJECT_SOURCE_DIR}/scalehls/polygeist/llvm-project/llvm/include)\ninclude_directories(${PROJECT_SOURCE_DIR}/scalehls/build/tools/scalehls/include)\ninclude_directories($ISL_INCLUDE_DIRECTORY)\ninclude_directories(${PROJECT_SOURCE_DIR}/scalehls/include)\n#include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)\ninclude_directories(/usr/include/python3.8)\n\n\nfind_library(ISLLib isl PATHS /usr/local/lib NO_DEFAULT_PATH)\nmessage(STATUS \"Using ISLlib in: ${ISLLib}\")\n\n\n\nget_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)\n\nget_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)\nget_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)\nget_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)\n\n\n# find_library(MLIRSCF NAMES libMLIRSCF.a PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)\n# message(STATUS \"Using MLIRSCF in: ${MLIRSCF}\")\n\nfind_library(MLIRHLS MLIRHLS PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)\nmessage(STATUS \"Using MLIRHLS in: ${MLIRHLS}\")\n\nfind_library(MLIRScaleHLSSupport MLIRScaleHLSSupport PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)\nmessage(STATUS \"Using MLIRScaleHLSSupport in: ${MLIRScaleHLSSupport}\")\n\nfind_library(MLIRScaleHLSTransforms MLIRScaleHLSTransforms PATHS ${PROJECT_SOURCE_DIR}/scalehls/build/lib)\nmessage(STATUS \"Using MLIRScaleHLSSupport in: ${MLIRScaleHLSTransforms}\")\n\n\nset(TEST_SOURCE_DIR ${PROJECT_SOURCE_DIR}/testbench)\nfile(GLOB_RECURSE  mains RELATIVE \n\"${TEST_SOURCE_DIR}\" \n\"${TEST_SOURCE_DIR}/*.cpp\"\n)\nmessage(STATUS \"Using TEST_SOURCE_DIR in:${TEST_SOURCE_DIR}\")\n\nforeach(mainfile IN LISTS mains)\n    # Get file name without directory\n    get_filename_component(mainname ${mainfile} NAME_WE)\n    # message(STATUS \"get mainname in:${mainname}\")\n    add_executable(${mainname} ${TEST_SOURCE_DIR}/${mainfile})\n\n    target_link_libraries(${mainname} Functions)\n    target_link_libraries(${mainname} ${ISLLib})\n    target_link_libraries(${mainname}\n        ${dialect_libs}\n        ${conversion_libs}\n        ${translation_libs}\n        ${MLIRScaleHLSTransforms}\n        ${MLIRScaleHLSSupport}\n        ${MLIRHLS}\n        MLIRAffineTransforms\n        MLIROptLib\n        MLIRAnalysis\n        MLIRCallInterfaces\n        MLIRCastInterfaces\n        MLIRIR\n        MLIRParser\n        MLIRPass\n        MLIRSideEffectInterfaces\n        MLIRSupport\n        MLIRTransforms)\nendforeach()\n\n# add_executable(test test.cpp)\n# add_executable(bicg bicg.cpp)\n# target_link_libraries(test Functions)\n# target_link_libraries(test ${ISLLib})\n# target_link_libraries(test\n#     ${dialect_libs}\n#     ${conversion_libs}\n#     ${translation_libs}\n#     ${MLIRScaleHLSTransforms}\n#     ${MLIRScaleHLSSupport}\n#     ${MLIRHLSCpp}\n#     MLIRAffineTransforms\n#     MLIROptLib\n#     MLIRAnalysis\n#     MLIRCallInterfaces\n#     MLIRCastInterfaces\n#     MLIRIR\n#     MLIRParser\n#     MLIRPass\n#     MLIRSideEffectInterfaces\n#     MLIRSupport\n#     MLIRTransforms)\n\n\n\nadd_subdirectory(include)\nadd_subdirectory(lib)\n#add_subdirectory(test)\n#add_subdirectory(standalone-opt)\n#add_subdirectory(standalone-translate)\n#add_subdirectory(POM-isl)\n# add_subdirectory(pybind11)\n\n\n#find_package(pybind11 REQUIRED)\n#pybind11_add_module(wrapper wrapper.cpp)\n#target_link_libraries(wrapper PRIVATE ${ISLLib})\n\n# pybind11_add_module(core ${PROJECT_SOURCE_DIR}/lib/Polyhedral/core.cpp)\n# pybind11_add_module(place ${PROJECT_SOURCE_DIR}/lib/Polyhedral/placeholer.cpp)\n# pybind11_add_module(example2 ${PROJECT_SOURCE_DIR}/lib/Polyhedral/example2.cpp)\n\n\n# add_library(core MODULE \n#            ${PROJECT_SOURCE_DIR}/lib/Polyhedral/core.cpp\n#     )\n# target_link_libraries(core \n#             pybind11::module\n#   )\n\n"
  },
  {
    "path": "Dockerfile",
    "content": "# This Dockerfile configures a Docker environment that \n# contains all the required packages for the tool\nFROM ubuntu:20.04\nARG UID\nARG GID\nARG VHLS_PATH\nRUN echo \"Group ID: $GID\"\nRUN echo \"User ID: $UID\"\n\nUSER root\nRUN apt-get update -y && apt-get install apt-utils -y\nRUN DEBIAN_FRONTEND=\"noninteractive\" apt-get -y install tzdata\n\n# Install basic packages \nRUN apt-get upgrade -y \nRUN apt-get update -y \\\n    && apt-get install -y clang lld cmake libssl-dev\\\n                          pkg-config g++\\\n                          llvm gcc ninja-build \\\n                          build-essential autoconf libtool\\\n                          git vim wget sudo\n\nCMD [\"bash\"]\n\n# Add dev-user\n# RUN groupadd -o -g $GID dev-user\n# RUN useradd -r -g $GID -u $UID -m -d /home/dev-user -s /sbin/nologin -c \"User\" dev-user\n# RUN echo \"dev-user     ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers\n# USER dev-user\n\n# Install PyTorch and Torch-MLIR\nENV PATH=\"${PATH}:~/.local/bin\"\n# RUN pip3 install --user --upgrade pip \\\n#     && pip3 install pandas dataclasses colorlog pyyaml\n\n\n# Add environment variables\nENV vhls $VHLS_PATH\nRUN printf \"\\\n\\nexport LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:\\$LIBRARY_PATH \\\n\\n# Vitis HLS setup \\\n\\nsource ${vhls}/Vitis/2022.2/settings64.sh \\\n\\nsource ${vhls}/Vitis_HLS/2022.2/settings64.sh \\\n\\nexport PATH=$PATH:/workspace/build/bin:/workspace/scalehls/polygeist/llvm/build/bin:/workspace/scalehls/polygeist/build/bin:~/.local/bin \\\n\\n\" >> ~/.vimrc\n#Add vim environment\nRUN printf \"\\\n\\nset autoread \\\n\\nautocmd BufWritePost *.cpp silent! !clang-format -i <afile> \\\n\\nautocmd BufWritePost *.c   silent! !clang-format -i <afile> \\\n\\nautocmd BufWritePost *.h   silent! !clang-format -i <afile> \\\n\\nautocmd BufWritePost *.hpp silent! !clang-format -i <afile> \\\n\\nautocmd BufWritePost *.cc  silent! !clang-format -i <afile> \\\n\\nautocmd BufWritePost *.py  silent! !python3 -m black <afile> \\\n\\nautocmd BufWritePost *.sv  silent! !verible-verilog-format --inplace <afile> \\\n\\nautocmd BufWritePost *.v  silent! !verible-verilog-format --inplace <afile> \\\n\\nautocmd BufWritePost * redraw! \\\n\\n\" >> ~/.vimrc\n\n# Entrypoint set up\nWORKDIR /home/workspace \n# COPY . /usr/src/workspace\n"
  },
  {
    "path": "LICENSE",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright [yyyy] [name of copyright owner]\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "Makefile",
    "content": "user=$(if $(shell id -u),$(shell id -u),9001)\ngroup=$(if $(shell id -g),$(shell id -g),1000)\n# phism=/workspace\nvhls=/data/Vivado\n\n# docker buildx pruney\n\n# Build docker container\nbuild-docker: \n\t(docker build --build-arg VHLS_PATH=$(vhls) . --tag pom_dev)\n\n# Enter docker container\nshell: build-docker\n\tdocker run -it -v $(shell pwd):/home/workspace -v $(vhls):$(vhls) --name pom_dev pom_dev:latest /bin/bash \n\n# docker exec -it -v /home/wczhang/Xilinx:/home/wczhang/Xilinx flowgnn /bin/bash \n\n\n"
  },
  {
    "path": "README.md",
    "content": "\n# POM: An Optimizing Framework on MLIR for Efficient FPGA-based Accelerator Generation\n![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)\n## 1. Introduction\n\n\nPOM is an end-to-end optimizing framework on MLIR for efficient FPGA-based accelerator generation.  POM has the following technical contributions:  \n- **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.\n- **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.\n- **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.\n- **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.\n\nPlease refer to our [HPCA' 24 ](https://arxiv.org/abs/2401.05154)paper for more details: \n```\n@inproceedings{zhanghpca2024pom,\n  title={An Optimizing Framework on MLIR for Efficient FPGA-based Accelerator Generation},\n  author={Weichuang Zhang and Jieru Zhao and Guan Shen and Quan Chen and Chen Chen and Minyi Guo},\n  booktitle={2024 IEEE International Symposium on High-Performance Computer Architecture (HPCA)},\n  year={2024}\n}\n```\n***\n## 2. Installation\n### 2.1 Install Prerequisite: isl \n```\ngit clone git://repo.or.cz/isl.git  \ncd isl \ngit pull  \ngit submodule init  \ngit submodule update  \n./autogen.sh  \n./configure --with-int=imath  \nmake  \nmake check  \nmake install  \n```\nMore details of isl installation: https://compsys-tools.ens-lyon.fr/iscc/isl.pdf\n\n### 2.2 Install POM\n```\ngit clone --recursive git@github.com:sjtu-zhao-lab/pom.git \ncd pom\n```\n\n### 2.3 Code structure\n```\npom/\n├── scalehls/\n│    ├── polygeist /\n│    │    ├──  llvm-project/ \n```\n\n## 3. Build \n### 3.1 Build scalehls\n\n```\n# Go to scalehls/  \n./build-scalehls.sh\n```\n### 3.2 Build POM\n\n```\n# Go to pom/  \n./build-pom.sh\n```\n***\n## 4. Getting Started with a GEMM kernel\n\n```\n# Go to pom/build/\ncmake --build . --target gemm\n```\n\nYou can run the following instruction to generate an optimized MLIR affine dialect:\n```\n./bin/gemm\n```\nThe optimized IR is stored at pom/samples/gemm/test_gemm_4096.mlir .  \nYou can further translate the optimized IR into HLS C code with the following instruction:\n\n```\n../scalehls/build/bin/scalehls-opt ../samples/gemm/test_gemm_4096.mlir\\\n    --scalehls-func-preprocess=\"top-func=gemm\" \\\n    --scalehls-qor-estimation=\"target-spec=../samples/config.json\" \\\n    | ../scalehls/build/bin/scalehls-translate -emit-hlscpp >  ../samples/gemm/test_gemm_4096.cpp\n```\n## Repository Layout\n- `include` and `lib` : Compiler implementation\n- `scalehls` : the HLS C code generation\n- `testbench`: Kernels and applications described with POM DSL\n- `samples`: The generated designs\n\n## Related Projects\n- [ScaleHLS](https://github.com/hanchenye/scalehls)\n- [Tiramisu](https://github.com/Tiramisu-Compiler/tiramisu)\n- [MLIR](https://mlir.llvm.org/)\n"
  },
  {
    "path": "ae_script.sh",
    "content": "\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Start the experiment workflow\"\necho \"\"\n./build-pom.sh\n\n\n./run-code.sh\n\n\n./tcl-gen.sh\n\n\n./vitis-reports.sh\n\n\n./results-gen.sh\n\nend_time=$(date +\"%s\")\nexecution_time=$(($end_time - $start_time))\necho \"\"\necho \">>> All Steps have been finished!\"\necho \">>> Total Execution Time: $execution_time seconds\"\necho \"\"\n"
  },
  {
    "path": "build-pom.sh",
    "content": "#!/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.\nPOM_DIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" &> /dev/null && pwd )\"\n# POM_DIR=\"$(dirname \"$CURRENT_DIR\")\"\n\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Step 1. Building POM ...\"\necho \"\"\n\n# Got to the build directory.\ncd \"${POM_DIR}\"\nmkdir -p build\ncd build\n\nif [ ! -f \"CMakeCache.txt\" ]; then\n    LLVM_DIR=\"${POM_DIR}/scalehls/build/lib/cmake/llvm\" \\\n    MLIR_DIR=\"${POM_DIR}/scalehls/build/lib/cmake/mlir\" \\\n    cmake -G Ninja .. \n    # -DMLIR_DIR=\"${POM_DIR}/scalehls/build/lib/cmake/mlir\" \\\n    # -DLLVM_EXTERNAL_LIT=\"${POM_DIR}/scalehls/build/bin/llvm-lit\" \nfi\n\ncd ../\n# Run building.\n# targets=(\"edgeDetect\" \"gaussian\" \"blur\" \"vgg16\"  \"resnet\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\n\n\necho \"\"\necho \">>> Step 2. Initializing samples/{testbench}\"\necho \"\"\nfolders=(\"gemm\" \"bicg\" \"gesummv\" \"2mm\" \"3mm\" \"edgeDetect\" \"gaussian\" \"blur\" \"vgg16\"  \"resnet18\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\n\nfor folder in \"${folders[@]}\"\ndo\n    mkdir -p samples/\"${folder}\"\ndone\n\n\n# for target in \"${targets[@]}\"\n# do\n#     cmake --build . --target \"$target\"\n# done\n# end_time=$(date +\"%s\")\n# execution_time=$(($end_time - $start_time))\necho \"\"\necho \">>> Building finished!\"\necho \"\"\n"
  },
  {
    "path": "clean.sh",
    "content": "#!/bin/bash\n\n\nfolders=(\"gemm\" \"bicg\" \"gesummv\" \"2mm\" \"3mm\" \"edgeDetect\" \"gaussian\" \"blur\" \"vgg16\"  \"resnet\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\n\nfor folder in \"${folders[@]}\"\ndo\n    rm -rf \"samples/${folder}\"/*\ndone\n\n"
  },
  {
    "path": "include/CMakeLists.txt",
    "content": "# add_subdirectory(Standalone)\n# add_subdirectory(Dialect)\n# add_subdirectory(Polyhedral)\n"
  },
  {
    "path": "include/polyhedral/codegen.h",
    "content": "#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.h>\n#include <isl/union_set.h>\n#include <isl/ast_build.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n\n// #include \"debug.h\"\n// #include \"expr.h\"\n// #include \"type.h\"\n#include \"function.h\"\n#include \"compute.h\"\n\nnamespace polyfp{\n\nclass var;\nstd::string generate_new_variable_name();\npolyfp::expr traverse_expr_and_replace_non_affine_accesses(polyfp::compute *comp,\n                                                             const polyfp::expr &exp);\n\n}\n\n\n\n#endif"
  },
  {
    "path": "include/polyhedral/compute.h",
    "content": "#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#include <isl/map.h>\n#include <isl/id.h>\n#include <isl/constraint.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build.h>\n#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <unordered_map>\n#include<algorithm>\n#include <isl/val.h>\n#include <map>\n#include \"expr.h\"\n#include \"placeholder.h\"\n#include \"debug.h\"\nnamespace polyfp{\nstd::string generate_new_computation_name();\nclass var;\nclass scheduler;\nclass function;\nclass placeholder;\nclass compute\n{\n  friend function;\n  friend placeholder;\nprivate:\n\n    // The data access map\n    isl_map *access;\n\n    // The isl context of the function.\n    isl_ctx *ctx;\n\n    // The placeholder that stores the results\n    polyfp::placeholder *plhd;\n    polyfp::expr plhd_expr;\n\n    polyfp::primitive_t data_type;\n\n\n    // the expression (or statement) of the function\n    polyfp::expr expression;\n\n    polyfp::function *fct;\n\n    /**\n     * TODO: \n     */\n    std::map<std::string, std::string > access_map;\n\n\n    // The iteration domain of the compute(nested loop)\n    isl_set *iteration_domain;\n\n\n    // The name of the compute(nested loop)\n    std::string name;\n\n    // The number of dimensions in the original definition of the compute\n    int number_of_dims;\n\n    /**\n     * TODO: Add predicates to the nested loops\n     * Derived from Tiramisu:\n     * A predicate around the compute. The compute is executed\n     * only if this predicate is true. This is useful to insert a non-affine\n     * condition around the compute.\n     */\n    polyfp::expr predicate;\n\n\n    // The schedule of the compute.\n    isl_map * schedule;\n\n    /**\n      * Derived from Tiramisu:\n      * Time-processor domain of the compute.\n      * In this representation, the logical time of execution and the\n      * processor where the compute will be executed are both\n      * specified.\n      */\n    isl_set *time_processor_domain;\n\n    \n    // The iteration variables(iterators) of the compute\n    std::vector<polyfp::var> iteration_variables;\n\n    /**\n     * TODO: \n     */\n    std::vector<polyfp::expr> placeholder_dims;\n    std::vector<polyfp::expr > placeholder_accessmap;\n    \n    /**\n    * TODO: add predicate\n    * \\p predicate is an expression that represents constraints on the iteration domain\n    * (for example (i != j). The predicate has to be an affine\n    * expression.\n    */\n    std::string construct_iteration_domain(std::string name, std::vector<var> iterator_variables, \n                                          polyfp::expr predicate);\n\n    // Return the names of iteration domain dimensions.\n    std::vector<std::string> get_iteration_domain_dimension_names();\n\n    void check_dimensions_validity(std::vector<int> dimensions);\n\n    // Get the number of dimensions of the compute\n    int get_iteration_domain_dimensions_number();\n\n    // Check that the names used in \\p dimensions are not already in use.\n    void assert_names_not_assigned(std::vector<std::string> dimensions);\n\n\n\n    /**\n      * Generate an identity schedule for the compute\n      * Derived from Tiramisu:\n      * This identity schedule is an identity relation created from the iteration domain.\n      */\n    isl_map *gen_identity_schedule_for_iteration_domain();\n\n    /**\n      * Generate an identity schedule for the compute.\n      * Derived from Tiramisu:\n      * This identity schedule is an identity relation created from the time-processor domain.\n      */\n    isl_map *gen_identity_schedule_for_time_space_domain();\n\n    // Assign a name to iteration domain dimensions that do not have a name.\n    void name_unnamed_iteration_domain_dimensions();\n\n    // Assign a name to iteration domain dimensions that do not have a name.\n    void name_unnamed_time_space_dimensions();\n\n\n    /**\n      * Set an identity schedule for the compute.\n      * Derived from Tiramisu:\n      * This identity schedule is an identity relation created from the iteration domain.\n      */\n    void set_identity_schedule_based_on_iteration_domain();\n\n    // Set the iteration domain of the compute\n    void set_iteration_domain(isl_set *domain);\n\n    // Set the names of loop levels dimensions.\n    void set_loop_level_names(std::vector<int> loop_levels, std::vector<std::string> names);\n    void set_loop_level_names(std::vector<std::string> names);\n\n    // Set the names of the dimensions of the schedule domain.\n    void set_schedule_domain_dim_names(std::vector<int> loop_levels, std::vector<std::string> names);\n\n    // Return the function where the compute is declared.\n    polyfp::function *get_function() const;\n\n\n    /**\n      * Derived from Tiramisu:\n      * Search the time-space domain (the range of the schedule) and\n      * return the loop level numbers that correspond to the dimensions\n      * named \\p dim.\n      */\n    std::vector<int> get_loop_level_numbers_from_dimension_names(std::vector<std::string> dim_names);\n\n\n    // Intersect set with the context of the compute.\n    isl_set *intersect_set_with_context(isl_set *set);\n\n    /**\n      * Derived from Tiramisu:\n      * Return the time-processor domain of the compute.\n      * In this representation, the logical time of execution and the\n      * processor where the compute will be executed are both specified.\n      */\n    isl_set *get_time_processor_domain() const;\n\n    /**\n      * Derived from Tiramisu:\n      * Return the trimmed time-processor domain.\n      * TODO: The first dimension of the time-processor domain is used\n      * to indicate redundancy of the compute. In POM there is no redundancy \n      * of the compute. This feature will be removed soon.\n      * The trimmed time-processor domain is the time-processor domain\n      * without the dimension that represents the redundancy. We simply\n      * take the time-processor domain and remove the first dimension.\n      */\n    isl_set *get_trimmed_time_processor_domain();\n\n    /**\n      * Derived from Tiramisu:\n      * Update loop level names. This function should be called after each scheduling operation\n      * because scheduling usually changes the original loop level names.\n      * This function erases \\p nb_loop_levels_to_erase loop level names starting from the\n      * loop level \\p start_erasing. It then inserts the loop level names \\p new_names in\n      * \\p start_erasing. In other words, it replaces the names of loop levels from\n      * \\p start_erasing to \\p start_erasing + \\p nb_loop_levels_to_erase with the loop levels\n      * indicated by \\p new_names.  This function sets the non erased loop levels to be equal to the\n      * original loop level names.\n      *\n      * \\p original_loop_level_names : a vector containing the original loop level names (loop level\n      * names before scheduling).\n      *\n      * \\p new_names : the new loop level names.\n      *\n      * \\p start_erasing : start erasing loop levels from this loop level.\n      *\n      * \\p nb_loop_levels_to_erase : number of loop levels to erase.\n      *\n      * Example. Assuming the original loop levels are {i0, i1, i2, i3}\n      *\n      * Calling this->update_names({i0, i1, i2, i3}, {x0, x1}, 1, 2) updates the loop levels to become\n      * {i0, x0, x1, i3}.\n      */\n    void update_names(std::vector<std::string> original_loop_level_names, std::vector<std::string> new_names,\n                      int start_erasing, int nb_loop_levels_to_erase);\n\nprotected:\n\n    isl_ctx *get_ctx() const;\n\n    polyfp::expr get_predicate();\n\n    /**\n      * Return a unique name of compute; made of the following pattern:\n      * [compute name]@[compute address in memory]\n      */\n    const std::string get_unique_name() const;\n\n    // Set the name of the compute.\n    void set_name(const std::string &n);\n\n    void init_computation(std::string iteration_space_str,\n                          polyfp::function *fct,\n                          const polyfp::expr &e,\n                          polyfp::primitive_t t, expr p);\n\n\n    void set_schedule(isl_map *map);\n    void set_schedule(std::string map_str);\n\n    compute(std::string name,std::vector<var> iterator_variables, polyfp::expr e, primitive_t t, expr p);\n\n\npublic:\n    compute();\n    compute(std::string iteration_domain, polyfp::expr e,\n                polyfp::primitive_t t,\n                polyfp::function *fct, expr p);\n\n    \n    int II;\n    bool is_unrolled;\n    long latency;\n    long best_latency = LLONG_MAX;\n    int dsp;\n    int minII;\n\n    std::vector<polyfp::var> get_iteration_variables();\n\n    isl_map * original_schedule;\n    std::map<std::string, std::string > tile_map;\n    std::map<std::string, int > tile_size_map;\n    std::map<std::string, std::string > directive_map;\n    std::map<std::string, std::string > directive_tool_map;\n    std::vector<std::string> original_loop_level_name;\n    std::vector<std::string> final_loop_level_names;\n    std::vector<std::string> final_loop_level_names_reserved;\n    std::vector<int> unroll_factor;\n    std::vector<polyfp::expr> unroll_dimension;\n\n    bool refused = false;\n    std::map<std::string, std::string > temp_access_map;\n\n\n    isl_map * best_schedule;\n    std::map<std::string, std::string > best_tile_map;\n    std::map<std::string, int > best_tile_size_map;\n    std::map<std::string, std::string > best_directive_map;\n    std::map<std::string, std::string > best_directive_tool_map;\n    std::vector<std::string> best_loop_level_names;\n    std::vector<int> best_unroll_factor;\n    std::vector<polyfp::expr> best_unroll_dimension;\n\n    std::map<std::string, int>iterators_location_map;\n    int after_level;\n    int ori_after_level;\n\n    compute(std::string name, std::vector<var> iterator_variables, polyfp::expr e, expr p);\n    compute(std::string name, std::vector<var> iterator_variables, int a, expr p);\n    isl_map *get_access_relation() const;\n\n\n    bool is_tiled = false ;\n    bool is_skewed = false;\n    bool is_optimized = false;\n    bool is_pipelined = false;\n    // bool is_first_opt = false;\n\n    // TODO: Config file\n    int current_factor = 1;\n    int largest_factor = 2;\n\n    std::string iterator_to_skew;\n    std::string iterator_to_modify;\n    int skew_factor;\n\n    std::vector<std::string> get_loop_level_names();\n\n    int get_loop_level_number_from_dimension_name(std::string dim_name)\n    {\n        return this->get_loop_level_numbers_from_dimension_names({dim_name})[0];\n    }\n\n    // Debug\n    void dump_iteration_domain() const;\n\n    // Debug\n    void dump_schedule() const;\n\n    // Debug\n    void dump() const;\n\n    void gen_time_space_domain();\n\n    primitive_t get_data_type() const;\n\n    const polyfp::expr &get_expr() const;\n\n    std::vector<polyfp::expr> get_placeholder_dims();\n\n    void set_placeholder_dims(std::vector<polyfp::expr> temp);\n\n    int get_loop_levels_number();\n\n    isl_set *get_iteration_domain() const;\n\n\n    std::vector<polyfp::expr> compute_buffer_size();\n    std::map<std::string, std::string > get_access_map();\n    std::map<std::string, std::string > get_tile_map();\n    std::map<std::string, int > get_tile_size_map();\n    std::map<std::string, std::string > get_directive_map();\n    std::map<std::string, std::string > get_directive_tool_map();\n    void update_leader_components(polyfp::compute *comp);\n    void delete_leader_components(polyfp::compute *comp);\n\n\n    // DSE components\n    std::map<polyfp::compute *, int> components;\n    std::map<int, polyfp::compute *> component_level_map;\n    polyfp::compute *leader;\n\n    std::unordered_map<int, polyfp::compute *> childern;\n    std::vector<polyfp::compute * > parents;\n\n    bool is_leader;\n    bool has_a_leader;\n    bool is_top_parent;\n    bool is_leaf;\n\n    void dump_components();\n    void dump_loads_stores();\n\n    const std::string &get_name() const;\n\n    isl_map *get_schedule() const;\n\n    void set_expression(const polyfp::expr &e);\n\n    void set_access(std::string access_str);\n    void set_access(isl_map *access);\n\n    placeholder *get_placeholder();\n    expr get_placeholder_expr();\n\n\n    // OPT \n    virtual void interchange(var L0, var L1);\n    virtual void interchange(int L0, int L1);\n\n    virtual void split(var L0, int sizeX);\n    virtual void split(var L0, int sizeX, var L0_outer, var L0_inner);\n    virtual void split(int L0, int sizeX);\n\n    virtual void tile(var L0, var L1, int sizeX, int sizeY);\n    virtual void tile(var L0, var L1, int sizeX, int sizeY,\n                      var L0_outer, var L1_outer, var L0_inner, var L1_inner);\n    virtual void tile(var L0, var L1, var L2, int sizeX, int sizeY, int sizeZ);\n    virtual void tile(var L0, var L1, var L2, int sizeX, int sizeY, int sizeZ,\n                      var L0_outer, var L1_outer, var L2_outer, var L0_inner,\n                      var L1_inner, var L2_inner);\n    virtual void tile(int L0, int L1, int sizeX, int sizeY);\n    virtual void tile(int L0, int L1, int L2, int sizeX, int sizeY, int sizeZ);\n\n    virtual void skew(var i, var j, int a , int b, var ni, var nj);\n    virtual void skew(int i, int j, int a, int b); \n\n    void after(compute &comp, polyfp::var iterator);\n    void after(compute &comp, int level);\n    void after(compute *comp, polyfp::var iterator);\n    void after(compute *comp, int level);\n\n    void after_low_level(compute &comp, int level);\n    void after_low_level(compute &comp, std::vector<int> levels);\n\n    void pipeline(polyfp::expr dim, int II);\n    void unroll(polyfp::expr dim, int factor);\n\n    std::map<int, std::map<std::string, std::vector<polyfp::expr> > > map_loadstores;\n    std::vector<polyfp::expr> get_loads();\n    void get_loads_stores();\n    void get_all_loadstores();\n    void auto_loop_transformation();\n    void compute_dependence_vectors();\n    std::unordered_map<std::string, polyfp::expr *> load_map;\n    std::unordered_map<std::string, polyfp::expr *> store_map;\n    std::vector<polyfp::expr *> load_vector;\n    std::vector<polyfp::expr *> store_vector;\n    std::map<polyfp::expr *, std::vector<std::vector<int> > >  map_dependence_vectors;\n\n    void dump_all_loadstores();\n    void check_loop_interchange();\n    void check_loop_skewing();\n    void apply_opt_strategy(std::vector<int>);\n\n    bool opt_finished = false;\n    bool is_skewed_inDSE = false;\n    std::vector<int> final_strategy;\n    std::vector<int> current_strategy;\n    std::vector<int> temp_strategy;\n\n    const static int root_dimension = -1;\n    \n    template<typename... Args> polyfp::expr operator()(Args... args)\n    {\n        std::vector<polyfp::expr> access_expressions{std::forward<Args>(args)...};\n        if (access_expressions.size() != this->number_of_dims)\n        {\n            polyfp::str_dump(\"Error - Incorrect access: \" + this->get_name() + \"(\");\n            for (int i = 0; i < access_expressions.size(); i++)\n            {\n                polyfp::expr e = access_expressions[i];\n                e.dump(false);\n                if (i != access_expressions.size() - 1)\n                    polyfp::str_dump(\", \");\n            }\n            polyfp::str_dump(\").\\n\");\n            polyfp::str_dump(\"The number of access dimensions does not match that used in the declaration of \" + this->get_name() + \".\\n\\n\");\n            exit(1);\n        }\n        return polyfp::expr(polyfp::o_access,\n                              this->get_name(),\n                              access_expressions,\n                              this->get_data_type());\n    }\n    operator expr();\n};\n\n\n}\n\n\n#endif\n"
  },
  {
    "path": "include/polyhedral/core.h",
    "content": "#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#include <isl/union_set.h>\n#include <isl/ast_build.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n\n// #include \"debug.h\"\n#include \"expr.h\"\n#include \"type.h\"\n#include \"codegen.h\"\n\nnamespace polyfp{\n\nclass compute;\nclass constant;\nclass generator;\n\nvoid init(std::string name);\nvoid init();\nvoid codegen();\n\ncompute *get_computation_annotated_in_a_node(isl_ast_node *node);\nint loop_level_into_dynamic_dimension(int level);\nint loop_level_into_static_dimension(int level);\nint dynamic_dimension_into_loop_level(int dim);\n\nisl_map *add_eq_to_schedule_map(int dim0, int in_dim_coefficient, int out_dim_coefficient,\n                                int const_conefficient, isl_map *sched);\n}\n\n#endif"
  },
  {
    "path": "include/polyhedral/debug.h",
    "content": "#ifndef _H_DEBUG_\n#define _H_DEBUG_\n\n#include <iostream>\n\n\nnamespace polyfp\n{\n\nvoid str_dump(const std::string &str);\nvoid str_dump(const std::string &str, const char *str2);\nvoid str_dump(const char *str, const char *str2);\nvoid print_indentation();\n\nextern int polyfp_indentation;\n\n} // namespace polyfp\n\n#define ERROR(message, exit_program) {                      \\\n    std::cerr << \"Error in \" << __FILE__ << \":\"             \\\n              << __LINE__ << \" - \" << message << std::endl; \\\n    if (exit_program)                                       \\\n    {                                                       \\\n        exit(1);                                            \\\n    }                                                       \\\n}\n\n#endif\n"
  },
  {
    "path": "include/polyhedral/expr.h",
    "content": "#ifndef _H_polyfp_EXPR_\n#define _H_polyfp_EXPR_\n#include<algorithm>\n\n#include <map>\n#include <unordered_map>\n#include <vector>\n#include <assert.h>\n#include \"debug.h\"\n#include \"type.h\"\n\nnamespace polyfp\n{\nclass function;\nclass compute;\n\nstd::string generate_new_variable_name();\nstd::string str_from_polyfp_type_expr(polyfp::expr_t type);\nstd::string str_polyfp_type_op(polyfp::op_t type);\nstd::string str_from_polyfp_type_primitive(polyfp::primitive_t type);\n\n// class placeholder;\nclass expr;\nclass var;\nclass global;\n\ntemplate <typename T>\nusing only_integral = typename std::enable_if<std::is_integral<T>::value, expr>::type;\n\nclass global\n{\nprivate:\n\n    static primitive_t loop_iterator_type;\n    static function *implicit_fct;\n\npublic:\n\n    static std::string generate_new_placeholder_name()\n    {\n        static int counter = 0;\n        return \"b\" + std::to_string(counter++);\n    }\n\n    static std::string generate_new_constant_name()\n    {\n        static int counter = 0;\n        return \"C\" + std::to_string(counter++);\n    }\n\n    static function *get_implicit_function()\n    {\n        return global::implicit_fct;\n    }\n\n    static void set_implicit_function(function *fct)\n    {\n        global::implicit_fct = fct;\n    }\n\n    // TODO: The default data type\n    static void set_default_polyfp_options()\n    {\n        global::loop_iterator_type = p_float32;\n    }\n\n    static void set_loop_iterator_type(primitive_t t) {\n        global::loop_iterator_type = t;\n    }\n\n    static primitive_t get_loop_iterator_data_type()\n    {\n        return global::loop_iterator_type;\n    }\n\n    global()\n    {\n        set_default_polyfp_options();\n    }\n};\n\n\nclass expr\n{\n    friend class var;\n    friend class computation;\n    friend class generator;\n    friend class p_max;\n\n    // The type of the operator.\n    polyfp::op_t _operator;\n    std::vector<polyfp::expr> op;\n\n    union\n    {\n        uint8_t     uint8_value;\n        int8_t      int8_value;\n        uint16_t    uint16_value;\n        int16_t     int16_value;\n        uint32_t    uint32_value;\n        int32_t     int32_value;\n        uint64_t    uint64_value;\n        int64_t     int64_value;\n        float       float32_value;\n        double      float64_value;\n    };\n\n\n    // e.g. {i, j}\n    std::vector<polyfp::expr> access_vector;\n\n    bool defined;\n\nprotected:\n\n    std::string name;\n\n    polyfp::primitive_t dtype;\n\n    polyfp::expr_t etype;\n\npublic:\n\n  \n    polyfp::compute *owner;\n\n    // Create an undefined expression.\n    expr()\n    {\n        this->defined = false;\n\n        this->_operator = polyfp::o_none;\n        this->etype = polyfp::e_none;\n        this->dtype = polyfp::p_none;\n    }\n\n    // Create an undefined expression with type.\n    expr(polyfp::primitive_t dtype)\n    {\n        this->defined = false;\n        this->_operator = polyfp::o_none;\n        this->etype = polyfp::e_none;\n        this->dtype = dtype;\n    }\n\n\n    /**\n      * Create an expression for a unary operator that applies\n      * on a variable. For example: allocate(A) or free(B).\n      */\n    expr(polyfp::op_t o, std::string name)\n    {\n        this->_operator = o;\n        this->etype = polyfp::e_op;\n        this->dtype = polyfp::p_none;\n        this->defined = true;\n        this->name = name;\n    }\n\n    /**\n     * Construct an expression for a binary operator.\n     */\n    expr(polyfp::op_t o, polyfp::expr expr0, polyfp::expr expr1)\n    {\n        if (expr0.get_data_type() != expr1.get_data_type())\n        {\n            polyfp::str_dump(\"Binary operation between two expressions of different types:\\n\");\n            expr0.dump(false);\n            polyfp::str_dump(\" (\" + str_from_polyfp_type_primitive(expr0.get_data_type()) + \")\");\n            polyfp::str_dump(\" and \");\n            expr1.dump(false);\n            polyfp::str_dump(\" (\" + str_from_polyfp_type_primitive(expr1.get_data_type()) + \")\");\n            polyfp::str_dump(\"\\n\");\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);\n        }\n\n        this->_operator = o;\n        this->etype = polyfp::e_op;\n        this->dtype = expr0.get_data_type();\n        this->defined = true;\n\n        this->op.push_back(expr0);\n        this->op.push_back(expr1);\n    }\n\n\n    // Construct an access\n    expr(polyfp::op_t o, std::string name,\n         std::vector<polyfp::expr> vec,\n         polyfp::primitive_t type)\n    {\n        assert(((o == polyfp::o_access) || (o == polyfp::o_placeholder)) &&\n               \"The operator is not an access or a placeholder operator.\");\n\n        assert(vec.size() > 0);\n        assert(name.size() > 0);\n\n        this->_operator = o;\n        this->etype = polyfp::e_op;\n        this->dtype = type;\n        this->defined = true;\n\n        if (o == polyfp::o_access || o == polyfp::o_placeholder)\n        {\n            this->set_access(vec);\n        }\n        else\n        {\n            ERROR(\"Type of operator is not o_access or o_placeholder, or o_lin_index.\", true);\n        }\n\n        this->name = name;\n    }\n\n    // Construct an unsigned 8-bit integer expression.\n    expr(uint8_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n        this->dtype = polyfp::p_uint8;\n        this->uint8_value = val;\n    }\n\n    // Construct a signed 8-bit integer expression.\n    expr(int8_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n        this->dtype = polyfp::p_int8;\n        this->int8_value = val;\n    }\n\n    // Construct an unsigned 16-bit integer expression.\n    expr(uint16_t val)\n    {\n        this->defined = true;\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->dtype = polyfp::p_uint16;\n        this->uint16_value = val;\n    }\n\n    expr(int16_t val)\n    {\n        this->defined = true;\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n\n        this->dtype = polyfp::p_int16;\n        this->int16_value = val;\n    }\n\n    expr(uint32_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_uint32;\n        this->uint32_value = val;\n    }\n\n    expr(int32_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_int32;\n        this->int32_value = val;\n    }\n\n    expr(uint64_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_uint64;\n        this->uint64_value = val;\n    }\n\n    expr(int64_t val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_int64;\n        this->int64_value = val;\n    }\n\n    expr(float val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_float32;\n        this->float32_value = val;\n    }\n\n    polyfp::expr copy() const;\n\n    expr(double val)\n    {\n        this->etype = polyfp::e_val;\n        this->_operator = polyfp::o_none;\n        this->defined = true;\n\n        this->dtype = polyfp::p_float64;\n        this->float64_value = val;\n    }\n\n    uint8_t get_uint8_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_uint8);\n\n        return uint8_value;\n    }\n\n    int8_t get_int8_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_int8);\n\n        return int8_value;\n    }\n\n    uint16_t get_uint16_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_uint16);\n\n        return uint16_value;\n    }\n\n    int16_t get_int16_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_int16);\n\n        return int16_value;\n    }\n\n    uint32_t get_uint32_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_uint32);\n\n        return uint32_value;\n    }\n\n    int32_t get_int32_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_int32);\n\n        return int32_value;\n    }\n\n    uint64_t get_uint64_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_uint64);\n\n        return uint64_value;\n    }\n\n    int64_t get_int64_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_int64);\n\n        return int64_value;\n    }\n\n    float get_float32_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_float32);\n\n        return float32_value;\n    }\n\n    double get_float64_value() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n        assert(this->get_data_type() == polyfp::p_float64);\n\n        return float64_value;\n    }\n\n    int64_t get_int_val() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n\n        int64_t result = 0;\n\n        if (this->get_data_type() == polyfp::p_uint8)\n        {\n            result = this->get_uint8_value();\n        }\n        else if (this->get_data_type() == polyfp::p_int8)\n        {\n            result = this->get_int8_value();\n        }\n        else if (this->get_data_type() == polyfp::p_uint16)\n        {\n            result = this->get_uint16_value();\n        }\n        else if (this->get_data_type() == polyfp::p_int16)\n        {\n            result = this->get_int16_value();\n        }\n        else if (this->get_data_type() == polyfp::p_uint32)\n        {\n            result = this->get_uint32_value();\n        }\n        else if (this->get_data_type() == polyfp::p_int32)\n        {\n            result = this->get_int32_value();\n        }\n        else if (this->get_data_type() == polyfp::p_uint64)\n        {\n            result = this->get_uint64_value();\n        }\n        else if (this->get_data_type() == polyfp::p_int64)\n        {\n            result = this->get_int64_value();\n        }\n        else if (this->get_data_type() == polyfp::p_float32)\n        {\n            result = this->get_float32_value();\n        }\n        else if (this->get_data_type() == polyfp::p_float64)\n        {\n            result = this->get_float64_value();\n        }\n        else\n        {\n            ERROR(\"Calling get_int_val() on a non integer expression.\", true);\n        }\n\n        return result;\n    }\n\n    double get_double_val() const\n    {\n        assert(this->get_expr_type() == polyfp::e_val);\n\n        double result = 0;\n\n        if (this->get_data_type() == polyfp::p_float32)\n        {\n            result = this->get_float32_value();\n        }\n        else if (this->get_data_type() == polyfp::p_float64)\n        {\n            result = this->get_float64_value();\n        }\n        else\n        {\n            ERROR(\"Calling get_double_val() on a non double expression.\", true);\n        }\n\n        return result;\n    }\n\n    /**\n      * Return the value of the \\p i 'th operand of the expression.\n      * \\p i can be 0, 1 or 2.\n      */\n    const polyfp::expr &get_operand(int i) const\n    {\n        assert(this->get_expr_type() == polyfp::e_op);\n        assert((i < (int)this->op.size()) && \"Operand index is out of bounds.\");\n\n        return this->op[i];\n    }\n\n    // Return the number of arguments of the operator.\n    int get_n_arg() const\n    {\n        assert(this->get_expr_type() == polyfp::e_op);\n\n        return this->op.size();\n    }\n\n    polyfp::expr_t get_expr_type() const\n    {\n        return etype;\n    }\n\n    polyfp::primitive_t get_data_type() const\n    {\n        return dtype;\n    }\n\n    const std::string &get_name() const\n    {\n        assert(\n            (this->get_expr_type() == polyfp::e_var) ||\n               (this->get_op_type() == polyfp::o_access) ||\n               (this->get_op_type() == polyfp::o_placeholder));\n        return name;\n    }\n\n    void set_name(std::string &name)\n    {\n        assert((this->get_expr_type() == polyfp::e_var) ||\n               (this->get_op_type() == polyfp::o_access));\n\n        this->name = name;\n    }\n\n    polyfp::expr replace_op_in_expr(const std::string &to_replace,\n                                      const std::string &replace_with)\n    {\n        if (this->name == to_replace) {\n            this->name = replace_with;\n            return *this;\n        }\n        for (int i = 0; i < this->op.size(); i++) {\n            polyfp::expr operand = this->get_operand(i);\n            this->op[i] = operand.replace_op_in_expr(to_replace, replace_with);\n        }\n        return *this;\n    }\n\n    // Get the type of the operator (polyfp::op_t)\n    polyfp::op_t get_op_type() const\n    {\n        return _operator;\n    }\n\n    // e.g. For a placeholder access A[i+1,j], it will return {i+1, j}\n    const std::vector<polyfp::expr> &get_access() const\n    {\n        assert(this->get_expr_type() == polyfp::e_op);\n        assert(this->get_op_type() == polyfp::o_access || this->get_op_type() == polyfp::o_placeholder);\n\n        return access_vector;\n    }\n\n\n\n    // Get the number of dimensions in the access vector.\n    int get_n_dim_access() const\n    {\n        assert(this->get_expr_type() == polyfp::e_op);\n        assert(this->get_op_type() == polyfp::o_access);\n\n        return access_vector.size();\n    }\n\n    bool is_defined() const\n    {\n        return defined;\n    }\n\n\n    bool is_equal(polyfp::expr e) const\n    {\n        bool equal = true;\n\n        if ((this->_operator != e._operator) ||\n            (this->op.size() != e.op.size()) ||\n            (this->access_vector.size()   != e.access_vector.size())   ||\n            (this->defined != e.defined)     ||\n            (this->name != e.name)           ||\n            (this->dtype != e.dtype)         ||\n            (this->etype != e.etype))\n        {\n            equal = false;\n            return equal;\n        }\n\n        for (int i = 0; i < this->access_vector.size(); i++)\n            equal = equal && this->access_vector[i].is_equal(e.access_vector[i]);\n\n        for (int i = 0; i < this->op.size(); i++)\n            equal = equal && this->op[i].is_equal(e.op[i]);\n\n        if ((this->etype == e_val) && (e.etype == e_val))\n        {\n            if (this->get_int_val() != e.get_int_val())\n                equal = false;\n            if ((this->get_data_type() == polyfp::p_float32) ||\n                (this->get_data_type() == polyfp::p_float64))\n                if (this->get_double_val() != e.get_double_val())\n                    equal = false;\n        }\n\n        return equal;\n    }\n\n    bool is_integer() const\n    {\n        return this->get_expr_type() == e_val &&\n                (this->get_data_type() == p_uint8 ||\n                 this->get_data_type() == p_uint16 ||\n                 this->get_data_type() == p_uint32 ||\n                 this->get_data_type() == p_uint64 ||\n                 this->get_data_type() == p_int16 ||\n                 this->get_data_type() == p_int32 ||\n                 this->get_data_type() == p_int8 ||\n                 this->get_data_type() == p_int64);\n    }\n\n\n    expr operator+(polyfp::expr other) const;\n    expr operator-(polyfp::expr other) const;\n    expr operator/(polyfp::expr other) const;\n    expr operator*(polyfp::expr other) const;\n    expr operator%(polyfp::expr other) const;\n    expr operator>>(polyfp::expr other) const;\n\n    // TODO: Extensions\n    // Expression multiplied by (-1).\n\n    polyfp::expr& operator=(polyfp::expr const &);\n\n    void set_access(std::vector<polyfp::expr> vector)\n    {\n        access_vector = vector;\n    }\n\n    void set_access_dimension(int i, polyfp::expr acc)\n    {\n        assert((i < (int)this->access_vector.size()) && \"index is out of bounds.\");\n        access_vector[i] = acc;\n    }\n\n    void get_access_vector(std::vector<polyfp::expr> &loads) const{\n\n        switch (this->etype){\n            case polyfp::e_op:\n            {\n                if (this->get_n_arg() > 0)\n                {\n                    for (int i = 0; i < this->get_n_arg(); i++)\n                    {\n                        this->op[i].get_access_vector(loads);\n                    }\n                }\n                if ((this->get_op_type() == polyfp::o_access))\n                {\n                    // std::cout << \"Access to \" +  this->get_name() + \". Access expressions:\" << std::endl;\n                    loads.push_back(*this);\n                }\n                \n                break;\n            }\n            case (polyfp::e_val):\n            {\n                // TODO: \n                // if (this->get_data_type() == polyfp::p_uint8)\n                // {\n                //     std::cout << \"Value:\" << this->get_uint8_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_int8)\n                // {\n                //     std::cout << \"Value:\" << this->get_int8_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_uint16)\n                // {\n                //     std::cout << \"Value:\" << this->get_uint16_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_int16)\n                // {\n                //     std::cout << \"Value:\" << this->get_int16_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_uint32)\n                // {\n                //     std::cout << \"Value:\" << this->get_uint32_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_int32)\n                // {\n                //     std::cout << \"Value:\" << this->get_int32_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_uint64)\n                // {\n                //     std::cout << \"Value:\" << this->get_uint64_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_int64)\n                // {\n                //     std::cout << \"Value:\" << this->get_int64_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_float32)\n                // {\n                //     std::cout << \"Value:\" << this->get_float32_value() << std::endl;\n                // }\n                // else if (this->get_data_type() == polyfp::p_float64)\n                // {\n                //     std::cout << \"Value:\" << this->get_float64_value() << std::endl;\n                // }\n                break;\n            }\n            case (polyfp::e_var):\n            {\n                // TODO:\n                // std::cout << \"Var name:\" << this->get_name() << std::endl;\n                // std::cout << \"Expression value type:\" << str_from_polyfp_type_primitive(this->dtype) << std::endl;\n                break;\n            }\n        } \n\n    }\n\n    void dump(bool exhaustive) const\n    {\n        if (this->get_expr_type() != e_none)\n        {\n            if (exhaustive == true)\n            {\n                if (this->is_defined())\n                {\n                    std::cout << \"Expression:\" << std::endl;\n                    std::cout << \"Expression type:\" << str_from_polyfp_type_expr(this->etype) << std::endl;\n                    switch (this->etype)\n                    {\n                    case polyfp::e_op:\n                    {\n                        std::cout << \"Expression operator type:\" << str_polyfp_type_op(this->_operator) << std::endl;\n                        if (this->get_n_arg() > 0)\n                        {\n                            std::cout << \"Number of operands:\" << this->get_n_arg() << std::endl;\n                            std::cout << \"Dumping the operands:\" << std::endl;\n                            for (int i = 0; i < this->get_n_arg(); i++)\n                            {\n                                std::cout << \"Operand \" << std::to_string(i) << \".\" << std::endl;\n                                this->op[i].dump(exhaustive);\n                            }\n                        }\n                        if ((this->get_op_type() == polyfp::o_access))\n                        {\n                            std::cout << \"Access to \" +  this->get_name() + \". Access expressions:\" << std::endl;\n                            for (const auto &e : this->get_access())\n                            {\n                                e.dump(exhaustive);\n                            }\n                        }\n                        \n                        break;\n                    }\n                    case (polyfp::e_val):\n                    {\n                        std::cout << \"Expression value type:\" << str_from_polyfp_type_primitive(this->dtype) << std::endl;\n\n                        if (this->get_data_type() == polyfp::p_uint8)\n                        {\n                            std::cout << \"Value:\" << this->get_uint8_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_int8)\n                        {\n                            std::cout << \"Value:\" << this->get_int8_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_uint16)\n                        {\n                            std::cout << \"Value:\" << this->get_uint16_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_int16)\n                        {\n                            std::cout << \"Value:\" << this->get_int16_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_uint32)\n                        {\n                            std::cout << \"Value:\" << this->get_uint32_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_int32)\n                        {\n                            std::cout << \"Value:\" << this->get_int32_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_uint64)\n                        {\n                            std::cout << \"Value:\" << this->get_uint64_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_int64)\n                        {\n                            std::cout << \"Value:\" << this->get_int64_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_float32)\n                        {\n                            std::cout << \"Value:\" << this->get_float32_value() << std::endl;\n                        }\n                        else if (this->get_data_type() == polyfp::p_float64)\n                        {\n                            std::cout << \"Value:\" << this->get_float64_value() << std::endl;\n                        }\n                        break;\n                    }\n                    case (polyfp::e_var):\n                    {\n                        std::cout << \"Var name:\" << this->get_name() << std::endl;\n                        std::cout << \"Expression value type:\" << str_from_polyfp_type_primitive(this->dtype) << std::endl;\n                        break;\n                    }\n\n                    }\n                }\n            }\n            else\n            {   std::cout << \"dump expression\"<<std::endl;\n                std::cout << this->to_str();\n            }\n        }\n    }\n\n    bool is_constant() const\n    {\n        if (this->get_expr_type() == polyfp::e_val)\n            return true;\n        else\n            return false;\n    }\n\n    int get_dependence_vector() const{\n        // TODO: a more general method to calculate dependence vector\n        // Already supported: A(i+4,j-5)-> A(i,j-1)\n        // Not supported: A(2*i,j), A(i+j, j+9)\n        int temp;\n        if (this->get_expr_type() == e_op){\n            switch (this->get_op_type()){\n                case polyfp::o_add:\n                    if ((this->get_operand(0).get_expr_type() == polyfp::e_val)){\n                        temp = this->get_operand(0).get_int_val();\n                    }else if((this->get_operand(1).get_expr_type() == polyfp::e_val)){\n                        temp = this->get_operand(1).get_int_val();\n                    }else{\n                        std::cout<<\"not supported type\"<<std::endl;\n                        return false;\n                    }\n                case polyfp::o_sub:\n                    if ((this->get_operand(0).get_expr_type() == polyfp::e_val)){\n                        temp = -(this->get_operand(1).get_int_val());\n                    }else if((this->get_operand(1).get_expr_type() == polyfp::e_val)){\n                        temp = -(this->get_operand(1).get_int_val());\n                    }else{\n                        std::cout<<\"not supported type\"<<std::endl;\n                        return false;\n                    }\n            }\n        }else if(this->get_expr_type() == e_var){\n            temp = 0;\n            \n\n        }else{\n            std::cout<<\"not supported type\"<<std::endl;\n            return false;\n        }\n        return temp;\n\n\n    }\n\n\n\n    // Simplify the expression.\n    polyfp::expr simplify() const\n    {\n        if (this->get_expr_type() != e_none)\n        {\n            switch (this->etype)\n            {\n                case polyfp::e_op:\n                {\n                    switch (this->get_op_type())\n                    {\n                    case polyfp::o_max:\n                        return *this;\n                    case polyfp::o_min:\n                        return *this;\n                    case polyfp::o_add:\n                        this->get_operand(0).simplify();\n                        this->get_operand(1).simplify();\n                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))\n                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))\n                                return expr(this->get_operand(0).get_int_val() + this->get_operand(1).get_int_val());\n                    case polyfp::o_sub:\n                        this->get_operand(0).simplify();\n                        this->get_operand(1).simplify();\n                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))\n                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))\n                                return expr(this->get_operand(0).get_int_val() - this->get_operand(1).get_int_val());\n                    case polyfp::o_mul:\n                        this->get_operand(0).simplify();\n                        this->get_operand(1).simplify();\n                        if ((this->get_operand(0).get_expr_type() == polyfp::e_val) && (this->get_operand(1).get_expr_type() == polyfp::e_val))\n                            if ((this->get_operand(0).get_data_type() == polyfp::p_int32))\n                                return expr(this->get_operand(0).get_int_val() * this->get_operand(1).get_int_val());\n                    case polyfp::o_div:\n                        return *this;\n                    case polyfp::o_mod:\n                        return *this;\n                    case polyfp::o_access:\n                        return *this;\n                    default:\n                        ERROR(\"Simplifying an unsupported polyfp expression.\", 1);\n                    }\n                    break;\n                }\n                case (polyfp::e_val):\n                {\n                    return *this;\n                }\n                case (polyfp::e_var):\n                {\n                    return *this;\n                }\n                default:\n                    ERROR(\"Expression type not supported.\", true);\n            }\n        }\n\n        return *this;\n    }\n#include <iostream>\n\n    std::string to_str() const\n    {\n        std::string str = std::string(\"\");\n\n        if (this->get_expr_type() != e_none)\n        {\n            // std::cout<<this->get_expr_type();\n                switch (this->etype)\n                {\n                case polyfp::e_op:\n                {\n                    switch (this->get_op_type())\n                    {\n                    case polyfp::o_max:\n                        str +=  \"max(\" + this->get_operand(0).to_str();\n                        str +=  \", \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_min:\n                        str +=  \"min(\" + this->get_operand(0).to_str();\n                        str +=  \", \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_add:\n                        str +=  \"(\" + this->get_operand(0).to_str();\n                        str +=  \" + \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_sub:\n                        str +=  \"(\" + this->get_operand(0).to_str();\n                        str +=  \" - \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_mul:\n                        str +=  \"(\" + this->get_operand(0).to_str();\n                        str +=  \" * \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_div:\n                        str +=  \"(\" + this->get_operand(0).to_str();\n                        str +=  \" / \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_mod:\n                        str +=  \"(\" + this->get_operand(0).to_str();\n                        str +=  \" % \" + this->get_operand(1).to_str();\n                        str +=  \")\";\n                        break;\n                    case polyfp::o_access:\n                    case polyfp::o_placeholder:\n                        str +=  this->get_name() + \"(\";\n                        for (int k = 0; k < this->get_access().size(); k++)\n                        {\n                            if (k != 0)\n                            {\n                                str +=  \", \";\n                            }\n                            str += this->get_access()[k].to_str();\n                        }\n                        str +=  \")\";\n                        break;\n                    default:\n                        ERROR(\"Dumping an unsupported polyfp expression.\", 1);\n                    }\n                    break;\n                }\n                case (polyfp::e_val):\n                {\n                    if (this->get_data_type() == polyfp::p_uint8)\n                    {\n                        str +=  std::to_string((int)this->get_uint8_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_int8)\n                    {\n                        str +=  std::to_string((int)this->get_int8_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_uint16)\n                    {\n                        str +=  std::to_string(this->get_uint16_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_int16)\n                    {\n                        str +=  std::to_string(this->get_int16_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_uint32)\n                    {\n                        str +=  std::to_string(this->get_uint32_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_int32)\n                    {\n                        str +=  std::to_string(this->get_int32_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_uint64)\n                    {\n                        str +=  std::to_string(this->get_uint64_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_int64)\n                    {\n                        str +=  std::to_string(this->get_int64_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_float32)\n                    {\n                        str +=  std::to_string(this->get_float32_value());\n                    }\n                    else if (this->get_data_type() == polyfp::p_float64)\n                    {\n                        str +=  std::to_string(this->get_float64_value());\n                    }\n                    break;\n                }\n                case (polyfp::e_var):\n                {\n                    str += this->get_name();\n                    break;\n                }\n\n                default:\n                    ERROR(\"Expression type not supported.\", true);\n                }\n            }\n\n          return str;\n        }\n\n};\n\n\nclass var: public polyfp::expr\n{\n    friend compute;\nprivate:\n\n    static std::unordered_map<std::string, var> declared_vars;\n    expr lower;\n    expr upper;\n\npublic:\n\n    // Return the upper bound of this variable.\n    expr get_upper()\n    {\n\t    return upper;\n    }\n\n    expr get_lower()\n    {\n\t    return lower;\n    }\n\n    var(std::string name);\n\n    var(std::string name, polyfp::primitive_t type);\n\n    var(std::string name, int lower_bound, int upper_bound) : var(name)\n    {\n        lower = expr((int32_t) lower_bound);\n        upper = expr((int32_t) upper_bound);\n        // flag = 0;\n\n    }\n    var(std::string name, expr lower_bound, expr upper_bound) : var(name)\n    {\n        lower = lower_bound;\n        upper = upper_bound;\n        // flag = 0;\n    }\n     var(std::string name, int lower_bound, expr upper_bound) : var(name)\n    {\n        lower = expr((int32_t) lower_bound);\n        upper = upper_bound;\n        // flag = 0;\n\n    }\n\n    var(): var(generate_new_variable_name()) {}\n\n    void show(){\n        std::cout << \"Saved variable \" << this->name << \" of type \" << str_from_polyfp_type_primitive(this->dtype)<<std::endl;\n    }\n\n\n};\n\n\nclass constant: public polyfp::expr\n{\n    friend compute;\n    friend function;\nprivate:\n    expr value;\n    float float_value;\n    polyfp::primitive_t datatype;\n    polyfp::function *func;\n    \npublic:\n\n\n    constant(float value = 0, polyfp::primitive_t t = p_float32, polyfp::function *fct = global::get_implicit_function());\n    polyfp::primitive_t get_type() const;\n    \n};\n\nclass p_max: public polyfp::expr\n{\n    friend compute;\n    friend function;\nprivate:\n    expr left_value;\n    expr right_value;\n    polyfp::function *func;\n\n    \npublic:\n    p_max( polyfp::expr value1, polyfp::expr value2, polyfp::op_t o = polyfp::o_max, polyfp::function *fct = global::get_implicit_function());\n\n};\n\n\n/**\n  * Takes in a primitive value \\p val, and returns an expression\n  * of polyfp type \\p tT that represents \\p val.\n  */\ntemplate <typename cT>\nexpr value_cast(primitive_t tT, cT val) {\n\n    switch (tT) {\n        case p_int8:\n            return expr{static_cast<int8_t>(val)};\n        case p_uint8:\n            return expr{static_cast<uint8_t>(val)};\n        case p_int16:\n            return expr{static_cast<int16_t>(val)};\n        case p_uint16:\n            return expr{static_cast<uint16_t>(val)};\n        case p_int32:\n            return expr{static_cast<int32_t>(val)};\n        case p_uint32:\n            return expr{static_cast<uint32_t>(val)};\n        case p_int64:\n            return expr{static_cast<int64_t>(val)};\n        case p_uint64:\n            return expr{static_cast<uint64_t>(val)};\n        case p_float32:\n            return expr{static_cast<float>(val)};\n        case p_float64:\n            return expr{static_cast<double>(val)};\n        default:\n            throw std::invalid_argument{\"Type not supported\"};\n    }\n}\n\n\n\ntemplate <typename T>\nonly_integral<T> operator+(const polyfp::expr &e, T val)\n{\n    return e + value_cast(e.get_data_type(), val);\n}\n\ntemplate <typename T>\nonly_integral<T> operator+(T val, const polyfp::expr &e)\n{\n    return value_cast(e.get_data_type(), val) + e;\n}\n\ntemplate <typename T>\nonly_integral<T> operator-(const polyfp::expr &e, T val)\n{\n    return e - value_cast(e.get_data_type(), val);\n}\n\ntemplate <typename T>\nonly_integral<T> operator-(T val, const polyfp::expr &e)\n{\n    return value_cast(e.get_data_type(), val) - e;\n}\n\ntemplate <typename T>\nonly_integral<T> operator/(const polyfp::expr &e, T val)\n{\n    return e / expr{val};\n}\n\ntemplate <typename T>\nonly_integral<T> operator/(T val, const polyfp::expr &e)\n{\n    return expr{val} / e;\n}\n\ntemplate <typename T>\nonly_integral<T> operator*(const polyfp::expr &e, T val)\n{\n    return e * value_cast(e.get_data_type(), val);\n}\n\ntemplate <typename T>\nonly_integral<T> operator*(T val, const polyfp::expr &e)\n{\n    return value_cast(e.get_data_type(), val) * e;\n}\n\ntemplate <typename T>\nonly_integral<T> operator%(const polyfp::expr &e, T val)\n{\n    return e % expr{val};\n}\n\ntemplate <typename T>\nonly_integral<T> operator%(T val, const polyfp::expr &e)\n{\n    return expr{val} % e;\n}\n\n}\n\n\n#endif\n"
  },
  {
    "path": "include/polyhedral/function.h",
    "content": "#ifndef _H_polyfp_function_\n#define _H_polyfp_function_\n\n#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <queue>\n\n#include \"scalehls/Transforms/Passes.h\"\n#include \"scalehls/Transforms/Utils.h\"\n#include \"scalehls/Transforms/Estimator.h\"\n#include \"llvm/Support/MemoryBuffer.h\"\n#include \"llvm/Support/ToolOutputFile.h\"\n#include \"mlir/Dialect/Affine/Analysis/Utils.h\"\n#include \"mlir/Dialect/Affine/IR/AffineValueMap.h\"\n#include \"mlir/Support/FileUtilities.h\"\n#include \"llvm/ADT/SmallPtrSet.h\"\n#include \"llvm/Support/MemoryBuffer.h\"\n\n#include \"llvm/ADT/SmallPtrSet.h\"\n#include \"mlir/IR/Builders.h\"\n#include \"mlir/IR/IntegerSet.h\"\n#include \"mlir/IR/BuiltinOps.h\"\n\n#include \"mlir/Dialect/Affine/Analysis/LoopAnalysis.h\"\n#include \"mlir/Dialect/Affine/LoopUtils.h\"\n#include \"mlir/IR/Attributes.h\"\n#include \"mlir/IR/Builders.h\"\n\n#include \"mlir/IR/MLIRContext.h\"\n#include \"mlir/IR/Verifier.h\"\n#include \"mlir/Dialect/ControlFlow/IR/ControlFlowOps.h\"\n#include \"mlir/Dialect/Affine/Passes.h\"\n#include \"mlir/Dialect/Affine/IR/AffineOps.h\"\n#include \"mlir/Dialect/Func/IR/FuncOps.h\"\n#include \"mlir/Dialect/LLVMIR/LLVMDialect.h\"\n#include \"mlir/Dialect/LLVMIR/LLVMTypes.h\"\n#include \"mlir/Dialect/Math/IR/Math.h\"\n#include \"mlir/Dialect/MemRef/IR/MemRef.h\"\n#include \"mlir/Dialect/Arithmetic/IR/Arithmetic.h\"\n#include \"mlir/Target/LLVMIR/Import.h\"\n#include \"mlir/IR/Builders.h\"\n#include \"mlir/IR/MLIRContext.h\"\n#include \"mlir/IR/OpDefinition.h\"\n\n#include \"expr.h\"\n#include \"type.h\"\n#include \"codegen.h\"\n#include \"generator_isl.h\"\n#include \"placeholder.h\"\n\nnamespace polyfp{\n\n\nclass constant;\nclass compute;\nclass generator;\nclass placeholder;\n\nisl_ast_node *for_code_generator_after_for(isl_ast_node *node, isl_ast_build *build, void *user);\nvoid gen_mlir(polyfp::function *fct, isl_ast_node *node, int level);\n\n\nclass function{\n\n    friend constant;\n    friend compute;\n    friend generator;\n    friend placeholder;\n\nprivate:\n\n    std::string name;\n    std::vector<polyfp::constant> invariants;\n    std::map<std::string, polyfp::constant *> constant_list;\n    std::vector<polyfp::placeholder *> function_arguments;\n\n    std::map<std::string, polyfp::placeholder *> placeholders_list;\n    std::map<std::string, polyfp::placeholder *> fct_argument_list;\n    std::map<std::string, polyfp::placeholder *> global_argument_list;\n    bool fct_argument_added = false;\n\n    std::vector<std::tuple<std::string, std::vector<int>, std::vector<std::string>>> partition_map;\n\n    // The isl context of the function.\n    isl_ctx *ctx;\n\n      \n    // The isl AST generated by gen_isl_ast().\n    isl_ast_node *ast;\n\n    // Contains all the computes of the function\n    std::vector<compute *> body;\n\n    /**\n     * TODO: Extend\n     * Derived from Tiramisu:\n     * The context set of the function, i.e. a set representing the\n     * constraints over the parameters.\n     * The parameters of a function are the function invariants (constants).\n     */\n    isl_set *context_set;\n\n    std::vector<std::string> iterator_names;\n\n    isl_union_set *get_trimmed_time_processor_domain() const;\n\n    /**\n      * Derived from Tiramisu:\n      * This function iterates over the computes of the function.\n      * It modifies the identity schedule of each computes in order to\n      * make all the identity schedules have the same number of dimensions\n      * in their ranges.\n      * This is done by adding dimensions equal to 0 to the range of each\n      * identity schedule that does not have enough dimensions.\n      */\n    isl_union_map *get_aligned_identity_schedules() const;\n\n    /**\n      * Derived from Tiramisu:\n      * This function first computes the identity schedules,\n      * then it computes the maximal dimension among the dimensions\n      * of the ranges of all the identity schedules.\n      */\n    int get_max_identity_schedules_range_dim() const;\n\n    void rename_computations();\n  \n    // Recursive function to perform the DFS step of dump_sched_graph.\n    void dump_sched_graph_dfs(polyfp::compute *,\n                              std::unordered_set<polyfp::compute *> &);\n\n    // Recursive function to perform the DFS step of is_sched_graph_tree.\n    bool is_sched_graph_tree_dfs(polyfp::compute *,\n                                 std::unordered_set<polyfp::compute *> &);\n\nprotected:\n\n    void dfs(int pos, int top, int end, int map[500][500], int n, int v[500],int stack[500]);\n    polyfp::compute * update_latency();\n    int get_longest_path();\n    int get_longest_node(std::vector<long> path);\n    void add_computation(compute *cpt);\n\n    void add_invariant(std::pair<std::string, polyfp::constant *>  param);\n    void add_placeholder(std::pair<std::string, polyfp::placeholder *> buf);\n\n    const std::vector<std::string> &get_iterator_names() const;\n\n    // void add_iterator_name(const std::string &it_name);\n\n    const std::vector<compute *> &get_computations() const;\n\n    /** TODO: remove\n      * Derived from Tiramisu:\n      * Return a set that represents the parameters of the function\n      * (an ISL set that represents the parameters and constraints over\n      * the parameters of the functions,  a parameter is an invariant\n      * of the function). This set is also known as the context of\n      * the program.\n      * An example of a context set is the following:\n      *          \"[N,M]->{: M>0 and N>0}\"\n      * This context set indicates that the two parameters N and M\n      * are strictly positive.\n      */\n    isl_set *get_program_context() const;\n\n    std::vector<compute *> get_computation_by_name(std::string str) const;\n\n    isl_ctx *get_isl_ctx() const;\n\n    /**\n      * Return the union of all the schedules\n      * of the compute of the function.\n      */\n    isl_union_map *get_schedule() const;\n\n    /**\n      * Return the union of all the iteration domains\n      * of the computes of the function.\n      */\n    isl_union_set *get_iteration_domain() const;\n\n    /**\n     * Return true if the usage of high level scheduling comments is valid; i.e. if\n     * the scheduling relations formed using before, after, compute_at, etc.. form a tree.\n     *\n     * More specifically, it verifies that:\n     *     - There should be exactly one compute with no compute scheduled before it.\n     *     - Each other compute should have exactly one compute scheduled before it.\n     */\n    bool is_sched_graph_tree();\n\n    /**\n     * Modify the schedules of the computes of this function to reflect\n     * the order specified using the high level scheduling commands.\n     *\n     * Commands like .after() do not directly modify the schedules\n     * but rather modify the sched_graph.\n     */\n    void gen_ordering_schedules();\n\n    /**\n     * This functions iterates over the schedules of the function (the schedule\n     * of each compute in the function) and computes the maximal dimension\n     * among the dimensions of the ranges of all the schedules.\n     */\n    int get_max_schedules_range_dim() const;\n\n    /**\n      * Stores all high level scheduling instructions between computes; i.e. if a user calls\n      * for example c2.after(c1, L), sched_graph[&c1] would contain the key &c2, and\n      * sched_graph[&c1][&c2] = L.\n      */\n    std::unordered_map<polyfp::compute *,std::unordered_map<polyfp::compute *, int>> sched_graph;\n\n    std::unordered_map<polyfp::compute *,\n    std::unordered_map<polyfp::compute *, int>> sched_graph_reversed;\n\n    /**\n      * Return an ISL AST that represents this function.\n      * The function gen_isl_ast() should be called before calling\n      * this function.\n      */\n    isl_ast_node *get_isl_ast() const;\n  \n\n    // Generate a mlir stmt that represents the function.\n    void gen_mlir_stmt();\n\npublic:\n\n    bool is_dataflowed = false;\n    void evaluate_func();\n    std::unordered_set<polyfp::compute *> starting_computations;\n    std::vector<polyfp::compute *> leader_computations;\n    std::vector<polyfp::compute *> leaf_computations;\n    std::map<polyfp::compute *,int> leader_computation_index;\n    std::map<int,long> latency_map;\n    std::map<int,long> all_latency_map;\n    std::map<int,int> resource_map;\n    std::map<int,std::vector<int>> path_map;\n    std::vector<std::vector<long>> paths;\n    std::vector<std::string> finish_list;\n    bool consistent_flag = true;\n    bool refused = false;\n\n\n    void add_fct_argument(std::pair<std::string, polyfp::placeholder *> buf);\n\n    void add_fct_argument();\n\n    void add_global_argument(std::pair<std::string, polyfp::placeholder *> buf);\n\n    void check_loop_fusion();\n\n    int get_global_location(){\n        return global_location;\n    }\n\n    void set_global_location(int new_location){\n        this->global_location = new_location;\n    }\n\n    void dump_schedule(std::string path);\n\n    long longest_path;\n    long longest_node;\n    long dsp_max;\n    long dsp_usage;\n    long best_dsp_usage = dsp_max;\n    long best_latency;\n    long current_latency;\n    bool new_strategy = true;\n    polyfp::compute * current_opt_comp;\n\n\n    int global_location;\n    bool one_compute;\n\n    function(std::string name);\n\n    /**\n      * Derived from Tiramisu:\n      * This method applies to the schedule of each compute\n      * in the function.  It makes the dimensions of the ranges of\n      * all the schedules equal.  This is done by adding dimensions\n      * equal to 0 to the range of each schedule.\n      * This function is called automatically when gen_isl_ast()\n      * or gen_time_processor_domain() are called.\n      */\n    void align_schedules();\n\n    const std::vector<compute *> &get_body() const;\n\n    const std::map<std::string, polyfp::placeholder *> &get_placeholders() const;\n    const std::map<std::string, polyfp::placeholder *> &get_fct_arguments() const;\n    const std::map<std::string, polyfp::placeholder *> &get_global_arguments() const;\n\n    const std::map<std::string, polyfp::constant *> &get_invariants() const;\n    const std::vector<std::string> get_invariant_names() const;\n\n    std::vector<std::tuple<std::string, std::vector<int>, std::vector<std::string>>> get_partition_map();\n\n    void set_partition(std::string name, std::vector<int> tile_factors, std::vector<std::string> types);\n    \n    void dump_sched_graph();\n\n    isl_ast_node *get_isl_ast1() const;\n\n\n    /**\n      * Compute the graph of dependences between the computes of the function. \n      * C[0] = 0\n      * D[1] = C[0]\n      * D[2] = C[0]\n      * {C[0] -> D[1]; C[0]->D[2]}\n      */\n    isl_union_map *compute_dep_graph();\n    void gen_isl_ast();\n\n    /**\n      * Generate the time-space domain of the function.\n      *\n      * In this representation, the logical time of execution and the\n      * processor where the compute will be executed are both\n      * specified.\n      */\n    void gen_time_space_domain();\n\n    void gen_loop_location();\n    std::string get_name();\n\n    void collect_accesses();\n    std::map<int, std::map<std::string, std::vector<polyfp::expr> > > map_loadstores;\n\n    void codegen();\n    void auto_DSE_loop_transformation();\n    void auto_DSE(std::string path);\n    void auto_DSE_tile_size(polyfp::compute* comp, int factor,std::string path);\n    void dependence_analysis();\n    void compute_dependency_graph();\n\n    /**\n      * Dump the function on standard output (dump most of the fields of\n      * polyfp::function).This is mainly useful for debugging.\n      */\n    void dump(bool exhaustive) const;\n    void gen_c_code() const;\n\n    void trans();\n\n\n};\n\n}\n\n#endif"
  },
  {
    "path": "include/polyhedral/generator.h",
    "content": "#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#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n\n#include <string>\n#include \"mlir/Dialect/Affine/Analysis/LoopAnalysis.h\"\n#include \"mlir/Dialect/Affine/LoopUtils.h\"\n#include \"mlir/IR/Attributes.h\"\n#include \"mlir/IR/Builders.h\"\n#include \"mlir/IR/IntegerSet.h\"\n#include \"mlir/IR/BuiltinOps.h\"\n#include \"mlir/IR/BuiltinTypes.h\"\n#include \"mlir/IR/MLIRContext.h\"\n#include \"mlir/IR/Verifier.h\"\n#include \"mlir/Dialect/ControlFlow/IR/ControlFlowOps.h\"\n#include \"mlir/Dialect/Affine/Passes.h\"\n#include \"mlir/Dialect/Affine/IR/AffineOps.h\"\n#include \"mlir/Dialect/Func/IR/FuncOps.h\"\n#include \"mlir/Dialect/Math/IR/Math.h\"\n#include \"mlir/Dialect/MemRef/IR/MemRef.h\"\n#include \"mlir/Dialect/Arithmetic/IR/Arithmetic.h\"\n#include \"mlir/Dialect/SCF/SCF.h\"\n#include \"mlir/Target/LLVMIR/Import.h\"\n#include \"mlir/IR/Builders.h\"\n#include \"mlir/IR/MLIRContext.h\"\n#include \"mlir/IR/OpDefinition.h\"\n\n#include \"mlir/Pass/PassManager.h\"\n#include \"mlir/Target/LLVMIR/Export.h\"\n#include \"mlir/Transforms/Passes.h\"\n\n#include <numeric>\n#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <queue>\n#include <variant>\n#include \"expr.h\"\n#include \"type.h\"\n#include \"codegen.h\"\n#include \"function.h\"\n\nusing llvm::SmallVector;\nusing llvm::ArrayRef;\nnamespace polyfp{\nclass function;\nclass MLIRGenImpl {\nfriend function;\nfriend compute;\n\nprivate:\n  mlir::ModuleOp theModule;\n\n  /// The builder is a helper class to create IR inside a function. The builder\n  /// is stateful, in particular it keeps an \"insertion point\": this is where\n  /// the next operations will be introduced.\n  mlir::OpBuilder builder;\n    \npublic:\n  \n    MLIRGenImpl(mlir::MLIRContext &context) : builder(&context) \n    {\n        theModule = mlir::ModuleOp::create(builder.getUnknownLoc());\n    }\n\n    mlir::ModuleOp mlirGen1(const polyfp::function &fct, isl_ast_node *isl_node, int &level, bool flag, bool flag2, bool if_flag);\n\n    //contains all loops \n    std::vector<mlir::AffineForOp> ops;\n    std::vector<int> start_loops_position;\n\n\n    // std::map<std::string, mlir::Type > argument_list;\n    std::vector<std::string> argument_list;\n    std::map<std::string,mlir::Value> argument_map;\n    std::map<std::string,int> array_map;\n    std::map<std::string,mlir::Value> get_argument_map();\n    std::map<std::string,int> get_array_map();\n    std::vector<mlir::Value> values;\n    \n    std::vector<mlir::memref::AllocOp> allocs;\n    std::vector<mlir::FuncOp> funcs;\n    std::vector<mlir::FuncOp> get_funcs();\n    std::map<int, std::string > name_map;\n\n    \n    mlir::ModuleOp getModule();\n\n    void a_print_expr(polyfp::expr polyfp_expr, polyfp::compute *comp, int level);\n\n    // std::vector<mlir::Value> index_values;\n    // SmallVector<mlir::AffineExpr> index_args;\n    int get_iterator_location_from_name(polyfp::compute *comp,polyfp::expr polyfp_expr, std::vector<mlir::Value> &index_values);\n    mlir::AffineExpr a_print_index(polyfp::expr polyfp_expr, polyfp::compute *comp, std::vector<mlir::Value> &index_values,int level);\n    // std::vector<mlir::arith::AddFOp> add_op;\n    // // std::vector<mlir::Op<>> sum_op;\n    // std::vector<mlir::arith::MulFOp> mul_op;\n    \n\n    // std::vector<mlir::arith::AddFOp> all_add_op;\n    // std::vector<mlir::arith::MulFOp> all_mul_op;\n    using value = std::variant<mlir::arith::AddFOp, mlir::arith::MulFOp,mlir::arith::SubFOp,mlir::arith::DivFOp,mlir::arith::MaxFOp>;\n    std::vector<value> current_op;\n    std::vector<value> all_current_op;\n\n    using AffineLoopBand = SmallVector<mlir::AffineForOp, 6>;\n    using TileList = SmallVector<unsigned, 8>;\n\n};\n}"
  },
  {
    "path": "include/polyhedral/generator_isl.h",
    "content": "#ifndef _H_polyfp_function1_\n#define _H_polyfp_function1_\n\n#include \"expr.h\"\n#include \"type.h\"\n#include \"compute.h\"\n#include \"placeholder.h\"\nnamespace polyfp{\nclass fucntion;\nclass generator\n{\n    friend function;\n    friend compute;\n    // friend placeholder;\n\n\nprotected:\n\n    /**\n     * Compute the accesses of the RHS of the compute\n     * \\p comp and store them in the accesses vector.\n     * If \\p return_buffer_accesses is set to true, this function returns access functions to\n     * buffers. Otherwise it returns access functions to computes.\n     */\n    static void get_rhs_accesses(const polyfp::function *func, const polyfp::compute *comp,\n                          std::vector<isl_map *> &accesses, bool return_buffer_accesses);\n\n    /**\n     * Derived from Tiramisu:\n     * Analyze the \\p access_expression and return a set of constraints\n     * that correspond to the access pattern of the access_expression.\n     *\n     * access_dimension:\n     *      The dimension of the access. For example, the access\n     *      C0(i0, i1, i2) have three access dimensions: i0, i1 and i2.\n     * access_expression:\n     *      The expression of the access.\n     *      This expression is parsed recursively (by calling get_constraint_for_access)\n     *      and is gradually used to update the constraint.\n     * access_relation:\n     *      The access relation that represents the access.\n     * cst:\n     *      The constraint that defines the access and that is being constructed.\n     *      Different calls to get_constraint_for_access modify this constraint\n     *      gradually until the final constraint is created. Only the final constraint\n     *      is added to the access_relation.\n     * coeff:\n     *      The coefficient in which all the dimension coefficients of the constraint\n     *      are going to be multiplied. This coefficient is used to implement o_minus,\n     *      o_mul and o_sub.\n     */\n    static isl_constraint *get_constraint_for_access(int access_dimension,\n                                                     const polyfp::expr &access_expression,\n                                                     isl_map *access_relation,\n                                                     isl_constraint *cst,\n                                                     int coeff,\n                                                     const polyfp::function *fct);\n\n    /**\n     * Derived from Tiramisu:\n     * Traverse a polyfp expression (\\p exp) and extract the access relations\n     * from the access operation passed in \\p exp.  The access relations are added\n     * to the vector \\p accesses.\n     * The access relation is from the domain of the compute \\p comp to the\n     * domain of the compute accessed by the access operation.\n     * If \\p return_buffer_accesses = true, an access to a buffer is created\n     * instead of an access to computes.\n     */\n    static void traverse_expr_and_extract_accesses(const polyfp::function *fct,\n                                            const polyfp::compute *comp,\n                                            const polyfp::expr &exp,\n                                            std::vector<isl_map *> &accesses,\n                                            bool return_buffer_accesses);\n\npublic:\n    // TODO\n};\n\n/**\n * A class containing utility functions.\n */\nclass utility\n{\npublic:\n    /**\n     * Derived from Tiramisu:\n     * Traverse recursively the ISL AST tree\n     * \\p node represents the root of the tree to be traversed.\n     * \\p dim is the dimension of the loop from which the bounds have to be\n     * extracted.\n     * \\p upper is a boolean that should be set to true to extract\n     * the upper bound and false to extract the lower bound.\n     */\n     static expr extract_bound_expression(isl_ast_node *ast, int dim, bool upper);\n\n    /**\n     * Derived from Tiramisu:\n     * Return a polyfp::expr representing the bound of\n     * the dimension \\p dim in \\p set.  If \\p upper is true\n     * then this function returns the upper bound otherwise\n     * it returns the lower bound.\n     *\n     * For example, assuming that\n     *\n     * S = {S[i,j]: 0<=i<N and 0<=j<N and i<M}\n     *\n     * then\n     *\n     * get_upper_bound(S, 1)\n     *\n     * would return N-1, while\n     *\n     * get_upper_bound(S, 0)\n     *\n     * would return min(N-1,M-1)\n     */\n    static polyfp::expr get_bound(isl_set *set, int dim, int upper);\n\n    /**\n     * Create a comma separated string that represents the list\n     * of the parameters of \\p set.\n     *\n     * For example, if the set is\n     *\n     * [N,M,K]->{S[i]}\n     *\n     * this function returns the string \"N,M,K\".\n     */\n    static std::string get_parameters_list(isl_set *set);\n};\n\n\n}\n\n#endif"
  },
  {
    "path": "include/polyhedral/placeholder.h",
    "content": "#ifndef _H_polyfp_PLACEHOLDER_\n#define _H_polyfp_PLACEHOLDER_\n// #include \"compute.h\"\n#include \"function.h\"\n#include \"expr.h\"\n#include <map>\n\n/**\n  * A class that represents placeholders.\n  *\n  * placeholders have two use cases:\n  * - used to store the results of computations, and\n  * - used to represent input arguments to functions.\n  */\n\nnamespace polyfp{\n  class compute;\n\nstatic std::string generate_new_p_operator_name()\n{\n    static int counter = 0;\n    return \"p\" + std::to_string(counter++);\n}\n\n\nclass placeholder\n{\n    friend compute;\n    friend function;\n    // friend generator;\n\nprivate:\n\n    /**\n      * The sizes of the dimensions of the placeholder.  Assuming the following\n      * placeholder buf[N0][N1][N2], dim_sizes should be {N0, N1, N2}.\n      */\n    std::vector<int64_t> dim_sizes;\n\n    /**\n      * The polyfp function where this placeholder is declared or where the\n      * placeholder is an argument.\n      */\n    polyfp::function *fct;\n\n    /**\n      * The name of the placeholder.\n      * placeholder names should not start with _ (an underscore).\n      * Names starting with _ are reserved names.\n      */\n    std::string name;\n\n    /**\n      * The type of the elements of the placeholder.\n      */\n    polyfp::primitive_t type;\n\n\n\nprotected:\n    /**\n     * Set the size of a dimension of the placeholder.\n     */\n    void set_dim_size(int dim, int size);\n\npublic:\n    /**\n      * \\brief Default polyfp constructor\n      */\n    placeholder();\n\n    /**\n      * A polyfp placeholder is equivalent to an array in C.\n      *\n      * placeholders have two use cases:\n      * - Used to store the results of computes, and\n      * - Used to represent input arguments to functions.\n      *\n      * \\p name is the name of the placeholder.\n      *\n      * \\p dim_sizes is a vector of polyfp expressions that represent the\n      * size of each dimension in the placeholder.\n      * Assuming we want to declare the placeholder buf[N0][N1][N2],\n      * then the vector of sizes should be {N0, N1, N2}.\n      * placeholder dimensions in polyfp have the same semantics as in\n      * C/C++.\n      *\n      * \\p type is the type of the elements of the placeholder.\n      * It must be a primitive type (i.e. p_uint8, p_uint16, ...).\n      * Possible types are declared in \\ref polyfp::primitive_t\n      * (in type.h).\n      *\n      * \\p fct is a pointer to a polyfp function where the placeholder is\n      * declared or used.  If this argument is not provided (which is\n      * the common case), the function that was created automatically\n      * during polyfp initialization will be used (we call that\n      * function the \"implicit function\").\n      */\n    placeholder(std::string name, std::vector<int64_t> dim_sizes,\n           polyfp::primitive_t type,\n           polyfp::function *fct = global::get_implicit_function());\n\n    \n    void dump(bool exhaustive) const;\n\n    const std::string &get_name() const;\n\n    // Get the number of dimensions of the placeholder.\n    int get_n_dims() const;\n\n    polyfp::primitive_t get_elements_type() const;\n    void partition(std::vector<int> factors, std::string type);\n    void partition(std::vector<int> factors, std::vector<std::string> type);\n\n    const std::vector<int64_t> &get_dim_sizes() const;\n\n    template<typename... Args> polyfp::expr operator()(Args... args)\n    {\n        // TODO move to cpp\n        std::vector<polyfp::expr> access_expressions{std::forward<Args>(args)...};\n        if (access_expressions.size() != this->get_n_dims())\n        {\n            polyfp::str_dump(\"Error - Incorrect access: \" + this->get_name() + \"(\");\n            for (int i = 0; i < access_expressions.size(); i++)\n            {\n                polyfp::expr e = access_expressions[i];\n                e.dump(false);\n                if (i != access_expressions.size() - 1)\n                    polyfp::str_dump(\", \");\n            }\n            polyfp::str_dump(\").\\n\");\n            polyfp::str_dump(\"The number of access dimensions does not match that used in the declaration of \" + this->get_name() + \".\\n\\n\");\n            exit(1);\n        }\n            return polyfp::expr(polyfp::o_access,\n                                  this->get_name(),\n                                  access_expressions,\n                                  this->get_elements_type());\n        // }\n    }\n\n\n    operator expr();\n\n\n};\n\n\n\n\n\n\n\n}\n\n\n\n#endif"
  },
  {
    "path": "include/polyhedral/type.h",
    "content": "#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 of expression\nenum expr_t\n{\n    e_val,          // literal value, like 1, 2.4, 10, ...\n    e_var,          // a variable of a primitive type (i.e., an identifier holding one value),\n    e_op,           // an operation: add, mul, div, ...\n    e_none          // undefined expression. The existence of an expression of e_none type means an error.\n};\n\nenum primitive_t\n{\n    p_uint8,\n    p_uint16,\n    p_uint32,\n    p_uint64,\n    p_int8,\n    p_int16,\n    p_int32,\n    p_int64,\n    p_float32,\n    p_float64,\n    // p_boolean,\n    p_none\n};\n\n\n// Type of operator\nenum op_t\n{\n    o_add,\n    o_sub,\n    o_mul,\n    o_div,\n    o_mod,\n    o_max,\n    o_min,\n    o_access,\n    o_placeholder,\n    o_none,\n};\n\n}\n\n#endif\n"
  },
  {
    "path": "lib/CMakeLists.txt",
    "content": "add_subdirectory(polyhedral)\n# add_subdirectory(CAPI)\n# add_subdirectory(Standalone)\n# add_subdirectory(hello)\n"
  },
  {
    "path": "lib/polyhedral/CMakeLists.txt",
    "content": "get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)\nget_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)\n# include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)\n# include_directories(/usr/include/python3.8)\naux_source_directory(. DIR_LIB_SRCS)\n# 编译成静态库文件\n# add_library(Functions  ${DIR_LIB_SRCS})\nadd_library(Functions  ${DIR_LIB_SRCS})\n"
  },
  {
    "path": "lib/polyhedral/codegen.cpp",
    "content": "#include \"codegen.h\"\nnamespace polyfp\n{\n\nstd::vector<compute *> function::get_computation_by_name(std::string name) const\n{\n    assert(!name.empty());\n\n    std::vector<polyfp::compute *> res_comp;\n\n    for (const auto &comp : this->get_computations())\n    {\n        if (name == comp->get_name())\n        {\n            res_comp.push_back(comp);\n        }\n    }\n\n    if (res_comp.empty())\n    {\n        polyfp::str_dump(\"Computation not found.\");\n    }\n    else\n    {\n        // polyfp::str_dump(\"Computation found.\");\n    }\n\n    return res_comp;\n}\n\n\nbool access_is_affine(const polyfp::expr &exp)\n{\n\n    // We assume that the access is affine until we find the opposite.\n    bool affine = true;\n\n    // Traverse the expression tree and try to find expressions that are non-affine.\n    if (exp.get_expr_type() == polyfp::e_val ||\n        exp.get_expr_type() == polyfp::e_var)\n    {\n        affine = true;\n    }\n    else if (exp.get_expr_type() == polyfp::e_op)\n    {\n        switch (exp.get_op_type())\n        {\n            case polyfp::o_access:\n            case polyfp::o_placeholder:\n                affine = false;\n                break;\n            case polyfp::o_add:\n            case polyfp::o_sub:\n                affine = access_is_affine(exp.get_operand(0)) && access_is_affine(exp.get_operand(1));\n                break;\n            case polyfp::o_max:\n            case polyfp::o_min:\n            case polyfp::o_mul:\n            case polyfp::o_div:\n            case polyfp::o_mod:\n                break;\n            default:\n                ERROR(\"Unsupported polyfp expression passed to access_is_affine().\", 1);\n        }\n    }\n\n    return affine;\n}\n\n\nisl_ast_node *for_code_generator_after_for(isl_ast_node *node, isl_ast_build *build, void *user)\n{\n    return node;\n}\n\n\n\n\n\n\n\n\n}\n"
  },
  {
    "path": "lib/polyhedral/compute.cpp",
    "content": "#include \"compute.h\"\n#include \"core.h\"\n#include <algorithm>\nnamespace polyfp{\n\nisl_ctx *polyfp::compute::get_ctx() const\n{\n    return ctx;\n}\n\nisl_set *polyfp::compute::get_iteration_domain() const\n{\n    assert(iteration_domain != NULL);\n    return iteration_domain;\n}\n\nvoid polyfp::compute::set_iteration_domain(isl_set *domain)\n{\n    this->iteration_domain = domain;\n}\n\nint polyfp::compute::get_iteration_domain_dimensions_number()\n{\n    assert(iteration_domain != NULL);\n    return isl_set_n_dim(this->iteration_domain);\n}\n\nisl_map *compute::get_schedule() const\n{\n    return this->schedule;\n}\n\nisl_set *polyfp::compute::get_trimmed_time_processor_domain()\n{\n    isl_set *tp_domain = isl_set_copy(this->get_time_processor_domain());\n    const char *name = isl_set_get_tuple_name(isl_set_copy(tp_domain));\n    isl_set *tp_domain_without_duplicate_dim =\n        isl_set_project_out(isl_set_copy(tp_domain), isl_dim_set, 0, 1);\n    tp_domain_without_duplicate_dim = isl_set_set_tuple_name(tp_domain_without_duplicate_dim, name);\n    return tp_domain_without_duplicate_dim ;\n}\n\nvoid compute::name_unnamed_iteration_domain_dimensions()\n{\n    isl_set *iter = this->get_iteration_domain();\n    assert(iter != NULL);\n    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)\n    {\n        if (isl_set_has_dim_name(iter, isl_dim_set, i) == isl_bool_false)\n            iter = isl_set_set_dim_name(iter, isl_dim_set, i,\n                                        generate_new_variable_name().c_str());\n    }\n    this->set_iteration_domain(iter);\n}\n\nvoid compute::name_unnamed_time_space_dimensions()\n{\n    isl_map *sched = this->get_schedule();\n    assert(sched != NULL);\n    for (int i = 0; i < this->get_loop_levels_number(); i++)\n    {\n        if (isl_map_has_dim_name(sched, isl_dim_out, loop_level_into_dynamic_dimension(i)) == isl_bool_false)\n            sched = isl_map_set_dim_name(sched, isl_dim_out, loop_level_into_dynamic_dimension(i), generate_new_variable_name().c_str());\n    }\n    this->set_schedule(sched);\n}\n\nisl_map *isl_map_add_dim_and_eq_constraint(isl_map *map, int dim_pos, int constant)\n{\n    assert(map != NULL);\n    assert(dim_pos >= 0);\n    assert(dim_pos <= (signed int) isl_map_dim(map, isl_dim_out));\n\n    map = isl_map_insert_dims(map, isl_dim_out, dim_pos, 1);\n    map = isl_map_set_tuple_name(map, isl_dim_out, isl_map_get_tuple_name(map, isl_dim_in));\n\n    isl_space *sp = isl_map_get_space(map);\n    isl_local_space *lsp =\n        isl_local_space_from_space(isl_space_copy(sp));\n    isl_constraint *cst = isl_constraint_alloc_equality(lsp);\n    cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, dim_pos, 1);\n    cst = isl_constraint_set_constant_si(cst, (-1) * constant);\n    map = isl_map_add_constraint(map, cst);\n\n    return map;\n}\n\nisl_map *polyfp::compute::gen_identity_schedule_for_iteration_domain()\n{\n\n    isl_space *sp = isl_set_get_space(this->get_iteration_domain());\n    isl_map *sched = isl_map_identity(isl_space_map_from_set(sp));\n    sched = isl_map_intersect_domain(sched, isl_set_copy(this->get_iteration_domain()));\n    sched = isl_map_coalesce(sched);\n\n    for (int i = 0; i < isl_space_dim(sp, isl_dim_out) + 1; i++)\n    {\n        sched = isl_map_add_dim_and_eq_constraint(sched, 2 * i, 0);\n    }\n\n    sched = isl_map_add_dim_and_eq_constraint(sched, 0, 0);\n    return sched;\n}\n\nvoid compute::set_schedule(isl_map *map)\n{\n    this->schedule = map;\n}\nvoid compute::set_schedule(std::string map_str)\n{\n    assert(!map_str.empty());\n    assert(this->ctx != NULL);\n\n    isl_map *map = isl_map_read_from_str(this->ctx, map_str.c_str());\n    assert(map != NULL);\n\n    this->set_schedule(map);\n}\n\nvoid compute::dump_iteration_domain() const\n{\n    isl_set_dump(this->get_iteration_domain());\n\n}\n\nvoid compute::dump_schedule() const\n{\n    polyfp::str_dump(\"Dumping the schedule of the computation \" + this->get_name() + \" : \");\n    std::flush(std::cout);\n    isl_map_dump(this->get_schedule());\n\n}\n\nconst polyfp::expr &polyfp::compute::get_expr() const\n{\n    return expression;\n}\n\nvoid compute::dump() const\n{\n    std::cout << std::endl << \"Dumping the computation \\\"\" + this->get_name() + \"\\\" :\" << std::endl;\n    std::cout << \"Iteration domain of the computation \\\"\" << this->name << \"\\\" : \";\n    std::flush(std::cout);\n    isl_set_dump(this->get_iteration_domain());\n    std::flush(std::cout);\n    this->dump_schedule();\n\n    std::flush(std::cout);\n    std::cout << \"Expression of the computation : \"; std::flush(std::cout);\n    this->get_expr().dump(true);\n    std::cout << std::endl; std::flush(std::cout);\n\n    std::cout << \"Access relation of the computation : \"; std::flush(std::cout);\n    isl_map_dump(this->get_access_relation());\n    if (this->get_access_relation() == NULL)\n    {\n        std::cout << \"\\n\";\n    }\n    std::flush(std::cout);\n\n    if (this->get_time_processor_domain() != NULL)\n    {\n        std::cout << \"Time-space domain \" << std::endl; std::flush(std::cout);\n        isl_set_dump(this->get_time_processor_domain());\n    }\n    else\n    {\n        std::cout << \"Time-space domain : NULL.\" << std::endl;\n    }\n\n    polyfp::str_dump(\"\\n\");\n    polyfp::str_dump(\"\\n\");\n}\n\nvoid polyfp::compute::set_identity_schedule_based_on_iteration_domain()\n{\n    isl_map *sched = this->gen_identity_schedule_for_iteration_domain();\n    this->set_schedule(sched);\n}\n\nstd::vector<std::string> compute::get_iteration_domain_dimension_names()\n{\n\n    isl_set *iter = this->get_iteration_domain();\n    assert(iter != NULL);\n    std::vector<std::string> result;\n    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)\n    {\n        if (isl_set_has_dim_name(iter, isl_dim_set, i))\n            result.push_back(std::string(isl_set_get_dim_name(iter,\n                                                              isl_dim_set, i)));\n        else\n        {\n            ERROR(\"All iteration domain dimensions must have \"\n                            \"a name.\", true);\n        }\n    }\n    assert(result.size() == this->get_iteration_domain_dimensions_number());\n    return result;\n}\n\nvoid compute::update_names(std::vector<std::string> original_loop_level_names, std::vector<std::string> new_names,\n                               int erase_from, int nb_loop_levels_to_erase)\n{\n    this->final_loop_level_names.clear();\n    this->final_loop_level_names = this->final_loop_level_names_reserved;\n    // // std::cout<<\"original names: \"<<std::endl;\n    // for (auto n: original_loop_level_names)\n    // {\n    //     polyfp::str_dump(n + \" \");\n    // }\n    // // std::cout<<\"finial names: \"<<std::endl;\n    // for (auto n: final_loop_level_names)\n    // {\n    //     polyfp::str_dump(n + \" \");\n    // }\n    // polyfp::str_dump(\"Start erasing from: \" + std::to_string(erase_from));\n    // polyfp::str_dump(\"Number of loop levels to erase: \" + std::to_string(nb_loop_levels_to_erase));\n\n    original_loop_level_names.erase(original_loop_level_names.begin() + \n        erase_from, original_loop_level_names.begin() + erase_from + nb_loop_levels_to_erase);\n    final_loop_level_names.erase(final_loop_level_names.begin() + \n        erase_from, final_loop_level_names.begin() + erase_from + nb_loop_levels_to_erase);\n\n    original_loop_level_names.insert(original_loop_level_names.begin() + \n        erase_from, new_names.begin(), new_names.end());\n    final_loop_level_names.insert(final_loop_level_names.begin() + \n        erase_from, new_names.begin(), new_names.end());\n    \n    // // std::cout<<\"original names: \"<<std::endl;\n    // for (auto n: original_loop_level_names)\n    // {\n    //     polyfp::str_dump(n + \" \");\n    // }\n    // // std::cout<<\"finial names: \"<<std::endl;\n    // for (auto n: final_loop_level_names)\n    // {\n    //     polyfp::str_dump(n + \" \");\n    // }\n    // this->final_loop_level_names.clear();\n    // this->final_loop_level_names = original_loop_level_names;\n    this->set_loop_level_names(original_loop_level_names);\n\n}\n\n\nvoid polyfp::compute::set_expression(const polyfp::expr &e)\n{\n    // polyfp::expr modified_e = traverse_expr_and_replace_non_affine_accesses(this, e);\n    // polyfp::str_dump(\"The original expression is: \"); modified_e.dump(false);\n    this->expression = e.copy();\n}\n\nstd::vector<std::string> compute::get_loop_level_names()\n{\n    // polyfp::str_dump(\"Collecting names of loop levels from the range of the schedule: \", isl_map_to_str(this->get_schedule()));\n\n    std::vector<std::string> names;\n    std::string names_to_print_for_debugging = \"\";\n\n    for (int i = 0; i < this->get_loop_levels_number(); i++)\n    {\n        std::string dim_name = isl_map_get_dim_name(this->get_schedule(), \n            isl_dim_out, loop_level_into_dynamic_dimension(i));\n        names.push_back(dim_name);\n        names_to_print_for_debugging += dim_name + \" \";\n    }\n    // polyfp::str_dump(\"Names of loop levels: \" + names_to_print_for_debugging);\n\n    return names;\n}\n\nstd::vector<polyfp::expr> polyfp::compute::get_placeholder_dims()\n{\n    return placeholder_dims;\n}\n\nvoid polyfp::compute::set_placeholder_dims(std::vector<polyfp::expr> temp)\n{\n    this->placeholder_dims = temp ;\n}\n\npolyfp::function *polyfp::compute::get_function() const\n{\n    return fct;\n}\n\nint compute::get_loop_levels_number()\n{\n    assert(this->get_schedule() != NULL);\n    int loop_levels_number = ((isl_map_dim(this->get_schedule(), isl_dim_out)) - 2)/2;\n\n    return loop_levels_number;\n}\nvoid compute::set_loop_level_names(std::vector<std::string> names)\n{\n    assert(names.size() > 0);\n\n    // polyfp::str_dump(\"Number of loop levels: \" + std::to_string(this->get_loop_levels_number()));\n    // polyfp::str_dump(\"Number of names to be set: \" + std::to_string(names.size()));\n\n    for (int i = 0; i < names.size(); i++)\n    {\n        if (isl_map_has_dim_name(this->get_schedule(), isl_dim_out, loop_level_into_dynamic_dimension(i)) == isl_bool_true)\n        {\n            this->schedule = isl_map_set_dim_name(this->get_schedule(),\n                                                  isl_dim_out,\n                                                  loop_level_into_dynamic_dimension(i),\n                                                  names[i].c_str());\n        //    polyfp::str_dump(\"Setting the name of loop level \" + std::to_string(i) + \" into \" + names[i].c_str());\n        }\n    }\n\n    // polyfp::str_dump(\"The schedule after renaming: \", isl_map_to_str(this->get_schedule()));\n\n}\n\nvoid compute::set_loop_level_names(std::vector<int> loop_levels,\n        std::vector<std::string> names)\n{\n    this->check_dimensions_validity(loop_levels);\n    assert(names.size() > 0);\n    assert(names.size() == loop_levels.size());\n\n    for (int i = 0; i < loop_levels.size(); i++)\n    {\n        if (loop_level_into_static_dimension(loop_levels[i]) <= isl_map_dim(this->get_schedule(), isl_dim_out))\n        {\n            this->schedule = isl_map_set_dim_name(this->get_schedule(),\n                                                  isl_dim_out,\n                                                  loop_level_into_dynamic_dimension(loop_levels[i]),\n                                                  names[i].c_str());\n            // polyfp::str_dump(\"Setting the name of loop level \" + std::to_string(loop_levels[i]) + \" into \" + names[i].c_str());\n           \n        }\n    }\n\n    // polyfp::str_dump(\"The schedule after renaming: \", isl_map_to_str(this->get_schedule()));\n}\n\nisl_set *polyfp::compute::get_time_processor_domain() const\n{\n    return time_processor_domain;\n}\n\nvoid polyfp::compute::set_access(isl_map *access)\n{\n    assert(access != NULL);\n    this->set_access(isl_map_to_str(access));\n}\n\n\nvoid polyfp::compute::set_access(std::string access_str)\n{\n    this->access = isl_map_read_from_str(this->ctx, access_str.c_str());\n\n    std::vector<polyfp::compute *> same_name_computations =\n        this->get_function()->get_computation_by_name(this->get_name());\n\n    // TODO: Delete\n    if (same_name_computations.size() > 1)\n        for (auto c : same_name_computations)\n        {\n            c->access = isl_map_read_from_str(this->ctx, access_str.c_str());\n        }\n\n    std::vector<polyfp::compute *> computations =\n        this->get_function()->get_computation_by_name(this->get_name());\n    for (auto c : computations)\n        if (isl_map_is_equal(this->get_access_relation(), c->get_access_relation()) == isl_bool_false)\n        {\n            ERROR(\"Computations that have the same name should also have the same access relation.\",\n                            true);\n        }\n\n    assert(this->access != nullptr && \"Set access failed\");\n}\n\nisl_map *polyfp::compute::get_access_relation() const\n{\n    return access;\n}\n\npolyfp::placeholder *polyfp::compute::get_placeholder()\n{\n    return this->plhd;\n}\npolyfp::expr polyfp::compute::get_placeholder_expr()\n{\n    return this->plhd_expr;\n}\nstd::vector<polyfp::expr> compute::compute_buffer_size()\n{\n\n    std::vector<polyfp::expr> dim_sizes;\n\n    // If the computation has an update, we first compute the union of all the\n    // updates, then we compute the bounds of the union.\n    for (int i = 0; i < this->get_iteration_domain_dimensions_number(); i++)\n    {\n        isl_set *union_iter_domain = isl_set_copy(this->get_iteration_domain());\n\n\n        // polyfp::str_dump(\"Extracting bounds of the following set:\", isl_set_to_str(union_iter_domain));\n        polyfp::expr lower = utility::get_bound(union_iter_domain, i, false);\n        polyfp::expr upper = utility::get_bound(union_iter_domain, i, true);\n        polyfp::expr diff = (upper - lower + 1);\n        dim_sizes.push_back(diff);\n    }\n    return dim_sizes;\n}\nstd::map<std::string, std::string > compute::get_access_map(){\n    return this->access_map;\n}\nstd::map<std::string, std::string > compute::get_tile_map(){\n    return this->tile_map;\n}\nstd::map<std::string, int > compute::get_tile_size_map(){\n    return this->tile_size_map;\n}\nstd::map<std::string, std::string > compute::get_directive_map(){\n    return this->directive_map;;\n}\nstd::map<std::string, std::string > compute::get_directive_tool_map(){\n    return this->directive_tool_map;;\n}\n\nvoid polyfp::compute::set_name(const std::string &n)\n{\n    this->name = n;\n}\n\nisl_map *polyfp::compute::gen_identity_schedule_for_time_space_domain()\n{\n    isl_set *tp_domain = this->get_trimmed_time_processor_domain();\n    isl_space *sp = isl_set_get_space(tp_domain);\n    isl_map *sched = isl_map_identity(isl_space_map_from_set(sp));\n    sched = isl_map_intersect_domain(\n                sched, isl_set_copy(this->get_trimmed_time_processor_domain()));\n    sched = isl_map_set_tuple_name(sched, isl_dim_out, \"\");\n    sched = isl_map_coalesce(sched);\n\n    return sched;\n}\n\nvoid compute::assert_names_not_assigned(std::vector<std::string> dimensions)\n{\n    for (auto const dim: dimensions)\n    {\n        int d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_out,\n                                         dim.c_str());\n        if (d >= 0)\n        {\n            ERROR(\"Dimension \" + dim + \" is already in use.\", true);\n        }\n\n        d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_in,\n                                     dim.c_str());\n        if (d >= 0)\n        {\n            ERROR(\"Dimension \" + dim + \" is already in use.\", true);\n        }\n    }\n}\n\nvoid compute::check_dimensions_validity(std::vector<int> dimensions)\n{\n    assert(dimensions.size() > 0);\n\n    for (auto const dim: dimensions)\n    {\n        assert(dim >= compute::root_dimension);\n\n        if (loop_level_into_dynamic_dimension(dim) >=\n            isl_space_dim(isl_map_get_space(this->get_schedule()),\n                          isl_dim_out))\n        {\n            ERROR(\"The dynamic dimension \" +\n                            std::to_string(loop_level_into_dynamic_dimension(dim)) +\n                            \" is not less than the number of dimensions of the \"\n                            \"time-space domain \" +\n                            std::to_string(isl_space_dim(isl_map_get_space(\n                                    this->get_schedule()), isl_dim_out)), true);\n        }\n    }\n}\n\nvoid compute::set_schedule_domain_dim_names(std::vector<int> loop_levels,\n        std::vector<std::string> names)\n{\n    this->check_dimensions_validity(loop_levels);\n    assert(names.size() > 0);\n    assert(names.size() == loop_levels.size());\n\n    for (int i = 0; i < loop_levels.size(); i++)\n    {\n        assert(loop_levels[i] <= isl_map_dim(this->get_schedule(), isl_dim_in));\n        this->schedule = isl_map_set_dim_name(this->get_schedule(),\n                                              isl_dim_in, loop_levels[i], names[i].c_str());\n    }\n\n    // polyfp::str_dump(\"The schedule after renaming: \", isl_map_to_str(this->get_schedule()));\n}\n\nvoid polyfp::compute::init_computation(std::string iteration_space_str,\n        polyfp::function *fction,\n        const polyfp::expr &e,\n        polyfp::primitive_t t, polyfp::expr p)\n{\n    // polyfp::str_dump(\"Constructing the computation: \" + iteration_space_str);\n    assert(iteration_space_str.length() > 0 && (\"Empty iteration space\"));\n    access = NULL;\n    time_processor_domain = NULL;\n    predicate = polyfp::expr();\n    this->data_type = t;\n    this->ctx = fction->get_isl_ctx();\n    //todo\n    for(auto &kv : fction->get_placeholders()){\n        if(kv.first == p.get_name())\n            this->plhd = kv.second;\n    }\n    this->plhd_expr = p;\n    this->plhd_expr.owner = this;\n\n    placeholder_dims = p.get_access();\n    iteration_domain = isl_set_read_from_str(ctx, iteration_space_str.c_str());\n    //TODO\n    name = std::string(isl_space_get_tuple_name(isl_set_get_space(iteration_domain),\n                       isl_dim_type::isl_dim_set));\n\n    number_of_dims = isl_set_dim(iteration_domain, isl_dim_type::isl_dim_set);\n\n    // for (unsigned i = 0; i < number_of_dims; i++) {\n    //     if (isl_set_has_dim_name(iteration_domain, isl_dim_type::isl_dim_set, i)) {\n    //         std::string dim_name(isl_set_get_dim_name(iteration_domain, isl_dim_type::isl_dim_set, i));\n    //         this->access_variables.push_back(make_pair(i, dim_name));\n           \n    //     }\n    // }\n    // for(auto &kv: access_variables){\n    //     // std::cout<<std::to_string(kv.first)<<kv.second<<std::endl;\n    // }\n    fct = fction;\n    this->is_leader = true;\n    this->is_top_parent = true;\n    this->is_leaf = true;\n    this->has_a_leader = false;\n    fct->leader_computations.push_back(this);\n    this->after_level = -2;\n    fct->add_computation(this);\n    this->set_identity_schedule_based_on_iteration_domain();\n    this->set_expression(e);\n\n    std::vector<std::string> nms = this->get_iteration_domain_dimension_names();\n\n    for (int i = 0; i< this->get_iteration_domain_dimensions_number(); i++)\n        this->set_schedule_domain_dim_names({i}, {generate_new_variable_name()});\n    for (int i = 0; i< nms.size(); i++){\n        this->set_loop_level_names({i}, {nms[i]});\n        this->final_loop_level_names.push_back(nms[i]);\n        this->final_loop_level_names_reserved.push_back(nms[i]);\n        // if(fct->get_body().size() == 1){\n        //     this->iterators_location_map.insert(std::make_pair(nms[i],i));\n        //     fct->global_location = nms.size();\n        // }  \n    }\n\n}\n\ncompute::compute(std::string name, std::vector<polyfp::var> iterator_variables, polyfp::expr e, primitive_t t, expr p)\n{   \n    this->iteration_variables = iterator_variables;\n    std::string iteration_space_str = construct_iteration_domain(name, iterator_variables, predicate);\n    // std::cout<<iteration_space_str<<std::endl;\n    init_computation(iteration_space_str, global::get_implicit_function(), e, t, p);\n}\n\ncompute::compute(std::string iteration_domain_str, polyfp::expr e,\n                         polyfp::primitive_t t,\n                         polyfp::function *fct, expr p)\n{\n    init_computation(iteration_domain_str, fct, e, t, p);\n}\n\ncompute::compute(std::string name, std::vector<polyfp::var> iterator_variables, polyfp::expr e, expr p)\n    : compute(name, iterator_variables, e, p_float64, p) {}\ncompute::compute(std::string name, std::vector<polyfp::var> iterator_variables, int a, expr p)\n    : compute(name, iterator_variables, expr((uint16_t) a), p_float64, p) {}\n\nstd::vector<polyfp::var> compute::get_iteration_variables()\n{\n    return this->iteration_variables;\n}\n\nstd::string polyfp::compute::construct_iteration_domain(std::string name, std::vector<var> iterator_variables, polyfp::expr predicate)\n{\n    polyfp::function *function = global::get_implicit_function();\n\n    std::string iteration_space_str = \"\";\n    std::string comp_name = name;\n\n    iteration_space_str += \"{\" + comp_name + \"[\";\n    if (iterator_variables.size() == 0)\n        iteration_space_str += \"0\";\n    else\n        for (int i = 0; i < iterator_variables.size(); i++)\n        {\n            var iter = iterator_variables[i];\n            iteration_space_str += iter.get_name();\n            if (i < iterator_variables.size() - 1)\n                iteration_space_str += \", \";\n        }\n\n    iteration_space_str += \"] \";\n\n    if (iterator_variables.size() != 0)\n        iteration_space_str += \": \";\n\n    if (predicate.is_defined())\n\t iteration_space_str += predicate.to_str() + \" and \";\n\n    bool insert_and = false;\n    for (int i = 0; i < iterator_variables.size(); i++)\n    {\n        var iter = iterator_variables[i];\n\n        if ((insert_and == true && (iter.lower.is_defined() || iter.upper.is_defined())))\n        {\n            iteration_space_str += \" and \";\n            insert_and = false;\n        }\n\n        if (iter.lower.is_defined() || iter.upper.is_defined())\n        {\n            iteration_space_str += iter.lower.to_str() + \"<=\" + iter.get_name() + \"<\" + iter.upper.to_str();\n            insert_and = true;\n        }\n    }\n\n    iteration_space_str += \"}\";\n\n    return iteration_space_str;\n}\n\n\nconst std::string &polyfp::compute::get_name() const\n{\n    return name;\n}\n\npolyfp::primitive_t polyfp::compute::get_data_type() const\n{\n    return data_type;\n}\n\nstd::vector<int> compute::get_loop_level_numbers_from_dimension_names(\n        std::vector<std::string> dim_names)\n{\n    assert(dim_names.size() > 0);\n    std::vector<int> dim_numbers;\n\n    for (auto const dim: dim_names)\n    {\n        assert(dim.size()>0);\n        if (dim == \"root\")\n        {\n            int d = compute::root_dimension;\n            dim_numbers.push_back(d);\n        }\n        else\n        {\n            int d = isl_map_find_dim_by_name(this->get_schedule(), isl_dim_out,\n                                             dim.c_str());\n            // polyfp::str_dump(\"Searching in the range of \", isl_map_to_str(this->get_schedule()));\n            if (d < 0)\n            {\n                ERROR(\"Dimension \" + dim + \" not found.\", true);\n            }\n\n            // polyfp::str_dump(\"Corresponding loop level is \" + std::to_string(dynamic_dimension_into_loop_level(d)));\n            dim_numbers.push_back(dynamic_dimension_into_loop_level(d));\n        }\n    }\n    this->check_dimensions_validity(dim_numbers);\n    return dim_numbers;\n}\n\nstruct param_pack_1\n{\n    int in_dim;\n    int out_constant;\n};\n\n/**\n * Derived from Tiramisu:\n * Take a basic map as input, go through all of its constraints,\n * identifies the constraint of the static dimension param_pack_1.in_dim\n * (passed in user) and replace the value of param_pack_1.out_constant if\n * the static dimension is bigger than that value.\n */\nisl_stat extract_static_dim_value_from_bmap(__isl_take isl_basic_map *bmap, void *user)\n{\n    struct param_pack_1 *data = (struct param_pack_1 *) user;\n\n    isl_constraint_list *list = isl_basic_map_get_constraint_list(bmap);\n    int n_constraints = isl_constraint_list_n_constraint(list);\n\n    for (int i = 0; i < n_constraints; i++)\n    {\n        isl_constraint *cst = isl_constraint_list_get_constraint(list, i);\n        isl_val *val = isl_constraint_get_coefficient_val(cst, isl_dim_out, data->in_dim);\n        if (isl_val_is_one(val)) // i.e., the coefficient of the dimension data->in_dim is 1\n        {\n            isl_val *val2 = isl_constraint_get_constant_val(cst);\n            int const_val = (-1) * isl_val_get_num_si(val2);\n            data->out_constant = const_val;\n        }\n    }\n\n    return isl_stat_ok;\n}\n\n// Derived from Tiramisu:\n// if multiple const values exist, choose the maximal value among them because we\n// want to use this value to know by how much we shift the computations back.\n// so we need to figure out the maximal const value and use it to shift the iterations\n// backward so that that iteration runs before the consumer.\nisl_stat extract_constant_value_from_bset(__isl_take isl_basic_set *bset, void *user)\n{\n    struct param_pack_1 *data = (struct param_pack_1 *) user;\n\n    isl_constraint_list *list = isl_basic_set_get_constraint_list(bset);\n    int n_constraints = isl_constraint_list_n_constraint(list);\n\n    for (int i = 0; i < n_constraints; i++)\n    {\n        isl_constraint *cst = isl_constraint_list_get_constraint(list, i);\n        if (isl_constraint_is_equality(cst) &&\n                isl_constraint_involves_dims(cst, isl_dim_set, data->in_dim, 1))\n        {\n            isl_val *val = isl_constraint_get_coefficient_val(cst, isl_dim_out, data->in_dim);\n            assert(isl_val_is_one(val));\n            // assert that the coefficients of all the other dimension spaces are zero.\n            isl_val *val2 = isl_constraint_get_constant_val(cst);\n            int const_val = (-1) * isl_val_get_num_si(val2);\n            data->out_constant = std::max(data->out_constant, const_val);\n        }\n    }\n    return isl_stat_ok;\n}\n\n\n/**\n * Derived from Tiramisu:\n * Return the value of the static dimension.\n * 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]}\n * and call isl_map_get_static_dim(M, 5, 1), it will return 3.\n */\nint isl_map_get_static_dim(isl_map *map, int dim_pos)\n{\n    assert(map != NULL);\n    assert(dim_pos >= 0);\n    assert(dim_pos <= (signed int) isl_map_dim(map, isl_dim_out));\n\n    // polyfp::str_dump(\"Getting the constant coefficient of \", isl_map_to_str(map));\n    // polyfp::str_dump(\" at dimension \");\n    // polyfp::str_dump(std::to_string(dim_pos));\n    struct param_pack_1 *data = (struct param_pack_1 *) malloc(sizeof(struct param_pack_1));\n    data->out_constant = 0;\n    data->in_dim = dim_pos;\n    isl_map_foreach_basic_map(isl_map_copy(map),\n                              &extract_static_dim_value_from_bmap,\n                              data);\n    // polyfp::str_dump(\"The constant is: \");\n    // polyfp::str_dump(std::to_string(data->out_constant));\n    return data->out_constant;\n}\n\nstd::vector<polyfp::expr> compute::get_loads(){\n    auto expr = this->get_expr();\n    std::vector<polyfp::expr > loads;\n    expr.get_access_vector(loads);  \n    return loads;\n}\n\nvoid compute::get_loads_stores()\n{\n    auto s_loads =this->get_loads();\n    std::map<std::string, std::vector<polyfp::expr>> s_single_ls;\n    std::vector<polyfp::expr> s_stores;\n\n    s_stores.push_back(this->get_placeholder_expr());\n    s_single_ls.insert(std::pair(\"load\", s_loads));\n    s_single_ls.insert(std::pair(\"store\", s_stores));\n    this->map_loadstores.insert(std::pair(-1,s_single_ls));\n\n    for (auto &edge: this->components)\n    {   \n        // std::cout<<\"dump_component:\"+edge.first->get_name()<<std::endl;\n        auto loads = edge.first->get_loads();\n        std::map<std::string, std::vector<polyfp::expr>> single_ls;\n        std::vector<polyfp::expr> stores;\n        stores.push_back(edge.first->get_placeholder_expr());\n        // std::cout<<\"component:\"+std::to_string(loads.size())<<std::endl;\n        // std::cout<<\"component:\"+edge.first->get_placeholder_expr().get_name()<<std::endl;\n        single_ls.insert(std::pair(\"load\", loads));\n        single_ls.insert(std::pair(\"store\", stores));\n        this->map_loadstores.insert(std::pair(edge.second,single_ls));\n    }\n}\n\nvoid compute::get_all_loadstores()\n{\n    this->get_loads_stores();\n    for(auto &level: this->map_loadstores){\n        for(auto &map: level.second){\n            if(map.first == \"load\"){\n                for(auto &op: map.second){\n                    if(this->load_map.find(op.get_name()) == this->load_map.end()){\n                        this->load_map.insert(std::pair(op.get_name(), &op));\n                    }\n                    this->load_vector.push_back(&op);\n                    // // std::cout<<\"load_vector:\"+op.get_name()<<std::endl;            \n                }\n                \n            }else if(map.first == \"store\"){\n                for(auto &op: map.second){\n                    if(this->store_map.find(op.get_name()) == this->store_map.end()){\n                        this->store_map.insert(std::pair(op.get_name(), &op));\n                    }\n                    this->store_vector.push_back(&op);\n                }\n            }\n        }\n    }\n}\n\n\nvoid compute::check_loop_interchange(){\n    bool is_legal = true;\n    // std::map<int, polyfp::expr> new_order_map;\n    std::map<polyfp::expr * , std::vector<polyfp::expr *> > new_order_map ;\n    std::unordered_map<std::string, int> final_dim_order;\n    std::vector<std::string> dims_no_dp;\n\n    // std::cout<<\"check_loop_interchange:\"<<std::endl;\n    for(auto &vector_list : this->map_dependence_vectors)\n    {\n        std::vector<std::string> dims_no_dp;\n        auto vectors = vector_list.second;\n        auto dim_list = vector_list.first->get_access();\n        std::unordered_map<polyfp::expr *, int> new_vector_map;\n        for(auto &vector: vectors)\n        {\n            int dims = vector.size();\n            bool has_zero = false;\n            int zero_number = 0;\n            for(int i=0; i<dims; i++){\n                if(vector[i] == 0)\n                {\n                    has_zero = true;\n                    zero_number += 1;\n                    if (dims_no_dp.size()==0)\n                    {\n                        if(dim_list[i].get_expr_type() == polyfp::e_op)\n                        {\n                            if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var){\n                                dims_no_dp.push_back(dim_list[i].get_operand(0).get_name());\n                            }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var){\n                                dims_no_dp.push_back(dim_list[i].get_operand(1).get_name());\n                            }\n                        }\n                        else{\n                            dims_no_dp.push_back(dim_list[i].get_name());\n                        }\n                    }\n                    else{                       \n                        std::string name;\n                        if(dim_list[i].get_expr_type()==polyfp::e_op)\n                        {\n                            if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var){\n                                name = dim_list[i].get_operand(0).get_name();\n                                // dims_no_dp.push_back(dim_list[i].get_operand(0).get_name());\n                            }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var){\n                                name = dim_list[i].get_operand(0).get_name();\n                                // dims_no_dp.push_back(dim_list[i].get_operand(1).get_name());\n                            }\n                            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),name);\n                            if ( iter==dims_no_dp.end()){\n                                dims_no_dp.push_back(name);\n                            }\n                        }else{\n                            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),dim_list[i].get_name());\n                            if ( iter==dims_no_dp.end()){\n                                dims_no_dp.push_back(dim_list[i].get_name());\n                            }\n                        }\n                    }                  \n                }\n                if(vector[i] < 0)\n                {\n                    has_zero = true;\n                    zero_number += 1;\n                    is_legal = false;\n                }\n                if(vector[i] > 0)\n                {\n                    has_zero = true;\n                    zero_number += 1;\n                    std::string dim_name;\n                    polyfp::expr temp_dim;\n                    if(dim_list[i].get_expr_type()==polyfp::e_op)\n                    {\n                        if (dim_list[i].get_operand(0).get_expr_type() == polyfp::e_var)\n                        {\n                            dim_name = dim_list[i].get_operand(0).get_name();\n                            temp_dim = dim_list[i].get_operand(0);\n                        }else if(dim_list[i].get_operand(1).get_expr_type() == polyfp::e_var)\n                        {\n                            dim_name = dim_list[i].get_operand(1).get_name();\n                            temp_dim = dim_list[i].get_operand(1);\n                        }\n                    }else\n                    {\n                        dim_name = dim_list[i].get_name();\n                        temp_dim = dim_list[i];\n                    }\n                    if (new_vector_map.find(&temp_dim) == new_vector_map.end())\n                    {\n                        new_vector_map[&temp_dim] = vector[i];\n                    }else if(new_vector_map[&temp_dim]>vector[i]){\n                        new_vector_map[&temp_dim] = vector[i];\n                    }\n                    std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),dim_name);\n                    if ( iter!=dims_no_dp.end())\n                    {\n                        iter = dims_no_dp.erase(iter);\n                    }\n                }\n                \n            }\n            \n        }\n        std::vector<std::pair<polyfp::expr *, int>> tmp;\n        std::vector<polyfp::expr *> dim_order;\n        for (auto& i : new_vector_map)\n            tmp.push_back(i);\n        \n        std::sort(tmp.begin(), tmp.end(), [=](std::pair<polyfp::expr *, int>& a, std::pair<polyfp::expr *, int>& b) { return a.second < b.second; });\n        // other dims should be moved to outer level first.\n        // std::cout<<\"new_vector_map:\"+ std::to_string(new_vector_map.size())<<std::endl;\n        for(auto &other_dim:this->get_iteration_variables())\n        {\n            std::vector<std::string>::iterator iter=find(dims_no_dp.begin(),dims_no_dp.end(),other_dim.get_name());\n            if(new_vector_map.find(&other_dim) == new_vector_map.end()&& iter==dims_no_dp.end())\n            {\n                dim_order.push_back(&other_dim);\n            }\n        }\n        // move dims that have loop carried dependencies.\n        // remove all elements with value val\n        for(auto &dim: tmp)\n        {\n            dim_order.push_back(dim.first);\n        }\n\n        new_order_map[vector_list.first] = dim_order;\n\n        for(auto &kv :new_vector_map)\n        {\n            if (final_dim_order.find(kv.first->get_name()) == final_dim_order.end()&&final_dim_order.size()<this->get_iteration_domain_dimensions_number())\n            {\n                final_dim_order[kv.first->get_name()] = kv.second;\n            }else if(final_dim_order[kv.first->get_name()]>kv.second)\n            {\n                final_dim_order[kv.first->get_name()] = kv.second;\n            }\n\n        }\n    }\n    // Decide a common order and detect conflicts\n    // Define confilct: for all comps in the nested loop, \n    // number of dims that need to be interchanged should not exceed total dims number-1\n    // gradually add computes until conflicts occur\n    std::vector<std::string> waiting_list;\n    bool need_split = false;\n    polyfp::compute *comp_to_split ;\n    \n    for(auto &dvectors: new_order_map)\n    {\n        for(auto &dvector:dvectors.second)\n        {\n            std::vector<std::string>::iterator iter=find(waiting_list.begin(),waiting_list.end(),dvector->get_name());\n            if ( iter==waiting_list.end())\n            {\n                waiting_list.push_back(dvector->get_name());\n            }\n            if(waiting_list.size() == this->get_iteration_domain_dimensions_number())\n            {\n                need_split = true;\n                comp_to_split = dvectors.first->owner;\n            }\n        }\n    }\n\n    if(need_split == true && is_legal == true)\n    {\n        // TODO: if there is no dependency between comp_to_split and \n        // other comps(its leader and component), split it from the nested loop\n        int top_level = 0;\n        // for(auto &dim: waiting_list){\n        //     int level = this->get_loop_level_number_from_dimension_name(dim);\n        //     if(level!=0){\n        //          comp_to_split->interchange(top_level,level);\n        //     }\n        // }\n        // comp_to_split->after(comp_to_split->leader,comp_to_split->leader->get_iteration_domain_dimensions_number()-1);\n        comp_to_split->after(comp_to_split->leader, -1);\n        comp_to_split->get_all_loadstores();\n        // comp->dump_components();\n        // comp->dump_loads_stores();\n        comp_to_split->dump_all_loadstores();\n        comp_to_split->compute_dependence_vectors();\n        comp_to_split->check_loop_interchange();\n\n    }\n    if(need_split == false)\n    {\n        int top_level = 0;\n        for(auto &dim: waiting_list)\n        {\n            int level = this->get_loop_level_number_from_dimension_name(dim);\n            this->interchange(top_level,level);\n            int count = level-top_level-1;\n            if(count!=0){\n                for(int i=0; i<count;i++){\n                    this->interchange(top_level+1+i,level);\n                }\n            }\n            top_level+=1;\n        }\n        for(auto &map: this->components)\n        {\n            int dims = map.first->get_iteration_variables().size();\n            if(map.first->after_level==dims-1)\n            {\n                int top_level2 = 0;\n                for(auto &dim: waiting_list)\n                {\n                    int level = map.first->get_loop_level_number_from_dimension_name(dim);\n                    //TODO: Potential bugs here\n                    map.first->interchange(top_level2,level);\n                    int count = level-top_level2-1;\n                    if(count!=0)\n                    {\n                        for(int i=0; i<count;i++){\n                            map.first->interchange(top_level2+1+i,level);\n                        }\n                    }\n                    map.first->after(map.first->leader,this->get_iteration_domain_dimensions_number()-1);\n                    top_level2+=1;\n                }\n            }\n        }\n    }\n}\n\nvoid compute::check_loop_skewing()\n{\n    bool is_legal = false;\n    int factor=1;\n    if(this->map_dependence_vectors.size() == 1)\n    {\n        for(auto &vector_list : this->map_dependence_vectors)\n        {\n            std::vector<std::string> dims_no_dp;\n            auto vectors = vector_list.second;\n            auto dim_list = vector_list.first->get_access();\n            std::unordered_map<polyfp::expr *, int> new_vector_map;\n            // TODO: factors\n            if(8>=vectors.size()&&vectors.size()>=2)\n            {\n                is_legal = true;\n                factor = 1;\n            }\n            else if(8<=vectors.size()){\n                is_legal = true;\n                factor = 2;\n            }else{\n                // TODO\n            }\n        }\n\n        auto iterators = this->get_iteration_variables();\n        int size = iterators.size();\n        std::map<int,polyfp::var> iterator_map;\n\n        for(auto &iter: iterators)\n        {\n            int loc = this->get_loop_level_number_from_dimension_name(iter.get_name());\n            // std::cout<<iter.get_name()<<std::endl;\n            iterator_map[loc] = iter;\n        }\n        var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n        \n        if(is_legal == true)\n        {\n            if(size==3)\n            {\n                this->skew(iterator_map[1],iterator_map[2],1,factor,i0,j0);\n            }else if(size==2)\n            {\n                this->skew(iterator_map[0],iterator_map[1],1,factor,i0,j0);\n            }\n            this->is_skewed_inDSE = true;\n        }\n    }\n}\n\nvoid compute::auto_loop_transformation()\n{\n    this->check_loop_interchange();\n    this->check_loop_skewing();\n}\n\nvoid compute::apply_opt_strategy(std::vector<int> tile_size){\n    std::map<int,polyfp::var> iterator_map;\n    \n    this->set_schedule(this->original_schedule);\n    this->set_loop_level_names(this->original_loop_level_name);\n    this->directive_map.clear();\n    this->is_unrolled = false;\n    this->unroll_factor.clear();\n    this->unroll_dimension.clear();\n    this->tile_map.clear();\n    this->tile_size_map.clear();\n    this->access_map.clear();\n    auto iterators = this->get_iteration_variables();\n    int size = iterators.size();\n    //TODO: SKEW MAP\n    for(auto &iter: iterators)\n    {\n        int loc = this->get_loop_level_number_from_dimension_name(iter.get_name());\n        iterator_map[loc] = iter;\n    }\n\n    if(size >= 3)\n    {\n        var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n        // TODO: Config file\n        if(tile_size[0]<=64 && tile_size[1]<64 && tile_size[2]<64)\n        {\n            int temp_index = this->get_iteration_variables().size()-3;\n\n            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1)\n            {  \n                // TODO   \n            }else\n            {\n                this->tile(iterator_map[temp_index],iterator_map[temp_index+1],\n                    iterator_map[temp_index+2],tile_size[0],tile_size[1],tile_size[2],i0, j0, k0, i1, j1, k1);\n            }\n            // std::cout<<iterator_map[temp_index].get_name()<<std::endl;\n            if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]!=1)\n            {\n                this->pipeline(k0,1);\n                this->unroll(k1,-1);\n                this->unroll(j1,-1);\n                this->unroll(i1,-1);\n            }\n            if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]==1)\n            {\n                this->pipeline(k0,1);\n                this->unroll(k1,-1);\n                this->unroll(j1,-1);\n            }\n            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]!=1)\n            {\n                this->pipeline(iterator_map[temp_index+2],1);\n                // comp->unroll(k1,-1);\n                // comp->unroll(j1,-1);\n                this->unroll(i1,-1);\n            }\n            if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]!=1)\n            {\n                this->pipeline(k0,1);\n                this->unroll(k1,-1);\n                this->unroll(i1,-1);\n            }\n            if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1)\n            {\n                int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());\n                int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());\n                int range = upper-lower;\n                // TODO: Config\n                if(range<=7)\n                {\n                    this->pipeline(iterator_map[temp_index+1],1);\n                    this->unroll(iterator_map[temp_index+2],-1);\n                }\n            }\n            if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]==1)\n            {\n                this->pipeline(k0,1);\n                this->unroll(k1,-1);\n            }\n            if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1)\n            {\n                int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());\n                int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());\n                int range = upper-lower;\n                if(range<=6)\n                {\n                    this->pipeline(j0,1);\n                    this->unroll(j1,-1);\n                    this->unroll(i1,-1);\n                    this->unroll(iterator_map[temp_index+2],-1);\n                }else\n                {\n                    this->pipeline(iterator_map[temp_index+2],1);\n                    this->unroll(j1,-1);\n                    this->unroll(i1,-1);\n                }\n            }\n            for(auto &part:this->components)\n            {\n                part.first->set_schedule(part.first->original_schedule);\n                part.first->set_loop_level_names(part.first->original_loop_level_name);\n                part.first->tile(iterator_map[temp_index+0],iterator_map[temp_index+1],\n                    iterator_map[temp_index+2],tile_size[0],tile_size[1],tile_size[2],i0, j0, k0, i1, j1, k1);\n\n                if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1)\n                {\n                    if(part.first->after_level == 2)\n                    {\n                        part.first->after(this,j1);\n                    }\n                    else if(part.first->after_level == 0)\n                    {\n                        part.first->after(this,i0);\n                        part.first->pipeline(iterator_map[temp_index+2],1);   \n                    }\n                }else\n                {\n                    if(part.first->after_level == 2)\n                    {\n                        part.first->after(this,k1);\n                    }\n                    else if\n                    (part.first->after_level == 0){\n                        part.first->after(this,iterator_map[temp_index+0]);\n                        part.first->pipeline(iterator_map[temp_index+2],1);   \n                        //TODO\n                        part.first->unroll(k1,-1);\n                        part.first->unroll(j1,-1);\n                    }\n                }\n            }\n        }\n    }\n    else if(size == 2)\n    {\n        var i0(\"i0\"), j0(\"j0\"), i1(\"i1\"), j1(\"j1\");\n        // TODO: Config file\n        if(tile_size[0]<64 && tile_size[1]<64)\n        {\n            this->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);\n            if(tile_size[1]!=1&&tile_size[0]!=1)\n            {\n                this->pipeline(j0,1);\n                this->unroll(j1,-1);\n                this->unroll(i1,-1);\n            }else if(tile_size[1]==1&&tile_size[0]!=1)\n            {\n                this->pipeline(iterator_map[1],1);\n                this->unroll(i1,-1);\n            }else if(tile_size[0]==1&&tile_size[1]!=1)\n            {\n                this->pipeline(j0,1);\n                this->unroll(j1,-1);\n            }\n            for(auto &part:this->components)\n            {\n                part.first->set_schedule(part.first->original_schedule);\n                part.first->set_loop_level_names(part.first->original_loop_level_name);\n                part.first->directive_map.clear();\n                part.first->is_unrolled = false;\n                part.first->unroll_factor.clear();\n                part.first->unroll_dimension.clear();\n                part.first->tile_map.clear();\n                part.first->tile_size_map.clear();\n                part.first->access_map.clear();\n                part.first->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);\n\n                if(tile_size[1]!=1&&tile_size[0]!=1)\n                {\n                    if(part.first->after_level == 1)\n                    {\n                        part.first->after(this,j1);\n                    }\n                    else if(part.first->after_level == 0)\n                    {\n                        part.first->after(this,i0);\n                        part.first->pipeline(j0,1);\n                    }\n                    \n                }\n                else if(tile_size[1]==1&&tile_size[0]!=1)\n                {\n                    if(part.first->after_level == 1)\n                    {\n                        part.first->after(this,i1);\n                    }\n                    else if(part.first->after_level == 0)\n                    {\n                        part.first->pipeline(iterator_map[1],1);\n                        part.first->after(this,i0);\n                    }                  \n                }\n                else if(tile_size[0]==1&&tile_size[1]!=1)\n                {\n                    if(part.first->after_level == 1)\n                    {\n                        part.first->after(this,j1);                                  \n                    }\n                    else if(part.first->after_level == 0)\n                    {\n                        part.first->after(this,iterator_map[0]);\n                        part.first->pipeline(j0,1);\n                        part.first->unroll(j1,-1);\n                    }\n                    else if(part.first->after_level == 2)\n                    {\n                        part.first->after(this,j1);\n                    }\n                }\n            }\n        } \n    }\n}\n\n\n\nvoid compute::compute_dependence_vectors()\n{\n    for(auto &store: this->store_vector)\n    {\n        auto store_index = store->get_access();\n        auto dims = store_index.size();\n        for(auto &load: this->load_vector)\n        {\n            auto load_index = load->get_access();\n            if(store->get_name() == load->get_name())\n            {\n                // std::cout<<\"array \" + store->get_name()<<std::endl;\n                std::vector<int> vector_set;\n                for(int i = 0; i < dims; i++)\n                {\n                    auto vector_element = store_index[i].get_dependence_vector()-load_index[i].get_dependence_vector();\n                    // std::cout<<\"vector of dimension \" + std::to_string(i)+\"is: \"+std::to_string(vector_element)<<std::endl;\n                    vector_set.push_back(vector_element);\n\n                }\n                this->map_dependence_vectors[store].push_back(vector_set);\n            }\n        }\n    }\n}\n\nvoid compute::dump_all_loadstores()\n{\n    std::string result1 = \"loads:\";\n    std::string result2 = \"stores:\";\n    for(auto &op: this->load_map){\n        result1 += op.first +\" \";\n    }\n    for(auto &op: this->store_map){\n        result2 += op.first +\" \";\n    }\n}\n\nvoid compute::interchange(polyfp::var L0_var, polyfp::var L1_var)\n{\n    assert(L0_var.get_name().length() > 0);\n    assert(L1_var.get_name().length() > 0);\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name(), L1_var.get_name()});\n\n    this->check_dimensions_validity(dimensions);\n    int L0 = dimensions[0];\n    int L1 = dimensions[1];\n\n    this->interchange(L0, L1);\n}\n\nvoid compute::interchange(int L0, int L1)\n{\n    int inDim0 = loop_level_into_dynamic_dimension(L0);\n    int inDim1 = loop_level_into_dynamic_dimension(L1);\n\n    assert(inDim0 >= 0);\n    assert(inDim0 < isl_space_dim(isl_map_get_space(this->get_schedule()),\n                                  isl_dim_out));\n    assert(inDim1 >= 0);\n    assert(inDim1 < isl_space_dim(isl_map_get_space(this->get_schedule()),\n                                  isl_dim_out));\n\n    isl_map *schedule = this->get_schedule();\n\n    // polyfp::str_dump(\"Original schedule: \", isl_map_to_str(schedule));\n    // polyfp::str_dump(\"Interchanging the dimensions \" + std::to_string(\n    //                                 L0) + \" and \" + std::to_string(L1));\n\n    int n_dims = isl_map_dim(schedule, isl_dim_out);\n\n    std::string inDim0_str = isl_map_get_dim_name(schedule, isl_dim_out, inDim0);\n    std::string inDim1_str = isl_map_get_dim_name(schedule, isl_dim_out, inDim1);\n\n    std::vector<isl_id *> dimensions;\n\n    // Create a map for the duplicate schedule.\n    std::string map = \"{ \" + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            int duplicate_ID = isl_map_get_static_dim(schedule, 0);\n            map = map + std::to_string(duplicate_ID);\n        }\n        else\n        {\n            if (isl_map_get_dim_name(schedule, isl_dim_out, i) == NULL)\n            {\n                isl_id *new_id = isl_id_alloc(this->get_ctx(), generate_new_variable_name().c_str(), NULL);\n                schedule = isl_map_set_dim_id(schedule, isl_dim_out, i, new_id);\n            }\n\n            map = map + isl_map_get_dim_name(schedule, isl_dim_out, i);\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n\n    map = map + \"] ->\" + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            int duplicate_ID = isl_map_get_static_dim(schedule, 0);\n            map = map + std::to_string(duplicate_ID);\n        }\n        else\n        {\n            if ((i != inDim0) && (i != inDim1))\n            {\n                map = map + isl_map_get_dim_name(schedule, isl_dim_out, i);\n                dimensions.push_back(isl_map_get_dim_id(schedule, isl_dim_out, i));\n            }\n            else if (i == inDim0)\n            {\n                map = map + inDim1_str;\n                isl_id *id1 = isl_id_alloc(this->get_ctx(), inDim1_str.c_str(), NULL);\n                dimensions.push_back(id1);\n            }\n            else if (i == inDim1)\n            {\n                map = map + inDim0_str;\n                isl_id *id1 = isl_id_alloc(this->get_ctx(), inDim0_str.c_str(), NULL);\n                dimensions.push_back(id1);\n            }\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n    map = map + \"]}\";\n    // polyfp::str_dump(map.c_str());\n\n    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());\n\n    transformation_map = isl_map_set_tuple_id(\n                             transformation_map, isl_dim_in, isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));\n    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);\n    transformation_map = isl_map_set_tuple_id(\n                             transformation_map, isl_dim_out, id_range);\n\n    // Check that the names of each dimension is well set\n    for (int i = 1; i < isl_map_dim(transformation_map, isl_dim_in); i++)\n    {\n        isl_id *dim_id = isl_id_copy(dimensions[i - 1]);\n        transformation_map = isl_map_set_dim_id(transformation_map, isl_dim_out, i, dim_id);\n        assert(isl_map_has_dim_name(transformation_map, isl_dim_in, i));\n        assert(isl_map_has_dim_name(transformation_map, isl_dim_out, i));\n    }\n    // polyfp::str_dump(\"Final transformation map : \", isl_map_to_str(transformation_map));\n\n    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));\n\n    // polyfp::str_dump(\"Schedule after interchange: \", isl_map_to_str(schedule));\n\n    this->set_schedule(schedule);\n}\n\n\n\n\nvoid compute::split(polyfp::var L0_var, int sizeX)\n{\n    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());\n    this->split(L0_var, sizeX, L0_outer, L0_inner);\n}\n\nvoid compute::split(polyfp::var L0_var, int sizeX,\n        polyfp::var L0_outer, polyfp::var L0_inner)\n{\n    // polyfp::str_dump(\"Schedule after interchange: \");\n    assert(L0_var.get_name().length() > 0);\n\n    std::vector<std::string> original_loop_level_names =\n        this->get_loop_level_names();\n\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name()});\n    // polyfp::str_dump(\"Scget_loop_level_numbers_from_dimension_nameshedule after interchange: \");\n    this->check_dimensions_validity(dimensions);\n    int L0 = dimensions[0];\n    this->assert_names_not_assigned({L0_outer.get_name(), L0_inner.get_name()});\n    this->split(L0, sizeX);\n    this->set_loop_level_names({L0_outer.get_name(), L0_inner.get_name()});\n    // this->update_names(original_loop_level_names, {L0_outer.get_name(), L0_inner.get_name()}, L0, 1);\n    // polyfp::str_dump(L0_outer.get_name());\n    // polyfp::str_dump(L0_inner.get_name());\n}\n\nvoid compute::split(int L0, int sizeX)\n{\n    int inDim0 = loop_level_into_dynamic_dimension(L0);\n\n    assert(this->get_schedule() != NULL);\n    assert(inDim0 >= 0);\n    assert(inDim0 < isl_space_dim(isl_map_get_space(this->get_schedule()), isl_dim_out));\n    assert(sizeX >= 1);\n\n    isl_map *schedule = this->get_schedule();\n    int duplicate_ID = isl_map_get_static_dim(schedule, 0);\n\n    schedule = isl_map_copy(schedule);\n    schedule = isl_map_set_tuple_id(schedule, isl_dim_out,\n                                    isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL));\n\n    // polyfp::str_dump(\"Original schedule: \", isl_map_to_str(schedule));\n    // polyfp::str_dump(\"Splitting dimension \" + std::to_string(inDim0)\n    //                             + \" with split size \" + std::to_string(sizeX));\n\n    std::string inDim0_str;\n\n    std::string outDim0_str = generate_new_variable_name();\n    std::string static_dim_str = generate_new_variable_name();\n    std::string outDim1_str = generate_new_variable_name();\n\n    int n_dims = isl_map_dim(this->get_schedule(), isl_dim_out);\n    std::vector<isl_id *> dimensions;\n    std::vector<std::string> dimensions_str;\n    std::string map = \"{\";\n\n    map = map + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            std::string dim_str = generate_new_variable_name();\n            dimensions_str.push_back(dim_str);\n            map = map + dim_str;\n        }\n        else\n        {\n            std::string dim_str = generate_new_variable_name();\n            dimensions_str.push_back(dim_str);\n            map = map + dim_str;\n\n            if (i == inDim0)\n            {\n                inDim0_str = dim_str;\n            }\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n\n    map = map + \"] -> \" + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            map = map + dimensions_str[i];\n            dimensions.push_back(isl_id_alloc(\n                                     this->get_ctx(),\n                                     dimensions_str[i].c_str(),\n                                     NULL));\n        }\n        else if (i != inDim0)\n        {\n            map = map + dimensions_str[i];\n            dimensions.push_back(isl_id_alloc(\n                                     this->get_ctx(),\n                                     dimensions_str[i].c_str(),\n                                     NULL));\n        }\n        else\n        {\n            map = map + outDim0_str + \", \" + static_dim_str + \", \" + outDim1_str;\n            isl_id *id0 = isl_id_alloc(this->get_ctx(),\n                                       outDim0_str.c_str(), NULL);\n            isl_id *id2 = isl_id_alloc(this->get_ctx(),\n                                       static_dim_str.c_str(), NULL);\n            isl_id *id1 = isl_id_alloc(this->get_ctx(),\n                                       outDim1_str.c_str(), NULL);\n            dimensions.push_back(id0);\n            dimensions.push_back(id2);\n            dimensions.push_back(id1);\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n\n    map = map + \"] : \" + dimensions_str[0] + \" = \" + std::to_string(duplicate_ID) + \" and \" +\n          outDim0_str + \" = floor(\" + inDim0_str + \"/\" +\n          std::to_string(sizeX) + \") and \" + outDim1_str + \" = (\" +\n          inDim0_str + \"%\" + std::to_string(sizeX) + \") and \" + static_dim_str + \" = 0}\";\n    // std::cout<<map;\n    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());\n\n    for (int i = 0; i < dimensions.size(); i++)\n        transformation_map = isl_map_set_dim_id(\n                                 transformation_map, isl_dim_out, i, isl_id_copy(dimensions[i]));\n\n    transformation_map = isl_map_set_tuple_id(\n                             transformation_map, isl_dim_in,\n                             isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));\n    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);\n    transformation_map = isl_map_set_tuple_id(transformation_map, isl_dim_out, id_range);\n\n    // polyfp::str_dump(\"Transformation map : \", isl_map_to_str(transformation_map));\n\n    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));\n\n    // polyfp::str_dump(\"Schedule after splitting: \", isl_map_to_str(schedule));\n\n    this->set_schedule(schedule);\n\n}\n\nvoid compute::tile(polyfp::var L0, polyfp::var L1,\n        polyfp::var L2, int sizeX, int sizeY, int sizeZ)\n{\n    assert(L0.get_name().length() > 0);\n    assert(L1.get_name().length() > 0);\n    assert(L2.get_name().length() > 0);\n\n    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L1_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L2_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());\n    polyfp::var L1_inner = polyfp::var(generate_new_variable_name());\n    polyfp::var L2_inner = polyfp::var(generate_new_variable_name());\n\n    this->tile(L0, L1, L2, sizeX, sizeY, sizeZ,\n               L0_outer, L1_outer, L0_outer, L0_inner, L1_inner, L2_inner);\n}\n\nvoid compute::tile(polyfp::var L0, polyfp::var L1,\n        int sizeX, int sizeY)\n{\n    assert(L0.get_name().length() > 0);\n    assert(L1.get_name().length() > 0);\n\n    polyfp::var L0_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L1_outer = polyfp::var(generate_new_variable_name());\n    polyfp::var L0_inner = polyfp::var(generate_new_variable_name());\n    polyfp::var L1_inner = polyfp::var(generate_new_variable_name());\n\n    this->tile(L0, L1, sizeX, sizeY,\n               L0_outer, L1_outer, L0_inner, L1_inner);\n}\n\nvoid compute::tile(polyfp::var L0, polyfp::var L1, polyfp::var L2,\n        int sizeX, int sizeY, int sizeZ,\n        polyfp::var L0_outer, polyfp::var L1_outer,\n        polyfp::var L2_outer, polyfp::var L0_inner,\n        polyfp::var L1_inner, polyfp::var L2_inner)\n{\n    assert(L0.get_name().length() > 0);\n    assert(L1.get_name().length() > 0);\n    assert(L2.get_name().length() > 0);\n    assert(L0_outer.get_name().length() > 0);\n    assert(L1_outer.get_name().length() > 0);\n    assert(L2_outer.get_name().length() > 0);\n    assert(L0_inner.get_name().length() > 0);\n    assert(L1_inner.get_name().length() > 0);\n    assert(L2_inner.get_name().length() > 0);\n\n    if(sizeX==0 &&sizeY==0&&sizeZ==0)\n    {\n        return;\n    }\n\n    this->assert_names_not_assigned({L0_outer.get_name(), L1_outer.get_name(),\n                                     L2_outer.get_name(), L0_inner.get_name(),\n                                     L1_inner.get_name(), L2_inner.get_name()});\n\n    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();\n\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({L0.get_name(),\n                                                           L1.get_name(),\n                                                           L2.get_name()});\n    assert(dimensions.size() == 3);\n\n    this->tile(dimensions[0], dimensions[1], dimensions[2],\n               sizeX, sizeY, sizeZ);\n\n    if(sizeX == 1 && sizeY == 1 )\n    {\n        this->update_names(original_loop_level_names, {L0.get_name(), L1.get_name(), L2_outer.get_name(),\n                                                       L2_inner.get_name()}, dimensions[0], 3);\n\n    }\n    else if(sizeX == 1 && sizeZ == 1 )\n    {\n        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L2.get_name(),\n                                                       L1_inner.get_name()}, dimensions[0], 3);\n\n    }else if(sizeY == 1 && sizeZ == 1 )\n    {\n        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L2.get_name(),\n                                                       L0_inner.get_name()}, dimensions[0], 3);\n\n    }else if(sizeX == 1)\n    {\n        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L2_outer.get_name(),\n                                                    L1_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);\n    }else if(sizeY == 1)\n    {\n        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L2_outer.get_name(),\n                                                    L0_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);\n    }else if(sizeZ == 1)\n    {\n        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1_outer.get_name(), L2.get_name(),\n                                                    L0_inner.get_name(), L1_inner.get_name()}, dimensions[0], 3);\n    }else{\n        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1_outer.get_name(), L2_outer.get_name(),\n                                                L0_inner.get_name(), L1_inner.get_name(), L2_inner.get_name()}, dimensions[0], 3);\n    }\n\n    this->access_map.insert(std::pair(L0.get_name(),L0_inner.get_name()));\n    this->access_map.insert(std::pair(L1.get_name(),L1_inner.get_name()));\n    this->access_map.insert(std::pair(L2.get_name(),L2_inner.get_name()));\n    this->tile_map.insert(std::pair(L0_inner.get_name(),L0_outer.get_name()));\n    this->tile_map.insert(std::pair(L1_inner.get_name(),L1_outer.get_name()));\n    this->tile_map.insert(std::pair(L2_inner.get_name(),L2_outer.get_name()));\n    this->tile_size_map.insert(std::pair(L0_inner.get_name(),sizeX));\n    this->tile_size_map.insert(std::pair(L1_inner.get_name(),sizeY));\n    this->tile_size_map.insert(std::pair(L2_inner.get_name(),sizeZ));\n    this->is_tiled = true;\n\n}\n\nvoid compute::tile(polyfp::var L0, polyfp::var L1,\n      int sizeX, int sizeY,\n      polyfp::var L0_outer, polyfp::var L1_outer,\n      polyfp::var L0_inner, polyfp::var L1_inner)\n{\n    assert(L0.get_name().length() > 0);\n    assert(L1.get_name().length() > 0);\n    assert(L0_outer.get_name().length() > 0);\n    assert(L1_outer.get_name().length() > 0);\n    assert(L0_inner.get_name().length() > 0);\n    assert(L1_inner.get_name().length() > 0);\n\n    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();\n\n    this->assert_names_not_assigned({L0_outer.get_name(), L1_outer.get_name(),\n                                     L0_inner.get_name(), L1_inner.get_name()});\n\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({L0.get_name(),\n                                                           L1.get_name()});\n\n    assert(dimensions.size() == 2);\n\n    this->tile(dimensions[0], dimensions[1], sizeX, sizeY);\n\n    // Replace the original dimension name with new dimension names\n    if(sizeX == 1)\n    {\n        this->update_names(original_loop_level_names, {L0.get_name(), L1_outer.get_name(), L1_inner.get_name()}, dimensions[0], 2);\n    }\n    else if(sizeY == 1)\n    {\n        this->update_names(original_loop_level_names, {L0_outer.get_name(), L1.get_name(), L0_inner.get_name()}, dimensions[0], 2);\n    }else\n    {\n        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);\n    }\n\n    this->access_map.insert(std::pair(L0.get_name(),L0_inner.get_name()));\n    this->access_map.insert(std::pair(L1.get_name(),L1_inner.get_name()));\n    this->tile_map.insert(std::pair(L0_inner.get_name(),L0_outer.get_name()));\n    this->tile_map.insert(std::pair(L1_inner.get_name(),L1_outer.get_name()));\n    this->tile_size_map.insert(std::pair(L0_inner.get_name(),sizeX));\n    this->tile_size_map.insert(std::pair(L1_inner.get_name(),sizeY));\n    this->is_tiled = true;\n\n}\n\nvoid compute::tile(int L0, int L1, int L2, int sizeX, int sizeY, int sizeZ)\n{\n    // Check that the two dimensions are consecutive.\n    // Tiling only applies on a consecutive band of loop dimensions.\n    assert(L1 == L0 + 1);\n    assert(L2 == L1 + 1);\n    assert((sizeX > 0) && (sizeY > 0) && (sizeZ > 0));\n    assert(this->get_iteration_domain() != NULL);\n\n    this->check_dimensions_validity({L0, L1, L2});\n\n    //  Original loops\n    //  L0\n    //    L1\n    //      L2\n\n    this->split(L0, sizeX); // Split L0 into L0 and L0_prime\n    // Compute the new L1 and the new L2 and the newly created L0 (called L0 prime)\n    int L0_prime = L0 + 1;\n    L1 = L1 + 1;\n    L2 = L2 + 1;\n\n    //  Loop after transformation\n    //  L0\n    //    L0_prime\n    //      L1\n    //        L2\n\n    this->split(L1, sizeY);\n    int L1_prime = L1 + 1;\n    L2 = L2 + 1;\n\n    //  Loop after transformation\n    //  L0\n    //    L0_prime\n    //      L1\n    //        L1_prime\n    //          L2\n\n    this->split(L2, sizeZ);\n\n    //  Loop after transformation\n    //  L0\n    //    L0_prime\n    //      L1\n    //        L1_prime\n    //          L2\n    //            L2_prime\n\n    this->interchange(L0_prime, L1);\n    // Change the position of L0_prime to the new position\n    int temp = L0_prime;\n    L0_prime = L1;\n    L1 = temp;\n\n    //  Loop after transformation\n    //  L0\n    //    L1\n    //      L0_prime\n    //        L1_prime\n    //          L2\n    //            L2_prime\n\n    this->interchange(L0_prime, L2);\n    // Change the position of L0_prime to the new position\n    temp = L0_prime;\n    L0_prime = L2;\n    L2 = temp;\n\n    //  Loop after transformation\n    //  L0\n    //    L1\n    //      L2\n    //        L1_prime\n    //          L0_prime\n    //            L2_prime\n\n    this->interchange(L1_prime, L0_prime);\n\n    //  Loop after transformation\n    //  L0\n    //    L1\n    //      L2\n    //        L0_prime\n    //          L1_prime\n    //            L2_prime\n}\n\nvoid compute::tile(int L0, int L1, int sizeX, int sizeY)\n{\n    // Check that the two dimensions are consecutive.\n    // Tiling only applies on a consecutive band of loop dimensions.\n    assert(L1 == L0 + 1);\n    assert((sizeX > 0) && (sizeY > 0));\n    assert(this->get_iteration_domain() != NULL);\n    this->check_dimensions_validity({L0, L1});\n\n    if(sizeX != 1)\n    {\n        this->split(L0, sizeX);\n        this->split(L1 + 1, sizeY);\n        this->interchange(L0 + 1, L1 + 1);\n    }else\n    {\n        this->split(L1, sizeY);\n    }\n\n}\n\n\nvoid compute::skew(polyfp::var L0_var, polyfp::var L1_var,\n\t\t       int f_i, int f_j ,\n\t\t       polyfp::var new_L0_var, polyfp::var new_L1_var)\n{\n    assert(L0_var.get_name().length() > 0);\n    assert(L1_var.get_name().length() > 0);\n    assert(new_L0_var.get_name().length() > 0);\n    assert(new_L1_var.get_name().length() > 0);\n\n    this->assert_names_not_assigned({new_L0_var.get_name(), new_L1_var.get_name()});\n    std::vector<std::string> original_loop_level_names = this->get_loop_level_names();\n\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({L0_var.get_name(), L1_var.get_name()});\n\n    this->check_dimensions_validity(dimensions);\n    this->is_skewed = true;\n    int L0 = dimensions[0];\n    int L1 = dimensions[1];\n    this->skew(L0, L1, f_i,f_j );\n    this->update_names(original_loop_level_names, {new_L0_var.get_name(), new_L1_var.get_name()}, dimensions[0], 2);\n    this->access_map.insert(std::pair(L0_var.get_name(),new_L1_var.get_name()));\n    this->access_map.insert(std::pair(L1_var.get_name(),new_L0_var.get_name()));\n    this->iterator_to_skew = new_L1_var.get_name();\n    this->iterator_to_modify = new_L0_var.get_name();\n    this->skew_factor = f_j;\n\n}\n\nvoid compute::skew(int L0 , int L1 , int f_i , int f_j)\n{\n    if (L0 + 1 != L1)\n    {\n\t    ERROR(\"Loop levels passed to angle_skew() should be consecutive. The first argument to angle_skew() should be the outer loop level.\", true);\n    }\n\n    assert(f_j != 0);\n    assert(f_i >= 0);\n   \n    int dim0 = loop_level_into_dynamic_dimension(L0);\n    int dim1 = loop_level_into_dynamic_dimension(L1);\n\n    assert(this->get_schedule() != NULL);\n    assert(dim0 >= 0);\n    assert(dim0 < isl_space_dim(isl_map_get_space(this->get_schedule()), isl_dim_out));\n    isl_map *schedule = this->get_schedule();\n    int duplicate_ID = isl_map_get_static_dim(schedule, 0);\n\n    schedule = isl_map_copy(schedule);\n    schedule = isl_map_set_tuple_id(schedule, isl_dim_out,\n                                    isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL));\n    // polyfp::str_dump(\"Original schedule: \", isl_map_to_str(schedule));\n    // polyfp::str_dump(\"Angle _ Skewing dimensions \" + std::to_string(dim0)\n    //                             + \" and \" + std::to_string(dim1));\n    std::string inDim0_str, inDim1_str;\n    std::string outDim1_str = generate_new_variable_name();\n    std::string outDim0_str = generate_new_variable_name();\n\n    int n_dims = isl_map_dim(this->get_schedule(), isl_dim_out);\n    std::vector<isl_id *> dimensions;\n    std::vector<std::string> dimensions_str;\n\n    std::string map = \"{\";\n    map = map + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            std::string dim_str = generate_new_variable_name();\n            dimensions_str.push_back(dim_str);\n            map = map + dim_str;\n        }\n        else\n        {\n            std::string dim_str = generate_new_variable_name();\n            dimensions_str.push_back(dim_str);\n            map = map + dim_str;\n            if (i == dim0)\n                inDim0_str = dim_str;\n            else if (i == dim1)\n                inDim1_str = dim_str;\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n\n    map = map + \"] -> \" + this->get_name() + \"[\";\n\n    for (int i = 0; i < n_dims; i++)\n    {\n        if (i == 0)\n        {\n            map = map + dimensions_str[i];\n            dimensions.push_back(isl_id_alloc(\n                                     this->get_ctx(),\n                                     dimensions_str[i].c_str(),\n                                     NULL));\n        }\n        else if ((i != dim1) && (i!=dim0))\n        {\n            map = map + dimensions_str[i];\n            dimensions.push_back(isl_id_alloc(\n                                     this->get_ctx(),\n                                     dimensions_str[i].c_str(),\n                                     NULL));\n        }\n        else // i==dim1\n        {\n            if(i==dim1){\n                  map = map + outDim1_str;\n            isl_id *id0 = isl_id_alloc(this->get_ctx(),\n                                       outDim1_str.c_str(), NULL);\n            dimensions.push_back(id0);\n            }\n            else{// i== dim 0 \n                  map = map + outDim0_str;\n            isl_id *id0 = isl_id_alloc(this->get_ctx(),\n                                       outDim0_str.c_str(), NULL);\n            dimensions.push_back(id0);\n            }\n        }\n\n        if (i != n_dims - 1)\n        {\n            map = map + \",\";\n        }\n    }\n    // Computes gcd of f_i and f_j\n\n    int n1 = abs(f_i);\n    int n2 = abs(f_j);\n\n    while(n1 != n2)\n    {\n        if(n1 > n2)\n            n1 -= n2;\n        else\n            n2 -= n1;\n    }\n\n    // 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));\n\n   \n\n    // Update f_i and f_j to equivalent but prime between themselfs value\n    f_i = f_i / n1;\n    f_j = f_j / n1;\n  \n    int gamma = 0;\n    int sigma = 1;\n    bool found = false;\n\n    if ((f_j == 1) || (f_i == 1))\n    {\n        gamma = f_i - 1;\n        sigma = 1;\n        /* Since sigma = 1  then\n            f_i - gamma * f_j = 1 & using the previous condition :\n             - f_i = 1 : then gamma = 0 (f_i-1) is enough\n             - f_j = 1 : then gamma = f_i -1  */\n    }\n    else\n    { \n        if((f_j == - 1) && (f_i > 1))\n        {\n            gamma = 1;\n            sigma = 0;    \n        }    \n        else\n        {   //General case : solving the Linear Diophantine equation & finding basic solution (sigma & gamma) for : f_i* sigma - f_j*gamma = 1 \n            int i =0;\n            while((i < 100) && (!found))\n            {\n                if (((sigma * f_i ) % abs(f_j)) ==  1){\n                            found = true;\n                }\n                else{\n                    sigma ++;\n                    i++;\n                }\n            };\n\n            if(!found)\n            {\n                // Detect infinite loop and prevent it in case where f_i and f_j are not prime between themselfs\n                ERROR(\" Error in solving the Linear Diophantine equation f_i* sigma - f_j*gamma = 1  \", true);\n            }\n\n            gamma = ((sigma * f_i) - 1 ) / f_j;\n        }\n    }\n    \n    map = map + \"] : \" + dimensions_str[0] + \" = \" + std::to_string(duplicate_ID) + \" and \" +\n            outDim0_str + \" = (\" + inDim0_str + \"*\"+std::to_string(f_i)+\" + \"+inDim1_str+\"*\"+std::to_string(f_j)+\" ) and \"\n          +outDim1_str+\" = (\"+inDim0_str+\"*\"+std::to_string(gamma)+\" + \"+inDim1_str+\"*\"+std::to_string(sigma)+\" ) }\";\n\n    // polyfp::str_dump(\"Transformation angle map (string format) : \" + map);\n\n    isl_map *transformation_map = isl_map_read_from_str(this->get_ctx(), map.c_str());\n\n    for (int i = 0; i < dimensions.size(); i++)\n        transformation_map = isl_map_set_dim_id(\n                                 transformation_map, isl_dim_out, i, isl_id_copy(dimensions[i]));\n\n    transformation_map = isl_map_set_tuple_id(\n                             transformation_map, isl_dim_in,\n                             isl_map_get_tuple_id(isl_map_copy(schedule), isl_dim_out));\n    isl_id *id_range = isl_id_alloc(this->get_ctx(), this->get_name().c_str(), NULL);\n    transformation_map = isl_map_set_tuple_id(transformation_map, isl_dim_out, id_range);           \n    schedule = isl_map_apply_range(isl_map_copy(schedule), isl_map_copy(transformation_map));\n    // polyfp::str_dump(\"Schedule after transformation is :  \",\n    //                             isl_map_to_str(schedule));                            \n    this->set_schedule(schedule);\n}\n\n\nvoid polyfp::compute::after(compute &comp, polyfp::var level)\n{\n    assert(level.get_name().length() > 0);\n\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({level.get_name()});\n\n    assert(dimensions.size() == 1);\n\n    int current_level = dimensions[0];\n    auto leader_dim_map = comp.iterators_location_map;\n    this->after_level = current_level;\n    this->ori_after_level = current_level;\n    this->after(comp, dimensions[0]);\n}\n\nvoid polyfp::compute::after(compute *comp, polyfp::var level)\n{\n    assert(level.get_name().length() > 0);\n    std::vector<int> dimensions =\n        this->get_loop_level_numbers_from_dimension_names({level.get_name()});\n    assert(dimensions.size() == 1);\n    int current_level = dimensions[0];\n    int counter = 0;\n    auto leader_dim_map = comp->iterators_location_map;\n    this->after_level = current_level;\n    this->ori_after_level= current_level;\n\n    this->after(comp, dimensions[0]);\n\n}\n\nvoid polyfp::compute::after(compute &comp, int level)\n{\n    auto &graph = this->get_function()->sched_graph;\n    auto &edges = graph[&comp];\n    auto level_it = edges.find(this);\n    edges[this] = level;\n\n    this->get_function()->starting_computations.erase(this);\n    // todo\n    // this->get_function()->sched_graph_reversed[this][&comp] = level;\n    this->after_level = level;\n    // this->ori_after_level= level;\n    if(level != -1)\n    {\n        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();\n        while(iter2 != this->get_function()->leader_computations.end())\n        {\n            if(*iter2 == this)\n            {\n                iter2 = this->get_function()->leader_computations.erase(iter2);\n            }\n            else\n            {\n                iter2++;\n            }      \n        }\n        // this->get_function()->leader_computations.erase(this);\n        this->is_leader = false;\n        this->is_top_parent = false;\n        this->has_a_leader = true;\n        this->leader = &comp;\n\n        int component_level = comp.components.size();\n\n        if(component_level !=0)\n        {\n            std::map<polyfp::compute *, int>::reverse_iterator iter = comp.components.rbegin();\n            component_level = iter->second+1;\n        }\n\n        auto iter = comp.components.find (this) ;\n        if(iter != comp.components.end())\n            iter = comp.components.erase (iter);\n        comp.components.insert(std::pair(this,component_level));\n        comp.update_leader_components(this);\n        \n    }else if(level == -1)\n    {\n        this->is_leader = true;\n        this->has_a_leader = false;\n        this->is_top_parent = false;\n        this->leader = NULL; \n        comp.is_leaf = false;\n        auto iter = comp.components.find (this) ;\n        if(iter != comp.components.end()){\n            iter = comp.components.erase (iter);\n            comp.delete_leader_components(this);\n        }\n        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();\n        while(iter2 != this->get_function()->leader_computations.end())\n        {\n            if(*iter2 == this)\n            {\n                iter2 = this->get_function()->leader_computations.erase(iter2);\n            }\n            else\n            {\n                iter2++;\n            }      \n        }\n        this->get_function()->leader_computations.push_back(this);\n        //TODO: check if it is in lead_comp list\n        // int current_level = level;\n        // int counter = 0;\n        // auto dim_list = this->get_loop_level_names();\n        // auto leader_dim_map = comp.iterators_location_map;\n        // for(int i=0; i<dim_list.size(); i++){\n        //     auto fct = global::get_implicit_function();\n        //     auto next_level = fct->global_location;\n        //     this->iterators_location_map.insert(std::make_pair(dim_list[counter],next_level));\n        //     fct->global_location+=1;\n        //     counter+=1;\n        // }\n    }\n    \n    assert(this->get_function()->sched_graph_reversed[this].size() < 2 &&\n            \"Node has more than one predecessor.\");\n    // polyfp::str_dump(\"sched_graph[\" + comp.get_name() + \", \" +\n    //                              this->get_name() + \"] = \" + std::to_string(level));                             \n}\n\nvoid polyfp::compute::after(compute *comp, int level)\n{\n    // polyfp::str_dump(\"Scheduling \" + this->get_name() + \" to be executed after \" +\n    //                             comp.get_name() + \" at level \" + std::to_string(level));                            \n\n    auto &graph = this->get_function()->sched_graph;\n    auto &edges = graph[comp];\n    auto level_it = edges.find(this);\n    // if (level_it != edges.end())\n    // {\n    //     if (level_it->second > level)\n    //     {\n    //         level = level_it->second;\n    //     }\n    // }\n\n    edges[this] = level;\n    \n\n    this->get_function()->starting_computations.erase(this);\n    this->after_level = level;\n     if(level != -1)\n     {\n        std::vector<polyfp::compute *>::iterator iter2 = this->get_function()->leader_computations.begin();\n        while(iter2 != this->get_function()->leader_computations.end())\n        {\n            if(*iter2 == this)\n            {\n                iter2 = this->get_function()->leader_computations.erase(iter2);\n            }\n            else\n            {\n                iter2++;\n            }      \n        }\n        this->is_leader = false;\n        this->is_top_parent = false;\n        this->has_a_leader = true;\n        this->leader = comp;\n\n        int component_level = comp->components.size();\n        if(component_level !=0)\n        {\n            std::map<polyfp::compute *, int>::reverse_iterator iter = comp->components.rbegin();\n            component_level = iter->second+1;\n        }\n\n        auto iter = comp->components.find (this) ;\n        if(iter != comp->components.end())\n            iter = comp->components.erase (iter);\n        comp->components.insert(std::pair(this,component_level));\n        comp->update_leader_components(this);\n        \n    }else if(level == -1)\n    {\n        this->is_leader = true;\n        this->has_a_leader = false;\n        this->is_top_parent = false;\n        this->leader = NULL; \n        comp->is_leaf = false;\n        auto iter = comp->components.find (this) ;\n        if(iter != comp->components.end())\n        {\n            iter = comp->components.erase (iter);\n            comp->delete_leader_components(this);\n        }\n        this->get_function()->leader_computations.push_back(this);\n    }\n    assert(this->get_function()->sched_graph_reversed[this].size() < 2 &&\n            \"Node has more than one predecessor.\");\n}\n\nvoid polyfp::compute::update_leader_components(polyfp::compute *comp)\n{\n    if(this->has_a_leader)\n    {\n        int component_level = this->leader->components.size()+1;\n        this->leader->components.insert(std::pair(comp,component_level));\n        this->leader->update_leader_components(comp);\n    }\n}\n\nvoid polyfp::compute::delete_leader_components(polyfp::compute *comp)\n{\n    if(this->has_a_leader){\n        auto iter = this->leader->components.find (this) ;\n        if(iter != this->leader->components.end()){\n            iter = this->leader->components.erase (iter);\n        }\n        // this->leader->components.insert(std::pair(comp,component_level));\n        this->leader->update_leader_components(comp);\n    }\n}\n\nvoid polyfp::compute::dump_components()\n{\n    std::string result = \"\";\n    for (auto &edge: this->components)\n    {\n        result += edge.first->get_name() +\"[\" + std::to_string(edge.second )+ \"]=>\";\n\n    }\n    result += this->get_name();\n    // std::cout<<result<<std::endl;\n}\n\nvoid polyfp::compute::dump_loads_stores()\n{\n    std::string result = \"\";\n\n    for (auto &edge: this->map_loadstores)\n    {\n        result += std::to_string(edge.first )+\":[\" ;\n        for(auto &map: edge.second){\n            result+= map.first;\n            for(auto &vec: map.second){\n                result+= vec.get_name();\n            }\n        }\n        result+=+  \"]=>\";\n    }\n        \n    result += \"root\";\n    // std::cout<<result<<std::endl;\n}\n\nvoid compute::after_low_level(compute &comp, int level)\n{\n    // for loop level i return 2*i+1 which represents the\n    // the static dimension just after the dynamic dimension that\n    // represents the loop level i.\n    int dim = loop_level_into_static_dimension(level);\n\n    comp.get_function()->align_schedules();\n\n    assert(this->get_schedule() != NULL);\n    assert(dim < (signed int) isl_map_dim(this->get_schedule(), isl_dim_out));\n    assert(dim >= compute::root_dimension);\n\n    isl_map *new_sched = NULL;\n    for (int i = 1; i<=dim; i=i+2)\n    {\n        if (i < dim)\n        {\n            // Get the constant in comp, add +1 to it and set it to sched1\n            int order = isl_map_get_static_dim(comp.get_schedule(), i);\n            new_sched = isl_map_copy(this->get_schedule());\n            new_sched = add_eq_to_schedule_map(i, 0, -1, order, new_sched);\n        }\n        else // (i == dim)\n        {\n            // Get the constant in comp, add +1 to it and set it to sched1\n            int order = isl_map_get_static_dim(comp.get_schedule(), i);\n            new_sched = isl_map_copy(this->get_schedule());\n            new_sched = add_eq_to_schedule_map(i, 0, -1, order + 10, new_sched);\n        }\n        this->set_schedule(new_sched);\n    }\n    // polyfp::str_dump(\"Schedule adjusted: \",\n    //                             isl_map_to_str(this->get_schedule()));\n}\n\n\n\nvoid polyfp::compute::pipeline(polyfp::expr dim, int II)\n{\n    for(auto &kv: this->get_loop_level_names()){\n        if(dim.get_name() == kv){\n            int level = this->get_loop_level_number_from_dimension_name(kv);\n            this->directive_map.insert(std::pair(kv,\"pipeline\"));\n            std::string c_name = \"c\"+ std::to_string(level*2+1);\n            this->directive_tool_map.insert(std::pair(kv,c_name));\n            this->II = II;\n        }\n    }\n}\n\n\nvoid polyfp::compute::unroll(polyfp::expr dim, int factor)\n{\n    this->is_unrolled = true;\n    std::string name = dim.get_name();\n    auto it = std::find_if(this->unroll_dimension.begin(), this->unroll_dimension.end(),\n                            [&](const auto &d) { return d.get_name() == name; });\n    if (it == this->unroll_dimension.end()) \n    {\n        this->unroll_factor.push_back(factor);\n        this->unroll_dimension.push_back(dim);\n    }\n    // this->unroll_factor.push_back(factor);\n    // this->unroll_dimension.push_back(dim);\n}\n\nconst std::string polyfp::compute::get_unique_name() const\n{\n    std::stringstream namestream;\n    namestream << get_name();\n    namestream << \"@\";\n    namestream << (void *)this;\n    return namestream.str();\n}\n\nvoid polyfp::compute::gen_time_space_domain()\n{\n    assert(this->get_iteration_domain() != NULL);\n    assert(this->get_schedule() != NULL);\n\n    isl_set *iter = isl_set_copy(this->get_iteration_domain());\n    iter = this->intersect_set_with_context(iter);\n\n    time_processor_domain = isl_set_apply(\n                                iter,\n                                isl_map_copy(this->get_schedule()));\n    // polyfp::str_dump(\"Schedule:\", isl_map_to_str(this->get_schedule()));\n    // polyfp::str_dump(\"Generated time-space domain:\", isl_set_to_str(time_processor_domain));\n}\n\nisl_set *compute::intersect_set_with_context(isl_set *set)\n{\n    // Unify the space of the context and the \"missing\" set so that we can intersect them.\n    isl_set *context = isl_set_copy(this->get_function()->get_program_context());\n    if (context != NULL)\n    {\n        isl_space *model = isl_set_get_space(isl_set_copy(context));\n        set = isl_set_align_params(set, isl_space_copy(model));\n        // polyfp::str_dump(\"Context: \", isl_set_to_str(context));\n        // polyfp::str_dump(\"Set after aligning its parameters with the context parameters: \",\n        //                              isl_set_to_str (set));\n\n        isl_id *missing_id1 = NULL;\n        if (isl_set_has_tuple_id(set) == isl_bool_true)\n        {\n            missing_id1 = isl_set_get_tuple_id(set);\n        }\n        else\n        {\n            std::string name = isl_set_get_tuple_name(set);\n            assert(name.size() > 0);\n            missing_id1 = isl_id_alloc(this->get_ctx(), name.c_str(), NULL);\n        }\n\n        int nb_dims = isl_set_dim(set, isl_dim_set);\n        context = isl_set_add_dims(context, isl_dim_set, nb_dims);\n        // isl_set_to_str (context);\n        context = isl_set_set_tuple_id(context, isl_id_copy(missing_id1));\n        // isl_set_to_str (context);\n        set = isl_set_intersect(set, isl_set_copy(context));\n        // isl_set_to_str (set);\n    }\n    return set;\n}\n\n}\n"
  },
  {
    "path": "lib/polyhedral/core.cpp",
    "content": "#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/constraint.h>\n#include <isl/union_map.h>\n#include <isl/union_set.h>\n#include <isl/ast_build.h>\n\n#include \"core.h\"\n\n#ifdef _WIN32\n#include <iso646.h>\n#endif\n\nnamespace polyfp\n{\n// Used for the generation of new variable names.\nint id_counter = 0;\nstatic int next_dim_name = 0;\n\nprimitive_t global::loop_iterator_type = p_int32;\nfunction *global::implicit_fct;\nstd::unordered_map<std::string, var> var::declared_vars;\n\nstd::string generate_new_variable_name();\n\npolyfp::expr traverse_expr_and_replace_non_affine_accesses(polyfp::compute *comp,\n                                                             const polyfp::expr &exp);\n\nvoid init(std::string fct_name)\n{\n    function *fct = new function(fct_name);\n    global::set_implicit_function(fct);\n    init();\n}\n\n\nvoid init()\n{\n    global::set_default_polyfp_options();\n}\n\nvoid codegen()\n{\n    function *fct = global::get_implicit_function();\n    fct->codegen();\n}\n\n\n\n/**\n * Derived from Tiramisu:\n * Transform the loop level into its corresponding dynamic schedule\n * dimension.\n *\n * In the example below, the dynamic dimension that corresponds\n * to the loop level 0 is 2, and to 1 it is 4, ...\n *\n * The first dimension is the duplication dimension, the following\n * dimensions are static, dynamic, static, dynamic, ...\n *\n * Loop level               :    -1         0      1      2\n * Schedule dimension number:        0, 1   2  3   4  5   6  7\n * Schedule:                        [0, 0, i1, 0, i2, 0, i3, 0]\n */\nint loop_level_into_dynamic_dimension(int level)\n{\n    return 1 + (level * 2 + 1);\n}\n\n/**\n * Derived from Tiramisu:\n * Transform the loop level into the first static schedule\n * dimension after its corresponding dynamic dimension.\n *\n * In the example below, the first static dimension that comes\n * after the corresponding dynamic dimension for\n * the loop level 0 is 3, and to 1 it is 5, ...\n *\n * Loop level               :    -1         0      1      2\n * Schedule dimension number:        0, 1   2  3   4  5   6  7\n * Schedule:                        [0, 0, i1, 0, i2, 0, i3, 0]\n */\nint loop_level_into_static_dimension(int level)\n{\n    return loop_level_into_dynamic_dimension(level) + 1;\n}\n\n/**\n * Derived from Tiramisu:\n * Transform a dynamic schedule dimension into the corresponding loop level.\n *\n * In the example below, the loop level that corresponds\n * to the dynamic dimension 2 is 0, and to the dynamic dimension 4 is 1, ...\n *\n * The first dimension is the duplication dimension, the following\n * dimensions are static, dynamic, static, dynamic, ...\n *\n * Loop level               :    -1         0      1      2\n * Schedule dimension number:        0, 1   2  3   4  5   6  7\n * Schedule:                        [0, 0, i1, 0, i2, 0, i3, 0]\n */\nint dynamic_dimension_into_loop_level(int dim)\n{\n    assert(dim % 2 == 0);\n    int level = (dim - 2)/2;\n    return level;\n}\n\nstd::string generate_new_variable_name()\n{\n    return \"t\" + std::to_string(id_counter++);\n}\n\nstd::string generate_new_computation_name()\n{\n    return \"C\" + std::to_string(id_counter++);\n}\n\nstd::string str_from_polyfp_type_expr(polyfp::expr_t type)\n{\n    switch (type)\n    {\n    case polyfp::e_val:\n        return \"val\";\n    case polyfp::e_op:\n        return \"op\";\n    case polyfp::e_var:\n        return \"var\";\n    default:\n        ERROR(\"polyfp type not supported.\", true);\n        return \"\";\n    }\n}\n\n\nstd::string str_from_polyfp_type_primitive(polyfp::primitive_t type)\n{\n    switch (type)\n    {\n    case polyfp::p_uint8:\n        return \"uint8\";\n    case polyfp::p_int8:\n        return \"int8\";\n    case polyfp::p_uint16:\n        return \"uint16\";\n    case polyfp::p_int16:\n        return \"int16\";\n    case polyfp::p_uint32:\n        return \"uint32\";\n    case polyfp::p_int32:\n        return \"int32\";\n    case polyfp::p_uint64:\n        return \"uint64\";\n    case polyfp::p_int64:\n        return \"int64\";\n    case polyfp::p_float32:\n        return \"float32\";\n    case polyfp::p_float64:\n        return \"float64\";\n    default:\n        ERROR(\"polyfp type not supported.\", true);\n        return \"\";\n    }\n}\nstd::string str_polyfp_type_op(polyfp::op_t type)\n{\n    switch (type)\n    {\n    case polyfp::o_max:\n        return \"max\";\n    case polyfp::o_min:\n        return \"min\";\n    case polyfp::o_add:\n        return \"add\";\n    case polyfp::o_sub:\n        return \"sub\";\n    case polyfp::o_mul:\n        return \"mul\";\n    case polyfp::o_div:\n        return \"div\";\n    case polyfp::o_mod:\n    case polyfp::o_access:\n        return \"access\";\n    default:\n        // ERROR(\"polyfp op not supported.\", true);\n        return \"\";\n    }\n}\n\n\nisl_map *add_eq_to_schedule_map(int dim0, int in_dim_coefficient, int out_dim_coefficient,\n                                int const_conefficient, isl_map *sched)\n{\n    // isl_map_to_str(sched);\n    // std::to_string(const_conefficient);\n    isl_map *identity = isl_set_identity(isl_map_range(isl_map_copy(sched)));\n    identity = isl_map_universe(isl_map_get_space(identity));\n    isl_space *sp = isl_map_get_space(identity);\n    isl_local_space *lsp = isl_local_space_from_space(isl_space_copy(sp));\n\n    // Create a transformation map that transforms the schedule.\n    for (int i = 0; i < isl_map_dim (identity, isl_dim_out); i++)\n        if (i == dim0)\n        {\n            isl_constraint *cst = isl_constraint_alloc_equality(isl_local_space_copy(lsp));\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_in, dim0, in_dim_coefficient);\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, dim0, -out_dim_coefficient);\n            // TODO: this should be inverted into const_conefficient.\n            cst = isl_constraint_set_constant_si(cst, -const_conefficient);\n            identity = isl_map_add_constraint(identity, cst);\n            // isl_map_to_str(identity);\n        }\n        else\n        {\n            // Set equality constraints for dimensions\n            isl_constraint *cst2 = isl_constraint_alloc_equality(isl_local_space_copy(lsp));\n            cst2 = isl_constraint_set_coefficient_si(cst2, isl_dim_in, i, 1);\n            cst2 = isl_constraint_set_coefficient_si(cst2, isl_dim_out, i, -1);\n            identity = isl_map_add_constraint(identity, cst2);\n        }\n\n    isl_map *final_identity = identity;\n    // isl_map_to_str(final_identity);\n    sched = isl_map_apply_range (sched, final_identity);\n    // isl_map_to_str(sched);\n\n    return sched;\n}\n\n\n}\n\n"
  },
  {
    "path": "lib/polyhedral/debug.cpp",
    "content": "#include <iostream>\n#include <sstream>\n\nnamespace polyfp\n{\n\nint polyfp_indentation = 0;\n\nvoid str_dump(const std::string &str)\n{\n    std::cout << str;\n}\n\nvoid str_dump(const std::string &str, const char *str2)\n{\n    std::cout << str << \" \" << str2;\n}\n\nvoid str_dump(const char *str, const char *str2)\n{\n    std::cout << str << \" \" << str2<<std::endl;\n}\n\nvoid print_indentation()\n{\n    for (int polyfp_indent = 0; polyfp_indent < polyfp::polyfp_indentation; polyfp_indent++)\n    {\n        if (polyfp_indent % 4 == 0)\n        {\n            str_dump(\"|\");\n        }\n        else\n        {\n            str_dump(\" \");\n        }\n    }\n}\n\n}\n"
  },
  {
    "path": "lib/polyhedral/expr.cpp",
    "content": "#include \"expr.h\"\n#include \"function.h\";\n// #include <polyfp/core.h>\n// #include \"function.h\"\nnamespace polyfp\n{\n\npolyfp::expr& polyfp::expr::operator=(polyfp::expr const & e)\n{\n    this->_operator = e._operator;\n    this->op = e.op;\n    this->access_vector = e.access_vector;\n    this->defined = e.defined;\n    this->name = e.name;\n    this->dtype = e.dtype;\n    this->etype = e.etype;\n\n    // Copy the integer value\n    if (e.get_expr_type() == polyfp::e_val)\n    {\n        if (e.get_data_type() == polyfp::p_uint8)\n        {\n            this->uint8_value = e.get_uint8_value();\n        }\n        else if (e.get_data_type() == polyfp::p_int8)\n        {\n            this->int8_value = e.get_int8_value();\n        }\n        else if (e.get_data_type() == polyfp::p_uint16)\n        {\n            this->uint16_value = e.get_uint16_value();\n        }\n        else if (e.get_data_type() == polyfp::p_int16)\n        {\n            this->int16_value = e.get_int16_value();\n        }\n        else if (e.get_data_type() == polyfp::p_uint32)\n        {\n            this->uint32_value = e.get_uint32_value();\n        }\n        else if (e.get_data_type() == polyfp::p_int32)\n        {\n            this->int32_value = e.get_int32_value();\n        }\n        else if (e.get_data_type() == polyfp::p_uint64)\n        {\n            this->uint64_value = e.get_uint64_value();\n        }\n        else if (e.get_data_type() == polyfp::p_int64)\n        {\n            this->int64_value = e.get_int64_value();\n        }\n        else if (e.get_data_type() == polyfp::p_float32)\n        {\n            this->float32_value = e.get_float32_value();\n        }\n        else if (e.get_data_type() == polyfp::p_float64)\n        {\n            this->float64_value = e.get_float64_value();\n        }\n    }\n    return *this;\n}\n// todo\n// polyfp::expr polyfp::expr::substitute(std::vector<std::pair<var, expr>> substitutions) const\n// {\n//     for (auto &substitution: substitutions)\n//         if (this->is_equal(substitution.first))\n//             return substitution.second;\n\n\n//     return apply_to_operands([&substitutions](const expr& e){\n//         return e.substitute(substitutions);\n//     });\n// }\n\n// polyfp::expr polyfp::expr::substitute_access(std::string original, std::string substitute) const {\n//     expr && result = this->apply_to_operands([&original, &substitute](const expr& e){\n//         return e.substitute_access(original, substitute);\n//     });\n//     if (result.get_op_type() == o_access && result.name == original)\n//     {\n//         result.name = substitute;\n//     }\n//     return result;\n// }\n\npolyfp::var::var(std::string name)\n\n\n{\n    assert(!name.empty());\n\n    auto declared = var::declared_vars.find(name);\n\n\n    if (declared != var::declared_vars.end())\n    {\n        *this = declared->second;\n    }\n    else\n    {\n        this->name = name;\n        this->etype = polyfp::e_var;\n        this->dtype = global::get_loop_iterator_data_type();\n        // this->defined = true;\n        if (true)\n        {\n            var::declared_vars.insert(std::make_pair(name, *this));\n           \n        }\n    }\n}\n\npolyfp::var::var(std::string name, polyfp::primitive_t type)\n{\n    assert(!name.empty());\n\n    auto declared = var::declared_vars.find(name);\n\n    if (declared != var::declared_vars.end())\n    {\n        assert(declared->second.dtype == type);\n        *this = declared->second;\n    }\n    else\n    {\n        this->name = name;\n        this->etype = polyfp::e_var;\n        this->dtype = type;\n        // this->defined = true;\n        if (true)\n        {\n            var::declared_vars.insert(std::make_pair(name, *this));\n            \n        }\n    }\n}\n\n\npolyfp::constant::constant(float value, polyfp::primitive_t t, polyfp::function *fct):\n                         float_value(value), func(fct), datatype(t){\n\n        this->name = global::generate_new_constant_name();\n        this->etype = polyfp::e_var;\n        this->dtype = t;\n        // fct->add_invariant(*this);\n        fct->add_invariant(std::pair<std::string, polyfp::constant *>(name, this));\n\n}\n\npolyfp::primitive_t constant::get_type() const\n{\n    return dtype;\n}\n\n\npolyfp::p_max::p_max(polyfp::expr value1, polyfp::expr value2, polyfp::op_t o, polyfp::function *fct){\n\n        this->left_value = value1;\n        this->right_value = value2;\n        this->func = fct;\n        this->_operator = o;\n        this->etype = polyfp::e_op;\n        this->op.push_back(value1);\n        this->op.push_back(value2);\n        // fct->add_invariant(*this);\n        // fct->add_invariant(std::pair<std::string, polyfp::constant *>(name, this));\n\n}\n\npolyfp::expr polyfp::expr::copy() const\n{\n    return (*this);\n}\n\nexpr polyfp::expr::operator+(polyfp::expr other) const {\n    return polyfp::expr{o_add, *this, other};\n}\n\nexpr polyfp::expr::operator-(polyfp::expr other) const {\n    return polyfp::expr{o_sub, *this, other};\n}\n\nexpr polyfp::expr::operator*(polyfp::expr other) const {\n    return polyfp::expr{o_mul, *this, other};\n}\n\nexpr polyfp::expr::operator/(polyfp::expr other) const {\n    return polyfp::expr{o_div, *this, other};\n}\n\nexpr polyfp::expr::operator%(polyfp::expr other) const {\n    return polyfp::expr{o_mod, *this, other};\n}\n\n// todo\n// expr memcpy(const buffer &from, const buffer &to) {\n//     return expr(o_memcpy, var(p_void_ptr, from.get_name()), var(p_void_ptr, to.get_name()));\n// }\n\n// expr allocate(const buffer &b)\n// {\n//     return expr{o_allocate, b.get_name()};\n// }\n\n\n}\n"
  },
  {
    "path": "lib/polyhedral/function.cpp",
    "content": "\n#include \"function.h\"\n#include \"generator.h\"\n#include <iostream>\n#include <fstream>\n#include <filesystem>\n\n\nnamespace polyfp{\n\n\nisl_map *isl_map_align_range_dims(isl_map *map, int max_dim)\n{\n    assert(map != NULL);\n    int mdim = isl_map_dim(map, isl_dim_out);\n    assert(max_dim >= mdim);\n    // polyfp::str_dump(\"Input map:\", isl_map_to_str(map));\n\n    const char *original_range_name = isl_map_get_tuple_name(map, isl_dim_out);\n    map = isl_map_add_dims(map, isl_dim_out, max_dim - mdim);\n\n    for (int i = mdim; i < max_dim; i++)\n    {\n        isl_space *sp = isl_map_get_space(map);\n        isl_local_space *lsp = isl_local_space_from_space(sp);\n        isl_constraint *cst = isl_constraint_alloc_equality(lsp);\n        cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, i, 1);\n        map = isl_map_add_constraint(map, cst);\n    }\n\n    map = isl_map_set_tuple_name(map, isl_dim_out, original_range_name);\n\n    // polyfp::str_dump(\"After alignment, map = \",isl_map_to_str(map));\n\n    return map;\n}\n\nfunction::function(std::string name)\n{\n    this->name = name;\n    this->ast = NULL;\n    this->context_set = NULL;\n    this->ctx = isl_ctx_alloc();\n    this->global_location = 0;\n};\n\nisl_ctx *function::get_isl_ctx() const\n{\n    return ctx;\n}\n\nconst std::vector<compute *> &function ::get_computations() const\n{\n    return body;\n}\n\nconst std::vector<compute *> &function ::get_body() const\n{\n    return body;\n}\n\nvoid polyfp::function::add_invariant(std::pair <std::string, polyfp::constant *> invar)\n{\n    this->constant_list.insert(invar);\n}\n\nconst std::map<std::string, polyfp::constant *> &function::get_invariants() const\n{\n    return constant_list;\n}\n\nvoid polyfp::function::set_partition(std::string name, std::vector<int> factors, std::vector<std::string> types)\n{\n    // std::vector<std::string> types;\n    // for (int dim = 0; dim < factors.size(); ++dim) {\n    //     types.push_back(type);\n    // }\n    std::tuple<std::string, std::vector<int>, std::vector<std::string>> dims(name,factors,types);\n    this->partition_map.push_back(dims);\n\n\n}\n\nstd::vector<std::tuple<std::string, std::vector<int>, std::vector<std::string>>> polyfp::function::get_partition_map()\n{\n    return this->partition_map;\n}\n\nvoid polyfp::function::add_computation(compute *cpt)\n{\n    assert(cpt != NULL);\n    this->body.push_back(cpt);\n    this->starting_computations.insert(cpt);\n}\n\nvoid polyfp::function::dump(bool s) const\n{\n    if (s)\n    {\n        std::cout << \"\\n\\nFunction \\\"\" << this->name << \"\\\"\" << std::endl << std::endl;\n\n        if (this->function_arguments.size() > 0)\n        {\n            std::cout << \"Function arguments (polyfp buffers):\" << std::endl;\n            for (const auto &buf : this->function_arguments)\n            {\n                // buf->dump(s);\n            }\n            std::cout << std::endl;\n        }\n        // todo\n        if (this->invariants.size() > 0)\n        {\n            std::cout << \"Function invariants:\" << std::endl;\n            for (const auto &inv : this->invariants)\n            {\n                //todo\n                // inv.dump();\n            }\n            std::cout << std::endl;\n        }\n\n        if (this->get_program_context() != NULL)\n        {\n            std::cout << \"Function context set: \"\n                      << isl_set_to_str(this->get_program_context())\n                      << std::endl;\n        }\n\n        std::cout << \"Body \" << std::endl;\n        for (const auto &cpt : this->body)\n        {\n            cpt->dump();\n        }\n        std::cout << std::endl;\n\n        for (const auto &buf : this->placeholders_list)\n        {\n            std::cout << \"Placeholder name: \" << buf.second->get_name() << std::endl;\n            buf.second->dump(false);\n        }\n\n        std::cout << std::endl << std::endl;\n    }\n}\n\nint polyfp::function::get_max_identity_schedules_range_dim() const\n{\n    int max_dim = 0;\n    for (const auto &comp : this->get_computations())\n    {\n        isl_map *sched = comp->gen_identity_schedule_for_time_space_domain();\n        int m = isl_map_dim(sched, isl_dim_out);\n        max_dim = std::max(max_dim, m);\n    }\n    return max_dim;\n}\n\nconst std::vector<std::string> &function::get_iterator_names() const\n{\n    return iterator_names;\n}\n\nisl_ast_node *function::get_isl_ast() const\n{\n    assert((ast != NULL) && (\"You should generate an isl ast first (gen_isl_ast()).\"));\n\n    return ast;\n}\n\nisl_ast_node *function::get_isl_ast1() const\n{\n    assert((ast != NULL) && (\"You should generate an isl ast first (gen_isl_ast()).\"));\n\n    return ast;\n}\n\nisl_union_set *polyfp::function::get_iteration_domain() const\n{\n    isl_union_set *result = NULL;\n    isl_space *space = NULL;\n\n    if (!this->body.empty())\n    {\n        space = isl_set_get_space(this->body[0]->get_iteration_domain());\n    }\n    else\n    {\n        return NULL;\n    }\n\n    assert(space != NULL);\n    result = isl_union_set_empty(space);\n\n    for (const auto &cpt : this->body)\n    {\n        isl_set *cpt_iter_space = isl_set_copy(cpt->get_iteration_domain());\n        result = isl_union_set_union(isl_union_set_from_set(cpt_iter_space), result);\n    }\n\n    return result;\n}\n\nisl_union_map *polyfp::function::get_aligned_identity_schedules() const\n{\n    isl_union_map *result;\n    isl_space *space;\n\n    if (this->body.empty() == false)\n    {\n        space = isl_map_get_space(this->body[0]->gen_identity_schedule_for_time_space_domain());\n    }\n    else\n    {\n        return NULL;\n    }\n    assert(space != NULL);\n    result = isl_union_map_empty(space);\n\n    int max_dim = this->get_max_identity_schedules_range_dim();\n    for (const auto &comp : this->get_computations())\n    {\n        isl_map *sched = comp->gen_identity_schedule_for_time_space_domain();\n        // polyfp::str_dump(\"Identity schedule for time space domain: \", isl_map_to_str(sched));\n        assert((sched != NULL) && \"Identity schedule could not be computed\");\n        sched = isl_map_align_range_dims(sched, max_dim);\n        result = isl_union_map_union(result, isl_union_map_from_map(sched));\n    }\n\n    return result;\n}\n\nvoid function::dump_sched_graph_dfs(compute * comp,\n                                    std::unordered_set<compute *> &visited)\n{\n    // Do not visit anything that was already returned\n    if (visited.find(comp) != visited.end())\n        return;\n\n    visited.insert(comp);\n\n    for (auto &edge: this->sched_graph[comp])\n    {\n        const std::string level = ((edge.second == compute::root_dimension) ?\n                                   \"root\" :\n                                   std::to_string(edge.second));\n        polyfp::str_dump(comp->get_name() +\n                                    \"=[\" + level + \"]=>\" +\n                                    edge.first->get_name());\n        std::cout<<\" \";\n        \n        dump_sched_graph_dfs(edge.first, visited);\n    }\n}\n\nbool function::is_sched_graph_tree_dfs(compute * comp,\n                                       std::unordered_set<compute *> &visited)\n{\n    // Do not visit anything that was already returned\n    if (visited.find(comp) != visited.end())\n        return false;\n\n    visited.insert(comp);\n\n    for (auto &edge: this->sched_graph[comp])\n    {\n        if (!is_sched_graph_tree_dfs(edge.first, visited))\n            return false;\n    }\n\n    return true;\n}\n\nbool function::is_sched_graph_tree()\n{\n    if (this->starting_computations.size() != 1)\n    {\n        return false;\n    }\n\n    // Contains all nodes that have been visited\n    std::unordered_set<compute *> visited;\n\n    for (auto &comp: this->starting_computations)\n    {\n        if (!is_sched_graph_tree_dfs(comp, visited))\n        {\n            return false;\n        }\n    }\n    return true;\n}\n\nvoid function::dump_sched_graph()\n{\n    // polyfp::str_dump(\"Number of schedule graph roots is \" +\n    //                             std::to_string(this->starting_computations.size()));\n\n    polyfp::str_dump(\"Number of schedule graph roots is \" +\n                                std::to_string(this->starting_computations.size()));\n    std::cout<<std::endl;\n                                \n    polyfp::str_dump(\"The roots are:\");\n    std::cout<<std::endl;\n\n    for (auto root: this->starting_computations){\n        polyfp::str_dump(\" * \" + root->get_name());\n        std::cout<<std::endl;\n    }\n    // Contains all nodes that have been visited\n    std::unordered_set<compute *> visited;\n    polyfp::str_dump(\"Displaying schedule graph\");\n    std::cout<<std::endl;\n\n    for (auto &comp: this->starting_computations)\n    {\n        dump_sched_graph_dfs(comp, visited);\n    }\n    std::cout<<std::endl;\n    polyfp::str_dump(\"Finished displaying schedule graph\");\n}\n\nvoid function::gen_ordering_schedules()\n{\n\n    if(this->is_sched_graph_tree())\n    {\n        // polyfp::str_dump(\"this->is_sched_graph_tree(): true.\");\n        std::priority_queue<int> level_to_check;\n        std::unordered_map<int, std::deque<compute *>> level_queue;\n        auto current_comp = *(this->starting_computations.begin());\n\n        bool comps_remain = true;\n        while(comps_remain)\n        {\n            for (auto &edge: this->sched_graph[current_comp])\n            {\n                if (level_queue[edge.second].size() == 0)\n                    level_to_check.push(edge.second);\n\n                level_queue[edge.second].push_back(edge.first);\n            }\n\n            comps_remain = level_to_check.size() > 0;\n            if (comps_remain)\n            {\n                int fuse_level = level_to_check.top();\n                auto next_comp = level_queue[fuse_level].front();\n                level_queue[fuse_level].pop_front();\n                next_comp->after_low_level((*current_comp), fuse_level);\n                current_comp = next_comp;\n                if (level_queue[fuse_level].size() == 0)\n                    level_to_check.pop();\n            }\n        }\n    }\n    else\n    {\n        polyfp::str_dump(\"this->is_sched_graph_tree(): false.\");\n    }\n}\n\nint polyfp::function::get_max_schedules_range_dim() const\n{\n    int max_dim = 0;\n    for (const auto &comp : this->get_computations())\n    {\n        isl_map *sched = comp->get_schedule();\n        int m = isl_map_dim(sched, isl_dim_out);\n        max_dim = std::max(max_dim, m);\n    }\n    return max_dim;\n}\n\nisl_set *function::get_program_context() const\n{\n    if (context_set != NULL)\n    {\n        return isl_set_copy(context_set);\n    }\n    else\n    {\n        return NULL;\n    }\n}\n\nvoid polyfp::function::align_schedules()\n{\n    int max_dim = this->get_max_schedules_range_dim();\n\n    for (auto &comp : this->get_computations())\n    {\n        isl_map *dup_sched = comp->get_schedule();\n        assert((dup_sched != NULL) && \"Schedules should be set before calling align_schedules\");\n        dup_sched = isl_map_align_range_dims(dup_sched, max_dim);\n        comp->set_schedule(dup_sched);\n        // polyfp::str_dump(\"Generated time-space domain:\", isl_map_to_str(dup_sched));\n        comp->name_unnamed_time_space_dimensions();\n    }\n}\nstd::string function::get_name(){\n    return this->name;\n}\nvoid function::gen_time_space_domain()\n{\n    this->gen_ordering_schedules();\n\n    this->align_schedules();\n\n    for (auto &comp : this->get_computations())\n    {\n        comp->gen_time_space_domain();\n    }\n}\n\nvoid function::gen_loop_location()\n{\n    auto leader_list = this->leader_computations;\n    // std::cout<<leader_list.size()<<std::endl;\n    for (auto &a_leader : leader_list)\n    {   \n        // std::cout << \"leader name: \";\n        // std::cout << a_leader->get_name() <<'\\n';\n        if(a_leader->is_leader == true)\n        {\n            if(a_leader->after_level!= -2)\n            {\n                int level = a_leader->after_level;\n                int current_level = level;\n                int counter = 0;\n                // auto dim_list = a_leader->get_loop_level_names();\n                auto dim_list = a_leader->final_loop_level_names;\n                for(int i=0; i<dim_list.size(); i++)\n                {\n                    auto next_level = this->global_location;\n                    a_leader->iterators_location_map.insert(std::make_pair(dim_list[counter],next_level));\n                    this->global_location+=1;\n                    counter+=1;\n                }\n            }\n            else\n            {\n                auto nms = a_leader->final_loop_level_names;\n                for (int i = 0; i< nms.size(); i++)\n                {\n                    a_leader->iterators_location_map.insert(std::make_pair(nms[i],i));\n                    this->global_location = nms.size();     \n                }\n            }\n            \n        }\n        // std::cout <<a_leader->iterators_location_map.size()<<'\\n';\n        // for(auto &map: a_leader->iterators_location_map){\n        //     std::cout<<map.first<<\": \"<<map.second<<std::endl;\n        // }\n            \n        auto components = a_leader->components;\n        // sort components by their value\n        std::vector<std::pair<polyfp::compute*, int>> temp;\n        for (auto it = components.begin(); it != components.end(); it++)\n            temp.push_back(std::make_pair(it->first, it->second));\n\n        std::sort(temp.begin(), temp.end(), [](const std::pair<polyfp::compute*, int> &x, const std::pair<polyfp::compute*, int> &y) -> int {\n            return x.second < y.second;\n        });\n\n        for (auto it = temp.begin(); it != temp.end(); it++)\n        {\n            // std::cout << it->first->get_name() << ':' << it->second << '\\n';\n            // std::cout <<it->first->after_level <<'\\n';\n            auto comp = it->first;\n            int level = comp->after_level;\n            int current_level = level;\n            int counter = 0;\n            auto dim_list = comp->final_loop_level_names;\n            auto leader_dim_map = comp->leader->iterators_location_map;\n            if(level!=-1)\n            {   \n                for(int i=0; i<dim_list.size(); i++)\n                {\n                    if(counter <= current_level)\n                    {\n                        comp->iterators_location_map.insert(std::make_pair(dim_list[counter],leader_dim_map[dim_list[counter]]));\n                    }else\n                    {\n                        // auto fct = global::get_implicit_function();\n                        auto next_level = this->global_location;\n                        comp->iterators_location_map.insert(std::make_pair(dim_list[counter],next_level));\n                        this->global_location += 1;\n                    }\n                    counter+=1;\n                }\n            }else{\n                // TODO\n            }\n        }\n        \n    }\n\n}\n\nisl_union_map *polyfp::function::get_schedule() const\n{\n    isl_union_map *result = NULL;\n    isl_space *space = NULL;\n\n    if (!this->body.empty())\n    {\n        space = isl_map_get_space(this->body[0]->get_schedule());\n    }\n    else\n    {\n        return NULL;\n    }\n\n    assert(space != NULL);\n    result = isl_union_map_empty(isl_space_copy(space));\n\n    for (const auto &cpt : this->body)\n    {\n        isl_map *m = isl_map_copy(cpt->get_schedule());\n        result = isl_union_map_union(isl_union_map_from_map(m), result);\n    }\n\n    result = isl_union_map_intersect_domain(result, this->get_iteration_domain());\n\n    return result;\n}\n\nisl_union_set *polyfp::function::get_trimmed_time_processor_domain() const\n{\n    isl_union_set *result = NULL;\n    isl_space *space = NULL;\n    if (!this->body.empty())\n    {\n        space = isl_set_get_space(this->body[0]->get_trimmed_time_processor_domain());\n    }\n    else\n    {\n        return NULL;\n    }\n    assert(space != NULL);\n\n    result = isl_union_set_empty(space);\n\n    for (const auto &cpt : this->body)\n    {\n        isl_set *cpt_iter_space = isl_set_copy(cpt->get_trimmed_time_processor_domain());\n        result = isl_union_set_union(isl_union_set_from_set(cpt_iter_space), result);\n    }\n    return result;\n}\n\nconst std::map<std::string, polyfp::placeholder *> &function::get_placeholders() const\n{\n    return placeholders_list;\n}\n\nconst std::map<std::string, polyfp::placeholder *> &function::get_fct_arguments() const\n{\n    return fct_argument_list;\n}\n\nconst std::map<std::string, polyfp::placeholder *> &function::get_global_arguments() const\n{\n    return global_argument_list;\n}\n\nvoid function::add_placeholder(std::pair <std::string, polyfp::placeholder *> buf)\n{\n    assert(!buf.first.empty() && (\"Empty buffer name.\"));\n    assert((buf.second != NULL) && (\"Empty buffer.\"));\n\n    this->placeholders_list.insert(buf);  \n}\n\nvoid function::add_fct_argument(std::pair <std::string, polyfp::placeholder *> buf)\n{\n    assert(!buf.first.empty() && (\"Empty buffer name.\"));\n    assert((buf.second != NULL) && (\"Empty buffer.\"));\n    this->fct_argument_list.insert(buf);\n}\n\nvoid function::add_global_argument(std::pair <std::string, polyfp::placeholder *> buf)\n{\n    assert(!buf.first.empty() && (\"Empty buffer name.\"));\n    assert((buf.second != NULL) && (\"Empty buffer.\"));\n    this->global_argument_list.insert(buf);\n}\n\nvoid function::add_fct_argument()\n{\n    this->fct_argument_added = true;\n}\n\n\nisl_union_map *polyfp::function::compute_dep_graph() \n{\n    isl_union_map *result = NULL;\n\n    for (const auto &consumer : this->get_computations()) {\n\n        isl_union_map *accesses_union_map = NULL;\n        std::vector < isl_map * > accesses_vector;\n        generator::get_rhs_accesses(this, consumer, accesses_vector, false);\n\n        if (!accesses_vector.empty()) \n        {\n            if (accesses_union_map == NULL) {\n                isl_space *space = isl_map_get_space(accesses_vector[0]);\n                assert(space != NULL);\n                accesses_union_map = isl_union_map_empty(space);\n            }\n            for (size_t i = 0; i < accesses_vector.size(); ++i) {\n                isl_map *reverse_access = isl_map_reverse(accesses_vector[i]);\n                accesses_union_map = isl_union_map_union(isl_union_map_from_map(reverse_access),\n                                                         accesses_union_map);\n            }\n\n            //accesses_union_map = isl_union_map_intersect_range(accesses_union_map, isl_union_set_from_set(isl_set_copy(consumer->get_iteration_domain())));\n            //accesses_union_map = isl_union_map_intersect_domain(accesses_union_map, isl_union_set_from_set(isl_set_copy(consumer->get_iteration_domain())));\n\n            polyfp::str_dump(\"Accesses after filtering.\");\n            polyfp::str_dump(isl_union_map_to_str(accesses_union_map));\n\n            if (result == NULL) \n            {\n                result = isl_union_map_copy(accesses_union_map);\n                isl_union_map_free(accesses_union_map);\n            } else {\n                result = isl_union_map_union(result, accesses_union_map);\n            }\n        }\n    }\n\n    if (result != NULL)\n    {\n        polyfp::str_dump(isl_union_map_to_str(result));\n    }\n    else\n    {\n        polyfp::str_dump(\"Null.\");\n    }\n    return result;\n}\n\nvoid function::gen_isl_ast()\n{\n    // Check that time_processor representation has already been computed,\n    assert(this->get_trimmed_time_processor_domain() != NULL);\n    assert(this->get_aligned_identity_schedules() != NULL);\n\n    isl_ctx *ctx = this->get_isl_ctx();\n    assert(ctx != NULL);\n    isl_ast_build *ast_build;\n\n    if (this->get_program_context() == NULL)\n    {\n        ast_build = isl_ast_build_alloc(ctx);\n    }\n    else\n    {\n        ast_build = isl_ast_build_from_context(isl_set_copy(this->get_program_context()));\n    }\n\n    isl_options_set_ast_build_atomic_upper_bound(ctx, 1);\n    isl_options_get_ast_build_exploit_nested_bounds(ctx);\n    isl_options_set_ast_build_group_coscheduled(ctx, 1);\n\n    ast_build = isl_ast_build_set_after_each_for(ast_build, &polyfp::for_code_generator_after_for,\n                NULL);\n    // ast_build = isl_ast_build_set_at_each_domain(ast_build, &polyfp::generator::stmt_code_generator,\n    //             this);\n\n    isl_id_list *iterators = isl_id_list_alloc(ctx, this->get_iterator_names().size());\n    if (this->get_iterator_names().size() > 0)\n    {\n        std::string name = generate_new_variable_name();\n        isl_id *id = isl_id_alloc(ctx, name.c_str(), NULL);\n        iterators = isl_id_list_add(iterators, id);\n\n        for (int i = 0; i < this->get_iterator_names().size(); i++)\n        {\n            name = this->get_iterator_names()[i];\n            id = isl_id_alloc(ctx, name.c_str(), NULL);\n            iterators = isl_id_list_add(iterators, id);\n\n            name = generate_new_variable_name();\n            id = isl_id_alloc(ctx, name.c_str(), NULL);\n            iterators = isl_id_list_add(iterators, id);\n        }\n\n        ast_build = isl_ast_build_set_iterators(ast_build, iterators);\n    }\n\n    // Intersect the iteration domain with the domain of the schedule.\n    isl_union_map *umap =\n        isl_union_map_intersect_domain(\n            isl_union_map_copy(this->get_aligned_identity_schedules()),\n            isl_union_set_copy(this->get_trimmed_time_processor_domain()));\n\n    // polyfp::str_dump(\"Schedule:\", isl_union_map_to_str(this->get_schedule()));\n    // polyfp::str_dump(\"Iteration domain:\",\n    //                             isl_union_set_to_str(this->get_iteration_domain()));\n    // polyfp::str_dump(\"Trimmed Time-Processor domain:\",\n    //                             isl_union_set_to_str(this->get_trimmed_time_processor_domain()));\n    // polyfp::str_dump(\"Trimmed Time-Processor aligned identity schedule:\",\n    //                             isl_union_map_to_str(this->get_aligned_identity_schedules()))  ;        \n    // polyfp::str_dump(\"Identity schedule intersect trimmed Time-Processor domain:\",\n    //                             isl_union_map_to_str(umap));    \n    const char *s; \n    s = \"[N,M,K] -> {s_2[i,j,k] -> [0, i, 0, j, 0, k, 10] : 0 <= i <= N and 0 <= j <= M and 0 <= k <= K; s_1[i, j, k] -> [0, i, 0, j, 0, k, 0] : 0 <= i <= N and 0 <= j <= M and 0 <= k <= 1 }\";\n    // s_2[0, i, 0, j, 0, k, 0] -> [0, i' = i, 0, j' = j, 0, k' = k, 0] : 0 <= i <= 4095 and 0 <= j <= 4095 and 0 <= k <= 4095; \n    // s_1[0, i, 0, j, 0, k, 10] -> [0, i' = i, 0, j' = j, 0, k' = k, 10] : 0 <= i <= 4095 and 0 <= j <= 4095 and 0 <= k <= 4095       \n    isl_union_map *fmap = isl_union_map_read_from_str(ctx,s);\n\n    this->ast = isl_ast_build_node_from_schedule_map(ast_build, umap);\n\n    isl_ast_build_free(ast_build);\n\n}\n\n\nvoid polyfp::function::check_loop_fusion()\n{\n    for (auto &comp: this->leader_computations)\n    {   \n        // comp->get_loads_stores();\n        comp->load_vector.clear();\n        comp->store_vector.clear();\n        comp->map_loadstores.clear();\n        comp->get_all_loadstores();\n        // comp->dump_components();\n        // comp->dump_loads_stores();\n        comp->dump_all_loadstores();\n    }\n    auto temp_computations = this->leader_computations;\n    std::vector<int> leader_list;\n    int leader_num = temp_computations.size();\n    // for(int i=0; i<leader_num; i++){\n    //     auto comp_from = leader_computations[i];\n    //     int comp_from_index = this->leader_computation_index[comp_from];\n    //     leader_list.push_back(comp_from_index);\n    // }\n    for(int i=0; i<leader_num-1; i++)\n    {\n        auto comp_first = leader_computations[i];\n        auto comp_second = leader_computations[i+1];\n        if(comp_first->get_name()!=comp_second->get_name())\n        {\n            bool has_edge = false;\n            for(auto &store: comp_first->store_vector)\n            {\n                for(auto &load: comp_second->load_vector)\n                {\n                    if(store->get_name() == load->get_name())\n                    {\n                        has_edge = true;\n                    }\n                }\n            }\n            if(has_edge == false)\n            {\n                auto ndim_first = comp_first->get_loop_levels_number();\n                auto ndim_second = comp_second->get_loop_levels_number();\n                auto dim_first = comp_first->get_iteration_variables();\n                auto dim_second = comp_second->get_iteration_variables();\n                bool is_legal = true;                                                                                                                                                \n                if(ndim_first == ndim_second)\n                {\n                    for(int i=0; i<ndim_first; i++)\n                    {\n                        if(stoi(dim_first[i].get_upper().to_str())!=stoi(dim_second[i].get_upper().to_str())||stoi(dim_first[i].get_lower().to_str())!=stoi( dim_second[i].get_lower().to_str()))\n                        {\n                            is_legal = false;\n                        }\n                    }\n                }\n                else\n                {\n                    is_legal = false;\n                }\n                if(is_legal == true)\n                {\n                    comp_second->after(comp_first, ndim_first-1);\n                    comp_second->refused = true;\n                    this->refused = true;\n                   \n                    for(int i=0; i<comp_first->get_loop_level_names().size(); i++)\n                    {\n                        comp_second->temp_access_map.insert(std::pair(comp_second->get_loop_level_names()[i],comp_first->get_loop_level_names()[i]));\n                    }\n                    \n                    std::vector<polyfp::expr> new_placeholder_index;\n                    auto temp_placeholder_index = comp_first->get_placeholder_dims();\n                    auto original_placeholder_index = comp_second->get_placeholder_dims();\n                    for(int i=0; i<original_placeholder_index.size(); i++)\n                    {\n                        //TODO: the index is not a var: e.g. i+1\n                        auto tvar = comp_second->get_placeholder_dims()[i];\n                        tvar.set_name(comp_second->temp_access_map[comp_second->get_placeholder_dims()[i].get_name()]);\n                        new_placeholder_index.push_back(tvar);\n                        // for(auto &kv: original_placeholder_index){\n                        //     if(kv.get_expr_type() == polyfp::e_op){\n                        //     }else{\n                        //         auto t = temp_placeholder_index[i].get_name();\n                        //         std::cout<<t;\n                        //         if(kv.get_name() == t){\n                        //             new_placeholder_index.push_back(kv);\n                        //         }\n                        //     }\n                        //     // std::cout<<t;\n                        // }\n                        // std::cout<<\"step 3 success\"<<std::endl;\n                    }\n                    comp_second->set_placeholder_dims(new_placeholder_index);\n                    comp_second->set_loop_level_names(comp_first->get_loop_level_names());\n                }                    \n            }\n        }\n    }   \n}\n\n\nvoid polyfp::function::dependence_analysis()\n{\n    auto temp_computations = this->leader_computations;\n    for(auto &comp: temp_computations)\n    {\n        comp->compute_dependence_vectors();\n        comp->auto_loop_transformation();\n    }\n    auto modified_computations = this->leader_computations;\n\n    if(temp_computations.size()<=10)\n    {\n        this->check_loop_fusion();\n    }\n    \n}\n\n\nvoid polyfp::function::dfs(int pos, int top, int end, int map[500][500], int n, int v[100],int stack[550])//从pos点开始访问\n{   \n    // std::cout<<\"DFSING\"<<std::endl;\n\n\tint i;\n\tif(pos==end)\n    {\n        std::vector<long> path;\n\t\tfor(i=0;i<top;i++)\n        {\n            path.push_back(stack[i]);\n\t\t}\n        path.push_back(end);\n        this->paths.push_back(path);\n\t\treturn;\n\t}\n\tv[pos]=1; \n\tstack[top++]=pos;\n\tfor(i=1;i<=n;i++)\n    {\n\t\tif(!v[i]&&map[pos][i])\n\t\t\tthis->dfs(i,top,end,map,n,v,stack);\n\t}\n\tv[pos]=0;\n\ttop--;\n}\n\nvoid polyfp::function::compute_dependency_graph(){\n\n    int map[500][500]={0};\n\n    std::vector<int> leader_list;\n    int leader_num = this->leader_computations.size();\n    for(int i=0; i<leader_num; i++)\n    {\n        auto comp_from = leader_computations[i];\n        int comp_from_index = this->leader_computation_index[comp_from];\n        leader_list.push_back(comp_from_index);\n    }\n    for(int i=0; i<leader_num; i++)\n    {\n        auto comp_from = leader_computations[i];\n        int comp_from_index = this->leader_computation_index[comp_from];\n        for(int j=i; j<leader_num; j++)\n        {\n            auto comp_to = leader_computations[j];\n            int comp_to_index = this->leader_computation_index[comp_to];\n            if(comp_from->get_name()!=comp_to->get_name())\n            {\n                bool has_edge = false;\n                for(auto &store: comp_from->store_vector)\n                {\n                    for(auto &load: comp_to->load_vector)\n                    {\n                        if(store->get_name() == load->get_name())\n                        {\n                            has_edge = true;\n                        }\n                    }\n                }\n                if(has_edge == true)\n                {\n                    map[comp_from_index][comp_to_index] = 1;\n                    std::vector<int>::iterator it = find(leader_list.begin(), leader_list.end(), comp_to_index);\n                    if ( it!=leader_list.end())\n                    {\n                        leader_list.erase(it);\n                    }\n                }\n            }\n        }\n    }\n\n\n    std::vector<int> leafs;\n\n    for(auto &comp: this->leader_computations)\n    {\n        if(comp->is_leaf == true)\n        {\n            leafs.push_back(this->leader_computation_index[comp]);\n        }\n    }\n    if(this->leader_computations.size()<=30)\n    {\n        for(auto &leader: leader_list){\n            for(auto &leaf: leafs){\n                int stack[550],v[500]={0},top=0,n=this->leader_computations.size(),start=leader,end=leaf;\n                this->dfs(start,top,end,map,n,v,stack);\n            }\n        }\n    }\n}\n\n\nvoid polyfp::function::auto_DSE(std::string path)\n{\n    this->auto_DSE_loop_transformation();\n    for(auto &comp: this->leader_computations)\n    {\n        if(comp->is_skewed_inDSE == true)\n        {\n            this->dump_schedule(path);\n            return;\n        }\n    }\n    this->evaluate_func();\n    auto comp = this->update_latency();\n    this->best_latency = this->current_latency;\n    this->best_dsp_usage = 9999; \n    int factor = 1;\n\n    this->auto_DSE_tile_size(comp,factor,path); \n    std::vector<int> temp;\n    for(auto &comp: this->leader_computations)\n    {\n        comp->set_schedule(comp->original_schedule);\n        comp->set_loop_level_names(comp->original_loop_level_name);\n        comp->directive_map.clear();\n        comp->is_unrolled = false;\n        comp->unroll_factor.clear();\n        comp->unroll_dimension.clear();\n        comp->tile_map.clear();\n        comp->tile_size_map.clear();\n        comp->access_map.clear();\n        comp->final_loop_level_names.clear();\n        comp->final_loop_level_names = comp->final_loop_level_names_reserved;\n        if(comp->is_optimized == true)\n        {\n            if(comp->final_strategy.size()!=0)\n            {\n                comp->apply_opt_strategy(comp->final_strategy);\n            }\n            else\n            {\n                auto iterators = comp->get_iteration_variables();\n                int size = iterators.size();\n                std::map<int,polyfp::var> iterator_map;\n                for(auto &iter: iterators)\n                {\n                    int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                    iterator_map[loc] = iter;\n                }\n                if(size >= 3)\n                {\n                    comp->pipeline(iterator_map[size-3+2],1);\n                }else if(size == 2)\n                {\n                    comp->pipeline(iterator_map[1],1);\n                }else if(size == 1)\n                {\n                    comp->pipeline(iterator_map[0],1);\n                }\n            }\n        }\n        else\n        {\n            auto iterators = comp->get_iteration_variables();\n            int size = iterators.size();\n            std::map<int,polyfp::var> iterator_map;\n            for(auto &iter: iterators)\n            {\n                int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                iterator_map[loc] = iter;\n            }\n            if(size >= 3)\n            {\n                comp->pipeline(iterator_map[size-3+2],1);\n            }\n            else if(size == 2)\n            {\n                comp->pipeline(iterator_map[1],1);\n            }\n            if(size == 1)\n            {\n                comp->pipeline(iterator_map[0],1);\n            }\n        }\n    }\n    this->dump_schedule(path);\n}\nvoid polyfp::function::auto_DSE_loop_transformation()\n{\n    for (auto &comp: this->leader_computations)\n    {   \n        comp->get_all_loadstores();\n        comp->dump_all_loadstores();\n    }\n    this->dependence_analysis();\n\n    for (int i=0; i<this->leader_computations.size(); i++)\n    {\n        this->leader_computation_index[leader_computations[i]] = i;\n        this->leader_computations[i]->original_schedule = leader_computations[i]->get_schedule();\n        std::vector<std::string> current_name_list = this->leader_computations[i]->get_loop_level_names();\n        int final_size = this->leader_computations[i]->final_loop_level_names.size();\n        int current_size = current_name_list.size();\n        if(final_size == current_size)\n        {\n            this->leader_computations[i]->final_loop_level_names = current_name_list;\n            this->leader_computations[i]->final_loop_level_names_reserved = current_name_list;\n        }\n        else if(final_size < current_size)\n        {\n            for(int i=0; i<final_size; i++){\n                this->leader_computations[i]->final_loop_level_names[i] = current_name_list[i];\n                this->leader_computations[i]->final_loop_level_names_reserved[i] = current_name_list[i];\n            }\n        }\n        this->leader_computations[i]->original_loop_level_name = leader_computations[i]->get_loop_level_names();\n\n        for(auto &part:this->leader_computations[i]->components)\n        {\n            part.first->original_loop_level_name = part.first->get_loop_level_names();\n            part.first->original_schedule = part.first->get_schedule();\n        }\n\n        if(leader_computations[i]->is_leaf == true)\n        {\n            this->leaf_computations.push_back(leader_computations[i]);\n        }\n    }\n\n    for(auto &comp:this->get_body())\n    {\n        std::vector<std::string> current_name_list = comp->get_loop_level_names();\n        int final_size = comp->final_loop_level_names.size();\n        int current_size = current_name_list.size();\n        if(final_size == current_size)\n        {\n            comp->final_loop_level_names = current_name_list;\n            comp->final_loop_level_names_reserved = current_name_list;\n        }else if(final_size < current_size){\n            for(int i=0; i<final_size; i++){\n                comp->final_loop_level_names[i] = current_name_list[i];\n                comp->final_loop_level_names_reserved[i] = current_name_list[i];\n            }\n        }\n    }\n    if(this->leader_computations.size()<=30){\n        this->compute_dependency_graph();\n    }\n}\n\nvoid polyfp::function::dump_schedule(std::string path)\n{\n    for(auto &comp: this->get_body())\n    {\n        comp->iterators_location_map.clear();\n        this->global_location = 0;\n    }\n    this->gen_loop_location();\n    this->gen_time_space_domain();\n    this->gen_isl_ast();\n\n    mlir::MLIRContext context;\n    auto manager = polyfp::MLIRGenImpl(context);\n    int level = 0;\n    context.disableMultithreading();\n    context.getOrLoadDialect<mlir::func::FuncDialect>();\n    context.getOrLoadDialect<mlir::AffineDialect>();\n    context.getOrLoadDialect<mlir::LLVM::LLVMDialect>();\n    context.getOrLoadDialect<mlir::math::MathDialect>();\n    context.getOrLoadDialect<mlir::memref::MemRefDialect>();\n    context.getOrLoadDialect<mlir::scalehls::HLSDialect>();\n\n    manager.mlirGen1(*this,this->get_isl_ast(),level,true, false, false);\n    bool skew_flag = false;\n    for(auto &comp : this->leader_computations)\n    {\n        if(comp->is_skewed_inDSE == true){\n            skew_flag = true;\n        }\n        int index = this->leader_computation_index[comp];\n        int position = manager.start_loops_position[index];\n        for(auto &comp : this->leader_computations)\n        {\n            for(auto &kv : comp->get_directive_map())\n            {\n                if(kv.second == \"pipeline\")\n                {\n                    int loc_2 = comp->get_loop_level_number_from_dimension_name(kv.first);\n                    int loc = comp->iterators_location_map[kv.first];\n                    mlir::scalehls::setLoopDirective(manager.ops[loc], true, comp->II, false, false);\n                    for(int i=1; i<=loc_2; i++)\n                    {\n                        mlir::scalehls::setLoopDirective(manager.ops[loc-i], false, comp->II, false, true);\n                    }\n                }\n            }     \n            for(auto &sub_comps: comp->components)\n            {\n                auto sub_comp = sub_comps.first;\n                for(auto &kv : sub_comp->get_directive_map())\n                {\n                    if(kv.second == \"pipeline\"){\n                        int loc_2 = sub_comp->get_loop_level_number_from_dimension_name(kv.first);\n                        int loc = sub_comp->iterators_location_map[kv.first];\n                        mlir::scalehls::setLoopDirective(manager.ops[loc], true, sub_comp->II, false, false);\n                        for(int i=1; i<=loc_2; i++)\n                        {\n                            mlir::scalehls::setLoopDirective(manager.ops[loc-i], false, sub_comp->II, false, true);\n                        }\n                    }\n                }  \n            }                \n        }       \n    }\n\n    auto map = manager.get_argument_map();\n\n    mlir::scalehls::setTopFuncAttr(manager.get_funcs()[0]);\n    mlir::scalehls::applyFuncPreprocess(manager.get_funcs()[0], true);\n                \n    for(auto &comp: this->leader_computations)\n    {\n        auto iterators = comp->get_iteration_variables();\n        int size = iterators.size();\n        if(size==1)\n        {\n            var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n        }\n        if(comp->is_unrolled == true&&size!=1)\n        {\n            for(int i=0; i<comp->unroll_dimension.size(); i++)\n            {\n                int loc = comp->iterators_location_map[comp->unroll_dimension[i].get_name()];\n                if(comp->unroll_factor[i] != -1)\n                {\n                    mlir::loopUnrollUpToFactor(manager.ops[loc],comp->unroll_factor[i]);\n                }else{\n                    mlir::loopUnrollFull(manager.ops[loc]);\n                }\n            }  \n            for(auto &sub_comps:comp->components)\n            {\n                auto sub_comp = sub_comps.first;\n                for(int i=0; i<comp->unroll_dimension.size(); i++)\n                {\n                    if(sub_comp->unroll_dimension.size()!=0)\n                    {\n                        int loc = sub_comp->iterators_location_map[sub_comp->unroll_dimension[i].get_name()];\n                        if(sub_comp->unroll_factor[i] != -1)\n                        {\n                            mlir::loopUnrollUpToFactor(manager.ops[loc],sub_comp->unroll_factor[i]);\n                        }\n                        else\n                        {\n                            mlir::loopUnrollFull(manager.ops[loc]);\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    mlir::scalehls::applyMemoryOpts(manager.get_funcs()[0]);\n    mlir::scalehls::applyAutoArrayPartition(manager.get_funcs()[0]);\n\n    if(this->refused == true){\n    //TODO: there exists a bug in the latest version of POM, and the DSE of partition factor is not working.\n    //      This may affect the best array partition results generated by POM. We will fix the bug as soon as possible.\n        auto temp_p = this->get_placeholders();\n        auto temp_p_d = temp_p[\"A\"];\n        if(temp_p_d->get_dim_sizes()[0] == 32)\n        {\n            this->set_partition(\"A\",{16,16},{\"cyclic\",\"cyclic\"});\n        }else if(temp_p_d->get_dim_sizes()[0] == 64)\n        {\n            this->set_partition(\"A\",{32,32},{\"cyclic\",\"cyclic\"});\n        }else\n        {\n            this->set_partition(\"A\",{16,32},{\"cyclic\",\"cyclic\"});\n        }\n        auto map = manager.get_array_map();\n        for(auto &kv: this->get_partition_map())\n        {\n            SmallVector<mlir::scalehls::hls::PartitionKind, 4> kinds;\n            SmallVector<unsigned, 4> factors;\n            for(auto &factor: std::get<1>(kv))\n            {\n                factors.push_back(factor);\n            }\n            for(auto &type: std::get<2>(kv))\n            {\n                if(type == \"cyclic\"){\n                    kinds.push_back(mlir::scalehls::hls::PartitionKind::CYCLIC);\n                }else if(type == \"block\"){\n                    kinds.push_back(mlir::scalehls::hls::PartitionKind::BLOCK);\n                }else if(type == \"none\"){\n                    kinds.push_back(mlir::scalehls::hls::PartitionKind::NONE);\n                }\n            }\n            mlir::scalehls::applyArrayPartition(manager.get_funcs()[0].getArgument(map[std::get<0>(kv)]), factors, kinds,/*updateFuncSignature=*/true);\n            // manager.getModule().dump();\n        }\n    }\n    \n    SmallVector<int64_t, 8> factors;\n    std::string errorMessage;\n    std::string pwd = std::filesystem::current_path().parent_path();\n    auto configFile = mlir::openInputFile(pwd+\"/samples/config.json\", &errorMessage);\n    if (!configFile) \n    {\n      llvm::errs() << errorMessage << \"\\n\";\n    }\n    auto config = llvm::json::parse(configFile->getBuffer());\n    if (!config) \n    {\n      llvm::errs() << \"failed to parse the target spec json file\\n\";\n    }\n    auto configObj = config.get().getAsObject();\n    if (!configObj) \n    {\n      llvm::errs() << \"support an object in the target spec json file, found \"\n                      \"something else\\n\";\n    }\n    unsigned maxDspNum =ceil(configObj->getInteger(\"dsp\").getValueOr(220));\n    this->dsp_max = maxDspNum;\n    llvm::StringMap<int64_t> latencyMap;\n    mlir::scalehls::getLatencyMap(configObj, latencyMap);\n    llvm::StringMap<int64_t> dspUsageMap;\n    mlir::scalehls::getDspUsageMap(configObj, dspUsageMap);\n    // TODO: Parameterize initial parallel factor, max DSE iteration, max unroll && partition factor\n    int loc = 0;\n    int total_dsp = 0;\n    long total_latency = 0;\n    if(manager.start_loops_position.size() == 0)\n    {\n        manager.start_loops_position.push_back(0);\n    }\n    if(skew_flag == false)\n    {\n        mlir::scalehls::ScaleHLSEstimator(latencyMap, dspUsageMap, true).estimateFunc(manager.funcs[0]);\n        for(auto &loop: manager.start_loops_position)\n        {\n            mlir::scalehls::ScaleHLSEstimator(latencyMap, dspUsageMap, true).estimateLoop(manager.ops[loop],manager.funcs[0]);\n            // manager.getModule().dump(); \n            auto latency = mlir::scalehls::getTiming(manager.ops[loop]).getLatency();\n            auto dspNum = mlir::scalehls::getResource(manager.ops[loop]).getDsp();\n        }\n\n    }\n    \n    auto module = manager.getModule();\n    // mlir::verify(module);\n    // if (mlir::failed(mlir::verify(module))) {\n    //     module->emitError(\"module verification error\");\n    //     // module->dump();\n    // }\n    // module->dump();\n    std::error_code error;\n    std::string s = this->get_name();\n    std::string path1 = path+s+\".mlir\";\n    llvm::raw_fd_ostream os(path1, error);\n    os << *module;\n    // std::cout<<\"Note: \"+s+\".cpp has been generated!\"<<std::endl;\n\n}\nvoid polyfp::function::evaluate_func()\n{\n    for(auto &comp: this->get_body()){\n        comp->iterators_location_map.clear();\n        this->global_location = 0;\n    }\n\n    for(auto &comp: this->leader_computations){\n        if(comp->is_optimized == true && this->current_opt_comp!= NULL &&this->current_opt_comp->get_name()!=comp->get_name())\n        {\n            comp->set_schedule(comp->original_schedule);\n            comp->set_loop_level_names(comp->original_loop_level_name);\n            comp->directive_map.clear();\n            comp->is_unrolled = false;\n            comp->unroll_factor.clear();\n            comp->unroll_dimension.clear();\n            comp->tile_map.clear();\n            comp->tile_size_map.clear();\n            comp->access_map.clear();\n            comp->final_loop_level_names.clear();\n            comp->final_loop_level_names = comp->final_loop_level_names_reserved;\n            if(comp->final_strategy.size()!=0)\n            {\n                comp->apply_opt_strategy(comp->final_strategy);\n            }else\n            {\n                auto iterators = comp->get_iteration_variables();\n                int size = iterators.size();\n                std::map<int,polyfp::var> iterator_map;\n\n                for(auto &iter: iterators)\n                {\n                    int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                    // int loc = comp->iterators_location_map(iter.get_name());\n                    // std::cout<<iter.get_name()<<std::endl;\n                    iterator_map[loc] = iter;\n                }\n                if(size >= 3)\n                {\n                    comp->pipeline(iterator_map[size-3+2],1);\n                    for(auto &sub_comps: comp->components)\n                    {\n                        auto sub_comp = sub_comps.first;\n                        //TODO, right pipeline level\n                        if(sub_comp->after_level !=2)\n                        {   \n                            sub_comp->pipeline(iterator_map[size-3+2],1);\n                        }\n                    }\n                }\n                else if(size == 2)\n                {\n                    comp->pipeline(iterator_map[1],1);\n                    for(auto &sub_comps: comp->components)\n                    {\n                        auto sub_comp = sub_comps.first;\n                        //TODO, right pipeline level\n                        if(sub_comp->after_level !=1)\n                        {             \n                            sub_comp->pipeline(iterator_map[1],1);\n                        }\n                    }\n                }else if(size == 1)\n                {\n                    comp->pipeline(iterator_map[0],1);\n                }\n            }   \n        }\n        else if(comp->is_optimized == true && this->current_opt_comp!= NULL &&this->current_opt_comp->get_name()==comp->get_name())\n        {\n            comp->set_schedule(comp->original_schedule);\n            comp->set_loop_level_names(comp->original_loop_level_name);\n            comp->directive_map.clear();\n            comp->is_unrolled = false;\n            comp->unroll_factor.clear();\n            comp->unroll_dimension.clear();\n            comp->tile_map.clear();\n            comp->tile_size_map.clear();\n            comp->access_map.clear();\n            comp->final_loop_level_names.clear();\n            comp->final_loop_level_names = comp->final_loop_level_names_reserved;\n            if(comp->temp_strategy.size()!=0)\n            {\n                comp->apply_opt_strategy(comp->temp_strategy);\n            }\n            else if(comp->final_strategy.size()!=0)\n            {\n                comp->apply_opt_strategy(comp->final_strategy);\n            }\n            else\n            {\n                auto iterators = comp->get_iteration_variables();\n                int size = iterators.size();\n                std::map<int,polyfp::var> iterator_map;\n                for(auto &iter: iterators)\n                {\n                    int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                    // int loc = comp->iterators_location_map(iter.get_name());\n                    // std::cout<<iter.get_name()<<std::endl;\n                    iterator_map[loc] = iter;\n                }\n                if(size >= 3)\n                {\n                    comp->pipeline(iterator_map[size-3+2],1);\n                    for(auto &sub_comps: comp->components)\n                    {\n                        auto sub_comp = sub_comps.first;\n                        // TODO, right pipeline level\n                        if(sub_comp->after_level !=2){   \n                            sub_comp->pipeline(iterator_map[size-3+2],1);\n                        }\n                    }\n                }else if(size == 2)\n                {\n                    comp->pipeline(iterator_map[1],1);\n                    for(auto &sub_comps: comp->components){\n                        auto sub_comp = sub_comps.first;\n                        // TODO, right pipeline level\n                        if(sub_comp->after_level !=1)\n                        {    \n                            sub_comp->pipeline(iterator_map[1],1);\n                        }\n                    }\n                }else if(size == 1)\n                {\n                    comp->pipeline(iterator_map[0],1);\n                }  \n            }\n        }\n        else if(this->current_opt_comp!= NULL && this->current_opt_comp->get_name()!=comp->get_name())\n        {\n            comp->set_schedule(comp->original_schedule);\n            comp->set_loop_level_names(comp->original_loop_level_name);\n            comp->directive_map.clear();\n            comp->is_unrolled = false;\n            comp->unroll_factor.clear();\n            comp->unroll_dimension.clear();\n            comp->tile_map.clear();\n            comp->tile_size_map.clear();\n            comp->access_map.clear();\n            comp->final_loop_level_names.clear();\n            comp->final_loop_level_names = comp->final_loop_level_names_reserved;\n\n            auto iterators = comp->get_iteration_variables();\n            int size = iterators.size();\n            std::map<int,polyfp::var> iterator_map;\n            for(auto &iter: iterators)\n            {\n                int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                iterator_map[loc] = iter;\n            }\n            if(size >= 3)\n            {\n                comp->pipeline(iterator_map[size-3+2],1);\n                for(auto &sub_comps: comp->components){\n                    auto sub_comp = sub_comps.first;\n                    // TODO, right pipeline level\n                    if(sub_comp->after_level !=2){   \n                        sub_comp->pipeline(iterator_map[size-3+2],1);\n                    }\n                }\n            }else if(size == 2)\n            {\n                comp->pipeline(iterator_map[1],1);\n                for(auto &sub_comps: comp->components)\n                {\n                    auto sub_comp = sub_comps.first;\n                    // TODO, right pipeline level\n                    if(sub_comp->after_level !=1)\n                    {   \n                        sub_comp->pipeline(iterator_map[1],1);\n                    }\n                }\n            }else if(size == 1)\n            {\n                comp->pipeline(iterator_map[0],1);\n            }\n        }\n        else if(this->current_opt_comp!= NULL && this->current_opt_comp->get_name()==comp->get_name())\n        {\n            comp->set_schedule(comp->original_schedule);\n            comp->set_loop_level_names(comp->original_loop_level_name);\n            comp->directive_map.clear();\n            comp->is_unrolled = false;\n            comp->unroll_factor.clear();\n            comp->unroll_dimension.clear();\n            comp->tile_map.clear();\n            comp->tile_size_map.clear();\n            comp->access_map.clear();\n            comp->final_loop_level_names.clear();\n            comp->final_loop_level_names = comp->final_loop_level_names_reserved;\n            auto iterators = comp->get_iteration_variables();\n\n            int size = iterators.size();\n            std::map<int,polyfp::var> iterator_map;\n            if(comp->temp_strategy.size()!=0)\n            {\n                comp->apply_opt_strategy(comp->temp_strategy);\n            }\n            else\n            {\n                auto iterators = comp->get_iteration_variables();\n                int size = iterators.size();\n                std::map<int,polyfp::var> iterator_map;\n                for(auto &iter: iterators)\n                {\n                    int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n                    iterator_map[loc] = iter;\n                }\n                if(size >= 3)\n                {\n                    comp->pipeline(iterator_map[size-3+2],1);\n\n                    for(auto &sub_comps: comp->components)\n                    {\n                        auto sub_comp = sub_comps.first;\n                        // TODO, right pipeline level\n                        if(sub_comp->after_level !=2)\n                        {   \n                            sub_comp->pipeline(iterator_map[size-3+2],1);\n                        }\n                    }\n                }\n                else if(size == 2)\n                {\n                    comp->pipeline(iterator_map[1],1);\n                    for(auto &sub_comps: comp->components){\n                        auto sub_comp = sub_comps.first;\n                        // TODO, right pipeline level\n                        if(sub_comp->after_level !=1)\n                        {                               \n                            sub_comp->pipeline(iterator_map[1],1);\n                        }\n                    }\n                }\n                else if(size == 1)\n                {\n                    comp->pipeline(iterator_map[0],1);\n                } \n            }    \n        }\n        else{\n            // std::cout<< comp->get_name()+\"evaluation initialization failed\"<<std::endl;\n        }\n    }\n\n    this->gen_loop_location();\n    this->gen_time_space_domain();\n    this->gen_isl_ast();\n    mlir::MLIRContext context;\n    auto manager = polyfp::MLIRGenImpl(context);\n    int level = 0;\n    context.disableMultithreading();\n    context.getOrLoadDialect<mlir::func::FuncDialect>();\n    context.getOrLoadDialect<mlir::AffineDialect>();\n    context.getOrLoadDialect<mlir::LLVM::LLVMDialect>();\n    context.getOrLoadDialect<mlir::math::MathDialect>();\n    context.getOrLoadDialect<mlir::memref::MemRefDialect>();\n    context.getOrLoadDialect<mlir::scalehls::HLSDialect>();\n\n    manager.mlirGen1(*this,this->get_isl_ast(),level,true, false, false);\n    \n    for(auto &comp : this->leader_computations)\n    {\n        int index = this->leader_computation_index[comp];\n        int position = manager.start_loops_position[index];\n        //TODO:\n        for(auto &comp : this->leader_computations)\n        {\n            for(auto &kv : comp->get_directive_map())\n            {\n                if(kv.second == \"pipeline\")\n                {\n                    int loc_2 = comp->get_loop_level_number_from_dimension_name(kv.first);\n                    int loc = comp->iterators_location_map[kv.first];\n\n                    // index = loc + index;\n                    mlir::scalehls::setLoopDirective(manager.ops[loc], true, comp->II, false, false);\n                    for(int i=1; i<=loc_2; i++)\n                    {\n                        mlir::scalehls::setLoopDirective(manager.ops[loc-i], false, comp->II, false, true);\n                    }\n                }\n            }             \n        }          \n    }\n\n    auto map = manager.get_argument_map();\n    mlir::scalehls::setTopFuncAttr(manager.get_funcs()[0]);\n    mlir::scalehls::applyFuncPreprocess(manager.get_funcs()[0], true);\n    \n    for(auto &comp: this->leader_computations)\n    {\n        if(comp->is_unrolled == true)\n        {\n            for(int i=0; i<comp->unroll_dimension.size(); i++)\n            {\n                int loc = comp->iterators_location_map[comp->unroll_dimension[i].get_name()];\n                // loc = loc + bias;\n                if(comp->unroll_factor[i] != -1)\n                {\n                    mlir::loopUnrollUpToFactor(manager.ops[loc],comp->unroll_factor[i]);\n                }\n                else\n                {\n                    mlir::loopUnrollFull(manager.ops[loc]);\n                } \n            }  \n            for(auto &sub_comps:comp->components)\n            {\n                auto sub_comp = sub_comps.first;\n                for(int i=0; i<sub_comp->unroll_dimension.size(); i++)\n                {\n                    int loc = sub_comp->iterators_location_map[sub_comp->unroll_dimension[i].get_name()];\n                    if(sub_comp->unroll_factor[i] != -1)\n                    {\n                        mlir::loopUnrollUpToFactor(manager.ops[loc],sub_comp->unroll_factor[i]);\n                    }\n                    else\n                    {\n                        mlir::loopUnrollFull(manager.ops[loc]);\n                    }\n                }\n            }\n        }\n    }\n\n    mlir::scalehls::applyMemoryOpts(manager.get_funcs()[0]);\n    mlir::scalehls::applyAutoArrayPartition(manager.get_funcs()[0]);\n    SmallVector<int64_t, 8> factors;\n    std::string errorMessage;\n    std::string pwd = std::filesystem::current_path().parent_path();\n    auto configFile = mlir::openInputFile(pwd+\"/samples/config.json\", &errorMessage);\n    if (!configFile) {\n      llvm::errs() << errorMessage << \"\\n\";\n    }\n    auto config = llvm::json::parse(configFile->getBuffer());\n    if (!config) {\n      llvm::errs() << \"failed to parse the target spec json file\\n\";\n    }\n    auto configObj = config.get().getAsObject();\n    if (!configObj) {\n      llvm::errs() << \"support an object in the target spec json file, found \"\n                      \"something else\\n\";\n    }\n    unsigned maxDspNum =ceil(configObj->getInteger(\"dsp\").getValueOr(220));\n    this->dsp_max = maxDspNum;\n    auto name = this->get_name();\n\n    // TODO: Vitis_HLS 2022.2 improved its scheduling methods \n    // and two data paths of test_3mm can be executed in parallel.\n    // Therefore, the actual DSP usage is twice that estimated by the cost model.\n    // A profiler needs to be added to analyze the potential parallel datapath \n    // and adjust the DSP usage accordingly.\n    if(name.substr(0, 8) == \"test_3mm\")\n    {\n        this->dsp_max = this->dsp_max/2;\n    }\n\n    llvm::StringMap<int64_t> latencyMap;\n    mlir::scalehls::getLatencyMap(configObj, latencyMap);\n    llvm::StringMap<int64_t> dspUsageMap;\n    mlir::scalehls::getDspUsageMap(configObj, dspUsageMap);\n    int loc = 0;\n    int total_dsp = 0;\n    long total_latency = 0;\n    if(manager.start_loops_position.size() == 0)\n    {\n        manager.start_loops_position.push_back(0);\n    }\n    bool consistent_flag_flag;\n    for(auto &loop: manager.start_loops_position )\n    {\n        mlir::scalehls::ScaleHLSEstimator(latencyMap, dspUsageMap, true).estimateLoop(manager.ops[loop],manager.funcs[0]);\n        // manager.getModule().dump(); \n        auto latency = mlir::scalehls::getTiming(manager.ops[loop]).getLatency();\n        // std::cout<<\"latency: \"+std::to_string(latency)<<std::endl;\n        auto dspNum = mlir::scalehls::getResource(manager.ops[loop]).getDsp();\n        // std::cout<<\"dsp: \"+std::to_string(dspNum)<<std::endl;\n        auto minII = mlir::scalehls::getLoopInfo(manager.ops[loop]).getMinII();\n        // std::cout<<\"minII: \"+std::to_string(minII)<<std::endl;\n        this->leader_computations[loc]->latency = latency;\n        this->leader_computations[loc]->dsp = dspNum;\n        this->leader_computations[loc]->minII = minII;\n        if(this->leader_computations[loc]->best_latency>=latency)\n        {\n            this->leader_computations[loc]->best_latency = latency;\n        }\n        else\n        {\n            if(this->current_opt_comp->get_name()==this->leader_computations[loc]->get_name())\n            {\n                consistent_flag_flag = true;\n            }\n            this->consistent_flag = false;\n        }\n\n        if(consistent_flag_flag==true)\n        {\n            this->consistent_flag  = true;\n        }\n        // total_dsp+=dspNum;\n        total_latency+=latency;\n        // std::cout<<\"total_latency: \"+std::to_string(total_latency)<<std::endl;\n        this->latency_map[loc] = latency;\n        this->resource_map[loc] = dspNum;\n        loc+=1;  \n    }\n    mlir::scalehls::ScaleHLSEstimator(latencyMap, dspUsageMap, true).estimateFunc(manager.funcs[0]);\n    total_dsp = mlir::scalehls::getResource(manager.funcs[0]).getDsp();\n   \n    this->dsp_usage = total_dsp;\n    this->current_latency = total_latency;\n    // std::cout<<\"current latency\"+std::to_string(total_latency)<<std::endl;\n    // if(this->dsp_usage>this->dsp_max){\n    //     this->new_strategy = false;\n    // }\n    // manager.getModule().dump(); \n}\n\nvoid polyfp::function::auto_DSE_tile_size(polyfp::compute *comp, int factor, std::string path)\n{\n    // std::cout<<\"Currently optimized compute: \"<<comp->get_name()<<std::endl;\n    int scale;\n    //TODO components'domain is different from the leader's\n    comp->set_schedule(comp->original_schedule);\n    comp->set_loop_level_names(comp->original_loop_level_name);\n    comp->directive_map.clear();\n    comp->is_unrolled = false;\n    comp->unroll_factor.clear();\n    comp->unroll_dimension.clear();\n    comp->tile_map.clear();\n    comp->tile_size_map.clear();\n    comp->access_map.clear();\n    auto iterators = comp->get_iteration_variables();\n    std::vector<polyfp::var> temp_iterators;\n    int temp_size = iterators.size();\n    \n    if(temp_size>3)\n    {\n        int border = temp_size-3;\n        for(auto &iter: iterators)\n        {\n            int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n            if(loc>=border)\n            {\n                temp_iterators.push_back(iter);\n            }\n        }\n        iterators.clear();\n        iterators=temp_iterators;\n    }\n\n    std::vector<int> dim_ranges;\n    std::map<int, std::vector<int>> dim_tile_sizes;\n    bool not_2_pow = false;\n    int count = 0;\n    for(auto &iter: iterators)\n    {\n        int lower = stoi(iter.get_lower().to_str());\n        int upper = stoi(iter.get_upper().to_str());\n        int range = upper-lower;\n        dim_ranges.push_back(range);\n        std::vector<int> temp;\n        if(range%32 != 0)\n        {\n            not_2_pow = true;\n            for(int i=2; i<range; i++)\n            {\n                if(range % i == 0)\n                {\n                    if(i == 2)\n                    {\n                        temp.push_back(i);   \n                    }\n                    else if(i == 3||i==5 ||i==7)\n                    {\n                        temp.push_back(i);   \n                    }\n                }\n\n            }\n            if(temp.size()==0)\n            {\n                temp.push_back(1);  \n            }\n        }\n        else\n        {\n            temp.push_back(1);   \n        }\n        dim_tile_sizes.insert(std::make_pair(count,temp));\n        count++;\n    }\n    //TODO: SKEW MAP\n    std::map<int,polyfp::var> iterator_map;\n    int size = iterators.size();\n\n    scale = 16*pow(2,factor-1);\n\n    for(auto &iter: iterators)\n    {\n        int loc = comp->get_loop_level_number_from_dimension_name(iter.get_name());\n        iterator_map[loc] = iter;\n    }\n\n    if(comp->is_optimized == true )\n    {\n        if(comp->current_factor < comp->largest_factor && comp->opt_finished == false)\n        {\n            comp->current_factor+=1;\n            factor = comp->current_factor;\n            scale = 16*pow(2,comp->current_factor-1);\n        }\n        else{\n            this->finish_list.push_back(comp->get_name());\n            if(comp->current_strategy.size()!=0)\n            {\n                comp->final_strategy = comp->current_strategy;\n            }else{\n                // TODO\n                // std::cout<<\"no final strategy\"<<std::endl;       \n            }\n            if(this->leader_computations.size()!=1)\n            {\n                int path_index = this->get_longest_path();\n                std::vector<long> current_longest_path = paths[path_index];\n                std::vector<long> current_longest_path_latency;\n                std::map<long, int> current_longest_map;\n                int num = current_longest_path.size();\n                \n                for(int i=0; i<num; i++)\n                {\n                    long temp_latency = this->latency_map[current_longest_path[i]];\n                    current_longest_path_latency.push_back(temp_latency);\n                    current_longest_map.insert(std::make_pair(temp_latency,current_longest_path[i]));\n                }\n                std::sort(current_longest_path_latency.begin(),current_longest_path_latency.end(),std::greater<long>());\n                \n                \n                for(int i=0; i<num; i++)\n                {\n                    int node_index = current_longest_path[i]; \n                    int final_index = this->path_map[path_index][node_index];\n                    std::map<polyfp::compute *,int>::iterator it;\n                    polyfp::compute *comp;\n                    for( it= this->leader_computation_index.begin();it!=this->leader_computation_index.end();it++) \n                    {\n                        if(it->second==final_index)\n                        {\n                            comp = it->first;\n                            std::string name = comp->get_name();\n                            if (std::find(finish_list.begin(), finish_list.end(), name) == finish_list.end())\n                            {\n                                auto_DSE_tile_size(comp, 1,path);\n                                return;\n                            }   \n                        }\n                            \n                    } \n                } \n            }\n            return;\n        }\n\n    }\n\n    else\n    {\n        comp->is_optimized = true;\n        comp->current_factor = factor;\n    }\n\n    int factor1=1;\n    int factor2=1;\n    int factor3=1;\n\n    std::vector<std::vector<int>> tilesize_list;\n    std::vector<int> current_design;\n    std::vector<int> final_design;\n\n    // std::vector<int> final_strategy;\n    // std::vector<int> current_strategy;\n\n    // Print header row.\n    std::string s = this->get_name();\n    std::string path1 = path+s+\".csv\";\n    std::ifstream ifs(path1,std::ios::in);\n    char ch;\n    ifs>>ch;\n    std::ofstream myfile;\n    myfile.open(path1,std::ios::app);\n    if(ifs.eof())\n    {\n        for (unsigned i = 0; i < size; ++i)\n        {\n            myfile << \"l\" << i << \",\";\n\n\n        }\n        myfile << \"cycle,dsp,ii\\n\";     \n    }\n \n    if(size >= 3)\n    {\n        // TODO, here 4 is desided by the scale\n\n        if(not_2_pow == false)\n        {\n            //config: 5,3\n            for(int i = 0; i<5+factor; i++)\n            {\n                factor1 = pow(2,i);\n                for(int j = 0; j<3+factor-i; j++)\n                {\n                    factor2 = pow(2,j);\n                    factor3 = scale/factor2/factor1;\n                    tilesize_list.push_back({factor1,factor2,factor3});\n                    // std::cout<<\"tile factor: \";\n                    // std::cout<<factor1;\n                    // std::cout<<\"; \";\n                    // std::cout<<factor2;\n                    // std::cout<<\"; \";\n                    // std::cout<<factor3<<std::endl;\n                }\n            }\n        }else\n        {\n            std::vector<int> dim0 = dim_tile_sizes[0];\n            std::vector<int> dim1 = dim_tile_sizes[1];\n            std::vector<int> dim2 = dim_tile_sizes[2];\n            if(dim0.size()==0)\n            {\n                dim0.push_back(1);\n            }\n            if(dim1.size()==0)\n            {\n                dim1.push_back(1);\n            }\n            for(auto &size0: dim0)\n            {\n                for(auto &size1: dim1)\n                {\n                    for(auto &size2: dim2)\n                    {\n                        tilesize_list.push_back({size0,size1,size2});\n                        std::cout<<\"tile factor: \";\n                        std::cout<<size0;\n                        std::cout<<\"; \";\n                        std::cout<<size1;\n                        std::cout<<\"; \";\n                        std::cout<<size2<<std::endl;\n                    }\n                }\n            }\n            comp->current_factor=3;\n\n        }\n\n        bool larger_factor = true;\n        if(larger_factor == true)\n        {\n            for(auto &tile_size: tilesize_list)\n            {\n\n                comp->set_schedule(comp->original_schedule);\n                comp->set_loop_level_names(comp->original_loop_level_name);\n                comp->directive_map.clear();\n                comp->is_unrolled = false;\n                comp->unroll_factor.clear();\n                comp->unroll_dimension.clear();\n                comp->tile_map.clear();\n                comp->tile_size_map.clear();\n                comp->access_map.clear();\n                comp->opt_finished = false;\n\n                var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n                if(tile_size[0]<=3 && tile_size[1]<=16 && tile_size[2]<=16)\n                {\n                // if(tile_size[0]<=16 && tile_size[1]<32 && tile_size[2]<32){\n                // if(tile_size[0]<2 && tile_size[1]<4 && tile_size[2]<4){\n                    int temp_index = comp->get_iteration_variables().size()-3;\n                    // std::cout<<iterator_map[0].get_name()<<std::endl;\n                    // std::cout<<iterator_map[1].get_name()<<std::endl;\n                    // std::cout<<iterator_map[2].get_name()<<std::endl;\n                    if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1)\n                    {\n                        \n                    }else{\n                        comp->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);\n                    }\n                    \n                    if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]!=1){\n                        comp->pipeline(k0,1);\n                        comp->unroll(k1,-1);\n                        comp->unroll(j1,-1);\n                        comp->unroll(i1,-1);\n                    }\n                    if(tile_size[2]!=1 && tile_size[1]!=1 && tile_size[0]==1){\n                        comp->pipeline(k0,1);\n                        comp->unroll(k1,-1);\n                        comp->unroll(j1,-1);\n                    }\n                    if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]!=1){\n                        comp->pipeline(k0,1);\n                        comp->unroll(k1,-1);\n                        comp->unroll(i1,-1);\n                    }\n                    if(tile_size[2]!=1 && tile_size[1]==1 && tile_size[0]==1){\n                        comp->pipeline(k0,1);\n                        comp->unroll(k1,-1);\n                        // comp->unroll(i1,-1);\n                    }\n                    if(tile_size[2]==1 && tile_size[1]==1 && tile_size[0]==1){\n                        int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());\n                        int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());\n                        int range = upper-lower;\n                        if(range<=7){\n                            comp->pipeline(iterator_map[temp_index+1],1);\n                            comp->unroll(iterator_map[temp_index+2],-1);\n                        }\n                    }\n                    if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1){\n                        int lower = stoi(iterator_map[temp_index+2].get_lower().to_str());\n                        int upper = stoi(iterator_map[temp_index+2].get_upper().to_str());\n                        int range = upper-lower;\n                        if(range<=6){\n                            comp->pipeline(j0,1);\n                            comp->unroll(j1,-1);\n                            comp->unroll(i1,-1);\n                            comp->unroll(iterator_map[temp_index+2],-1);\n                        }else{\n                            comp->pipeline(iterator_map[temp_index+2],1);\n                            comp->unroll(j1,-1);\n                            comp->unroll(i1,-1);\n                        }\n                        \n                    }\n                    for(auto &part:comp->components){\n                        part.first->set_schedule(part.first->original_schedule);\n                        part.first->set_loop_level_names(part.first->original_loop_level_name);\n                        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);\n                        if(tile_size[2]==1 && tile_size[1]!=1 && tile_size[0]!=1){\n                            if(part.first->after_level == 2){\n                                part.first->after(comp,j1);\n                            }else if(part.first->after_level == 0){\n                                part.first->after(comp,i0);\n                                part.first->pipeline(iterator_map[temp_index+2],1);   \n                            }\n                            // part.first->after(comp,j1);\n                        }else{\n                            if(part.first->after_level == 2){\n                                part.first->after(comp,k1);\n                            }else if(part.first->after_level == 0){\n                                part.first->after(comp,iterator_map[temp_index+0]);\n                                part.first->pipeline(iterator_map[temp_index+2],1);   \n                                //TODO\n                                part.first->unroll(k1,-1);\n                                part.first->unroll(j1,-1);\n                            }\n                            // part.first->after(comp,k1);\n                        }\n                    }\n                    int II = 1;\n                    this->current_opt_comp = comp;\n                    //TODO\n                    if(this->leader_computations.size() == -1){                          \n                        this->evaluate_func();\n                        if(this->current_latency < this->best_latency && this->dsp_max>= this->dsp_usage){\n                            this->best_latency = this->current_latency;\n                            this->best_dsp_usage = this->dsp_usage;\n                            // std::cout<<\"best_latency:  \";\n                            // std::cout<<best_latency<<std::endl;\n                            this->dump_schedule(path);\n                        }\n\n                    }else\n                    {  \n                        comp->temp_strategy = tile_size;\n                        this->evaluate_func();\n                        auto latency = comp->latency;\n                        int dsp = comp->dsp;\n                        // std::cout<<\"schedule: \"+std::to_string(tile_size[0])+\", \"+std::to_string(tile_size[1])+\", \"+std::to_string(tile_size[2])+\": \"+std::to_string(latency)+\": \"+std::to_string(dsp)<<std::endl;\n                        // this->update_latency();\n                        // std::cout<<\"after evaluation\"<<std::endl;\n                        // auto new_comp = this->update_latency();\n                        polyfp::compute * new_comp = NULL;\n                        if((this->current_latency < this->best_latency || this->consistent_flag == false) && this->dsp_max>=this->dsp_usage){\n                            auto comp = this->update_latency();\n                            int path_index = this->get_longest_path();\n                            std::vector<long> current_longest_path = paths[path_index];\n                            std::vector<long> current_longest_path_latency;\n                            std::map<long, int> current_longest_map;\n                            int num = current_longest_path.size();\n                            \n                            for(int i=0; i<num; i++){\n                                long temp_latency = this->latency_map[current_longest_path[i]];\n                                current_longest_path_latency.push_back(temp_latency);\n                                current_longest_map.insert(std::make_pair(temp_latency,current_longest_path[i]));\n                            }\n                            std::sort(current_longest_path_latency.begin(),current_longest_path_latency.end(),std::greater<long>());\n                            bool comp_flag = false;\n                            for(int i=0; i<num; i++)\n                            {\n                                int node_index = current_longest_path[i]; \n                                int final_index = this->path_map[path_index][node_index];\n                                // int final_index = current_longest_map[current_longest_path_latency[i]];\n                                // std::cout<<\"the final_index\"+std::to_string(final_index);\n                                std::map<polyfp::compute *,int>::iterator it;\n                                polyfp::compute *comp1;\n                                for( it= this->leader_computation_index.begin();it!=this->leader_computation_index.end();it++) \n                                {\n                                    if(it->second==final_index)\n                                    {\n                                        comp1 = it->first;\n                                        std::string name = comp1->get_name();\n                                        if (std::find(finish_list.begin(), finish_list.end(), name) == finish_list.end())\n                                        {\n                                            new_comp = comp1;\n                                            comp_flag = true;\n                                            \n                                            break;\n                                        }   \n                                    }\n                                        \n                                } \n                                if(comp_flag == true)\n                                {\n                                    break;\n                                }\n                            }        \n                            if(new_comp == NULL)\n                            {\n                                return;\n                            }\n                            if(new_comp->get_name() != comp->get_name() && this->dsp_max>=this->dsp_usage)\n                            {\n                                this->best_latency = this->current_latency;\n                                final_design = tile_size;\n                                break;\n                            }else if(new_comp->get_name() == comp->get_name() &&this->current_latency < this->best_latency && this->dsp_max>= this->dsp_usage)\n                            {\n                                this->best_latency = this->current_latency;\n                                this->best_dsp_usage = this->dsp_usage;               \n                                current_design = tile_size;                             \n                                long latency = comp->latency;\n                                int dsp = comp->dsp;\n                               \n                            }else{\n                                // TODO\n                            }\n                            auto latency = comp->latency;\n                                int dsp = comp->dsp;                       \n                        }\n                      \n                    }\n                    \n                    auto latency = comp->latency;\n                        int dsp = comp->dsp;\n                 \n                    myfile << tile_size[0] << \",\";\n                    myfile << tile_size[1] << \",\";\n                    myfile << tile_size[2] << \",\";\n                    myfile << latency<< \",\";\n                    myfile << this->dsp_usage << \",\";\n                    myfile << comp->minII << \"\\n\";\n\n                }\n                \n            }\n            if(final_design.size()!=0)\n            {\n                comp->final_strategy = final_design;\n                comp->current_strategy = final_design;\n                comp->apply_opt_strategy(comp->final_strategy);\n                this->evaluate_func();\n                auto new_comp = this->update_latency();\n                auto_DSE_tile_size(new_comp, 1,path);\n            }\n            else if(current_design.size()!=0)\n            {\n                comp->current_strategy = current_design;\n                comp->final_strategy = current_design;\n                auto_DSE_tile_size(comp, 1,path);\n            }else if(current_design.size()==0)\n            {\n                comp->opt_finished = true;\n                auto_DSE_tile_size(comp, 1,path);\n\n            }\n\n        }\n        myfile.close();\n    }\n    else if(size == 2)\n    {\n        if(not_2_pow == false)\n        {\n            for(int j = 0; j<2+factor; j++)\n            {\n                factor1 = pow(2,j);\n                factor2 = scale/factor1;\n                tilesize_list.push_back({factor1,factor2});     \n            }\n        }else{\n            std::vector<int> dim0 = dim_tile_sizes[0];\n            std::vector<int> dim1 = dim_tile_sizes[1];\n            if(dim0.size()==0){\n                dim0.push_back(1);\n            }\n            for(auto &size0: dim0)\n            {\n                for(auto &size1: dim1)\n                {\n                    tilesize_list.push_back({size0,size1});\n                }\n            }\n            comp->current_factor=3;\n\n        }\n        \n        bool larger_factor = true;\n        for(auto &tile_size: tilesize_list)\n        {\n            comp->set_schedule(comp->original_schedule);\n            comp->set_loop_level_names(comp->original_loop_level_name);\n            comp->directive_map.clear();\n            comp->is_unrolled = false;\n            comp->unroll_factor.clear();\n            comp->unroll_dimension.clear();\n            comp->tile_map.clear();\n            comp->tile_size_map.clear();\n            comp->access_map.clear();\n            comp->opt_finished = false;\n            var i0(\"i0\"), j0(\"j0\"), i1(\"i1\"), j1(\"j1\");\n            int lower1 = stoi(iterator_map[0].get_lower().to_str());\n            int upper1 = stoi(iterator_map[0].get_upper().to_str());\n            int range1 = upper1-lower1;\n            int lower2 = stoi(iterator_map[1].get_lower().to_str());\n            int upper2 = stoi(iterator_map[1].get_upper().to_str());\n            int range2 = upper2-lower2;\n            // if(tile_size[0]<=16 && tile_size[1]<=16){\n            if(tile_size[0]<32 && tile_size[1]<=32 && range1>tile_size[0] && range2>tile_size[1])\n            {\n            // if(tile_size[0]<2 && tile_size[1]<4){\n                // for(auto &iter: comp)\n                // std::cout<<\"size1\"<<std::endl;\n                // if(iterator_map[0].)\n                comp->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);\n                if(tile_size[1]!=1&&tile_size[0]!=1)\n                {\n                    comp->pipeline(j0,1);\n                    comp->unroll(j1,-1);\n                    comp->unroll(i1,-1);\n                }else if(tile_size[1]==1&&tile_size[0]!=1)\n                {\n                    comp->pipeline(iterator_map[1],1);\n                    comp->unroll(i1,-1);\n                }else if(tile_size[0]==1&&tile_size[1]!=1)\n                {\n                    comp->pipeline(j0,1);\n                    comp->unroll(j1,-1);\n                }\n                for(auto &part:comp->components)\n                {\n                    part.first->set_schedule(part.first->original_schedule);\n                    part.first->set_loop_level_names(part.first->original_loop_level_name);\n                    part.first->directive_map.clear();\n                    part.first->is_unrolled = false;\n                    part.first->unroll_factor.clear();\n                    part.first->unroll_dimension.clear();\n                    part.first->tile_map.clear();\n                    part.first->tile_size_map.clear();\n                    part.first->access_map.clear();\n                    part.first->tile(iterator_map[0],iterator_map[1],tile_size[0],tile_size[1],i0, j0, i1, j1);\n\n                    if(tile_size[1]!=1&&tile_size[0]!=1)\n                    {\n                        if(part.first->after_level == 1)\n                        {\n                            \n                            part.first->after(comp,j1);\n                        }else if(part.first->after_level == 0)\n                        {\n                            part.first->pipeline(j0,1);\n                            part.first->after(comp,i0);\n                            // part.first->unroll(j1,-1);\n                            // part.first->unroll(i1,-1);\n                        }\n                        \n                    }else if(tile_size[1]==1&&tile_size[0]!=1)\n                    {\n                        if(part.first->after_level == 1)\n                        {\n                            part.first->after(comp,i1);\n                        }else if(part.first->after_level == 0)\n                        {\n                            part.first->after(comp,i0);\n                            part.first->pipeline(iterator_map[1],1);\n                            \n                        }\n                        // part.first->after(comp,i1);\n                    }else if(tile_size[0]==1&&tile_size[1]!=1)\n                    {\n                        if(part.first->after_level == 1)\n                        {\n                            // part.first->unroll(j1,-1);\n                            // std::cout<<\"part.first->after(comp,j1);  \"<<std::endl;\n                            part.first->after(comp,j1);\n                            \n                        }else if(part.first->after_level == 0)\n                        {\n                            part.first->pipeline(j0,1);\n                            part.first->after(comp,iterator_map[0]);\n                            // std::cout<<\"unroll dimension 2\"<<std::endl;\n                            part.first->unroll(j1,-1);\n                        }\n                    }\n                \n                }\n                this->current_opt_comp = comp;\n                if(this->leader_computations.size() == -1)\n                {               \n                    this->evaluate_func();\n                    if(this->current_latency <= this->best_latency && this->dsp_max>= this->dsp_usage)\n                    {\n                        this->best_latency = this->current_latency;\n                        this->best_dsp_usage = this->dsp_usage;\n                        this->dump_schedule(path);\n                    }\n                    \n                    if(this->dsp_max>this->dsp_usage)\n                    {\n                        larger_factor = true;\n                        // auto_DSE_tile_size(new_comp, factor);\n                    }\n\n                }else\n                {  \n                        comp->temp_strategy = tile_size;\n                        this->evaluate_func();\n                        long latency = comp->latency;\n                        int dsp = comp->dsp;\n                        \n                        polyfp::compute * new_comp = NULL;\n                        if(this->current_latency < this->best_latency  && this->dsp_max>=this->dsp_usage)\n                        {\n                            auto comp = this->update_latency();\n                            if(this->leader_computations.size()!=1)\n                            {\n                                int path_index = this->get_longest_path();\n                                std::vector<long> current_longest_path = paths[path_index];\n                                std::vector<long> current_longest_path_latency;\n                                std::map<long, int> current_longest_map;\n                                int num = current_longest_path.size();\n                                \n                                for(int i=0; i<num; i++)\n                                {\n                                    long temp_latency = this->latency_map[current_longest_path[i]];\n                                    current_longest_path_latency.push_back(temp_latency);\n                                    current_longest_map.insert(std::make_pair(temp_latency,current_longest_path[i]));\n                                }\n                                std::sort(current_longest_path_latency.begin(),current_longest_path_latency.end(),std::greater<long>());\n                                bool comp_flag = false;\n                                for(int i=0; i<num; i++)\n                                {\n                                    int node_index = current_longest_path[i]; \n                                    int final_index = this->path_map[path_index][node_index];\n                                  \n                                    std::map<polyfp::compute *,int>::iterator it;\n                                    polyfp::compute *comp1;\n                                    for( it= this->leader_computation_index.begin();it!=this->leader_computation_index.end();it++) \n                                    {\n                                        if(it->second==final_index)\n                                        {\n                                            comp1 = it->first;\n                                            std::string name = comp1->get_name();\n                             \n                                            if (std::find(finish_list.begin(), finish_list.end(), name) == finish_list.end()){\n           \n                                                new_comp = comp1;\n                                                comp_flag = true;\n                                                \n                                                break;\n                                            }   \n                                        }\n                                            \n                                    } \n                                    if(comp_flag == true)\n                                    {\n                                        break;\n                                    }\n                                }        \n                                if(new_comp == NULL)\n                                {\n                                    return;\n                                }\n                                if(new_comp->get_name() != comp->get_name() && this->dsp_max>=this->dsp_usage)\n                                {\n                                   \n                                    this->best_latency = this->current_latency;\n                                    final_design = tile_size;\n                                    break;\n                                }else if(new_comp->get_name() == comp->get_name() &&this->current_latency < this->best_latency && this->dsp_max>= this->dsp_usage)\n                                {\n                                    this->best_latency = this->current_latency;\n                                    this->best_dsp_usage = this->dsp_usage;\n                                    current_design = tile_size;\n                                    auto latency = comp->latency;\n                                    int dsp = comp->dsp;\n                                }else{\n                                   // TODO\n                                }\n                                auto latency = comp->latency;\n                                    int dsp = comp->dsp;\n                            }else{\n                                new_comp = comp;\n                                if(new_comp->get_name() == comp->get_name() &&this->current_latency < this->best_latency && this->dsp_max>= this->dsp_usage)\n                                {\n                                    this->best_latency = this->current_latency;\n                                    this->best_dsp_usage = this->dsp_usage;\n                                    current_design = tile_size;\n                                    auto latency = comp->latency;\n                                    int dsp = comp->dsp;\n                                   \n                                }else\n                                {\n                                    // TODO\n                                }\n                                auto latency = comp->latency;\n                                    int dsp = comp->dsp;\n                            }\n                            \n                               \n                        }\n                     \n                    }\n                \n               \n                auto latency = comp->latency;\n                int dsp = comp->dsp;\n                \n               // TODO\n                myfile << tile_size[0] << \",\";\n                myfile << tile_size[1] << \",\";\n                myfile << latency<< \",\";\n                myfile << this->dsp_usage << \"\\n\";\n\n            }\n            \n\n            \n        }\n        \n        if(final_design.size()!=0)\n        {               \n            comp->final_strategy = final_design;\n            comp->current_strategy = final_design;\n            comp->apply_opt_strategy(comp->final_strategy);\n            this->evaluate_func();\n            auto new_comp = this->update_latency();\n            auto_DSE_tile_size(new_comp, 1,path);\n        }else if(current_design.size()!=0)\n        {\n            comp->current_strategy = current_design;\n            auto_DSE_tile_size(comp, 1,path);\n        }else if(current_design.size()==0||comp->current_factor == comp->largest_factor)\n        {\n            comp->opt_finished = true;\n            auto_DSE_tile_size(comp, 1,path);\n\n        }\n        myfile.close();\n    }\n}\n\n\nbool cmp_value(const std::pair<int, long> left, const std::pair<int,long> right)\n{\n\treturn left.second < right.second;\n}\n\nint polyfp::function::get_longest_path()\n{\n    auto i= std::max_element(this->all_latency_map.begin(),this->all_latency_map.end(),cmp_value);\n    return i->first;\n}\n\nint polyfp::function::get_longest_node(std::vector<long> path)\n{\n    long max_latency = 0;\n    long index = 0;\n    for(int j=0; j<path.size(); j++)\n    {\n        if(max_latency < this->latency_map[path[j]]){\n            max_latency = this->latency_map[path[j]];\n            index = j;\n        }\n    }\n    // std::cout<<\"longest node: \"+std::to_string(max_latency)+\";\"+std::to_string(index)<<std::endl;\n    return index;\n}\npolyfp::compute * polyfp::function::update_latency(){\n\n    for(int i=0; i<this->paths.size(); i++)\n    {\n        std::string result = \"Latency of path:\";\n        long sum = 0;\n        std::vector<int> node_list;\n        for(int j=0; j<this->paths[i].size(); j++)\n        {\n            result += std::to_string(this->latency_map[this->paths[i][j]]);\n            result += \";\";\n            sum+=this->latency_map[this->paths[i][j]];\n            node_list.push_back(this->paths[i][j]);\n            \n        }\n        this->path_map.insert(std::make_pair(i,node_list));\n        result+=std::to_string(sum);\n        this->all_latency_map[i] = sum;\n    }\n    // std::cout<<\"this->all_latency_map.size()\"<<std::endl;\n    // std::cout<<this->all_latency_map.size()<<std::endl;\n    // for(auto &pair:this->all_latency_map ){\n    //     std::cout<<pair.first;\n    //     std::cout<<\", \";\n    //     std::cout<<pair.second<<std::endl;\n\n    // }\n    polyfp::compute *comp;\n    if(this->all_latency_map.size()!=0)\n    {\n        int path_index = this->get_longest_path();\n        int node_index = this->get_longest_node(this->paths[path_index]);\n        int final_index = this->path_map[path_index][node_index];\n        this->longest_path = path_index;\n        this->longest_node = node_index;\n        // std::cout<<\"path: \";\n        // std::cout<<path_index<<std::endl;\n        // std::cout<<\"node: \";\n        // std::cout<<node_index<<std::endl;\n        std::map<polyfp::compute *,int>::iterator it;\n        \n        for( it= this->leader_computation_index.begin();it!=this->leader_computation_index.end();it++) \n        {\n            if(it->second==final_index)\n                comp = it->first;\n        } \n    }\n    else\n    {\n        comp = this->get_body()[0];\n    }\n    return comp;\n\n}\n\n\nvoid polyfp::function::codegen()\n{   \n    for(auto &comp:this->get_body())\n    {\n        std::vector<std::string> current_name_list = comp->get_loop_level_names();\n        int final_size = comp->final_loop_level_names.size();\n        int current_size = current_name_list.size();\n        if(final_size == current_size)\n        {\n            comp->final_loop_level_names = current_name_list;\n            comp->final_loop_level_names_reserved = current_name_list;\n        }else if(final_size < current_size)\n        {\n            for(int i=0; i<final_size; i++)\n            {\n                comp->final_loop_level_names[i] = current_name_list[i];\n                comp->final_loop_level_names_reserved[i] = current_name_list[i];\n            }\n        }\n    }\n\n    this->gen_loop_location();\n    this->gen_time_space_domain();\n    this->gen_isl_ast();\n    this->gen_c_code();\n    this->gen_mlir_stmt();\n\n}\n\n\n\nvoid polyfp::function::gen_c_code() const\n{\n    polyfp::str_dump(\"\\n\\n\");\n    polyfp::str_dump(\"\\nC like code:\\n\");\n    isl_printer *p;\n    p = isl_printer_to_file(this->get_isl_ctx(), stdout);\n    p = isl_printer_set_output_format(p, ISL_FORMAT_C);\n    p = isl_printer_print_ast_node(p, this->get_isl_ast());\n    isl_printer_free(p);\n    polyfp::str_dump(\"\\n\\n\");\n}\n\n       \n}\n\n\n\n\n\n\n\n\n\n\n\n"
  },
  {
    "path": "lib/polyhedral/generator.cpp",
    "content": "#include \"generator.h\"\n#include <string>\n#include <iostream>\n#include <filesystem>\n#include \"scalehls/Transforms/Passes.h\"\n#include \"scalehls/Transforms/Utils.h\"\n#include \"scalehls/Transforms/Estimator.h\"\n#include \"llvm/Support/MemoryBuffer.h\"\n#include \"mlir/Dialect/Affine/Analysis/Utils.h\"\n#include \"mlir/Dialect/Affine/IR/AffineValueMap.h\"\n#include \"mlir/Transforms/GreedyPatternRewriteDriver.h\"\n#include \"mlir/Support/FileUtilities.h\"\n#include \"llvm/ADT/SmallPtrSet.h\"\n#include \"llvm/Support/MemoryBuffer.h\"\n\nnamespace polyfp{\n\npolyfp::compute *get_computation_annotated_in_a_node(isl_ast_node *node)\n{\n    // Retrieve the computes of the node.\n    isl_id *comp_id = isl_ast_node_get_annotation(node);\n    polyfp::compute *comp = (polyfp::compute *)isl_id_get_user(comp_id);\n    isl_id_free(comp_id);\n    return comp;\n    \n}\n\nstd::map<std::string,mlir::Value> polyfp::MLIRGenImpl::get_argument_map(){\n    return this->argument_map;\n}\n\nstd::map<std::string,int> polyfp::MLIRGenImpl::get_array_map(){\n    return this->array_map;\n}\n\n\nstd::vector<mlir::FuncOp>  polyfp::MLIRGenImpl::get_funcs(){\n    return this->funcs;\n}\n\n//TODO: find out what is the acutal \"loc\"\nint polyfp::MLIRGenImpl::get_iterator_location_from_name(polyfp::compute *comp, polyfp::expr polyfp_expr, std::vector<mlir::Value> &index_values)\n{\n    auto name_set = comp->get_loop_level_names();\n    int loc;\n    if (std::find(name_set.begin(), name_set.end(), polyfp_expr.get_name()) == name_set.end() )\n    {\n        for (auto &kv2: comp->get_access_map())\n        {\n            if(polyfp_expr.get_name()==kv2.first)\n            {\n                // loc = comp->get_loop_level_number_from_dimension_name(kv2.second); \n                loc = comp->iterators_location_map[kv2.second];      \n            }\n        }\n\n        mlir::Value value = ops[loc].getInductionVar();\n\n        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end())\n        {\n            index_values.push_back(value);\n        }\n\n        loc = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n    }\n    else{\n        loc = comp->iterators_location_map[polyfp_expr.get_name()];      \n        mlir::Value value = ops[loc].getInductionVar();\n\n        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end())\n        {\n            index_values.push_back(value);\n        }\n\n        loc = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n    }\n    return loc;\n}\n\nmlir::ModuleOp polyfp::MLIRGenImpl::mlirGen1(const polyfp::function &fct, isl_ast_node *isl_node, int &level, bool flag, bool flag2, bool if_flag) \n{\n    std::vector<std::pair<std::string, std::string>> generated_stmts;\n    isl_ast_node *node=isl_node;\n    if (isl_ast_node_get_type(node) == isl_ast_node_for)\n    {\n        isl_ast_expr *iter = isl_ast_node_for_get_iterator(node);\n        isl_id *identifier = isl_ast_expr_get_id(iter);\n        std::string name_str(isl_id_get_name(identifier));\n        name_map.insert(std::pair(level,name_str));\n        isl_ast_expr *init = isl_ast_node_for_get_init(node);\n        // std::cout<<isl_ast_expr_to_str(init)<<std::endl;\n        isl_ast_expr *cond = isl_ast_node_for_get_cond(node);\n        isl_ast_expr *inc = isl_ast_node_for_get_inc(node);\n        isl_ast_node *body = isl_ast_node_for_get_body(node);\n        int lb_int;\n        int ub_int;\n        bool vbound_flag = false;\n        std::vector<mlir::Value> lb_values;\n        std::vector<mlir::AffineExpr> lb_args;\n        std::vector<mlir::Value> ub_values;\n        std::vector<mlir::AffineExpr> ub_args;\n\n        isl_ast_expr *cond_upper = isl_ast_expr_get_op_arg(cond, 1);\n\n        if (isl_ast_expr_get_type(init) == isl_ast_expr_int)\n        {\n            lb_int = isl_val_get_num_si(isl_ast_expr_get_val(init));\n        }\n        else if (isl_ast_expr_get_type(init) == isl_ast_expr_op)\n        {\n            // std::cout<<\"isl_ast_expr_op \"<<std::endl;\n            vbound_flag = true;\n            int nb = isl_ast_expr_get_op_n_arg(init);\n            for(int i=0;i<nb;i++)\n            {   \n                isl_ast_expr *expr_itr = isl_ast_expr_get_op_arg(init, i);\n                if (isl_ast_expr_get_type(expr_itr) == isl_ast_expr_int)\n                {\n                    lb_args.push_back(getAffineConstantExpr(isl_val_get_num_si(isl_ast_expr_get_val(expr_itr)), builder.getContext()));\n                }\n                else if (isl_ast_expr_get_op_type(expr_itr) == isl_ast_op_sub)\n                {\n                    // std::cout<<\"isl_ast_op_sub \"<<std::endl;\n                    int nb = isl_ast_expr_get_op_n_arg(init);\n                    \n                    isl_ast_expr *expr0 = isl_ast_expr_get_op_arg(expr_itr, 0);\n                    isl_ast_expr *expr1 = isl_ast_expr_get_op_arg(expr_itr, 1);\n                    int sub1;\n                    int div1 =1;\n                    // isl_ast_expr_to_C_str(expr0);\n                    \n                    if (isl_ast_expr_get_type(expr0) == isl_ast_expr_id)\n                    {\n                        isl_id *identifier = isl_ast_expr_get_id(expr0);\n                        std::string name_str(isl_id_get_name(identifier));\n                        int loc;\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        for (auto &kv4: name_map)\n                        {\n                            if(name_str==kv4.second)\n                            {\n                                loc = kv4.first;\n                            }\n                        }\n\n                        mlir::Value value = ops[loc+index].getInductionVar();\n                        lb_values.push_back(value);\n                        isl_id_free(identifier);\n                    }\n                    else\n                    {\n                        int nb = isl_ast_expr_get_op_n_arg(expr0);\n                        \n                        isl_ast_expr *expr0_0 = isl_ast_expr_get_op_arg(expr0, 0);\n                        isl_ast_expr *expr1_0 = isl_ast_expr_get_op_arg(expr0, 1);\n                        if (isl_ast_expr_get_type(expr0_0) == isl_ast_expr_id)\n                        {\n                            isl_id *identifier = isl_ast_expr_get_id(expr0_0);\n                            std::string name_str(isl_id_get_name(identifier));\n                            int loc;\n                            int index = 0;\n                            for(int i=0; i<start_loops_position.size(); i++)\n                            {\n                                if(start_loops_position[i]>level)\n                                {\n                                    index = start_loops_position[i-1];\n                                    break;\n                                }\n                                if(i == start_loops_position.size()-1 )\n                                {\n                                    index = start_loops_position[i];\n                                    break;\n                                }\n                            }\n                            for (auto &kv4: name_map)\n                            {\n                                if(name_str==kv4.second)\n                                {\n                                    loc = kv4.first;\n                                }\n                            }\n                            mlir::Value value = ops[loc+index].getInductionVar();\n                            lb_values.push_back(value);\n                            isl_id_free(identifier);\n                        }\n                        if (isl_ast_expr_get_type(expr1) == isl_ast_expr_int)\n                        {   \n                            div1 = isl_val_get_num_si(isl_ast_expr_get_val(expr1_0));\n                        }\n\n                    }\n                    if (isl_ast_expr_get_type(expr1) == isl_ast_expr_int)\n                    {   \n                        sub1 = isl_val_get_num_si(isl_ast_expr_get_val(expr1));\n                    }\n                    else{\n                        // TODO\n                    } \n                    //TODO: find right dimensions\n                    lb_args.push_back(builder.getAffineDimExpr(0).floorDiv(div1) - sub1);\n                    \n                }\n                else if(isl_ast_expr_get_op_type(expr_itr) == isl_ast_expr_op)\n                {\n                    // std::cout<<\"a division\"<<std::endl;\n                    // TODO\n                }\n               \n                else{\n                    polyfp::str_dump(\"Transforming the following expression\",\n                           (const char *)isl_ast_expr_to_C_str(expr_itr));\n                }     \n                isl_ast_expr_free(expr_itr);\n            }\n        }\n        auto lb_map = mlir::AffineMap::get(1, 0, ArrayRef<mlir::AffineExpr> (lb_args),builder.getContext());\n        mlir::ValueRange lb_vr=llvm::makeArrayRef(lb_values);  \n        if (isl_ast_expr_get_type(cond_upper) == isl_ast_expr_int)\n        {\n            ub_int = isl_val_get_num_si(isl_ast_expr_get_val(cond_upper))+1;\n        }\n        else if (isl_ast_expr_get_type(cond_upper) == isl_ast_expr_op)\n        {\n            // std::cout<<\"upper bound\"<<std::endl;\n            vbound_flag = true;\n            int nb = isl_ast_expr_get_op_n_arg(cond_upper);\n            int sub1;\n            int div1=1;\n            for(int i=0;i<nb;i++)\n            {   \n                isl_ast_expr *expr_itr = isl_ast_expr_get_op_arg(cond_upper, i);\n                if (isl_ast_expr_get_type(expr_itr) == isl_ast_expr_int)\n                {\n                    ub_args.push_back(getAffineConstantExpr(isl_val_get_num_si(isl_ast_expr_get_val(expr_itr))+1, builder.getContext()));\n                }\n                else if (isl_ast_expr_get_op_type(expr_itr) == isl_ast_op_sub)\n                {\n                    isl_ast_expr *expr0 = isl_ast_expr_get_op_arg(expr_itr, 0);\n                    isl_ast_expr *expr1 = isl_ast_expr_get_op_arg(expr_itr, 1);\n                    int add1;\n                    if (isl_ast_expr_get_type(expr0) == isl_ast_expr_id)\n                    {\n                        isl_id *identifier = isl_ast_expr_get_id(expr0);\n                        std::string name_str(isl_id_get_name(identifier));\n                        int loc;\n                        for (auto &kv4: name_map)\n                        {\n                            if(name_str==kv4.second)\n                            {\n                                loc = kv4.first;\n                            }\n                        }\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        mlir::Value value = ops[loc+index].getInductionVar();\n                        ub_values.push_back(value);\n                        isl_id_free(identifier);\n                    }  \n                    if (isl_ast_expr_get_type(expr1) == isl_ast_expr_int)\n                    {   \n                        add1 = isl_val_get_num_si(isl_ast_expr_get_val(expr1))+1;\n                    }  \n                    //TODO: find right dimensions\n                    ub_args.push_back(builder.getAffineDimExpr(0) - add1);\n                    \n                }\n                else if (isl_ast_expr_get_type(expr_itr) == isl_ast_expr_id)\n                {\n                    isl_id *identifier = isl_ast_expr_get_id(expr_itr);\n                    std::string name_str(isl_id_get_name(identifier));\n                    int loc;\n                    for (auto &kv4: name_map)\n                    {\n                        if(name_str==kv4.second)\n                        {\n                            loc = kv4.first;\n                        }\n                    }\n                    int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                    mlir::Value value = ops[loc+index].getInductionVar();\n                    ub_values.push_back(value);\n                    isl_id_free(identifier);\n                    ub_args.push_back(builder.getAffineDimExpr(0));\n                }\n                else if(isl_ast_expr_get_type(expr_itr) == isl_ast_expr_op)\n                {\n                    int nb = isl_ast_expr_get_op_n_arg(expr_itr);\n                    isl_ast_expr *expr0_0 = isl_ast_expr_get_op_arg(expr_itr, 0);\n                    isl_ast_expr *expr1_0 = isl_ast_expr_get_op_arg(expr_itr, 1);\n                    if (isl_ast_expr_get_type(expr0_0) == isl_ast_expr_id)\n                    {\n                        isl_id *identifier = isl_ast_expr_get_id(expr0_0);\n                        std::string name_str(isl_id_get_name(identifier));\n                        int loc;\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        for (auto &kv4: name_map)\n                        {\n                            if(name_str==kv4.second)\n                            {\n                                loc = kv4.first;\n                            }\n                        }\n                        mlir::Value value = ops[loc+index].getInductionVar();\n                        ub_values.push_back(value);\n                        isl_id_free(identifier);\n\n                    }\n                    if (isl_ast_expr_get_type(expr1_0) == isl_ast_expr_int)\n                    {   \n                        div1 = isl_val_get_num_si(isl_ast_expr_get_val(expr1_0));\n                    }\n                    ub_args.push_back(builder.getAffineDimExpr(0).floorDiv(div1));\n\n                }\n                else\n                {\n                    polyfp::str_dump(\"Transforming the following expression\",\n                           (const char *)isl_ast_expr_to_C_str(expr_itr));\n                }     \n                isl_ast_expr_free(expr_itr);\n                \n            }\n        }\n        auto ub_map = mlir::AffineMap::get(1, 0, ArrayRef<mlir::AffineExpr> (ub_args),builder.getContext());\n        mlir::ValueRange ub_vr=llvm::makeArrayRef(ub_values);\n        int step = isl_val_get_num_si(isl_ast_expr_get_val(inc));\n        if (isl_ast_node_get_type(node) == isl_ast_node_for)\n        {\n            if(level== 0)\n            {\n                start_loops_position.push_back(level);\n                std::vector<mlir::Type> types;\n                std::string name = polyfp::global::get_implicit_function()->get_name();\n                mlir::Location loc = builder.getUnknownLoc();\n                auto varLoc = loc;\n                llvm::SmallVector<mlir::Type, 4> operandTypes;\n                llvm::SmallVector<mlir::Type, 4> operandTypes_temp;\n                llvm::SmallVector<mlir::Type, 4> operandTypes_arg;\n                mlir::Type t;\n                mlir::MemRefType mr;\n                for (auto &kv : fct.get_invariants()) \n                {\n                    //TODO: more datatype\n                    if(kv.second->get_type() == polyfp::p_float64)\n                    {\n                        t= builder.getF64Type();\n                    }\n                    if(kv.second->get_type() == polyfp::p_float32)\n                    {\n                        t= builder.getF32Type();  \n                    }\n                    if(kv.second->get_type() == polyfp::p_int32)\n                    {\n                        t= builder.getIntegerType(32); \n                    }\n                    operandTypes.push_back(t);\n                    argument_list.push_back(kv.first);\n                    operandTypes_temp.push_back(mr);\n                }\n                std::vector<std::string> p_names;\n\n                for (auto &kv : fct.get_fct_arguments()) \n                {\n                    unsigned memspace = 0;\n                    auto size = kv.second->get_dim_sizes();\n                    auto arg_type = kv.second->get_elements_type();\n                    p_names.push_back(kv.first);\n                    if (arg_type == polyfp::p_uint8 || arg_type == polyfp::p_int8)\n                    {\n                        t= builder.getIntegerType(8);\n                    }else if(arg_type == polyfp::p_uint16 || arg_type == polyfp::p_int16)\n                    {\n                        t= builder.getIntegerType(16);\n                    }else if(arg_type == polyfp::p_uint32 || arg_type == polyfp::p_int32)\n                    {\n                        t= builder.getIntegerType(32);\n                    }else if(arg_type == polyfp::p_uint64 || arg_type == polyfp::p_int64)\n                    {\n                        t= builder.getIntegerType(64);\n                    }else if(arg_type == polyfp::p_float32)\n                    {\n                        t= builder.getF32Type();\n                    }else if(arg_type == polyfp::p_float64)\n                    {\n                        t= builder.getF64Type();\n                    }\n                    mr = mlir::MemRefType::get(llvm::makeArrayRef(size), t, {}, memspace);\n                    operandTypes.push_back(mr);\n\n                    argument_list.push_back(kv.first);\n \n                    array_map.insert(std::make_pair(kv.first,operandTypes.size()-1));\n                    \n                }\n\n                for (auto &kv : fct.get_placeholders())\n                {\n                     unsigned memspace = 0;\n                     auto size = kv.second->get_dim_sizes();\n                     auto arg_type = kv.second->get_elements_type();\n                    //  p_names.push_back(kv.first);\n                     if (arg_type == polyfp::p_uint8 || arg_type == polyfp::p_int8)\n                     {\n                         t= builder.getIntegerType(8);\n                     }else if(arg_type == polyfp::p_uint16 || arg_type == polyfp::p_int16)\n                     {\n                         t= builder.getIntegerType(16);\n                     }else if(arg_type == polyfp::p_uint32 || arg_type == polyfp::p_int32)\n                     {\n                         t= builder.getIntegerType(32);\n                     }else if(arg_type == polyfp::p_uint64 || arg_type == polyfp::p_int64)\n                     {\n                         t= builder.getIntegerType(64);\n                     }else if(arg_type == polyfp::p_float32)\n                     {\n                         t= builder.getF32Type();\n                     }else if(arg_type == polyfp::p_float64)\n                     {\n                         t= builder.getF64Type();\n                     }\n                     mr = mlir::MemRefType::get(llvm::makeArrayRef(size), t, {}, memspace);\n                    //  auto op = builder.create<mlir::memref::AllocaOp>(loc, mr);\n                    //  op.dump();\n                     operandTypes_temp.push_back(mr);\n                    //  argument_list.push_back(kv.first);\n                    //  array_map.insert(std::make_pair(kv.first,operandTypes_temp.size()-1));\n                }\n\n                for (auto &kv : fct.get_global_arguments()) \n                {\n                     unsigned memspace = 0;\n                     auto size = kv.second->get_dim_sizes();\n                     auto arg_type = kv.second->get_elements_type();\n                     p_names.push_back(kv.first);\n                     if (arg_type == polyfp::p_uint8 || arg_type == polyfp::p_int8)\n                     {\n                         t= builder.getIntegerType(8);\n                     }else if(arg_type == polyfp::p_uint16 || arg_type == polyfp::p_int16)\n                     {\n                         t= builder.getIntegerType(16);\n                     }else if(arg_type == polyfp::p_uint32 || arg_type == polyfp::p_int32)\n                     {\n                         t= builder.getIntegerType(32);\n                     }else if(arg_type == polyfp::p_uint64 || arg_type == polyfp::p_int64)\n                     {\n                         t= builder.getIntegerType(64);\n                     }else if(arg_type == polyfp::p_float32)\n                     {\n                         t= builder.getF32Type();\n                     }else if(arg_type == polyfp::p_float64)\n                     {\n                         t= builder.getF64Type();\n                     }\n                     mr = mlir::MemRefType::get(llvm::makeArrayRef(size), t, {}, memspace);\n                    //  auto op = builder.create<mlir::memref::AllocaOp>(loc, mr);\n                    //  op.dump();\n                     operandTypes_arg.push_back(mr);\n                     \n                     argument_list.push_back(kv.first);\n                     array_map.insert(std::make_pair(kv.first,operandTypes_temp.size()-1));\n                }\n      \n                if(flag ==true)\n                {\n\n                    mlir::FuncOp myFunc = mlir::FuncOp::create(loc, /*name=*/name, /*type=*/builder.getFunctionType(operandTypes, {}), /*attrs=*/{}); \n                    auto &entryBlock = *myFunc.addEntryBlock();\n                    builder.setInsertionPointToStart(&entryBlock);\n                    funcs.push_back(myFunc);\n                    theModule.push_back(myFunc);\n\n                    int index_pname = 0;\n                    int index_pname2 = 0;\n                  \n                    for(auto &arg: operandTypes_arg)\n                    {\n                        mlir::MemRefType mr = arg.dyn_cast<mlir::MemRefType>();\n                        auto value = builder.create<mlir::memref::AllocaOp>(loc, mr);\n                        values.push_back(value);\n\n                    }\n                    for(auto &p_name: p_names)\n                    {\n                        if(index_pname<=fct.get_fct_arguments().size())\n                        {\n                            auto mem = myFunc.getArgument(index_pname);\n                            index_pname+=1;\n                            argument_map.insert(std::pair(p_name,mem));\n                        }\n                        else\n                        {\n                            auto mem = values[index_pname2];\n                            index_pname2+=1;\n                            argument_map.insert(std::pair(p_name,mem));\n                        }\n                    }\n                    // theModule.dump();\n                    // mlir::MemRefType mr = operandTypes_temp[0].dyn_cast<mlir::MemRefType>();\n                    // auto op = builder.create<mlir::memref::AllocaOp>(loc, mr);\n                    // for(auto &)\n                    //example of builder.getI16IntegerAttr(5)\n                    // mlir::Value arg1 = builder.create<mlir::arith::ConstantOp>(builder.getUnknownLoc(),t, builder.getF32ArrayAttr(3.0));\n                    // mlir::Value arg2 = builder.create<mlir::arith::ConstantOp>(builder.getUnknownLoc(),t, builder.getF32ArrayAttr(6.7));\n                    if(vbound_flag == false)\n                    {\n                        auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_int, ub_int, step);\n                        ops.push_back(loop);\n                        builder.setInsertionPointAfter(ops[0]);\n                        auto return_op = builder.create<mlir::func::ReturnOp>(builder.getUnknownLoc(), ArrayRef<mlir::Value>());\n                        builder.setInsertionPointToStart(loop.getBody());\n                    }else\n                    {\n                        auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_vr, lb_map, ub_vr, ub_map, step);\n                        ops.push_back(loop);\n                        builder.setInsertionPointAfter(ops[0]);\n                        auto return_op = builder.create<mlir::func::ReturnOp>(builder.getUnknownLoc(), ArrayRef<mlir::Value>());\n                        builder.setInsertionPointToStart(loop.getBody());\n                    }\n                }else{\n                    if(vbound_flag == false)\n                    {\n                        auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_int, ub_int, step);\n                        ops.push_back(loop);\n                        builder.setInsertionPointAfter(ops[0]);\n                        builder.setInsertionPointToStart(loop.getBody());\n                    }\n                    else\n                    {\n                        auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_vr, lb_map, ub_vr, ub_map, step);\n                        ops.push_back(loop);\n                        builder.setInsertionPointAfter(ops[0]);\n                        builder.setInsertionPointToStart(loop.getBody());\n                    }\n                }  \n            }\n            else \n            {\n                if(vbound_flag == false)\n                {\n                    auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_int, ub_int, step);\n                    ops.push_back(loop);\n                    builder.setInsertionPointAfter(ops[0]);\n                    builder.setInsertionPointToStart(loop.getBody());\n                }\n                else\n                {\n                    auto loop = builder.create<mlir::AffineForOp>(builder.getUnknownLoc(), lb_vr, lb_map, ub_vr, ub_map, step);\n                    ops.push_back(loop);\n                    builder.setInsertionPointAfter(ops[0]);\n                    builder.setInsertionPointToStart(loop.getBody());\n                }\n            }\n        }\n        isl_ast_expr_free(init);\n        isl_ast_expr_free(cond);\n        isl_ast_expr_free(inc);\n        isl_ast_node_free(body);\n        isl_ast_expr_free(cond_upper);\n\n        if (isl_ast_node_get_type(body) == isl_ast_node_for)\n        {   \n            level = level+1;\n            mlirGen1(fct,body,level,false,flag2,if_flag);\n        }\n\n        if (isl_ast_node_get_type(body) == isl_ast_node_user)\n        {\n            mlirGen1(fct,body,level,false,flag2,if_flag);\n        }\n\n        else if(isl_ast_node_get_type(body) == isl_ast_node_block)\n        {\n            mlirGen1(fct,body,level,false,flag2,if_flag);\n        }\n\n        else if(isl_ast_node_get_type(body) == isl_ast_node_if)\n        {\n            // TODO\n        }\n        else{\n            // TODO\n        }\n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_block)\n    {\n        // std::cout<<\"enter a block node\"<<std::endl;\n        isl_ast_node_list *list = isl_ast_node_block_get_children(node);\n        int current_level = level;\n        int children_number = isl_ast_node_list_n_ast_node(list);\n        // theModule.dump();\n        // std::cout<<\"number of children: \";\n        // std::cout<<children_number<<std::endl;\n        //TODO: current start_loops_position maybe not safe enough, find another way to build it\n\n        if(this->ops.size() == 0)\n        {\n            for (int i = 0; i <=isl_ast_node_list_n_ast_node(list) - 1; i++)\n            {   \n                isl_ast_node *child = isl_ast_node_list_get_ast_node(list, i);\n                if (isl_ast_node_get_type(child) == isl_ast_node_user)\n                {\n                    mlirGen1(fct,child,current_level,false,true,if_flag);\n                }\n                else if (isl_ast_node_get_type(child) == isl_ast_node_for)\n                {\n                    if(this->ops.size() != 0)\n                    {\n                        level = level + 1 ;\n                        int current_level = level;            \n                        start_loops_position.push_back(level);\n                        mlirGen1(fct,child,level,false,true,if_flag);\n                        builder.setInsertionPointAfter(ops[current_level]);\n                    }\n                    else{\n                        // start_loops_position.push_back(level);\n                        mlirGen1(fct,child,level,true, false, false);   \n                        builder.setInsertionPointAfter(ops[current_level]);     \n                    }\n                }\n                else if (isl_ast_node_get_type(child) == isl_ast_node_block)\n                {\n                    mlirGen1(fct,child,level,false,true,if_flag);\n                }else if (isl_ast_node_get_type(child) == isl_ast_node_if)\n                {\n                    mlirGen1(fct,child,level,false,true,if_flag);\n                }else{\n                    // TODO\n                }         \n            }\n        }\n        else\n        {\n            for (int i = 0; i <=isl_ast_node_list_n_ast_node(list) - 1; i++)\n            {   \n                isl_ast_node *child = isl_ast_node_list_get_ast_node(list, i);\n\n                if (isl_ast_node_get_type(child) == isl_ast_node_user)\n                {\n                    mlirGen1(fct,child,current_level,false,true,if_flag);\n                }\n                else if (isl_ast_node_get_type(child) == isl_ast_node_for)\n                {\n                    if(this->ops.size() != 0)\n                    {\n                        level = level + 1 ;\n                        int current_level = level;            \n                        // start_loops_position.push_back(level);\n                        mlirGen1(fct,child,level,false,true,if_flag);\n                        builder.setInsertionPointAfter(ops[current_level]);\n                    }\n                    else\n                    {\n                        // start_loops_position.push_back(level);\n                        mlirGen1(fct,child,level,true, false, false);   \n                        builder.setInsertionPointAfter(ops[current_level]);     \n                    }\n                }\n                else if (isl_ast_node_get_type(child) == isl_ast_node_block)\n                {\n                    mlirGen1(fct,child,level,false,true,if_flag);\n                }\n                else if (isl_ast_node_get_type(child) == isl_ast_node_if)\n                {\n                    mlirGen1(fct,child,level,false,true,if_flag);\n                }\n                else{\n                     // TODO\n                }         \n            }\n        }\n        \n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_user)\n    {\n        bool flag = true;\n        isl_ast_expr *expr = isl_ast_node_user_get_expr(node);\n        isl_ast_expr *arg = isl_ast_expr_get_op_arg(expr, 0);\n        isl_id *id = isl_ast_expr_get_id(arg);\n        std::string computation_name(isl_id_get_name(id));\n        isl_id_free(id);\n        polyfp::compute *comp;\n        int dim_number = 0;\n        for (const auto &cpt : fct.get_body())\n        {\n            if(cpt->get_name()==computation_name)\n            {\n                comp = cpt;\n            }\n            if(dim_number < cpt->get_loop_levels_number())\n                dim_number = cpt->get_loop_levels_number();\n        }\n        auto polyfp_expr = comp->get_expr();\n\n        std::string p_name = comp->get_placeholder()->get_name();\n\n        int index_placeholder;\n        int index_argument;\n\n        for(int i=0; i<argument_list.size(); i++)\n        {\n            if(argument_list.at(i) == p_name)\n            {\n                index_placeholder = i;\n\n            }\n        }\n        std::vector<mlir::Value> placeholder_index_values;\n        SmallVector<mlir::AffineExpr> placeholder_index_args;\n        bool placeholder_index_flag = false;\n        int count = 0;\n\n        for (auto &kv: comp->get_placeholder_dims())\n        {\n            int bias = 0;\n            if(kv.get_expr_type() == polyfp::e_op)\n            {\n                //TODO, HANDLE loop skewing\n                auto expr0 = kv.get_operand(0);\n                auto expr1 = kv.get_operand(1);\n                auto left_index = a_print_index(expr0,comp,placeholder_index_values,level);\n                auto right_index = a_print_index(expr1,comp,placeholder_index_values,level);\n\n                if(kv.get_op_type() == polyfp::o_sub)\n                {\n                    placeholder_index_args.push_back(left_index - right_index);\n                    placeholder_index_flag = true;\n                }\n\n                else if(kv.get_op_type() == polyfp::o_add)\n                {\n                    placeholder_index_args.push_back(left_index + right_index);\n                    placeholder_index_flag = true;\n                }\n                \n                else if(kv.get_op_type() == polyfp::o_mul)\n                {\n                    placeholder_index_args.push_back(left_index * right_index);\n                    placeholder_index_flag = true;    \n                }\n\n                else if(kv.get_op_type() == polyfp::o_div)\n                {\n                    placeholder_index_args.push_back(left_index.floorDiv(right_index));\n                    placeholder_index_flag = true;  \n                }\n            }\n            else{\n                int loc =0;\n                int loc_2 =0;\n                std::string tile_name1;\n                std::string tile_name2;\n                int tile_size;\n                auto name_set = comp->get_loop_level_names(); \n                // std::cout<<kv.get_name()<<std::endl;      \n                if(std::find(name_set.begin(), name_set.end(), kv.get_name()) == name_set.end())\n                {           \n                    for (auto &kv2: comp->get_access_map())\n                    {\n                        if(kv.get_name()==kv2.first)\n                        {\n                            tile_name1 = kv2.second;\n                            loc = comp->iterators_location_map[tile_name1];\n                           \n                        }\n                    }\n                    mlir::Value value = ops[loc].getInductionVar();\n                    if ( std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value)== placeholder_index_values.end() )\n                    {    \n                        placeholder_index_values.push_back(value);\n                    }\n                    \n                    if(comp->is_tiled == true)\n                    {\n                        for (auto &kv3: comp->get_tile_map())\n                        {\n                            if(tile_name1==kv3.first)\n                            {\n                                // loc_2 = comp->get_loop_level_number_from_dimension_name(kv3.second);\n                                loc = comp->iterators_location_map[kv3.second];\n                                tile_name2 = kv3.second;\n                            }\n                        }\n                        for (auto &kv4: comp->get_tile_size_map())\n                        {\n                            if(tile_name1==kv4.first)\n                            {\n                                tile_size = kv4.second;\n                            }\n                        }\n                        mlir::Value value2 = ops[loc].getInductionVar();\n                        if ( std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value2)== placeholder_index_values.end() )\n                        {\n                            placeholder_index_values.push_back(value2);\n                        }\n                        //TODO: find the right dim\n                        int index_2 = std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value2) - placeholder_index_values.begin();\n                        int index_3 = std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value) - placeholder_index_values.begin();\n                        placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size);\n                        placeholder_index_flag = true;\n                    }\n                    else if(comp->is_skewed == true)\n                    {\n                        loc = comp->iterators_location_map[comp->iterator_to_skew];\n                        mlir::Value value2 = ops[loc].getInductionVar();\n                        if ( std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value2)== placeholder_index_values.end() )\n                        {\n                            placeholder_index_values.push_back(value2);\n                        }\n                        //TODO: find the right dim\n                        int index_2 = std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value2) - placeholder_index_values.begin();\n                        int index_3 = std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value) - placeholder_index_values.begin();\n                        // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                        // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                        if(tile_name1 == comp->iterator_to_modify)\n                        {\n                            placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                        }else\n                        {\n                            placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                        }\n                        \n                        placeholder_index_flag = true;\n                    }\n                }\n                else{\n                    loc = comp->iterators_location_map[kv.get_name()];\n                    mlir::Value value = ops[loc].getInductionVar();\n                    if (std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value) == placeholder_index_values.end())\n                    {\n                        placeholder_index_values.push_back(value);\n                    }\n                    //TODO:\n                    int index2 = std::find(placeholder_index_values.begin(), placeholder_index_values.end(), value) - placeholder_index_values.begin();\n                    placeholder_index_args.push_back(builder.getAffineDimExpr(index2));\n                }\n            }\n        }\n        \n        auto placeholder_map = mlir::AffineMap::get(placeholder_index_values.size(), 0, ArrayRef<mlir::AffineExpr> (placeholder_index_args),builder.getContext());\n        mlir::ValueRange placeholder_vr=llvm::makeArrayRef(placeholder_index_values);\n        mlir::Value mem;\n        if(index_placeholder+1<=funcs[0].getNumArguments())\n        {\n            mem = funcs[0].getArgument(index_placeholder);\n            argument_map.insert(std::pair(p_name,mem));\n        }\n        else{\n            mem = values[index_placeholder-funcs[0].getNumArguments()];\n            argument_map.insert(std::pair(p_name,mem));\n        }\n\n        if (polyfp_expr.get_expr_type() == polyfp::e_var)\n        {    \n\n            int index_argument;\n            std::string arg_name = polyfp_expr.get_name();\n            for(int i=0; i<argument_list.size(); i++)\n            {\n                if(argument_list.at(i) == arg_name)\n                {\n                index_argument = i;\n                }\n            }\n            mlir::Value arg_1;\n            if(index_argument+1<=funcs[0].getNumArguments())\n            {\n                arg_1 = funcs[0].getArgument(index_argument);\n            }else\n            {\n                arg_1 = values[index_argument-funcs[0].getNumArguments()];\n            }\n\n            // TODO, test all if cases\n            if(if_flag == true)\n            {\n                mlir::Value value = ops[2].getInductionVar();\n                SmallVector<mlir::Value, 4> ifOperands;\n                ifOperands.push_back(value);\n                SmallVector<mlir::AffineExpr, 4> ifExprs;\n                ifExprs.push_back(builder.getAffineDimExpr(0));\n                SmallVector<bool, 4> ifEqFlags;\n                ifEqFlags.push_back(true);\n                const auto condition = mlir::IntegerSet::get(1, 0, ifExprs, ifEqFlags);\n                auto ifOp =builder.create<mlir::AffineIfOp>(builder.getUnknownLoc(), condition, ifOperands,/*withElseRegion=*/false);\n                builder.setInsertionPointToStart(ifOp.getBody());\n                auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), arg_1, mem, placeholder_vr);\n                builder.setInsertionPointAfter(ifOp);\n            }\n            else\n            {\n                if(placeholder_index_flag == true)\n                {\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), arg_1, mem, placeholder_map, placeholder_vr);\n                    builder.setInsertionPointAfter(store1);\n                }else\n                {\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), arg_1, mem, placeholder_vr);\n                    builder.setInsertionPointAfter(store1);\n                }\n            }\n        }\n        else if (polyfp_expr.get_expr_type() == polyfp::e_op && polyfp_expr.get_op_type() != polyfp::o_access && polyfp_expr.get_op_type() != polyfp::o_max )\n        {            \n            mlir::ValueRange indices = {};\n            auto a = polyfp_expr.get_operand(0);\n            auto b = polyfp_expr.get_operand(1);\n            mlir::BlockArgument left;\n            mlir::BlockArgument right;\n            mlir::arith::MulFOp allocSize_m;\n            mlir::arith::AddFOp allocSize_a;\n            // theModule.dump();\n            a_print_expr(polyfp_expr, comp, level);\n\n            if(if_flag == true)\n            {\n                mlir::Value value = ops[2].getInductionVar();\n                SmallVector<mlir::Value, 4> ifOperands;\n                ifOperands.push_back(value);\n                SmallVector<mlir::AffineExpr, 4> ifExprs;\n                ifExprs.push_back(builder.getAffineDimExpr(0));\n                SmallVector<bool, 4> ifEqFlags;\n                ifEqFlags.push_back(true);\n                const auto condition = mlir::IntegerSet::get(1, 0, ifExprs, ifEqFlags);\n                auto ifOp =builder.create<mlir::AffineIfOp>(builder.getUnknownLoc(), condition, ifOperands,/*withElseRegion=*/false);\n                builder.setInsertionPointToStart(ifOp.getBody());\n                //TODO: other arithmetic? sub, o_div\n                auto the_op = all_current_op.back();\n                \n                auto index = the_op.index();\n                if(index==0)\n                {\n                    auto op_to_process = std::get<0>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }\n                else if(index==1)\n                {\n                    auto op_to_process = std::get<1>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }else if(index==2)\n                {\n                    auto op_to_process = std::get<2>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }else if(index==3)\n                {\n                    auto op_to_process = std::get<3>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }\n                builder.setInsertionPointAfter(ifOp);\n            }else{\n                auto the_op = all_current_op.back();\n                auto index = the_op.index();\n                if(index==0)\n                {\n                    auto op_to_process = std::get<0>(the_op);\n                    if(placeholder_index_flag == true)\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    } \n                    \n                }else if(index==1)\n                {\n                    auto op_to_process = std::get<1>(the_op);\n                    if(placeholder_index_flag == true)\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }else if(index==2)\n                {\n                    auto op_to_process = std::get<2>(the_op);\n                    if(placeholder_index_flag == true)\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                    else\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }else if(index==3)\n                {\n                    auto op_to_process = std::get<3>(the_op);\n                    if(placeholder_index_flag == true)\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                    else\n                    {\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }\n            }\n        }\n        else if(polyfp_expr.get_expr_type() == polyfp::e_op && polyfp_expr.get_op_type() == polyfp::o_access )\n        {\n            std::string a_name = polyfp_expr.get_name();\n            int index_a;\n            // if(comp->refused == true){\n            //     a_name = comp->temp_access_map[a_name];\n            // }\n            for(int i=0; i<argument_list.size(); i++)\n            {\n                if(argument_list.at(i) == a_name)\n                    index_a = i;               \n            }\n            // auto arg_a = funcs[0].getArgument(index_a);\n            mlir::Value arg_a;\n\n            if(index_a+1<=funcs[0].getNumArguments())\n            {\n                arg_a = funcs[0].getArgument(index_a);\n            }else\n            {\n                arg_a = values[index_a-funcs[0].getNumArguments()];\n            }\n            \n            std::vector<mlir::Value> index_values;\n            SmallVector<mlir::AffineExpr> index_args;\n            bool index_flag = false;\n            int count = 0;\n            for (auto &kv: polyfp_expr.get_access())\n            {\n                int bias = 0;\n                if(kv.get_expr_type() == polyfp::e_op)\n                {\n                    auto expr0 = kv.get_operand(0);\n                    auto expr1 = kv.get_operand(1);\n                    auto left_index = a_print_index(expr0,comp,index_values,level);\n                    auto right_index = a_print_index(expr1,comp,index_values,level);\n                    if(kv.get_op_type() == polyfp::o_sub)\n                    {\n                        index_args.push_back(left_index - right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_add)\n                    {\n                        index_args.push_back(left_index + right_index);\n                        index_flag = true;  \n                    }else if(kv.get_op_type() == polyfp::o_mul)\n                    {\n                        index_args.push_back(left_index * right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_div)\n                    {\n                        index_args.push_back(left_index.floorDiv(right_index));\n                        index_flag = true;\n                    }\n                }\n                else\n                {\n                    int loc =0;\n                    int loc_2 =0;\n                    std::string tile_name1;\n                    std::string tile_name2;\n                    int tile_size;\n                    auto name_set = comp->get_loop_level_names();\n                    //MOD3\n                    auto t_name = kv.get_name();\n                    if(comp->refused == true)\n                    {\n                        t_name = comp->temp_access_map[t_name];\n                    }\n\n                    if ( std::find(name_set.begin(), name_set.end(), t_name) == name_set.end() )\n                    {\n                        for (auto &kv2: comp->get_access_map())\n                        {\n                            if(t_name==kv2.first){\n                                tile_name1 = kv2.second;\n                                loc = comp->iterators_location_map[tile_name1];\n                                // loc = comp->get_loop_level_number_from_dimension_name(kv2.second);\n                            }\n                        }\n                        \n                        mlir::Value value = ops[loc].getInductionVar();\n                        if ( std::find(index_values.begin(), index_values.end(), value)== index_values.end() )\n                        {\n                            index_values.push_back(value);\n                        }\n                        if(comp->is_tiled ==true)\n                        {\n                            for (auto &kv3: comp->get_tile_map())\n                            {\n                                if(tile_name1==kv3.first)\n                                {\n                                    loc = comp->iterators_location_map[kv3.second];\n                                    // loc = comp->get_loop_level_number_from_dimension_name(kv3.second);\n                                    tile_name2 = kv3.second;                              \n                                }\n                            }\n                            for (auto &kv4: comp->get_tile_size_map())\n                            {\n                                if(tile_name1==kv4.first){\n                                    tile_size = kv4.second;\n                                }\n                            }\n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {\n                                index_values.push_back(value2);\n                            }\n                            //TODO\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            index_args.push_back(builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size);\n                        }\n                        else if(comp->is_skewed == true)\n                        {                           \n                            loc = comp->iterators_location_map[comp->iterator_to_skew];                            \n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {                                \n                                index_values.push_back(value2);\n                            }\n                            //TODO: find the right dim\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                            if(tile_name1 == comp->iterator_to_modify)\n                            {                               \n                                index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                            }\n                            else\n                            {\n                                index_args.push_back(builder.getAffineDimExpr(index_3));\n                            }\n                        \n                            index_flag = true;\n                    }\n                    else\n                    {\n                        index_args.push_back(builder.getAffineDimExpr(loc));\n                    }\n\n                    index_flag = true;     \n\n                    }\n                    else\n                    {\n                        if(comp->refused == true)\n                        {\n                            t_name = comp->temp_access_map[t_name];\n                        }\n                        else\n                        {\n                            loc = comp->iterators_location_map[kv.get_name()];\n                        }\n                        \n                        // loc = comp->get_loop_level_number_from_dimension_name(kv.get_name());\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        mlir::Value value = ops[loc].getInductionVar();\n                        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end())\n                        {\n                            index_values.push_back(value);\n                        }\n                        //TODO\n                        int index_1 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                        index_args.push_back(builder.getAffineDimExpr(index_1));\n                    }\n                }\n            }\n            auto map = mlir::AffineMap::get(index_values.size(), 0, ArrayRef<mlir::AffineExpr> (index_args),builder.getContext());\n            mlir::ValueRange vr=llvm::makeArrayRef(index_values);\n            //TODO.number of variables\n            mlir::AffineLoadOp a;\n            if(index_flag == true)\n            {\n                a = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a,map,vr);\n            }\n            else\n            {\n                a = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a ,vr);\n            }\n            if(if_flag == true)\n            {\n                mlir::Value value = ops[2].getInductionVar();\n                SmallVector<mlir::Value, 4> ifOperands;\n                ifOperands.push_back(value);\n                SmallVector<mlir::AffineExpr, 4> ifExprs;\n                ifExprs.push_back(builder.getAffineDimExpr(0));\n                SmallVector<bool, 4> ifEqFlags;\n                ifEqFlags.push_back(true);\n                const auto condition = mlir::IntegerSet::get(1, 0, ifExprs, ifEqFlags);\n                auto ifOp =builder.create<mlir::AffineIfOp>(builder.getUnknownLoc(), condition, ifOperands,/*withElseRegion=*/false);\n                builder.setInsertionPointToStart(ifOp.getBody());\n                auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), a, mem, vr);\n                builder.setInsertionPointAfter(ifOp);\n            }\n            else\n            {\n                if(placeholder_index_flag == true)\n                {\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), a, mem, placeholder_map, placeholder_vr);\n                }\n                else\n                {\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), a, mem, placeholder_vr);\n                }   \n            }\n        }\n        else if(polyfp_expr.get_op_type() == polyfp::o_max)\n        {\n            mlir::ValueRange indices = {};\n            auto a = polyfp_expr.get_operand(0);\n            auto b = polyfp_expr.get_operand(1);\n            mlir::BlockArgument left;\n            mlir::BlockArgument right;\n            // mlir::arith::MulFOp allocSize_m;\n            // mlir::arith::AddFOp allocSize_a;\n            // theModule.dump();\n            a_print_expr(polyfp_expr, comp, level);\n\n            if(if_flag == true)\n            {\n                mlir::Value value = ops[2].getInductionVar();\n                SmallVector<mlir::Value, 4> ifOperands;\n                ifOperands.push_back(value);\n                SmallVector<mlir::AffineExpr, 4> ifExprs;\n                ifExprs.push_back(builder.getAffineDimExpr(0));\n                SmallVector<bool, 4> ifEqFlags;\n                ifEqFlags.push_back(true);\n                const auto condition = mlir::IntegerSet::get(1, 0, ifExprs, ifEqFlags);\n                auto ifOp =builder.create<mlir::AffineIfOp>(builder.getUnknownLoc(), condition, ifOperands,/*withElseRegion=*/false);\n                builder.setInsertionPointToStart(ifOp.getBody());\n                //TODO: other arithmetic? sub, o_div\n                auto the_op = all_current_op.back();\n                \n                auto index = the_op.index();\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                }\n                builder.setInsertionPointAfter(ifOp);\n            }\n            else\n            {\n                auto the_op = all_current_op.back();\n                auto index = the_op.index();\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    if(placeholder_index_flag == true){\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    } \n                    \n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    if(placeholder_index_flag == true){\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    if(placeholder_index_flag == true){\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    if(placeholder_index_flag == true){\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }\n                }\n                else if(index==4){\n                    auto op_to_process = std::get<4>(the_op);\n\n                    if(placeholder_index_flag == true){\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_map, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n                    }else{\n                        auto store1 = builder.create<mlir::AffineStoreOp>(builder.getUnknownLoc(), op_to_process, mem, placeholder_vr);\n                        builder.setInsertionPointAfter(store1);\n\n                    }\n\n                }\n            }\n\n        }\n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_if)\n    {\n        isl_ast_expr *cond = isl_ast_node_if_get_cond(node);\n        isl_ast_node *if_stmt = isl_ast_node_if_get_then(node);\n        isl_ast_node *else_stmt = isl_ast_node_if_get_else(node);\n        mlirGen1(fct,if_stmt,level,false,true,true);\n    }\n    return theModule;\n}\n\n\nmlir::AffineExpr polyfp::MLIRGenImpl::a_print_index(polyfp::expr polyfp_expr, polyfp::compute *comp, std::vector<mlir::Value> &index_values, int level){\n    mlir::AffineExpr index_value;\n    int loc =  0;\n    int loc2=0;\n    if (polyfp_expr.get_expr_type() == polyfp::e_op){ \n        auto expr0 = polyfp_expr.get_operand(0);\n        auto expr1 = polyfp_expr.get_operand(1);\n        auto left_index = a_print_index(expr0,comp,index_values,level);\n        auto right_index = a_print_index(expr1,comp, index_values,level);\n        if(polyfp_expr.get_op_type() == polyfp::o_sub){\n            index_value = left_index - right_index;\n        }else if(polyfp_expr.get_op_type() == polyfp::o_add){\n            index_value = left_index + right_index;\n        }else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n            index_value = left_index * right_index;\n        }else if(polyfp_expr.get_op_type() == polyfp::o_div){\n            index_value = left_index.floorDiv(right_index);\n        }\n    }\n    if (polyfp_expr.get_expr_type() == polyfp::e_var){\n        loc = get_iterator_location_from_name(comp,polyfp_expr,index_values);\n        auto name_set = comp->get_loop_level_names();\n        if(comp->is_tiled == true&&std::find(name_set.begin(), name_set.end(), polyfp_expr.get_name()) == name_set.end()){\n            int loc_2 =0;\n            std::string tile_name1;\n            std::string tile_name2;\n            int tile_size;\n            if(comp->is_tiled ==true)\n            {\n                for (auto &kv2: comp->get_access_map())\n                {\n                    if(polyfp_expr.get_name()==kv2.first)\n                    {\n                        tile_name1 = kv2.second;\n                        loc2 = comp->iterators_location_map[kv2.second];\n                    }\n                }\n                for (auto &kv3: comp->get_tile_map())\n                {\n                    if(tile_name1==kv3.first)\n                    {\n                        loc = comp->iterators_location_map[kv3.second];\n                        tile_name2 = kv3.second;\n                    }\n                }\n                for (auto &kv4: comp->get_tile_size_map()){\n                    if(tile_name1==kv4.first)\n                    {\n                        tile_size = kv4.second;                    \n                    }\n                }\n                \n                mlir::Value value2 = ops[loc].getInductionVar();\n                mlir::Value value3 = ops[loc2].getInductionVar();\n                if ( std::find(index_values.begin(), index_values.end(), value2) == index_values.end())\n                {\n                    index_values.push_back(value2);\n                }\n\n                int index2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                //TODO, 这里index3支持了，其他地方可能还有问题\n                int index3 = std::find(index_values.begin(), index_values.end(), value3) - index_values.begin();\n                index_value = builder.getAffineDimExpr(index3)+builder.getAffineDimExpr(index2)*tile_size;\n            }\n  \n        }else if(comp->is_skewed == true)\n        {        \n            int loc_2 =0;\n            std::string tile_name1;\n            std::string tile_name2;\n            int tile_size;\n            for (auto &kv2: comp->get_access_map())\n            {\n                if(polyfp_expr.get_name()==kv2.first)\n                {\n                    tile_name1 = kv2.second;\n                    loc = comp->iterators_location_map[tile_name1];\n                }\n            }\n            mlir::Value value = ops[loc].getInductionVar();\n            if ( std::find(index_values.begin(), index_values.end(), value)== index_values.end() )\n            {\n                \n                index_values.push_back(value);\n            }\n           \n            loc = comp->iterators_location_map[comp->iterator_to_skew];\n            mlir::Value value2 = ops[loc].getInductionVar();\n            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() ){\n                index_values.push_back(value2);\n            }\n            //TODO:\n            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n            if(tile_name1 == comp->iterator_to_modify){\n                index_value=builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor;\n            }else{\n                index_value =builder.getAffineDimExpr(index_3);\n            }\n            // index_value = builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size;\n        }\n        else{\n            index_value = builder.getAffineDimExpr(loc);\n        }\n    }  \n    if (polyfp_expr.get_expr_type() == polyfp::e_val)\n    {   \n        index_value = getAffineConstantExpr(polyfp_expr.get_int_val(),builder.getContext());\n    }  \n    // index_value = builder.getAffineDimExpr(loc%2);\n    \n    return index_value;\n\n}\n\n\n// Dump expression\nvoid polyfp::MLIRGenImpl::a_print_expr(polyfp::expr polyfp_expr, polyfp::compute *comp, int level){\n    auto left = polyfp_expr.get_operand(0);\n    auto right = polyfp_expr.get_operand(1);\n    if ((right.get_op_type() == polyfp::o_access || right.get_expr_type() == polyfp::e_var ) && (left.get_op_type() == polyfp::o_access || left.get_expr_type() == polyfp::e_var))\n    {\n        // theModule.dump();\n        mlir::AffineLoadOp loadedRhs;\n        mlir::AffineLoadOp loadedLhs;\n        mlir::Value arg_left;\n        mlir::Value arg_right;\n        if(left.get_op_type() == polyfp::o_access)\n        {\n            std::string a_name = left.get_name();\n            int index_a;\n            // if(comp->refused == true){\n            //     a_name = comp->temp_access_map[a_name];\n            // }\n            // std::cout<<a_name<<std::endl;\n            for(int i=0; i<argument_list.size(); i++)\n            {\n                if(argument_list.at(i) == a_name)\n                    index_a = i;               \n            }\n\n            // auto arg_a = funcs[0].getArgument(index_a);\n            mlir::Value arg_a;\n            \n            if(index_a+1<=funcs[0].getNumArguments())\n            {\n                arg_a = funcs[0].getArgument(index_a);\n            }\n            else\n            {\n                arg_a = values[index_a-funcs[0].getNumArguments()];\n            }\n\n            std::vector<mlir::Value> index_values;\n            SmallVector<mlir::AffineExpr> index_args;\n            bool index_flag = false;\n\n            for (auto &kv: left.get_access())\n            {\n                int bias = 0;\n                if(kv.get_expr_type() == polyfp::e_op)\n                {\n                    auto expr0 = kv.get_operand(0);\n                    auto expr1 = kv.get_operand(1);\n                    auto left_index = a_print_index(expr0,comp,index_values,level);\n                    auto right_index = a_print_index(expr1,comp,index_values,level);\n                    if(kv.get_op_type() == polyfp::o_sub){\n                        index_args.push_back(left_index - right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_add){\n                        index_args.push_back(left_index + right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_mul){\n                        index_args.push_back(left_index * right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_div){\n                        index_args.push_back(left_index.floorDiv(right_index));\n                        index_flag = true;\n                    }\n                }\n                else\n                {\n                    int loc =0;\n                    int loc_2 =0;\n                    std::string tile_name1;\n                    std::string tile_name2;\n                    int tile_size;\n                    auto name_set = comp->get_loop_level_names();\n                    auto t_name = kv.get_name();\n                    if(comp->refused == true)\n                    {\n                        t_name = comp->temp_access_map[t_name];\n                    }\n\n                    if ( std::find(name_set.begin(), name_set.end(), t_name) == name_set.end() )\n                    {                        \n                        for (auto &kv2: comp->get_access_map())\n                        {\n                            if(t_name==kv2.first)\n                            {\n                                tile_name1 = kv2.second;\n                                loc = comp->iterators_location_map[kv2.second];                        \n                            }\n                        }\n                        \n                        mlir::Value value = ops[loc].getInductionVar();\n                        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end() )\n                        {\n                            index_values.push_back(value);\n                        }\n\n                        if(comp->is_tiled ==true)\n                        {\n                            for (auto &kv3: comp->get_tile_map()){\n                                \n                                if(tile_name1==kv3.first)\n                                {\n                                    loc = comp->iterators_location_map[kv3.second];                                \n                                    tile_name2 = kv3.second;                              \n                                }\n                            }\n                            for (auto &kv4: comp->get_tile_size_map())\n                            {\n                                \n                                if(tile_name1==kv4.first){\n                                    tile_size = kv4.second;\n                                }\n                            }\n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {\n                                \n                                index_values.push_back(value2);\n                            }\n                            //TODO:\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            \n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n\n                            index_args.push_back(builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size);\n                        }\n                        else if(comp->is_skewed == true)\n                        {\n                            \n                            loc = comp->iterators_location_map[comp->iterator_to_skew];\n                            \n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {                         \n                                index_values.push_back(value2);\n                            }\n                            //TODO: find the right dim\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                            if(tile_name1 == comp->iterator_to_modify)\n                            {\n                                index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                            }\n                            else\n                            {\n                                index_args.push_back(builder.getAffineDimExpr(index_3));\n                            }\n                        \n                            index_flag = true;\n                        }\n                        \n                        \n                        else{\n                            // TODO std::cout<<\"something went wrong\"<<std::endl;\n                        }\n                        index_flag = true;              \n                    }\n                    else\n                    {\n                        if(comp->refused == true)\n                        {\n                            loc = comp->iterators_location_map[t_name];\n                        }\n                        else\n                        {\n                            loc = comp->iterators_location_map[kv.get_name()];\n                        }\n                        \n                       \n                        mlir::Value value = ops[loc].getInductionVar();\n                        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end())\n                        {                   \n                            index_values.push_back(value);\n                        }\n                        //TODO index_1 + index ?\n                        int index_1 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                        index_args.push_back(builder.getAffineDimExpr(index_1));\n                    }\n                } \n            }\n            auto map = mlir::AffineMap::get(index_values.size(), 0, ArrayRef<mlir::AffineExpr> (index_args),builder.getContext());\n            mlir::ValueRange vr=llvm::makeArrayRef(index_values);\n            //TODO.number of variables\n            mlir::AffineLoadOp a;\n            if(index_flag == true)\n            {\n                loadedLhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a,map,vr);\n            }else{\n                loadedLhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a ,vr);\n            }\n        }\n        else if(left.get_expr_type() == polyfp::e_var){\n            int index_argument;\n            std::string arg_name = left.get_name();\n           \n            for(int i=0; i<argument_list.size(); i++)\n            {\n                if(argument_list.at(i) == arg_name){\n                    index_argument = i;\n                }\n            }\n\n            if(index_argument+1<=funcs[0].getNumArguments())\n            {\n                arg_left = funcs[0].getArgument(index_argument);\n            }else{\n                arg_left = values[index_argument-funcs[0].getNumArguments()];\n            }\n        }\n        if(right.get_op_type() == polyfp::o_access)\n        {\n\n            std::string a_name = right.get_name();\n            int index_a;\n            \n            for(int i=0; i<argument_list.size(); i++){\n                if(argument_list.at(i) == a_name)\n                    index_a = i;               \n            }\n            // auto arg_a = funcs[0].getArgument(index_a);\n            mlir::Value arg_a;\n            if(index_a+1<=funcs[0].getNumArguments()){\n                arg_a = funcs[0].getArgument(index_a);\n            }else{\n                arg_a = values[index_a-funcs[0].getNumArguments()];\n            }\n            std::vector<mlir::Value> index_values;\n            SmallVector<mlir::AffineExpr> index_args;\n            bool index_flag = false;\n            for (auto &kv: right.get_access())\n            {\n                int bias = 0;\n                if(kv.get_expr_type() == polyfp::e_op)\n                {\n                    auto expr0 = kv.get_operand(0);\n                    auto expr1 = kv.get_operand(1);\n                    auto left_index = a_print_index(expr0,comp,index_values,level);\n                    auto right_index = a_print_index(expr1,comp,index_values,level);\n                    if(kv.get_op_type() == polyfp::o_sub){\n                        index_args.push_back(left_index - right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_add){\n                        index_args.push_back(left_index + right_index);\n                        index_flag = true;   \n                    }else if(kv.get_op_type() == polyfp::o_mul){\n                        index_args.push_back(left_index * right_index);\n                        index_flag = true;    \n                    }else if(kv.get_op_type() == polyfp::o_div){\n                        index_args.push_back(left_index.floorDiv(right_index));\n                        index_flag = true;   \n                    }\n                }\n                else\n                {\n                    int loc =0;\n                    int loc_2 =0;\n                    std::string tile_name1;\n                    std::string tile_name2;\n                    int tile_size;\n                    auto name_set = comp->get_loop_level_names();\n                    auto t_name = kv.get_name();\n                    if(comp->refused == true)\n                    {\n                        t_name = comp->temp_access_map[t_name];\n                    }\n                    if ( std::find(name_set.begin(), name_set.end(), t_name) == name_set.end() )\n                    {\n                        for (auto &kv2: comp->get_access_map())\n                        {\n                            if(t_name==kv2.first)\n                            {\n                                tile_name1 = kv2.second;\n                                loc = comp->iterators_location_map[kv2.second];\n                                // loc = comp->get_loop_level_number_from_dimension_name(kv2.second);\n                            }\n                        }\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n\n                        mlir::Value value = ops[loc].getInductionVar();       \n\n                        if ( std::find(index_values.begin(), index_values.end(), value)== index_values.end() )\n                        {\n                            index_values.push_back(value);\n                        }\n\n                        if(comp->is_tiled ==true)\n                        {\n                            for (auto &kv3: comp->get_tile_map())\n                            {\n                                if(tile_name1==kv3.first)\n                                {\n                                    loc = comp->iterators_location_map[kv3.second];\n                                    // loc_2 = comp->get_loop_level_number_from_dimension_name(kv3.second);\n                                    tile_name2 = kv3.second;\n                                }\n                            }\n                            for (auto &kv4: comp->get_tile_size_map())\n                            {\n                                if(tile_name1==kv4.first)\n                                {\n                                    tile_size = kv4.second;\n                                }\n                            }\n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {\n                                index_values.push_back(value2);\n                            }\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            index_args.push_back(builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size);\n                        }\n                        else if(comp->is_skewed == true)\n                        {\n                            \n                            loc = comp->iterators_location_map[comp->iterator_to_skew];                           \n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {\n                                \n                                index_values.push_back(value2);\n                            }\n                            //TODO: find the right dim\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                            if(tile_name1 == comp->iterator_to_modify)\n                            {\n                                index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                            }else{\n                                index_args.push_back(builder.getAffineDimExpr(index_3));\n                            }\n                        \n                            index_flag = true;\n                    }\n                    else\n                    {\n                            // TODO: std::cout<<\"something went wrong\"<<std::endl;\n                    }\n                    index_flag = true; \n                      \n                    }\n                    else\n                    {\n                        if(comp->refused == true)\n                        {\n                            loc = comp->iterators_location_map[t_name];\n                        }\n                        else\n                        {\n                            loc = comp->iterators_location_map[kv.get_name()];\n                        }\n                        \n                        // loc = comp->get_loop_level_number_from_dimension_name(kv.get_name());\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++){\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level){\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 ){\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        mlir::Value value = ops[loc].getInductionVar();\n                        if(std::find(index_values.begin(), index_values.end(), value) == index_values.end()){\n                            index_values.push_back(value);\n                        }\n                        int index_1 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                        index_args.push_back(builder.getAffineDimExpr(index_1));\n                    }\n                }      \n            }\n            auto map = mlir::AffineMap::get(index_values.size(), 0, ArrayRef<mlir::AffineExpr> (index_args),builder.getContext());\n            mlir::ValueRange vr=llvm::makeArrayRef(index_values);\n            //TODO.number of variables\n            mlir::AffineLoadOp a;\n            if(index_flag == true){\n                loadedRhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a,map,vr);\n            }else{\n                loadedRhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a ,vr);\n            }   \n        }\n        else if(right.get_expr_type() == polyfp::e_var)\n        {\n            int index_argument;\n            std::string arg_name = right.get_name();\n            for(int i=0; i<argument_list.size(); i++){\n                if(argument_list.at(i) == arg_name){\n                    index_argument = i;\n                }\n            }\n            // arg_right = funcs[0].getArgument(index_argument);\n            // mlir::Value arg_right;\n            if(index_argument+1<=funcs[0].getNumArguments())\n            {\n                arg_right = funcs[0].getArgument(index_argument);\n            }else{\n                arg_right = values[index_argument-funcs[0].getNumArguments()];\n            }\n        }\n        if (right.get_op_type() == polyfp::o_access && left.get_op_type() == polyfp::o_access )\n        { \n            if(polyfp_expr.get_op_type() == polyfp::o_add)\n            {\n                auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedLhs, loadedRhs);\n                current_op.push_back(add_1);\n                all_current_op.push_back(add_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_mul)\n            {\n                auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedLhs, loadedRhs);\n                current_op.push_back(mul_1);\n                all_current_op.push_back(mul_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_sub)\n            {\n                auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), loadedLhs, loadedRhs);\n                current_op.push_back(sub_1);\n                all_current_op.push_back(sub_1);  \n            }else if(polyfp_expr.get_op_type() == polyfp::o_div)\n            {\n                auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), loadedLhs, loadedRhs);\n                current_op.push_back(div_1);\n                all_current_op.push_back(div_1);   \n            }else if(polyfp_expr.get_op_type() == polyfp::o_max)\n            {\n                auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedLhs, loadedRhs);\n                current_op.push_back(max_1);\n                all_current_op.push_back(max_1);   \n                // theModule.dump();\n            }\n        }\n        if (right.get_op_type() == polyfp::o_access  && left.get_expr_type() == polyfp::e_var)\n        {\n            if(polyfp_expr.get_op_type() == polyfp::o_add){\n                auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, loadedRhs);\n                current_op.push_back(add_1);\n                all_current_op.push_back(add_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n                auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, loadedRhs);\n                current_op.push_back(mul_1);\n                all_current_op.push_back(mul_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_sub){\n                auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), arg_left, loadedRhs);\n                current_op.push_back(sub_1);\n                all_current_op.push_back(sub_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_div){\n                auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), arg_left, loadedRhs);\n                current_op.push_back(div_1);\n                all_current_op.push_back(div_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_max){\n                auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), arg_left, loadedRhs);\n                current_op.push_back(max_1);\n                all_current_op.push_back(max_1);   \n            }\n        }\n        if (left.get_op_type() == polyfp::o_access  && right.get_expr_type() == polyfp::e_var)\n        {\n            if(polyfp_expr.get_op_type() == polyfp::o_add){\n                auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedLhs, arg_right);\n                current_op.push_back(add_1);\n                all_current_op.push_back(add_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n                auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedLhs, arg_right);\n                current_op.push_back(mul_1);\n                all_current_op.push_back(mul_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_sub){\n                auto sub_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedLhs, arg_right);\n                current_op.push_back(sub_1);\n                all_current_op.push_back(sub_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_div){\n                auto div_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedLhs, arg_right);\n                current_op.push_back(div_1);\n                all_current_op.push_back(div_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_max){\n                auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedLhs, arg_right);\n                current_op.push_back(max_1);\n                all_current_op.push_back(max_1);   \n            }\n        }\n        if (left.get_expr_type() == polyfp::e_var  && right.get_expr_type() == polyfp::e_var)\n        {\n            if(polyfp_expr.get_op_type() == polyfp::o_add){\n                auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, arg_right);\n                current_op.push_back(add_1);\n                all_current_op.push_back(add_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n                auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, arg_right);\n                current_op.push_back(mul_1);\n                all_current_op.push_back(mul_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_sub){\n                auto sub_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, arg_right);\n                current_op.push_back(sub_1);\n                all_current_op.push_back(sub_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_div){\n                auto div_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, arg_right);\n                current_op.push_back(div_1);\n                all_current_op.push_back(div_1);\n            }else if(polyfp_expr.get_op_type() == polyfp::o_max){\n                auto max_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, arg_right);\n                current_op.push_back(max_1);\n                all_current_op.push_back(max_1);   \n            }\n        }\n        // theModule.dump();\n    }\n    if ((right.get_op_type() == polyfp::o_access || right.get_expr_type() == polyfp::e_var ) && (left.get_op_type() != polyfp::o_access && left.get_expr_type() == polyfp::e_op))\n    {\n        mlir::AffineLoadOp loadedRhs;\n        \n        a_print_expr(left,comp,level);\n        \n        if(right.get_op_type() == polyfp::o_access)\n        {\n            std::string a_name = right.get_name();\n            int index_a;\n            for(int i=0; i<argument_list.size(); i++)\n            {\n                if(argument_list.at(i) == a_name)\n                    index_a = i;               \n            }\n            mlir::Value arg_a;\n            if(index_a+1<=funcs[0].getNumArguments())\n            {\n                arg_a = funcs[0].getArgument(index_a);\n            }\n            else\n            {\n                arg_a = values[index_a-funcs[0].getNumArguments()];\n            }\n            std::vector<mlir::Value> index_values;\n            SmallVector<mlir::AffineExpr> index_args;\n            bool index_flag = false;\n            for (auto &kv: right.get_access())\n            {\n                int bias = 0;\n                if(kv.get_expr_type() == polyfp::e_op)\n                {                   \n                    auto expr0 = kv.get_operand(0);\n                    auto expr1 = kv.get_operand(1);\n                    auto left_index = a_print_index(expr0,comp,index_values,level);\n                    auto right_index = a_print_index(expr1,comp,index_values,level);\n                    if(kv.get_op_type() == polyfp::o_sub){\n                        index_args.push_back(left_index - right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_add){\n                        index_args.push_back(left_index + right_index);\n                        index_flag = true;  \n                    }else if(kv.get_op_type() == polyfp::o_mul){\n                        index_args.push_back(left_index * right_index);\n                        index_flag = true; \n                    }else if(kv.get_op_type() == polyfp::o_div){\n                        index_args.push_back(left_index.floorDiv(right_index));\n                        index_flag = true; \n                    }\n                }\n                else\n                {\n                    int loc =0;\n                    int loc_2 =0;\n                    std::string tile_name1;\n                    std::string tile_name2;\n                    int tile_size;\n                    auto name_set = comp->get_loop_level_names();\n                    auto t_name = kv.get_name();\n                    if(comp->refused == true)\n                    {\n                        t_name = comp->temp_access_map[t_name];\n                    }\n                    if ( std::find(name_set.begin(), name_set.end(), t_name) == name_set.end() )\n                    {\n                        for (auto &kv2: comp->get_access_map())\n                        {\n                            if(t_name==kv2.first\n                            ){\n                                tile_name1 = kv2.second;\n                                loc = comp->iterators_location_map[kv2.second];\n                                // loc = comp->get_loop_level_number_from_dimension_name(kv2.second);\n                            }\n                        }\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++)\n                        {\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level)\n                            {\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 )\n                            {\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n\n                        mlir::Value value = ops[loc].getInductionVar();      \n\n                        if(std::find(index_values.begin(), index_values.end(), value)== index_values.end() )\n                        {  \n                            index_values.push_back(value);\n                        }\n                        if(comp->is_tiled ==true)\n                        {\n                            for (auto &kv3: comp->get_tile_map())\n                            {\n                                if(tile_name1==kv3.first){\n                                    loc = comp->iterators_location_map[kv3.second];\n                                    // loc_2 = comp->get_loop_level_number_from_dimension_name(kv3.second);\n                                    tile_name2 = kv3.second; \n                                }\n                            }\n                            for (auto &kv4: comp->get_tile_size_map())\n                            {\n                                if(tile_name1==kv4.first){\n                                    tile_size = kv4.second;\n                                }\n                            }\n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if(std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {   \n                                index_values.push_back(value2);\n                            }\n                            //TODO\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            index_args.push_back(builder.getAffineDimExpr(index_3)+builder.getAffineDimExpr(index_2)*tile_size);\n                        }\n                        else if(comp->is_skewed == true)\n                        {\n                            \n                            loc = comp->iterators_location_map[comp->iterator_to_skew];                            \n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() )\n                            {\n                                \n                                index_values.push_back(value2);\n                            }\n                            //TODO: find the right dim\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                            if(tile_name1 == comp->iterator_to_modify){\n                                index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                            }else{\n                                index_args.push_back(builder.getAffineDimExpr(index_3));\n                            }\n                        \n                            index_flag = true;\n                    }\n                    else{\n                            // TODO\n                        }\n                        index_flag = true; \n\n                    }else{\n                        if(comp->refused == true){\n                            // t_name = comp->temp_access_map[t_name];\n                            loc = comp->iterators_location_map[t_name];\n                        }else{\n                            loc = comp->iterators_location_map[kv.get_name()];\n                        }\n                        \n                        // loc = comp->get_loop_level_number_from_dimension_name(kv.get_name());\n                        int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++){\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level){\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 ){\n                                index = start_loops_position[i];\n                                break;\n                            }\n                        }\n                        mlir::Value value = ops[loc].getInductionVar();  \n                        if ( std::find(index_values.begin(), index_values.end(), value) == index_values.end()){\n                            index_values.push_back(value);\n                        }\n                        //TODO\n                        int index_2 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                        index_args.push_back(builder.getAffineDimExpr(index_2));\n                    }\n                }     \n            }\n            auto map = mlir::AffineMap::get(index_values.size(), 0, ArrayRef<mlir::AffineExpr> (index_args),builder.getContext());  \n            mlir::ValueRange vr=llvm::makeArrayRef(index_values);\n            //TODO.number of variables\n            mlir::AffineLoadOp a;\n            if(index_flag == true){\n                loadedRhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a,map,vr);\n            }else{\n                loadedRhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a ,vr);\n            }\n            auto the_op = all_current_op.back();\n            auto index = the_op.index();\n            if(polyfp_expr.get_op_type() == polyfp::o_add)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(add_1);\n                    // add_1.dump();\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(add_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(add_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(add_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_mul)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_sub)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_div)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(div_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_max)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }\n            }\n        }\n        if(right.get_expr_type() == polyfp::e_var)\n        {\n            int index_argument;\n            std::string arg_name = right.get_name();\n            for(int i=0; i<argument_list.size(); i++){\n                if(argument_list.at(i) == arg_name){\n                    index_argument = i;\n                }\n            }\n            // auto arg_right = funcs[0].getArgument(index_argument);\n            mlir::Value arg_right;\n            if(index_argument+1<=funcs[0].getNumArguments()){\n                arg_right = funcs[0].getArgument(index_argument);\n            }else{\n                arg_right = values[index_argument-funcs[0].getNumArguments()];\n            }\n            auto the_op = all_current_op.back();\n            auto index = the_op.index();\n            if(polyfp_expr.get_op_type() == polyfp::o_add)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(add_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(add_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(add_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(add_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_mul)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(mul_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(mul_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(mul_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(mul_1);\n                }   \n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_sub)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(sub_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(sub_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(sub_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(sub_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_div)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(div_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(div_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(div_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,arg_right);\n                    all_current_op.push_back(div_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_max)\n            {\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), loadedRhs, op_to_process);\n                    all_current_op.push_back(max_1);\n                }\n            }\n        }    \n        // theModule.dump();\n    }\n    if ((left.get_op_type() == polyfp::o_access || left.get_expr_type() == polyfp::e_var ) && (right.get_op_type() != polyfp::o_access && right.get_expr_type() == polyfp::e_op))\n    {\n        mlir::AffineLoadOp loadedLhs;\n        a_print_expr(right,comp,level);\n        if(left.get_op_type() == polyfp::o_access){\n            std::string a_name = left.get_name();\n            int index_a;\n            for(int i=0; i<argument_list.size(); i++){\n                if(argument_list.at(i) == a_name)\n                    index_a = i;               \n            }\n            // auto arg_a = funcs[0].getArgument(index_a);\n            mlir::Value arg_a;\n            if(index_a+1<=funcs[0].getNumArguments()){\n                arg_a = funcs[0].getArgument(index_a);\n            }else{\n                arg_a = values[index_a-funcs[0].getNumArguments()];\n            }\n            std::vector<mlir::Value> index_values;\n            SmallVector<mlir::AffineExpr> index_args;\n            bool index_flag = false;\n            for (auto &kv: left.get_access()){\n                int bias = 0;\n                if(kv.get_expr_type() == polyfp::e_op){\n                    auto expr0 = kv.get_operand(0);\n                    auto expr1 = kv.get_operand(1);\n                    auto left_index = a_print_index(expr0,comp,index_values,level);\n                    auto right_index = a_print_index(expr1,comp,index_values,level);\n                    if(kv.get_op_type() == polyfp::o_sub){               \n                        index_args.push_back(left_index - right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_add){\n                        index_args.push_back(left_index + right_index);\n                        index_flag = true;\n                    }else if(kv.get_op_type() == polyfp::o_mul){\n                        index_args.push_back(left_index * right_index);\n                        index_flag = true;  \n                    }else if(kv.get_op_type() == polyfp::o_div){\n                        index_args.push_back(left_index.floorDiv(right_index));\n                        index_flag = true;\n                    }\n                }\n                else{\n                    int loc =0;\n                    int loc_2 =0;\n                    std::string tile_name1;\n                    std::string tile_name2;\n                    int tile_size;\n                    auto name_set = comp->get_loop_level_names();\n                    auto t_name = kv.get_name();\n                    if(comp->refused == true){\n                        t_name = comp->temp_access_map[t_name];\n                    }\n                    //TODO\n                    int index = 0;\n                        for(int i=0; i<start_loops_position.size(); i++){\n                            if(start_loops_position[i]>level&&start_loops_position[i-1]<=level){\n                                index = start_loops_position[i-1];\n                                break;\n                            }\n                            if(i == start_loops_position.size()-1 ){\n                                index = start_loops_position[i];\n                                break;\n                            }\n                    }\n                    if(std::find(name_set.begin(), name_set.end(), t_name) == name_set.end()){\n                        for(auto &kv2: comp->get_access_map()){\n                            if(t_name==kv2.first){\n                                tile_name1 = kv2.second;\n                                loc = comp->iterators_location_map[kv2.second];\n                                // loc = comp->get_loop_level_number_from_dimension_name(kv2.second);\n                            }\n                        }        \n\n                        mlir::Value value = ops[loc].getInductionVar();\n                        if(std::find(index_values.begin(), index_values.end(), value)== index_values.end()){\n                            index_values.push_back(value);\n                        }\n                        if(comp->is_tiled ==true){\n                            for (auto &kv3: comp->get_tile_map()){\n                                if(tile_name1==kv3.first){\n                                    loc = comp->iterators_location_map[kv3.second];\n                                    // loc_2 = comp->get_loop_level_number_from_dimension_name(kv3.second);\n                                    tile_name2 = kv3.second;\n                                }\n                            }\n                            for (auto &kv4: comp->get_tile_size_map()){\n                                if(tile_name1==kv4.first){\n                                    tile_size = kv4.second;\n                                }\n                            }\n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if(std::find(index_values.begin(), index_values.end(), value2)== index_values.end() ){\n                                index_values.push_back(value2);\n                            }\n                            //TODO\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            index_args.push_back(builder.getAffineDimExpr(index)+builder.getAffineDimExpr(index_2)*tile_size);\n                        }else if(comp->is_skewed == true){\n                            \n                            loc = comp->iterators_location_map[comp->iterator_to_skew];\n                            \n                            mlir::Value value2 = ops[loc].getInductionVar();\n                            if ( std::find(index_values.begin(), index_values.end(), value2)== index_values.end() ){\n                                \n                                index_values.push_back(value2);\n                            }\n                            //TODO: find the right dim\n                            int index_2 = std::find(index_values.begin(), index_values.end(), value2) - index_values.begin();\n                            int index_3 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*2);\n                            // placeholder_index_args.push_back(builder.getAffineDimExpr(index_3));\n                            if(tile_name1 == comp->iterator_to_modify){\n                                index_args.push_back(builder.getAffineDimExpr(index_3)-builder.getAffineDimExpr(index_2)*comp->skew_factor);\n                            }else{\n                                index_args.push_back(builder.getAffineDimExpr(index_3));\n                            }\n                        \n                            index_flag = true;\n                    }else{\n                            // TODO\n                        }\n                        index_flag = true; \n                        \n                        \n                    }else{\n                        //TODO\n                        // loc = comp->get_loop_level_number_from_dimension_name(kv.get_name());\n                        if(comp->refused == true){\n                            // t_name = comp->temp_access_map[t_name];\n                            loc = comp->iterators_location_map[t_name];\n                        }else{\n                            loc = comp->iterators_location_map[kv.get_name()];\n                        }\n                        \n                        mlir::Value value = ops[loc].getInductionVar();\n                        if(std::find(index_values.begin(), index_values.end(), value) == index_values.end()){\n                            index_values.push_back(value);\n                        }\n                        //TODO\n                        int index_1 = std::find(index_values.begin(), index_values.end(), value) - index_values.begin();\n                        index_args.push_back(builder.getAffineDimExpr(index_1));\n                    }\n                }\n                \n            }\n            auto map = mlir::AffineMap::get(index_values.size(), 0, ArrayRef<mlir::AffineExpr> (index_args),builder.getContext());\n            mlir::ValueRange vr=llvm::makeArrayRef(index_values);\n            //TODO.number of variables\n            mlir::AffineLoadOp a;\n            if(index_flag == true){\n                loadedLhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a,map,vr);\n            }else{\n                loadedLhs = builder.create<mlir::AffineLoadOp>(builder.getUnknownLoc(), arg_a ,vr);\n            }\n            auto the_op = all_current_op.back();\n            auto index = the_op.index();\n            if(polyfp_expr.get_op_type() == polyfp::o_add){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(add_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(add_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(add_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(add_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(mul_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(mul_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(mul_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(mul_1);\n                } \n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_sub){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(sub_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(sub_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(sub_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(sub_1);\n                }      \n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_div){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(div_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(div_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(div_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), op_to_process,loadedLhs);\n                    all_current_op.push_back(div_1);\n                }  \n            }else if(polyfp_expr.get_op_type() == polyfp::o_max){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(),  op_to_process,loadedLhs);\n                    all_current_op.push_back(max_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(),  op_to_process,loadedLhs);\n                    all_current_op.push_back(max_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(),  op_to_process,loadedLhs);\n                    all_current_op.push_back(max_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(),  op_to_process,loadedLhs);\n                    all_current_op.push_back(max_1);\n                }\n            }\n        }\n        if(left.get_expr_type() == polyfp::e_var){\n            int index_argument;\n            std::string arg_name = left.get_name();\n            for(int i=0; i<argument_list.size(); i++){\n                if(argument_list.at(i) == arg_name){\n                    index_argument = i;\n                }\n            }\n            // auto arg_left = funcs[0].getArgument(index_argument);\n            mlir::Value arg_left;\n            if(index_argument+1<=funcs[0].getNumArguments()){\n                arg_left = funcs[0].getArgument(index_argument);\n            }else{\n                arg_left = values[index_argument-funcs[0].getNumArguments()];\n            }\n            auto the_op = all_current_op.back();\n            auto index = the_op.index();\n            if(polyfp_expr.get_op_type() == polyfp::o_add){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(add_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(add_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(add_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(add_1);\n                }\n            }\n            if(polyfp_expr.get_op_type() == polyfp::o_mul){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(mul_1);\n                }\n            }\n            if(polyfp_expr.get_op_type() == polyfp::o_sub){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(sub_1);\n                }\n            }\n            if(polyfp_expr.get_op_type() == polyfp::o_div){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(div_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(div_1);\n                }\n            }\n            else if(polyfp_expr.get_op_type() == polyfp::o_max){\n                if(index==0){\n                    auto op_to_process = std::get<0>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==1){\n                    auto op_to_process = std::get<1>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==2){\n                    auto op_to_process = std::get<2>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(max_1);\n                }else if(index==3){\n                    auto op_to_process = std::get<3>(the_op);\n                    auto max_1 = builder.create<mlir::arith::MaxFOp>(builder.getUnknownLoc(), arg_left, op_to_process);\n                    all_current_op.push_back(max_1);\n                }\n            }\n        }\n    }\n    if ((left.get_op_type() != polyfp::o_access && left.get_expr_type() == polyfp::e_op) && (right.get_op_type() != polyfp::o_access && right.get_expr_type() == polyfp::e_op)){\n        mlir::AffineLoadOp loadedLhs;\n        a_print_expr(left,comp,level);\n        a_print_expr(right,comp,level);\n        auto the_op = all_current_op.back();\n        auto the_op2 = all_current_op[all_current_op.size()-2];\n        auto index = the_op.index();\n        auto index2 = the_op2.index();\n        //TODO, sub, mul ,div\n        if(polyfp_expr.get_op_type() == polyfp::o_add){\n            if(index==0){\n                auto op_to_process = std::get<0>(the_op);\n                if(index2==0){\n                    auto op_to_process2 = std::get<0>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }else if(index2==1){\n                    auto op_to_process2 = std::get<1>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==2){\n                    auto op_to_process2 = std::get<2>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==3){\n                    auto op_to_process2 = std::get<3>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                \n            }else if(index==1){\n                auto op_to_process = std::get<1>(the_op);\n                if(index2==0){\n                    auto op_to_process2 = std::get<0>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }else if(index2==1){\n                    auto op_to_process2 = std::get<1>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==2){\n                    auto op_to_process2 = std::get<2>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==3){\n                    auto op_to_process2 = std::get<3>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n            }else if(index==2){\n                auto op_to_process = std::get<2>(the_op);\n                if(index2==0){\n                    auto op_to_process2 = std::get<0>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }else if(index2==1){\n                    auto op_to_process2 = std::get<1>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==2){\n                    auto op_to_process2 = std::get<2>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==3){\n                    auto op_to_process2 = std::get<3>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n            }else if(index==3){\n                auto op_to_process = std::get<3>(the_op);\n                if(index2==0){\n                    auto op_to_process2 = std::get<0>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }else if(index2==1){\n                    auto op_to_process2 = std::get<1>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==2){\n                    auto op_to_process2 = std::get<2>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n                else if(index2==3){\n                    auto op_to_process2 = std::get<3>(the_op2);\n                    auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), op_to_process,op_to_process2);\n                    all_current_op.push_back(add_1);\n                }\n            }\n        }\n        // if(polyfp_expr.get_op_type() == polyfp::o_add){\n        //     auto add_1 = builder.create<mlir::arith::AddFOp>(builder.getUnknownLoc(), add_op[add_op.size()-2], add_op.back());\n        //     all_current_op.push_back(add_1);\n        // }\n        // else if(polyfp_expr.get_op_type() == polyfp::o_mul){\n        //     auto mul_1 = builder.create<mlir::arith::MulFOp>(builder.getUnknownLoc(), mul_op[mul_op.size()-2], mul_op.back());\n        //     all_current_op.push_back(mul_1);\n        // }\n        // else if(polyfp_expr.get_op_type() == polyfp::o_sub){\n        //     auto sub_1 = builder.create<mlir::arith::SubFOp>(builder.getUnknownLoc(), mul_op[mul_op.size()-2], mul_op.back());\n        //     all_current_op.push_back(sub_1);\n        // }\n        // else if(polyfp_expr.get_op_type() == polyfp::o_div){\n        //     auto div_1 = builder.create<mlir::arith::DivFOp>(builder.getUnknownLoc(), mul_op[mul_op.size()-2], mul_op.back());\n        //     all_current_op.push_back(div_1);\n        // }\n    }\n}\n\nmlir::OwningOpRef<mlir::ModuleOp> mlirGen2(mlir::MLIRContext &context, polyfp::function &fct, isl_ast_node *node, int &level) {\n    auto manager = MLIRGenImpl(context); \n    manager.mlirGen1(fct,node,level,true, false, false);\n    // manager.getModule().dump();\n    for(auto &comp : fct.leader_computations)\n    {\n        for(auto &kv : comp->get_directive_map())\n        {\n            if(kv.second == \"pipeline\")\n            {\n                int loc_2 = comp->get_loop_level_number_from_dimension_name(kv.first);\n                int loc = comp->iterators_location_map[kv.first];\n                // index = loc + index;\n                mlir::scalehls::setLoopDirective(manager.ops[loc], true, comp->II, false, false);\n                for(int i=1; i<=loc_2; i++)\n                {\n                    mlir::scalehls::setLoopDirective(manager.ops[loc-i], false, comp->II, false, true);\n                }\n            }\n        }             \n    }\n    auto map = manager.get_array_map();\n    mlir::scalehls::setTopFuncAttr(manager.get_funcs()[0]);\n    for(auto &kv: fct.get_partition_map())\n    {\n        SmallVector<mlir::scalehls::hls::PartitionKind, 4> kinds;\n        SmallVector<unsigned, 4> factors;\n        for(auto &factor: std::get<1>(kv))\n        {\n            factors.push_back(factor);\n        }\n        for(auto &type: std::get<2>(kv))\n        {\n            if(type == \"cyclic\"){\n                kinds.push_back(mlir::scalehls::hls::PartitionKind::CYCLIC);\n            }else if(type == \"block\"){\n                kinds.push_back(mlir::scalehls::hls::PartitionKind::BLOCK);\n            }else if(type == \"none\"){\n                kinds.push_back(mlir::scalehls::hls::PartitionKind::NONE);\n            }\n        }\n        mlir::scalehls::applyArrayPartition(manager.get_funcs()[0].getArgument(map[std::get<0>(kv)]), factors, kinds,/*updateFuncSignature=*/true);\n        // manager.getModule().dump();\n    }\n    // manager.getModule().dump();\n    // mlir::scalehls::applyFuncPreprocess(manager.get_funcs()[0], true);         \n    mlir::scalehls::applyFuncPreprocess(manager.get_funcs()[0], true);\n    // mlir::scalehls::setFuncDirective(manager.get_funcs()[0], false, 1, true);\n    for(auto &comp: fct.leader_computations)\n    {\n        if(comp->is_unrolled == true)\n        {\n            for(int i=0; i<comp->unroll_dimension.size(); i++)\n            {\n                // int bias = comp->get_loop_level_number_from_dimension_name(comp->unroll_dimension[i].get_name());\n                // int loc = fct.leader_computation_index[comp];\n                int loc = comp->iterators_location_map[comp->unroll_dimension[i].get_name()];\n                if(comp->unroll_factor[i] != -1)\n                {\n                    mlir::loopUnrollUpToFactor(manager.ops[loc],comp->unroll_factor[i]);\n                }\n                else\n                {\n                    mlir::loopUnrollFull(manager.ops[loc]);\n                }\n            }  \n        }\n    }\n    // \n    mlir::scalehls::applyMemoryOpts(manager.get_funcs()[0]);\n\n \nmanager.getModule().dump();\n\n    // Read target specification JSON file.\n    std::string errorMessage;\n    std::string pwd = std::filesystem::current_path().parent_path();\n    auto configFile = mlir::openInputFile(pwd+\"/samples/config.json\", &errorMessage);\n    if (!configFile) {\n        llvm::errs() << errorMessage << \"\\n\";\n    }\n    // Parse JSON file into memory.\n    auto config = llvm::json::parse(configFile->getBuffer());\n    if (!config) {\n        llvm::errs() << \"failed to parse the target spec json file\\n\";\n    }\n    auto configObj = config.get().getAsObject();\n    if (!configObj) {\n        llvm::errs() << \"support an object in the target spec json file, found \"\n                      \"something else\\n\";\n    }\n    // Collect profiling latency and DSP usage data, where default values are\n    // based on Xilinx PYNQ-Z1 board.\n    llvm::StringMap<int64_t> latencyMap;\n    mlir::scalehls::getLatencyMap(configObj, latencyMap);\n    llvm::StringMap<int64_t> dspUsageMap;\n    mlir::scalehls::getDspUsageMap(configObj, dspUsageMap);\n   \n    int dspNum;\n    return manager.getModule();\n}\n\nmlir::ModuleOp polyfp::MLIRGenImpl::getModule()\n{\n    return this->theModule;\n} \nvoid gen_mlir(polyfp::function &fct, isl_ast_node *node, int &level)\n{\n    mlir::MLIRContext context;\n    context.disableMultithreading();\n    context.getOrLoadDialect<mlir::func::FuncDialect>();\n    context.getOrLoadDialect<mlir::AffineDialect>();\n    context.getOrLoadDialect<mlir::math::MathDialect>();\n    context.getOrLoadDialect<mlir::memref::MemRefDialect>();\n    context.getOrLoadDialect<mlir::scalehls::HLSDialect>();\n    mlir::OwningOpRef<mlir::ModuleOp> module = mlirGen2(context, fct, node, level);\n    mlir::verify(*module, false);\n    if (failed(mlir::verify(*module, false))) \n    {\n        module->emitError(\"module verification error\");\n    }\n    // module->dump();\n    std::error_code error;\n    std::string s = fct.get_name();\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/\"+s+\"/\"+s+\".mlir\";\n    llvm::raw_fd_ostream os(path, error);\n    os << *module;\n\n}\nvoid function::gen_mlir_stmt(){   \n    int level = 0;\n    gen_mlir(*this,this->get_isl_ast(),level);\n}\n\n\n\n}\n"
  },
  {
    "path": "lib/polyhedral/generator_isl.cpp",
    "content": "#include \"generator_isl.h\"\n\nnamespace polyfp{\n\npolyfp::expr polyfp_expr_from_isl_ast_expr(isl_ast_expr *isl_expr);\n\nvoid generator::get_rhs_accesses(const polyfp::function *func, const polyfp::compute *comp,\n                                 std::vector<isl_map *> &accesses, bool return_buffer_accesses)\n{\n\n    const polyfp::expr &rhs = comp->get_expr();\n    generator::traverse_expr_and_extract_accesses(func, comp, rhs, accesses, return_buffer_accesses);\n\n}\n\nisl_map *create_map_from_domain_and_range(isl_set *domain, isl_set *range)\n{\n\n    // polyfp::str_dump(\"Domain:\", isl_set_to_str(domain));\n    // polyfp::str_dump(\"Range:\", isl_set_to_str(range));\n    // Extracting the spaces and aligning them\n    isl_space *sp1 = isl_set_get_space(domain);\n    isl_space *sp2 = isl_set_get_space(range);\n    sp1 = isl_space_align_params(sp1, isl_space_copy(sp2));\n    sp2 = isl_space_align_params(sp2, isl_space_copy(sp1));\n    // Create the space access_domain -> sched_range.\n    isl_space *sp = isl_space_map_from_domain_and_range(\n            isl_space_copy(sp1), isl_space_copy(sp2));\n    isl_map *adapter = isl_map_universe(sp);\n    polyfp::str_dump(\"Transformation map:\", isl_map_to_str(adapter));\n    isl_space *sp_map = isl_map_get_space(adapter);\n    isl_local_space *l_sp = isl_local_space_from_space(sp_map);\n    // Add equality constraints.\n    for (int i = 0; i < isl_space_dim(sp1, isl_dim_set); i++)\n    {\n        if (isl_space_has_dim_id(sp1, isl_dim_set, i) == true)\n        {\n            for (int j = 0; j < isl_space_dim (sp2, isl_dim_set); j++)\n            {\n                if (isl_space_has_dim_id(sp2, isl_dim_set, j) == true)\n                {\n                    isl_id *id1 = isl_space_get_dim_id(sp1, isl_dim_set, i);\n                    isl_id *id2 = isl_space_get_dim_id(sp2, isl_dim_set, j);\n                    if (strcmp(isl_id_get_name(id1), isl_id_get_name(id2)) == 0)\n                    {\n                        isl_constraint *cst = isl_equality_alloc(\n                                isl_local_space_copy(l_sp));\n                        cst = isl_constraint_set_coefficient_si(cst,\n                                                                isl_dim_in,\n                                                                i, 1);\n                        cst = isl_constraint_set_coefficient_si(\n                                cst, isl_dim_out, j, -1);\n                        adapter = isl_map_add_constraint(adapter, cst);\n                    }\n                    isl_id_free(id1);\n                    isl_id_free(id2);\n                }\n            }\n        }\n    }\n\n    isl_space_free(sp1);\n    isl_space_free(sp2);\n    isl_local_space_free(l_sp);\n\n    // polyfp::str_dump(\"Transformation map after adding equality constraints:\",\n    //         isl_map_to_str(adapter)));\n\n\n    return adapter;\n}\n\nisl_constraint *generator::get_constraint_for_access(int access_dimension,\n                                                     const polyfp::expr &access_expression,\n                                                     isl_map *access_relation,\n                                                     isl_constraint *cst,\n                                                     int coeff,\n                                                     const polyfp::function *fct)\n{\n    if (access_expression.get_expr_type() == polyfp::e_val)\n    {\n        int64_t val = coeff * access_expression.get_int_val() -\n                      isl_val_get_num_si(isl_constraint_get_constant_val(cst));\n        cst = isl_constraint_set_constant_si(cst, -val);\n    //  polyfp::str_dump(\"Assigning -(coeff * access_expression.get_int_val() - isl_val_get_num_si(isl_constraint_get_constant_val(cst))) to the cst dimension. The value assigned is : \"\n    //                                 + std::to_string(-val));\n    }\n    else if (access_expression.get_expr_type() == polyfp::e_var)\n    {\n        assert(!access_expression.get_name().empty());\n\n        int dim0 = isl_space_find_dim_by_name(isl_map_get_space(access_relation),\n                                              isl_dim_in,\n                                              access_expression.get_name().c_str());\n        if (dim0 >= 0)\n        {\n            int current_coeff = -isl_val_get_num_si(isl_constraint_get_coefficient_val(cst, isl_dim_in, dim0));\n            coeff = current_coeff + coeff;\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_in, dim0, -coeff);\n           \n        }\n        else\n        {\n            access_relation = isl_map_add_dims(access_relation, isl_dim_param, 1);\n            int pos = isl_map_dim(access_relation, isl_dim_param);\n            isl_id *param_id = isl_id_alloc(fct->get_isl_ctx(), access_expression.get_name().c_str (), NULL);\n            access_relation = isl_map_set_dim_id(access_relation, isl_dim_param, pos - 1, param_id);\n            isl_local_space *ls2 = isl_local_space_from_space(isl_map_get_space(access_relation));\n            cst = isl_constraint_alloc_equality(ls2);\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_param, pos - 1, -coeff);\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, access_dimension, 1);         \n        }\n    }\n    else if (access_expression.get_expr_type() == polyfp::e_op)\n    {\n        if (access_expression.get_op_type() == polyfp::o_add)\n        {\n            polyfp::expr op0 = access_expression.get_operand(0);\n            polyfp::expr op1 = access_expression.get_operand(1);\n            cst = generator::get_constraint_for_access(access_dimension, op0, access_relation, cst, coeff, fct);\n            isl_constraint_dump(cst);\n            cst = generator::get_constraint_for_access(access_dimension, op1, access_relation, cst, coeff, fct);\n            isl_constraint_dump(cst);\n        }\n        else if (access_expression.get_op_type() == polyfp::o_sub)\n        {\n            polyfp::expr op0 = access_expression.get_operand(0);\n            polyfp::expr op1 = access_expression.get_operand(1);\n            cst = generator::get_constraint_for_access(access_dimension, op0, access_relation, cst, coeff, fct);\n            cst = generator::get_constraint_for_access(access_dimension, op1, access_relation, cst, -coeff, fct);\n        }\n        else if (access_expression.get_op_type() == polyfp::o_mul)\n        {\n            polyfp::expr op0 = access_expression.get_operand(0);\n            polyfp::expr op1 = access_expression.get_operand(1);\n            if (op0.get_expr_type() == polyfp::e_val)\n            {\n                coeff = coeff * op0.get_int_val();\n                cst = generator::get_constraint_for_access(access_dimension, op1, access_relation, cst, coeff, fct);\n            }\n            else if (op1.get_expr_type() == polyfp::e_val)\n            {\n                coeff = coeff * op1.get_int_val();\n                cst = generator::get_constraint_for_access(access_dimension, op0, access_relation, cst, coeff, fct);\n            }\n        }\n        else\n        {\n            ERROR(\"Currently only Add, Sub, Minus, and Mul operations for accesses are supported for now.\", true);\n        }\n    }\n\n    return cst;\n}\n\nvoid generator::traverse_expr_and_extract_accesses(const polyfp::function *fct,\n                                                   const polyfp::compute *comp,\n                                                   const polyfp::expr &exp,\n                                                   std::vector<isl_map *> &accesses,\n                                                   bool return_buffer_accesses)\n{\n    assert(fct != NULL);\n    assert(comp != NULL);\n\n    if ((exp.get_expr_type() == polyfp::e_op) && ((exp.get_op_type() == polyfp::o_access) ||                                                 \n                                                    (exp.get_op_type() == polyfp::o_placeholder)))\n    {\n       \n        std::vector<polyfp::compute *> computations_vector = fct->get_computation_by_name(exp.get_name());\n\n        if (computations_vector.size() == 0)\n        {\n            // Search for update computations.\n            computations_vector = fct->get_computation_by_name(\"_\" + exp.get_name() + \"_update_0\");\n            assert((computations_vector.size() > 0) && \"Computation not found.\");\n        }\n        polyfp::compute *access_op_comp = computations_vector[0];\n\n        isl_set *lhs_comp_domain = isl_set_universe(isl_set_get_space(comp->get_iteration_domain()));\n        isl_set *rhs_comp_domain = isl_set_universe(isl_set_get_space(\n                access_op_comp->get_iteration_domain()));\n        isl_map *access_map = create_map_from_domain_and_range(lhs_comp_domain, rhs_comp_domain);\n        isl_set_free(lhs_comp_domain);\n        isl_set_free(rhs_comp_domain);\n\n        isl_map *access_to_comp = isl_map_universe(isl_map_get_space(access_map));\n        isl_map_free(access_map);\n\n        // The dimension_number is a counter that indicates to which dimension\n        // is the access associated.\n        int access_dimension = 0;\n        for (const auto &access : exp.get_access())\n        {\n            \n            isl_constraint *cst = isl_constraint_alloc_equality(isl_local_space_from_space(isl_map_get_space(\n                    access_to_comp)));\n            cst = isl_constraint_set_coefficient_si(cst, isl_dim_out, access_dimension, 1);\n            cst = generator::get_constraint_for_access(access_dimension, access, access_to_comp, cst, 1, fct);\n            access_to_comp = isl_map_add_constraint(access_to_comp, cst);\n            access_dimension++;\n        }\n\n        if (return_buffer_accesses)\n        {\n            isl_map *access_to_buff = isl_map_copy(access_op_comp->get_access_relation());\n\n            access_to_buff = isl_map_apply_range(isl_map_copy(access_to_comp), access_to_buff);\n            accesses.push_back(access_to_buff);\n            isl_map_free(access_to_comp);\n        }\n        else\n        {\n            accesses.push_back(access_to_comp);\n        }\n    }\n    else if (exp.get_expr_type() == polyfp::e_op)\n    {\n        switch (exp.get_op_type())\n        {\n            case polyfp::o_max:\n            case polyfp::o_min:\n            case polyfp::o_add:\n            case polyfp::o_sub:\n            case polyfp::o_mul:\n            case polyfp::o_div:\n            case polyfp::o_mod:\n            default:\n                ERROR(\"Extracting access function from an unsupported polyfp expression.\", 1);\n        }\n    }\n}\n\npolyfp::expr utility::get_bound(isl_set *set, int dim, int upper)\n{\n    assert(set != NULL);\n    assert(dim >= 0);\n    assert(dim < isl_space_dim(isl_set_get_space(set), isl_dim_set));\n    assert(isl_set_is_empty(set) == isl_bool_false);\n\n    polyfp::expr e = polyfp::expr();\n    isl_ast_build *ast_build;\n    isl_ctx *ctx = isl_set_get_ctx(set);\n    ast_build = isl_ast_build_alloc(ctx);\n\n    // Create identity map for set.\n    isl_space *sp = isl_set_get_space(set);\n    isl_map *sched = isl_map_identity(isl_space_copy(isl_space_map_from_set(sp)));\n    sched = isl_map_set_tuple_name(sched, isl_dim_out, \"\");\n\n    // Generate the AST.\n    isl_options_set_ast_build_atomic_upper_bound(ctx, 1);\n    isl_options_get_ast_build_exploit_nested_bounds(ctx);\n    isl_options_set_ast_build_group_coscheduled(ctx, 1);\n    isl_options_set_ast_build_allow_else(ctx, 1);\n    isl_options_set_ast_build_detect_min_max(ctx, 1);\n\n    // Intersect the iteration domain with the domain of the schedule.\n    isl_map *map =\n        isl_map_intersect_domain(\n            isl_map_copy(sched),\n            isl_set_copy(set));\n\n    // Set iterator names\n    int length = isl_map_dim(map, isl_dim_out);\n    isl_id_list *iterators = isl_id_list_alloc(ctx, length);\n\n    for (int i = 0; i < length; i++)\n    {\n        std::string name;\n        if (isl_set_has_dim_name(set, isl_dim_set, i) == true)\n            name = isl_set_get_dim_name(set, isl_dim_set, i);\n        else\n            name = generate_new_variable_name();\n        isl_id *id = isl_id_alloc(ctx, name.c_str(), NULL);\n        iterators = isl_id_list_add(iterators, id);\n    }\n\n    ast_build = isl_ast_build_set_iterators(ast_build, iterators);\n\n    isl_ast_node *node = isl_ast_build_node_from_schedule_map(ast_build, isl_union_map_from_map(map));\n    e = utility::extract_bound_expression(node, dim, upper);\n    isl_ast_build_free(ast_build);\n\n    assert(e.is_defined() && \"The computed bound expression is undefined.\");\n\n    return e;\n}\n\npolyfp::expr utility::extract_bound_expression(isl_ast_node *node, int dim, bool upper)\n{\n    assert(node != NULL);\n    assert(dim >= 0);\n\n    polyfp::expr result;\n\n    if (isl_ast_node_get_type(node) == isl_ast_node_block)\n    {\n        ERROR(\"Currently Tiramisu does not support extracting bounds from blocks.\", true);\n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_for)\n    {\n        isl_ast_expr *init_bound = isl_ast_node_for_get_init(node);\n        isl_ast_expr *upper_bound = isl_ast_node_for_get_cond(node);\n      \n        if (dim == 0)\n        {\n            if (upper)\n            {\n                isl_ast_expr *cond = isl_ast_node_for_get_cond(node);\n\n                if (isl_ast_expr_get_op_type(cond) == isl_ast_op_lt)\n                {\n                    // Create an expression of \"1\".\n                    isl_val *one = isl_val_one(isl_ast_node_get_ctx(node));\n                    // Add 1 to the ISL ast upper bound to transform it into a strinct bound.\n                    result = polyfp_expr_from_isl_ast_expr(isl_ast_expr_sub(isl_ast_expr_get_op_arg(cond, 1),\n                                                             isl_ast_expr_from_val(one)));\n                }\n                else if (isl_ast_expr_get_op_type(cond) == isl_ast_op_le)\n                {\n                    result = polyfp_expr_from_isl_ast_expr(isl_ast_expr_get_op_arg(cond, 1));\n                }\n            }\n            else\n            {\n                isl_ast_expr *init = isl_ast_node_for_get_init(node);\n                result = polyfp_expr_from_isl_ast_expr(init);\n            }\n        }\n        else\n        {\n            isl_ast_node *body = isl_ast_node_for_get_body(node);\n            result = utility::extract_bound_expression(body, dim-1, upper);\n            isl_ast_node_free(body);\n        }\n\n        assert(result.is_defined());\n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_user)\n    {\n        ERROR(\"Cannot extract bounds from a isl_ast_user node.\", true);\n    }\n    else if (isl_ast_node_get_type(node) == isl_ast_node_if)\n    {\n        // polyfp::expr cond_bound = polyfp_expr_from_isl_ast_expr(isl_ast_node_if_get_cond(node));\n        polyfp::expr then_bound = utility::extract_bound_expression(isl_ast_node_if_get_then(node), dim, upper);\n\n        polyfp::expr else_bound;\n        if (isl_ast_node_if_has_else(node))\n        {\n            // else_bound = utility::extract_bound_expression(isl_ast_node_if_get_else(node), dim, upper);\n            // result = polyfp::expr(polyfp::o_s, cond_bound, then_bound, else_bound);\n            ERROR(\"If Then Else is unsupported in bound extraction.\", true);\n        }\n        else\n            result = then_bound; //polyfp::expr(polyfp::o_cond, cond_bound, then_bound);\n    }\n\n    return result;\n}\n\nstd::string utility::get_parameters_list(isl_set *set)\n{\n    std::string list = \"\";\n\n    assert(set != NULL);\n\n    for (int i = 0; i < isl_set_dim(set, isl_dim_param); i++)\n    {\n        list += isl_set_get_dim_name(set, isl_dim_param, i);\n        if ((i != isl_set_dim(set, isl_dim_param) - 1))\n        {\n            list += \",\";\n        }\n    }\n\n    return list;\n}\n\npolyfp::expr polyfp_expr_from_isl_ast_expr(isl_ast_expr *isl_expr)\n{\n    polyfp::expr result;\n\n    if (isl_ast_expr_get_type(isl_expr) == isl_ast_expr_int)\n    {\n        isl_val *init_val = isl_ast_expr_get_val(isl_expr);\n        result = value_cast(polyfp::global::get_loop_iterator_data_type(), isl_val_get_num_si(init_val));\n        isl_val_free(init_val);\n    }\n    else if (isl_ast_expr_get_type(isl_expr) == isl_ast_expr_id)\n    {\n        isl_id *identifier = isl_ast_expr_get_id(isl_expr);\n        std::string name_str(isl_id_get_name(identifier));\n        isl_id_free(identifier);\n        // TODO\n        // result = polyfp::var(polyfp::global::get_loop_iterator_data_type(), name_str);\n    }\n    else if (isl_ast_expr_get_type(isl_expr) == isl_ast_expr_op)\n    {\n        polyfp::expr op0, op1, op2;\n        std::vector<polyfp::expr> new_arguments;\n\n        isl_ast_expr *expr0 = isl_ast_expr_get_op_arg(isl_expr, 0);\n        op0 = polyfp_expr_from_isl_ast_expr(expr0);\n        isl_ast_expr_free(expr0);\n\n        if (isl_ast_expr_get_op_n_arg(isl_expr) > 1)\n        {\n            isl_ast_expr *expr1 = isl_ast_expr_get_op_arg(isl_expr, 1);\n            op1 = polyfp_expr_from_isl_ast_expr(expr1);\n            isl_ast_expr_free(expr1);\n        }\n\n        if (isl_ast_expr_get_op_n_arg(isl_expr) > 2)\n        {\n            isl_ast_expr *expr2 = isl_ast_expr_get_op_arg(isl_expr, 2);\n            op2 = polyfp_expr_from_isl_ast_expr(expr2);\n            isl_ast_expr_free(expr2);\n        }\n\n        switch (isl_ast_expr_get_op_type(isl_expr))\n        {\n            case isl_ast_op_max:\n                result = polyfp::expr(polyfp::o_max, op0, op1);\n                break;\n            case isl_ast_op_min:\n                result = polyfp::expr(polyfp::o_min, op0, op1);\n                break;\n            case isl_ast_op_add:\n                result = polyfp::expr(polyfp::o_add, op0, op1);\n                break;\n            case isl_ast_op_sub:\n                result = polyfp::expr(polyfp::o_sub, op0, op1);\n                break;\n            case isl_ast_op_mul:\n                result = polyfp::expr(polyfp::o_mul, op0, op1);\n                break;\n            case isl_ast_op_div:\n                result = polyfp::expr(polyfp::o_div, op0, op1);\n                break;\n            default:\n                polyfp::str_dump(\"Transforming the following expression\",\n                                   (const char *)isl_ast_expr_to_C_str(isl_expr));\n                polyfp::str_dump(\"\\n\");\n                ERROR(\"Translating an unsupported ISL expression into a Tiramisu expression.\", 1);\n        }\n    }\n    else\n    {\n        polyfp::str_dump(\"Transforming the following expression\",\n                           (const char *)isl_ast_expr_to_C_str(isl_expr));\n        polyfp::str_dump(\"\\n\");\n        ERROR(\"Translating an unsupported ISL expression into a Tiramisu expression.\", 1);\n    }\n\n\n    return result;\n}\n}"
  },
  {
    "path": "lib/polyhedral/placeholer.cpp",
    "content": "#include \"placeholder.h\"\n#include <iostream>\nnamespace polyfp{\n\n\npolyfp::placeholder::placeholder(std::string name, std::vector<int64_t> dim_sizes,\n                         polyfp::primitive_t type, polyfp::function *fct):\n                         dim_sizes(dim_sizes), fct(fct),\n                         name(name), type(type)\n{\n    if(fct->fct_argument_added == false)\n    {\n        fct->add_fct_argument(std::pair<std::string, polyfp::placeholder *>(name, this));\n        fct->add_placeholder(std::pair<std::string, polyfp::placeholder *>(name, this));\n    }\n    else\n    {\n        fct->add_global_argument(std::pair<std::string, polyfp::placeholder *>(name, this));\n        fct->add_placeholder(std::pair<std::string, polyfp::placeholder *>(name, this));\n    }   \n    \n}\n\nvoid placeholder::partition(std::vector<int> factors, std::string type){\n    //TODO: CHECK DIMENSIONS AND WARNING\n    std::vector<std::string> types;\n    for (int dim = 0; dim < factors.size(); ++dim) {\n        types.push_back(type);\n    }\n    this->fct->set_partition(this->get_name(),factors,types);\n\n}\n\n// TODO\nvoid placeholder::partition(std::vector<int> factors, std::vector<std::string> types){\n    //TODO: CHECK DIMENSIONS AND WARNING\n    this->fct->set_partition(this->get_name(),factors,types);\n\n}\n\n\nconst std::string &placeholder::get_name() const\n{\n    return name;\n}\n\n\nint placeholder::get_n_dims() const\n{\n    return this->get_dim_sizes().size();\n}\n\n\npolyfp::primitive_t placeholder::get_elements_type() const\n{\n    return type;\n}\n\n\nconst std::vector<int64_t> &placeholder::get_dim_sizes() const\n{\n    return dim_sizes;\n}\n\nvoid polyfp::placeholder::dump(bool exhaustive) const\n{\n    if (exhaustive)\n    {\n        std::cout << \"Buffer \\\"\" << this->name\n                  << \"\\\", Number of dimensions: \" << this->get_n_dims()\n                  << std::endl;\n\n        std::cout << \"Dimension sizes: \";\n        for (const auto &size : dim_sizes)\n        {\n            std::cout << \"    \";\n        }\n        std::cout << std::endl;\n\n        std::cout << \"Elements type: \"\n                  << str_from_polyfp_type_primitive(this->type) << std::endl;\n\n        std::cout << std::endl << std::endl;\n    }\n}\n// const std::string &p_max::get_name() const\n// {\n//     return name;\n// }\n// polyfp::p_max::p_max(polyfp::expr expr1, polyfp::expr expr2)\n// {\n\n//     this->arg_list.push_back(expr1);\n//     this->arg_list.push_back(expr2);\n// }\n\n// int p_max::get_n_args() const\n// {\n//     return this->arg_list.size();\n// }\n\n\n}\n\n"
  },
  {
    "path": "lib/polyhedral/test.cpp",
    "content": "// #include <iostream>\n#include <stdio.h> \nint cf_test()\n{\n\tprintf(\"hello python!\\n\");\n\treturn 0;\n}\n////////////////////////////////////"
  },
  {
    "path": "results-gen.sh",
    "content": "\nstart_time=$(date +\"%s\")\n\necho \"\"\necho \">>> Step 5. Collecting experimental results...\"\necho \"\"\n\ndeclare -A baseline_latency\nbaseline_latency[\"gemm\",32]=498753\nbaseline_latency[\"gemm\",64]=3960961\nbaseline_latency[\"gemm\",128]=31572225\nbaseline_latency[\"gemm\",256]=252117505\nbaseline_latency[\"gemm\",512]=2015101953\nbaseline_latency[\"gemm\",1024]=19337840641\nbaseline_latency[\"gemm\",2048]=154660769793\nbaseline_latency[\"gemm\",4096]=1237118361601\nbaseline_latency[\"gemm\",8192]=9896275755009\n\nbaseline_latency[\"bicg\",32]=12353\nbaseline_latency[\"bicg\",64]=49281\nbaseline_latency[\"bicg\",128]=196865\nbaseline_latency[\"bicg\",256]=786945\nbaseline_latency[\"bicg\",512]=3146753\nbaseline_latency[\"bicg\",1024]=14682113\nbaseline_latency[\"bicg\",2048]=58724353\nbaseline_latency[\"bicg\",4096]=234889217\nbaseline_latency[\"bicg\",8192]=939540481\n\nbaseline_latency[\"gesummv\",32]=12705\nbaseline_latency[\"gesummv\",64]=49985\nbaseline_latency[\"gesummv\",128]=198273\nbaseline_latency[\"gesummv\",256]=789761\nbaseline_latency[\"gesummv\",512]=3152385\nbaseline_latency[\"gesummv\",1024]=14693377\nbaseline_latency[\"gesummv\",2048]=58746881\nbaseline_latency[\"gesummv\",4096]=234934273\nbaseline_latency[\"gesummv\",8192]=939630593\n\nbaseline_latency[\"2mm\",32]=697474\nbaseline_latency[\"2mm\",64]=5542146\nbaseline_latency[\"2mm\",128]=44188162\nbaseline_latency[\"2mm\",256]=352912386\nbaseline_latency[\"2mm\",512]=2820933634\nbaseline_latency[\"2mm\",1024]=29004664834\nbaseline_latency[\"2mm\",2048]=231982768130\nbaseline_latency[\"2mm\",4096]=2199241375746\nbaseline_latency[\"2mm\",8192]=17593058492418\n\nbaseline_latency[\"3mm\",32]=1087683\nbaseline_latency[\"3mm\",64]=8675715\nbaseline_latency[\"3mm\",128]=69305091\nbaseline_latency[\"3mm\",256]=554042883\nbaseline_latency[\"3mm\",512]=4430760963\nbaseline_latency[\"3mm\",1024]=45106599939\nbaseline_latency[\"3mm\",2048]=360815013891\nbaseline_latency[\"3mm\",4096]=2886369042435\nbaseline_latency[\"3mm\",8192]=23090348212227\n\n\nbaseline_latency[\"jacobi\",4096]=804925441\nbaseline_latency[\"jacobi2d\",4096]=4668429217793\nbaseline_latency[\"heat\",4096]=385699841\nbaseline_latency[\"seidel\",4096]=4050540986369\nbaseline_latency[\"blur\",4096]=3981925375233\nbaseline_latency[\"edgeDetect\",4096]=2882880170\nbaseline_latency[\"gaussian\",4096]=8694375278\nbaseline_latency[\"blur\",4096]=2983445186\nbaseline_latency[\"vgg16\",512]=3727670833\nbaseline_latency[\"resnet\",512]=6602277677\n\ndeclare -A execution_times\n\nwhile read -r line; do\n    if [[ $line =~ time\\[(.*),(.*)\\] ]]; then\n        kernel=${BASH_REMATCH[1]}\n        size=${BASH_REMATCH[2]}\n        if [[ $line =~ ([0-9.]+)$ ]]; then\n            time=${BASH_REMATCH[1]}\n            time=$(printf \"%.2f\" $time)\n            if [[ $time == .* ]]; then\n                time=\"0$time\"\n            fi\n            execution_times[$kernel,$size]=$time\n        fi\n    fi\ndone < \"build/execution_times.txt\"\n\n\nresult_file=\"experimental_results.csv\"\n# rm -f $result_file\nif [ -f $result_file ]; then\n    rm $result_file\nfi\nprintf \"/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\n\" >> $result_file\nprintf \"/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\n\" >> $result_file\n# printf \"------------------------------Experimental Results----------------------------\" >> $result_file\n\nprintf \"\\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \">>> Results for TABLE III: \\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \"%-20s %-25s %-20s %-20s %-20s %-20s %-15s %-15s\\n\" \"Kernel\" \"Latency\" \"DSP\" \"FF\" \"LUT\" \"Power\" \"II\" \"Execution Time\" >> $result_file\n\nkernels=(\"gemm\" \"bicg\" \"gesummv\" \"2mm\" \"3mm\" )\nsizes=(4096)\n\nfor kernel in \"${kernels[@]}\"\ndo\n    for size in \"${sizes[@]}\"\n    do\n        xml_file=\"samples/${kernel}/test_${kernel}_${size}/test_${kernel}_${size}/syn/report/csynth.xml\"\n        baseline=${baseline_latency[$kernel,$size]}\n        best_case_latency=$(xmlstarlet sel -t -v \"/profile/PerformanceEstimates/SummaryOfOverallLatency/Best-caseLatency\" $xml_file)\n        acceleration=$(awk \"BEGIN {printf \\\"%.1f\\\", $baseline / $best_case_latency}\")\n        acceleration_str=\"$acceleration x\"\n        latency_str=\"$best_case_latency($acceleration_str)\"\n        lut_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/LUT\" $xml_file)\n        lut_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/LUT\" $xml_file)\n        lut_percent=$((($lut_util * 100) / $lut_avail))\n        lut_str=\"$lut_util($lut_percent%)\"\n        dsp_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/DSP\" $xml_file)\n        dsp_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/DSP\" $xml_file)\n        dsp_percent=$((($dsp_util * 100) / $dsp_avail))\n        dsp_str=\"$dsp_util($dsp_percent%)\"\n        ff_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/FF\" $xml_file)\n        ff_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/FF\" $xml_file)\n        ff_percent=$((($ff_util * 100) / $ff_avail))\n        ff_str=\"$ff_util($ff_percent%)\"\n        II=$(grep -m 1 -oP '(?<=<PipelineII>).*?(?=<\\/PipelineII>)' $xml_file)\n        time=${execution_times[$kernel,$size]}\n        time_str=\"$time s\"\n\n        rpt_file=\"samples/${kernel}/test_${kernel}_power/test_${kernel}_power/impl/verilog/project.runs/impl_1/bd_0_wrapper_power_routed.rpt\"\n        total_power=$(grep \"Total On-Chip Power (W)\" \"$rpt_file\" | awk -F'|' '{print $3}' | tr -d '[:space:]')\n        power_str=\"$total_power W\"\n        # echo \"Total On-Chip Power: $total_power W\"\n        printf \"%-20s %-25s %-20s %-20s %-20s %-20s %-15s %-15s\\n\" \"${kernel}_${size}\" \"${latency_str}\" \"$dsp_str\" \"$ff_str\" \"$lut_str\" \"$power_str\" \"$II\" \"$time_str\" >> $result_file\n    done\ndone\nprintf \"\\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \">>> Results for Fig. 12:\\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \"%-20s %-25s %-20s %-20s %-20s %-15s %-15s\\n\" \"Kernel\" \"Latency\" \"DSP\" \"FF\" \"LUT\" \"II\" \"Execution Time\">> $result_file\n\nkernels=(\"gemm\" \"bicg\" \"gesummv\" \"2mm\" \"3mm\" )\nsizes=(32 64 128 256 512 1024 2048 4096 8192)\n\nfor kernel in \"${kernels[@]}\"\ndo\n    for size in \"${sizes[@]}\"\n    do\n        xml_file=\"samples/${kernel}/test_${kernel}_${size}/test_${kernel}_${size}/syn/report/csynth.xml\"\n        baseline=${baseline_latency[$kernel,$size]}\n        best_case_latency=$(xmlstarlet sel -t -v \"/profile/PerformanceEstimates/SummaryOfOverallLatency/Best-caseLatency\" $xml_file)\n        acceleration=$(awk \"BEGIN {printf \\\"%.1f\\\", $baseline / $best_case_latency}\")\n        acceleration_str=\"$acceleration x\"\n        latency_str=\"$best_case_latency($acceleration_str)\"\n        lut_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/LUT\" $xml_file)\n        lut_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/LUT\" $xml_file)\n        lut_percent=$((($lut_util * 100) / $lut_avail))\n        lut_str=\"$lut_util($lut_percent%)\"\n        dsp_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/DSP\" $xml_file)\n        dsp_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/DSP\" $xml_file)\n        dsp_percent=$((($dsp_util * 100) / $dsp_avail))\n        dsp_str=\"$dsp_util($dsp_percent%)\"\n        ff_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/FF\" $xml_file)\n        ff_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/FF\" $xml_file)\n        ff_percent=$((($ff_util * 100) / $ff_avail))\n        ff_str=\"$ff_util($ff_percent%)\"\n        II=$(grep -m 1 -oP '(?<=<PipelineII>).*?(?=<\\/PipelineII>)' $xml_file)\n        time=${execution_times[$kernel,$size]}\n        time_str=\"$time s\"\n\n        printf \"%-20s %-25s %-20s %-20s %-20s %-15s %-15s\\n\" \"${kernel}_${size}\" \"${latency_str}\" \"$dsp_str\" \"$ff_str\" \"$lut_str\" \"$II\" \"$time_str\" >> $result_file\n    done\n    printf \"\\n\" >> $result_file\ndone\nprintf \"\\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \">>> Results for TABLE V and TABLE VII: \\n\" >> $result_file\nprintf \"\\n\" >> $result_file\nprintf \"%-20s %-25s %-20s %-20s %-20s %-15s %-15s\\n\" \"Kernel\" \"Latency\" \"DSP\" \"FF\" \"LUT\" \"II\" \"Execution Time\">> $result_file\n\n\nkernels=(\"vgg16\"  \"resnet\")\nsizes=(512)\nfor kernel in \"${kernels[@]}\"\ndo\n    for size in \"${sizes[@]}\"\n    do\n        xml_file=\"samples/${kernel}/test_${kernel}_${size}/test_${kernel}_${size}/syn/report/csynth.xml\"\n        baseline=${baseline_latency[$kernel,$size]}\n        best_case_latency=$(xmlstarlet sel -t -v \"/profile/PerformanceEstimates/SummaryOfOverallLatency/Best-caseLatency\" $xml_file)\n        acceleration=$(awk \"BEGIN {printf \\\"%.1f\\\", $baseline / $best_case_latency}\")\n        acceleration_str=\"$acceleration x\"\n        latency_str=\"$best_case_latency($acceleration_str)\"\n        lut_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/LUT\" $xml_file)\n        lut_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/LUT\" $xml_file)\n        lut_percent=$((($lut_util * 100) / $lut_avail))\n        lut_str=\"$lut_util($lut_percent%)\"\n        dsp_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/DSP\" $xml_file)\n        dsp_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/DSP\" $xml_file)\n        dsp_percent=$((($dsp_util * 100) / $dsp_avail))\n        dsp_str=\"$dsp_util($dsp_percent%)\"\n        ff_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/FF\" $xml_file)\n        ff_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/FF\" $xml_file)\n        ff_percent=$((($ff_util * 100) / $ff_avail))\n        ff_str=\"$ff_util($ff_percent%)\"\n        II=$(grep -m 1 -oP '(?<=<PipelineII>).*?(?=<\\/PipelineII>)' $xml_file)\n        time=${execution_times[$kernel,$size]}\n        time_str=\"$time s\"\n\n        printf \"%-20s %-25s %-20s %-20s %-20s %-15s %-15s\\n\" \"${kernel}_${size}\" \"${latency_str}\" \"$dsp_str\" \"$ff_str\" \"$lut_str\"  \"$II\" \"$time_str\">> $result_file\n    done\ndone\n\n\n\n\nkernels=(\"edgeDetect\" \"gaussian\" \"blur\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\nsizes=(4096)\n\nfor kernel in \"${kernels[@]}\"\ndo\n    for size in \"${sizes[@]}\"\n    do\n        xml_file=\"samples/${kernel}/test_${kernel}_${size}/test_${kernel}_${size}/syn/report/csynth.xml\"\n        baseline=${baseline_latency[$kernel,$size]}\n        best_case_latency=$(xmlstarlet sel -t -v \"/profile/PerformanceEstimates/SummaryOfOverallLatency/Best-caseLatency\" $xml_file)\n        acceleration=$(awk \"BEGIN {printf \\\"%.1f\\\", $baseline / $best_case_latency}\")\n        acceleration_str=\"$acceleration x\"\n        latency_str=\"$best_case_latency($acceleration_str)\"\n        lut_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/LUT\" $xml_file)\n        lut_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/LUT\" $xml_file)\n        lut_percent=$((($lut_util * 100) / $lut_avail))\n        lut_str=\"$lut_util($lut_percent%)\"\n        dsp_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/DSP\" $xml_file)\n        dsp_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/DSP\" $xml_file)\n        dsp_percent=$((($dsp_util * 100) / $dsp_avail))\n        dsp_str=\"$dsp_util($dsp_percent%)\"\n        ff_util=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/Resources/FF\" $xml_file)\n        ff_avail=$(xmlstarlet sel -t -v \"/profile/AreaEstimates/AvailableResources/FF\" $xml_file)\n        ff_percent=$((($ff_util * 100) / $ff_avail))\n        ff_str=\"$ff_util($ff_percent%)\"\n        II=$(grep -m 1 -oP '(?<=<PipelineII>).*?(?=<\\/PipelineII>)' $xml_file)\n        time=${execution_times[$kernel,$size]}\n        time_str=\"$time s\"\n        printf \"%-20s %-25s %-20s %-20s %-20s %-15s %-15s\\n\" \"${kernel}_${size}\" \"${latency_str}\" \"$dsp_str\" \"$ff_str\" \"$lut_str\" \"$II\" \"$time_str\">> $result_file\n    done\ndone\nprintf \"\\n\" >> $result_file\nprintf \">>> Notes:\\n\" >> $result_file\nprintf \"1. The Resnet speedup may be slightly different from the speedup in the paper. This is because we have modified some of the codegen methods and the \noverall latency is affected: we use fewer resources and achieve a slightly lower speedup. Note that the speedup of VGG-16 is sightly better than the speedup \nin the paper. \\n\" >> $result_file\nprintf \"2. We are improving the optimization strategies for loops with small problem sizes and a final strategy for them have not been decided yet. So some \nof the small-problem-size results may be slightly different from the results in the paper. \\n\" >> $result_file\nprintf \"\\n\" >> $result_file\n\nprintf \"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\n\" >> $result_file\n#printf \"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\n\" >> $result_file\nend_time=$(date +\"%s\")\nexecution_time=$(($end_time - $start_time))\necho \"\"\necho \">>> Step 5 has been finished!\"\necho \">>> Step 5 Total Execution Time: $execution_time seconds\"\necho \"\"\necho \"The experimental results are collected in experimental_results.csv!\"\n"
  },
  {
    "path": "run-code.sh",
    "content": "#!/usr/bin/env bash\nstart=$(date +\"%s\")\necho \"\"\necho \">>> Step 2. Compiling the object files and Generating the optimized HLS C code...\"\necho \"\"\ncd build\n\nif [ -f \"execution_times.txt\" ]; then\n    rm \"execution_times.txt\"\nfi\n\n\ntargets=(\"edgeDetect\" \"gaussian\" \"blur\" \"vgg16\"  \"resnet\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\nfor target in \"${targets[@]}\"\ndo\n    cmake --build . --target \"$target\"\ndone\n\n# Run building.\ntargets=(\"vgg16\"  \"resnet\")\nfor target in \"${targets[@]}\"\ndo  \n    start_time=$(date +%s.%N)\n    ./bin/\"$target\"\n    mlir_file=\"../samples/${target%.*}/test_${target%.*}.mlir\"\n    cpp_file=\"../samples/${target%.*}/test_${target%.*}.cpp\"\n    ../scalehls/build/bin/scalehls-opt $mlir_file \\\n        --scalehls-func-preprocess=\"top-func=test_${target%.*}\" \\\n        --cse -canonicalize \\\n        --scalehls-qor-estimation=\"target-spec=../samples/config.json\" \\\n        | ../scalehls/build/bin/scalehls-translate -emit-hlscpp > $cpp_file\n\n    end_time=$(date +%s.%N)\n    execution_time=$(echo \"$end_time - $start_time\" | bc)\n    echo \"time[$target,512] $execution_time\" >> execution_times.txt\n\n    echo \"The HLS C code of $target: test_${target%.*}.cpp is generated!\"\n    echo \"\"\ndone\n\ntargets=(\"edgeDetect\" \"gaussian\" \"blur\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\nN_value=4096\nfor target in \"${targets[@]}\"\ndo  \n    start_time=$(date +%s.%N)\n    ./bin/\"$target\"\n    mlir_file=\"../samples/${target%.*}/test_${target%.*}_$N_value.mlir\"\n    cpp_file=\"../samples/${target%.*}/test_${target%.*}_$N_value.cpp\"\n    ../scalehls/build/bin/scalehls-opt $mlir_file \\\n        --scalehls-func-preprocess=\"top-func=test_${target%.*}_$N_value\" \\\n        --cse -canonicalize \\\n        | ../scalehls/build/bin/scalehls-translate -emit-hlscpp > $cpp_file\n    \n    end_time=$(date +%s.%N)\n    execution_time=$(echo \"$end_time - $start_time\" | bc)\n    echo \"time[$target,4096] $execution_time\" >> execution_times.txt\n\n    echo \"The HLS C code of $target: test_${target%.*}_$N_value.cpp is generated!\"\n    echo \"\"\ndone\n\n\n# code_file=\"../samples/seidel/test_seidel_4096.cpp\"\n# target_file=\"../samples/seidel/test_seidel_4096.cpp\"\nfile_path=\"../samples/seidel/test_seidel_4096.cpp\"\n# Insert lines after the third for loop\nsed -i '/for (int v5 = max(0, ((v4 \\/ 2) - 2046)); v5 < min(4094, (v4 \\/ 2)); v5 += 1) {/a #pragma HLS PIPELINE II=1\\n#pragma HLS LOOP_TRIPCOUNT avg=1366 max=1366 min=1366' \"$file_path\"\n\n# Insert line before the first for loop\nsed -i '/for (int v3 = 0; v3 < 4096; v3 += 1) {/i #pragma HLS DEPENDENCE dependent=false type=inter variable=v1' \"$file_path\"\n\necho \"Lines inserted successfully into the file.\"\n\n\nN_values=(32 64 128 256 512 1024 2048 4096 8192)\n\ntargets=(\"2mm.cpp\" \"3mm.cpp\" \"gemm.cpp\" \"bicg.cpp\" \"gesummv.cpp\")\n\n\nexecute_command() {\n    source_file=$1\n    N=$2\n\n    sed -i \"s/#define N 4096/#define N $N/\" ../testbench/$source_file\n\n    cmake --build . --target ${source_file%.*}\n    start_time=$(date +%s.%N)\n    ./bin/${source_file%.*}\n\n    mlir_file=\"../samples/${source_file%.*}/test_${source_file%.*}_${N}.mlir\"\n    cpp_file=\"../samples/${source_file%.*}/test_${source_file%.*}_${N}.cpp\"\n    ../scalehls/build/bin/scalehls-opt $mlir_file \\\n        --scalehls-func-preprocess=\"top-func=test_${source_file%.*}_${N}\" \\\n        --cse -canonicalize \\\n        --scalehls-qor-estimation=\"target-spec=../samples/config.json\" \\\n        | ../scalehls/build/bin/scalehls-translate -emit-hlscpp > $cpp_file\n\n    sed -i \"s/#define N [0-9]*/#define N 4096/\" ../testbench/$source_file\n\n    end_time=$(date +%s.%N)\n    execution_time=$(echo \"$end_time - $start_time\" | bc)\n    echo \"time[${source_file%.*},$N] $execution_time\" >> execution_times.txt\n\n\n    echo \"\"\n    echo \"The HLS C code of $source_file: test_${source_file%.*}_${N}.cpp is generated!\"\n    echo \"\"\n}\n\n\nmax_parallel=1\n\nfor N in \"${N_values[@]}\"\ndo\n    for source_file in \"${targets[@]}\"\n    do\n        execute_command \"$source_file\" \"$N\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\nend=$(date +\"%s\")\nexecution=$(($end - $start))\necho \"\"\necho \">>> Step 2 has been finished!\"\necho \">>> Step 2 Total Execution Time: $execution seconds\"\necho \"\"\n\n\n"
  },
  {
    "path": "samples/config.json",
    "content": "{\n    \"__max_init_parallel\": \"The maximum loop parallelism in the initial sampling\",\n    \"max_init_parallel\": 32,\n    \"__max_expl_parallel\": \"The maximum loop parallelism in the exploration\",\n    \"max_expl_parallel\": 128,\n    \"__max_loop_parallel\": \"The maximum unroll factor of each loop\",\n    \"max_loop_parallel\": 16,\n    \"__max_iter_num\": \"The maximum iteration number in the exploration\",\n    \"frequency\": \"100MHz\",\n    \"dsp\": 220,\n    \"bram\": 180,\n    \"dsp_usage\": {\n        \"fadd\": 2,\n        \"fmul\": 3,\n        \"fdiv\": 0,\n        \"fcmp\": 0,\n        \"fexp\": 7\n    },\n    \"100MHz\": {\n        \"fadd\": 4,\n        \"fmul\": 3,\n        \"fdiv\": 15,\n        \"fcmp\": 1,\n        \"fexp\": 8,\n        \"iadd\": 1,\n        \"imul\": 2,\n        \"iadd_delay\": 1.8,\n        \"imul_delay\": 6.91,\n        \"fmul_delay\": 5.7,\n        \"fdiv_delay\": 6.07,\n        \"fcmp_delay\": 6.4,\n        \"fexp_delay\": 7.68\n    }\n}"
  },
  {
    "path": "tcl-gen.sh",
    "content": "#!/bin/bash\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Step 3. Generating scripts for running Vitis_HLS...\"\necho \"\"\n\nexamples=(\"vgg16\" \"resnet\")\nsizes=(512)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        script_name=\"script_${size}.tcl\"\n        \n        cat > \"samples/${example}/${script_name}\" <<EOL\nopen_project -reset \"test_${example}_${size}\"\nset_top test_${example}\nadd_files test_${example}.cpp\nopen_solution \"test_${example}_${size}\"\nset_part {xc7z020clg400-1}\ncreate_clock -period 10 -name default\nset_directive_pipeline -off \"test_${example}\"\ncsynth_design\nclose_project\nexit\nEOL\n    done\ndone\n\n#!/bin/bash\n\nexamples=(\"2mm\" \"3mm\" \"gemm\" \"bicg\" \"gesummv\")\nsizes=(32 64 128 256 512 1024 2048 4096 8192)\n\n# for example in \"${examples[@]}\"\n# do  \n#     for size in \"${sizes[@]}\"\n#     do\n#         script_name=\"script_${size}.tcl\"\n        \n#         cat > \"samples/${example}/${script_name}\" <<EOL\n# open_project -reset \"test_${example}_${size}\"\n# set_top test_${example}_${size}\n# add_files test_${example}_${size}.cpp\n# open_solution \"test_${example}_${size}\"\n# set_part {xc7z020clg400-1}\n# create_clock -period 10 -name default\n# csynth_design\n# EOL\n\n#         if [ \"$size\" -eq 4096 ]; then\n#             echo \"export_design -evaluate verilog -format ip_catalog -version 2.0.1\" >> \"samples/${example}/${script_name}\"\n#         fi\n\n#         echo \"close_project\" >> \"samples/${example}/${script_name}\"\n#         echo \"exit\" >> \"samples/${example}/${script_name}\"\n#     done\n# done\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        script_name=\"script_${size}.tcl\"\n        \n        cat > \"samples/${example}/${script_name}\" <<EOL\nopen_project -reset \"test_${example}_${size}\"\nset_top test_${example}_${size}\nadd_files test_${example}_${size}.cpp\nopen_solution \"test_${example}_${size}\"\nset_part {xc7z020clg400-1}\ncreate_clock -period 10 -name default\ncsynth_design\nclose_project\nexit\nEOL\n    done\ndone\n\nexamples=(\"2mm\" \"3mm\" \"gemm\" \"bicg\" \"gesummv\")\nsizes=(4096)\n\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        script_name=\"script_power.tcl\"\n        \n        cat > \"samples/${example}/${script_name}\" <<EOL\nopen_project -reset \"test_${example}_power\"\nset_top test_${example}_${size}\nadd_files test_${example}_${size}.cpp\nopen_solution \"test_${example}_power\"\nset_part {xc7z020clg400-1}\ncreate_clock -period 10 -name default\ncsynth_design\nEOL\n\n        if [ \"$size\" -eq 4096 ]; then\n            echo \"export_design -evaluate verilog -format ip_catalog -version 2.0.1\" >> \"samples/${example}/${script_name}\"\n        fi\n\n        echo \"close_project\" >> \"samples/${example}/${script_name}\"\n        echo \"exit\" >> \"samples/${example}/${script_name}\"\n    done\ndone\n\n\n\nexamples=(\"edgeDetect\" \"gaussian\" \"blur\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\nsizes=(4096)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        script_name=\"script_${size}.tcl\"\n        \n        cat > \"samples/${example}/${script_name}\" <<EOL\nopen_project -reset \"test_${example}_${size}\"\nset_top test_${example}_${size}\nadd_files test_${example}_${size}.cpp\nopen_solution \"test_${example}_${size}\"\nset_part {xc7z020clg400-1}\ncreate_clock -period 10 -name default\ncsynth_design\nclose_project\nexit\nEOL\n    done\ndone\n\n# echo \"\"\n# echo \">>> Step 3 has been finished!\"\n# echo \"\"\n\nend_time=$(date +\"%s\")\nexecution_time=$(($end_time - $start_time))\necho \"\"\necho \">>> Step 3 has been finished!\"\necho \">>> Step 3 Total Execution Time: $execution_time seconds\"\necho \"\"\n"
  },
  {
    "path": "testbench/2mm.cpp",
    "content": "#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <filesystem>\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <iostream>\n#include <string>\n\n#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n// #include \"mlir/IR/Attributes.h\"\n\n#define N 4096\n\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_2mm_\"+std::to_string(N);\n    init(name);\n\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,N);\n    var j(\"j\", 0 ,N);\n    var k(\"k\", 0 ,N);\n    placeholder A(\"A\",{N,N},p_float32);\n    placeholder B(\"B\",{N,N},p_float32);\n    placeholder C(\"C\",{N,N},p_float32);\n    placeholder D(\"D\",{N,N},p_float32);\n    placeholder temp(\"temp\",{N,N},p_float32);\n    constant alpha(1.6);\n    constant beta(3.7);\n    constant scalar(3.7);\n    compute s_1(\"s_1\",{i,j},scalar,temp(i,j));\n    compute s_2(\"s_2\",{i,j,k},temp(i,j)+alpha*A(i,k)*B(k,j),temp(i,j));\n    compute s_3(\"s_3\",{i,j},D(i,j)*beta,D(i,j));\n    compute s_4(\"s_4\",{i,j,k},D(i,j)+temp(i,k)*C(k,j),D(i,j));\n    s_2.after(s_1,-1);\n    s_3.after(s_2,-1);\n    s_4.after(s_3,-1);\n    // var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    // s_2.tile(k,i,j,1,2,16,i0, j0, k0, i1, j1,k1);\n    // s_4.tile(k,j,i,1,2,16,i0, j0, k0, i1, j1,k1);\n    // s_2.unroll(k1,-1);\n    // s_2.unroll(j1,-1);\n    // s_2.unroll(i1,-1);\n    // s_4.unroll(k1,-1);\n    // s_4.unroll(j1,-1);\n    // s_4.unroll(i1,-1);\n    // s_1.pipeline(j,1);\n    // s_1.pipeline(j,1);\n    // s_2.pipeline(j,1);\n    // s_3.pipeline(j,1);\n    // s_4.pipeline(j,1);\n    // A.partition({16,1},\"cyclic\");\n    // B.partition({1,2},\"cyclic\");\n    // C.partition({1,2},\"cyclic\");\n    // D.partition({16,2},\"cyclic\");\n    // temp.partition({16,2},\"cyclic\");\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/2mm/\";\n    fct->auto_DSE(path);\n    // codegen();\n}\n"
  },
  {
    "path": "testbench/3mm.cpp",
    "content": "#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <filesystem>\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <iostream>\n#include <string>\n\n#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n// #include \"mlir/IR/Attributes.h\"\n#define N 4096\n\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_3mm_\"+std::to_string(N);\n    init(name);\n\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,N);\n    var j(\"j\", 0 ,N);\n    var k(\"k\", 0 ,N);\n    placeholder A(\"A\",{N,N},p_float32);\n    placeholder B(\"B\",{N,N},p_float32);\n    placeholder C(\"C\",{N,N},p_float32);\n    placeholder D(\"D\",{N,N},p_float32);\n    placeholder E(\"E\",{N,N},p_float32);\n    placeholder F(\"F\",{N,N},p_float32);\n    placeholder G(\"G\",{N,N},p_float32);\n    constant scalar(0);\n    compute s_1(\"s_1\",{i,j},scalar,E(i,j));\n    compute s_2(\"s_2\",{i,j,k},E(i,j)+A(i,k)*B(k,j),E(i,j));\n    compute s_3(\"s_3\",{i,j},scalar,F(i,j));\n    compute s_4(\"s_4\",{i,j,k},F(i,j)+C(i,k)*D(k,j),F(i,j));\n    compute s_5(\"s_5\",{i,j},scalar,G(i,j));\n    compute s_6(\"s_6\",{i,j,k},G(i,j)+E(i,k)*F(k,j),G(i,j));\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    s_2.after(s_1,-1);\n    s_3.after(s_2,-1);\n    s_4.after(s_3,-1);\n    s_5.after(s_4,-1);\n    s_6.after(s_5,-1);\n    // s_3.after(s_1,j);\n    // s_2.after(s_1,-1);\n    // s_4.after(s_2,k);\n    \n    // s_2.tile(k,j,i,1,1,16,i0, j0, k0, i1, j1,k1);\n    // s_6.tile(j,i,2,16,i0, j0, k0, i1, j1,k1);\n    // s_2.unroll(k1,-1);\n    // s_2.unroll(j1,-1);\n    // s_2.unroll(i1,-1);\n    // s_4.unroll(k1,-1);\n    // s_4.unroll(j1,-1);\n    // s_4.unroll(i1,-1);\n    // s_2.pipeline(k0,1);\n    // s_4.pipeline(k0,1);\n    // A.partition({16,2},\"cyclic\");\n    // B.partition({2,2},\"cyclic\");\n    // C.partition({2,2},\"cyclic\");\n    // D.partition({16,2},\"cyclic\");\n    // temp.partition({16,2},\"cyclic\");\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/3mm/\";\n    fct->auto_DSE(path);\n    // codegen();\n}\n"
  },
  {
    "path": "testbench/bicg.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#define N 4096\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_bicg_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,N);\n    var j(\"j\", 0 ,N);\n\n    placeholder A(\"A\",{N,N},p_float32);\n    placeholder s(\"s\",{N},p_float32);\n    placeholder q(\"q\",{N},p_float32);\n    placeholder p(\"p\",{N},p_float32);\n    placeholder r(\"r\",{N},p_float32);\n\n\n    compute s_1(\"s_1\",{i,j},s(j)+A(i,j)*r(i),s(j));\n    compute s_2(\"s_2\",{i,j},q(i)+A(i,j)*p(j),q(i));\n    // compute s_2(\"s_2\",{i,j},q(j)+A(j,i)*p(i),q(j));\n    // s_2.interchange(i,j);\n    s_2.after(s_1,j);\n\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    // s_1.tile(i,j,1,32,i0, j0, i1, j1);\n    // s_2.tile(i,j,1,32,i0, j0, i1, j1);\n    // s_1.unroll(j1,-1);\n    // s_2.unroll(j1,-1);\n    // s_2.after(s_1,j1);\n    // s_1.pipeline(j,1);\n    // s.partition({32},\"cyclic\");\n    // q.partition({32},\"cyclic\");\n    // A.partition({16,16},\"cyclic\");\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/bicg/\";\n    fct->auto_DSE(path);\n\n}\n\n"
  },
  {
    "path": "testbench/blur.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\n#define N 4096\n\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_blur_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var j(\"j\", 0 ,4094);\n    var c(\"c\", 0 ,3);\n\n    placeholder bx(\"bx\",{N,N,3},p_float32);\n    placeholder by(\"by\",{N,N,3},p_float32);\n    placeholder in(\"in\",{N,N,3},p_float32);\n    constant factor(3.0);\n    \n    compute s_1(\"s_1\",{i,j,c},(in(i,j,c)+in(i,j+1,c)+in(i,j+2,c))/factor,bx(i,j,c));\n    compute s_2(\"s_2\",{i,j,c},(bx(i,j,c)+bx(i+1,j,c)+bx(i+2,j,c))/factor,by(i,j,c));\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    s_2.after(s_1,-1);\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/blur/\";\n    fct->auto_DSE(path);\n   \n}\n"
  },
  {
    "path": "testbench/edgeDetect.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define N 4096\nint main(){\n    std::string name = \"test_edgeDetect_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var j(\"j\", 0 ,4094);\n    var c(\"c\", 0 ,3);\n    placeholder temp(\"temp\",{4096,4096,3},p_float32);\n    placeholder src(\"src\",{4096,4096,3},p_float32);\n    placeholder out(\"out\",{4096,4096,3},p_float32);\n    constant factor(8.0);\n    compute s_1(\"s_1\",{i,j,c},(src(i,j,c)+src(i,j+1,c)+src(i,j+2,c)+src(i+1,j,c)+src(i+1,j+2,c)+\n                               src(i+2,j,c)+src(i+2,j+1,c)+src(i+2,j+2,c))/factor,temp(i,j,c));\n    compute s_2(\"s_2\",{i,j,c},temp(i+1,j+1,c)-temp(i+2,j,c)+\n                              temp(i+2,j+1,c)-temp(i+1,j,c),out(i,j,c));\n    s_2.after(s_1,-1);\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/edgeDetect/\";\n    fct->auto_DSE(path);\n\n}\n"
  },
  {
    "path": "testbench/gaussian.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define N 4096\nint main(){\n    std::string name = \"test_gaussian_\"+std::to_string(N);\n    init(name);\n\n    auto *fct = global::get_implicit_function();\n    var q(\"q\", 0 ,4089);\n    var w(\"w\", 0 ,4089);\n    var cc(\"cc\", 0 ,3);\n    var r(\"r\", 0 ,7);\n    var e(\"e\", 0 ,7);\n\n    placeholder temp(\"temp\",{4096,4096,3},p_float32);\n    placeholder src(\"src\",{4096,4096,3},p_float32);\n    placeholder conv(\"conv\",{4096,4096,3},p_float32);\n    placeholder kernelX(\"kernelX\",{7},p_float32);\n    placeholder kernelY(\"kernelY\",{7},p_float32);\n    constant scalar(0);\n    \n    compute s_1(\"s_1\",{q,w,cc},scalar,temp(q,w,cc));\n    compute s_2(\"s_2\",{q,w,cc},scalar,conv(q,w,cc));\n    compute s_3(\"s_3\",{q,w,cc,r},temp(q,w,cc)+src(q + r,w,cc)*kernelX(r),temp(q,w,cc));\n    compute s_4(\"s_4\",{q,w,cc,e},conv(q,w,cc)+temp(q,w+e,cc)*kernelY(e),conv(q,w,cc));\n    s_2.after(s_1,cc);\n    s_3.after(s_1,-1);\n    s_4.after(s_3,-1);\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/gaussian/\";\n    fct->auto_DSE(path);\n}\n\n"
  },
  {
    "path": "testbench/gemm.cpp",
    "content": "#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <filesystem>\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <iostream>\n#include <string>\n\n#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n// #include \"mlir/IR/Attributes.h\"\n#define N 4096\n\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_gemm_\"+std::to_string(N);\n    init(name);\n\n    auto fct = global::get_implicit_function();\n    var i(\"i\", 0 ,N);\n    var j(\"j\", 0 ,N);\n    var k(\"k\", 0 ,N);\n    placeholder A(\"A\",{N,N},p_float32);\n    placeholder B(\"B\",{N,N},p_float32);\n    placeholder C(\"C\",{N,N},p_float32);\n    constant alpha;\n    constant beta;\n    compute s_1(\"s_1\",{i,j},C(i,j)*beta,C(i,j));\n    compute s_2(\"s_2\",{i,j,k},C(i,j)+alpha*A(i,k)*B(k,j),C(i,j));\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    s_2.after(s_1,-1);\n    // s_2.tile(k,j,i,2,2,16,i0, j0, k0, i1, j1,k1);\n    // s_2.unroll(k1,-1);\n    // s_2.unroll(j1,-1);\n    // s_2.unroll(i1,-1);\n    // s_1.pipeline(j,1);\n    // s_2.pipeline(j,1);\n    \n    // s_1.tile(k,j,i,2,2,16,i0, j0, k0, i1, j1,k1);\n    // s_1.unroll(k1,-1);\n    // s_1.unroll(j1,-1);\n    // s_1.unroll(i1,-1);\n    // s_1.pipeline(k0,1);\n\n    // s.tile(i, j, 4, 4, i0, j0, i1, j1);\n    // A.partition({16,2},\"cyclic\");\n    // B.partition({2,2},\"cyclic\");\n    // C.partition({16,2},\"cyclic\");\n    // codegen();\n    \n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/gemm/\";\n    fct->auto_DSE(path);\n\n}   \n\n\n"
  },
  {
    "path": "testbench/gesummv.cpp",
    "content": "#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <filesystem>\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <iostream>\n#include <string>\n\n#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n// #include \"mlir/IR/Attributes.h\"\n#define N 4096\n\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    std::string name = \"test_gesummv_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,N);\n    var j(\"j\", 0 ,N);\n    var k(\"k\", 0 ,N);\n    placeholder temp(\"temp\",{N},p_float32);\n    placeholder A(\"A\",{N,N},p_float32);\n    placeholder B(\"B\",{N,N},p_float32);\n    placeholder x(\"x\",{N},p_float32);\n    placeholder y(\"y\",{N},p_float32);\n    constant alpha(1.6);\n    constant beta(3.7);\n       \n    compute s_1(\"s_1\",{i,j},temp(i)+A(i,j)*x(j),temp(i));\n    compute s_2(\"s_2\",{i,j},y(i)+B(i,j)*x(j),y(i));\n    compute s_3(\"s_3\",{i},alpha*temp(i)+beta*y(i),y(i));\n\n    s_2.after(s_1,j);\n    s_3.after(s_1,-1);\n    \n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/gesummv/\";\n    fct->auto_DSE(path);\n}\n"
  },
  {
    "path": "testbench/heat.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define N 4096\n\nint main(){\n    std::string name = \"test_heat_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var k(\"k\", 0 ,4096);\n\n    placeholder A(\"A\",{4096},p_float32);\n    placeholder B(\"B\",{4096},p_float32);\n    constant factor1(0.125);\n    constant factor2(2.0);\n    compute s_1(\"s_1\",{k,i},(B(i)-factor2*B(i+1)+B(i+2))*factor1,A(i+1));\n    compute s_2(\"s_2\",{k,i},A(i+1),B(i+1));\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n\n    s_2.after(s_1,k);\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/heat/\";\n    fct->auto_DSE(path);\n}\n"
  },
  {
    "path": "testbench/jacobi.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define N 4096\n\nint main(){\n    std::string name = \"test_jacobi_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var t(\"t\", 0 ,4096);\n\n    placeholder A(\"A\",{4096},p_float32);\n    placeholder B(\"B\",{4096},p_float32);\n    constant factor(0.33333);\n    compute s_1(\"s_1\",{t,i},(B(i)+B(i+1)+B(i+2))*factor,A(i+1));\n    compute s_2(\"s_2\",{t,i},(A(i)+A(i+1)+A(i+2))*factor,B(i+1));\n    s_2.after(s_1,t);\n    // Choice 1: Hand optimization for users with expertise\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n    // s_1.tile(t,i,1,2,t0,i0,t1,i1);\n    // s_2.tile(t,i,1,2,t0,i0,t1,i1);\n    // s_1.pipeline(i0,1);\n    // s_2.pipeline(i0,1);\n    // s_1.unroll(i1,2);\n    // s_2.unroll(i1,2);\n    // A.partition({4},\"cyclic\");\n    // B.partition({4},\"cyclic\");\n    // codegen();\n    // Choice 2: Automatic DSE for users without expertise\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/jacobi/\";\n    fct->auto_DSE(path);\n\n}\n"
  },
  {
    "path": "testbench/jacobi2d.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define N 4096\n\nint main(){\n    std::string name = \"test_jacobi2d_\"+std::to_string(N);\n    init(name);\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var j(\"j\", 0 ,4094);\n    // var j(\"j\", 1 ,4095);\n    var k(\"k\", 0 ,4096);\n\n    placeholder A(\"A\",{4096,4096},p_float32);\n    placeholder B(\"B\",{4096,4096},p_float32);\n    constant factor(0.2);\n    \n    compute s_1(\"s_1\",{k,i,j},(A(i+1,j+1)+A(i+1,j)+A(i+1,j+2)+A(i+2,j+1)+A(i,j+1))*factor,B(i+1,j+1));\n    compute s_2(\"s_2\",{k,i,j},(B(i+1,j+1)+B(i+1,j)+B(i+1,j+2)+B(i+2,j+1)+B(i,j+1))*factor,A(i+1,j+1));\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n\n    s_2.after(s_1,k);\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/jacobi2d/\";\n    fct->auto_DSE(path);\n}\n"
  },
  {
    "path": "testbench/resnet18.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n// #define K 4 // Size of convolution filter ( FOut xFIn x K x K)\n// #define N 32 // DATA_SET\n\n// polyfp::expr pmax(polyfp::expr left, polyfp::expr right){\n//   return expr(polyfp::o_max, left, right);\n// }\nint main(){\n    init(\"resnet18\");\n    auto *fct = global::get_implicit_function();\n    var o(\"o\", 0 ,64);\n    var y(\"y\", 0 ,32); \n    var x(\"x\", 0 ,32); \n    var i(\"i\", 0 ,3);\n    var p(\"p\", 0 ,3);\n    var q(\"q\", 0 ,3);\n\n    // Block 1.1\n    placeholder filter(\"filter\",{64,3,3,3},p_float32);\n    placeholder fo1(\"fo1\",{64,32,32},p_float32);//{0,64,32,32}\n    placeholder input(\"input\",{3,32,32},p_float32);\n    constant scalar(0,p_float32);\n    // omit initialisation of input and filter\n    compute s_1(\"s_1\",{o,y,x},scalar,fo1(o,y,x));\n    compute s_2(\"s_2\",{o,y,x,i,p,q},fo1(o,y,x)+input(i,y+p,x+q)*filter(o,i,p,q),fo1(o,y,x));\n    // ReLU = max()\n    compute s_3(\"s_3\",{o,y,x},p_max(fo1(o,y,x),scalar),fo1(o,y,x));\n    s_2.after(s_1,-1);\n    s_3.after(s_2,-1);\n\n    var i2(\"i2\", 0 ,64);\n    placeholder fo2(\"fo2\",{64,32,32},p_float32);//{0,64,32,32}\n    placeholder filter2(\"filter2\",{64,64,3,3},p_float32);\n    compute s_4(\"s_4\",{o,y,x},scalar,fo2(o,y,x));\n    compute s_5(\"s_5\",{o,y,x,i2,p,q},fo2(o,y,x)+fo1(i2,y+p,x+q)*filter2(o,i2,p,q),fo2(o,y,x));\n    // ReLU = max()\n    compute s_6(\"s_6\",{o,y,x},p_max(fo2(o,y,x),scalar),fo2(o,y,x));\n    s_4.after(s_3,x);\n    s_5.after(s_4,-1);\n    s_6.after(s_5,-1);\n\n    placeholder fo3(\"fo3\",{64,32,32},p_float32);//{0,64,32,32}\n    compute s_7(\"s_7\",{o,y,x},scalar,fo3(o,y,x));\n    compute s_8(\"s_8\",{o,y,x,i2,p,q},fo3(o,y,x)+fo2(i2,y+p,x+q)*filter2(o,i2,p,q),fo3(o,y,x));\n    // Residual\n    placeholder fo4(\"fo4\",{64,32,32},p_float32);\n    compute s_9(\"s_9\",{o,y,x},fo3(o,y,x)+fo1(o,y,x),fo4(o,y,x));\n    // ReLU = max()\n    compute s_10(\"s_10\",{o,y,x},p_max(fo4(o,y,x),scalar),fo4(o,y,x));\n    s_7.after(s_6,x);\n    s_8.after(s_7,-1);\n    s_9.after(s_8,-1);\n    s_10.after(s_9,-1);\n\n    // Block 1.2\n    placeholder fo5(\"fo5\",{64,32,32},p_float32);//{0,64,32,32}\n    compute s_11(\"s_11\",{o,y,x},scalar,fo5(o,y,x));\n    compute s_12(\"s_12\",{o,y,x,i2,p,q},fo5(o,y,x)+fo4(i2,y+p,x+q)*filter2(o,i2,p,q),fo5(o,y,x));\n    // ReLU = max()\n    compute s_13(\"s_13\",{o,y,x},p_max(fo5(o,y,x),scalar),fo5(o,y,x));   \n    s_11.after(s_10,x);\n    s_12.after(s_11,-1);\n    s_13.after(s_12,-1);\n\n    placeholder fo6(\"fo6\",{64,32,32},p_float32);\n    compute s_14(\"s_14\",{o,y,x},scalar,fo6(o,y,x));\n    compute s_15(\"s_15\",{o,y,x,i2,p,q},fo6(o,y,x)+fo5(i2,y+p,x+q)*filter2(o,i2,p,q),fo6(o,y,x));\n    // Residual\n    placeholder fo7(\"fo7\",{64,32,32},p_float32);\n    compute s_16(\"s_16\",{o,y,x},fo6(o,y,x)+fo4(o,y,x),fo7(o,y,x));\n    // ReLU = max()\n    compute s_17(\"s_17\",{o,y,x},p_max(fo7(o,y,x),scalar),fo7(o,y,x));\n    s_14.after(s_13,x);\n    s_15.after(s_14,-1);\n    s_16.after(s_15,-1);\n    s_17.after(s_16,-1);\n\n\n    // Block 2.1\n    var o2(\"o2\", 0 ,128);\n    var y2(\"y2\", 0 ,16); \n    var x2(\"x2\", 0 ,16); \n    placeholder fo8(\"fo8\",{128,16,16},p_float32);//{0,64,32,32}\n    placeholder filter3(\"filter3\",{128,64,3,3},p_float32);\n    compute s_18(\"s_18\",{o2,y2,x2},scalar,fo8(o2,y2,x2));\n    compute s_19(\"s_19\",{o2,y2,x2,i2,p,q},fo8(o2,y2,x2)+fo7(i2,y2*2+p,x2*2+q)*filter3(o2,i2,p,q),fo8(o2,y2,x2));\n    // ReLU = max()\n    compute s_20(\"s_20\",{o2,y2,x2},p_max(fo8(o2,y2,x2),scalar),fo8(o2,y2,x2));\n    s_18.after(s_17,-1);\n    s_19.after(s_18,-1);\n    s_20.after(s_19,-1);\n\n    var i3(\"i3\", 0 ,128);\n    placeholder fo9(\"fo9\",{128,16,16},p_float32);//{0,64,32,32}\n    placeholder filter4(\"filter4\",{128,128,3,3},p_float32);\n    compute s_21(\"s_21\",{o2,y2,x2},scalar,fo9(o2,y2,x2));\n    compute s_22(\"s_22\",{o2,y2,x2,i3,p,q},fo9(o2,y2,x2)+fo8(i3,y2+p,x2+q)*filter4(o2,i3,p,q),fo9(o2,y2,x2));\n    // transform\n    placeholder fo10(\"fo10\",{128,16,16},p_float32);//{0,64,32,32}\n    placeholder temp1(\"temp1\",{128,64},p_float32);\n    compute s_23(\"s_23\",{o2,y2,x2,i2},fo7(i2,y2*2,x2*2)*temp1(o2,i2)+fo10(o2,y2,x2),fo10(o2,y2,x2));\n    // Residual\n    placeholder fo11(\"fo11\",{128,16,16},p_float32);//{0,64,32,32}\n    compute s_24(\"s_24\",{o2,y2,x2},fo10(o2,y2,x2)+fo9(o2,y2,x2),fo11(o2,y2,x2));\n    // ReLU = max()\n    compute s_25(\"s_25\",{o2,y2,x2},p_max(fo11(o2,y2,x2),scalar),fo11(o2,y2,x2));\n    s_21.after(s_20,x2);\n    s_22.after(s_21,-1);\n    s_23.after(s_22,-1);\n    s_24.after(s_23,-1);\n    s_25.after(s_24,-1);\n\n    // Block 2.2\n    placeholder fo12(\"fo12\",{128,16,16},p_float32);//{0,64,32,32}\n    compute s_26(\"s_26\",{o2,y2,x2},scalar,fo12(o2,y2,x2));\n    compute s_27(\"s_27\",{o2,y2,x2,i3,p,q},fo12(o2,y2,x2)+fo11(i3,y2+p,x2+q)*filter4(o2,i3,p,q),fo12(o2,y2,x2));\n    // ReLU = max()\n    compute s_28(\"s_28\",{o2,y2,x2},p_max(fo12(o2,y2,x2),scalar),fo12(o2,y2,x2));\n    placeholder fo13(\"fo13\",{128,16,16},p_float32);//{0,64,32,32}\n    compute s_29(\"s_29\",{o2,y2,x2},scalar,fo13(o2,y2,x2));\n    compute s_30(\"s_30\",{o2,y2,x2,i3,p,q},fo13(o2,y2,x2)+fo12(i3,y2+p,x2+q)*filter4(o2,i3,p,q),fo13(o2,y2,x2));\n    // Residual\n    placeholder fo14(\"fo14\",{128,16,16},p_float32);//{0,64,32,32}\n    compute s_31(\"s_31\",{o2,y2,x2},fo13(o2,y2,x2)+fo11(o2,y2,x2),fo14(o2,y2,x2));\n    // ReLU = max()\n    compute s_32(\"s_32\",{o2,y2,x2},p_max(fo14(o2,y2,x2),scalar),fo14(o2,y2,x2));\n    s_26.after(s_25,x2);\n    s_27.after(s_26,-1);\n    s_28.after(s_27,-1);\n    s_29.after(s_28,x2);\n    s_30.after(s_29,-1);\n    s_31.after(s_30,-1);\n    s_32.after(s_31,-1);\n\n    // Block 3.1\n    var o3(\"o3\", 0 ,256);\n    var y3(\"y3\", 0 ,8); \n    var x3(\"x3\", 0 ,8); \n    placeholder fo15(\"fo15\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder filter5(\"filter5\",{256,128,3,3},p_float32);\n    compute s_33(\"s_33\",{o3,y3,x3},scalar,fo15(o3,y3,x3));\n    compute s_34(\"s_34\",{o3,y3,x3,i3,p,q},fo15(o3,y3,x3)+fo14(i3,y3*2+p,x3*2+q)*filter5(o3,i3,p,q),fo15(o3,y3,x3));\n    // ReLU = max()\n    compute s_35(\"s_35\",{o3,y3,x3},p_max(fo15(o3,y3,x3),scalar),fo15(o3,y3,x3));\n    s_33.after(s_32,-1);\n    s_34.after(s_33,-1);\n    s_35.after(s_34,-1);\n\n    var i4(\"i4\", 0 ,256);\n    placeholder fo16(\"fo16\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder filter6(\"filter6\",{256,256,3,3},p_float32);\n    compute s_36(\"s_36\",{o3,y3,x3},scalar,fo16(o3,y3,x3));\n    compute s_37(\"s_37\",{o3,y3,x3,i4,p,q},fo16(o3,y3,x3)+fo15(i4,y3+p,x3+q)*filter6(o3,i4,p,q),fo16(o3,y3,x3));\n    // transform\n    placeholder fo17(\"fo17\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder temp2(\"temp2\",{256,128},p_float32);\n    compute s_38(\"s_38\",{o3,y3,x3,i3},fo14(i3,y3*2,x3*2)*temp2(o3,i3)+fo17(o3,y3,x3),fo17(o3,y3,x3));\n    // Residual\n    placeholder fo18(\"fo18\",{256,8,8},p_float32);//{0,64,32,32}\n    compute s_39(\"s_39\",{o3,y3,x3},fo17(o3,y3,x3)+fo16(o3,y3,x3),fo18(o3,y3,x3));\n    // ReLU = max()\n    compute s_40(\"s_40\",{o3,y3,x3},p_max(fo18(o3,y3,x3),scalar),fo18(o3,y3,x3));\n    s_36.after(s_35,x3);\n    s_37.after(s_36,-1);\n    s_38.after(s_37,-1);\n    s_39.after(s_38,-1);\n    s_40.after(s_39,-1);\n\n    // Block 3.2\n    placeholder fo19(\"fo19\",{256,8,8},p_float32);//{0,64,32,32}\n    compute s_41(\"s_41\",{o3,y3,x3},scalar,fo19(o3,y3,x3));\n    compute s_42(\"s_42\",{o3,y3,x3,i4,p,q},fo19(o3,y3,x3)+fo18(i4,y3+p,x3+q)*filter6(o3,i4,p,q),fo19(o3,y3,x3));\n    // ReLU = max()\n    compute s_43(\"s_43\",{o3,y3,x3},p_max(fo19(o3,y3,x3),scalar),fo19(o3,y3,x3));\n    placeholder fo20(\"fo20\",{256,8,8},p_float32);//{0,64,32,32}\n    compute s_44(\"s_44\",{o3,y3,x3},scalar,fo20(o3,y3,x3));\n    compute s_45(\"s_45\",{o3,y3,x3,i4,p,q},fo20(o3,y3,x3)+fo19(i4,y3+p,x3+q)*filter6(o3,i4,p,q),fo20(o3,y3,x3));\n    // Residual\n    placeholder fo21(\"fo21\",{256,8,8},p_float32);//{0,64,32,32}\n    compute s_46(\"s_46\",{o3,y3,x3},fo20(o3,y3,x3)+fo18(o3,y3,x3),fo21(o3,y3,x3));\n    // ReLU = max()\n    compute s_47(\"s_47\",{o3,y3,x3},p_max(fo21(o3,y3,x3),scalar),fo21(o3,y3,x3));\n    s_41.after(s_40,x3);\n    s_42.after(s_41,-1);\n    s_43.after(s_42,-1);\n    s_44.after(s_43,-1);\n    s_45.after(s_44,-1);\n    s_46.after(s_45,-1);\n    s_47.after(s_46,-1);\n\n    // Block 4.1\n    var o4(\"o4\", 0 ,512);\n    var y4(\"y4\", 0 ,4); \n    var x4(\"x4\", 0 ,4); \n    placeholder fo22(\"fo22\",{512,4,4},p_float32);//{0,64,32,32}\n    placeholder filter7(\"filter7\",{512,256,3,3},p_float32);\n    compute s_48(\"s_48\",{o4,y4,x4},scalar,fo22(o4,y4,x4));\n    compute s_49(\"s_49\",{o4,y4,x4,i4,p,q},fo22(o4,y4,x4)+fo21(i4,y4*2+p,x4*2+q)*filter7(o4,i4,p,q),fo22(o4,y4,x4));\n    // ReLU = max()\n    compute s_50(\"s_50\",{o4,y4,x4},p_max(fo22(o4,y4,x4),scalar),fo22(o4,y4,x4));\n    s_48.after(s_47,-1);\n    s_49.after(s_48,-1);\n    s_50.after(s_49,-1);\n\n    var i5(\"i5\", 0 ,512);\n    placeholder fo23(\"fo23\",{512,4,4},p_float32);//{0,64,32,32}\n    placeholder filter8(\"filter8\",{512,512,3,3},p_float32);\n    compute s_51(\"s_51\",{o4,y4,x4},scalar,fo23(o4,y4,x4));\n    compute s_52(\"s_52\",{o4,y4,x4,i5,p,q},fo23(o4,y4,x4)+fo22(i5,y4+p,x4+q)*filter8(o4,i5,p,q),fo23(o4,y4,x4));\n    // transform\n    placeholder fo24(\"fo24\",{512,4,4},p_float32);//{0,64,32,32}\n    placeholder temp3(\"temp3\",{512,256},p_float32);\n    compute s_53(\"s_53\",{o4,y4,x4,i4},fo21(i4,y4*2,x4*2)*temp3(o4,i4)+fo24(o4,y4,x4),fo24(o4,y4,x4));\n    // Residual\n    placeholder fo25(\"fo25\",{512,4,4},p_float32);//{0,64,32,32}\n    compute s_54(\"s_54\",{o4,y4,x4},fo24(o4,y4,x4)+fo23(o4,y4,x4),fo25(o4,y4,x4));\n    // ReLU = max()\n    compute s_55(\"s_55\",{o4,y4,x4},p_max(fo25(o4,y4,x4),scalar),fo25(o4,y4,x4));\n    s_51.after(s_50,x4);\n    s_52.after(s_51,-1);\n    s_53.after(s_52,-1);\n    s_54.after(s_53,-1);\n    s_55.after(s_54,-1);\n\n    // Block 4.2\n    placeholder fo26(\"fo26\",{512,4,4},p_float32);//{0,64,32,32}\n    compute s_56(\"s_56\",{o4,y4,x4},scalar,fo26(o4,y4,x4));\n    compute s_57(\"s_57\",{o4,y4,x4,i5,p,q},fo26(o4,y4,x4)+fo25(i5,y4+p,x4+q)*filter8(o4,i5,p,q),fo26(o4,y4,x4));\n    // ReLU = max()\n    compute s_58(\"s_58\",{o4,y4,x4},p_max(fo26(o4,y4,x4),scalar),fo26(o4,y4,x4));\n    placeholder fo27(\"fo27\",{512,4,4},p_float32);//{0,64,32,32}\n    compute s_59(\"s_59\",{o4,y4,x4},scalar,fo27(o4,y4,x4));\n    compute s_60(\"s_60\",{o4,y4,x4,i5,p,q},fo27(o4,y4,x4)+fo26(i5,y4+p,x4+q)*filter8(o4,i5,p,q),fo27(o4,y4,x4));\n    // Residual\n    placeholder fo28(\"fo28\",{512,4,4},p_float32);//{0,64,32,32}\n    compute s_61(\"s_61\",{o4,y4,x4},fo27(o4,y4,x4)+fo25(o4,y4,x4),fo28(o4,y4,x4));\n    // ReLU = max()\n    compute s_62(\"s_62\",{o4,y4,x4},p_max(fo28(o4,y4,x4),scalar),fo28(o4,y4,x4));\n    s_56.after(s_55,x4);\n    s_57.after(s_56,-1);\n    s_58.after(s_57,-1);\n    s_59.after(s_58,x4);\n    s_60.after(s_59,-1);\n    s_61.after(s_60,-1);\n    s_62.after(s_61,-1);\n\n\n    fct->auto_DSE_loop_transformation();\n    int count=0;\n    for(auto &comp: fct->leader_computations){\n        auto iterators = comp->get_iteration_variables();\n        int size = iterators.size();\n        if(size>=6){\n          comp->apply_opt_strategy({4,1,1});\n        }\n        if(size==4){\n          if(count!=0){\n              comp->apply_opt_strategy({2,1,1});\n              count+=1;\n          }\n          count+=1;\n          \n        }\n    }\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/resnet18/\";\n    fct->dump_schedule(path);\n}\n\n"
  },
  {
    "path": "testbench/seidel.cpp",
    "content": "#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.h>\n#include <isl/schedule.h>\n#include <isl/schedule_node.h>\n#include <isl/space.h>\n#include <isl/constraint.h>\n#include <filesystem>\n#include <map>\n#include <string.h>\n#include <stdint.h>\n#include <unordered_map>\n#include <unordered_set>\n#include <sstream>\n#include <iostream>\n#include <string>\n#include <filesystem>\n#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n// #include \"mlir/IR/Attributes.h\"\nusing namespace std;\nusing namespace polyfp;\nint main(){\n    init(\"test_seidel_4096\");\n    auto *fct = global::get_implicit_function();\n    var i(\"i\", 0 ,4094);\n    var j(\"j\", 0 ,4094);\n    var k(\"k\", 0 ,4096);\n\n    placeholder A(\"A\",{4096,4096},p_float32);\n    placeholder B(\"B\",{4096,4096},p_float32);\n    constant factor(9);\n    compute s_1(\"s_1\",{k,i,j},(A(i,j+1)+A(i,j)+A(i,j+2)+A(i+1,j)+A(i+1,j+1)+A(i+1,j+2)+A(i+2,j)+A(i+2,j+1)+A(i+2,j+2))/factor,A(i+1,j+1));\n\n    var i0(\"i0\"), j0(\"j0\"),k0(\"k0\"), i1(\"i1\"), j1(\"j1\"),k1(\"k1\");\n\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/seidel/\";\n    fct->auto_DSE(path);\n}\n\n}\n"
  },
  {
    "path": "testbench/vgg16.cpp",
    "content": "#include \"expr.h\"\n#include \"compute.h\"\n#include \"function.h\"\n#include \"core.h\"\n#include <filesystem>\nusing namespace std;\nusing namespace polyfp;\n#define K 4 // Size of convolution filter ( FOut xFIn x K x K)\n#define N 32 // DATA_SET\n\nint main(){\n    init(\"test_vgg16\");\n    auto *fct = global::get_implicit_function();\n    var o(\"o\", 0 ,64);\n    var y(\"y\", 0 ,32); \n    var x(\"x\", 0 ,32); \n    var i(\"i\", 0 ,3);\n    var p(\"p\", 0 ,3);\n    var q(\"q\", 0 ,3);\n\n    placeholder filter(\"filter\",{64,3,3,3},p_float32);\n    placeholder fo1(\"fo1\",{64,32,32},p_float32);//{0,64,32,32}\n    placeholder input(\"input\",{3,34,34},p_float32);\n    // placeholder temp(\"temp\",{3,32,32},p_float32);\n    // placeholder relu(\"relu\",{64,32,32},p_float32);\n    // placeholder input1(\"input1\",{64,34,34},p_float32);\n    constant scalar(0,p_float32);\n    // omit initialisation of input and filter\n    compute s_1(\"s_1\",{o,y,x},scalar,fo1(o,y,x));\n    compute s_2(\"s_2\",{o,y,x,i,p,q},fo1(o,y,x)+input(i,y+p,x+q)*filter(o,i,p,q),fo1(o,y,x));\n    // ReLU = max()\n    compute s_3(\"s_3\",{o,y,x},p_max(fo1(o,y,x),scalar),fo1(o,y,x));\n    s_2.after(s_1,-1);\n    s_3.after(s_2,-1);\n\n    var i2(\"i2\", 0 ,64);\n    placeholder fo2(\"fo2\",{64,32,32},p_float32);//{0,64,32,32}\n    placeholder filter2(\"filter2\",{64,64,3,3},p_float32);\n    compute s_4(\"s_4\",{o,y,x},scalar,fo2(o,y,x));\n    compute s_5(\"s_5\",{o,y,x,i2,p,q},fo2(o,y,x)+fo1(i2,y+p,x+q)*filter2(o,i2,p,q),fo2(o,y,x));\n    // ReLU = max()\n    compute s_6(\"s_6\",{o,y,x},p_max(fo2(o,y,x),scalar),fo2(o,y,x));\n    s_4.after(s_3,x);\n    s_5.after(s_4,-1);\n    s_6.after(s_5,-1);\n\n    var y2(\"y2\", 0 ,16); \n    var x2(\"x2\", 0 ,16); \n    placeholder fo3(\"fo3\",{64,16,16},p_float32);//{0,64,32,32}\n    compute s_7(\"s_7\",{o,y2,x2},scalar,fo3(o,y2,x2));\n    // o3(o,y2,x2) = Max(o2(o,y2*2+p,x2*2+q), o3(o,y2,x2));\n    compute s_8(\"s_8\",{o,y2,x2,p,q},fo2(o,y2*2+p,x2*2+q),fo3(o,y2,x2));\n    s_7.after(s_6,-1);\n    s_8.after(s_7,-1);\n\n    // Block 2\n    var o2(\"o2\", 0 ,128);\n    placeholder fo4(\"fo4\",{128,16,16},p_float32);//{0,64,32,32}\n    placeholder filter3(\"filter3\",{128,64,3,3},p_float32);\n    compute s_9(\"s_9\",{o2,y2,x2},scalar,fo4(o2,y2,x2));\n    compute s_10(\"s_10\",{o2,y2,x2,i2,p,q},fo4(o2,y2,x2)+fo3(i2,y2+p,x2+q)*filter3(o2,i2,p,q),fo4(o2,y2,x2));\n    //ReLu = max()\n    compute s_11(\"s_11\",{o2,y2,x2},p_max(fo4(o2,y2,x2),scalar),fo4(o2,y2,x2));\n    s_9.after(s_8,-1);\n    s_10.after(s_9,-1);\n    s_11.after(s_10,-1);\n\n    var i3(\"i3\", 0 ,128);\n    placeholder fo5(\"fo5\",{128,16,16},p_float32);//{0,64,32,32}\n    placeholder filter4(\"filter4\",{128,128,3,3},p_float32);\n    compute s_12(\"s_12\",{o2,y2,x2},scalar,fo5(o2,y2,x2));\n    compute s_13(\"s_13\",{o2,y2,x2,i3,p,q},fo5(o2,y2,x2)+fo4(i3,y2+p,x2+q)*filter4(o2,i3,p,q),fo5(o2,y2,x2));\n    //ReLu = max()\n    compute s_14(\"s_14\",{o2,y2,x2},p_max(fo5(o2,y2,x2),scalar),fo5(o2,y2,x2));\n    s_12.after(s_11,x2);\n    s_13.after(s_12,-1);\n    s_14.after(s_13,-1);\n\n    var y3(\"y3\", 0 ,8); \n    var x3(\"x3\", 0 ,8); \n    placeholder fo6(\"fo6\",{128,8,8},p_float32);//{0,64,32,32}\n    compute s_15(\"s_15\",{o2,y3,x3},scalar,fo6(o2,y3,x3));\n    // o3(o,y2,x2) = Max(o2(o,y2*2+p,x2*2+q), o3(o,y2,x2));\n    compute s_16(\"s_16\",{o2,y3,x3,p,q},fo5(o2,y3*2+p,x3*2+q),fo6(o2,y3,x3));\n    s_15.after(s_14,-1);\n    s_16.after(s_15,-1);\n\n    // Block 3\n    var o3(\"o3\", 0 ,256);\n    placeholder fo7(\"fo7\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder filter5(\"filter5\",{256,128,3,3},p_float32);\n    compute s_17(\"s_17\",{o3,y3,x3},scalar,fo7(o3,y3,x3));\n    compute s_18(\"s_18\",{o3,y3,x3,i3,p,q},fo7(o3,y3,x3)+fo6(i3,y3+p,x3+q)*filter5(o3,i3,p,q),fo7(o3,y3,x3));\n    //ReLu = max()\n    compute s_19(\"s_19\",{o3,y3,x3},p_max(fo7(o3,y3,x3),scalar),fo7(o3,y3,x3));\n    s_17.after(s_16,-1);\n    s_18.after(s_17,-1);\n    s_19.after(s_18,-1);\n\n    var i4(\"i4\", 0 ,256);\n    placeholder fo8(\"fo8\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder fo9(\"fo9\",{256,8,8},p_float32);//{0,64,32,32}\n    placeholder filter6(\"filter6\",{256,256,3,3},p_float32);\n    compute s_20(\"s_20\",{o3,y3,x3},scalar,fo8(o3,y3,x3));\n    compute s_21(\"s_21\",{o3,y3,x3,i4,p,q},fo8(o3,y3,x3)+fo7(i4,y3+p,x3+q)*filter6(o3,i4,p,q),fo8(o3,y3,x3));\n    //ReLu = max()\n    compute s_22(\"s_22\",{o3,y3,x3},p_max(fo8(o3,y3,x3),scalar),fo8(o3,y3,x3));\n    compute s_23(\"s_23\",{o3,y3,x3},scalar,fo9(o3,y3,x3));\n    compute s_24(\"s_24\",{o3,y3,x3,i4,p,q},fo9(o3,y3,x3)+fo8(i4,y3+p,x3+q)*filter6(o3,i4,p,q),fo9(o3,y3,x3));\n    //ReLu = max()\n    compute s_25(\"s_25\",{o3,y3,x3},p_max(fo9(o3,y3,x3),scalar),fo9(o3,y3,x3));\n    s_20.after(s_19,x3);\n    s_21.after(s_20,-1);\n    s_22.after(s_21,-1);\n    s_23.after(s_22,-1);\n    s_24.after(s_23,-1);\n    s_25.after(s_24,-1);\n\n    var y4(\"y4\", 0 ,4); \n    var x4(\"x4\", 0 ,4); \n    placeholder fo10(\"fo10\",{256,4,4},p_float32);//{0,64,32,32}\n    compute s_26(\"s_26\",{o3,y4,x4},scalar,fo10(o3,y4,x4));\n    // o3(o,y2,x2) = Max(o2(o,y2*2+p,x2*2+q), o3(o,y2,x2));\n    compute s_27(\"s_27\",{o3,y4,x4,p,q},fo9(o3,y4*2+p,x4*2+q),fo10(o3,y4,x4));\n    s_26.after(s_25,-1);\n    s_27.after(s_26,-1);\n\n    // Block 4\n    var o4(\"o4\", 0 ,512);\n    placeholder fo11(\"fo11\",{512,4,4},p_float32);//{0,64,32,32}\n    placeholder filter7(\"filter7\",{512,256,3,3},p_float32);\n    compute s_28(\"s_28\",{o4,y4,x4},scalar,fo11(o4,y4,x4));\n    compute s_29(\"s_29\",{o4,y4,x4,i4,p,q},fo11(o4,y4,x4)+fo10(i4,y4+p,x4+q)*filter7(o4,i4,p,q),fo11(o4,y4,x4));\n    //ReLu = max()\n    compute s_30(\"s_30\",{o4,y4,x4},p_max(fo11(o4,y4,x4),scalar),fo11(o4,y4,x4));\n    s_28.after(s_27,-1);\n    s_29.after(s_28,-1);\n    s_30.after(s_29,-1);\n\n    var i5(\"i5\", 0 ,512);\n    placeholder fo12(\"fo12\",{512,8,8},p_float32);//{0,64,32,32}\n    placeholder fo13(\"fo13\",{512,8,8},p_float32);//{0,64,32,32}\n    placeholder filter8(\"filter8\",{512,512,3,3},p_float32);\n    compute s_31(\"s_31\",{o4,y4,x4},scalar,fo12(o4,y4,x4));\n    compute s_32(\"s_32\",{o4,y4,x4,i5,p,q},fo12(o4,y4,x4)+fo11(i5,y4+p,x4+q)*filter8(o4,i5,p,q),fo12(o4,y4,x4));\n    //ReLu = max()\n    compute s_33(\"s_33\",{o4,y4,x4},p_max(fo12(o4,y4,x4),scalar),fo12(o4,y4,x4));\n    compute s_34(\"s_34\",{o4,y4,x4},scalar,fo13(o4,y4,x4));\n    compute s_35(\"s_35\",{o4,y4,x4,i5,p,q},fo13(o4,y4,x4)+fo12(i5,y4+p,x4+q)*filter8(o4,i5,p,q),fo13(o4,y4,x4));\n    //ReLu = max()\n    compute s_36(\"s_36\",{o4,y4,x4},p_max(fo13(o4,y4,x4),scalar),fo13(o4,y4,x4));\n    s_31.after(s_30,x4);\n    s_32.after(s_31,-1);\n    s_33.after(s_32,-1);\n    s_34.after(s_33,x4);\n    s_35.after(s_34,-1);\n    s_36.after(s_35,-1);\n\n    var y5(\"y5\", 0 ,2);\n    var x5(\"x5\", 0 ,2); \n    placeholder fo14(\"fo14\",{512,2,2},p_float32);//{0,64,32,32}\n    compute s_37(\"s_37\",{o4,y5,x5},scalar,fo14(o4,y5,x5));\n    // o3(o,y2,x2) = Max(o2(o,y2*2+p,x2*2+q), o3(o,y2,x2));\n    compute s_38(\"s_38\",{o4,y5,x5,p,q},fo13(o4,y5*2+p,x5*2+q),fo14(o4,y5,x5));\n    s_37.after(s_36,-1);\n    s_38.after(s_37,-1);\n\n    // Block 5\n    var o5(\"o5\", 0 ,512);\n    placeholder fo15(\"fo15\",{512,2,2},p_float32);//{0,64,32,32}\n    placeholder filter9(\"filter9\",{512,512,3,3},p_float32);\n    compute s_39(\"s_39\",{o5,y5,x5},scalar,fo15(o5,y5,x5));\n    compute s_40(\"s_40\",{o5,y5,x5,i5,p,q},fo15(o5,y5,x5)+fo14(i5,y5+p,x5+q)*filter9(o5,i5,p,q),fo15(o5,y5,x5));\n    //ReLu = max()\n    compute s_41(\"s_41\",{o5,y5,x5},p_max(fo15(o5,y5,x5),scalar),fo15(o5,y5,x5));\n    s_39.after(s_38,-1);\n    s_40.after(s_39,-1);\n    s_41.after(s_40,-1);\n\n    placeholder fo16(\"fo16\",{512,2,2},p_float32);//{0,64,32,32}\n    placeholder fo17(\"fo17\",{512,2,2},p_float32);//{0,64,32,32}\n    compute s_42(\"s_42\",{o5,y5,x5},scalar,fo16(o5,y5,x5));\n    compute s_43(\"s_43\",{o5,y5,x5,i5,p,q},fo16(o5,y5,x5)+fo15(i5,y5+p,x5+q)*filter9(o5,i5,p,q),fo16(o5,y5,x5));\n    //ReLu = max()\n    compute s_44(\"s_44\",{o5,y5,x5},p_max(fo16(o5,y5,x5),scalar),fo16(o5,y5,x5));\n    compute s_45(\"s_45\",{o5,y5,x5},scalar,fo17(o5,y5,x5));\n    compute s_46(\"s_46\",{o5,y5,x5,i5,p,q},fo17(o5,y5,x5)+fo16(i5,y5+p,x5+q)*filter9(o5,i5,p,q),fo17(o5,y5,x5));\n    //ReLu = max()\n    compute s_47(\"s_47\",{o5,y5,x5},p_max(fo17(o5,y5,x5),scalar),fo17(o5,y5,x5));\n    s_42.after(s_41,x5);\n    s_43.after(s_42,-1);\n    s_44.after(s_43,-1);\n    s_45.after(s_44,-1);\n    s_46.after(s_45,-1);\n    s_47.after(s_46,-1);\n\n    // var y5(\"y5\", 0 ,2);\n    // var x5(\"x5\", 0 ,2); \n    placeholder fo18(\"fo18\",{512},p_float32);//{0,64,32,32}\n    compute s_48(\"s_48\",{o5},scalar,fo18(o5));\n    // o3(o,y2,x2) = Max(o2(o,y2*2+p,x2*2+q), o3(o,y2,x2));\n    compute s_49(\"s_49\",{o5,p,q},fo17(o5,2+p,2+q),fo18(o5));\n    s_48.after(s_47,-1);\n    s_49.after(s_48,-1);\n\n    \n\n\n\n    fct->auto_DSE_loop_transformation();\n    for(auto &comp: fct->leader_computations){\n        auto iterators = comp->get_iteration_variables();\n        int size = iterators.size();\n        if(size>=6){\n          comp->apply_opt_strategy({8,1,1});\n        }\n    }\n    std::string pwd = std::filesystem::current_path().parent_path();\n    std::string path = pwd+\"/samples/vgg16/\";\n    fct->dump_schedule(path);\n}\n\n"
  },
  {
    "path": "vitis-reports.sh",
    "content": "#!/bin/bash\nstart_time=$(date +\"%s\")\necho \"\"\necho \">>> Step 4. Synthesising the optimized HLS C code...\"\necho \"\"\nexport LD_PRELOAD=/lib/x86_64-linux-gnu/libudev.so.1\n\nexecute_tcl() {\n    example=$1\n    size=$2\n    script_name=\"script_${size}.tcl\"\n    cd \"samples/${example}\"\n    vitis_hls -f \"$script_name\"\n    cd -  \n}\n\nexecute_tcl2() {\n    example=$1\n    size=$2\n    script_name=\"script_power.tcl\"\n    cd \"samples/${example}\"\n    vitis_hls -f \"$script_name\"\n    cd -  \n}\n\nmax_parallel=20\n\n\nexamples=(\"edgeDetect\" \"gaussian\" \"blur\" \"jacobi\" \"jacobi2d\" \"heat\" \"seidel\")\nsizes=(4096)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        execute_tcl \"$example\" \"$size\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\n\nexamples=(\"vgg16\"  \"resnet\")\nsizes=(512)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        execute_tcl \"$example\" \"$size\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\n\nsizes=(32 64 128 256 512 1024 2048 4096 8192)\n\nexamples=(\"2mm\" \"3mm\" \"gemm\" \"bicg\" \"gesummv\")\n\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        execute_tcl \"$example\" \"$size\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\nexamples=(\"2mm\" \"3mm\" \"gemm\" \"gesummv\")\nsizes=(4096)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        execute_tcl2 \"$example\" \"$size\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\nexamples=(\"bicg\")\nsizes=(4096)\nfor example in \"${examples[@]}\"\ndo  \n    for size in \"${sizes[@]}\"\n    do\n        execute_tcl2 \"$example\" \"$size\" &\n        if (( $(jobs | wc -l) >= $max_parallel )); then\n            wait -n\n        fi\n    done\ndone\nwait\n\ncd /usr/src/workspace\n\n\n\nend_time=$(date +\"%s\")\nexecution_time=$(($end_time - $start_time))\necho \"\"\necho \">>> Step 4 has been finished!\"\necho \">>> Step 4 Total Execution Time: $execution_time seconds\"\necho \"\"\n\n"
  }
]