diff --git a/.clang-format b/.clang-format index 9b3aa8b72..392e20189 100644 --- a/.clang-format +++ b/.clang-format @@ -1 +1,2 @@ BasedOnStyle: LLVM +AlwaysBreakTemplateDeclarations: Yes \ No newline at end of file diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 000000000..aaf40c744 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,92 @@ +Checks: > + -*, + clang-diagnostic-*, + llvm-*, + misc-*, + -misc-const-correctness, + -misc-unused-parameters, + -misc-non-private-member-variables-in-classes, + -misc-no-recursion, + -misc-use-anonymous-namespace, + readability-identifier-naming, + -misc-const-correctness, + bugprone-argument-comment, + bugprone-assert-side-effect, + bugprone-branch-clone, + bugprone-copy-constructor-init, + bugprone-dangling-handle, + bugprone-dynamic-static-initializers, + bugprone-macro-parentheses, + bugprone-macro-repeated-side-effects, + bugprone-misplaced-widening-cast, + bugprone-move-forwarding-reference, + bugprone-multiple-statement-macro, + bugprone-suspicious-semicolon, + bugprone-swapped-arguments, + bugprone-terminating-continue, + bugprone-unused-raii, + bugprone-unused-return-value, + misc-redundant-expression, + misc-static-assert, + misc-unused-using-decls, + modernize-use-bool-literals, + modernize-loop-convert, + modernize-make-unique, + modernize-raw-string-literal, + modernize-use-equals-default, + modernize-use-default-member-init, + modernize-use-emplace, + modernize-use-nullptr, + modernize-use-override, + modernize-use-using, + performance-for-range-copy, + performance-implicit-conversion-in-loop, + performance-inefficient-algorithm, + performance-inefficient-vector-operation, + performance-move-const-arg, + performance-no-automatic-move, + performance-trivially-destructible, + performance-unnecessary-copy-initialization, + performance-unnecessary-value-param, + readability-avoid-const-params-in-decls, + readability-const-return-type, + readability-container-size-empty, + readability-inconsistent-declaration-parameter-name, + readability-misleading-indentation, + readability-redundant-control-flow, + readability-redundant-smartptr-get, + readability-simplify-boolean-expr, + readability-simplify-subscript-expr, + readability-use-anyofallof + +CheckOptions: + # LLVM + - key: readability-identifier-naming.ClassCase + value: CamelCase + - key: readability-identifier-naming.EnumCase + value: CamelCase + - key: readability-identifier-naming.FunctionCase + value: camelBack + - key: readability-identifier-naming.FunctionIgnoredRegexp + value: "LLVMFuzzerTestOneInput" + - key: readability-identifier-naming.MemberCase + value: CamelCase + - key: readability-identifier-naming.ParameterCase + value: CamelCase + - key: readability-identifier-naming.UnionCase + value: CamelCase + - key: readability-identifier-naming.VariableCase + value: CamelCase + - key: readability-identifier-naming.IgnoreMainLikeFunctions + value: 1 + - key: readability-redundant-member-init.IgnoreBaseInCopyConstructors + value: 1 + - key: modernize-use-default-member-init.UseAssignment + value: 1 + # MLIR + - key: readability-identifier-naming.MemberCase + value: camelBack + - key: readability-identifier-naming.ParameterCase + value: camelBack + - key: readability-identifier-naming.VariableCase + value: camelBack diff --git a/.github/workflows/buildAndTest.yml b/.github/workflows/buildAndTest.yml index 64bdd3d7d..efc92e2a7 100644 --- a/.github/workflows/buildAndTest.yml +++ b/.github/workflows/buildAndTest.yml @@ -193,121 +193,3 @@ jobs: rm -rf build_release env: LD_LIBRARY_PATH: ${{ github.workspace }}/aienginev2/install/lib - - lint-repo: - name: Check code format - runs-on: ubuntu-20.04 - steps: - - # Clone the repo and its submodules. Do shallow clone to save clone - # time. - - name: Get repo - uses: actions/checkout@v3 - with: - fetch-depth: 2 - submodules: "true" - - # -------- - # Lint the code. - # ------- - - # Choose the git commit to diff against for the purposes of linting. - # Since this workflow is triggered on both pushes and pull requests, we - # have to determine if the pull request target branch is set (which it - # will only be on the PR triggered flow). If it's not, then compare - # against the last commit. - - name: choose-commit - if: ${{ always() }} - env: - # Base ref is the target branch, in text form (not hash) - PR_BASE: ${{ github.base_ref }} - run: | - # Run clang-format - if [[ -z "$PR_BASE" ]]; then - DIFF_COMMIT_NAME="HEAD^" - else - DIFF_COMMIT_NAME="$PR_BASE" - fi - echo "DIFF_COMMIT_NAME=$DIFF_COMMIT_NAME" >> $GITHUB_ENV - - # Since we did a shallow fetch for this repo, we must fetch the commit - # upon which we be diff'ing. The last step set the ref name in the - # $DIFF_COMMIT_NAME environment variable. When running the fetch, resolve - # it to the commit hash and pass that hash along to subsequent steps. - - name: git fetch base commit - continue-on-error: true - run: | - if [[ ! "$DIFF_COMMIT_NAME" == *"HEAD"* ]]; then - git fetch --recurse-submodules=no origin $DIFF_COMMIT_NAME - DIFF_COMMIT_SHA=$( git rev-parse origin/$DIFF_COMMIT_NAME ) - else - DIFF_COMMIT_SHA=$( git rev-parse $DIFF_COMMIT_NAME ) - fi - echo "DIFF_COMMIT=$DIFF_COMMIT_SHA" >> $GITHUB_ENV - - # Run 'git clang-format', comparing against the target commit hash. If - # clang-format fixed anything, fail and output a patch. - - name: clang-format - if: ${{ always() }} - run: | - # Run clang-format - git clang-format-12 $DIFF_COMMIT - git diff --ignore-submodules > clang-format.patch - if [ -s clang-format.patch ]; then - echo "Clang-format found formatting problems in the following " \ - "files. See diff in the clang-format.patch artifact." - git diff --ignore-submodules --name-only - git checkout . - exit 1 - fi - echo "Clang-format found no formatting problems" - exit 0 - - # Run clang-tidy against only the changes. The 'clang-tidy-diff' script - # does this if supplied with the diff. - - name: clang-tidy - if: ${{ always() }} - run: | - git diff -U0 $DIFF_COMMIT | \ - clang-tidy-diff-12.py -path build_assert -p1 -fix - git diff --ignore-submodules > clang-tidy.patch - if [ -s clang-tidy.patch ]; then - echo "Clang-tidy problems in the following files. " \ - "See diff in the clang-tidy.patch artifact." - git diff --ignore-submodules --name-only - git checkout . - exit 1 - fi - echo "Clang-tidy found no problems" - exit 0 - - # Upload the format and tidy patches to an artifact (zip'd) associated - # with the workflow run. Only run this on a failure. - - name: Upload format and tidy patches - uses: actions/upload-artifact@v2 - continue-on-error: true - if: ${{ failure() }} - with: - name: clang-format-tidy-patches - path: clang-*.patch - - # Unfortunately, artifact uploads are always zips so display the diff as - # well to provide feedback at a glance. - - name: clang format and tidy patches display - if: ${{ failure() }} - continue-on-error: true - run: | - # Display patches - if [ ! -z clang-format.patch ]; then - echo "Clang-format patch" - echo "================" - cat clang-format.patch - echo "================" - fi - if [ ! -z clang-tidy.patch ]; then - echo "Clang-tidy patch" - echo "================" - cat clang-tidy.patch - echo "================" - fi - diff --git a/.github/workflows/lintAndFormat.yml b/.github/workflows/lintAndFormat.yml new file mode 100644 index 000000000..1a37bbbb8 --- /dev/null +++ b/.github/workflows/lintAndFormat.yml @@ -0,0 +1,188 @@ +name: Lint and Format + +on: + pull_request: + types: [assigned, opened, synchronize, reopened] + workflow_dispatch: + +env: + # Run apt package manager in the CI in non-interactive mode. + # Otherwise, on Ubuntu 20.04 the installation of tzdata asking question + DEBIAN_FRONTEND: noninteractive + +jobs: + + clang-tidy-pylint: + + name: C/C++ clang-tidy + + runs-on: ubuntu-22.04 + + permissions: + contents: write + pull-requests: write + + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 2 + submodules: "true" + + - name: Install clang-tidy + run: | + sudo apt-get update + sudo apt-get install -y clang-tidy ninja-build clang libelf-dev + + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Get Submodule Hash + id: get-submodule-hash + run: echo "::set-output name=hash::$(md5sum $(echo utils/clone-mlir-aie.sh))" + shell: bash + + - name: Ccache for C++ compilation + uses: hendrikmuhs/ccache-action@4687d037e4d7cf725512d9b819137a3af34d39b3 + with: + key: ${{ runner.os }}-lintformat-${{ steps.get-submodule-hash.outputs.hash }} + max-size: 1G + + - name: Build and Install libxaie + run: utils/github-clone-build-libxaie.sh + + - name: Get mlir-aie + id: clone-mlir-aie + run: utils/clone-mlir-aie.sh + + - name: Build and install mlir-aie + run: | + pushd mlir-aie + pip install -r python/requirements.txt + + VERSION=$(utils/clone-llvm.sh --get-wheel-version) + pip -q download mlir==$VERSION \ + -f https://github.com/Xilinx/mlir-aie/releases/expanded_assets/mlir-distro + unzip -q mlir-*.whl + find mlir -exec touch -a -m -t 201108231405.14 {} \; + popd + + utils/github-build-mlir-aie.sh + + - name: Prepare compile_commands.json + run: | + mkdir build + pushd build + + cmake .. \ + -GNinja \ + -DCMAKE_TOOLCHAIN_FILE=`pwd`/../cmake/modules/toolchain_x86_64.cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DLLVM_ENABLE_ASSERTIONS=ON \ + -DCMAKE_MODULE_PATH=`pwd`/../mlir-aie/cmake/modulesXilinx \ + -DMLIR_DIR=`pwd`/../mlir-aie/mlir/lib/cmake/mlir \ + -DLLVM_DIR=`pwd`/../mlir-aie/mlir/lib/cmake/llvm \ + -DAIE_DIR=`pwd`/../mlir-aie/install/lib/cmake/aie/ \ + -DLibXAIE_ROOT=`pwd`/../aienginev2/install \ + -DAIR_RUNTIME_TARGETS:STRING="x86_64" \ + -Dx86_64_TOOLCHAIN_FILE=`pwd`/../cmake/modules/toolchain_x86_64.cmake \ + -DLLVM_EXTERNAL_LIT=$(which lit) \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + + ninja air-headers mlir-headers + + popd + + - name: Analyze + id: clang-tidy-fixes + run: | + git fetch origin main + git diff -U0 origin/main | clang-tidy-diff -p1 -path build -export-fixes fixes.yml + if [ -f fixes.yml ]; then + echo "FIXES=true" | tee $GITHUB_OUTPUT + fi + + - name: Upload clang-tidy fixes + if: ${{ steps.clang-tidy-fixes.outputs.FIXES }} + uses: actions/upload-artifact@v3 + with: + path: fixes.yml + name: clang-tidy-fixes.yml + + + formatting: + + name: Python and C/C++ Check Format + + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + + steps: + - name: Get the project repository + uses: actions/checkout@v3 + with: + fetch-depth: 2 + submodules: "true" + + - name: Install clang-format + uses: aminya/setup-cpp@v1 + with: + clangformat: 17.0.1 + + - name: Setup Python env + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Install black + run: pip install black[jupyter] + + - name: Run git-clang-format + id: git-clang-format + run: | + git fetch origin main + # git clang-format returns an error if changes made? + git clang-format origin/main || true + git diff > clang-format.diff + cat clang-format.diff + + - name: Upload clang-format + uses: actions/upload-artifact@v3 + with: + path: clang-format.diff + name: format_diffs + + - name: Check C/C++ format + uses: reviewdog/action-suggester@v1 + with: + tool_name: clang-format + level: error + cleanup: true + fail_on_error: true + + - name: Run black format + if: success() || failure() + id: black-format + run: | + black . || true + git diff > black-format.diff + cat black-format.diff + + - name: Upload black-format + uses: actions/upload-artifact@v3 + with: + path: black-format.diff + name: format_diffs + + - name: Check Python format + if: success() || failure() + uses: reviewdog/action-suggester@v1 + with: + tool_name: black + level: error + fail_on_error: true diff --git a/utils/github-build-mlir-aie.sh b/utils/github-build-mlir-aie.sh index afdfaedeb..78bebed9e 100755 --- a/utils/github-build-mlir-aie.sh +++ b/utils/github-build-mlir-aie.sh @@ -24,6 +24,8 @@ pushd $MLIR_AIE_DIR/$BUILD_DIR cmake .. \ -GNinja \ -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ -DAIE_COMPILER=NONE \ -DAIE_LINKER=NONE \ -DHOST_COMPILER=NONE \