[CIR][ABI][Lowering] Fixes calling convention #1308
Merged
+112
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes two run time bugs in the calling convention pass. These bugs were found with
csmith
.Case #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!
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
produced the next CIR:
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 #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 prefer