Metadata

But that's not even a grammar.

I simply want to know what the syntax is for specifying metadata on an
instruction. From what I gather, it is this:

<instruction> ',' <MetadataSpec>

<MetadataSpec>: '!' <ID> '!' <ID>

<ID>: [a-zA-Z_][a-zA-Z0-9_]*

And the second <ID> needs to be the name of a metadata node:

'!' <ID> '=' METADATA '!' '{' <MDNodeList> '}'

And MDNodeList contains a list of stuff like integer values, strings and so
on.

Is that basically right? So I would have to do this:

  %r8 = load <2 x double>* %r6, align 16, !nontemporal !0
[...]
!0 = metadata !{ i32 1 }

I would really rather not have to specify the entirely redundant !0. Just the
fact that the instruction has metadata with index/name "nontemporal" should
be enough.

                                                    -Dave

Ok, this sounds right, but this look wrong:

/// Abstact virtual class for operations for memory operations
class MemSDNode : public SDNode {
[...]
  bool isVolatile() const { return (SubclassData >> 5) & 1; }

Shouldn't that be MMO->isVolatile()?

                                             -Dave

I give up! What is the syntax for attaching metadata to instructions? The
documentation is very unclear.

Some examples are in llvm/test/Feature/md_on_instruction.ll

Or you could just compile a file with -g.

And then fix the documentation!

That's on my TODO list for 2.7, unless someone beats me writing this doc :slight_smile:

Here !1 is just an example metadata which has 4 values.

!1 attached to load instruction is an MDNode, which is a collection of
llvm::Values.

I don't think the lang ref metadata grammar section
(http://llvm.org/docs/LangRef.html#metadata) has been fully updated with
how !dbg metadata is used in
http://llvm.org/docs/SourceLevelDebugging.html. At least to me it is not
clear. In my mind I translate the phrase "LLVM IR allows metadata to be
attached to instructions " into a grammar depicted in
SourceLevelDebugging.html. I was going to ask about this, but I'll let you
instead. :slight_smile:

But that's not even a grammar.

I simply want to know what the syntax is for specifying metadata on an
instruction. From what I gather, it is this:

<instruction> ',' <MetadataSpec>

<MetadataSpec>: '!' <ID> '!' <ID>

<ID>: [a-zA-Z_][a-zA-Z0-9_]*

And the second <ID> needs to be the name of a metadata node:

'!' <ID> '=' METADATA '!' '{' <MDNodeList> '}'

And MDNodeList contains a list of stuff like integer values, strings and so
on.

Is that basically right? So I would have to do this:

   %r8 = load &lt;2 x double&gt;\* %r6, align 16, \!nontemporal \!0

[...]
!0 = metadata !{ i32 1 }

I guess, that's what I described in the example in previous email :).
In this case, your metadata is just an integer one. In my example, my
metadata was a collection of 4 values.

While hacking around in the SelectionDAG build code, I've made the
isVolatile, (new) isNonTemporal and Alignment parameters to
SelectionDAG::getLoad/getStore and friends non-default.

I've already caught one bug in the XCore backend by doing this:

    if (Offset % 4 == 0) {
      // We've managed to infer better alignment information than the load
      // already has. Use an aligned load.
      return DAG.getLoad(getPointerTy(), dl, Chain, BasePtr, NULL, 4,
                         false, false, 0);
    }

Whoops! There's no alignment info being set here!

Is there any reason to keep these as default arguments? It invites silent
coding errors. I did this because I have to propagate the non-temporal
information through various phases of lowering and making these non-default
helps me catch missing cases.

We've done the same in our local sources and we've never found
specifying the extra arguments to be burdensome.

                                          -Dave

Dan,

Sharing only happens when two instructions have the "same" memory
reference info. You just need to make sure that the non-temporal
flag is significant. It's not fundamentally different from the
volatile flag in this respect.

Metadata is used to monitor values from the sideline. It should not
influence optimization or code gen in general other then the intended
recipient of the metadata info. So your suggestion is not a good idea
for all kind of metadata in general. Just a note.

I don't think the lang ref metadata grammar section
(http://llvm.org/docs/LangRef.html#metadata) has been fully updated with
how !dbg metadata is used in
http://llvm.org/docs/SourceLevelDebugging.html. At least to me it is not
clear. In my mind I translate the phrase "LLVM IR allows metadata to be
attached to instructions " into a grammar depicted in
SourceLevelDebugging.html. I was going to ask about this, but I'll let you
instead. :slight_smile:

But that's not even a grammar.

I simply want to know what the syntax is for specifying metadata on an
instruction. From what I gather, it is this:

<instruction> ',' <MetadataSpec>

<MetadataSpec>: '!' <ID> '!' <ID>

<ID>: [a-zA-Z_][a-zA-Z0-9_]*

And the second <ID> needs to be the name of a metadata node:

'!' <ID> '=' METADATA '!' '{' <MDNodeList> '}'

And MDNodeList contains a list of stuff like integer values, strings and so
on.

Is that basically right? So I would have to do this:

   %r8 = load &lt;2 x double&gt;\* %r6, align 16, \!nontemporal \!0

[...]
!0 = metadata !{ i32 1 }

I guess, that's what I described in the example in previous email :).
In this case, your metadata is just an integer one. In my example, my
metadata was a collection of 4 values.

So David would write what he wants as:

  %r8 = load <2 x double>* %r6, align 16, !nontemporal !{ i32 1 }

?

This is what a call, inlined metadata. The asm parser at the moment
won't handle this. It expects stand alone metadata.

Is that basically right? So I would have to do this:

  %r8 = load <2 x double>* %r6, align 16, !nontemporal !0
[...]
!0 = metadata !{ i32 1 }

Yes.

I would really rather not have to specify the entirely redundant !0. Just the
fact that the instruction has metadata with index/name "nontemporal" should
be enough.

Why? These are uniqued anyway, it won't matter for performance and "!nontermporal !0" is not substantially worse than "!nontermporal". I'd rather keep the metadata model simple than optimize it for nullary metadata.

-Chris

I was talking about MachineMemOperands in the above, not metadata.

But that does suggest a consideration: if you're using metadata at
the LLVM IR level, it may make sense to use metadata at the codegen
level too, to avoid this confusion.

Dan

It's not a bug; the code could be written either way. There's actually
an assert in MemSDNode's constructor which checks that they're the
same. I believe the code is structured this way because it makes it
easy to lump the volatile flag in with other data which is
significant for CSE purposes, but it's not critical that it work
that way.

Dan

There doesn't appear to be any way to do this right now. Is it planned?

                                                     -Dave

It just seems wordy, but I don't have a big issue with it if it makes other
things simpler.

                                                      -Dave

Ah, I see that now. Will I need to do the same with the NonTemporal
flag to make it "significant?"

It's a bit jarring to see the other boolean operations work through the
MMO and then see isVolatile mess with SubclassData.

                                                 -Dave

I have a patch ready to track nontemporal semantics but I'm waiting for an
opinion on this. I'd really like to get this integrated for 2.7. In my
opinion default arguments are a bad idea in this context. It's very easy
to make coding errors. I found a couple more today.

Thanks.

                                                -Dave

We had a similar problem in the ARM backend. You get correct code, but
suboptimal scheduling.

deep

FWIW, I'm happy with removing default arguments. I'm not happy with
long strings of simply-typed arguments like "NULL, 4, false, false, 0"
that don't give a reader any indication of what the arguments mean,
but that's trickier to fix.

I agree. It's not all that hard to implement. It's just giving a name to a
constant. Doing strong typechecking would be even better but that
requires wrapper classes.

                                                 -Dave

Today, we use metadata to represent debug info at LLVM IR level as
well at the codegen level.