Which LLVM-IR function attribute (if any) do I use to promise that a function reads/writes only to its own stack frame?

Viewed 93

Looking at the LLVM-IR documentation on function attributes, I've been unable to parse out how I can use a function attribute to promise to LLVM that a function will alloca a stack frame for itself and mutate that stack frame, but will not read or write any other memory. I think that's what inaccessiblememonly means by

the function may only access memory that is not accessible by the module being compiled

but I'm unsure. From one low-level perspective, the stack is accessible everywhere, since other functions may alloca the same memory, but from another slightly more abstracted perspective, the regions alloca'd by a function are inaccessible except through the pointer returned by alloca, which in my case will not be passed to downstream functions.

So, is the following a legal use of inaccessiblememonly, ignoring its obvious inefficiency compared to a no-stack version which uses insertvalue?

define { i64, i32 } @foo ({ i64, i32 } %bar, i32 %baz) inaccessiblememonly {
  ;; save bar to the stack
  %bar-save = alloca { i64, i32 }
  store { i64, i32 } %bar, { i64, i32 }* %bar-save
  
  ;; overwrite its second field with baz
  %bar-second = getelementptr { i64, i32 }, { i64, i32 }* %bar-save, i32 0, i32 1
  store i32 %baz, i32* %bar-second

  ;; return the modified struct
  %bar-val = load { i64, i32 }, { i64, i32 }* %bar-save
  ret { i64, i32 } %bar-val
}
0 Answers
Related