Variable attribute annotating LLVM IR with metadata

Hi all,

I'm currently looking for a way to propagate a clang attribute applied
to a variable through to LLVM such that any generated LLVM IR
instruction which touched that variable could be annotated with a piece
of metadata.

Currently, I am achieving this by creating a type attribute and adding a
new qualifier to the types. I then examine this type qualifier during
CodeGen to generate the metadata. Doing it this way requires changing a
lot of places in CodeGen (any point which an instruction is generated
may require metadata), and it also means that the type qualifier has to
be preserved through all of the operations.

As an example of what I'm trying to achieve, given the following C code:

int foo(int ATTRIBUTE a, int b) {
  int c = a + b;
  return c;

I am aiming for the following IR to be generated:

define i16 @foo(i16 %a, i16 %b) {
  %a.addr = alloca i16, align 2
  %b.addr = alloca i16, align 2
  %c = alloca i16, align 2
  store i16 %a, i16* %a.addr, align 2, !MYATTRIBUTE !0
  store i16 %b, i16* %b.addr, align 2
  %0 = load i16, i16* %a.addr, align 2, !MYATTRIBUTE !0
  %1 = load i16, i16* %b.addr, align 2
  %add = add nsw i16 %0, %1, !MYATTRIBUTE !0
  store i16 %add, i16* %c, align 2
  %2 = load i16, i16* %c, align 2
  ret i16 %2

However, if I instead had the following:

int foo(int a, int b) {
  int ATTRIBUTE c = a + b;
  return c;

or even

int foo(int a, int b) {
  return (int ATTRIBUTE)(a + b);

I would want the same attribute to appear on the add, the only
difference being the attribute not showing on a's alloca (and c's alloca
being non-existent in the last example).

Is using a type attribute the appropriate way to do this, or is there a
better way? Is there a way to achieve this which wouldn't require a
large number of changes to generic code?

Thank you,
Edward Jones