Skip to content

Commit

Permalink
Organize contributing docs in a subdir and add notes on clang-tidy (#…
Browse files Browse the repository at this point in the history
…15235)

### Ticket
None

### Problem description
Wanted to add clang-tidy notes to the repo. Expanded the scope to
organize docs for contributing to the repo a little better.

### What's changed
Moved existing contribution-related docs to a dedicated subdir
Added a clang-tidy doc
Referenced the extra docs from the entry-point CONTRIBUTING.md

### Checklist
- [ ] Post commit CI passes
- [ ] Blackhole Post commit (if applicable)
- [ ] Model regression CI testing passes (if applicable)
- [ ] Device performance regression CI testing passes (if applicable)
- [ ] New/Existing tests provide coverage for changes
  • Loading branch information
afuller-TT authored Nov 20, 2024
1 parent c177d4f commit a7fcd67
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
15 changes: 14 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Table of Contents
- [Machine setup](#machine-setup)
- [Hugepages setup](#hugepages-setup)
- [Developing tt-metal](#developing-tt-metal)
- [Setting up Git](#setting-up-git)
- [Setting logger level](#setting-logger-level)
- [Building and viewing the documentation locally](#building-and-viewing-the-documentation-locally)
- [Tests in tt-metal](#tests-in-tt-metal)
Expand All @@ -22,17 +21,25 @@ Table of Contents
- [Debugging host-side code](#debugging-host-side-code)
- [Debugging device-side code](#debugging-device-side-code)
- [Debugging device hangs](#debugging-device-hangs)
- [Using watcher](#using-watcher)
- [Using watcher hang dump tool](#using-watcher-hang-dump-tool)
- [Contribution standards](#contribution-standards)
- [File structure and formats](#file-structure-and-formats)
- [CI/CD Principles](#cicd-principles)
- [Using CI/CD for development](#using-cicd-for-development)
- [Skipping CI/CD for documentation updates](#skipping-cicd-for-documentation-updates)
- [Documentation](#documentation)
- [Git rules and guidelines](#git-rules-and-guidelines)
- [Creating a branch](#creating-a-branch)
- [Saving your changes](#saving-your-changes)
- [Saving the commit to origin and create a pull request](#saving-the-commit-to-origin-and-create-a-pull-request)
- [Rebasing your branch](#rebasing-your-branch)
- [Merging to main](#merging-to-main)
- [Code reviews](#code-reviews)
- [New feature and design specifications](#new-feature-and-design-specifications)
- [Release flows](#release-flows)
- [Logging, assertions, and exceptions](#logging-assertions-and-exceptions)
- [Further reading](#further-reading)
- [Hardware troubleshooting](#hardware-troubleshooting)
- [Resetting an accelerator board](#resetting-an-accelerator-board)

Expand Down Expand Up @@ -780,6 +787,12 @@ After that, the UI will usually delete your branch.
- Use Loguru for Python logging.
- Use Tenstorrent logger for C++ logging.

### Further reading

- [General best practices](contributing/BestPractices.md)
- [Error message best practices](contributing/ErrorMessageBestPractices.md)
- [Working with Clang Tidy](contributing/ClangTidy.md)

## Hardware troubleshooting

### Resetting an accelerator board
Expand Down
File renamed without changes.
49 changes: 49 additions & 0 deletions contributing/ClangTidy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Scan the repo for clang-tidy violations
This is done automatically in post-commit, or can be run manually via [Code analysis · Workflow runs · tenstorrent/tt-metal](https://github.com/tenstorrent/tt-metal/actions/workflows/code-analysis.yaml)

# Enable a check
Delete the line from [tt-metal/.clang-tidy](../.clang-tidy) that disables the check. Check [Clang-Tidy Checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html) and see if another check is an alias for the same. If so, then delete the other name, too. Now fix all the diagnostics that are now flagged!

> We take the approach of “enable everything, then disable specific checks” so that we have a clear TODO list to work on, rather than the opposite that only tells us where we’re currently at.
# Fix the violations
## Automatic
If the check being enabled has FIX-ITs (see [Clang-Tidy Checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html) and note the Offers fixes column), then leverage it by doing the following:

Prepare a clang-tidy tree

```
cmake --prefix clang-tidy-fix
cmake --build --prefix clang-tidy-fix --target clean
cmake --build --prefix clang-tidy-fix
```
Go home for the day. This will take a long long time if you only have a dozen cores.

When it’s done, review the changes it made and commit. Re-run the final build step until it reaches the end.

Perform a final clean && build-everything to ensure everything was fixed. Some checks have automatic fixes only for a subset of what it is able to diagnose. The remaining diagnostics will need to be addressed manually.

## Manual
If the check does not have FIX-ITs, then it’s a manual process. Perform the same steps as above, but after each build, review the log and manually address each diagnostic.

# FAQ
## What sorts of things can Clang Tidy detect?
Many things. Some categories of checks are performance, security, modernization, readability, recommended practices, and convensions. For a full list see [Clang-Tidy Checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html).

## Can we turn on ALL the checks?
No. Some checks are mutually exclusive. Generally when a style or opinion is involved. For example [modernize-use-trailing-return-type](https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-trailing-return-type.html) helps to use a trailing return type. While [fuchsia-trailing-return](https://clang.llvm.org/extra/clang-tidy/checks/fuchsia/trailing-return.html) enforces that such syntax is NOT used.

## What gotchas are there with the automatic FIX-ITs?
* When a FIX-IT is attempted on a header file referenced by multiple TUs, the resulting race condition can butcher the header file. Workaround: git revert the affected file(s) and then run ninja -j1 to build single-threaded for long enough to process the header exactly once before going back to parallel builds. If this is chronic, then consider running ninja -j1 -k0 and check back after the weekend.

* Some checks have FIX-ITs only for a subset of what it is able to detect. Just because a check says it has FIX-ITs, don’t assume that it’s able to fix everything. eg: when fixing performance-unnecessary-value-param, it seemed to not attempt an auto-fix inside templates or lambdas. Those had to be manually adjusted.

* Sometimes the automatic edit can leave the repo in an unbuildable state. This was encountered when fixing performance-unnecessary-value-param and a function was defined in one TU, and forward declares and invoked in another TU with no shared definition between them. Clang-tidy diagnosed and fixed the definition, but the forward declaration was untouched, leaving it dangling and causing an undefinted reference at link time.

## Why are we scanning during a build instead of just pointing run-clang-tidy at the compile_commands.json?
In compile_commands.json, all code is equal. But in tt-metal, some code is 3rd party that we aren’t interested in scanning. We are unable to fully control this with judicious use of no-op .clang-tidy files as some 3rd party libs have their own .clang-tidy files. As a result we need the full control of a build from CMake to scan what we need and not what we don’t need. With ccache the build overhead is negligible in a clang-tidy scan, so it’s okay.

## What else should I know?
* Some checks have options to tweak the behaviour. Review the documentation for the check you’re enabling if you feel like it should behave a little differently than it does.

* One check in particular *requires* options. [readability-identifier-naming](https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html) has an elaborate set of knobs to define a naming convention and can even adjust existing code to the defined convention, within some constraints. But without specifying the naming conventions, the check itself will do nothing even if enabled.
File renamed without changes.

0 comments on commit a7fcd67

Please sign in to comment.