Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Unified clang format #1536

Merged
merged 5 commits into from
Jan 17, 2025
Merged

Unified clang format #1536

merged 5 commits into from
Jan 17, 2025

Conversation

ldrumm
Copy link
Contributor

@ldrumm ldrumm commented Apr 22, 2024

Until now we've had multiple styles throughout the project. This doesn't
buy us anything for contributors as each source file might be different - totally negating the point of having a clang-format style in the first
place - indeed I started debugging clang-format since it was apparently
not formatting my adapter code as 4 spaces after my editor had loaded
the editconfig file specifying 4 spaces. I wasted an hour on that!

Here we codify everything as LLVM style, which seems to be used by the
majority of the project, and change the placement of autogenerated doxygen comments.

n.b. if you want to see what's really happening just look at the first two commits, f68e8c0, and fd6b63b - the others are just regeneration or formatting

intel/llvm#16672

@ldrumm ldrumm requested review from a team as code owners April 22, 2024 09:30
@ldrumm ldrumm requested a review from MartinWehking April 22, 2024 09:30
@github-actions github-actions bot added loader Loader related feature/bug common Changes or additions to common utilities conformance Conformance test suite issues. level-zero L0 adapter specific issues cuda CUDA adapter specific issues hip HIP adapter specific issues native-cpu Native CPU adapter specific issues sanitizer Sanitizer layer issues/changes/specification labels Apr 22, 2024
@pbalcer
Copy link
Contributor

pbalcer commented Apr 22, 2024

This was intentional to keep headers in a more readable format. Also, disabling ReflowComments was necessary to keep the output consistent, as the option is not indempotent.

@ldrumm
Copy link
Contributor Author

ldrumm commented Apr 22, 2024

This was intentional to keep headers in a more readable format.

Define "readable".

@pbalcer
Copy link
Contributor

pbalcer commented Apr 22, 2024

This was intentional to keep headers in a more readable format.

Define "readable".

If I recall correctly, the primary issue was line breaks around comments and enum value definitions. Take a look at this enum: https://github.com/oneapi-src/unified-runtime/blob/main/include/ur_api.h#L415

@ldrumm
Copy link
Contributor Author

ldrumm commented Apr 22, 2024

This was intentional to keep headers in a more readable format.

Define "readable".

If I recall correctly, the primary issue was line breaks around comments and enum value definitions. Take a look at this enum: https://github.com/oneapi-src/unified-runtime/blob/main/include/ur_api.h#L415

That problem could be neatly solved by putting the comment on the line before and using /// instead of <///

typedef enum ur_result_t {
  /// Success
  UR_RESULT_SUCCESS = 0,
  /// Invalid operation
  UR_RESULT_ERROR_INVALID_OPERATION = 1,
  /// Invalid queue properties
  UR_RESULT_ERROR_INVALID_QUEUE_PROPERTIES = 2,
  /// Invalid queue
  UR_RESULT_ERROR_INVALID_QUEUE = 3,
  /// Invalid Value
  UR_RESULT_ERROR_INVALID_VALUE = 4,
  /// Invalid context
  UR_RESULT_ERROR_INVALID_CONTEXT = 5,
  /// Invalid platform
  UR_RESULT_ERROR_INVALID_PLATFORM = 6,
  /// Invalid binary
  UR_RESULT_ERROR_INVALID_BINARY = 7,
  /// Invalid program
  UR_RESULT_ERROR_INVALID_PROGRAM = 8,
  /// Invalid sampler
  UR_RESULT_ERROR_INVALID_SAMPLER = 9,
  /// Invalid buffer size
  UR_RESULT_ERROR_INVALID_BUFFER_SIZE = 10,
  /// Invalid memory object
  UR_RESULT_ERROR_INVALID_MEM_OBJECT = 11,
  /// Invalid event
  UR_RESULT_ERROR_INVALID_EVENT = 12,

@ldrumm
Copy link
Contributor Author

ldrumm commented Apr 22, 2024

I just checked my above suggestion. This might use more or less vertical space than the existing version, since lines aren't off the right hand side. As an example:

urLoaderConfigGetInfo(
    ur_loader_config_handle_t hLoaderConfig, ///< [in] handle of the loader config object
    ur_loader_config_info_t propName,        ///< [in] type of the info to retrieve
    size_t propSize,                         ///< [in] the number of bytes pointed to by pPropValue.
    void *pPropValue,                        ///< [out][optional][typename(propName, propSize)] array of bytes holding
                                             ///< the info.
                                             ///< If propSize is not equal to or greater than the real number of bytes
                                             ///< needed to return the info
                                             ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and
                                             ///< pPropValue is not used.
    size_t *pPropSizeRet                     ///< [out][optional] pointer to the actual size in bytes of the queried propName.
);

->

urLoaderConfigGetInfo(
  /// [in] handle of the loader config object
  ur_loader_config_handle_t hLoaderConfig,
  /// [in] type of the info to retrieve
  ur_loader_config_info_t propName,
  /// [in] the number of bytes pointed to by pPropValue.
  size_t propSize,
  /// [out][optional][typename(propName, propSize)] array of bytes holding the
  /// info. If propSize is not equal to or greater than the real number of bytes
  /// needed to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is
  /// returned and pPropValue is not used.
  void *pPropValue,
  /// [out][optional] pointer to the actual size in bytes of the queried propName.
  size_t *pPropSizeRet
);

The comment and declaration for pPropValue takes fewer lines here, but the shorter comments occupy more space.

Here is another example, where certain sequences can end up the same length, but with a slightly more consistent appearance : enumerations where the enumerator itself is long, and explicit values for the enumerator declaration:

    UR_FUNCTION_CONTEXT_SET_EXTENDED_DELETER =
      7,                      ///< Enumerator for ::urContextSetExtendedDeleter

->

  /// Enumerator for ::urContextSetExtendedDeleter
  UR_FUNCTION_CONTEXT_SET_EXTENDED_DELETER = 7,

Generalising to all autogenerated comment lines

diff --git a/scripts/templates/helper.py b/scripts/templates/helper.py
index 3b8e5693..678159ef 100644
--- a/scripts/templates/helper.py
+++ b/scripts/templates/helper.py
@@ -768,8 +768,8 @@ def make_etor_lines(namespace, tags, obj, meta=None):
             prologue = "%s,"%(name)
 
         for line in split_line(subt(namespace, tags, item['desc'], True), 70):
-            lines.append("%s%s %s"%(append_ws(prologue, 48), "///<", line))
-            prologue = ""
+            lines.append(" /// %s" % line)
+        lines.append(prologue)
 
     lines += [
         "/// @cond",
@@ -820,8 +820,8 @@ def make_member_lines(namespace, tags, obj, prefix="", meta=None):
         prologue = "%s %s %s;"%(tname, name, array_suffix)
 
         for line in split_line(subt(namespace, tags, item['desc'], True), 70):
-            lines.append("%s%s %s"%(append_ws(prologue, 48), "///<", line))
-            prologue = ""
+            lines.append(" /// %s" % line)
+        lines.append(prologue)
     return lines
 
 """
@@ -869,8 +869,8 @@ def make_param_lines(namespace, tags, obj, decl=False, meta=None, format=["type"
         if "desc" in format:
             desc = item['desc']
             for line in split_line(subt(namespace, tags, desc, True), 70):
-                lines.append("%s///< %s"%(append_ws(prologue, 48), line))
-                prologue = ""
+                lines.append(" /// %s" % line)
+            lines.append(prologue)
         else:
             lines.append(prologue)
 

running the generate target leaves the following files modified:

   include/ur_api.h
   include/ur_ddi.h
   scripts/templates/helper.py
   source/adapters/null/ur_nullddi.cpp
   source/loader/layers/tracing/ur_trcddi.cpp
   source/loader/layers/validation/ur_valddi.cpp
   source/loader/ur_ldrddi.cpp
   source/loader/ur_libapi.cpp
   source/ur_api.cpp

The total line length change for those files:
before:

  12411 include/ur_api.h
   1832 include/ur_ddi.h
   1564 scripts/templates/helper.py
   6818 source/adapters/null/ur_nullddi.cpp
   7650 source/loader/layers/tracing/ur_trcddi.cpp
  10658 source/loader/layers/validation/ur_valddi.cpp
   9173 source/loader/ur_ldrddi.cpp
   8706 source/loader/ur_libapi.cpp
   7390 source/ur_api.cpp
  66202 total

After:

  13051 include/ur_api.h
   1832 include/ur_ddi.h
   1564 scripts/templates/helper.py
   6986 source/adapters/null/ur_nullddi.cpp
   7671 source/loader/layers/tracing/ur_trcddi.cpp
  10153 source/loader/layers/validation/ur_valddi.cpp
   8986 source/loader/ur_ldrddi.cpp
   8770 source/loader/ur_libapi.cpp
   7637 source/ur_api.cpp
  66650 total

That's a less than 1% line length increase for generated code, so I think this could be a win too

Copy link
Contributor

@jchlanda jchlanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we codify everything as LLVM style, which seems to be used by the majority of the project.

I might be biased, but given the current use of unified runtime - oneAPI, which in turn is forked from llvm, the fact that we bring this repo's style closer to the mainline can only be seen as a net positive.

@MartinWehking
Copy link

I agree with @ldrumm and @jchlanda.
It would definitely be better to not have 2 different styles for unified runtime and intel/llvm.

Copy link
Contributor

@nrspruit nrspruit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for Level Zero

@ldrumm ldrumm force-pushed the unified-clang-format branch from aff3eeb to feab158 Compare May 24, 2024 15:46
@ldrumm
Copy link
Contributor Author

ldrumm commented May 24, 2024

Thanks for the reviews everyone. I've now updated the patch with the suggestions from this comment

The substantive changes are ldrumm@65322c4 and ldrumm@c2610b2. The third commit, is simply the result of running the generate target, and then committing all changes so is entirely mechanical.

It's quite a lot of churn, but the separate commits should make it easy for people to resolve merge conflicts

@github-actions github-actions bot added the opencl OpenCL adapter specific issues label Jan 13, 2025
@ldrumm
Copy link
Contributor Author

ldrumm commented Jan 13, 2025

The SPDX license identifier needs to be on its own line according to the spec: https://spdx.github.io/spdx-spec/v2.3/using-SPDX-short-identifiers-in-source-files/

python needs 4 space indents in the .editorconfig

@ldrumm
Copy link
Contributor Author

ldrumm commented Jan 13, 2025

The SPDX license identifier needs to be on its own line according to the spec: https://spdx.github.io/spdx-spec/v2.3/using-SPDX-short-identifiers-in-source-files/

python needs 4 space indents in the .editorconfig

Fixen

@ldrumm ldrumm force-pushed the unified-clang-format branch 2 times, most recently from aff32df to e233656 Compare January 15, 2025 17:44
@ldrumm
Copy link
Contributor Author

ldrumm commented Jan 15, 2025

I think this should be good to go if tests pass. I'll add the ready-to-merge label and then wait for something terrible to happen

@ldrumm ldrumm added the ready to merge Added to PR's which are ready to merge label Jan 15, 2025
@kbenzie
Copy link
Contributor

kbenzie commented Jan 16, 2025

Since this is changing a lot of files all at once I do wonder if this would also be an opportune time to update the clang-format version used from 15 to the version built as part of intel/llvm. This idea is with an eye to existing within the intel/llvm repo moving forwards.

.editorconfig Show resolved Hide resolved
@ldrumm ldrumm force-pushed the unified-clang-format branch from ea581dc to 1990bad Compare January 16, 2025 13:20
@ldrumm
Copy link
Contributor Author

ldrumm commented Jan 16, 2025

Since this is changing a lot of files all at once I do wonder if this would also be an opportune time to update the clang-format version used from 15 to the version built as part of intel/llvm. This idea is with an eye to existing within the intel/llvm repo moving forwards.

Can do. It's trivial for me to remake this patch with new versions of tools / rebase it.
Will you update the clang-format version and I rebase on top of your work or should I add it to this patch series?

@ldrumm
Copy link
Contributor Author

ldrumm commented Jan 16, 2025 via email

@ldrumm ldrumm force-pushed the unified-clang-format branch from 1990bad to c2bde1f Compare January 16, 2025 15:33
@github-actions github-actions bot added the command-buffer Command Buffer feature addition/changes/specification label Jan 16, 2025
@ldrumm ldrumm force-pushed the unified-clang-format branch 4 times, most recently from 5768875 to 509db23 Compare January 16, 2025 17:28
Copy link
Contributor

@kbenzie kbenzie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will aim to merge this next once the conflicts are resolved.

Until now we've had multiple styles throught the project. This doesn't
buy us anything for contributors as each source file might be different
- totally negating the point of having a clang-format style in the first
place - indeed I started debugging clang-format since it was apparently
not formatting my adapter code as 4 spaces after my editor had loaded
the editconfig file specifying 4 spaces. I wasted an hour on that!

Here we codify everything as LLVM style, which seems to be used by the
majority of the project.

We also bump the clang-format version requirement to v18.1 to align with
dpc++.
How comments are laid out can have a significant effect on readability.
If we're to have 80column headers, then placing documentation comments
before a declaration line - rather than on the end of the same line -
can enable clang format to generate more readable headers without a
great change in the overall line length..

This might use more or less vertical space than the existing version,
since lines aren't off the right hand side. As an example:

```cpp
urLoaderConfigGetInfo(
    ur_loader_config_handle_t hLoaderConfig, ///< [in] handle of the loader config object
    ur_loader_config_info_t propName,        ///< [in] type of the info to retrieve
    size_t propSize,                         ///< [in] the number of bytes pointed to by pPropValue.
    void *pPropValue,                        ///< [out][optional][typename(propName, propSize)] array of bytes holding
                                             ///< the info.
                                             ///< If propSize is not equal to or greater than the real number of bytes
                                             ///< needed to return the info
                                             ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and
                                             ///< pPropValue is not used.
    size_t *pPropSizeRet                     ///< [out][optional] pointer to the actual size in bytes of the queried propName.
);
```
->

```cpp
urLoaderConfigGetInfo(
  /// [in] handle of the loader config object
  ur_loader_config_handle_t hLoaderConfig,
  /// [in] type of the info to retrieve
  ur_loader_config_info_t propName,
  /// [in] the number of bytes pointed to by pPropValue.
  size_t propSize,
  /// [out][optional][typename(propName, propSize)] array of bytes holding the
  /// info. If propSize is not equal to or greater than the real number of bytes
  /// needed to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is
  /// returned and pPropValue is not used.
  void *pPropValue,
  /// [out][optional] pointer to the actual size in bytes of the queried propName.
  size_t *pPropSizeRet
);
```
The comment and declaration for pPropValue takes fewer lines here, but
the shorter comments occupy more space.

Here is another example, where certain sequences can end up the same
length, but with a slightly more consistent appearance : enumerations
where the enumerator itself is long, and explicit values for the
enumerator declaration:

```cpp
    UR_FUNCTION_CONTEXT_SET_EXTENDED_DELETER =
      7,                      ///< Enumerator for ::urContextSetExtendedDeleter
```
->
```cpp
  /// Enumerator for ::urContextSetExtendedDeleter
  UR_FUNCTION_CONTEXT_SET_EXTENDED_DELETER = 7,
```

```
The total line length change for those files touched by the generator:
before:
```
  12411 include/ur_api.h
   1832 include/ur_ddi.h
   1564 scripts/templates/helper.py
   6818 source/adapters/null/ur_nullddi.cpp
   7650 source/loader/layers/tracing/ur_trcddi.cpp
  10658 source/loader/layers/validation/ur_valddi.cpp
   9173 source/loader/ur_ldrddi.cpp
   8706 source/loader/ur_libapi.cpp
   7390 source/ur_api.cpp
  66202 total
```
After:
```
  13051 include/ur_api.h
   1832 include/ur_ddi.h
   1564 scripts/templates/helper.py
   6986 source/adapters/null/ur_nullddi.cpp
   7671 source/loader/layers/tracing/ur_trcddi.cpp
  10153 source/loader/layers/validation/ur_valddi.cpp
   8986 source/loader/ur_ldrddi.cpp
   8770 source/loader/ur_libapi.cpp
   7637 source/ur_api.cpp
  66650 total
```

That's a less than 1% line length increase for generated code, so I
think this is a win.
clang-format and generation rules were updated. This is simply a run of
the generator on that code.

If you need to rebase as a result of the churn, all that should be
needed is to run the generate target on "theirs".
The comments are now more left-aligned leaving more space for prose.
The SPDX-license header must be on its own line according to the spec
@ldrumm ldrumm force-pushed the unified-clang-format branch from 509db23 to 041179a Compare January 17, 2025 13:34
kbenzie added a commit to kbenzie/intel-llvm that referenced this pull request Jan 17, 2025
oneapi-src/unified-runtime#1536

Align the UR clang-format version with intel/llvm.
@kbenzie kbenzie merged commit 029a977 into oneapi-src:main Jan 17, 2025
73 checks passed
kbenzie added a commit to kbenzie/intel-llvm that referenced this pull request Jan 17, 2025
oneapi-src/unified-runtime#1536

Align the UR clang-format version with intel/llvm.
kbenzie added a commit to kbenzie/intel-llvm that referenced this pull request Jan 17, 2025
oneapi-src/unified-runtime#1536

Align the UR clang-format version with intel/llvm.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
command-buffer Command Buffer feature addition/changes/specification common Changes or additions to common utilities conformance Conformance test suite issues. cuda CUDA adapter specific issues hip HIP adapter specific issues level-zero L0 adapter specific issues loader Loader related feature/bug native-cpu Native CPU adapter specific issues opencl OpenCL adapter specific issues ready to merge Added to PR's which are ready to merge sanitizer Sanitizer layer issues/changes/specification
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants