Skip to content

Conversation

@ggerganov
Copy link
Member

@ggerganov ggerganov commented Jan 11, 2025

This flag would be useful to prevent shadowing class members.

TODO:

  • Fix all existing warnings
  • Does clang-tidy support this?

@ggerganov ggerganov changed the title cmake : enable -Wshadow for C++ code [no ci] cmake : enable -Wshadow for C++ code Jan 11, 2025
@github-actions github-actions bot added the build Compilation issues label Jan 11, 2025

// mutable variable, needed during the last layer of the computation to skip unused tokens
int32_t n_tokens = this->n_tokens;

Copy link
Member Author

@ggerganov ggerganov Jan 12, 2025

Choose a reason for hiding this comment

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

This variable is mutated at il == n_layer - 1, which IIUC has practically no effect on the computation. Am I missing something? Maybe the intent was to mutate it at il == n_layer - 2?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like historically it was used by llm_build_moe_ffn, but was replaced by int64_t n_tokens = cur->ne[1] at some point. It's safe to remove it here though

@ggerganov ggerganov force-pushed the gg/llama-shadow-on branch 2 times, most recently from 039f77d to f67d7ec Compare January 12, 2025 11:28
@github-actions github-actions bot added the devops improvements to build systems and github actions label Jan 12, 2025
@github-actions github-actions bot added the ggml changes relating to the ggml tensor library for machine learning label Jan 12, 2025
ggml-ci
@ggerganov ggerganov marked this pull request as ready for review January 12, 2025 15:22
@ggerganov ggerganov requested a review from ngxson as a code owner January 12, 2025 15:22
Comment on lines +21 to +23
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(APPEND CXX_FLAGS -Wshadow-field-in-constructor)
endif()
Copy link
Member Author

Choose a reason for hiding this comment

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

Decided to add the -Wshadow-field-in-constructor flag because there is no way to disable it with GCC, so it would be more consistent to have it in Clang as well. This requires to add underscore _ suffix to constructor arguments that have the same name as the members in order to disambiguate the two.


// Copy a graph to a different backend
GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph);
GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy_init(ggml_backend_t backend, struct ggml_cgraph * graph);
Copy link
Member Author

Choose a reason for hiding this comment

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

@slaren This function caused a shadow warning with GCC, so I renamed it to ggml_backend_graph_copy_init:

llama.cpp/ggml/src/../include/ggml-backend.h:334:119: warning: ‘ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t, ggml_cgraph*)’ hides constructor for ‘struct ggml_backend_graph_copy’ [-Wshadow]
  334 |     GGML_API struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph);

Copy link
Member

Choose a reason for hiding this comment

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

I would rather rename the struct to something like ggml_backend_graph_copy_state, since this is not really a constructor, it's just that it returns multiple objects to the caller.

@ggerganov
Copy link
Member Author

ggerganov commented Jan 12, 2025

This change increases the difficulty of naming things since we can no longer reuse symbol names in nested contexts. IMO it's a useful change, but I'm having doubts about the extra effort that would be required to come up with meaningful and consistent names.

An alternative solution is to keep -Wno-shadow and start prefixing class members with something like m_ - this would be more straightforward.

I'm leaning more toward the former option of enabling -Wshadow. Let me know what you think about this change and if you have any alternatives or preferences in mind.

@ggerganov ggerganov requested a review from slaren January 12, 2025 15:34
@slaren
Copy link
Member

slaren commented Jan 12, 2025

My opinion is that shadowing is rarely a problem by itself. The cases when shadowing can become a source of bugs are often caused by having functions that are too big and complex to really understand. When functions are brief and concise, with clear and simple responsibilities, shadowing is rarely a problem because the variable declaration is always close to the location where it is used.

@ngxson
Copy link
Collaborator

ngxson commented Jan 12, 2025

I'd agree with @slaren that shadowing is rarely a problem. Even if there are bugs caused by that, most of the time they will produce obvious bugs that are easy to pinpoint. IMO -Wshadow is quite strict and more suitable for "high stake" situation like writing program to be used in aerospace.

Also, this makes new (and even existing) contributors to spend more time on fixing naming, which may result in a poor DX overall.

@ggerganov ggerganov marked this pull request as draft January 13, 2025 12:48
@ggerganov
Copy link
Member Author

Yes, this change is probably not really worth it. Let's shelve it for now.

@ericcurtin
Copy link
Collaborator

This change increases the difficulty of naming things since we can no longer reuse symbol names in nested contexts. IMO it's a useful change, but I'm having doubts about the extra effort that would be required to come up with meaningful and consistent names.

Sometimes I like to discourage unnecessary nesting. Like C++ lamdas, why use them when you can write a static function with a nice descriptive name? Or random scope blocks (which again could be a little static function with a nice descriptive name).

An alternative solution is to keep -Wno-shadow and start prefixing class members with something like m_ - this would be more straightforward.

some people do a trailing _ suffix also, saves a character. But I really don't mind. Too many standards can slow down PRs getting merged significantly also, which is not good 😄

I'm glad we got the .clang-format file in. I struggle to remember the little nitpicks of each and every C/C++ codebase, one less thing to worry about 😄

I'm leaning more toward the former option of enabling -Wshadow. Let me know what you think about this change and if you have any alternatives or preferences in mind.

@ggerganov ggerganov closed this May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Compilation issues devops improvements to build systems and github actions examples ggml changes relating to the ggml tensor library for machine learning server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants