As of now, we're purely reading loops, but aren't changing anything about/in the IR.
We have to attach 'vectorise this' tags and not merely read. In this post, we'll just be practicing the metadata node construction in isolation and not attach them to any loops detected.
What I built: Commit ad7ad71
What I understood:
1) Metadata:
- The metadata nodes in IR live in memory. So when we attach metadata to a branch, we're just setting a pointer.
- As we know, a module is a container for everything LLVM knows about a file.
- All these objects (like functions, basic blocks, instructions, types, constants, metadata, etc.) need to be stored somewhere in the computer's memory as LLVM works on them. That somewhere is the
LLVMContext.
2) LLVMContext:
- It's a shared memory area that owns/manages all the fundamental building blocks. It is especially helpful with "interning."
- When different functions use the same type object, they don't have their own copies as they point to the same object in the context.
- So if we create the same metadata node twice, LLVM checks the context's intern table and returns the existing one.
- It is important for any MDNode call to take the context as an argument because we could have MDNodes created in a different context but being put in a different one. This can cause LLVM to crash or give corrupt IR.
LLVMContext &Ctx = F.getContext();
- This fetches the context the function belongs to, so the metadata node we attach belongs to the same memory area as the function it's being attached to.
3) Creating hint operands:
MDString *HintName = MDString::get(Ctx, "llvm.loop.vectorize.enable");
ConstantInt *TrueVal = ConstantInt::get(Type::getInt1Ty(Ctx), 1);
ValueAsMetadata *TrueMD = ValueAsMetadata::get(TrueVal);
- This creates an LLVM-native string that can live inside a metadata node. LLVM looks for
"llvm.loop.vectorize.enable"before deciding whether or not to vectorise a loop. - Then, we create the LLVM IR equivalent of the
booltype, so we can set it to true, hinting that the loop must be vectorised. We fetch the 1-bit integer type from the context and create an actual constant value of this type, holding the number 1. - Because
MDNode::get()expects its operands to beMetadata*andConstantIntis aValue*, it literally cannot be passed whereMetadata*is expected. - So we use
ValueAsMetadataas the bridge class to promoteValue*into aMetadata*so the type system is satisfied.
4) Building the node:
- We now assemble the metadata node, which is a list containing two operands: the name string and the true value.
- It results in the in-memory equivalent of the IR text:
!{!"llvm.loop.vectorize.enable", i1 true}
5) Self-referencing:
- LLVM needs a way to uniquely identify an exact loop using a metadata node. And for this, the loop ID's node must list itself as its own first operand, like so:
!x = !{!x, !y}
- This self-referencing helps LLVM distinguish a "loop ID node" from any other ordinary metadata node.
MDNode *TempLoopID = MDNode::getTemporary(Ctx, {}).release();
MDNode *LoopID = MDNode::get(Ctx, {TempLoopID, VectorizeHint});
TempLoopID->replaceAllUsesWith(LoopID);
MDNode::deleteTemporary(TempLoopID);
- We first create an empty (temporary) placeholder node with no real operands.
-
release()is for manually deleting a smart pointer, that would otherwise have been deleted automatically throughgetTemporary. - Then, we build the actual loop ID node, with its first operand being the placeholder node.
- We then replace all instances of the temporary node with the newly created "actual"
LoopIDnode. This is how we create a self-referencing node. - Finally, since we took manual ownership of the placeholder, we destroy it to prevent memory leakage.
What's next: Attach the created metadata to the actual for block.
Musings:
I have the cutest little peace lily. Her name is Lulu, and she brings me immense joy. I read and sing to her, play the piano for her sometimes, and tell her just about everything. As crazy as it might sound, I think she understands and responds. I once read that plants respond incredibly well to chatting and affection in general. And I totally believe it. After a long day's work, being welcomed by the greenest and freshest of leaves, and the prettiest of flowers, sure does bring one a lot of peace.


Top comments (0)