Best way to pass an implicit parameter

Hi,

I am trying to add a new parameter to functions which have a specific attribute in Clang, however I am unsure of the best way to do this.

For context, I am trying to add support for SME functions to Clang. The SME ABI requires passing in some state in X0 (modelled in LLVM with an intrinsic call) for certain functions. To achieve this, I am trying to add an implicit parameter before any other arguments (including ‘this’, if present).

I found that there is an ImplicitParamDecl class which is used in various places in lib/CodeGen to add new arguments. I’ve had some success in pushing the new parameter in various places in CGCall (arrangeCXXMethodType, arrangeFreeFunctionType, etc), though I am not sure if this is the best place to add it.

I believe my two options are to either:

  1. Add the new ImplicitParamDecl to the AST.
  2. Add the ImplicitParamDecl during CodeGen as described above, similar to what seems to be done in CGCXXABI for the C++ ‘this’ parameter.

Does anyone have suggestions on the best approach?

Thanks,
Kerry

(Pull request for alpha-level ABI support for SME: https://github.com/ARM-software/abi-aa/pull/123)

Adding the parameter during CodeGen like you’re doing seems okay. Adding it to the function type would be a nightmare; you don’t want to touch that. (You could attach the ImplicitParamDecl to the AST, I guess, but that isn’t much different from what you’re doing.)

I guess alternatively, you could just insert in the backend, and avoid modelling it in IR at all. But maybe we want to let target-independent optimizations hack on the current_state? I haven’t tried to figure out if there’s any other reason we want to avoid optimizing the parameters to SME functions.

To clarify, you don’t want to add it as an explicit argument in the AST. You might need to mark the fact that a function is an SME function on the function type, so it’s possible to declare a pointer to an SME function.

Thank you for answering this, @efriedma-quic!