allocas are emitted in reverse order. this is unintentional; can we fix? #1808
Replies: 3 comments 3 replies
-
Right, I think the only reason it's like this is b/c it was the easiest way to implement it.
Sounds reasonable to me
👍 |
Beta Was this translation helpful? Give feedback.
-
if we want to emit the allocas at the top of the function but in correct order (i.e. not reversed) I do have some code that demonstrates how to split the entry block into "entry" and "posthoist": Birch-san@078c5f0 (split the block) this would enable us to change |
Beta Was this translation helpful? Give feedback.
-
it also feels to me like i.e. it would no longer need knowledge of "how/whether to clean up". the same can be said of |
Beta Was this translation helpful? Give feedback.
-
CreateAllocaBPF
hoists allocas to the start of the entry block.I believe CreateAllocaBPF employs hoisting because it wishes to "ensure that a variable's storage is declared before any of its uses". probably to support a pathological case that you can get into with conditions (see #1787).
but one thing that's confusing about the generated code, is that variable allocations are emitted in reverse:
https://github.com/iovisor/bpftrace/blob/master/tests/codegen/builtin_arg.cpp
https://github.com/iovisor/bpftrace/blob/master/tests/codegen/llvm/builtin_arg.ll#L11-L14
if you call CreateAllocaBPF once, it hoists an alloca to the start of your program.
if you call CreateAllocaBPF again, it hoists another alloca to the start of your program, declaring it earlier even though we asked for it later.
this is not inherently a problem, but is a caveat that the reader needs to understand when reviewing the LLVM IR.
moreover it can make it hard to intuit how the BPF stack is laid out.
allocas are reversed because we put them at "the start of the entry block".
the only reason we do that is because it's an easy way to ensure that it's declared before any code accesses it.
there's a way to create the same guarantee (declared before access), but without the downsides of "everything's in reverse".
my proposal is "a block to put allocas into". we'd emit allocas into the end of that block (which fixes the ordering). this block would be the first block that runs in our probe. it neatly separates "hoisted declarations" and "everything else" into different, labelled blocks.
the reason I'm raising this is because I believe it's one piece needed for "big strings".
Beta Was this translation helpful? Give feedback.
All reactions