Move all script-dependent stack allocations to scratch maps
Obviously at the program level the aggregate stack usage is always script dependent. This issue is specific to individual stack allocations that are script dependent. This attempts to tackle the issue of big strings being "infectious", in that it can cause other language construct stack allocations to balloon.
To that end, I audited every CreateAllocaBPF() call in the codebase and compiled the following list of script-dependent allocas. The easily fixable ones are marked as such. The rest need to use scratch maps.
- Reading from a map https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/irbuilderbpf.cpp#L584
- Instantiating a tuple https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2248
- Deleting a non-clearable map element https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L765
- Updating a map element
https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2259-L2316
- Some of these allocations are only word sized, though. But the others are script-dependent
- Getting a map key https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2854 https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2867 https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L2907
- Using a ternary
https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L1885-L1890
- The stack allocations here seem unnecessary. Can probably remove them.
- Reading an element from a data structure
https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L3963
https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L4003
- Pre-existing and unrelated to big strings
- Dereferencing a pointer
https://github.com/bpftrace/bpftrace/blob/b2e255870ba010d4a7e4852bffcf1c567b016fd0/src/ast/passes/codegen_llvm.cpp#L1840
- This one is pre-existing and is unrelated to big strings
Good news is the issue is not too bad yet. Three broad categories:
- map keys
- map values
- tuples
They are all amenable to current scratch map approach
That was the last one, yay!