diff options
author | Ralf Jung <post@ralfj.de> | 2025-07-23 10:36:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-23 10:36:06 +0200 |
commit | b1aece90f32c0bb0685e1e79d6dc8e1a147bde37 (patch) | |
tree | b14632181519e576c7ffde8cff3b631dac9e56b6 /clang/lib/CodeGen/CodeGenModule.cpp | |
parent | 6c50e2b2dda185816b3a4d65cef6771dad5113d8 (diff) | |
download | llvm-b1aece90f32c0bb0685e1e79d6dc8e1a147bde37.zip llvm-b1aece90f32c0bb0685e1e79d6dc8e1a147bde37.tar.gz llvm-b1aece90f32c0bb0685e1e79d6dc8e1a147bde37.tar.bz2 |
LangRef: allocated objects can grow (#141338)
This enables the (reasonably common) pattern of using `mmap` to reserve
but not actually map a wide range of pages, and then only adding in more
pages as memory is actually needed. Effectively, that region of memory
is one big allocated object for LLVM, but crucially, that allocated
object *changes its size*.
Having an allocated object grow seems entirely compatible with what LLVM
optimizations assume, *except* that when LLVM sees an `alloca` or
similar instruction, it will assume that a pointer that has been
`getelementptr inbounds` by more than the size of the allocated object
cannot alias that `alloca`. But for allocated objects that are created
e.g. by `mmap`, where LLVM does not know their size, this cannot happen
anyway.
The other main point to be concerned about is having a `getelementptr
inbounds` that is moved up across an operation that grows an allocated
object: this should be legal as `getelementptr` is freely reorderable.
We achieve that by saying that for allocated objects that change their
size, "inbounds" means "inbounds of their maximal size", not "inbounds
of their current size".
It would be nice to also allow shrinking allocations (e.g. by
`munmap`ing pages at the end), but that is more tricky. Consider an
example like this:
- load 4 bytes from `ptr`
- call some function
- load 1 byte from `ptr`
Right now, LLVM could argue that since `ptr` clearly has not been
deallocated, there must be at least 4 bytes of dereferenceable memory
behind `ptr` after the call. If allocations can shrink, this kind of
reasoning is no longer valid. I don't know if LLVM actually does
reasoning like that -- I think it should not, since I think it should be
possible to have allocations that shrink -- but to remain conservative I
am not proposing that as part of this patch.
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
0 files changed, 0 insertions, 0 deletions