Commit 0addc0d
[CIR][ABI][Lowering] Fixes calling convention (llvm#1308)
This PR fixes two run time bugs in the calling convention pass. These
bugs were found with `csmith`.
Case llvm#1. Return value from a function.
Before this PR the returned value were stored in a bit casted memory
location.
But for the next example it's not safe: the size of a memory slot is
less than the size of return value. And the store operation cause a
segfault!
```
#pragma pack(push)
#pragma pack(1)
typedef struct {
int f0 : 18;
int f1 : 31;
int f2 : 5;
int f3 : 29;
int f4 : 24;
} PackedS;
#pragma pack(pop)
```
CIR type for this struct is `!ty_PackedS1_ = !cir.struct<struct
"PackedS1" {!cir.array<!u8i x 14>}>`, i.e. it occupies 14 bytes.
Before this PR the next code
```
PackedS foo(void) {
PackedS s;
return s;
}
void check(void) {
PackedS y = foo();
}
```
produced the next CIR:
```
%0 = cir.alloca !ty_PackedS1_, !cir.ptr<!ty_PackedS1_>, ["y", init] {alignment = 1 : i64}
%1 = cir.call @foo() : () -> !cir.array<!u64i x 2>
%2 = cir.cast(bitcast, %0 : !cir.ptr<!ty_PackedS1_>), !cir.ptr<!cir.array<!u64i x 2>>
cir.store %1, %2 : !cir.array<!u64i x 2>, !cir.ptr<!cir.array<!u64i x 2>>
```
As one cat see, `%1` is an array of two 64-bit integers and the memory
was allocated for 14 bytes only (size of struct). Hence the segfault!
This PR fixes such cases and now we have a coercion through memory,
which is even with the OG.
Case llvm#2. Passing an argument from a pointer deref.
Previously for the struct types passed by value we tried to find alloca
instruction in order to use it as a source for memcpy operation. But if
we have pointer dereference, (in other words if we have a `<!cir.ptr <
!cir.ptr ... > >` as alloca result) we don't need to search for the
address of the location where this pointer stored - instead we're
interested in the pointer itself. And it's a general approach - instead
of trying to find an alloca instruction we need to find a first pointer
on the way - that will be an address we meed to use for the memcpy
source.
I combined these two cases into a single PR since there are only few
changes actually. But I can split in two if you'd prefer1 parent f6cc5f6 commit 0addc0d
File tree
2 files changed
+113
-19
lines changed- clang
- lib/CIR/Dialect/Transforms/TargetLowering
- test/CIR/CallConvLowering/AArch64
2 files changed
+113
-19
lines changedLines changed: 25 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
235 | 246 | | |
236 | 247 | | |
237 | 248 | | |
| |||
338 | 349 | | |
339 | 350 | | |
340 | 351 | | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
345 | 356 | | |
346 | 357 | | |
347 | 358 | | |
| |||
371 | 382 | | |
372 | 383 | | |
373 | 384 | | |
374 | | - | |
375 | 385 | | |
376 | 386 | | |
377 | 387 | | |
| |||
1265 | 1275 | | |
1266 | 1276 | | |
1267 | 1277 | | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
1268 | 1286 | | |
1269 | 1287 | | |
1270 | 1288 | | |
| |||
1283 | 1301 | | |
1284 | 1302 | | |
1285 | 1303 | | |
| 1304 | + | |
1286 | 1305 | | |
1287 | | - | |
1288 | 1306 | | |
1289 | 1307 | | |
1290 | | - | |
1291 | | - | |
1292 | | - | |
1293 | | - | |
1294 | | - | |
1295 | | - | |
| 1308 | + | |
1296 | 1309 | | |
1297 | 1310 | | |
1298 | 1311 | | |
| |||
Lines changed: 88 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
302 | 303 | | |
303 | 304 | | |
304 | 305 | | |
305 | | - | |
| 306 | + | |
306 | 307 | | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
315 | 316 | | |
316 | 317 | | |
317 | | - | |
318 | 318 | | |
319 | 319 | | |
320 | 320 | | |
| |||
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
0 commit comments