<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62512>62512</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Bogus `-Wtype-safety` warnings in MPICH when using const buffers
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
msimberg
</td>
</tr>
</table>
<pre>
I'm "cross-posting" an issue from https://github.com/pmodels/mpich/issues/6496 because I'm not sure what the most appropriate place for this is:
MPICH uses attributes from clang that are supposed to help catch type errors where the type of a buffer is not the same as the `MPI_Datatype` passed in to a function (https://clang.llvm.org/docs/AttributeReference.html#type-safety-checking). In functions that take a `const void*` buffer clang reports bogus (as far as I can tell) warnings where the data type is correct, but the constness is different. E.g.
```
const int x;
MPI_Send(&x, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
```
reports `warning: argument type 'const int *' doesn't match specified 'MPI' type tag that requires 'int *' [-Wtype-safety]`. The only difference is `const` and this call should not trigger a warning.
Workarounds include:
- Disabling `-Wtype-safety` either locally or globally. This is not satisfying because it completely turns off checking and mismatches are not at all reported. E.g.
```
int x;
MPI_Send(&x, 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
```
would not produce a warning.
- `const_cast` because we know `MPI_Send` anyway takes a `const` pointer but this is verbose and requires repeating the type:
```
const int x;
MPI_Send(const_cast<int*>(&x), 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
```
- Making a const `MPI_INT`. This is again verbose and requires repeating the type:
```
auto const MPI_CONST_INT = MPI_INT;
const int x;
MPI_Send(&x, 1, MPI_CONST_INT, 1, 0, MPI_COMM_WORLD);
```
- Disable the attribute when configuring MPICH. This is not always an option.
Here's a short reproducer on godbolt (without MPICH, but using the attributes): https://godbolt.org/z/M8P1oqz44.
Given that the MPICH developers think that this is a clang issue https://github.com/pmodels/mpich/issues/6496#issuecomment-1531691516: is it? That is:
- Is this working as intended? The warning does make sense in isolation because `int*` and `const int*` are not the same type. On the other hand, it does not make sense in the context of MPICH where `MPI_INT` is there only to signal the size of the type, and not the constness.
- Is there currently a way for MPICH to use the attributes while ignoring constness? If yes, what would that be?
- If there's no way currently to treat `int*` and `const int*` as the same for the purposes of the warnings, would it make sense to add an option to the attribute to support that?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV0Fv2zwS_TX0ZWBDpmLZPviQOJ-3BjZfgzZAjwEljSRuKFIlqbjur18MKcl2d1G03QWK1KYtzps3896MhXOy1og7tnpgq8eZ6H1j7K51ss3R1rPclOfdkfF1C4zzwhrn5p1xXuqacQ5Cg3SuR6isaaHxvnMsvWf8wPihlr7p80VhWsYPXWtKVI7xQ9vJomH8EJ6jg-xum0GOhegdQgyljQfXW4RTIzz4BqE1zoPoOms6K4VH6JQoECpjwTfSgQxxk0eWDH-fno_7D9A7dCC8tzLvPboIs1BC1-DpamERXN91xmEJ3kCDqoNC-KIBf-4Q0FpjHZwatBhwhFNTgYC8ryq0IF1AS5850SIIF16zLHl6Pr4-Ci_oEZYl0AlHUaSmQAKqXhdeGg2Mb26JC_gWSr23C2Nrxg-lKYio-zGPT1ihRV3govGtYjylEHMnKvTnedFg8RbKs13AUU9xXMzYizcEQfgKo52HdyNLxu8J4JBRpMdiZ6x3kJu6d4RROKiEpfyOUAgNHpVifAsnYbXU9TVJpfAiMiUdFMZaLDzje8j7SFSIrNFR2aCUVcjGL-CvRb0Yapglw7_wNkKV2sM3lj5MBX79jLpkfMN49o3uX9IfOj_-_TK9T8bD_cenp9cvHz_985Hx7XTND5HGtFmWDImx9B6ErfsWtY9JMb6-ACLu-BpKg04zvvbQhu5xHRaykljSt5-eqa3jw14MrWfxay8tErfrq4vY6mH-5aqebPXIsmQBLw2C0eo88VUEdsc6Uv2ELqMYCqEUuMb0qozNaWVdowUxFmtxrZQvxr4Ja3pdOpC6UH2Jk5bm8CidyJXUNYW6RZYlgNI3aEEZCnkGY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